dsh-memvault 0.0.0-stage → 0.9.11
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/LICENSE +21 -0
- package/README.md +453 -2
- package/README.zh.md +383 -0
- package/cordis.patch.yml +59 -0
- package/docs/DEVLOG.md +820 -0
- package/docs/architecture.svg +101 -0
- package/docs/mcp/memvault.mcp.json +13 -0
- package/docs/overview.png +0 -0
- package/docs/screenshots/panel-browse-filters.jpg +0 -0
- package/docs/screenshots/panel-core-blocks.jpg +0 -0
- package/docs/screenshots/panel-detail.png +0 -0
- package/docs/screenshots/panel-inject.jpg +0 -0
- package/docs/screenshots/panel-review.png +0 -0
- package/docs/screenshots/panel-structure-dimensions.jpg +0 -0
- package/docs/screenshots/panel-structure-graph.jpg +0 -0
- package/docs/social-preview-1280x640.jpg +0 -0
- package/lib/blocks-write.js +121 -0
- package/lib/blocks.js +123 -0
- package/lib/cli.js +90 -0
- package/lib/client.js +1191 -0
- package/lib/config.js +442 -0
- package/lib/extract.js +318 -0
- package/lib/flags.js +67 -0
- package/lib/index.js +862 -0
- package/lib/memories.js +279 -0
- package/lib/panel.js +568 -0
- package/lib/reindex.js +32 -0
- package/lib/review.js +222 -0
- package/lib/schema.js +90 -0
- package/lib/structure.js +195 -0
- package/lib/window.js +204 -0
- package/package.json +73 -4
- package/scripts/build-client.mjs +52 -0
- package/scripts/smoke-config.mjs +247 -0
- package/scripts/smoke-extract.mjs +300 -0
- package/scripts/smoke-package.mjs +226 -0
- package/scripts/smoke-panel-routes.mjs +56 -0
- package/scripts/smoke-panel.mjs +1273 -0
- package/scripts/smoke.mjs +69 -0
- package/src/client/index.js +1185 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 620" width="1000" height="620" role="img" aria-labelledby="archTitle archDesc">
|
|
2
|
+
<title id="archTitle">MemVault 与 DSH 插件:一个引擎,三个适配器</title>
|
|
3
|
+
<desc id="archDesc">MemVault 由引擎与一个 SQLite 库组成;CLI、MCP stdio、HTTP 控制台是它的三个适配器。DSH 插件通过 CLI 子进程写库、用 node:sqlite 直读库,并借 MCP 连接器把工具交给模型。</desc>
|
|
4
|
+
|
|
5
|
+
<defs>
|
|
6
|
+
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
|
7
|
+
<path d="M 0 0 L 10 5 L 0 10 z" fill="#57606a"/>
|
|
8
|
+
</marker>
|
|
9
|
+
<marker id="arrowDash" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
|
|
10
|
+
<path d="M 0 0 L 10 5 L 0 10 z" fill="#8250df"/>
|
|
11
|
+
</marker>
|
|
12
|
+
</defs>
|
|
13
|
+
|
|
14
|
+
<style>
|
|
15
|
+
.t { font-family: -apple-system, "Segoe UI", "Noto Sans SC", "Microsoft YaHei", sans-serif; fill: #1f2328; }
|
|
16
|
+
.m { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; fill: #1f2328; }
|
|
17
|
+
.mu { font-family: -apple-system, "Segoe UI", "Noto Sans SC", sans-serif; fill: #656d76; }
|
|
18
|
+
.h { font-weight: 600; }
|
|
19
|
+
.box { fill: #ffffff; stroke: #d0d7de; stroke-width: 1.5; rx: 10; }
|
|
20
|
+
.engine { fill: #f6f8fa; stroke: #8c959f; stroke-width: 1.5; rx: 12; }
|
|
21
|
+
.store { fill: #fff8c5; stroke: #d4a72c; stroke-width: 1.5; rx: 12; }
|
|
22
|
+
.plugin { fill: #ddf4ff; stroke: #0969da; stroke-width: 1.5; rx: 12; }
|
|
23
|
+
.flow { stroke: #57606a; stroke-width: 1.6; fill: none; marker-end: url(#arrow); }
|
|
24
|
+
.dash { stroke: #8250df; stroke-width: 1.6; fill: none; stroke-dasharray: 6 4; marker-end: url(#arrowDash); }
|
|
25
|
+
.note { fill: #f6f8fa; stroke: #d0d7de; stroke-width: 1; rx: 8; }
|
|
26
|
+
</style>
|
|
27
|
+
|
|
28
|
+
<rect x="0" y="0" width="1000" height="620" fill="#ffffff"/>
|
|
29
|
+
<text class="t h" x="32" y="42" font-size="21">MemVault 与 DSH 插件:一个引擎,三个适配器</text>
|
|
30
|
+
<text class="mu" x="32" y="66" font-size="13">One engine, one store, three adapters — and one host integration</text>
|
|
31
|
+
|
|
32
|
+
<!-- adapters (left) -->
|
|
33
|
+
<text class="mu" x="32" y="108" font-size="12">适配器 / adapters(薄层,共用同一引擎)</text>
|
|
34
|
+
<rect class="box" x="32" y="120" width="252" height="72"/>
|
|
35
|
+
<text class="m h" x="48" y="148" font-size="14">cli.py</text>
|
|
36
|
+
<text class="mu" x="48" y="170" font-size="12">脚本化、一次性操作</text>
|
|
37
|
+
|
|
38
|
+
<rect class="box" x="32" y="212" width="252" height="72"/>
|
|
39
|
+
<text class="m h" x="48" y="240" font-size="14">mcp_server.py</text>
|
|
40
|
+
<text class="mu" x="48" y="262" font-size="12">stdio · 16 个工具</text>
|
|
41
|
+
|
|
42
|
+
<rect class="box" x="32" y="304" width="252" height="72"/>
|
|
43
|
+
<text class="m h" x="48" y="332" font-size="14">api.py + dashboard</text>
|
|
44
|
+
<text class="mu" x="48" y="354" font-size="12">HTTP 接口与控制台</text>
|
|
45
|
+
|
|
46
|
+
<!-- engine (middle) -->
|
|
47
|
+
<rect class="engine" x="352" y="120" width="292" height="256"/>
|
|
48
|
+
<text class="t h" x="370" y="150" font-size="16">MemVault 引擎</text>
|
|
49
|
+
<text class="m" x="370" y="180" font-size="12.5">memory.py · storage.py</text>
|
|
50
|
+
<text class="m" x="370" y="202" font-size="12.5">vector_index.py · extractors.py</text>
|
|
51
|
+
<text class="m" x="370" y="224" font-size="12.5">scopes.py · blocks</text>
|
|
52
|
+
<text class="mu" x="370" y="256" font-size="12">写入即判定 ADD / UPDATE / DELETE</text>
|
|
53
|
+
<text class="mu" x="370" y="276" font-size="12">矛盾记为 relations(带权重)</text>
|
|
54
|
+
<text class="mu" x="370" y="296" font-size="12">每次判定进 history,可审计</text>
|
|
55
|
+
<text class="mu" x="370" y="330" font-size="12">作用域是维度:user / agent / run</text>
|
|
56
|
+
<text class="mu" x="370" y="350" font-size="12">三者正交,可同时成立</text>
|
|
57
|
+
|
|
58
|
+
<!-- store -->
|
|
59
|
+
<rect class="store" x="352" y="396" width="292" height="72"/>
|
|
60
|
+
<text class="m h" x="370" y="424" font-size="14">data/memvault.db</text>
|
|
61
|
+
<text class="mu" x="370" y="446" font-size="12">SQLite · 唯一真相(含 embedding)</text>
|
|
62
|
+
|
|
63
|
+
<!-- engine → store -->
|
|
64
|
+
<path class="flow" d="M 498 376 L 498 394"/>
|
|
65
|
+
|
|
66
|
+
<!-- adapters → engine -->
|
|
67
|
+
<path class="flow" d="M 284 156 L 350 172"/>
|
|
68
|
+
<path class="flow" d="M 284 248 L 350 248"/>
|
|
69
|
+
<path class="flow" d="M 284 340 L 350 324"/>
|
|
70
|
+
|
|
71
|
+
<!-- consumers (right) -->
|
|
72
|
+
<text class="mu" x="700" y="108" font-size="12">消费者 / consumers</text>
|
|
73
|
+
|
|
74
|
+
<rect class="plugin" x="700" y="120" width="272" height="112"/>
|
|
75
|
+
<text class="t h" x="716" y="148" font-size="15">DSH 插件 dsh-memvault</text>
|
|
76
|
+
<text class="mu" x="716" y="170" font-size="12">注入核心块 · 按轮抽取 · 面板</text>
|
|
77
|
+
<text class="mu" x="716" y="190" font-size="12">被商店收录的那个条目</text>
|
|
78
|
+
<text class="mu" x="716" y="210" font-size="12">(声明了 dsh.bundle)</text>
|
|
79
|
+
|
|
80
|
+
<rect class="box" x="700" y="250" width="272" height="86"/>
|
|
81
|
+
<text class="t h" x="716" y="278" font-size="15">任何 MCP 宿主</text>
|
|
82
|
+
<text class="mu" x="716" y="300" font-size="12">Claude Code / Codex / DSH …</text>
|
|
83
|
+
<text class="mu" x="716" y="320" font-size="12">按需调用工具,不参与注入</text>
|
|
84
|
+
|
|
85
|
+
<rect class="box" x="700" y="362" width="272" height="86"/>
|
|
86
|
+
<text class="t h" x="716" y="390" font-size="15">浏览器 / 控制台</text>
|
|
87
|
+
<text class="mu" x="716" y="412" font-size="12">记忆列表 · 关系图 · 统计</text>
|
|
88
|
+
<text class="mu" x="716" y="432" font-size="12">人工查看与编辑</text>
|
|
89
|
+
|
|
90
|
+
<!-- the two flows that are actually distinctive: plugin writes by spawning the CLI,
|
|
91
|
+
and plugin reads the store directly with node:sqlite -->
|
|
92
|
+
<path class="dash" d="M 700 136 L 310 136 L 310 118"/>
|
|
93
|
+
<text class="mu" x="694" y="128" font-size="11.5" text-anchor="end">spawn 子进程(写)</text>
|
|
94
|
+
<path class="dash" d="M 700 224 L 700 486 L 660 486 L 660 432 L 648 432"/>
|
|
95
|
+
<text class="mu" x="694" y="502" font-size="11.5" text-anchor="end">node:sqlite 直读(读)</text>
|
|
96
|
+
|
|
97
|
+
<!-- note -->
|
|
98
|
+
<rect class="note" x="32" y="520" width="940" height="72"/>
|
|
99
|
+
<text class="t" x="48" y="548" font-size="13">为什么是两个仓库:插件声明 <tspan class="m">dsh.bundle</tspan>,所以它可以进插件商店;MemVault 是通用服务,刻意不带宿主概念,</text>
|
|
100
|
+
<text class="t" x="48" y="570" font-size="13">因此任何 MCP 客户端都能用它。两者共用同一个库文件——插件把 <tspan class="m">MEMVAULT_DB_PATH</tspan> 显式传给子进程,而不是靠默认值恰好相等。</text>
|
|
101
|
+
</svg>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mcpServers": {
|
|
3
|
+
"memvault": {
|
|
4
|
+
"type": "stdio",
|
|
5
|
+
"command": "D:/Claude_code/memory/.venv/Scripts/python.exe",
|
|
6
|
+
"args": ["-m", "memvault.mcp_server"],
|
|
7
|
+
"cwd": "D:/Claude_code/memory",
|
|
8
|
+
"env": {
|
|
9
|
+
"MEMVAULT_DB_PATH": "D:/Claude_code/memory/data/memvault.db"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write core memory blocks — the panel's only write action.
|
|
3
|
+
*
|
|
4
|
+
* Transport: the MemVault CLI (`blocks-set` / `blocks-delete`), same as turn
|
|
5
|
+
* extraction and for the same reasons (see `cli.js`). The CLI is what owns the
|
|
6
|
+
* semantics: `core_append` is an **upsert** keyed by `(scope_type, scope_id,
|
|
7
|
+
* label)`, it emits `block.updated`, and it applies `value_limit`. A direct
|
|
8
|
+
* SQLite write would skip the event and the limit.
|
|
9
|
+
*
|
|
10
|
+
* Blocks are cheap to write — no LLM call and no embedding, unlike memories — so
|
|
11
|
+
* a panel button can do it synchronously.
|
|
12
|
+
*
|
|
13
|
+
* Validation is deliberately stricter than MemVault's in two places, because the
|
|
14
|
+
* panel is a UI and a mistake there is a user-visible one:
|
|
15
|
+
*
|
|
16
|
+
* - an empty value is refused: the reader skips empty values, so such a block
|
|
17
|
+
* would exist and never be injected — a silent no-op;
|
|
18
|
+
* - the value is capped at {@link BLOCK_VALUE_MAX}: the CLI takes the value as
|
|
19
|
+
* an **argv positional**, and argv has a length limit (32 KiB on Windows).
|
|
20
|
+
*/
|
|
21
|
+
import { runCli } from './cli.js'
|
|
22
|
+
|
|
23
|
+
/** Hard cap on a block value shipped as one argv element. */
|
|
24
|
+
export const BLOCK_VALUE_MAX = 8000
|
|
25
|
+
|
|
26
|
+
/** Hard cap on a block label. */
|
|
27
|
+
export const BLOCK_LABEL_MAX = 120
|
|
28
|
+
|
|
29
|
+
/** Accepted actions. */
|
|
30
|
+
export const BLOCK_ACTIONS = ['set', 'delete']
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Validate a panel write request.
|
|
34
|
+
*
|
|
35
|
+
* @returns `{ ok: true, action, type, id, label, value, limit }` or
|
|
36
|
+
* `{ ok: false, error }`. Never throws.
|
|
37
|
+
*/
|
|
38
|
+
export function validateBlockAction(input = {}) {
|
|
39
|
+
const action = String(input?.action ?? '').trim()
|
|
40
|
+
if (!BLOCK_ACTIONS.includes(action)) return { ok: false, error: `action must be one of ${BLOCK_ACTIONS.join(' | ')}` }
|
|
41
|
+
|
|
42
|
+
const type = String(input?.type ?? '').trim()
|
|
43
|
+
if (type !== 'user' && type !== 'agent') return { ok: false, error: 'type must be user or agent' }
|
|
44
|
+
|
|
45
|
+
const id = String(input?.id ?? '').trim()
|
|
46
|
+
if (id === '') return { ok: false, error: 'id is required' }
|
|
47
|
+
if (id.length > 200) return { ok: false, error: 'id is longer than 200 characters' }
|
|
48
|
+
|
|
49
|
+
const label = String(input?.label ?? '').trim()
|
|
50
|
+
if (label === '') return { ok: false, error: 'label is required' }
|
|
51
|
+
if (label.length > BLOCK_LABEL_MAX) return { ok: false, error: `label is longer than ${BLOCK_LABEL_MAX} characters` }
|
|
52
|
+
if (/[\r\n]/.test(label)) return { ok: false, error: 'label must be a single line' }
|
|
53
|
+
|
|
54
|
+
const value = input?.value === undefined || input?.value === null ? '' : String(input.value)
|
|
55
|
+
if (value.length > BLOCK_VALUE_MAX) {
|
|
56
|
+
return { ok: false, error: `value is ${value.length} characters; the panel limit is ${BLOCK_VALUE_MAX}` }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let limit
|
|
60
|
+
if (input?.limit !== undefined && input?.limit !== null && input?.limit !== '') {
|
|
61
|
+
limit = Number(input.limit)
|
|
62
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100_000) {
|
|
63
|
+
return { ok: false, error: 'limit must be a positive integer' }
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (action === 'delete') {
|
|
68
|
+
if (value !== '') return { ok: false, error: 'delete takes no value' }
|
|
69
|
+
return { ok: true, action, type, id, label, value: '', limit: undefined }
|
|
70
|
+
}
|
|
71
|
+
if (value.trim() === '') {
|
|
72
|
+
return { ok: false, error: 'value is required: the prompt reader skips empty blocks, so an empty write would be invisible' }
|
|
73
|
+
}
|
|
74
|
+
return { ok: true, action, type, id, label, value, limit }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* argv for `blocks-set`.
|
|
79
|
+
*
|
|
80
|
+
* `--` ends option parsing before the positionals, so a value that starts with a
|
|
81
|
+
* dash (`-50%`, `--flag`) is still data rather than an unknown option.
|
|
82
|
+
*/
|
|
83
|
+
export function buildBlockSetArgs({ type, id, label, value, limit }) {
|
|
84
|
+
const args = ['blocks-set', '--type', type, '--id', id]
|
|
85
|
+
if (limit !== undefined) args.push('--limit', String(limit))
|
|
86
|
+
args.push('--', label)
|
|
87
|
+
if (value !== undefined && value !== '') args.push(value)
|
|
88
|
+
return args
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** argv for `blocks-delete`. */
|
|
92
|
+
export function buildBlockDeleteArgs({ type, id, label }) {
|
|
93
|
+
return ['blocks-delete', '--type', type, '--id', id, '--', label]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validate and run one block action against MemVault.
|
|
98
|
+
*
|
|
99
|
+
* @param options.config - CLI config (`pythonPath`, `projectDir`, `env`, `timeoutMs`).
|
|
100
|
+
* @param options.input - the raw request body.
|
|
101
|
+
* @returns `{ ok, status, error? , action?, result? }`; `status` is the HTTP code
|
|
102
|
+
* the route should answer with (400 for a bad request, 502 for a CLI failure).
|
|
103
|
+
*/
|
|
104
|
+
export async function runBlockAction({ config, input, spawn }) {
|
|
105
|
+
const valid = validateBlockAction(input)
|
|
106
|
+
if (!valid.ok) return { ok: false, status: 400, error: valid.error }
|
|
107
|
+
|
|
108
|
+
const args = valid.action === 'set' ? buildBlockSetArgs(valid) : buildBlockDeleteArgs(valid)
|
|
109
|
+
const result = await runCli({ config, args, ...(spawn ? { spawn } : {}) })
|
|
110
|
+
if (!result.ok) return { ok: false, status: 502, error: result.error }
|
|
111
|
+
|
|
112
|
+
let parsed = null
|
|
113
|
+
try {
|
|
114
|
+
parsed = JSON.parse(result.stdout)
|
|
115
|
+
} catch {
|
|
116
|
+
// The CLI prints JSON on success; if a future version does not, the write
|
|
117
|
+
// still happened and the panel refreshes from the store anyway.
|
|
118
|
+
parsed = null
|
|
119
|
+
}
|
|
120
|
+
return { ok: true, status: 200, action: valid.action, result: parsed }
|
|
121
|
+
}
|
package/lib/blocks.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read MemVault's core memory blocks, with no dependency on the MemVault
|
|
3
|
+
* service being up.
|
|
4
|
+
*
|
|
5
|
+
* Transport choice: a **direct read-only SQLite read** via Node's built-in
|
|
6
|
+
* `node:sqlite` (Node >= 22.5). The alternatives were worse for a prompt
|
|
7
|
+
* injector that must answer synchronously on every assemble:
|
|
8
|
+
*
|
|
9
|
+
* - HTTP REST (`/api/v1/blocks`) needs the FastAPI server running on 8780;
|
|
10
|
+
* if it is down the prompt silently loses memory.
|
|
11
|
+
* - spawning the Python CLI costs ~200-300 ms per refresh and needs a
|
|
12
|
+
* process spawn per turn.
|
|
13
|
+
* - `node:sqlite` costs ~1 ms, needs no server, no subprocess and no
|
|
14
|
+
* third-party dependency.
|
|
15
|
+
*
|
|
16
|
+
* The store is shared with the MemVault server and MCP clients, but this only
|
|
17
|
+
* ever SELECTs, and SQLite handles concurrent readers fine.
|
|
18
|
+
*/
|
|
19
|
+
import { DatabaseSync } from 'node:sqlite'
|
|
20
|
+
|
|
21
|
+
/** Default store; matches MEMVAULT_DB_PATH in the MemVault project .env. */
|
|
22
|
+
export const DEFAULT_DB_PATH = process.env.MEMVAULT_DB_PATH ?? 'D:/Claude_code/memory/data/memvault.db'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Which blocks to inject. MemVault keys blocks by `(scope_type, scope_id)`
|
|
26
|
+
* (`scope_type` is `user` or `agent`), so both halves must be given explicitly —
|
|
27
|
+
* there is no "current scope" in a foreign process.
|
|
28
|
+
*/
|
|
29
|
+
export const DEFAULT_SCOPES = [
|
|
30
|
+
{ type: 'user', id: 'lenovo' },
|
|
31
|
+
{ type: 'agent', id: 'claude-code-memory' },
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Read the configured blocks in `position` order.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} options
|
|
38
|
+
* @param {string} [options.dbPath] - path to memvault.db.
|
|
39
|
+
* @param {Array<{type: string, id: string}>} [options.scopes] - scope pairs.
|
|
40
|
+
* @param {string[]} [options.labels] - only these labels (empty/absent = all).
|
|
41
|
+
* @returns {Array<{scopeType: string, scopeId: string, label: string, value: string}>}
|
|
42
|
+
*/
|
|
43
|
+
export function readCoreBlocks({ dbPath = DEFAULT_DB_PATH, scopes = DEFAULT_SCOPES, labels = [] } = {}) {
|
|
44
|
+
const wanted = scopes.filter((s) => s && s.type && s.id)
|
|
45
|
+
if (wanted.length === 0) return []
|
|
46
|
+
|
|
47
|
+
// `timeout` is a busy timeout (Node docs: `new DatabaseSync(path, { timeout })`,
|
|
48
|
+
// added v24.0.0), not an HTTP-style one. The store is shared with the MemVault
|
|
49
|
+
// server and other clients, so a read landing while someone writes should wait
|
|
50
|
+
// instead of surfacing SQLITE_BUSY to the prompt assembler.
|
|
51
|
+
const db = new DatabaseSync(dbPath, { readOnly: true, timeout: 2000 })
|
|
52
|
+
try {
|
|
53
|
+
const wantedLabels = new Set(labels)
|
|
54
|
+
const out = []
|
|
55
|
+
for (const scope of wanted) {
|
|
56
|
+
const rows = db.prepare(
|
|
57
|
+
'SELECT label, value, value_limit FROM blocks WHERE scope_type = ? AND scope_id = ? ORDER BY position',
|
|
58
|
+
).all(scope.type, scope.id)
|
|
59
|
+
for (const row of rows) {
|
|
60
|
+
const value = typeof row.value === 'string' ? row.value.trim() : ''
|
|
61
|
+
if (value.length === 0) continue
|
|
62
|
+
if (wantedLabels.size > 0 && !wantedLabels.has(row.label)) continue
|
|
63
|
+
out.push({
|
|
64
|
+
scopeType: scope.type,
|
|
65
|
+
scopeId: scope.id,
|
|
66
|
+
label: row.label,
|
|
67
|
+
value,
|
|
68
|
+
// Advisory, and shown by the panel: MemVault stores it per block
|
|
69
|
+
// (default 2000) and does not truncate to it.
|
|
70
|
+
limit: typeof row.value_limit === 'number' ? row.value_limit : null,
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return out
|
|
75
|
+
} finally {
|
|
76
|
+
db.close()
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Render blocks as prompt text, capped at `maxChars`.
|
|
82
|
+
*
|
|
83
|
+
* Returns '' when there is nothing to say: an empty contribution is dropped by
|
|
84
|
+
* the assembler, which is what we want (no header noise on an empty store).
|
|
85
|
+
*
|
|
86
|
+
* Budget contract: `maxChars` bounds the **whole returned string**. Space for the
|
|
87
|
+
* omission note is reserved up front, so appending a note can never push the
|
|
88
|
+
* result over budget. The header is always emitted — without it the lines are
|
|
89
|
+
* unattributable — so a `maxChars` smaller than the header yields the header
|
|
90
|
+
* alone (the documented floor; callers should pass a real budget).
|
|
91
|
+
*
|
|
92
|
+
* @param {Array} blocks - output of {@link readCoreBlocks}.
|
|
93
|
+
* @param {object} [options]
|
|
94
|
+
* @param {number} [options.maxChars] - budget for the whole rendered string.
|
|
95
|
+
* @param {string} [options.header] - first line.
|
|
96
|
+
*/
|
|
97
|
+
export function formatCoreBlocks(blocks, {
|
|
98
|
+
maxChars = 4000,
|
|
99
|
+
header = 'MemVault core memory (persistent facts about the user and this agent; already loaded — no need to call a tool to read them):',
|
|
100
|
+
} = {}) {
|
|
101
|
+
if (!blocks || blocks.length === 0) return ''
|
|
102
|
+
|
|
103
|
+
// Reserved for "(N more block(s) omitted to stay within M chars)".
|
|
104
|
+
const NOTE_RESERVE = 90
|
|
105
|
+
const budget = maxChars - header.length - NOTE_RESERVE
|
|
106
|
+
|
|
107
|
+
const lines = [header]
|
|
108
|
+
let used = 0
|
|
109
|
+
let dropped = 0
|
|
110
|
+
for (const b of blocks) {
|
|
111
|
+
const line = `- [${b.scopeType}/${b.scopeId}/${b.label}] ${b.value}`
|
|
112
|
+
if (used + line.length + 1 > budget) { dropped += 1; continue }
|
|
113
|
+
lines.push(line)
|
|
114
|
+
used += line.length + 1
|
|
115
|
+
}
|
|
116
|
+
if (dropped > 0) {
|
|
117
|
+
const note = `(${dropped} more block(s) omitted to stay within ${maxChars} chars)`
|
|
118
|
+
// Only emit the note if the whole string still fits: the invariant is
|
|
119
|
+
// "output <= maxChars whenever maxChars >= header length".
|
|
120
|
+
if (header.length + used + note.length + 1 <= maxChars) lines.push(note)
|
|
121
|
+
}
|
|
122
|
+
return lines.join('\n')
|
|
123
|
+
}
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run the MemVault CLI — the one transport this plugin uses for *writes*.
|
|
3
|
+
*
|
|
4
|
+
* Both write paths go through it (turn extraction and panel block edits), for
|
|
5
|
+
* the same reasons:
|
|
6
|
+
*
|
|
7
|
+
* - REST (`POST /api/v1/...`) needs the FastAPI server alive on 8780; a write
|
|
8
|
+
* that silently stops because a second process is down is worse than a
|
|
9
|
+
* process spawn.
|
|
10
|
+
* - A direct SQLite write bypasses MemVault's own semantics (validation, the
|
|
11
|
+
* `block.updated` event, extraction + embeddings for memories) and produces
|
|
12
|
+
* rows the store cannot score.
|
|
13
|
+
*
|
|
14
|
+
* The cost is one Python process per call, which is why callers throttle.
|
|
15
|
+
*
|
|
16
|
+
* Two details are load-bearing and were both learned the hard way:
|
|
17
|
+
*
|
|
18
|
+
* - the child's environment forces UTF-8 (`PYTHONUTF8`/`PYTHONIOENCODING`);
|
|
19
|
+
* without it the CLI decodes its stdin with the host locale codec (cp936
|
|
20
|
+
* here), a character GBK cannot represent becomes a lone surrogate, and
|
|
21
|
+
* httpx then dies with "surrogates not allowed";
|
|
22
|
+
* - on failure the **tail** of stderr is kept, because a Python traceback puts
|
|
23
|
+
* the actual exception last.
|
|
24
|
+
*/
|
|
25
|
+
import { spawn as nodeSpawn } from 'node:child_process'
|
|
26
|
+
|
|
27
|
+
/** argv for a `python -m memvault.cli …` call. */
|
|
28
|
+
export function buildCliInvocation({ pythonPath, projectDir, args = [] }) {
|
|
29
|
+
return { command: pythonPath, args: ['-m', 'memvault.cli', ...args], cwd: projectDir }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Spawn the CLI and resolve with its output. Never rejects: a write path is a
|
|
34
|
+
* side channel and must not throw into whatever is driving it.
|
|
35
|
+
*
|
|
36
|
+
* @param options.config - `{ pythonPath, projectDir, timeoutMs, env }`.
|
|
37
|
+
* @param options.args - CLI arguments after `memvault.cli`.
|
|
38
|
+
* @param options.stdin - optional string written to the child's stdin.
|
|
39
|
+
* @param options.spawn - injectable spawn (tests).
|
|
40
|
+
* @returns {Promise<{ok: boolean, stdout?: string, stderr?: string, error?: string}>}
|
|
41
|
+
*/
|
|
42
|
+
export function runCli({ config, args, stdin = null, spawn = nodeSpawn }) {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const { command, args: argv, cwd } = buildCliInvocation({ ...config, args })
|
|
45
|
+
let child
|
|
46
|
+
try {
|
|
47
|
+
child = spawn(command, argv, {
|
|
48
|
+
cwd,
|
|
49
|
+
env: { ...process.env, ...(config.env ?? {}) },
|
|
50
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
51
|
+
windowsHide: true,
|
|
52
|
+
})
|
|
53
|
+
} catch (error) {
|
|
54
|
+
resolve({ ok: false, error: `spawn failed: ${error.message}` })
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let stdout = ''
|
|
59
|
+
let stderr = ''
|
|
60
|
+
let settled = false
|
|
61
|
+
const finish = (value) => {
|
|
62
|
+
if (settled) return
|
|
63
|
+
settled = true
|
|
64
|
+
clearTimeout(timer)
|
|
65
|
+
resolve(value)
|
|
66
|
+
}
|
|
67
|
+
const timer = setTimeout(() => {
|
|
68
|
+
try { child.kill() } catch { /* already gone */ }
|
|
69
|
+
finish({ ok: false, error: `timed out after ${config.timeoutMs ?? 120_000}ms` })
|
|
70
|
+
}, config.timeoutMs ?? 120_000)
|
|
71
|
+
|
|
72
|
+
child.stdout?.on('data', (d) => { stdout += d })
|
|
73
|
+
child.stderr?.on('data', (d) => { stderr += d })
|
|
74
|
+
child.on('error', (error) => finish({ ok: false, error: error.message }))
|
|
75
|
+
child.on('close', (code) => {
|
|
76
|
+
if (code !== 0) {
|
|
77
|
+
finish({ ok: false, error: `exit ${code}: ${stderr.trim().slice(-700)}`, stdout, stderr })
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
finish({ ok: true, stdout, stderr })
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
if (stdin === null) child.stdin?.end()
|
|
85
|
+
else child.stdin?.end(stdin)
|
|
86
|
+
} catch (error) {
|
|
87
|
+
finish({ ok: false, error: `stdin failed: ${error.message}` })
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
}
|