meminbox 0.0.0-stage → 0.2.1
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/.env.example +19 -0
- package/LICENSE +21 -0
- package/README.md +162 -2
- package/dist/agents.js +112 -0
- package/dist/chunk.js +48 -0
- package/dist/cli.js +137 -0
- package/dist/config.js +21 -0
- package/dist/db.js +7 -0
- package/dist/dotenv.js +64 -0
- package/dist/embeddings.js +39 -0
- package/dist/extract.js +53 -0
- package/dist/init.js +155 -0
- package/dist/mcp/server.js +122 -0
- package/dist/memory.js +207 -0
- package/dist/types.js +2 -0
- package/package.json +1 -6
- package/supabase/migrations/20261108000001_init.sql +156 -0
package/.env.example
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# --- Supabase (your own project; use a DEDICATED project for memory) ---
|
|
2
|
+
# Project Settings > API
|
|
3
|
+
SUPABASE_URL=https://your-project.supabase.co
|
|
4
|
+
# Server-side only. Never expose this to browsers or commit it.
|
|
5
|
+
# The MCP server / CLI run on your machine, so the service role key stays local.
|
|
6
|
+
SUPABASE_SERVICE_ROLE_KEY=
|
|
7
|
+
|
|
8
|
+
# --- Embeddings (optional) ---
|
|
9
|
+
# Without this, search falls back to full-text only and everything still works.
|
|
10
|
+
# Model changes require a full reindex: embeddings are a derived index, not the source of truth.
|
|
11
|
+
OPENAI_API_KEY=
|
|
12
|
+
EMBEDDING_MODEL=text-embedding-3-small
|
|
13
|
+
EMBEDDING_DIMS=1536
|
|
14
|
+
|
|
15
|
+
# --- Defaults ---
|
|
16
|
+
# Project slug used when a write does not name one.
|
|
17
|
+
MEMORY_DEFAULT_PROJECT=default
|
|
18
|
+
# Actor name recorded in provenance / audit log for this process (e.g. claude-code, codex, cursor).
|
|
19
|
+
MEMORY_ACTOR=cli
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 daaquan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,163 @@
|
|
|
1
|
-
#
|
|
1
|
+
# meminbox
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Every agent writes to the inbox. Only reviewed memory becomes shared truth.**
|
|
4
|
+
|
|
5
|
+
Shared memory for AI agents, stored in **your own Supabase project**.
|
|
6
|
+
|
|
7
|
+
One memory that Claude Code, Codex, Cursor and any other MCP client read and
|
|
8
|
+
write through the same interface — instead of every agent keeping its own
|
|
9
|
+
private notes that die with its session.
|
|
10
|
+
|
|
11
|
+
## Design (the parts that matter)
|
|
12
|
+
|
|
13
|
+
This project borrows Cerefox's philosophy and uses extraction (the mem0
|
|
14
|
+
idea) as a component — in that order, on purpose:
|
|
15
|
+
|
|
16
|
+
1. **Documents are the source of truth.**
|
|
17
|
+
Memory is human-readable Markdown in a Postgres table (`documents`).
|
|
18
|
+
You can read it, edit it, export it with `pg_dump`, and leave with it.
|
|
19
|
+
No proprietary payload, no lock-in.
|
|
20
|
+
2. **Vectors are a derived index, never the truth.**
|
|
21
|
+
`document_chunks` + pgvector exist only to make search better. Drop them
|
|
22
|
+
and run `meminbox reindex` and they come back. Changing the embedding
|
|
23
|
+
model is a reindex, not a migration.
|
|
24
|
+
3. **Agent writes land in an inbox first.**
|
|
25
|
+
Shared memory amplifies errors: one agent's wrong note becomes every
|
|
26
|
+
agent's fact. So writes default to `status = 'inbox'` and only become
|
|
27
|
+
trusted memory on `promote`. Every document carries **provenance**
|
|
28
|
+
(which agent, which session, what source) and every action is in
|
|
29
|
+
`audit_log`.
|
|
30
|
+
4. **Extraction is a replaceable component, not the foundation.**
|
|
31
|
+
`src/extract.ts` turns raw transcripts into inbox candidates. It is a
|
|
32
|
+
small interface — swap in mem0, a local model, anything — because
|
|
33
|
+
extracted facts are inputs to review, not the store itself.
|
|
34
|
+
5. **Keyword search is a first-class mode.**
|
|
35
|
+
At personal scale (hundreds–thousands of documents), full-text +
|
|
36
|
+
substring search carries most of the weight. Embeddings are optional:
|
|
37
|
+
no `OPENAI_API_KEY`, no vector search, everything else still works.
|
|
38
|
+
|
|
39
|
+
## Architecture
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
Claude Code ─┐
|
|
43
|
+
Codex ───────┤ MCP (stdio) ┌──────────────────────────────┐
|
|
44
|
+
Cursor ──────┼───────────────────► │ meminbox MCP server │
|
|
45
|
+
Muse (files)┤ CLI / import │ search · write · extract │
|
|
46
|
+
Human ───────┘ CLI + Supabase UI └──────────────┬───────────────┘
|
|
47
|
+
│ service role (local only)
|
|
48
|
+
┌──────────▼───────────────┐
|
|
49
|
+
│ Your Supabase project │
|
|
50
|
+
│ documents (truth) │
|
|
51
|
+
│ document_chunks (index) │
|
|
52
|
+
│ audit_log │
|
|
53
|
+
└──────────────────────────┘
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Setup
|
|
57
|
+
|
|
58
|
+
### 1. Create a Supabase project
|
|
59
|
+
|
|
60
|
+
Use a **dedicated** project for memory (don't mix it into an app's project).
|
|
61
|
+
Free tier is enough. You'll need:
|
|
62
|
+
|
|
63
|
+
- the project URL and **service role** key (Project Settings → API)
|
|
64
|
+
- optionally, the Postgres **connection string** (Project Settings →
|
|
65
|
+
Database) — if you provide it, `init` applies the schema for you;
|
|
66
|
+
it is used once and never saved
|
|
67
|
+
|
|
68
|
+
### 2. Install
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
npm install -g meminbox
|
|
72
|
+
# from source instead: npm install -g github:daaquan/meminbox
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Initialise
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
meminbox init
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This prompts for your Supabase credentials, applies the schema when a
|
|
82
|
+
connection string is provided (otherwise it tells you which SQL file to
|
|
83
|
+
paste into the Supabase SQL Editor), saves your configuration to
|
|
84
|
+
`~/.meminbox/.env` (mode `0600`), and smoke-tests the connection.
|
|
85
|
+
|
|
86
|
+
Non-interactive form:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
meminbox init --yes \
|
|
90
|
+
--url https://your-project.supabase.co \
|
|
91
|
+
--service-key <service-role-key> \
|
|
92
|
+
--database-url <postgres-connection-string>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 4. Connect your agents
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
meminbox configure-agent --tool claude-code # or: codex | cursor | claude-desktop
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
This writes the MCP server entry into the client's own config file
|
|
102
|
+
(backing up the previous one to `<file>.bak`). The entry contains **no
|
|
103
|
+
secrets** — the server reads `~/.meminbox/.env` itself. Each client gets
|
|
104
|
+
its own `MEMORY_ACTOR`, so provenance shows which agent wrote what.
|
|
105
|
+
Use `--print` to preview the entry without writing anything.
|
|
106
|
+
Restart the client afterwards.
|
|
107
|
+
|
|
108
|
+
### 5. First memory
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
meminbox add --title "How we deploy" --text "Staging first, then prod. See runbook." --active
|
|
112
|
+
meminbox search "deploy"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Development (from a clone)
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npm install # then: npm run build to refresh dist/ (dist/ is committed
|
|
119
|
+
# so git-based installs work without building)
|
|
120
|
+
npm test
|
|
121
|
+
npm run cli -- list
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## MCP tools
|
|
125
|
+
|
|
126
|
+
| Tool | What it does |
|
|
127
|
+
|---|---|
|
|
128
|
+
| `memory_search` | Hybrid search over **active** memory (`includeInbox` to also search the inbox) |
|
|
129
|
+
| `memory_read` | Read a document in full |
|
|
130
|
+
| `memory_write` | Write a document — **inbox by default** |
|
|
131
|
+
| `memory_extract` | Extract durable candidates from raw text into the inbox |
|
|
132
|
+
| `memory_promote` | Promote inbox → active, or archive |
|
|
133
|
+
| `memory_list` | List documents by status |
|
|
134
|
+
| `memory_projects` | List projects (namespaces) |
|
|
135
|
+
|
|
136
|
+
## Security notes (read before connecting anything)
|
|
137
|
+
|
|
138
|
+
- Access is **service-role from your own machines only**. The service
|
|
139
|
+
role key bypasses RLS; treat it like a root password. `meminbox init`
|
|
140
|
+
keeps it in exactly one place — `~/.meminbox/.env`, mode `0600` — and
|
|
141
|
+
agent client configs never contain it. Never put it in a
|
|
142
|
+
browser, a hosted endpoint, or a repo.
|
|
143
|
+
- RLS is enabled with no public policies on purpose. A future remote MCP
|
|
144
|
+
endpoint must add per-agent tokens and explicit policies first.
|
|
145
|
+
- Anything an agent can read, a prompt injection in its input can try to
|
|
146
|
+
exfiltrate. The inbox/promote gate limits *writes*; reads of active memory
|
|
147
|
+
are trusted-agent-only by design.
|
|
148
|
+
|
|
149
|
+
## Known limitations / roadmap
|
|
150
|
+
|
|
151
|
+
- Search RPCs don't filter by project yet (single-project use is the v0
|
|
152
|
+
assumption); project filtering is the next migration.
|
|
153
|
+
- `embedBatch` is sequential; reindex of large libraries will want batching.
|
|
154
|
+
- No web UI yet: the human window is the CLI + Supabase Table Editor.
|
|
155
|
+
A small read/review UI for the inbox is the natural next step.
|
|
156
|
+
- No remote (HTTP) MCP transport yet — stdio only, one process per client,
|
|
157
|
+
all sharing the same database.
|
|
158
|
+
- Muse's own file-based memory does not plug into this directly; bridging
|
|
159
|
+
it means an import/export sync, which is intentionally not faked here.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
MIT — see [LICENSE](./LICENSE).
|
package/dist/agents.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { homedir, platform } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
export const AGENT_TOOLS = [
|
|
5
|
+
"claude-code",
|
|
6
|
+
"codex",
|
|
7
|
+
"cursor",
|
|
8
|
+
"claude-desktop",
|
|
9
|
+
];
|
|
10
|
+
export function mcpServerEntry(actor) {
|
|
11
|
+
return { command: "meminbox-mcp", args: [], env: { MEMORY_ACTOR: actor } };
|
|
12
|
+
}
|
|
13
|
+
/** Merge the meminbox entry into an MCP JSON config (pure; unit-tested). */
|
|
14
|
+
export function mergeMcpJson(existingText, entry) {
|
|
15
|
+
const root = existingText ? JSON.parse(existingText) : {};
|
|
16
|
+
const servers = (root.mcpServers ?? {});
|
|
17
|
+
servers.meminbox = entry;
|
|
18
|
+
root.mcpServers = servers;
|
|
19
|
+
return JSON.stringify(root, null, 2) + "\n";
|
|
20
|
+
}
|
|
21
|
+
export function codexTomlBlock(actor) {
|
|
22
|
+
return [
|
|
23
|
+
"[mcp_servers.meminbox]",
|
|
24
|
+
'command = "meminbox-mcp"',
|
|
25
|
+
"args = []",
|
|
26
|
+
"",
|
|
27
|
+
"[mcp_servers.meminbox.env]",
|
|
28
|
+
`MEMORY_ACTOR = "${actor}"`,
|
|
29
|
+
"",
|
|
30
|
+
].join("\n");
|
|
31
|
+
}
|
|
32
|
+
/** Insert or replace the meminbox section in Codex's config.toml (pure). */
|
|
33
|
+
export function upsertCodexToml(existingText, actor) {
|
|
34
|
+
const lines = existingText.split(/\r?\n/);
|
|
35
|
+
const kept = [];
|
|
36
|
+
let skipping = false;
|
|
37
|
+
for (const line of lines) {
|
|
38
|
+
const header = line.match(/^\s*\[([^\]]+)\]\s*$/);
|
|
39
|
+
if (header) {
|
|
40
|
+
skipping =
|
|
41
|
+
header[1] === "mcp_servers.meminbox" ||
|
|
42
|
+
header[1] === "mcp_servers.meminbox.env";
|
|
43
|
+
}
|
|
44
|
+
if (!skipping)
|
|
45
|
+
kept.push(line);
|
|
46
|
+
}
|
|
47
|
+
const base = kept.join("\n").replace(/\n+$/, "");
|
|
48
|
+
return (base ? base + "\n\n" : "") + codexTomlBlock(actor);
|
|
49
|
+
}
|
|
50
|
+
export function agentConfigPath(tool) {
|
|
51
|
+
const home = homedir();
|
|
52
|
+
switch (tool) {
|
|
53
|
+
case "claude-code":
|
|
54
|
+
return join(home, ".claude.json");
|
|
55
|
+
case "cursor":
|
|
56
|
+
return join(home, ".cursor", "mcp.json");
|
|
57
|
+
case "codex":
|
|
58
|
+
return join(home, ".codex", "config.toml");
|
|
59
|
+
case "claude-desktop": {
|
|
60
|
+
if (platform() === "darwin") {
|
|
61
|
+
return join(home, "Library", "Application Support", "Claude", "claude_desktop_config.json");
|
|
62
|
+
}
|
|
63
|
+
if (platform() === "win32") {
|
|
64
|
+
return join(process.env.APPDATA ?? home, "Claude", "claude_desktop_config.json");
|
|
65
|
+
}
|
|
66
|
+
return join(home, ".config", "Claude", "claude_desktop_config.json");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function onPath(bin) {
|
|
71
|
+
const exts = platform() === "win32" ? [".exe", ".cmd", ".bat", ""] : [""];
|
|
72
|
+
for (const dir of (process.env.PATH ?? "").split(":")) {
|
|
73
|
+
for (const ext of exts) {
|
|
74
|
+
if (dir && existsSync(join(dir, bin + ext)))
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
export function runConfigureAgent(argv) {
|
|
81
|
+
const toolIdx = argv.indexOf("--tool");
|
|
82
|
+
const tool = toolIdx >= 0 ? argv[toolIdx + 1] : undefined;
|
|
83
|
+
if (!tool || !AGENT_TOOLS.includes(tool)) {
|
|
84
|
+
throw new Error(`usage: meminbox configure-agent --tool <${AGENT_TOOLS.join("|")}> [--print]`);
|
|
85
|
+
}
|
|
86
|
+
const printOnly = argv.includes("--print");
|
|
87
|
+
const actorIdx = argv.indexOf("--actor");
|
|
88
|
+
const actor = actorIdx >= 0 ? argv[actorIdx + 1] : tool;
|
|
89
|
+
const path = agentConfigPath(tool);
|
|
90
|
+
const existing = existsSync(path) ? readFileSync(path, "utf8") : null;
|
|
91
|
+
const next = tool === "codex"
|
|
92
|
+
? upsertCodexToml(existing ?? "", actor)
|
|
93
|
+
: mergeMcpJson(existing, mcpServerEntry(actor));
|
|
94
|
+
if (printOnly) {
|
|
95
|
+
console.log(`# ${path}\n`);
|
|
96
|
+
console.log(next);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
100
|
+
if (existing !== null)
|
|
101
|
+
copyFileSync(path, `${path}.bak`);
|
|
102
|
+
writeFileSync(path, next);
|
|
103
|
+
console.log(`configured ${tool}: meminbox MCP server written to ${path}`);
|
|
104
|
+
if (existing !== null)
|
|
105
|
+
console.log(`previous config backed up to ${path}.bak`);
|
|
106
|
+
if (!onPath("meminbox-mcp")) {
|
|
107
|
+
console.log("note: `meminbox-mcp` is not on your PATH yet. Install meminbox globally first:\n" +
|
|
108
|
+
" npm install -g github:daaquan/meminbox");
|
|
109
|
+
}
|
|
110
|
+
console.log("restart the agent client to pick up the new MCP server.");
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=agents.js.map
|
package/dist/chunk.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Split Markdown into chunks for the derived vector index.
|
|
3
|
+
*
|
|
4
|
+
* Rules (kept deliberately simple and deterministic):
|
|
5
|
+
* - Split on heading boundaries first, then on blank lines.
|
|
6
|
+
* - Greedily pack paragraphs up to `maxChars`.
|
|
7
|
+
* - A single oversized paragraph is hard-split with `overlap` characters
|
|
8
|
+
* carried into the next chunk.
|
|
9
|
+
* The document itself is never modified — chunks are disposable.
|
|
10
|
+
*/
|
|
11
|
+
export function chunkMarkdown(markdown, opts = {}) {
|
|
12
|
+
const maxChars = opts.maxChars ?? 1200;
|
|
13
|
+
const overlap = opts.overlap ?? 120;
|
|
14
|
+
if (maxChars <= 0)
|
|
15
|
+
throw new Error("maxChars must be positive");
|
|
16
|
+
const blocks = markdown
|
|
17
|
+
.split(/\n(?=#{1,6}\s)/) // keep headings with the block that follows
|
|
18
|
+
.flatMap((section) => section.split(/\n{2,}/))
|
|
19
|
+
.map((b) => b.trim())
|
|
20
|
+
.filter((b) => b.length > 0);
|
|
21
|
+
const chunks = [];
|
|
22
|
+
let current = "";
|
|
23
|
+
const flush = () => {
|
|
24
|
+
if (current.trim().length > 0)
|
|
25
|
+
chunks.push(current.trim());
|
|
26
|
+
current = "";
|
|
27
|
+
};
|
|
28
|
+
for (const block of blocks) {
|
|
29
|
+
if (block.length > maxChars) {
|
|
30
|
+
flush();
|
|
31
|
+
let rest = block;
|
|
32
|
+
while (rest.length > maxChars) {
|
|
33
|
+
chunks.push(rest.slice(0, maxChars));
|
|
34
|
+
rest = rest.slice(maxChars - overlap);
|
|
35
|
+
}
|
|
36
|
+
if (rest.trim().length > 0)
|
|
37
|
+
current = rest;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (current.length > 0 && current.length + block.length + 2 > maxChars) {
|
|
41
|
+
flush();
|
|
42
|
+
}
|
|
43
|
+
current = current.length > 0 ? `${current}\n\n${block}` : block;
|
|
44
|
+
}
|
|
45
|
+
flush();
|
|
46
|
+
return chunks;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=chunk.js.map
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { runConfigureAgent } from "./agents.js";
|
|
4
|
+
import { loadConfig } from "./config.js";
|
|
5
|
+
import { createDb } from "./db.js";
|
|
6
|
+
import { extractCandidates } from "./extract.js";
|
|
7
|
+
import { runInit } from "./init.js";
|
|
8
|
+
import { MemoryService } from "./memory.js";
|
|
9
|
+
/**
|
|
10
|
+
* The human's window into the same memory the agents use.
|
|
11
|
+
*
|
|
12
|
+
* Setup:
|
|
13
|
+
* meminbox init [--url <url> --service-key <key> --database-url <url>]
|
|
14
|
+
* meminbox configure-agent --tool <claude-code|codex|cursor|claude-desktop> [--print]
|
|
15
|
+
*
|
|
16
|
+
* Memory:
|
|
17
|
+
* meminbox search <query>
|
|
18
|
+
* meminbox add --title <t> [--file <path> | --text <md>] [--active]
|
|
19
|
+
* meminbox list [--status inbox|active|archived]
|
|
20
|
+
* meminbox promote <id> | archive <id>
|
|
21
|
+
* meminbox extract --file <transcript>
|
|
22
|
+
* meminbox reindex
|
|
23
|
+
* meminbox projects
|
|
24
|
+
*/
|
|
25
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
26
|
+
function flag(name) {
|
|
27
|
+
const i = rest.indexOf(`--${name}`);
|
|
28
|
+
return i >= 0 ? rest[i + 1] : undefined;
|
|
29
|
+
}
|
|
30
|
+
function positional(i) {
|
|
31
|
+
return rest.filter((a) => !a.startsWith("--"))[i];
|
|
32
|
+
}
|
|
33
|
+
// Setup commands run BEFORE any config exists — they must not load it.
|
|
34
|
+
if (cmd === "init") {
|
|
35
|
+
await runInit(rest);
|
|
36
|
+
process.exit(0);
|
|
37
|
+
}
|
|
38
|
+
if (cmd === "configure-agent") {
|
|
39
|
+
runConfigureAgent(rest);
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
let cfg;
|
|
43
|
+
let memory;
|
|
44
|
+
try {
|
|
45
|
+
cfg = loadConfig();
|
|
46
|
+
memory = new MemoryService(cfg, createDb(cfg));
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
if (cmd === undefined) {
|
|
50
|
+
console.log("usage: meminbox <init|configure-agent|search|add|list|promote|archive|extract|reindex|projects>\n" +
|
|
51
|
+
"start with: meminbox init");
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
throw err;
|
|
55
|
+
}
|
|
56
|
+
switch (cmd) {
|
|
57
|
+
case "search": {
|
|
58
|
+
const query = rest.join(" ");
|
|
59
|
+
if (!query)
|
|
60
|
+
throw new Error("usage: meminbox search <query>");
|
|
61
|
+
const hits = await memory.search(query, { limit: 10 });
|
|
62
|
+
for (const h of hits) {
|
|
63
|
+
console.log(`\n## ${h.title} [${h.documentId}] score=${h.score.toFixed(2)} via=${h.via.join("+")}`);
|
|
64
|
+
console.log(h.snippet.replace(/\s+/g, " ").trim());
|
|
65
|
+
}
|
|
66
|
+
if (hits.length === 0)
|
|
67
|
+
console.log("no hits");
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case "add": {
|
|
71
|
+
const title = flag("title");
|
|
72
|
+
if (!title)
|
|
73
|
+
throw new Error("usage: meminbox add --title <t> [--file <path> | --text <md>]");
|
|
74
|
+
const content = flag("file")
|
|
75
|
+
? readFileSync(flag("file"), "utf8")
|
|
76
|
+
: (flag("text") ?? "");
|
|
77
|
+
if (!content)
|
|
78
|
+
throw new Error("provide --file or --text");
|
|
79
|
+
const doc = await memory.writeDocument({
|
|
80
|
+
title,
|
|
81
|
+
contentMd: content,
|
|
82
|
+
projectSlug: flag("project"),
|
|
83
|
+
status: rest.includes("--active") ? "active" : "inbox",
|
|
84
|
+
provenance: { source: "cli" },
|
|
85
|
+
});
|
|
86
|
+
console.log(`${doc.status}: ${doc.id} ${doc.title}`);
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "list": {
|
|
90
|
+
const status = flag("status");
|
|
91
|
+
const docs = await memory.listDocuments({ status, limit: 50 });
|
|
92
|
+
for (const d of docs) {
|
|
93
|
+
console.log(`${d.status.padEnd(8)} ${d.id} ${d.title} (${d.source_agent ?? "?"}, ${d.updated_at})`);
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "promote":
|
|
98
|
+
case "archive": {
|
|
99
|
+
const id = positional(0);
|
|
100
|
+
if (!id)
|
|
101
|
+
throw new Error(`usage: meminbox ${cmd} <id>`);
|
|
102
|
+
const doc = await memory.setStatus(id, cmd === "promote" ? "active" : "archived");
|
|
103
|
+
console.log(`${doc.status}: ${doc.id} ${doc.title}`);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "extract": {
|
|
107
|
+
const file = flag("file");
|
|
108
|
+
if (!file)
|
|
109
|
+
throw new Error("usage: meminbox extract --file <transcript>");
|
|
110
|
+
const candidates = await extractCandidates(cfg, readFileSync(file, "utf8"));
|
|
111
|
+
for (const c of candidates) {
|
|
112
|
+
const doc = await memory.writeDocument({
|
|
113
|
+
title: c.title,
|
|
114
|
+
contentMd: c.contentMd,
|
|
115
|
+
provenance: { source: "extraction" },
|
|
116
|
+
});
|
|
117
|
+
console.log(`inbox: ${doc.id} ${doc.title}`);
|
|
118
|
+
}
|
|
119
|
+
if (candidates.length === 0)
|
|
120
|
+
console.log("nothing durable extracted");
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "reindex": {
|
|
124
|
+
const n = await memory.reindexAll();
|
|
125
|
+
console.log(`reindexed ${n} documents`);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "projects": {
|
|
129
|
+
for (const p of await memory.listProjects())
|
|
130
|
+
console.log(`${p.slug}\t${p.name}`);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
console.log("usage: meminbox <init|configure-agent|search|add|list|promote|archive|extract|reindex|projects>");
|
|
135
|
+
process.exit(cmd ? 1 : 0);
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { loadEnvFiles } from "./dotenv.js";
|
|
2
|
+
function required(name) {
|
|
3
|
+
const v = process.env[name];
|
|
4
|
+
if (!v) {
|
|
5
|
+
throw new Error(`Missing required env var: ${name}. Run \`meminbox init\` or see .env.example`);
|
|
6
|
+
}
|
|
7
|
+
return v;
|
|
8
|
+
}
|
|
9
|
+
export function loadConfig() {
|
|
10
|
+
loadEnvFiles();
|
|
11
|
+
return {
|
|
12
|
+
supabaseUrl: required("SUPABASE_URL"),
|
|
13
|
+
supabaseServiceRoleKey: required("SUPABASE_SERVICE_ROLE_KEY"),
|
|
14
|
+
openaiApiKey: process.env.OPENAI_API_KEY || null,
|
|
15
|
+
embeddingModel: process.env.EMBEDDING_MODEL || "text-embedding-3-small",
|
|
16
|
+
embeddingDims: Number(process.env.EMBEDDING_DIMS || 1536),
|
|
17
|
+
defaultProject: process.env.MEMORY_DEFAULT_PROJECT || "default",
|
|
18
|
+
actor: process.env.MEMORY_ACTOR || "cli",
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=config.js.map
|
package/dist/db.js
ADDED
package/dist/dotenv.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
/** Directory holding the user's meminbox configuration (~/.meminbox). */
|
|
5
|
+
export function meminboxHome() {
|
|
6
|
+
return process.env.MEMINBOX_HOME || join(homedir(), ".meminbox");
|
|
7
|
+
}
|
|
8
|
+
export function meminboxEnvPath() {
|
|
9
|
+
return join(meminboxHome(), ".env");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Minimal .env parser: KEY=value lines, `#` comments, optional quotes.
|
|
13
|
+
* Deliberately tiny — the file format we write in `init` is our own.
|
|
14
|
+
*/
|
|
15
|
+
export function parseDotEnv(text) {
|
|
16
|
+
const out = {};
|
|
17
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
18
|
+
const line = rawLine.trim();
|
|
19
|
+
if (!line || line.startsWith("#"))
|
|
20
|
+
continue;
|
|
21
|
+
const eq = line.indexOf("=");
|
|
22
|
+
if (eq <= 0)
|
|
23
|
+
continue;
|
|
24
|
+
const key = line.slice(0, eq).trim();
|
|
25
|
+
let value = line.slice(eq + 1).trim();
|
|
26
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
27
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
28
|
+
value = value.slice(1, -1);
|
|
29
|
+
}
|
|
30
|
+
out[key] = value;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Load configuration files into process.env WITHOUT overriding variables
|
|
36
|
+
* that are already set. Lookup order:
|
|
37
|
+
* 1. ~/.meminbox/.env (written by `meminbox init` — the normal case)
|
|
38
|
+
* 2. ./.env (project-local, for development)
|
|
39
|
+
* Real environment variables always win over both files.
|
|
40
|
+
*/
|
|
41
|
+
export function loadEnvFiles() {
|
|
42
|
+
for (const path of [meminboxEnvPath(), join(process.cwd(), ".env")]) {
|
|
43
|
+
if (!existsSync(path))
|
|
44
|
+
continue;
|
|
45
|
+
const parsed = parseDotEnv(readFileSync(path, "utf8"));
|
|
46
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
47
|
+
if (process.env[key] === undefined)
|
|
48
|
+
process.env[key] = value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Serialise values back to .env text (used by `init`). */
|
|
53
|
+
export function formatDotEnv(values) {
|
|
54
|
+
const lines = [
|
|
55
|
+
"# meminbox configuration — written by `meminbox init`.",
|
|
56
|
+
"# Contains secrets (service role key). Keep this file private (mode 0600).",
|
|
57
|
+
"",
|
|
58
|
+
];
|
|
59
|
+
for (const [key, value] of Object.entries(values)) {
|
|
60
|
+
lines.push(`${key}=${value}`);
|
|
61
|
+
}
|
|
62
|
+
return lines.join("\n") + "\n";
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=dotenv.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Embeddings are OPTIONAL and DERIVED.
|
|
3
|
+
* - No OPENAI_API_KEY -> returns null, callers fall back to keyword search.
|
|
4
|
+
* - The embedding model/dims are configuration, not truth: changing them
|
|
5
|
+
* only means the chunk index must be rebuilt (`meminbox reindex`).
|
|
6
|
+
*/
|
|
7
|
+
export async function embedText(cfg, text) {
|
|
8
|
+
if (!cfg.openaiApiKey)
|
|
9
|
+
return null;
|
|
10
|
+
const res = await fetch("https://api.openai.com/v1/embeddings", {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
Authorization: `Bearer ${cfg.openaiApiKey}`,
|
|
14
|
+
"Content-Type": "application/json",
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify({ model: cfg.embeddingModel, input: text }),
|
|
17
|
+
});
|
|
18
|
+
if (!res.ok) {
|
|
19
|
+
throw new Error(`Embedding request failed: ${res.status} ${await res.text()}`);
|
|
20
|
+
}
|
|
21
|
+
const json = (await res.json());
|
|
22
|
+
const vec = json.data[0]?.embedding;
|
|
23
|
+
if (!vec)
|
|
24
|
+
throw new Error("Embedding response had no vector");
|
|
25
|
+
if (vec.length !== cfg.embeddingDims) {
|
|
26
|
+
throw new Error(`Embedding dims mismatch: model returned ${vec.length}, schema expects ${cfg.embeddingDims}. ` +
|
|
27
|
+
`Set EMBEDDING_DIMS to match the model, or keep the default model.`);
|
|
28
|
+
}
|
|
29
|
+
return vec;
|
|
30
|
+
}
|
|
31
|
+
export async function embedBatch(cfg, texts) {
|
|
32
|
+
// v0: sequential calls keep failure handling simple. Batching is a
|
|
33
|
+
// performance optimisation that belongs in the reindex path later.
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const t of texts)
|
|
36
|
+
out.push(await embedText(cfg, t));
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=embeddings.js.map
|