(rootDir: string, sourceId: string, targetId: string, relation: RelationType, weight?: number, metadata?: Record<string, string>)
| 153 | } |
| 154 | |
| 155 | export async function createRelation(rootDir: string, sourceId: string, targetId: string, relation: RelationType, weight?: number, metadata?: Record<string, string>): Promise<MemoryEdge | null> { |
| 156 | const graph = await loadGraph(rootDir); |
| 157 | if (!graph.nodes[sourceId] || !graph.nodes[targetId]) return null; |
| 158 | |
| 159 | const duplicate = Object.values(graph.edges).find(e => |
| 160 | e.source === sourceId && e.target === targetId && e.relation === relation |
| 161 | ); |
| 162 | if (duplicate) { |
| 163 | duplicate.weight = weight ?? duplicate.weight; |
| 164 | if (metadata) Object.assign(duplicate.metadata, metadata); |
| 165 | scheduleSave(rootDir); |
| 166 | return duplicate; |
| 167 | } |
| 168 | |
| 169 | const edge: MemoryEdge = { |
| 170 | id: generateId("me"), |
| 171 | source: sourceId, |
| 172 | target: targetId, |
| 173 | relation, |
| 174 | weight: weight ?? 1.0, |
| 175 | createdAt: Date.now(), |
| 176 | metadata: metadata ?? {}, |
| 177 | }; |
| 178 | graph.edges[edge.id] = edge; |
| 179 | scheduleSave(rootDir); |
| 180 | return edge; |
| 181 | } |
| 182 | |
| 183 | export async function searchGraph(rootDir: string, query: string, maxDepth: number = 1, topK: number = 5, edgeFilter?: RelationType[]): Promise<GraphSearchResult> { |
| 184 | const graph = await loadGraph(rootDir); |
no test coverage detected