()
| 69 | constructor(private memoryFilePath: string) {} |
| 70 | |
| 71 | private async loadGraph(): Promise<KnowledgeGraph> { |
| 72 | try { |
| 73 | const data = await fs.readFile(this.memoryFilePath, "utf-8"); |
| 74 | const lines = data.split("\n").filter(line => line.trim() !== ""); |
| 75 | return lines.reduce((graph: KnowledgeGraph, line) => { |
| 76 | const item = JSON.parse(line); |
| 77 | if (item.type === "entity") { |
| 78 | graph.entities.push({ |
| 79 | name: item.name, |
| 80 | entityType: item.entityType, |
| 81 | observations: item.observations |
| 82 | }); |
| 83 | } |
| 84 | if (item.type === "relation") { |
| 85 | graph.relations.push({ |
| 86 | from: item.from, |
| 87 | to: item.to, |
| 88 | relationType: item.relationType |
| 89 | }); |
| 90 | } |
| 91 | return graph; |
| 92 | }, { entities: [], relations: [] }); |
| 93 | } catch (error) { |
| 94 | if (error instanceof Error && 'code' in error && (error as any).code === "ENOENT") { |
| 95 | return { entities: [], relations: [] }; |
| 96 | } |
| 97 | throw error; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private async saveGraph(graph: KnowledgeGraph): Promise<void> { |
| 102 | const lines = [ |
no outgoing calls
no test coverage detected