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

Function upsertNode

src/core/memory-graph.ts:125–153  ·  view source on GitHub ↗
(rootDir: string, type: NodeType, label: string, content: string, metadata?: Record<string, string>)

Source from the content-addressed store, hash-verified

123}
124
125export async function upsertNode(rootDir: string, type: NodeType, label: string, content: string, metadata?: Record<string, string>): Promise<MemoryNode> {
126 const graph = await loadGraph(rootDir);
127 const existing = Object.values(graph.nodes).find(n => n.label === label && n.type === type);
128
129 if (existing) {
130 existing.content = content;
131 existing.lastAccessed = Date.now();
132 existing.accessCount++;
133 if (metadata) Object.assign(existing.metadata, metadata);
134 existing.embedding = (await fetchEmbedding(`${label} ${content}`))[0];
135 scheduleSave(rootDir);
136 return existing;
137 }
138
139 const node: MemoryNode = {
140 id: generateId("mn"),
141 type,
142 label,
143 content,
144 embedding: (await fetchEmbedding(`${label} ${content}`))[0],
145 createdAt: Date.now(),
146 lastAccessed: Date.now(),
147 accessCount: 1,
148 metadata: metadata ?? {},
149 };
150 graph.nodes[node.id] = node;
151 scheduleSave(rootDir);
152 return node;
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);

Callers 3

toolUpsertMemoryNodeFunction · 0.85
addInterlinkedContextFunction · 0.85

Calls 4

loadGraphFunction · 0.85
fetchEmbeddingFunction · 0.85
scheduleSaveFunction · 0.85
generateIdFunction · 0.85

Tested by

no test coverage detected