File size: 1,569 Bytes
a8f4b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 }
    });
  }
}