open-memex 0.1.0 → 0.3.0-alpha

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.
@@ -0,0 +1,307 @@
1
+ # open-memex
2
+
3
+ [English](./README.md)
4
+
5
+ 给 AI 编程助手的本地优先持久记忆:一个 [opencode](https://opencode.ai) 插件,
6
+ 加上一个通用 MCP server(VS Code Copilot、Cursor、Claude Code、Visual Studio 等)。
7
+
8
+ - **Markdown 文件**是 source of truth(人类可读、git 友好)
9
+ - **SQLite FTS5** 做可重建索引(BM25 关键词检索,`better-sqlite3`)
10
+ - **零云端**、零账号、零第三方 API
11
+ - 直接跑在 opencode 内嵌的 Bun 运行时里;CLI 和 MCP server 跑在 Node 下——无需构建、无需安装 Bun
12
+
13
+ ## 安装
14
+
15
+ ### 前置要求
16
+
17
+ - **Node.js ≥ 22.6**(`open-memex doctor` 会帮你检查)
18
+
19
+ ### 第一步——安装 CLI
20
+
21
+ **npm(推荐):**
22
+
23
+ ```sh
24
+ npm install -g open-memex@alpha
25
+ ```
26
+
27
+ 安装的是 `0.3.0-alpha` 预览通道。(`latest` 仍指向旧的 `0.1.0` 稳定版。)
28
+
29
+ **免安装——用 npx 直接跑:**
30
+
31
+ ```sh
32
+ npx -y open-memex@alpha <命令> # 例如 npx -y open-memex@alpha init --client vscode
33
+ ```
34
+
35
+ **从源码安装**(最新开发版,`V2-dev-p2` 分支):
36
+
37
+ ```sh
38
+ git clone -b V2-dev-p2 https://github.com/stoneskin/open-memex.git
39
+ cd open-memex
40
+ npm install
41
+ node --experimental-strip-types src/cli.ts <命令>
42
+ ```
43
+
44
+ > `0.3.0-alpha` 的 npm 发布从该分支切出——如果 npx 还解析到旧的 alpha 版,
45
+ > 请先用源码安装,等发布落地。
46
+
47
+ #### 提示 "'open-memex' 不是内部命令"?——PATH 设置
48
+
49
+ `npm install -g` 会把 `open-memex` 启动器放到 npm 的全局 bin 目录。
50
+ 如果终端找不到它,说明该目录不在你的 `PATH` 里:
51
+
52
+ 1. 先找到这个目录:`npm config get prefix`
53
+ - **Windows:** 启动器(`open-memex.cmd`)就在该目录下,例如
54
+ `C:\Users\<你>\AppData\Roaming\npm`
55
+ - **macOS / Linux:** 在 `<prefix>/bin` 下,例如 `/usr/local/bin`
56
+ 或 `~/.nvm/versions/node/v22.x.x/bin`
57
+ 2. 把它加进 `PATH`:
58
+ - **Windows:** 设置 → 系统 → 关于 → 高级系统设置 → 环境变量 →
59
+ 把该目录加到*用户*的 `Path` 里 → **重启终端**。用 `where open-memex` 验证。
60
+ - **macOS / Linux:** 在 `~/.zshrc`(或 `~/.bashrc`)里加一行
61
+ `export PATH="$(npm prefix -g)/bin:$PATH"`,重启 shell,
62
+ 用 `command -v open-memex` 验证。
63
+ 3. 没有管理员权限 / 不想动 `PATH`?用上面的 npx 形式——npx 自己解析包,
64
+ 不需要改 `PATH`。
65
+
66
+ ### 第二步——给你的编辑器一键配置
67
+
68
+ 在**项目根目录**下运行(这样 project scope 会解析到这个仓库):
69
+
70
+ **VS Code**(Copilot):
71
+
72
+ ```sh
73
+ open-memex init --client vscode
74
+ # ……没装全局包的话:
75
+ npx -y open-memex@alpha init --client vscode
76
+ ```
77
+
78
+ 自动写 `.vscode/mcp.json` 和 `.github/copilot-instructions.md`,然后重新加载窗口,
79
+ 在 Copilot Chat 的 MCP 面板里确认 `open-memex` server 已启动。
80
+
81
+ **Cursor:**
82
+
83
+ ```sh
84
+ open-memex init --client cursor
85
+ ```
86
+
87
+ 自动写 `.cursor/mcp.json` 和 `.github/copilot-instructions.md`。
88
+
89
+ **opencode**(作为普通 MCP 客户端):
90
+
91
+ ```sh
92
+ open-memex init --client opencode
93
+ ```
94
+
95
+ 写项目级 `opencode.jsonc`(`type: "local"`)。想用原生插件?
96
+ 在 `~/.config/opencode/opencode.jsonc` 里加
97
+ `"plugin": ["file:///absolute/path/to/open-memex/src/index.ts"]`——
98
+ 在 tools 之外还能获得关键词自动捕获和首轮上下文注入。
99
+
100
+ **Claude Code**(在项目根目录运行):
101
+
102
+ ```sh
103
+ claude mcp add open-memex -- open-memex mcp
104
+ # ……或打印配置片段:open-memex mcp --print-config claude
105
+ ```
106
+
107
+ **Visual Studio**(在 solution 目录运行):
108
+
109
+ ```sh
110
+ open-memex init --client visualstudio
111
+ ```
112
+
113
+ 写 solution 级 `.mcp.json` 和 `.github/copilot-instructions.md`。需要
114
+ Visual Studio 2022 17.14+ 或 Visual Studio 2026(**仅 Windows**)。
115
+ Visual Studio 也会自动发现 `.vscode/mcp.json` 和 `.cursor/mcp.json`,
116
+ 所以上面的 VS Code 配置同样可用。
117
+
118
+ **Codex:** 暂无 `init` 客户端——以 `open-memex mcp --print-config` 为起点手动添加
119
+ (`config.toml` 的 `[mcp_servers]`,或 `codex mcp add`)。
120
+
121
+ `init` 说明:
122
+
123
+ - 在终端里会交互式询问:配哪个编辑器、是否开启关键词自动捕获、
124
+ 是否在首轮注入记忆。`--yes` 全用默认值;脚本 / 非 TTY 环境不提问
125
+ (编辑器默认 VS Code)。
126
+ - 已有配置文件会被**合并,不会被覆盖**——重复运行是安全的。
127
+ `--force` 强制覆盖。
128
+ - 如果 `PATH` 上没有可用的 `open-memex`(比如一次性 npx),`init` 会把
129
+ `npx -y open-memex@alpha mcp` 写进配置,配置照样能用。
130
+ 以后 `npm i -g open-memex@alpha` + `open-memex init --force` 可切换到更快
131
+ 的直接调用。
132
+
133
+ ### 第三步——验证
134
+
135
+ ```sh
136
+ open-memex doctor
137
+ ```
138
+
139
+ 检查:Node 版本、配置来源、当前目录的 scope 解析、存储可写性,
140
+ 然后启动一个真实的 MCP server 做 `initialize` + `tools/list`——
141
+ 五个 tools 都必须出现。
142
+
143
+ ## Agent 可用的 tools
144
+
145
+ | Tool | 作用 |
146
+ |---|---|
147
+ | `memory_add` | 保存事实、偏好、决定、笔记 |
148
+ | `memory_search` | BM25 关键词检索,跨 project + personal 记忆 |
149
+ | `memory_list` | 按 scope 列出记忆,最新的在前 |
150
+ | `memory_supersede` | 用新版本替换一条记忆(保留替换链) |
151
+ | `memory_forget` | 按 id 删除一条记忆 |
152
+
153
+ ## 捕获(Capture)
154
+
155
+ - **关键词触发**(opencode 原生插件,扫描用户消息):中文 `记住…` /
156
+ `记一下` / `记录一下` / `别忘了…`,英文 `remember …` / `note that …` /
157
+ `don't forget …` / `TIL …` / `save this …`。
158
+ Scope 路由:第一人称单数进 **personal**(`记住我…`、`替我记…`、
159
+ `我觉得…`、`我喜欢…`、`remember for me`);第一人称复数进当前
160
+ **project** scope(`我们认为…`、`我们决定…`、`帮我们记住…`)。
161
+ - **Agent 主动调用** `memory_add`
162
+ - **脱敏**:`<private>…</private>` 标签内的内容会被剥离;检测到的密钥
163
+ (API key、token、高熵凭据)就地打码——保留前 4 个字符,其余替换为 `x`——
164
+ 然后照常保存。用 `open-memex capture --dry-run "…"` 预览一条消息会被如何捕获。
165
+
166
+ ## Scope
167
+
168
+ - **project** — 绑定当前仓库(用 git origin URL 哈希做 key,无 remote 则用 cwd)。新记忆默认进这里。
169
+ - **personal** — 跨所有项目全局,**仅本机,永不上传/同步**。放个人偏好。(v1 叫 `user`,`migrate --to-v2` 会自动改名。)
170
+
171
+ 完整 scope 模型(key 推导、迁移、visibility、保留名)见
172
+ [docs/SCOPES.md](./docs/SCOPES.md)。
173
+
174
+ ## 检索(Retrieval)
175
+
176
+ 每个会话的首轮,`open-memex` 会往 system prompt 里注入一个 `[OPEN-MEMEX]` 块,
177
+ 包含 top-N 最新 project 记忆 + top-N 个人偏好。Agent 也可以随时调用
178
+ `memory_search` 按需检索。
179
+
180
+ ## 存储布局
181
+
182
+ ```
183
+ %APPDATA%\open-memex\ (Windows)
184
+ $XDG_DATA_HOME/open-memex/ (Linux/macOS)
185
+ ├── index.db # SQLite FTS5 索引(可重建)
186
+ └── memories/
187
+ ├── personal/
188
+ │ └── <id>.md
189
+ └── project__<name>__<hash12>/
190
+ └── <id>.md
191
+ ```
192
+
193
+ 每个 `.md` 文件是 v2 YAML frontmatter(`id, scope, scope_key, visibility, role,
194
+ type, importance, status, tags, created_at, updated_at, schema_version` 等)+
195
+ 记忆正文。可以手工编辑——插件启动时按文件 mtime 重新同步。
196
+ Markdown 是 source of truth,SQLite 索引是派生的、可重建的
197
+ (`open-memex reindex`)。
198
+
199
+ ## 配置(Config)
200
+
201
+ 可选文件 `~/.config/opencode/open-memex.jsonc`(可用 `MY_O_MEMORY_CONFIG`
202
+ 改路径;`MY_O_MEMORY_HOME` 改存储根目录)。
203
+
204
+ 默认值:
205
+
206
+ ```jsonc
207
+ {
208
+ "maxProjectMemories": 8, // 首轮注入的 project 记忆条数
209
+ "maxProfileItems": 5, // 首轮注入的个人偏好条数
210
+ "injectOnFirstTurn": true, // [OPEN-MEMEX] system-prompt 块
211
+ "keywordCaptureEnabled": true,
212
+ "logLevel": "info" // info | debug
213
+ }
214
+ ```
215
+
216
+ `open-memex config` 打印生效配置(默认值 + 文件)。
217
+ 安装后改设置:
218
+
219
+ ```sh
220
+ open-memex config set keywordCaptureEnabled false
221
+ open-memex config set maxProjectMemories 12
222
+ ```
223
+
224
+ 可设置的 key:`maxProjectMemories`、`maxProfileItems`、`injectOnFirstTurn`、
225
+ `keywordCaptureEnabled`、`logLevel`。完整设计见
226
+ [docs/V2-DESIGN.md](./docs/V2-DESIGN.md)。
227
+
228
+ ## CLI 参考
229
+
230
+ 安装与健康检查:
231
+
232
+ ```sh
233
+ open-memex init [--client vscode|cursor|opencode|visualstudio] [--force] [--yes]
234
+ open-memex config # 打印生效配置
235
+ open-memex config set <key> <value> # 改设置
236
+ open-memex doctor # 环境健康检查
237
+ open-memex capture --dry-run "记住我喜欢简洁的回答" # 预览关键词捕获
238
+ open-memex mcp --print-config vscode|cursor|claude|opencode|visualstudio
239
+ ```
240
+
241
+ 记忆操作:
242
+
243
+ ```sh
244
+ open-memex add "This repo uses better-sqlite3" --type project-config
245
+ open-memex search "auth flow"
246
+ open-memex list --scope project
247
+ open-memex supersede <id> "Updated content"
248
+ open-memex status <id> deprecated
249
+ open-memex forget <id>
250
+ ```
251
+
252
+ 维护:
253
+
254
+ ```sh
255
+ open-memex where # 显示存储与配置文件路径
256
+ open-memex scopes # 列出 project scope 及记忆条数
257
+ open-memex reindex # 从 markdown 重建 SQLite 索引
258
+ open-memex migrate --to-v2 [--dry-run] # v1 数据 → v2
259
+ ```
260
+
261
+ CLI 跑在 Node 22 内置的实验性 TypeScript loader 下(无需构建)。
262
+ 从源码 checkout 使用时,每条命令前加
263
+ `node --experimental-strip-types src/cli.ts`(简单场景也可用
264
+ `npm run cli -- <命令>`——但 npm 会吞掉未知的 `--flag` 参数,
265
+ 所以推荐直接用 `node`)。
266
+
267
+ ## MCP server
268
+
269
+ 同一个五个 memory tools,走 Model Context Protocol 的 stdio server——
270
+ 不需要宿主专属插件,任何 MCP 客户端都能用 open-memex。
271
+
272
+ ```sh
273
+ open-memex mcp # 全局安装后
274
+ npx -y open-memex@alpha mcp # 免安装
275
+ ```
276
+
277
+ project scope 从进程工作目录解析,所以配置 server 时 cwd 要指向项目根目录
278
+ (`init` 会帮你处理好)。
279
+
280
+ > **注意:** MCP 是请求/响应式的——它给 agent 提供 tools,但没有 opencode
281
+ > 插件的关键词自动捕获和首轮上下文注入。想让 agent 主动用记忆,
282
+ > 靠的是 agent 的 instructions(`init` 写的 `.github/copilot-instructions.md`)。
283
+
284
+ ## 路线图(Roadmap)
285
+
286
+ **`0.3.0-alpha`(本版):** 通用 MCP server、`open-memex` bin/CLI、
287
+ 一键 `init` 配置、中文关键词捕获(含 personal/project 路由)、
288
+ `config` / `capture --dry-run` / `doctor` 助手命令、Visual Studio 支持。
289
+
290
+ **Coming —— `0.3.0-beta`:** 团队同步——用 git 做共享记忆
291
+ (`propose` / `promote` / `resolve` 工作流、仓库内记忆目录),
292
+ 找 1–2 个同事做 pilot。
293
+
294
+ **Coming —— `0.3.0`(稳定版):** 组织层——组织记忆仓库、
295
+ curator 约定、distill-to-AGENTS.md 辅助。
296
+
297
+ **未来(看信号再定,不承诺版本):** 原生 agent 插件
298
+ (Claude Code / Codex hooks,作为同一套 MCP tools 的增强路径);
299
+ 本地 embedding 做基准测试门控的实验(**未经明确 opt-in 绝不下载
300
+ embedding 模型**);云端 `RemoteProvider` 定制只在多仓库共享、
301
+ ACL 或合规需求出现时才做。
302
+
303
+ 设计细节:[docs/V2-DESIGN.md](./docs/V2-DESIGN.md)(append-only 决策日志 D1–D20)。
304
+
305
+ ## 许可证
306
+
307
+ [Apache-2.0](./LICENSE)
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ // `open-memex` bin launcher: re-execs src/cli.ts with TypeScript type-stripping
3
+ // enabled, so the command works on Node 22.6+ without the user passing
4
+ // --experimental-strip-types themselves. (Plain JS — no build step.)
5
+ import { spawn } from "node:child_process";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+
9
+ const entry = path.join(
10
+ path.dirname(fileURLToPath(import.meta.url)),
11
+ "..",
12
+ "src",
13
+ "cli.ts",
14
+ );
15
+
16
+ const child = spawn(
17
+ process.execPath,
18
+ ["--experimental-strip-types", entry, ...process.argv.slice(2)],
19
+ { stdio: "inherit" },
20
+ );
21
+ child.on("error", (err) => {
22
+ console.error(`open-memex: failed to start: ${err.message}`);
23
+ process.exit(1);
24
+ });
25
+ child.on("exit", (code, signal) => {
26
+ if (signal) process.kill(process.pid, signal);
27
+ else process.exit(code ?? 0);
28
+ });
package/docs/SCOPES.md ADDED
@@ -0,0 +1,81 @@
1
+ # Scopes
2
+
3
+ **Scope = ownership** (who owns the memory). It answers "whose memory is
4
+ this and where does it live", not "who may read it" — that's `visibility`,
5
+ a separate v2 field (see below).
6
+
7
+ ## The three scopes
8
+
9
+ | Scope | Owner | Lives where | Synced? |
10
+ |------------|------------------|--------------------------------------|----------------|
11
+ | `personal` | you | this machine only (`memories/personal/`) | **never** |
12
+ | `project` | repo collaborators | this machine, keyed by repo (`memories/project__<name>__<hash>/`) | via git, only if you opt in |
13
+ | `org` | org members | dedicated org memory repo (planned) | via git (planned) |
14
+
15
+ **`personal` never leaves the machine.** No sync, no upload, no exceptions.
16
+ Put anything here that should never be shared: credentials-adjacent notes,
17
+ private preferences, personal instructions.
18
+
19
+ **`project`** is the default for new memories. It is keyed off the repo, so
20
+ the same project resolves to the same scope on every machine.
21
+
22
+ **`org`** is reserved for a future dedicated org memory repo. The schema
23
+ accepts it; the CLI does not create org scopes yet.
24
+
25
+ ## How the project key is derived
26
+
27
+ `resolveProjectScope(cwd)` (`src/scope.ts`):
28
+
29
+ 1. Read `git config --get remote.origin.url` in the cwd.
30
+ 2. If a remote exists: normalize it (strip `.git`, `git@host:` → `https://host/`,
31
+ lowercase) and take `sha256(normalized).slice(0, 12)` as the key suffix.
32
+ The project name comes from the last URL path segment.
33
+ 3. If no remote: fall back to the absolute cwd path (lowercased), hashed the
34
+ same way. This is also how v1 "legacy" scopes are detected during
35
+ migration when a repo gains a remote later.
36
+
37
+ Key format: `project__<sanitized-name>__<12-hex-chars>`, e.g.
38
+ `project__open-memex__9e2a8a546c21`. The 12-char hash keeps collisions
39
+ astronomically unlikely while staying readable in `ls`.
40
+
41
+ ## CLI
42
+
43
+ ```
44
+ open-memex add "content" --scope personal # default is project
45
+ open-memex list --scope personal
46
+ open-memex search "query" --scope both # project + personal
47
+ open-memex scopes # list all known scope dirs
48
+ open-memex migrate --from <old-key> --to <new-key> [--dry-run]
49
+ ```
50
+
51
+ `--scope user` is accepted as a deprecated alias of `--scope personal`.
52
+
53
+ When a repo gains a git remote after memories were already stored under the
54
+ cwd-based key, `migrate --from <cwd-key> --to <remote-key>` moves them
55
+ (`scopes` shows you the exact keys). Nothing is automatic — you run it
56
+ explicitly, previewing with `--dry-run` first.
57
+
58
+ ## v1 → v2 rename
59
+
60
+ v1's `user` scope is renamed to `personal` in v2 (design §19 — "user" was
61
+ ambiguous next to "org members are users too"). `open-memex migrate --to-v2`
62
+ moves `memories/user/` → `memories/personal/` and rewrites the frontmatter
63
+ (`scope: user` → `scope: personal`). A dated backup of the pre-migration
64
+ tree is kept. Reads remain backward compatible: a v1 file with `scope: user`
65
+ is interpreted as `personal`.
66
+
67
+ ## Visibility (planned, not yet enforced)
68
+
69
+ v2 frontmatter carries a separate `visibility` field (`private` | `internal` |
70
+ `shared`). The intended rule: `visibility: private` inside a shared scope is
71
+ **physically isolated** — written to a local-only cache directory, never
72
+ under `.open-memex/` — rather than relying on `.gitignore`. This is not
73
+ implemented yet; today, treat `personal` as the only confidentiality
74
+ boundary and review anything you place under `.open-memex/` before pushing.
75
+
76
+ ## Reserved names
77
+
78
+ `team` and `public` are reserved scope names: the schema rejects writes to
79
+ them. Rationale: `project` already expresses team sharing; a distinct `team`
80
+ scope needs a clear semantic difference (e.g. cross-repo) before it earns
81
+ existence.