(rootDir: string, type: NodeType, label: string, content: string, metadata?: Record<string, string>)
| 123 | } |
| 124 | |
| 125 | export 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 | |
| 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); |
no test coverage detected