@illli-studio/mory 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bundle/server.mjs CHANGED
@@ -74,7 +74,7 @@ const server = createServer(async (request, response) => { if (request.method ==
74
74
  if (request.method === "POST" && url.pathname === "/v1/memories/search") { const input = await requestBody(request); return send(response, 200, { results: await search(input.query || "", input.scope || {}, Number(input.limit || 10)) }); }
75
75
  if (request.method === "POST" && url.pathname === "/v1/context") { const input = await requestBody(request); const results = await search(input.query || "", input.scope || {}, Number(input.limit || 12)); const memories = results.map((item) => item.memory); return send(response, 200, { query: input.query || "", memories, context: memories.map((memory) => `- ${memory.text}`).join("\n") }); }
76
76
  if (request.method === "POST" && url.pathname === "/v1/memories/remember") { const input = await requestBody(request); const text = messageText(input.messages || input.text || input.content); if (!text) return send(response, 400, { error: "messages or text is required" }); const rawId = randomUUID(); const scope = input.scope || {}; db.prepare("INSERT INTO raw_events(id,content,source,actor_json,scope_json,created_at) VALUES (?,?,?,?,?,?)").run(rawId, text, input.source || "agent", JSON.stringify(actor(input.actor)), JSON.stringify(scope), new Date().toISOString()); const extracted = await extract(input, (await search(text, scope, 10)).map((item) => item.memory)); const added = [], duplicates = []; for (const candidate of extracted.candidates) { const memory = makeMemory({ ...candidate, source: input.source || "agent", actor: actor(input.actor), scope, metadata: { ...(input.metadata || {}), ...(candidate.metadata || {}) }, sourceEventIds: [rawId], embedding: await embed(candidate.text) }, "agent"); const duplicate = allMemories(scope).find((item) => item.hash === memory.hash); if (duplicate) { duplicates.push(duplicate); continue; } insert(memory); addEvent("ADD", memory, { sourceEventId: rawId }); linkEntities(memory); if (candidate.supersedes) { db.prepare("INSERT INTO relations(id,from_memory_id,to_memory_id,type,created_at) VALUES (?,?,?,?,?)").run(randomUUID(), memory.id, candidate.supersedes, "supersedes", memory.createdAt); db.prepare("UPDATE memories SET status = 'superseded', updated_at = ? WHERE id = ?").run(memory.updatedAt, candidate.supersedes); } added.push(memory); } return send(response, 201, { eventId: rawId, extractor: extracted.extractor, memories: added, duplicates, addedCount: added.length }); }
77
- if (request.method === "POST" && url.pathname === "/v1/memories") { const memory = makeMemory(await requestBody(request)); const duplicate = allMemories(memory.scope).find((item) => item.hash === memory.hash); if (duplicate) return send(response, 200, { memory: duplicate, duplicate: true }); insert(memory); addEvent("ADD", memory); linkEntities(memory); return send(response, 201, { memory, duplicate: false }); }
77
+ if (request.method === "POST" && url.pathname === "/v1/memories") { const memory = makeMemory(await requestBody(request)); const duplicate = allMemories(memory.scope).find((item) => item.hash === memory.hash); if (duplicate) return send(response, 200, { memory: duplicate, duplicate: true }); const sameId = getMemory(memory.id); if (sameId) return send(response, 200, { memory: sameId, duplicate: true }); insert(memory); addEvent("ADD", memory); linkEntities(memory); return send(response, 201, { memory, duplicate: false }); }
78
78
  const match = url.pathname.match(/^\/v1\/memories\/([^/]+)$/);
79
79
  if (match && request.method === "GET") { const memory = getMemory(match[1]); return memory ? send(response, 200, { memory }) : send(response, 404, { error: "memory not found" }); }
80
80
  if (match && request.method === "PATCH") { const current = getMemory(match[1]); if (!current) return send(response, 404, { error: "memory not found" }); const input = await requestBody(request); const nextText = String(input.text || input.content || input.payload?.content || current.text).trim(); const contentChanged = nextText !== current.text || String(input.title || current.title) !== current.title; const next = makeMemory({ ...current, ...input, id: current.id, text: nextText, hash: undefined, contentHash: undefined, embedding: contentChanged ? await embed(nextText) : current.embedding, sourceEventIds: [...current.sourceEventIds, randomUUID()] }, current.source); db.exec("BEGIN"); try { db.prepare("DELETE FROM memory_fts WHERE memory_id = ?").run(current.id); db.prepare("DELETE FROM memories WHERE id = ?").run(current.id); insert(next); addEvent("UPDATE", next, { oldHash: current.hash, newHash: next.hash }); db.exec("COMMIT"); } catch (error) { db.exec("ROLLBACK"); throw error; } return send(response, 200, { memory: next }); }