|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// local-memory-mcp/mod.js — Persistent local memory cartridge (SQLite-backed). |
| 5 | +// |
| 6 | +// Delegates to backend at http://127.0.0.1:7750 (override with LOCAL_MEMORY_URL). |
| 7 | +// No auth required. All data stays local — no cloud, no API keys. |
| 8 | + |
| 9 | +const BASE_URL = Deno.env.get("LOCAL_MEMORY_URL") ?? "http://127.0.0.1:7750"; |
| 10 | +const TIMEOUT_MS = 15_000; |
| 11 | + |
| 12 | +async function post(path, payload) { |
| 13 | + const ctrl = new AbortController(); |
| 14 | + const t = setTimeout(() => ctrl.abort(), TIMEOUT_MS); |
| 15 | + try { |
| 16 | + const r = await fetch(`${BASE_URL}${path}`, { |
| 17 | + method: "POST", |
| 18 | + headers: { "Content-Type": "application/json" }, |
| 19 | + body: JSON.stringify(payload), |
| 20 | + signal: ctrl.signal, |
| 21 | + }); |
| 22 | + const data = await r.json().catch(() => ({ success: false, error: "non-JSON response" })); |
| 23 | + return { status: r.status, data }; |
| 24 | + } catch (e) { |
| 25 | + if (e.name === "AbortError") |
| 26 | + return { status: 504, data: { success: false, error: "local-memory-mcp backend timed out" } }; |
| 27 | + return { status: 503, data: { success: false, error: `local-memory-mcp backend unavailable: ${e.message}` } }; |
| 28 | + } finally { clearTimeout(t); } |
| 29 | +} |
| 30 | + |
| 31 | +export async function handleTool(toolName, args) { |
| 32 | + switch (toolName) { |
| 33 | + case "memory_session_start": { |
| 34 | + const { project } = args ?? {}; |
| 35 | + const payload = {}; |
| 36 | + if (project !== undefined) payload.project = project; |
| 37 | + return post("/api/v1/session/start", payload); |
| 38 | + } |
| 39 | + |
| 40 | + case "memory_session_end": { |
| 41 | + const { summary } = args ?? {}; |
| 42 | + const payload = {}; |
| 43 | + if (summary !== undefined) payload.summary = summary; |
| 44 | + return post("/api/v1/session/end", payload); |
| 45 | + } |
| 46 | + |
| 47 | + case "memory_learn": { |
| 48 | + const { category, content, tags, confidence, project } = args ?? {}; |
| 49 | + if (!category || !content) |
| 50 | + return { status: 400, data: { error: "category and content are required" } }; |
| 51 | + const payload = { category, content }; |
| 52 | + if (tags !== undefined) payload.tags = tags; |
| 53 | + if (confidence !== undefined) payload.confidence = confidence; |
| 54 | + if (project !== undefined) payload.project = project; |
| 55 | + return post("/api/v1/learnings", payload); |
| 56 | + } |
| 57 | + |
| 58 | + case "memory_recall": { |
| 59 | + const { query, limit } = args ?? {}; |
| 60 | + const payload = {}; |
| 61 | + if (query !== undefined) payload.query = query; |
| 62 | + if (limit !== undefined) payload.limit = limit; |
| 63 | + return post("/api/v1/learnings/recall", payload); |
| 64 | + } |
| 65 | + |
| 66 | + case "memory_search": { |
| 67 | + const { query, types, limit } = args ?? {}; |
| 68 | + if (!query) return { status: 400, data: { error: "query is required" } }; |
| 69 | + const payload = { query }; |
| 70 | + if (types !== undefined) payload.types = types; |
| 71 | + if (limit !== undefined) payload.limit = limit; |
| 72 | + return post("/api/v1/search", payload); |
| 73 | + } |
| 74 | + |
| 75 | + case "memory_decide": { |
| 76 | + const { title, decision, reasoning, alternatives, confidence, project } = args ?? {}; |
| 77 | + if (!title || !decision || !reasoning) |
| 78 | + return { status: 400, data: { error: "title, decision, and reasoning are required" } }; |
| 79 | + const payload = { title, decision, reasoning }; |
| 80 | + if (alternatives !== undefined) payload.alternatives = alternatives; |
| 81 | + if (confidence !== undefined) payload.confidence = confidence; |
| 82 | + if (project !== undefined) payload.project = project; |
| 83 | + return post("/api/v1/decisions", payload); |
| 84 | + } |
| 85 | + |
| 86 | + case "memory_entity_observe": { |
| 87 | + const { entityName, entityType, content } = args ?? {}; |
| 88 | + if (!entityName || !entityType || !content) |
| 89 | + return { status: 400, data: { error: "entityName, entityType, and content are required" } }; |
| 90 | + return post("/api/v1/entities/observe", { entityName, entityType, content }); |
| 91 | + } |
| 92 | + |
| 93 | + case "memory_entity_search": { |
| 94 | + const { query, entityType, limit } = args ?? {}; |
| 95 | + if (!query) return { status: 400, data: { error: "query is required" } }; |
| 96 | + const payload = { query }; |
| 97 | + if (entityType !== undefined) payload.entityType = entityType; |
| 98 | + if (limit !== undefined) payload.limit = limit; |
| 99 | + return post("/api/v1/entities/search", payload); |
| 100 | + } |
| 101 | + |
| 102 | + case "memory_entity_open": { |
| 103 | + const { name, id } = args ?? {}; |
| 104 | + const payload = {}; |
| 105 | + if (name !== undefined) payload.name = name; |
| 106 | + if (id !== undefined) payload.id = id; |
| 107 | + return post("/api/v1/entities/open", payload); |
| 108 | + } |
| 109 | + |
| 110 | + case "memory_entity_relate": { |
| 111 | + const { fromEntityId, toEntityId, relationType, weight } = args ?? {}; |
| 112 | + if (!fromEntityId || !toEntityId || !relationType) |
| 113 | + return { status: 400, data: { error: "fromEntityId, toEntityId, and relationType are required" } }; |
| 114 | + const payload = { fromEntityId, toEntityId, relationType }; |
| 115 | + if (weight !== undefined) payload.weight = weight; |
| 116 | + return post("/api/v1/entities/relate", payload); |
| 117 | + } |
| 118 | + |
| 119 | + case "memory_insights": { |
| 120 | + const { project } = args ?? {}; |
| 121 | + const payload = {}; |
| 122 | + if (project !== undefined) payload.project = project; |
| 123 | + return post("/api/v1/insights", payload); |
| 124 | + } |
| 125 | + |
| 126 | + case "memory_profile_set": { |
| 127 | + const { field, value } = args ?? {}; |
| 128 | + if (!field || !value) return { status: 400, data: { error: "field and value are required" } }; |
| 129 | + return post("/api/v1/profile/set", { field, value }); |
| 130 | + } |
| 131 | + |
| 132 | + case "memory_profile_get": { |
| 133 | + const { field } = args ?? {}; |
| 134 | + if (!field) return { status: 400, data: { error: "field is required" } }; |
| 135 | + return post("/api/v1/profile/get", { field }); |
| 136 | + } |
| 137 | + |
| 138 | + default: |
| 139 | + return { status: 404, data: { error: `Unknown tool: ${toolName}` } }; |
| 140 | + } |
| 141 | +} |
0 commit comments