open-memex 0.1.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/AGENTS.md +73 -0
- package/LICENSE +198 -0
- package/PLAN.md +168 -0
- package/README.md +95 -0
- package/package.json +31 -0
- package/scripts/smoke-pure.ts +91 -0
- package/src/capture/keywords.ts +34 -0
- package/src/cli.ts +264 -0
- package/src/config.ts +67 -0
- package/src/index.ts +106 -0
- package/src/paths.ts +45 -0
- package/src/redact.ts +24 -0
- package/src/retrieve/inject.ts +35 -0
- package/src/retrieve/search.ts +115 -0
- package/src/scope.ts +76 -0
- package/src/store/db.ts +98 -0
- package/src/store/markdown.ts +100 -0
- package/src/store/migrate.ts +141 -0
- package/src/store/sync.ts +83 -0
- package/src/tools/memory.ts +174 -0
- package/tsconfig.json +17 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
Local-first memory plugin for opencode. See `README.md` and `PLAN.md` for user-facing docs and the roadmap. This file lists only the non-obvious things an agent needs to work in this repo.
|
|
4
|
+
|
|
5
|
+
## Runtime model — read before touching anything
|
|
6
|
+
|
|
7
|
+
Two runtimes execute the same TypeScript source, with **no build step**:
|
|
8
|
+
|
|
9
|
+
- **opencode host** loads `src/index.ts` under embedded **Bun**. SQLite here is `bun:sqlite` (built-in).
|
|
10
|
+
- **CLI** (`src/cli.ts`) and smoke test run under **Node 22+** with `--experimental-strip-types`. SQLite here is `better-sqlite3` (native module).
|
|
11
|
+
|
|
12
|
+
`src/store/db.ts` picks the backend at runtime by sniffing `globalThis.Bun`. Both backends share the same surface (`new Database(path)`, `.exec`, `.prepare().run/all/get`, `.close`). Any DB code you write must stay on that common subset — do not import `better-sqlite3` or `bun:sqlite` directly outside `db.ts`.
|
|
13
|
+
|
|
14
|
+
Consequences:
|
|
15
|
+
- Imports **must** use explicit `.ts` extensions (`allowImportingTsExtensions: true`, `moduleResolution: "Bundler"`).
|
|
16
|
+
- No transpile / bundle output. Do not add one; opencode loads the `.ts` file directly.
|
|
17
|
+
- Adding a native dep means it must work under both Bun's N-API compat and Node.
|
|
18
|
+
|
|
19
|
+
## Commands
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
npm install # once
|
|
23
|
+
npm run typecheck # tsc --noEmit — the only lint/type gate
|
|
24
|
+
npm run cli -- where | list | search "q" | add ... | forget <id> | reindex
|
|
25
|
+
node --experimental-strip-types scripts\smoke-pure.ts # runs pure-logic checks (no sqlite)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
There is **no `npm test`** and no CI. Verification loop is: `npm run typecheck` + `smoke-pure.ts` + (if touching sqlite) `npm run cli -- reindex` against a scratch `MY_O_MEMORY_HOME`.
|
|
29
|
+
|
|
30
|
+
To load the plugin in opencode locally, `~/.config/opencode/opencode.jsonc` must have:
|
|
31
|
+
```
|
|
32
|
+
"plugin": ["file:///c:/github/open-memex/src/index.ts"]
|
|
33
|
+
```
|
|
34
|
+
Restart opencode after any change — the plugin is not hot-reloaded.
|
|
35
|
+
|
|
36
|
+
## Data lives outside the repo
|
|
37
|
+
|
|
38
|
+
Storage root:
|
|
39
|
+
- Windows: `%APPDATA%\open-memex\`
|
|
40
|
+
- Linux/macOS: `$XDG_DATA_HOME/open-memex/` (fallback `~/.local/share/open-memex/`)
|
|
41
|
+
- Override with `MY_O_MEMORY_HOME` (use this for tests to avoid clobbering real data).
|
|
42
|
+
|
|
43
|
+
Layout: `memories/<scope_key>/<id>.md` (YAML frontmatter + body) + `index.db` (SQLite FTS5).
|
|
44
|
+
|
|
45
|
+
**Markdown files are the source of truth; `index.db` is rebuildable.** `syncScope()` (in `src/store/sync.ts`) reconciles the two by mtime on plugin load and before every CLI read. When adding new frontmatter fields, update `Frontmatter` in `src/store/markdown.ts`, the `memories` table in `src/store/db.ts`, and `upsertFromFile` in `src/store/sync.ts` together, and bump the reindex path.
|
|
46
|
+
|
|
47
|
+
## Scope keys
|
|
48
|
+
|
|
49
|
+
- `user` scope key is literal `"user"`.
|
|
50
|
+
- `project` scope key: `project__<sanitized-name>__<12-hex-sha256>`, seeded from normalized git origin URL, else lowercased cwd. See `src/scope.ts`. Same repo across machines → same key (intentional; enables future git-commit of memories).
|
|
51
|
+
- The scope key **changes** when a repo gains/loses a git origin. `src/index.ts` logs a one-shot warning on load if it finds files under the legacy cwd-only key (`resolveCwdScope`). Use `cli scopes` to enumerate all scope dirs and `cli migrate --from <old>` to reconcile — see `src/store/migrate.ts`. No auto-migration; two unrelated repos at the same cwd would silently merge.
|
|
52
|
+
- Read-only callers must use `memoriesDirPath` (in `src/paths.ts`), never `memoriesDirFor`. The latter `mkdir -p`s the directory as a side effect and will pollute storage with empty scope dirs.
|
|
53
|
+
|
|
54
|
+
## Capture / write path invariants
|
|
55
|
+
|
|
56
|
+
Every write path (tool, keyword hook, CLI `add`) must:
|
|
57
|
+
1. Call `redact(content, cfg.redactPatterns)`.
|
|
58
|
+
2. If `hadSecret` → refuse the write (do not save `[REDACTED]` unless the user wrapped it in `<private>…</private>`).
|
|
59
|
+
3. `writeMemoryFile` first, then `readMemoryFile` + `upsertFromFile` to keep FTS in sync.
|
|
60
|
+
|
|
61
|
+
Keyword capture fires from `chat.message` on the assistant's `output.parts` text. Patterns live in `src/capture/keywords.ts` / config `keywordPatterns`; regex group 1 is the memory body.
|
|
62
|
+
|
|
63
|
+
Context injection happens exactly once per session in `experimental.chat.system.transform`, guarded by an in-memory `Set<sessionID>` in `src/index.ts`. It is not persisted — restarting opencode re-injects on the next first turn.
|
|
64
|
+
|
|
65
|
+
## Design constraints from PLAN.md worth honoring
|
|
66
|
+
|
|
67
|
+
MVP is intentionally: no cloud, no embeddings/vector search, no LLM-driven extraction, no knowledge graph. Do not add these without updating `PLAN.md`. Items 1–8 in the v2 roadmap have a rough priority order; prefer extending existing modules over new top-level concepts.
|
|
68
|
+
|
|
69
|
+
## Style notes
|
|
70
|
+
|
|
71
|
+
- `strict: true`, `verbatimModuleSyntax: false`. Prefer `import type` for types anyway.
|
|
72
|
+
- Log prefix is `[open-memex]`. Gate verbose logs behind `cfg.logLevel === "debug"`.
|
|
73
|
+
- Windows is a first-class target (this workspace is Windows). Use `node:path` and never hardcode `/`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
Use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format.
|
|
185
|
+
|
|
186
|
+
Copyright 2026 Wei Sun
|
|
187
|
+
|
|
188
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
189
|
+
you may not use this file except in compliance with the License.
|
|
190
|
+
You may obtain a copy of the License at
|
|
191
|
+
|
|
192
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
193
|
+
|
|
194
|
+
Unless required by applicable law or agreed to in writing, software
|
|
195
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
196
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
197
|
+
See the License for the specific language governing permissions and
|
|
198
|
+
limitations under the License.
|
package/PLAN.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# open-memex — Design & Roadmap
|
|
2
|
+
|
|
3
|
+
## Goal
|
|
4
|
+
|
|
5
|
+
A local-only persistent memory plugin for opencode. No SaaS, no account, no
|
|
6
|
+
network calls. Memories live as human-readable markdown files, indexed by
|
|
7
|
+
SQLite FTS5 for BM25 keyword search.
|
|
8
|
+
|
|
9
|
+
## Non-goals (MVP)
|
|
10
|
+
|
|
11
|
+
- No cloud sync
|
|
12
|
+
- No embeddings / vector search (v2)
|
|
13
|
+
- No LLM-driven memory extraction (v2)
|
|
14
|
+
- No knowledge graph / entity linking
|
|
15
|
+
- No multi-user collaboration
|
|
16
|
+
|
|
17
|
+
## Architecture
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
+-----------------------------+
|
|
21
|
+
| opencode (Bun) |
|
|
22
|
+
| |
|
|
23
|
+
| chat.message hook ------> keyword capture ---+
|
|
24
|
+
| system.transform ------> context inject <---|--- memory files
|
|
25
|
+
| tool.memory_* ------> add/search/list/forget (source of truth)
|
|
26
|
+
+-----------------------------+ |
|
|
27
|
+
v
|
|
28
|
+
+----------------+
|
|
29
|
+
| SQLite index |
|
|
30
|
+
| (FTS5, BM25) |
|
|
31
|
+
+----------------+
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- `memories` — one file per memory. YAML frontmatter + body.
|
|
35
|
+
- `index.db` — SQLite (via `better-sqlite3`) with FTS5 virtual table over content/tags/type.
|
|
36
|
+
- Loads under opencode's embedded Bun runtime (better-sqlite3 works via N-API compat).
|
|
37
|
+
- CLI runs under Node 22+ with `--experimental-strip-types` — no Bun required, no build step.
|
|
38
|
+
|
|
39
|
+
## Data model
|
|
40
|
+
|
|
41
|
+
### File on disk
|
|
42
|
+
|
|
43
|
+
`memories/<scope_key>/<id>.md`
|
|
44
|
+
|
|
45
|
+
```markdown
|
|
46
|
+
---
|
|
47
|
+
id: 01HZ...
|
|
48
|
+
scope_key: project__open-memex__a1b2c3d4e5f6
|
|
49
|
+
scope_kind: project
|
|
50
|
+
project_name: open-memex
|
|
51
|
+
type: project-config
|
|
52
|
+
tags: [bun, sqlite]
|
|
53
|
+
source: tool
|
|
54
|
+
created_at: 1770000000000
|
|
55
|
+
updated_at: 1770000000000
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
This project runs under Bun and uses bun:sqlite. Do not add better-sqlite3.
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### SQLite tables
|
|
62
|
+
|
|
63
|
+
- `memories(id PK, scope_key, scope_kind, project_name, type, tags CSV, content, source, file_path, mtime_ms, created_at, updated_at)`
|
|
64
|
+
- `memories_fts` — FTS5 virtual table over `content`, `tags`, `type`, external-content mode pointing at `memories.rowid`.
|
|
65
|
+
- Triggers keep `memories_fts` in sync on insert/update/delete.
|
|
66
|
+
|
|
67
|
+
## Scoping
|
|
68
|
+
|
|
69
|
+
- **user** scope key: literal `"user"`
|
|
70
|
+
- **project** scope key: `project__<sanitized-name>__<12-hex-of-sha256>`
|
|
71
|
+
- Seed: normalized git origin URL if present, else absolute cwd path (lowercased)
|
|
72
|
+
- Same repo across machines -> same key, so memories can be `git`-committed (future)
|
|
73
|
+
|
|
74
|
+
### Scope-key drift and migration
|
|
75
|
+
|
|
76
|
+
Because the scope key changes when a repo's git origin appears or changes,
|
|
77
|
+
memories captured *before* a remote was added end up orphaned under the
|
|
78
|
+
cwd-based key. The plugin logs a one-shot warning at load when it detects
|
|
79
|
+
this, and ships two CLI commands to resolve it:
|
|
80
|
+
|
|
81
|
+
- `cli scopes` — list every project scope directory with a file count.
|
|
82
|
+
- `cli migrate --from <old> [--to <current>] [--dry-run] [--on-conflict newer|overwrite|skip]`
|
|
83
|
+
— rewrites `scope_key` in each file's frontmatter, moves the files, and
|
|
84
|
+
reindexes. Default conflict strategy is "keep the newer `updated_at`".
|
|
85
|
+
|
|
86
|
+
Auto-migration on load is intentionally *not* done: two unrelated repos at
|
|
87
|
+
the same cwd would silently merge.
|
|
88
|
+
|
|
89
|
+
## Capture mechanisms
|
|
90
|
+
|
|
91
|
+
MVP:
|
|
92
|
+
1. **Explicit tool call** — agent calls `memory_add`.
|
|
93
|
+
2. **Keyword trigger** — regex against the user message parts. Captured group 1 becomes the memory body. Defaults: `remember`, `note that`, `TIL`, `save this`.
|
|
94
|
+
|
|
95
|
+
Not in MVP:
|
|
96
|
+
3. Auto-capture every N turns via LLM summary.
|
|
97
|
+
4. LLM-decided "should I recall?" per turn (supermemory's reasoned-recall).
|
|
98
|
+
|
|
99
|
+
## Retrieval
|
|
100
|
+
|
|
101
|
+
MVP: **BM25 via SQLite FTS5.** Query is tokenized, punctuation stripped, each token becomes a prefix match. `bm25(memories_fts)` used as the score. Snippets via `snippet(memories_fts, ...)`.
|
|
102
|
+
|
|
103
|
+
Not in MVP:
|
|
104
|
+
- Local embeddings (Transformers.js `Xenova/bge-small-en-v1.5`) + sqlite-vec.
|
|
105
|
+
- Reciprocal Rank Fusion across BM25 + vector lanes.
|
|
106
|
+
- Cross-encoder rerank (FastEmbed jina-reranker-tiny).
|
|
107
|
+
|
|
108
|
+
## Context injection
|
|
109
|
+
|
|
110
|
+
On the first `experimental.chat.system.transform` for each session, push a
|
|
111
|
+
`[OPEN-MEMEX]` block containing:
|
|
112
|
+
- User profile / preferences (top N from `user` scope)
|
|
113
|
+
- Project knowledge (top N from current project scope, newest first)
|
|
114
|
+
|
|
115
|
+
Injected once per session per plugin load (in-memory set keyed by sessionID).
|
|
116
|
+
|
|
117
|
+
## Redaction
|
|
118
|
+
|
|
119
|
+
Applied to any content on the way in (tool + keyword capture):
|
|
120
|
+
1. Strip `<private>...</private>` -> `[REDACTED]`.
|
|
121
|
+
2. Match remaining text against secret regex list (OpenAI, GitHub, AWS, Slack...). If any hit -> refuse write, tell agent to redact.
|
|
122
|
+
|
|
123
|
+
## Config surface
|
|
124
|
+
|
|
125
|
+
`~/.config/opencode/open-memex.jsonc`:
|
|
126
|
+
|
|
127
|
+
```jsonc
|
|
128
|
+
{
|
|
129
|
+
"maxProjectMemories": 8,
|
|
130
|
+
"maxProfileItems": 5,
|
|
131
|
+
"injectOnFirstTurn": true,
|
|
132
|
+
"keywordCaptureEnabled": true,
|
|
133
|
+
"keywordPatterns": ["^\\s*remember...", ...],
|
|
134
|
+
"redactPatterns": ["sk-[A-Za-z0-9]{20,}", ...],
|
|
135
|
+
"logLevel": "info"
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Env overrides:
|
|
140
|
+
- `MY_O_MEMORY_HOME` — override storage root
|
|
141
|
+
- `MY_O_MEMORY_CONFIG` — override config file path
|
|
142
|
+
|
|
143
|
+
## v2 roadmap (in rough priority)
|
|
144
|
+
|
|
145
|
+
1. **Local embeddings** — Xenova/bge-small ONNX via Transformers.js; sqlite-vec table; RRF fuse with BM25.
|
|
146
|
+
2. **Auto-capture** every N turns using the host LLM (opt-in).
|
|
147
|
+
3. **Preemptive compaction hook** — inject memories into `experimental.session.compacting` context.
|
|
148
|
+
4. **Deduplication** on write (hash + fuzzy).
|
|
149
|
+
5. **`/memory-init`** slash command — walk the repo, summarize modules, save.
|
|
150
|
+
6. **Recall receipts** — return which lane matched (BM25 / vector / recency) for debuggability.
|
|
151
|
+
7. **Bitemporal** valid_from/valid_to for facts that supersede each other.
|
|
152
|
+
8. **Admission barrier** — reject retrieved memories from being re-ingested, reject assistant self-writes, block prompt-injection in file content.
|
|
153
|
+
|
|
154
|
+
## v3+
|
|
155
|
+
|
|
156
|
+
- Ollama detection -> use `nomic-embed-text` when present.
|
|
157
|
+
- Cross-encoder rerank.
|
|
158
|
+
- Memory decay / consolidation.
|
|
159
|
+
- Import/export to markdown archives.
|
|
160
|
+
- Optional knowledge-graph layer for entity linking.
|
|
161
|
+
|
|
162
|
+
## Prior art referenced
|
|
163
|
+
|
|
164
|
+
- `opencode-supermemory` — plugin lifecycle, capture heuristics
|
|
165
|
+
- `doobidoo/mcp-memory-service` — local sqlite-vec + BM25 hybrid, decay
|
|
166
|
+
- `memoripy` — RRF, admission barrier, dependency-free hash embeddings
|
|
167
|
+
- `basic-memory` — markdown-first store, obsidian-compatible
|
|
168
|
+
- Anthropic client-managed memory tool pattern — filesystem primitives
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# open-memex
|
|
2
|
+
|
|
3
|
+
Local-first persistent memory plugin for [opencode](https://opencode.ai).
|
|
4
|
+
|
|
5
|
+
- **Markdown files** as the source of truth (human-editable, git-friendly)
|
|
6
|
+
- **SQLite FTS5** as a rebuildable index (BM25 keyword search, via `better-sqlite3`)
|
|
7
|
+
- **Zero cloud**, zero account, zero third-party API
|
|
8
|
+
- Loads directly under opencode's embedded Bun runtime; CLI runs under Node — no build step, no Bun install
|
|
9
|
+
|
|
10
|
+
## Install (dev)
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
cd c:\github\open-memex
|
|
14
|
+
npm install
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Then add to `~/.config/opencode/opencode.jsonc`:
|
|
18
|
+
|
|
19
|
+
```jsonc
|
|
20
|
+
{
|
|
21
|
+
"$schema": "https://opencode.ai/config.json",
|
|
22
|
+
"plugin": ["file:///c:/github/open-memex/src/index.ts"]
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Restart opencode.
|
|
27
|
+
|
|
28
|
+
## Tools the plugin exposes to the agent
|
|
29
|
+
|
|
30
|
+
| Tool | What it does |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `memory_add` | Save a fact, preference, decision, note |
|
|
33
|
+
| `memory_search` | Keyword search (BM25) across project + user memories |
|
|
34
|
+
| `memory_list` | List memories in a scope, newest first |
|
|
35
|
+
| `memory_forget` | Delete a memory by id |
|
|
36
|
+
|
|
37
|
+
## Capture
|
|
38
|
+
|
|
39
|
+
- **Keyword triggers** in user messages: `remember ...`, `note that ...`, `TIL ...`, `save this: ...`
|
|
40
|
+
- **Explicit tool calls** by the agent (via `memory_add`)
|
|
41
|
+
- **Redaction**: content inside `<private>...</private>` tags is stripped; content matching secret patterns (API keys, tokens) is refused
|
|
42
|
+
|
|
43
|
+
## Scopes
|
|
44
|
+
|
|
45
|
+
- **project** — scoped to the current repo (keyed off the git origin URL hash, or the cwd if no remote). Default for new memories.
|
|
46
|
+
- **user** — global across all your projects; use for personal preferences.
|
|
47
|
+
|
|
48
|
+
## Retrieval
|
|
49
|
+
|
|
50
|
+
On the first turn of every session, `open-memex` injects a `[OPEN-MEMEX]` block into the system prompt containing top-N recent project memories + top-N user preferences. The agent can also call `memory_search` on demand.
|
|
51
|
+
|
|
52
|
+
## Storage layout
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
%APPDATA%\open-memex\ (Windows)
|
|
56
|
+
$XDG_DATA_HOME/open-memex/ (Linux/macOS)
|
|
57
|
+
├── index.db # SQLite FTS5 index (rebuildable)
|
|
58
|
+
└── memories/
|
|
59
|
+
├── user/
|
|
60
|
+
│ └── <id>.md
|
|
61
|
+
└── project__<name>__<hash12>/
|
|
62
|
+
└── <id>.md
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Each `.md` file has YAML frontmatter (`id, scope_key, type, tags, created_at, ...`) followed by the memory content. You can edit them by hand — the plugin re-syncs on startup by comparing file mtimes.
|
|
66
|
+
|
|
67
|
+
## Config
|
|
68
|
+
|
|
69
|
+
Optional file at `~/.config/opencode/open-memex.jsonc`. See `PLAN.md` for defaults.
|
|
70
|
+
|
|
71
|
+
## CLI
|
|
72
|
+
|
|
73
|
+
Runs under Node 22 with the built-in experimental TypeScript loader (no build step).
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
node --experimental-strip-types src/cli.ts where
|
|
77
|
+
node --experimental-strip-types src/cli.ts list --scope project
|
|
78
|
+
node --experimental-strip-types src/cli.ts search "auth flow"
|
|
79
|
+
node --experimental-strip-types src/cli.ts add "This repo uses better-sqlite3" --type project-config
|
|
80
|
+
node --experimental-strip-types src/cli.ts forget <id>
|
|
81
|
+
node --experimental-strip-types src/cli.ts reindex
|
|
82
|
+
node --experimental-strip-types src/cli.ts scopes
|
|
83
|
+
node --experimental-strip-types src/cli.ts migrate --from <old-scope-key> [--dry-run]
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Or via the npm script: `npm run cli -- list --scope project`.
|
|
87
|
+
**Note:** flag arguments beyond the first must be passed via direct `node` invocation, not `npm run cli --`, because npm swallows unknown `--flag` args.
|
|
88
|
+
|
|
89
|
+
## Status
|
|
90
|
+
|
|
91
|
+
MVP. See `PLAN.md` for the v2 roadmap (local embeddings, auto-capture, compaction hook, etc).
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
[MIT](./LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "open-memex",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local-first memory layer and protocol for AI coding agents. Markdown source of truth, SQLite FTS5 index, zero cloud.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/stoneskin/open-memex.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/stoneskin/open-memex#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/stoneskin/open-memex/issues"
|
|
14
|
+
},
|
|
15
|
+
"main": "src/index.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"typecheck": "tsc --noEmit",
|
|
18
|
+
"cli": "node --experimental-strip-types src/cli.ts"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"better-sqlite3": "^11.7.0",
|
|
22
|
+
"js-yaml": "^4.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@opencode-ai/plugin": "^1.18.30",
|
|
26
|
+
"@types/better-sqlite3": "^7.6.11",
|
|
27
|
+
"@types/js-yaml": "^4.0.9",
|
|
28
|
+
"@types/node": "^22.0.0",
|
|
29
|
+
"typescript": "^5.6.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Quick smoke test — runs the pure-logic modules (no bun:sqlite dependency).
|
|
2
|
+
// Usage: node --experimental-strip-types scripts\smoke-pure.ts
|
|
3
|
+
import { parse, serialize, ulid, type Frontmatter } from "../src/store/markdown.ts";
|
|
4
|
+
import { redact, findSecret } from "../src/redact.ts";
|
|
5
|
+
import { detectKeywords } from "../src/capture/keywords.ts";
|
|
6
|
+
import { DEFAULT_CONFIG } from "../src/config.ts";
|
|
7
|
+
import { resolveProjectScope, resolveCwdScope, USER_SCOPE } from "../src/scope.ts";
|
|
8
|
+
|
|
9
|
+
let fails = 0;
|
|
10
|
+
function ok(name: string, cond: boolean, info?: unknown) {
|
|
11
|
+
if (cond) console.log(` ok ${name}`);
|
|
12
|
+
else {
|
|
13
|
+
console.log(` FAIL ${name}`, info ?? "");
|
|
14
|
+
fails++;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
console.log("== ulid ==");
|
|
19
|
+
const a = ulid();
|
|
20
|
+
const b = ulid();
|
|
21
|
+
ok("length is 26", a.length === 26);
|
|
22
|
+
ok("two calls differ", a !== b);
|
|
23
|
+
ok("sortable", a < b || a === b);
|
|
24
|
+
|
|
25
|
+
console.log("== markdown round-trip ==");
|
|
26
|
+
const fm: Frontmatter = {
|
|
27
|
+
id: a,
|
|
28
|
+
scope_key: "project__test__abcdef123456",
|
|
29
|
+
scope_kind: "project",
|
|
30
|
+
project_name: "test",
|
|
31
|
+
type: "note",
|
|
32
|
+
tags: ["hello", "world"],
|
|
33
|
+
source: "test",
|
|
34
|
+
created_at: 1000,
|
|
35
|
+
updated_at: 2000,
|
|
36
|
+
};
|
|
37
|
+
const body = "hello\n\nworld";
|
|
38
|
+
const raw = serialize(fm, body);
|
|
39
|
+
ok("has frontmatter fence", raw.startsWith("---\n"));
|
|
40
|
+
const parsed = parse(raw);
|
|
41
|
+
ok("parses", parsed !== null);
|
|
42
|
+
ok("id survives", parsed?.fm.id === a);
|
|
43
|
+
ok("tags survive", JSON.stringify(parsed?.fm.tags) === '["hello","world"]');
|
|
44
|
+
ok("body survives", parsed?.body.trim() === body);
|
|
45
|
+
|
|
46
|
+
console.log("== redact ==");
|
|
47
|
+
const r1 = redact("api key is <private>sk-supersecretkey</private> ok", DEFAULT_CONFIG.redactPatterns);
|
|
48
|
+
ok("private stripped", !r1.content.includes("sk-supersecretkey"));
|
|
49
|
+
ok("no secret after strip", !r1.hadSecret);
|
|
50
|
+
|
|
51
|
+
const r2 = redact("sk-1234567890abcdefghij1234", DEFAULT_CONFIG.redactPatterns);
|
|
52
|
+
ok("bare secret detected", r2.hadSecret === true);
|
|
53
|
+
|
|
54
|
+
const r3 = redact("ghp_" + "a".repeat(36), DEFAULT_CONFIG.redactPatterns);
|
|
55
|
+
ok("github PAT detected", r3.hadSecret === true);
|
|
56
|
+
|
|
57
|
+
console.log("== keywords ==");
|
|
58
|
+
const hits1 = detectKeywords("remember that this repo uses Bun, not Node", DEFAULT_CONFIG);
|
|
59
|
+
ok("basic remember matched", hits1.length === 1);
|
|
60
|
+
ok("body captured", hits1[0]?.content === "this repo uses Bun, not Node");
|
|
61
|
+
|
|
62
|
+
const hits2 = detectKeywords("TIL: sqlite bm25 returns lower=better", DEFAULT_CONFIG);
|
|
63
|
+
ok("TIL matched", hits2.length === 1);
|
|
64
|
+
|
|
65
|
+
const hits3 = detectKeywords("hello world", DEFAULT_CONFIG);
|
|
66
|
+
ok("no false positive", hits3.length === 0);
|
|
67
|
+
|
|
68
|
+
const hits4 = detectKeywords("Please note that FTS5 needs prefix matches", DEFAULT_CONFIG);
|
|
69
|
+
ok("please-note matched", hits4.length === 1);
|
|
70
|
+
|
|
71
|
+
console.log("== scope ==");
|
|
72
|
+
const s = resolveProjectScope(process.cwd());
|
|
73
|
+
ok("kind=project", s.kind === "project");
|
|
74
|
+
ok("key starts with project__", s.key.startsWith("project__"));
|
|
75
|
+
ok("hash length 12", /__[a-f0-9]{12}$/.test(s.key));
|
|
76
|
+
console.log(" scope.key =", s.key);
|
|
77
|
+
console.log(" user.key =", USER_SCOPE.key);
|
|
78
|
+
|
|
79
|
+
console.log("== scope (cwd-only legacy) ==");
|
|
80
|
+
const cwdOnly = resolveCwdScope(process.cwd());
|
|
81
|
+
ok("cwd scope kind=project", cwdOnly.kind === "project");
|
|
82
|
+
ok("cwd scope key format", /^project__[^_]+.*__[a-f0-9]{12}$/.test(cwdOnly.key));
|
|
83
|
+
// Deterministic: same input -> same key
|
|
84
|
+
const cwdOnly2 = resolveCwdScope(process.cwd());
|
|
85
|
+
ok("cwd scope deterministic", cwdOnly.key === cwdOnly2.key);
|
|
86
|
+
// In a repo with a git remote, cwd-only scope must differ from git-origin scope.
|
|
87
|
+
// (This repo does have an origin after `git push`.)
|
|
88
|
+
console.log(" cwd.key =", cwdOnly.key);
|
|
89
|
+
|
|
90
|
+
console.log(fails === 0 ? "\nALL PASS" : `\n${fails} FAILURES`);
|
|
91
|
+
process.exit(fails === 0 ? 0 : 1);
|