import os from "node:os"; import path from "node:path"; import { ensureModel } from "../src/model.ts"; import { LABELS, parseLabels, Store } from "../src/store.ts"; const root = process.env.MNEMO_DIR ?? path.join(os.homedir(), ".pi/agent/mnemo"); const modelDir = process.env.MNEMO_MODEL_DIR ?? path.join(root, "model"); const [cmd, arg, ...rest] = process.argv.slice(2); function withStore(fn: (store: Store) => T): T { const store = new Store(path.join(root, "mnemo.db"), modelDir); try { return fn(store); } finally { store.close(); } } if (cmd === "fetch-model") { console.log(`${modelDir}: ${await ensureModel(modelDir)}`); } else if (cmd === "search" && arg) { withStore((store) => { for (const h of store.searchMemories([arg], { limit: 8 })) console.log(`#${h.id} cos=${h.cos.toFixed(2)} fts=${h.ftsRank ?? "-"}${h.labels.length ? ` [${h.labels.join(",")}]` : ""} ${h.text.slice(0, 140).replace(/\s+/g, " ")}`); }); } else if (cmd === "label" && arg) { const labels = parseLabels(rest); if (labels.length !== rest.length) { console.log(`unknown label; known: ${LABELS.join(", ")}`); process.exitCode = 1; } else { withStore((store) => console.log(store.setMemoryLabels(Number(arg), labels) ? `#${arg} labels: ${labels.join(", ") || "-"}` : "not found")); } } else if (cmd === "stats") { withStore((store) => console.log(store.countMemories(), store.unlabeled().memoryIds.length, "unlabeled", store.jobStats())); } else { console.log(`usage: pi-mnemo fetch-model | search | label [${LABELS.join(" ")}] | stats`); process.exitCode = cmd ? 1 : 0; }