|
| 1 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import { dirname } from 'node:path'; |
| 3 | +import type { PlanetEdge, PlanetEdgeType } from './planet-edge-inference.js'; |
| 4 | + |
| 5 | +export interface PlanetEdgeStore { |
| 6 | + listManual(): PlanetEdge[]; |
| 7 | + upsertManual(edge: { fromPlanetId: string; toPlanetId: string; type: PlanetEdgeType; reason?: string }): PlanetEdge; |
| 8 | + removeManual(fromPlanetId: string, toPlanetId: string): boolean; |
| 9 | + getMerged(autoEdges: PlanetEdge[]): PlanetEdge[]; |
| 10 | +} |
| 11 | + |
| 12 | +interface SerializedEdgeFile { |
| 13 | + version: 1; |
| 14 | + manualEdges: PlanetEdge[]; |
| 15 | +} |
| 16 | + |
| 17 | +function normalizeEdge(value: unknown): PlanetEdge | null { |
| 18 | + if (!value || typeof value !== 'object') return null; |
| 19 | + const candidate = value as Partial<PlanetEdge>; |
| 20 | + if ( |
| 21 | + typeof candidate.fromPlanetId !== 'string' || |
| 22 | + typeof candidate.toPlanetId !== 'string' || |
| 23 | + (candidate.type !== 'depends-on' && candidate.type !== 'related-to' && candidate.type !== 'supersedes') |
| 24 | + ) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + return { |
| 29 | + fromPlanetId: candidate.fromPlanetId, |
| 30 | + toPlanetId: candidate.toPlanetId, |
| 31 | + type: candidate.type, |
| 32 | + confidence: typeof candidate.confidence === 'number' ? Math.max(0, Math.min(candidate.confidence, 1)) : 1, |
| 33 | + source: candidate.source === 'auto' ? 'auto' : 'manual', |
| 34 | + reason: typeof candidate.reason === 'string' ? candidate.reason : undefined, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function edgeKey(fromPlanetId: string, toPlanetId: string): string { |
| 39 | + return `${fromPlanetId}::${toPlanetId}`; |
| 40 | +} |
| 41 | + |
| 42 | +export class FilePlanetEdgeStore implements PlanetEdgeStore { |
| 43 | + private readonly manualEdges = new Map<string, PlanetEdge>(); |
| 44 | + |
| 45 | + constructor(private readonly filePath: string) { |
| 46 | + for (const edge of this.readFile()) { |
| 47 | + this.manualEdges.set(edgeKey(edge.fromPlanetId, edge.toPlanetId), edge); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + listManual(): PlanetEdge[] { |
| 52 | + return [...this.manualEdges.values()].map((edge) => structuredClone(edge)); |
| 53 | + } |
| 54 | + |
| 55 | + upsertManual(edge: { fromPlanetId: string; toPlanetId: string; type: PlanetEdgeType; reason?: string }): PlanetEdge { |
| 56 | + const nextEdge: PlanetEdge = { |
| 57 | + fromPlanetId: edge.fromPlanetId, |
| 58 | + toPlanetId: edge.toPlanetId, |
| 59 | + type: edge.type, |
| 60 | + confidence: 1, |
| 61 | + source: 'manual', |
| 62 | + reason: edge.reason, |
| 63 | + }; |
| 64 | + this.manualEdges.set(edgeKey(nextEdge.fromPlanetId, nextEdge.toPlanetId), nextEdge); |
| 65 | + this.persist(); |
| 66 | + return structuredClone(nextEdge); |
| 67 | + } |
| 68 | + |
| 69 | + removeManual(fromPlanetId: string, toPlanetId: string): boolean { |
| 70 | + const removed = this.manualEdges.delete(edgeKey(fromPlanetId, toPlanetId)); |
| 71 | + if (removed) this.persist(); |
| 72 | + return removed; |
| 73 | + } |
| 74 | + |
| 75 | + getMerged(autoEdges: PlanetEdge[]): PlanetEdge[] { |
| 76 | + const merged = new Map<string, PlanetEdge>(); |
| 77 | + |
| 78 | + for (const edge of autoEdges) { |
| 79 | + merged.set(edgeKey(edge.fromPlanetId, edge.toPlanetId), structuredClone(edge)); |
| 80 | + } |
| 81 | + |
| 82 | + for (const edge of this.manualEdges.values()) { |
| 83 | + merged.set(edgeKey(edge.fromPlanetId, edge.toPlanetId), structuredClone(edge)); |
| 84 | + } |
| 85 | + |
| 86 | + return [...merged.values()].sort((left, right) => { |
| 87 | + if (left.source !== right.source) return left.source === 'manual' ? -1 : 1; |
| 88 | + return right.confidence - left.confidence; |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private persist(): void { |
| 93 | + const data: SerializedEdgeFile = { |
| 94 | + version: 1, |
| 95 | + manualEdges: [...this.manualEdges.values()] |
| 96 | + .map((edge) => normalizeEdge(edge)) |
| 97 | + .filter((edge): edge is PlanetEdge => Boolean(edge)) |
| 98 | + .sort((left, right) => edgeKey(left.fromPlanetId, left.toPlanetId).localeCompare(edgeKey(right.fromPlanetId, right.toPlanetId))), |
| 99 | + }; |
| 100 | + |
| 101 | + mkdirSync(dirname(this.filePath), { recursive: true }); |
| 102 | + writeFileSync(this.filePath, JSON.stringify(data, null, 2), 'utf-8'); |
| 103 | + } |
| 104 | + |
| 105 | + private readFile(): PlanetEdge[] { |
| 106 | + if (!existsSync(this.filePath)) return []; |
| 107 | + |
| 108 | + try { |
| 109 | + const raw = readFileSync(this.filePath, 'utf-8'); |
| 110 | + const parsed = JSON.parse(raw) as SerializedEdgeFile | PlanetEdge[]; |
| 111 | + const edges = Array.isArray(parsed) |
| 112 | + ? parsed |
| 113 | + : Array.isArray(parsed.manualEdges) |
| 114 | + ? parsed.manualEdges |
| 115 | + : []; |
| 116 | + |
| 117 | + return edges |
| 118 | + .map((edge) => normalizeEdge(edge)) |
| 119 | + .filter((edge): edge is PlanetEdge => Boolean(edge)) |
| 120 | + .map((edge) => ({ ...edge, source: 'manual', confidence: 1 })); |
| 121 | + } catch { |
| 122 | + return []; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments