(rootDir: string, items: Array<{ type: NodeType; label: string; content: string; metadata?: Record<string, string> }>, autoLink: boolean = true)
| 271 | } |
| 272 | |
| 273 | export async function addInterlinkedContext(rootDir: string, items: Array<{ type: NodeType; label: string; content: string; metadata?: Record<string, string> }>, autoLink: boolean = true): Promise<{ nodes: MemoryNode[]; edges: MemoryEdge[] }> { |
| 274 | const createdNodes: MemoryNode[] = []; |
| 275 | for (const item of items) { |
| 276 | createdNodes.push(await upsertNode(rootDir, item.type, item.label, item.content, item.metadata)); |
| 277 | } |
| 278 | |
| 279 | const createdEdges: MemoryEdge[] = []; |
| 280 | |
| 281 | if (autoLink && createdNodes.length > 1) { |
| 282 | for (let i = 0; i < createdNodes.length; i++) { |
| 283 | for (let j = i + 1; j < createdNodes.length; j++) { |
| 284 | const similarity = cosine(createdNodes[i].embedding, createdNodes[j].embedding); |
| 285 | if (similarity >= SIMILARITY_THRESHOLD) { |
| 286 | const edge = await createRelation(rootDir, createdNodes[i].id, createdNodes[j].id, "similar_to", similarity); |
| 287 | if (edge) createdEdges.push(edge); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | const graph = await loadGraph(rootDir); |
| 294 | const existingNodes = Object.values(graph.nodes) |
| 295 | .filter(n => !createdNodes.find(cn => cn.id === n.id)) |
| 296 | .slice(0, 200); |
| 297 | if (autoLink) { |
| 298 | for (const newNode of createdNodes) { |
| 299 | for (const existing of existingNodes) { |
| 300 | const similarity = cosine(newNode.embedding, existing.embedding); |
| 301 | if (similarity >= SIMILARITY_THRESHOLD) { |
| 302 | const edge = await createRelation(rootDir, newNode.id, existing.id, "similar_to", similarity); |
| 303 | if (edge) createdEdges.push(edge); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return { nodes: createdNodes, edges: createdEdges }; |
| 310 | } |
| 311 | |
| 312 | export async function retrieveWithTraversal(rootDir: string, startNodeId: string, maxDepth: number = 2, edgeFilter?: RelationType[]): Promise<TraversalResult[]> { |
| 313 | const graph = await loadGraph(rootDir); |
no test coverage detected