Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 6x 6x 6x 6x 6x 6x 6x | import { Injectable, Logger } from '@nestjs/common';
import { SystemPromptsService } from '../system-prompts/system-prompts.service';
import * as path from 'path';
import { glob } from 'glob';
import { SystemPrompt } from '../core-entities';
@Injectable()
export class SystemPromptSeedingService {
private readonly logger = new Logger(SystemPromptSeedingService.name);
constructor(private readonly systemPromptsService: SystemPromptsService) {}
async seed() {
const existingPrompts = await this.systemPromptsService.findAll();
const existingPromptsMap = new Map<string, SystemPrompt>();
existingPrompts.forEach((p) => existingPromptsMap.set(p.prompt_name, p));
this.logger.log('Syncing system prompts from files...');
const seedFiles = await glob(
path.join(__dirname, 'data', 'system-prompts', '*.{ts,js}'),
);
Iif (seedFiles.length === 0) {
this.logger.warn('No system prompt seed files found.');
return;
}
for (const filePath of seedFiles) {
try {
const promptModule = await import(filePath);
const promptName = promptModule.name;
const promptContent = promptModule.content;
const enabledTools = promptModule.enabled_tools || 'all';
const enabledMcpTools = promptModule.enabled_mcp_tools;
const fileName = path.basename(filePath);
Iif (!promptName || !promptContent || promptContent.length === 0) {
this.logger.warn(`Skipping malformed seed file: ${fileName}`);
continue;
}
// Generate builtin_key from filename (e.g., "default_native_tool_agent.ts" -> "default-native-tool-agent")
const builtinKey = fileName
.replace(/\.(ts|js)$/, '')
.replace(/_/g, '-');
// Find existing by builtin_key (for version updates)
const existingPrompt =
await this.systemPromptsService.findByBuiltinKey(builtinKey);
if (existingPrompt && existingPrompt.is_builtin) {
// Update existing builtin prompt (version upgrade)
await this.systemPromptsService.updateBuiltinContent(
existingPrompt.id,
promptContent,
enabledTools,
enabledMcpTools !== undefined ? enabledMcpTools : undefined,
);
this.logger.log(
`Built-in prompt "${promptName}" updated (version upgrade).`,
);
} else if (existingPrompt) {
// User-created prompt with same builtin_key (shouldn't happen due to unique constraint)
this.logger.warn(
`Prompt with builtin_key "${builtinKey}" exists but is not built-in. Skipping.`,
);
} else {
// Check if prompt with same name exists (legacy compatibility)
const legacyPrompt = existingPromptsMap.get(promptName);
if (legacyPrompt) {
// Legacy prompt without builtin_key - leave it alone
this.logger.log(
`Legacy prompt "${promptName}" exists without builtin_key. Skipping seed.`,
);
} else {
// Create new builtin prompt
const newPrompt = await this.systemPromptsService.create({
prompt_name: promptName,
prompt_content: promptContent,
enabledTools: enabledTools,
enabledMcpTools:
enabledMcpTools !== undefined ? enabledMcpTools : 'all',
is_builtin: true,
builtin_key: builtinKey,
});
if (fileName.startsWith('default_')) {
await this.systemPromptsService.setDefault(newPrompt.id);
this.logger.log(
`Built-in prompt "${promptName}" seeded and set as default.`,
);
} else {
this.logger.log(`Built-in prompt "${promptName}" seeded.`);
}
}
}
} catch (error) {
this.logger.error(
`Failed to seed system prompt from ${filePath}`,
error.stack,
);
}
}
}
} |