dsh-memvault 0.9.12 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +52 -397
  2. package/README.zh.md +51 -325
  3. package/cordis.patch.yml +18 -20
  4. package/docs/CORE-DESIGN.md +277 -0
  5. package/docs/DETAIL.md +1 -0
  6. package/docs/DETAIL.zh.md +1 -0
  7. package/docs/DEVLOG.md +752 -0
  8. package/docs/images/relation-graph-2026-11-02.png +0 -0
  9. package/docs/preview.svg +41 -0
  10. package/icon.svg +9 -0
  11. package/lib/blocks.js +11 -2
  12. package/lib/client.js +705 -168
  13. package/lib/config.js +46 -87
  14. package/lib/core/adjudicate.js +94 -0
  15. package/lib/core/backfill.js +32 -0
  16. package/lib/core/block-write.js +85 -0
  17. package/lib/core/decide.js +159 -0
  18. package/lib/core/embed.js +122 -0
  19. package/lib/core/env.js +72 -0
  20. package/lib/core/extract.js +151 -0
  21. package/lib/core/host.js +81 -0
  22. package/lib/core/importers.js +261 -0
  23. package/lib/core/llm.js +49 -0
  24. package/lib/{memories.js → core/panel-read.js} +80 -136
  25. package/lib/core/pipeline.js +95 -0
  26. package/lib/core/schema.js +123 -0
  27. package/lib/core/search.js +191 -0
  28. package/lib/core/store.js +347 -0
  29. package/lib/core/tools.js +182 -0
  30. package/lib/core/write.js +89 -0
  31. package/lib/extract.js +1 -39
  32. package/lib/index.js +194 -128
  33. package/lib/panel.js +13 -11
  34. package/lib/review.js +5 -1
  35. package/lib/structure.js +6 -1
  36. package/locale/en.json +7 -0
  37. package/locale/zh.json +7 -0
  38. package/package.json +11 -8
  39. package/scripts/backfill-core.mjs +53 -0
  40. package/scripts/import-core.mjs +56 -0
  41. package/scripts/smoke-config.mjs +12 -14
  42. package/scripts/smoke-extract.mjs +19 -40
  43. package/scripts/smoke-package.mjs +105 -27
  44. package/scripts/smoke-panel.mjs +293 -162
  45. package/src/client/index.js +703 -166
  46. package/lib/blocks-write.js +0 -121
  47. package/lib/cli.js +0 -90
  48. package/lib/reindex.js +0 -32
@@ -1,121 +0,0 @@
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/cli.js DELETED
@@ -1,90 +0,0 @@
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
- }
package/lib/reindex.js DELETED
@@ -1,32 +0,0 @@
1
- /**
2
- * Ask MemVault to re-embed, or to report what that would cost.
3
- *
4
- * Dry run is the default and the caller has to opt in to writing: switching embedders spends
5
- * one API call per changed row and sends memory text off the machine, so the number comes
6
- * first. The CLI is the same one the rest of the write half drives, and it prints one JSON
7
- * object, which is what this returns.
8
- */
9
- import { spawnSync } from 'node:child_process'
10
-
11
- export function runReindex({ pythonPath, projectDir, env, apply = false, timeoutMs = 15 * 60 * 1000 } = {}) {
12
- if (!pythonPath || !projectDir) return { ok: false, error: 'MemVault python/project not configured' }
13
- const args = ['-m', 'memvault.cli', 'reindex']
14
- if (apply) args.push('--apply')
15
- const result = spawnSync(pythonPath, args, {
16
- cwd: projectDir,
17
- env: { ...env, PYTHONUTF8: '1' },
18
- encoding: 'utf8',
19
- timeout: timeoutMs,
20
- maxBuffer: 8 * 1024 * 1024,
21
- })
22
- if (result.error) return { ok: false, error: String(result.error.message ?? result.error) }
23
- if (result.status !== 0) {
24
- const tail = (result.stderr || result.stdout || '').trim().split(String.fromCharCode(10)).slice(-3).join(' | ')
25
- return { ok: false, error: tail || ('exit ' + String(result.status)) }
26
- }
27
- try {
28
- return { ok: true, report: JSON.parse(result.stdout) }
29
- } catch {
30
- return { ok: false, error: 'the CLI did not print one JSON object' }
31
- }
32
- }