@pinkpixel/memi 0.1.0 → 1.0.0
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/README.md +299 -0
- package/dist/cli/commands/config.js +38 -0
- package/dist/cli/commands/doctor.js +53 -0
- package/dist/cli/commands/memories.js +146 -0
- package/dist/cli/commands/reindex.js +34 -0
- package/dist/cli/commands/serve.js +9 -0
- package/dist/cli/commands/ui.js +26 -0
- package/dist/cli/index.js +20 -0
- package/dist/cli/util.js +46 -0
- package/dist/core/config.js +89 -0
- package/dist/core/db.js +21 -0
- package/dist/core/format.js +16 -0
- package/dist/core/memories.js +192 -0
- package/dist/core/project.js +53 -0
- package/dist/core/rows.js +38 -0
- package/dist/core/runtime.js +10 -0
- package/dist/core/schema.js +60 -0
- package/dist/core/search.js +86 -0
- package/dist/core/types.js +12 -0
- package/dist/core/vectors.js +102 -0
- package/dist/embed/http.js +20 -0
- package/dist/embed/index.js +9 -0
- package/dist/embed/ollama.js +24 -0
- package/dist/embed/openai.js +22 -0
- package/dist/embed/types.js +2 -0
- package/dist/lib/app-info.js +5 -0
- package/dist/mcp/server.js +24 -0
- package/dist/mcp/tools.js +152 -0
- package/dist/ui/assets.js +38 -0
- package/dist/ui/routes.js +91 -0
- package/dist/ui/security.js +47 -0
- package/dist/ui/server.js +33 -0
- package/package.json +8 -1
- package/skills/memi-setup/SKILL.md +28 -13
package/README.md
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/pinkpixel-dev/memi/HEAD/memi.png" alt="memi, a small smiling pastel blob with a sparkle" width="140">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# memi
|
|
6
|
+
|
|
7
|
+
Local-first memory for AI agents. Memories live in one SQLite file on your machine, and agents reach them through an MCP server.
|
|
8
|
+
|
|
9
|
+
AI agents forget everything when a session ends. memi gives them somewhere to keep the things worth remembering: decisions and why they were made, how you like things done, mistakes not to repeat. The next session can search that instead of asking you the same questions again.
|
|
10
|
+
|
|
11
|
+
## What it does
|
|
12
|
+
|
|
13
|
+
- Stores memories in a single SQLite file, with no server to run and no account to make
|
|
14
|
+
- Finds them by meaning and by keyword, using local embeddings from Ollama or OpenAI
|
|
15
|
+
- Keeps **global** memories (about you, across every project) apart from **project** memories (about one codebase)
|
|
16
|
+
- Lets you mark memories as important, pin the ones that must always be loaded, and sort them into categories
|
|
17
|
+
- Runs as an MCP server, so any client that speaks MCP can use it
|
|
18
|
+
- Comes with a CLI, a small local web UI for browsing and editing, and agent skills that teach an agent when to save and recall
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
You need:
|
|
23
|
+
|
|
24
|
+
- Node.js 22 or newer
|
|
25
|
+
- One embedder: [Ollama](https://ollama.com) (runs locally) or an OpenAI API key
|
|
26
|
+
|
|
27
|
+
Install it globally to get a `memi` command:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install -g @pinkpixel/memi
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or skip the install and let `npx` fetch it when it's needed. That's what the MCP setup below does:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npx -y @pinkpixel/memi doctor
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The examples below say `memi`. If you didn't install globally, use `npx -y @pinkpixel/memi` in its place. To build it from source instead, see [Development](#development).
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
The default embedder is Ollama with `nomic-embed-text`, so pull that first:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
ollama pull nomic-embed-text
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Check that everything is wired up:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
memi doctor
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Save a few things, then search for them in different words:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
memi add "I prefer tabs over spaces" --global --category preference
|
|
59
|
+
memi add "Deploys go through GitHub Actions to Cloudflare" --category fact
|
|
60
|
+
memi add "Keep answers short, with no summary at the end" --global --category preference
|
|
61
|
+
|
|
62
|
+
memi search "indentation style"
|
|
63
|
+
memi search "how do we ship releases"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Neither search shares a word with the memory it finds. That's the embeddings at work. (The second memory has no `--global`, so it belongs to whichever project you ran the command in.)
|
|
67
|
+
|
|
68
|
+
If you'd rather use OpenAI:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
export OPENAI_API_KEY=sk-...
|
|
72
|
+
memi config set embedder.provider openai
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Keep in mind that memory text gets sent to OpenAI to be embedded when you do this.
|
|
76
|
+
|
|
77
|
+
## Connect it to your agent
|
|
78
|
+
|
|
79
|
+
### Claude Code
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
claude mcp add memi -s user -e MEMI_AGENT=claude -- npx -y @pinkpixel/memi serve
|
|
83
|
+
claude mcp list
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`-s user` makes memi available in every project, which is usually what you want for a memory. `MEMI_AGENT` is optional (more on that below).
|
|
87
|
+
|
|
88
|
+
The first start downloads the package, which takes a few seconds. After that npx reuses its cache, so starts are quick, and that cache can hold on to an older version. Use `@pinkpixel/memi@latest` in the command to always get the newest one. If you installed globally, you can use `memi serve` as the command instead, which starts fastest.
|
|
89
|
+
|
|
90
|
+
### Other clients
|
|
91
|
+
|
|
92
|
+
Anything that can launch a stdio MCP server needs a command, its arguments, and optionally some environment variables. In the common `mcpServers` format:
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"mcpServers": {
|
|
97
|
+
"memi": {
|
|
98
|
+
"command": "npx",
|
|
99
|
+
"args": ["-y", "@pinkpixel/memi", "serve"],
|
|
100
|
+
"env": { "MEMI_AGENT": "my-agent" }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Restart the client after editing its config. I've only tested this with Claude Code so far, but other clients should work the same way.
|
|
107
|
+
|
|
108
|
+
### What the agent gets
|
|
109
|
+
|
|
110
|
+
| Tool | What it does |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `get_context` | Loads the pinned and high-importance memories at the start of a session |
|
|
113
|
+
| `recall` | Searches by meaning and keyword |
|
|
114
|
+
| `remember` | Saves a memory, and points out similar ones that already exist |
|
|
115
|
+
| `update_memory` | Changes a memory, or moves it between global and a project |
|
|
116
|
+
| `forget` | Deletes a memory |
|
|
117
|
+
| `list_memories` | Browses without a search query |
|
|
118
|
+
| `list_categories` | Lists categories with counts |
|
|
119
|
+
|
|
120
|
+
## Install the skills
|
|
121
|
+
|
|
122
|
+
The tools alone don't tell an agent when to use them, so there are three skills in `skills/`:
|
|
123
|
+
|
|
124
|
+
| Skill | What it covers |
|
|
125
|
+
|---|---|
|
|
126
|
+
| `memi-memory` | The everyday habit: load context first, recall before deciding, save what's worth keeping, and never save secrets |
|
|
127
|
+
| `memi-curate` | Cleaning up a messy store. It proposes changes and waits for your approval before deleting anything |
|
|
128
|
+
| `memi-setup` | Installing, connecting, switching embedders, and reading `memi doctor` |
|
|
129
|
+
|
|
130
|
+
They ship inside the package. Copy or symlink the folders into your client's skills directory. For Claude Code that's `~/.claude/skills/`, and with a global install it looks like this:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
ln -s "$(npm root -g)/@pinkpixel/memi/skills/"* ~/.claude/skills/
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Without a global install, grab the `skills` folder from the repo instead.
|
|
137
|
+
|
|
138
|
+
## The CLI
|
|
139
|
+
|
|
140
|
+
| Command | What it does |
|
|
141
|
+
|---|---|
|
|
142
|
+
| `memi add <content...>` | Save a memory. Options: `-c` category, `-i` importance 1 to 5, `-t` tags, `--pin`, `--global`, `-p` project |
|
|
143
|
+
| `memi search <query...>` | Search. Options: `--mode hybrid\|semantic\|text`, `--scope both\|project\|global\|all`, `-c`, `--min-importance`, `-n`, `--json` |
|
|
144
|
+
| `memi list` | Browse and filter. Same filters as search, plus `--pinned` and `--order recent\|oldest\|importance` |
|
|
145
|
+
| `memi update <id>` | Change a memory: `--content`, `-c`, `-i`, `-t`, `--pin`, `--unpin`, `--global`, `-p` |
|
|
146
|
+
| `memi forget <id>` | Delete one. Asks first, or pass `--yes` |
|
|
147
|
+
| `memi categories` | List categories with counts |
|
|
148
|
+
| `memi config` | Show settings. `memi config set <key> <value>` changes one |
|
|
149
|
+
| `memi reindex` | Rebuild the vector index. Asks first, or pass `--yes` |
|
|
150
|
+
| `memi doctor` | Check the database, the embedder, the index, and project detection |
|
|
151
|
+
| `memi ui` | Start the local web UI |
|
|
152
|
+
| `memi serve` | Run the MCP server |
|
|
153
|
+
|
|
154
|
+
Known errors print one line starting with `memi:` and exit with code 1. Commands that need a confirmation won't guess when there's no terminal to ask in. Pass `--yes` in scripts.
|
|
155
|
+
|
|
156
|
+
## The local UI
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
memi ui --open
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
This serves a small memory manager at `http://localhost:4747`. You can search, filter by scope, category, and importance, add and edit memories, pin them, and delete them. Deleting always asks first.
|
|
163
|
+
|
|
164
|
+
A few keys help: `/` jumps to search, `n` starts a new memory, `Esc` closes whatever is open, and `Ctrl+Enter` saves. It works on a phone-sized screen too.
|
|
165
|
+
|
|
166
|
+
It only listens on your own machine, and it ignores requests that don't come from `localhost`, since it can delete things. Pick another port with `--port`, or `memi config set ui.port <n>`.
|
|
167
|
+
|
|
168
|
+
## Projects, scopes, and the agent name
|
|
169
|
+
|
|
170
|
+
Every memory is either **global** or belongs to one **project**. memi works out the project in this order:
|
|
171
|
+
|
|
172
|
+
1. A name you pass (`--project`, or the `project` argument of a tool)
|
|
173
|
+
2. The `MEMI_PROJECT` environment variable
|
|
174
|
+
3. The folder name of the git repo you're in
|
|
175
|
+
4. The agent name, stored as `agent:<name>`
|
|
176
|
+
|
|
177
|
+
That fourth one is for folders with no git repo. Set it with `memi config set agent <name>` or `MEMI_AGENT`. The agent name is also recorded on each memory, so you can see who wrote what.
|
|
178
|
+
|
|
179
|
+
With none of those, project memories can't be saved, and the error says what to set. Global memories always work.
|
|
180
|
+
|
|
181
|
+
One thing to know: the git check uses the folder name only. Two repos with the same folder name share a project. Pass an explicit project name if that bites you.
|
|
182
|
+
|
|
183
|
+
## Embeddings
|
|
184
|
+
|
|
185
|
+
| Provider | Default model | What you need |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| `ollama` | `nomic-embed-text` | Ollama running, with the model pulled |
|
|
188
|
+
| `openai` | `text-embedding-3-small` | `OPENAI_API_KEY` in the environment that runs memi |
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
memi config # what's in effect right now
|
|
192
|
+
memi config set embedder.provider openai
|
|
193
|
+
memi config set embedder.model text-embedding-3-small
|
|
194
|
+
memi config set embedder.baseUrl http://localhost:11434
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The API key is only ever read from the environment. memi never writes it to a file.
|
|
198
|
+
|
|
199
|
+
### Switching embedders
|
|
200
|
+
|
|
201
|
+
Different models make vectors of different sizes, so the vectors you already have can't be reused. When you change the embedder, memi notices and turns semantic search off, with a warning. Keyword search keeps working, and nothing is deleted.
|
|
202
|
+
|
|
203
|
+
To finish the switch:
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
memi reindex
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
That drops the vector table, rebuilds it for the new model, and re-embeds every saved memory. **Your memories are kept.** It asks before it starts. The embedding happens before anything is dropped, so if the new embedder isn't reachable, nothing changes. If you switch back to the original model, you don't need to reindex at all.
|
|
210
|
+
|
|
211
|
+
If a memory was saved while the embedder was down, it's still saved. It just has no vector yet. `memi reindex --missing` fills those in without dropping anything.
|
|
212
|
+
|
|
213
|
+
## Configuration
|
|
214
|
+
|
|
215
|
+
Settings come from `~/.memi/config.json`, and environment variables override them.
|
|
216
|
+
|
|
217
|
+
| Setting | Config key | Environment variable |
|
|
218
|
+
|---|---|---|
|
|
219
|
+
| Embedder | `embedder.provider` | `MEMI_EMBED_PROVIDER` |
|
|
220
|
+
| Model | `embedder.model` | `MEMI_EMBED_MODEL` |
|
|
221
|
+
| Embedder URL | `embedder.baseUrl` | `MEMI_OLLAMA_URL` (Ollama), `OPENAI_BASE_URL` (OpenAI) |
|
|
222
|
+
| Agent name | `agent` | `MEMI_AGENT` |
|
|
223
|
+
| UI port | `ui.port` | none |
|
|
224
|
+
| Force a project | none | `MEMI_PROJECT` |
|
|
225
|
+
| Data folder | none | `MEMI_HOME` |
|
|
226
|
+
| OpenAI key | none | `OPENAI_API_KEY` |
|
|
227
|
+
|
|
228
|
+
Setting `embedder.provider` clears the saved model and URL, so the new provider starts from its own defaults.
|
|
229
|
+
|
|
230
|
+
## Where your data lives
|
|
231
|
+
|
|
232
|
+
- Database: `~/.memi/memi.db`
|
|
233
|
+
- Config: `~/.memi/config.json`
|
|
234
|
+
|
|
235
|
+
Set `MEMI_HOME` to move both. SQLite runs in WAL mode, so you'll see `memi.db-wal` and `memi.db-shm` next to the database while memi is running. To back up, copy all three files while nothing is using the database, or run `sqlite3 ~/.memi/memi.db ".backup backup.db"`.
|
|
236
|
+
|
|
237
|
+
## How search works
|
|
238
|
+
|
|
239
|
+
Searches run two ways and merge the results: a keyword search (SQLite FTS5, with stemming) and a vector search (`sqlite-vec`, cosine distance). The two ranked lists are combined with reciprocal rank fusion. Importance and recency then give a small nudge, small enough that relevance still wins.
|
|
240
|
+
|
|
241
|
+
If vectors aren't usable, search falls back to keywords and tells you.
|
|
242
|
+
|
|
243
|
+
## Limitations
|
|
244
|
+
|
|
245
|
+
- Only tested on Linux so far
|
|
246
|
+
- Only tested with Claude Code as the MCP client
|
|
247
|
+
- Semantic search always returns its nearest matches, even when none of them are really relevant. There's no relevance cutoff yet
|
|
248
|
+
- Search quality depends on the embedding model. `nomic-embed-text` scores are fairly bunched together, so vague queries can rank oddly
|
|
249
|
+
- Two git repos with the same folder name share a project
|
|
250
|
+
- The UI can't run `memi reindex` yet. It shows the command to copy instead
|
|
251
|
+
|
|
252
|
+
## Development
|
|
253
|
+
|
|
254
|
+
To build from source:
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
git clone https://github.com/pinkpixel-dev/memi.git
|
|
258
|
+
cd memi
|
|
259
|
+
npm install
|
|
260
|
+
npm run build
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Run it with `node dist/cli/index.js`, or point an MCP client at it with an absolute path:
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
claude mcp add memi -s user -- node /absolute/path/to/memi/dist/cli/index.js serve
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Checks:
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
npm run typecheck
|
|
273
|
+
npm test
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
The tests are real ones. The end-to-end tests start the actual CLI and MCP server as child processes, and the UI tests run the real frontend modules against the real API. There are no mocks.
|
|
277
|
+
|
|
278
|
+
The semantic tests need Ollama running with `nomic-embed-text` pulled, and skip themselves if it isn't. One test, the embedder switch from 768 to 1024 dimensions, also needs a second model. It defaults to `qwen3-embedding:0.6b`, and you can use another with `MEMI_TEST_ALT_MODEL`. Without it, that one test is skipped.
|
|
279
|
+
|
|
280
|
+
The code is split up like this:
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
src/core/ storage, search, project detection, embeddings plumbing
|
|
284
|
+
src/embed/ Ollama and OpenAI providers
|
|
285
|
+
src/mcp/ the MCP server and its tools
|
|
286
|
+
src/cli/ the command line
|
|
287
|
+
src/ui/ the web UI's server
|
|
288
|
+
ui/ the web UI's files (plain HTML, CSS, and JavaScript, no build step)
|
|
289
|
+
skills/ the agent skills
|
|
290
|
+
tests/
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
Apache 2.0. See [LICENSE](LICENSE).
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
Made with 💖 by [Pink Pixel](https://pinkpixel.dev)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { CONFIG_KEYS, configPath, loadConfig, resolveAgent, resolveEmbedder, saveConfig, setConfigValue } from "../../core/config.js";
|
|
2
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
3
|
+
import { MemiError } from "../../core/types.js";
|
|
4
|
+
import { action } from "../util.js";
|
|
5
|
+
export const registerConfig = (program) => {
|
|
6
|
+
const config = program.command("config").description("Show or change settings");
|
|
7
|
+
config.action(action(() => {
|
|
8
|
+
const c = loadConfig();
|
|
9
|
+
const e = resolveEmbedder(c);
|
|
10
|
+
console.log(`Config file: ${configPath()}\n`);
|
|
11
|
+
console.log(`embedder.provider ${e.provider}`);
|
|
12
|
+
console.log(`embedder.model ${e.model}`);
|
|
13
|
+
console.log(`embedder.baseUrl ${e.baseUrl}`);
|
|
14
|
+
if (e.provider === "openai")
|
|
15
|
+
console.log(`OPENAI_API_KEY ${e.apiKey ? "set" : "not set"}`);
|
|
16
|
+
console.log(`agent ${resolveAgent(c) ?? "(none)"}`);
|
|
17
|
+
console.log(`ui.port ${c.ui.port}`);
|
|
18
|
+
}));
|
|
19
|
+
config
|
|
20
|
+
.command("set <key> <value>")
|
|
21
|
+
.description(`Set one of: ${CONFIG_KEYS.join(", ")}`)
|
|
22
|
+
.action(action((key, value) => {
|
|
23
|
+
let next;
|
|
24
|
+
try {
|
|
25
|
+
next = setConfigValue(loadConfig(), key, value);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new MemiError(err.message);
|
|
29
|
+
}
|
|
30
|
+
saveConfig(next);
|
|
31
|
+
console.log(`Set ${key} = ${value}`);
|
|
32
|
+
if (!key.startsWith("embedder."))
|
|
33
|
+
return;
|
|
34
|
+
const { warning } = openRuntime().store.status();
|
|
35
|
+
if (warning)
|
|
36
|
+
console.error(`\nWarning: ${warning}`);
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { configPath, dbPath, loadConfig, memiHome, resolveAgent, resolveEmbedder } from "../../core/config.js";
|
|
3
|
+
import { resolveProject } from "../../core/project.js";
|
|
4
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
5
|
+
import { action } from "../util.js";
|
|
6
|
+
export const registerDoctor = (program) => {
|
|
7
|
+
program
|
|
8
|
+
.command("doctor")
|
|
9
|
+
.description("Check the database, the embedder, and project detection")
|
|
10
|
+
.action(action(async () => {
|
|
11
|
+
let failed = false;
|
|
12
|
+
const line = (level, msg) => {
|
|
13
|
+
if (level === "fail")
|
|
14
|
+
failed = true;
|
|
15
|
+
console.log(`${level.padEnd(4)} ${msg}`);
|
|
16
|
+
};
|
|
17
|
+
const config = loadConfig();
|
|
18
|
+
const resolved = resolveEmbedder(config);
|
|
19
|
+
console.log(`Home: ${memiHome()} (config ${existsSync(configPath()) ? "found" : "not created yet, using defaults"})\n`);
|
|
20
|
+
const { store } = openRuntime();
|
|
21
|
+
const status = store.status();
|
|
22
|
+
line("ok", `Database at ${dbPath()}: ${status.total} memories`);
|
|
23
|
+
if (resolved.provider === "openai" && !resolved.apiKey)
|
|
24
|
+
line("fail", "OPENAI_API_KEY is not set");
|
|
25
|
+
else {
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
try {
|
|
28
|
+
const [vec] = await store.embedder.embed(["memi doctor check"], "query");
|
|
29
|
+
line("ok", `Embedder ${resolved.provider}/${resolved.model} replied in ${Date.now() - start}ms (${vec?.length} dimensions)`);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
line("fail", err.message);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const { state } = status;
|
|
36
|
+
if (state.status === "uninitialized")
|
|
37
|
+
line("ok", "No vectors yet. The index is created when you save your first memory.");
|
|
38
|
+
else if (state.status === "mismatch")
|
|
39
|
+
line("fail", status.warning ?? "The embedder changed. Run `memi reindex`.");
|
|
40
|
+
else {
|
|
41
|
+
line("ok", `Vector index matches (${state.stored.provider}/${state.stored.model}, ${state.stored.dimensions} dimensions)`);
|
|
42
|
+
const missing = status.total - status.vectors;
|
|
43
|
+
if (missing > 0)
|
|
44
|
+
line("warn", `${missing} memories have no vector. Run \`memi reindex --missing\`.`);
|
|
45
|
+
}
|
|
46
|
+
const agent = resolveAgent(config);
|
|
47
|
+
const project = resolveProject({ agent });
|
|
48
|
+
line(project ? "ok" : "warn", project ? `Project here: ${project.project} (from ${project.source})` : "No project detected here. Set MEMI_AGENT or run inside a git repo.");
|
|
49
|
+
console.log(` Agent name: ${agent ?? "(none)"}`);
|
|
50
|
+
if (failed)
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}));
|
|
53
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { formatHits, formatMemories, formatMemory } from "../../core/format.js";
|
|
2
|
+
import { pickScope, resolveProject } from "../../core/project.js";
|
|
3
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
4
|
+
import { MemiError } from "../../core/types.js";
|
|
5
|
+
import { action, confirm, printJson, splitTags, toInt } from "../util.js";
|
|
6
|
+
const SCOPES = ["both", "project", "global", "all"];
|
|
7
|
+
const checkScope = (v) => {
|
|
8
|
+
if (!SCOPES.includes(v))
|
|
9
|
+
throw new MemiError(`Scope must be one of: ${SCOPES.join(", ")}`);
|
|
10
|
+
return v;
|
|
11
|
+
};
|
|
12
|
+
const currentProject = (rt, project) => resolveProject({ project, agent: rt.agent })?.project ?? null;
|
|
13
|
+
export const registerMemories = (program) => {
|
|
14
|
+
program
|
|
15
|
+
.command("add <content...>")
|
|
16
|
+
.description("Save a memory")
|
|
17
|
+
.option("-c, --category <name>", "category (default: context)")
|
|
18
|
+
.option("-i, --importance <1-5>", "importance", toInt("Importance"))
|
|
19
|
+
.option("-t, --tags <a,b>", "comma-separated tags")
|
|
20
|
+
.option("--pin", "load this at the start of every session")
|
|
21
|
+
.option("--global", "save globally instead of for this project")
|
|
22
|
+
.option("-p, --project <name>", "project name (default: git repo or agent name)")
|
|
23
|
+
.option("--agent <name>", "agent name to record")
|
|
24
|
+
.action(action(async (content, o) => {
|
|
25
|
+
const rt = openRuntime();
|
|
26
|
+
const agent = o.agent ?? rt.agent;
|
|
27
|
+
const target = pickScope({ scope: o.global ? "global" : undefined, project: o.project, agent });
|
|
28
|
+
const r = await rt.store.add({
|
|
29
|
+
content: content.join(" "),
|
|
30
|
+
...target,
|
|
31
|
+
agent,
|
|
32
|
+
category: o.category,
|
|
33
|
+
importance: o.importance,
|
|
34
|
+
tags: splitTags(o.tags),
|
|
35
|
+
pinned: o.pin,
|
|
36
|
+
});
|
|
37
|
+
console.log(`Saved #${r.memory.id} (${target.scope === "global" ? "global" : `project ${target.project}`}, ${r.memory.category}, importance ${r.memory.importance}).`);
|
|
38
|
+
if (r.similar.length)
|
|
39
|
+
console.log(`\nSimilar memories already exist:\n\n${formatMemories(r.similar.map((s) => s.memory))}`);
|
|
40
|
+
if (r.warning)
|
|
41
|
+
console.error(`\nNote: ${r.warning}`);
|
|
42
|
+
}));
|
|
43
|
+
program
|
|
44
|
+
.command("search <query...>")
|
|
45
|
+
.description("Search memories by meaning and keyword")
|
|
46
|
+
.option("--mode <mode>", "hybrid, semantic or text", "hybrid")
|
|
47
|
+
.option("--scope <scope>", "both, project, global or all", checkScope, "both")
|
|
48
|
+
.option("-p, --project <name>", "project name")
|
|
49
|
+
.option("-c, --category <name>")
|
|
50
|
+
.option("--min-importance <1-5>", "", toInt("Importance"))
|
|
51
|
+
.option("-n, --limit <n>", "", toInt("Limit"), 10)
|
|
52
|
+
.option("--json", "print JSON")
|
|
53
|
+
.action(action(async (query, o) => {
|
|
54
|
+
const rt = openRuntime();
|
|
55
|
+
const r = await rt.store.search({
|
|
56
|
+
query: query.join(" "),
|
|
57
|
+
mode: o.mode,
|
|
58
|
+
scope: o.scope,
|
|
59
|
+
project: currentProject(rt, o.project),
|
|
60
|
+
category: o.category,
|
|
61
|
+
minImportance: o.minImportance,
|
|
62
|
+
limit: o.limit,
|
|
63
|
+
});
|
|
64
|
+
if (o.json)
|
|
65
|
+
return printJson(r);
|
|
66
|
+
console.log(r.hits.length ? formatHits(r.hits) : "No matching memories.");
|
|
67
|
+
if (r.note)
|
|
68
|
+
console.error(`\nNote: ${r.note}`);
|
|
69
|
+
}));
|
|
70
|
+
program
|
|
71
|
+
.command("list")
|
|
72
|
+
.description("List memories")
|
|
73
|
+
.option("--scope <scope>", "both, project, global or all", checkScope, "both")
|
|
74
|
+
.option("-p, --project <name>", "project name")
|
|
75
|
+
.option("-c, --category <name>")
|
|
76
|
+
.option("--min-importance <1-5>", "", toInt("Importance"))
|
|
77
|
+
.option("--pinned", "only pinned memories")
|
|
78
|
+
.option("--order <order>", "recent, oldest or importance", "recent")
|
|
79
|
+
.option("-n, --limit <n>", "", toInt("Limit"), 50)
|
|
80
|
+
.option("--offset <n>", "", toInt("Offset"), 0)
|
|
81
|
+
.option("--json", "print JSON")
|
|
82
|
+
.action(action(async (o) => {
|
|
83
|
+
const rt = openRuntime();
|
|
84
|
+
const r = rt.store.list({
|
|
85
|
+
scope: o.scope,
|
|
86
|
+
project: currentProject(rt, o.project),
|
|
87
|
+
category: o.category,
|
|
88
|
+
minImportance: o.minImportance,
|
|
89
|
+
pinned: o.pinned ? true : undefined,
|
|
90
|
+
order: o.order,
|
|
91
|
+
limit: o.limit,
|
|
92
|
+
offset: o.offset,
|
|
93
|
+
});
|
|
94
|
+
if (o.json)
|
|
95
|
+
return printJson(r);
|
|
96
|
+
console.log(r.items.length ? `${r.items.length} of ${r.total}\n\n${formatMemories(r.items)}` : "No memories found.");
|
|
97
|
+
}));
|
|
98
|
+
program
|
|
99
|
+
.command("update <id>")
|
|
100
|
+
.description("Change a memory")
|
|
101
|
+
.option("--content <text>")
|
|
102
|
+
.option("-c, --category <name>")
|
|
103
|
+
.option("-i, --importance <1-5>", "", toInt("Importance"))
|
|
104
|
+
.option("-t, --tags <a,b>", "replaces existing tags")
|
|
105
|
+
.option("--pin")
|
|
106
|
+
.option("--unpin")
|
|
107
|
+
.option("--global", "move to global")
|
|
108
|
+
.option("-p, --project <name>", "move to this project")
|
|
109
|
+
.action(action(async (id, o) => {
|
|
110
|
+
const rt = openRuntime();
|
|
111
|
+
const scope = o.global ? "global" : o.project ? "project" : undefined;
|
|
112
|
+
const r = await rt.store.update(toInt("Id")(id), {
|
|
113
|
+
content: o.content,
|
|
114
|
+
category: o.category,
|
|
115
|
+
importance: o.importance,
|
|
116
|
+
tags: splitTags(o.tags),
|
|
117
|
+
pinned: o.pin ? true : o.unpin ? false : undefined,
|
|
118
|
+
scope,
|
|
119
|
+
project: o.project,
|
|
120
|
+
});
|
|
121
|
+
if (!r)
|
|
122
|
+
throw new MemiError(`No memory with id ${id}.`);
|
|
123
|
+
console.log(`Updated.\n\n${formatMemory(r.memory)}`);
|
|
124
|
+
if (r.warning)
|
|
125
|
+
console.error(`\nNote: ${r.warning}`);
|
|
126
|
+
}));
|
|
127
|
+
program
|
|
128
|
+
.command("forget <id>")
|
|
129
|
+
.description("Delete a memory")
|
|
130
|
+
.option("-y, --yes", "skip the confirmation")
|
|
131
|
+
.action(action(async (id, o) => {
|
|
132
|
+
const rt = openRuntime();
|
|
133
|
+
const memory = rt.store.get(toInt("Id")(id));
|
|
134
|
+
if (!memory)
|
|
135
|
+
throw new MemiError(`No memory with id ${id}.`);
|
|
136
|
+
console.error(formatMemory(memory) + "\n");
|
|
137
|
+
if (!(await confirm("Delete this memory permanently?", o.yes)))
|
|
138
|
+
return console.error("Kept.");
|
|
139
|
+
rt.store.remove(memory.id);
|
|
140
|
+
console.log(`Deleted #${memory.id}.`);
|
|
141
|
+
}));
|
|
142
|
+
program
|
|
143
|
+
.command("categories")
|
|
144
|
+
.description("List categories and how many memories each holds")
|
|
145
|
+
.action(action(() => console.log(openRuntime().store.categories().map((c) => `${c.name} (${c.count})${c.description ? `: ${c.description}` : ""}`).join("\n"))));
|
|
146
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
2
|
+
import { MemiError } from "../../core/types.js";
|
|
3
|
+
import { action, confirm } from "../util.js";
|
|
4
|
+
export const registerReindex = (program) => {
|
|
5
|
+
program
|
|
6
|
+
.command("reindex")
|
|
7
|
+
.description("Rebuild the vector index with the current embedder")
|
|
8
|
+
.option("--missing", "only embed memories that have no vector yet, nothing is dropped")
|
|
9
|
+
.option("-y, --yes", "skip the confirmation")
|
|
10
|
+
.action(action(async (o) => {
|
|
11
|
+
const { store } = openRuntime();
|
|
12
|
+
const status = store.status();
|
|
13
|
+
if (!status.embedder)
|
|
14
|
+
throw new MemiError("No embedder configured.");
|
|
15
|
+
const name = `${status.embedder.provider}/${status.embedder.model}`;
|
|
16
|
+
if (o.missing) {
|
|
17
|
+
if (status.state.status === "mismatch")
|
|
18
|
+
throw new MemiError(status.warning ?? "The embedder changed. Run a full reindex.");
|
|
19
|
+
console.log(`Embedded ${await store.backfill()} memories with ${name}.`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (status.state.status === "mismatch") {
|
|
23
|
+
const { provider, model, dimensions } = status.state.stored;
|
|
24
|
+
console.error(`The embedder changed from ${provider}/${model} (${dimensions} dims) to ${name}.\n`);
|
|
25
|
+
}
|
|
26
|
+
console.error(`This drops the vector table and rebuilds it for ${name}, re-embedding all ${status.total} memories.\n` +
|
|
27
|
+
"The memories themselves are kept. If the embedder is unreachable, nothing is changed.\n");
|
|
28
|
+
if (!(await confirm("Rebuild the vector index?", o.yes)))
|
|
29
|
+
return console.error("Nothing changed.");
|
|
30
|
+
const result = await store.reindex((done, total) => process.stderr.write(`\rEmbedding ${done}/${total}`));
|
|
31
|
+
process.stderr.write("\n");
|
|
32
|
+
console.log(`Rebuilt ${result.count} vectors with ${name} (${result.dimensions} dimensions).`);
|
|
33
|
+
}));
|
|
34
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
2
|
+
import { serveStdio } from "../../mcp/server.js";
|
|
3
|
+
import { action } from "../util.js";
|
|
4
|
+
export const registerServe = (program) => {
|
|
5
|
+
program
|
|
6
|
+
.command("serve")
|
|
7
|
+
.description("Run the MCP server over stdio")
|
|
8
|
+
.action(action(async () => serveStdio(openRuntime())));
|
|
9
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { openRuntime } from "../../core/runtime.js";
|
|
3
|
+
import { startUi } from "../../ui/server.js";
|
|
4
|
+
import { action, toInt } from "../util.js";
|
|
5
|
+
function openBrowser(url) {
|
|
6
|
+
const [cmd, args] = process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["cmd", ["/c", "start", "", url]] : ["xdg-open", [url]];
|
|
7
|
+
spawn(cmd, args, { detached: true, stdio: "ignore" })
|
|
8
|
+
.on("error", () => console.error(`Could not open a browser. Visit ${url}`))
|
|
9
|
+
.unref();
|
|
10
|
+
}
|
|
11
|
+
export const registerUi = (program) => {
|
|
12
|
+
program
|
|
13
|
+
.command("ui")
|
|
14
|
+
.description("Run the local memory manager")
|
|
15
|
+
.option("-p, --port <n>", "port (default: ui.port from config, 4747)", toInt("Port"))
|
|
16
|
+
.option("--open", "open it in your browser")
|
|
17
|
+
.action(action(async (o) => {
|
|
18
|
+
const rt = openRuntime();
|
|
19
|
+
const ui = await startUi(rt, { port: o.port ?? rt.config.ui.port });
|
|
20
|
+
console.log(`memi UI running at ${ui.url}\nPress Ctrl+C to stop.`);
|
|
21
|
+
if (o.open)
|
|
22
|
+
openBrowser(ui.url);
|
|
23
|
+
process.once("SIGINT", () => ui.close().then(() => process.exit(0)));
|
|
24
|
+
process.once("SIGTERM", () => ui.close().then(() => process.exit(0)));
|
|
25
|
+
}));
|
|
26
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { APP_VERSION } from "../lib/app-info.js";
|
|
4
|
+
import { registerConfig } from "./commands/config.js";
|
|
5
|
+
import { registerDoctor } from "./commands/doctor.js";
|
|
6
|
+
import { registerMemories } from "./commands/memories.js";
|
|
7
|
+
import { registerReindex } from "./commands/reindex.js";
|
|
8
|
+
import { registerServe } from "./commands/serve.js";
|
|
9
|
+
import { registerUi } from "./commands/ui.js";
|
|
10
|
+
import { exitOnKnownError } from "./util.js";
|
|
11
|
+
const program = new Command().name("memi").description("Local-first memory for AI agents").version(APP_VERSION);
|
|
12
|
+
for (const register of [registerServe, registerMemories, registerReindex, registerConfig, registerDoctor, registerUi])
|
|
13
|
+
register(program);
|
|
14
|
+
try {
|
|
15
|
+
// Option parsers such as --scope and --importance throw while parsing, before any command action runs.
|
|
16
|
+
await program.parseAsync();
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
exitOnKnownError(err);
|
|
20
|
+
}
|