MCPcopy Index your code
hub / github.com/forloopcodes/contextplus / addInterlinkedContext

Function addInterlinkedContext

src/core/memory-graph.ts:273–310  ·  view source on GitHub ↗
(rootDir: string, items: Array<{ type: NodeType; label: string; content: string; metadata?: Record<string, string> }>, autoLink: boolean = true)

Source from the content-addressed store, hash-verified

271}
272
273export 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
312export async function retrieveWithTraversal(rootDir: string, startNodeId: string, maxDepth: number = 2, edgeFilter?: RelationType[]): Promise<TraversalResult[]> {
313 const graph = await loadGraph(rootDir);

Callers 2

Calls 4

upsertNodeFunction · 0.85
createRelationFunction · 0.85
loadGraphFunction · 0.85
cosineFunction · 0.70

Tested by

no test coverage detected