import type { DatabaseSync } from 'node:sqlite'; import { CanonService } from './CanonService.js'; import { FrontierService } from './FrontierService.js'; export type ConsolidationInput = { projectId: string; title: string; body: string; sourceEntryIds: string[]; createdBy?: string; }; export class ConsolidationService { private readonly canon: CanonService; private readonly frontier: FrontierService; constructor(db: DatabaseSync) { this.canon = new CanonService(db); this.frontier = new FrontierService(db); } prepare(input: ConsolidationInput) { const title = input.title.trim(); const body = input.body.trim(); const sourceEntryIds = [...new Set(input.sourceEntryIds)]; if (!title || !body) throw new Error('A consolidation needs a title and reviewed content.'); if (!sourceEntryIds.length) throw new Error('A consolidation needs at least one source canon entry.'); const sources = sourceEntryIds.map((id) => this.canon.get(id)); if (sources.some((entry) => !entry || entry.status !== 'active' || entry.projectId !== input.projectId)) { throw new Error('Every consolidation source must be an active canon entry in the same project.'); } return this.frontier.create({ projectId: input.projectId, domain: 'knowledge-consolidation', itemType: 'research', title, description: body, rationale: 'Prepared from reviewed canon sources; pending logic and governance review.', metadata: { sourceEntryIds, createdBy: input.createdBy ?? null } }); } }