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

Function createRelation

src/core/memory-graph.ts:155–181  ·  view source on GitHub ↗
(rootDir: string, sourceId: string, targetId: string, relation: RelationType, weight?: number, metadata?: Record<string, string>)

Source from the content-addressed store, hash-verified

153}
154
155export 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
183export async function searchGraph(rootDir: string, query: string, maxDepth: number = 1, topK: number = 5, edgeFilter?: RelationType[]): Promise<GraphSearchResult> {
184 const graph = await loadGraph(rootDir);

Callers 3

toolCreateRelationFunction · 0.85
addInterlinkedContextFunction · 0.85

Calls 3

loadGraphFunction · 0.85
scheduleSaveFunction · 0.85
generateIdFunction · 0.85

Tested by

no test coverage detected