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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 6x 6x 6x 6x 6x 6x 6x | import { Injectable, Logger } from '@nestjs/common';
import { ContextTemplatesService } from '../context-templates/context-templates.service';
import * as path from 'path';
import { glob } from 'glob';
import { ContextTemplate } from '../core-entities';
@Injectable()
export class ContextTemplateSeedingService {
private readonly logger = new Logger(ContextTemplateSeedingService.name);
constructor(
private readonly contextTemplatesService: ContextTemplatesService,
) {}
async seed() {
const existingTemplates = await this.contextTemplatesService.findAll();
const existingTemplatesMap = new Map<string, ContextTemplate>();
existingTemplates.forEach((t) =>
existingTemplatesMap.set(t.template_name, t),
);
this.logger.log('Syncing context templates from files...');
const seedFiles = await glob(
path.join(__dirname, 'data', 'context-templates', '*.{ts,js}'),
);
Iif (seedFiles.length === 0) {
this.logger.warn('No context template seed files found.');
return;
}
for (const filePath of seedFiles) {
try {
const templateModule = await import(filePath);
const templateName = templateModule.template_name;
const templateContent = templateModule.template_content;
const fileName = path.basename(filePath);
Iif (!templateName || !templateContent || templateContent.length === 0) {
this.logger.warn(`Skipping malformed seed file: ${fileName}`);
continue;
}
// Generate builtin_key from filename (e.g., "default-initial_condensed-project-context.ts" -> "default-initial-condensed-project-context")
const builtinKey = fileName
.replace(/\.(ts|js)$/, '')
.replace(/_/g, '-');
// Find existing by builtin_key (for version updates)
const existingTemplate =
await this.contextTemplatesService.findByBuiltinKey(builtinKey);
if (existingTemplate && existingTemplate.is_builtin) {
// Update existing builtin template (version upgrade)
await this.contextTemplatesService.updateBuiltinContent(
existingTemplate.id,
templateContent,
);
this.logger.log(
`Built-in template "${templateName}" updated (version upgrade).`,
);
} else if (existingTemplate) {
// User-created template with same builtin_key
this.logger.warn(
`Template with builtin_key "${builtinKey}" exists but is not built-in. Skipping.`,
);
} else {
// Check if template with same name exists (legacy compatibility)
const legacyTemplate = existingTemplatesMap.get(templateName);
if (legacyTemplate) {
// Legacy template without builtin_key - leave it alone
this.logger.log(
`Legacy template "${templateName}" exists without builtin_key. Skipping seed.`,
);
} else {
// Create new builtin template
const newTemplate = await this.contextTemplatesService.create({
template_name: templateName,
template_content: templateContent,
is_builtin: true,
builtin_key: builtinKey,
});
if (fileName.startsWith('default-initial_condensed')) {
await this.contextTemplatesService.setDefaultCondensed(
newTemplate.id,
);
this.logger.log(
`Built-in template "${templateName}" seeded and set as default condensed.`,
);
} else if (fileName.startsWith('default-initial_')) {
await this.contextTemplatesService.setDefaultInitial(
newTemplate.id,
);
this.logger.log(
`Built-in template "${templateName}" seeded and set as default initial.`,
);
} else if (fileName.startsWith('default-followup_')) {
await this.contextTemplatesService.setDefaultFollowup(
newTemplate.id,
);
this.logger.log(
`Built-in template "${templateName}" seeded and set as default follow-up.`,
);
} else {
this.logger.log(`Built-in template "${templateName}" seeded.`);
}
}
}
} catch (error) {
this.logger.error(
`Failed to seed context template from ${filePath}`,
error.stack,
);
}
}
}
}
|