UNPKG

manifest

Version:

Self-hosted Manifest LLM router with embedded server, SQLite database, and dashboard

180 lines 7.74 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var DatabaseSeederService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseSeederService = void 0; const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const typeorm_1 = require("@nestjs/typeorm"); const typeorm_2 = require("typeorm"); const auth_instance_1 = require("../auth/auth.instance"); const tenant_entity_1 = require("../entities/tenant.entity"); const agent_entity_1 = require("../entities/agent.entity"); const agent_api_key_entity_1 = require("../entities/agent-api-key.entity"); const api_key_entity_1 = require("../entities/api-key.entity"); const agent_message_entity_1 = require("../entities/agent-message.entity"); const hash_util_1 = require("../common/utils/hash.util"); const seed_messages_1 = require("./seed-messages"); const SEED_API_KEY = 'dev-api-key-manifest-001'; const SEED_OTLP_KEY = 'mnfst_dev-otlp-key-001'; const SEED_TENANT_ID = 'seed-tenant-001'; const SEED_AGENT_ID = 'seed-agent-001'; let DatabaseSeederService = DatabaseSeederService_1 = class DatabaseSeederService { dataSource; configService; tenantRepo; agentRepo; agentKeyRepo; apiKeyRepo; messageRepo; logger = new common_1.Logger(DatabaseSeederService_1.name); constructor(dataSource, configService, tenantRepo, agentRepo, agentKeyRepo, apiKeyRepo, messageRepo) { this.dataSource = dataSource; this.configService = configService; this.tenantRepo = tenantRepo; this.agentRepo = agentRepo; this.agentKeyRepo = agentKeyRepo; this.apiKeyRepo = apiKeyRepo; this.messageRepo = messageRepo; } async onModuleInit() { if (this.configService.get('app.manifestMode') === 'local') return; await this.runBetterAuthMigrations(); const env = this.configService.get('app.nodeEnv', 'production'); const seedData = this.configService.get('SEED_DATA'); const shouldSeed = (env === 'development' || env === 'test') && seedData === 'true'; if (shouldSeed) { await this.seedAdminUser(); await this.seedApiKey(); await this.seedTenantAndAgent(); await this.seedAgentMessages(); this.logger.log('Seeded demo data (dev/test only, SEED_DATA=true)'); this.logger.warn('SECURITY: Default seed credentials are active (admin@manifest.build). Do NOT use in production.'); } } async runBetterAuthMigrations() { const ctx = await auth_instance_1.auth.$context; await ctx.runMigrations(); } async seedAdminUser() { const existing = await this.checkBetterAuthUser('admin@manifest.build'); if (existing) return; await auth_instance_1.auth.api.signUpEmail({ body: { email: 'admin@manifest.build', password: 'manifest', name: 'Admin', }, }); await this.dataSource.query(`UPDATE "user" SET "emailVerified" = true WHERE email = $1`, [ 'admin@manifest.build', ]); } async checkBetterAuthUser(email) { try { const rows = await this.dataSource.query(`SELECT id FROM "user" WHERE email = $1`, [email]); return rows.length > 0; } catch (err) { this.logger.warn(`Failed to check Better Auth user: ${err instanceof Error ? err.message : err}`); return false; } } async seedApiKey() { const count = await this.apiKeyRepo.count({ where: { id: 'seed-api-key-001' } }); if (count > 0) return; const userId = await this.getAdminUserId(); if (!userId) return; await this.apiKeyRepo.insert({ id: 'seed-api-key-001', key: null, key_hash: (0, hash_util_1.hashKey)(SEED_API_KEY), key_prefix: (0, hash_util_1.keyPrefix)(SEED_API_KEY), user_id: userId, name: 'Development API Key', }); } async getAdminUserId() { try { const rows = await this.dataSource.query(`SELECT id FROM "user" WHERE email = $1`, [ 'admin@manifest.build', ]); return rows.length > 0 ? String(rows[0].id) : null; } catch (err) { this.logger.warn(`Failed to get admin user ID: ${err instanceof Error ? err.message : err}`); return null; } } async seedTenantAndAgent() { const count = await this.tenantRepo.count({ where: { id: SEED_TENANT_ID } }); if (count > 0) return; const userId = await this.getAdminUserId(); if (!userId) return; await this.tenantRepo.insert({ id: SEED_TENANT_ID, name: userId, organization_name: 'Demo Organization', email: 'admin@manifest.build', is_active: true, }); await this.agentRepo.insert({ id: SEED_AGENT_ID, name: 'demo-agent', description: 'Default development agent', is_active: true, tenant_id: SEED_TENANT_ID, }); await this.agentKeyRepo.insert({ id: 'seed-otlp-key-001', key: null, key_hash: (0, hash_util_1.hashKey)(SEED_OTLP_KEY), key_prefix: (0, hash_util_1.keyPrefix)(SEED_OTLP_KEY), label: 'Demo OTLP ingest key', tenant_id: SEED_TENANT_ID, agent_id: SEED_AGENT_ID, is_active: true, }); this.logger.log(`Seeded tenant/agent with OTLP key: ${SEED_OTLP_KEY.substring(0, 8)}***`); } async seedAgentMessages() { const userId = await this.getAdminUserId(); if (!userId) return; await (0, seed_messages_1.seedAgentMessages)(this.messageRepo, userId, this.logger); } }; exports.DatabaseSeederService = DatabaseSeederService; exports.DatabaseSeederService = DatabaseSeederService = DatabaseSeederService_1 = __decorate([ (0, common_1.Injectable)(), __param(2, (0, typeorm_1.InjectRepository)(tenant_entity_1.Tenant)), __param(3, (0, typeorm_1.InjectRepository)(agent_entity_1.Agent)), __param(4, (0, typeorm_1.InjectRepository)(agent_api_key_entity_1.AgentApiKey)), __param(5, (0, typeorm_1.InjectRepository)(api_key_entity_1.ApiKey)), __param(6, (0, typeorm_1.InjectRepository)(agent_message_entity_1.AgentMessage)), __metadata("design:paramtypes", [typeorm_2.DataSource, config_1.ConfigService, typeorm_2.Repository, typeorm_2.Repository, typeorm_2.Repository, typeorm_2.Repository, typeorm_2.Repository]) ], DatabaseSeederService); //# sourceMappingURL=database-seeder.service.js.map