dsh-memvault 0.9.12 → 0.10.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 +51 -396
- package/README.zh.md +50 -324
- package/cordis.patch.yml +18 -20
- package/docs/CORE-DESIGN.md +277 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/DEVLOG.md +701 -0
- package/docs/images/relation-graph-2026-11-02.png +0 -0
- package/docs/preview.svg +41 -0
- package/icon.svg +9 -0
- package/lib/blocks.js +11 -2
- package/lib/client.js +705 -168
- package/lib/config.js +46 -87
- package/lib/core/adjudicate.js +94 -0
- package/lib/core/backfill.js +32 -0
- package/lib/core/block-write.js +85 -0
- package/lib/core/decide.js +159 -0
- package/lib/core/embed.js +122 -0
- package/lib/core/env.js +72 -0
- package/lib/core/extract.js +151 -0
- package/lib/core/host.js +81 -0
- package/lib/core/importers.js +261 -0
- package/lib/core/llm.js +49 -0
- package/lib/{memories.js → core/panel-read.js} +80 -136
- package/lib/core/pipeline.js +95 -0
- package/lib/core/schema.js +123 -0
- package/lib/core/search.js +191 -0
- package/lib/core/store.js +347 -0
- package/lib/core/tools.js +182 -0
- package/lib/core/write.js +89 -0
- package/lib/extract.js +1 -39
- package/lib/index.js +194 -128
- package/lib/panel.js +13 -11
- package/lib/review.js +5 -1
- package/lib/structure.js +6 -1
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +10 -7
- package/scripts/backfill-core.mjs +53 -0
- package/scripts/import-core.mjs +56 -0
- package/scripts/smoke-config.mjs +12 -14
- package/scripts/smoke-extract.mjs +19 -40
- package/scripts/smoke-package.mjs +88 -27
- package/scripts/smoke-panel.mjs +293 -162
- package/src/client/index.js +703 -166
- package/lib/blocks-write.js +0 -121
- package/lib/cli.js +0 -90
- package/lib/reindex.js +0 -32
package/lib/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
window.__ModuleLoader__.load({ id: "dsh-memvault", factory: (require) => { var module = { exports: {} }; var exports = module.exports;
|
|
2
2
|
// generated from src/client/index.js by scripts/build-client.mjs — do not edit
|
|
3
|
-
// source-sha256:
|
|
3
|
+
// source-sha256: 9e5dd40c233914b7
|
|
4
4
|
/**
|
|
5
5
|
* dsh-memvault — browser half: the memory panel.
|
|
6
6
|
*
|
|
@@ -21,7 +21,7 @@ window.__ModuleLoader__.load({ id: "dsh-memvault", factory: (require) => { var m
|
|
|
21
21
|
*
|
|
22
22
|
* @module dsh-memvault/client
|
|
23
23
|
*/
|
|
24
|
-
const { createElement: h, Fragment, useEffect, useState } = require('react')
|
|
24
|
+
const { createElement: h, Fragment, useEffect, useMemo, useState } = require('react')
|
|
25
25
|
|
|
26
26
|
/** Must equal the package name: it is the bundle id the host serves under /plugins. */
|
|
27
27
|
const NAME = 'dsh-memvault'
|
|
@@ -103,6 +103,22 @@ const S = {
|
|
|
103
103
|
border: `1px solid ${tone}`, background: filled ? tone : 'transparent',
|
|
104
104
|
color: filled ? '#fff' : tone,
|
|
105
105
|
}),
|
|
106
|
+
link: { border: 'none', background: 'transparent', color: C.muted, cursor: 'pointer', padding: 0, fontSize: '12px' },
|
|
107
|
+
// Tool cards: they replace the generic Tool row for the two memory tools, so they
|
|
108
|
+
// carry the row chrome themselves and stay inside the Chat flow's rhythm.
|
|
109
|
+
toolCard: {
|
|
110
|
+
border: `1px solid ${C.border}`, borderRadius: '10px', background: C.layer1,
|
|
111
|
+
padding: '8px 12px', margin: '6px 0', fontSize: '12.5px', lineHeight: 1.5,
|
|
112
|
+
},
|
|
113
|
+
toolHead: { display: 'flex', alignItems: 'baseline', gap: '8px', flexWrap: 'wrap', marginBottom: '4px' },
|
|
114
|
+
toolMeta: { color: C.muted, fontSize: '11.5px' },
|
|
115
|
+
toolRow: { display: 'flex', gap: '8px', alignItems: 'baseline', marginTop: '4px', minWidth: 0 },
|
|
116
|
+
toolText: { flex: '1 1 auto', minWidth: 0, wordBreak: 'break-word' },
|
|
117
|
+
toolRaw: {
|
|
118
|
+
margin: '4px 0 0', padding: '6px 8px', borderRadius: '6px', background: C.bg,
|
|
119
|
+
border: `1px solid ${C.border}`, fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
|
|
120
|
+
fontSize: '11.5px', whiteSpace: 'pre-wrap', wordBreak: 'break-word', maxHeight: '260px', overflow: 'auto',
|
|
121
|
+
},
|
|
106
122
|
}
|
|
107
123
|
|
|
108
124
|
const outcomeTone = (outcome) => (String(outcome ?? '').startsWith('ok') ? C.ok
|
|
@@ -126,6 +142,60 @@ function Card({ title, children }) {
|
|
|
126
142
|
return h('div', { style: S.card }, h('div', { style: S.cardTitle }, title), children)
|
|
127
143
|
}
|
|
128
144
|
|
|
145
|
+
function Details({ summary, children }) {
|
|
146
|
+
const [open, setOpen] = useState(false)
|
|
147
|
+
return h('div', { style: { marginTop: '10px' } },
|
|
148
|
+
h('button', { type: 'button', style: S.link, onClick: () => setOpen((v) => !v) },
|
|
149
|
+
`${open ? '▾' : '▸'} ${summary}`),
|
|
150
|
+
open && h('div', { style: { marginTop: '10px' } }, children),
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Vector backfill: the only write that costs embedding calls, so it is explicit. */
|
|
155
|
+
function VectorCard({ url }) {
|
|
156
|
+
const [busy, setBusy] = useState(false)
|
|
157
|
+
const [line, setLine] = useState('')
|
|
158
|
+
const call = async (apply) => {
|
|
159
|
+
setBusy(true)
|
|
160
|
+
try {
|
|
161
|
+
const res = await fetch(url, {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers: { 'content-type': 'application/json' },
|
|
164
|
+
body: JSON.stringify(apply ? { apply: true } : {}),
|
|
165
|
+
})
|
|
166
|
+
const json = await res.json().catch(() => null)
|
|
167
|
+
const report = json?.report
|
|
168
|
+
if (!res.ok || !report) {
|
|
169
|
+
setLine(`失败:${json?.error ?? `HTTP ${res.status}`}`)
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
setLine(apply
|
|
173
|
+
? `已回填 ${report.embedded ?? 0} 条(模型 ${report.model})`
|
|
174
|
+
: `缺失 ${report.missing ?? 0} 条(模型 ${report.model})`)
|
|
175
|
+
} catch (err) {
|
|
176
|
+
setLine(String(err?.message ?? err))
|
|
177
|
+
} finally {
|
|
178
|
+
setBusy(false)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return h(Card, { title: '向量回填' },
|
|
182
|
+
h('div', { style: S.hint }, '向量按模型存储;缺向量的记忆在检索里只剩关键词通道。回填会为每条缺失行调用一次嵌入模型。'),
|
|
183
|
+
h('div', { style: { ...S.rowActions, marginTop: '8px' } },
|
|
184
|
+
h('button', { type: 'button', style: S.small(C.brand), disabled: busy, onClick: () => call(false) }, '检查缺口'),
|
|
185
|
+
h('button', {
|
|
186
|
+
type: 'button',
|
|
187
|
+
style: S.small(C.warn),
|
|
188
|
+
disabled: busy,
|
|
189
|
+
onClick: () => {
|
|
190
|
+
if (!window.confirm('回填会为每条缺失行调用一次嵌入模型(记忆原文会离开本机)。先点「检查缺口」看数量。继续?')) return
|
|
191
|
+
call(true)
|
|
192
|
+
},
|
|
193
|
+
}, '回填'),
|
|
194
|
+
),
|
|
195
|
+
line && h('div', { style: { ...S.hint, marginTop: '6px' } }, line),
|
|
196
|
+
)
|
|
197
|
+
}
|
|
198
|
+
|
|
129
199
|
function fmtAge(ms) {
|
|
130
200
|
if (ms === null || ms === undefined) return '尚未读取'
|
|
131
201
|
if (ms < 1000) return `${ms} ms`
|
|
@@ -408,7 +478,7 @@ function ReviewCard({ refreshToken, onChanged, onError }) {
|
|
|
408
478
|
h('span', { style: S.mono, title: item.id }, `${String(item.id).slice(0, 12)}…`),
|
|
409
479
|
item.note && h('span', { style: S.badge(C.idle) }, item.note),
|
|
410
480
|
h('span', { style: S.spacer }),
|
|
411
|
-
item.replayable && h('button', { style: S.small(C.brand), onClick: () => openReplay(item) }, '重抽这个窗口'),
|
|
481
|
+
item.replayable && replayEnabled && h('button', { style: S.small(C.brand), onClick: () => openReplay(item) }, '重抽这个窗口'),
|
|
412
482
|
h('button', { style: S.small(C.muted), disabled: busy, onClick: () => unflag(item.id) }, '取消标记'),
|
|
413
483
|
h(CopyId, { id: item.id }),
|
|
414
484
|
),
|
|
@@ -416,7 +486,7 @@ function ReviewCard({ refreshToken, onChanged, onError }) {
|
|
|
416
486
|
h('div', { style: { ...S.hint, marginTop: '4px' } },
|
|
417
487
|
[
|
|
418
488
|
item.window ? `来自窗口 @${item.window.seq ?? '?'}(${item.window.at ? new Date(item.window.at).toLocaleString() : '时间未知'})` : '来源窗口已滚出保留窗口',
|
|
419
|
-
item.replayable ? '原文仍在,可重抽' : '原文已滚出(只保留最近几个窗口)',
|
|
489
|
+
item.replayable ? (replayEnabled ? '原文仍在,可重抽' : '原文仍在,但抽取已关闭') : '原文已滚出(只保留最近几个窗口)',
|
|
420
490
|
item.history?.length ? `审计 ${item.history.map((entry) => entry.action ?? '?').join('→')}` : null,
|
|
421
491
|
].filter(Boolean).join(' · ')),
|
|
422
492
|
editing?.id === item.id && h('div', { style: S.editor },
|
|
@@ -462,63 +532,415 @@ function TypeBar({ byType, height = 8 }) {
|
|
|
462
532
|
}
|
|
463
533
|
|
|
464
534
|
/**
|
|
465
|
-
*
|
|
535
|
+
* Layout constants for the relation graph. Kept together so the preview harness
|
|
536
|
+
* and the component cannot drift apart.
|
|
537
|
+
*/
|
|
538
|
+
const GRAPH_BOX = { width: 720, height: 400, pad: 30 }
|
|
539
|
+
const GRAPH_MAX_EDGES = 48
|
|
540
|
+
|
|
541
|
+
/** Deterministic [0,1) from a string: the same store must draw the same picture twice. */
|
|
542
|
+
function hash01(text) {
|
|
543
|
+
let h = 2166136261 >>> 0
|
|
544
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
545
|
+
h ^= text.charCodeAt(i)
|
|
546
|
+
h = Math.imul(h, 16777619)
|
|
547
|
+
}
|
|
548
|
+
h ^= h >>> 13
|
|
549
|
+
h = Math.imul(h, 0x5bd1e995)
|
|
550
|
+
h ^= h >>> 15
|
|
551
|
+
return (h >>> 0) / 4294967296
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const escapeXml = (text) => String(text ?? '')
|
|
555
|
+
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Fruchterman–Reingold on one connected component, in local coordinates.
|
|
466
559
|
*
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
* deterministic (the same store draws the same picture twice).
|
|
560
|
+
* Returns positions centred on the origin plus the component's bounding box. The
|
|
561
|
+
* simulation is deterministic: seeds come from `hash01(id)`, there is no
|
|
562
|
+
* randomness, so hovering or re-rendering never reshuffles the picture.
|
|
471
563
|
*/
|
|
472
|
-
function
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
564
|
+
function simulateComponent(ids, edges, { size = 160, iterations = 220 } = {}) {
|
|
565
|
+
const n = ids.length
|
|
566
|
+
const pos = ids.map((id) => {
|
|
567
|
+
const angle = hash01(`${id}:angle`) * Math.PI * 2
|
|
568
|
+
const radius = Math.sqrt(hash01(`${id}:radius`)) * (size * 0.42)
|
|
569
|
+
return { x: Math.cos(angle) * radius, y: Math.sin(angle) * radius }
|
|
570
|
+
})
|
|
571
|
+
const local = new Map(ids.map((id, i) => [id, i]))
|
|
572
|
+
const links = edges
|
|
573
|
+
.filter((edge) => local.has(edge.source) && local.has(edge.target))
|
|
574
|
+
.map((edge) => ({ s: local.get(edge.source), t: local.get(edge.target), weight: edge.weight }))
|
|
575
|
+
|
|
576
|
+
const k = Math.sqrt((size * size) / Math.max(1, n)) * 0.8
|
|
577
|
+
const limit = size * 0.48
|
|
578
|
+
const moveX = new Float64Array(n)
|
|
579
|
+
const moveY = new Float64Array(n)
|
|
580
|
+
|
|
581
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
582
|
+
moveX.fill(0)
|
|
583
|
+
moveY.fill(0)
|
|
584
|
+
for (let i = 0; i < n; i += 1) {
|
|
585
|
+
for (let j = i + 1; j < n; j += 1) {
|
|
586
|
+
let dx = pos[i].x - pos[j].x
|
|
587
|
+
let dy = pos[i].y - pos[j].y
|
|
588
|
+
let d2 = dx * dx + dy * dy
|
|
589
|
+
if (d2 < 1e-4) {
|
|
590
|
+
dx = hash01(`${ids[i]}|${ids[j]}`) - 0.5
|
|
591
|
+
dy = hash01(`${ids[j]}|${ids[i]}`) - 0.5
|
|
592
|
+
d2 = dx * dx + dy * dy || 1e-4
|
|
593
|
+
}
|
|
594
|
+
const d = Math.sqrt(d2)
|
|
595
|
+
const force = (k * k) / d
|
|
596
|
+
const fx = (dx / d) * force
|
|
597
|
+
const fy = (dy / d) * force
|
|
598
|
+
moveX[i] += fx
|
|
599
|
+
moveY[i] += fy
|
|
600
|
+
moveX[j] -= fx
|
|
601
|
+
moveY[j] -= fy
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
for (const link of links) {
|
|
605
|
+
const a = pos[link.s]
|
|
606
|
+
const b = pos[link.t]
|
|
607
|
+
const dx = a.x - b.x
|
|
608
|
+
const dy = a.y - b.y
|
|
609
|
+
const d = Math.hypot(dx, dy) || 0.01
|
|
610
|
+
const force = ((d * d) / k) * (0.75 + link.weight * 0.8)
|
|
611
|
+
const fx = (dx / d) * force
|
|
612
|
+
const fy = (dy / d) * force
|
|
613
|
+
moveX[link.s] -= fx
|
|
614
|
+
moveY[link.s] -= fy
|
|
615
|
+
moveX[link.t] += fx
|
|
616
|
+
moveY[link.t] += fy
|
|
617
|
+
}
|
|
618
|
+
const cooling = 1 - step / iterations
|
|
619
|
+
const stepLimit = k * cooling * 1.3
|
|
620
|
+
for (let i = 0; i < n; i += 1) {
|
|
621
|
+
const dx = Math.max(-stepLimit, Math.min(stepLimit, moveX[i]))
|
|
622
|
+
const dy = Math.max(-stepLimit, Math.min(stepLimit, moveY[i]))
|
|
623
|
+
pos[i].x = Math.max(-limit, Math.min(limit, pos[i].x + dx))
|
|
624
|
+
pos[i].y = Math.max(-limit, Math.min(limit, pos[i].y + dy))
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
let minX = Infinity
|
|
629
|
+
let minY = Infinity
|
|
630
|
+
let maxX = -Infinity
|
|
631
|
+
let maxY = -Infinity
|
|
632
|
+
for (const point of pos) {
|
|
633
|
+
minX = Math.min(minX, point.x)
|
|
634
|
+
minY = Math.min(minY, point.y)
|
|
635
|
+
maxX = Math.max(maxX, point.x)
|
|
636
|
+
maxY = Math.max(maxY, point.y)
|
|
637
|
+
}
|
|
638
|
+
const offsetX = -(minX + maxX) / 2
|
|
639
|
+
const offsetY = -(minY + maxY) / 2
|
|
640
|
+
return {
|
|
641
|
+
positions: pos.map((point) => ({ x: point.x + offsetX, y: point.y + offsetY })),
|
|
642
|
+
width: Math.max(1, maxX - minX),
|
|
643
|
+
height: Math.max(1, maxY - minY),
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Layout the relation graph as packed islands.
|
|
649
|
+
*
|
|
650
|
+
* The store's graph is not one big network: it is many small clusters (a pair of
|
|
651
|
+
* contradicting facts, a three-node chain). A single global spring layout spread
|
|
652
|
+
* those islands across the whole canvas — mostly empty space, every edge a short
|
|
653
|
+
* segment between neighbours — which is why the first version looked like a ring
|
|
654
|
+
* of dots. Laying each connected component out on its own and shelf-packing the
|
|
655
|
+
* results keeps related memories together and makes the islands visible.
|
|
656
|
+
*/
|
|
657
|
+
function layoutRelationGraph(graph, {
|
|
658
|
+
width = GRAPH_BOX.width,
|
|
659
|
+
height = GRAPH_BOX.height,
|
|
660
|
+
pad = GRAPH_BOX.pad,
|
|
661
|
+
maxEdges = GRAPH_MAX_EDGES,
|
|
662
|
+
} = {}) {
|
|
663
|
+
const byId = new Map((graph?.nodes ?? []).map((node) => [node.id, node]))
|
|
664
|
+
const chosen = (graph?.edges ?? [])
|
|
665
|
+
.filter((edge) => edge && byId.has(edge.source) && byId.has(edge.target) && edge.source !== edge.target)
|
|
479
666
|
.sort((a, b) => (Number(b.weight) || 0) - (Number(a.weight) || 0))
|
|
480
|
-
.slice(0,
|
|
481
|
-
|
|
482
|
-
const nodes =
|
|
483
|
-
|
|
667
|
+
.slice(0, maxEdges)
|
|
668
|
+
|
|
669
|
+
const nodes = []
|
|
670
|
+
const seen = new Set()
|
|
671
|
+
for (const edge of chosen) {
|
|
672
|
+
for (const id of [edge.source, edge.target]) {
|
|
673
|
+
if (seen.has(id)) continue
|
|
674
|
+
seen.add(id)
|
|
675
|
+
nodes.push(byId.get(id))
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
if (nodes.length === 0) return { width, height, nodes: [], edges: [], maxDegree: 0 }
|
|
679
|
+
|
|
680
|
+
const index = new Map(nodes.map((node, i) => [node.id, i]))
|
|
681
|
+
const edges = chosen.map((edge) => ({
|
|
682
|
+
source: edge.source,
|
|
683
|
+
target: edge.target,
|
|
684
|
+
weight: Number(edge.weight) || 0,
|
|
685
|
+
s: index.get(edge.source),
|
|
686
|
+
t: index.get(edge.target),
|
|
687
|
+
}))
|
|
688
|
+
const degree = new Array(nodes.length).fill(0)
|
|
689
|
+
const adjacency = nodes.map(() => [])
|
|
690
|
+
for (const edge of edges) {
|
|
691
|
+
degree[edge.s] += 1
|
|
692
|
+
degree[edge.t] += 1
|
|
693
|
+
adjacency[edge.s].push(edge.t)
|
|
694
|
+
adjacency[edge.t].push(edge.s)
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// Connected components (every drawn node has at least one edge).
|
|
698
|
+
const visited = new Set()
|
|
699
|
+
const components = []
|
|
700
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
701
|
+
if (visited.has(i)) continue
|
|
702
|
+
const stack = [i]
|
|
703
|
+
const members = []
|
|
704
|
+
visited.add(i)
|
|
705
|
+
while (stack.length > 0) {
|
|
706
|
+
const current = stack.pop()
|
|
707
|
+
members.push(current)
|
|
708
|
+
for (const neighbour of adjacency[current]) {
|
|
709
|
+
if (visited.has(neighbour)) continue
|
|
710
|
+
visited.add(neighbour)
|
|
711
|
+
stack.push(neighbour)
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
components.push(members)
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Lay each component out on its own, then shelf-pack the islands.
|
|
718
|
+
const islands = components
|
|
719
|
+
.map((members) => {
|
|
720
|
+
const ids = members.map((i) => nodes[i].id)
|
|
721
|
+
const size = Math.min(210, 64 + 22 * Math.sqrt(members.length))
|
|
722
|
+
const simulated = simulateComponent(ids, edges, { size })
|
|
723
|
+
const box = {
|
|
724
|
+
members,
|
|
725
|
+
xs: simulated.positions.map((p) => p.x),
|
|
726
|
+
ys: simulated.positions.map((p) => p.y),
|
|
727
|
+
width: simulated.width,
|
|
728
|
+
height: simulated.height,
|
|
729
|
+
}
|
|
730
|
+
return box
|
|
731
|
+
})
|
|
732
|
+
.sort((a, b) => b.height - a.height)
|
|
733
|
+
|
|
734
|
+
const gapX = 24
|
|
735
|
+
const gapY = 22
|
|
736
|
+
let cursorX = 0
|
|
737
|
+
let cursorY = 0
|
|
738
|
+
let rowHeight = 0
|
|
739
|
+
let widestRow = 0
|
|
740
|
+
for (const island of islands) {
|
|
741
|
+
const spanX = island.width + 12
|
|
742
|
+
if (cursorX > 0 && cursorX + spanX > width - pad * 2) {
|
|
743
|
+
widestRow = Math.max(widestRow, cursorX - gapX)
|
|
744
|
+
cursorX = 0
|
|
745
|
+
cursorY += rowHeight + gapY
|
|
746
|
+
rowHeight = 0
|
|
747
|
+
}
|
|
748
|
+
island.offsetX = cursorX + spanX / 2
|
|
749
|
+
island.offsetY = cursorY + island.height / 2
|
|
750
|
+
cursorX += spanX + gapX
|
|
751
|
+
rowHeight = Math.max(rowHeight, island.height + 12)
|
|
752
|
+
}
|
|
753
|
+
const usedWidth = Math.max(1, widestRow, cursorX - gapX)
|
|
754
|
+
const usedHeight = Math.max(1, cursorY + rowHeight)
|
|
755
|
+
const scale = Math.min((width - pad * 2) / usedWidth, (height - pad * 2) / usedHeight, 1)
|
|
756
|
+
const shiftX = (width - usedWidth * scale) / 2
|
|
757
|
+
const shiftY = (height - usedHeight * scale) / 2
|
|
758
|
+
|
|
759
|
+
const placed = new Array(nodes.length)
|
|
760
|
+
const islandOf = new Array(nodes.length).fill(0)
|
|
761
|
+
islands.forEach((island, islandIndex) => {
|
|
762
|
+
island.members.forEach((nodeIndex, i) => {
|
|
763
|
+
islandOf[nodeIndex] = islandIndex
|
|
764
|
+
placed[nodeIndex] = {
|
|
765
|
+
x: shiftX + (island.offsetX + island.xs[i]) * scale,
|
|
766
|
+
y: shiftY + (island.offsetY + island.ys[i]) * scale,
|
|
767
|
+
}
|
|
768
|
+
})
|
|
769
|
+
})
|
|
770
|
+
|
|
771
|
+
const maxDegree = Math.max(1, ...degree)
|
|
772
|
+
const laid = nodes.map((node, i) => ({
|
|
773
|
+
id: node.id,
|
|
774
|
+
type: node.type,
|
|
775
|
+
flagged: node.flagged === true,
|
|
776
|
+
degree: degree[i],
|
|
777
|
+
island: islandOf[i],
|
|
778
|
+
chars: node.chars,
|
|
779
|
+
text: node.text,
|
|
780
|
+
x: placed[i].x,
|
|
781
|
+
y: placed[i].y,
|
|
782
|
+
r: Math.max(3.6, (4.6 + Math.min(6.2, (degree[i] / maxDegree) * 6.2)) * scale),
|
|
783
|
+
}))
|
|
784
|
+
|
|
785
|
+
return {
|
|
786
|
+
width,
|
|
787
|
+
height,
|
|
788
|
+
nodes: laid,
|
|
789
|
+
edges: edges.map((edge) => ({
|
|
790
|
+
source: laid[edge.s].id,
|
|
791
|
+
target: laid[edge.t].id,
|
|
792
|
+
weight: edge.weight,
|
|
793
|
+
x1: laid[edge.s].x,
|
|
794
|
+
y1: laid[edge.s].y,
|
|
795
|
+
x2: laid[edge.t].x,
|
|
796
|
+
y2: laid[edge.t].y,
|
|
797
|
+
})),
|
|
798
|
+
maxDegree,
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* Render a layout as an SVG string.
|
|
804
|
+
*
|
|
805
|
+
* A string rather than JSX so the same renderer feeds the preview harness; the
|
|
806
|
+
* component attaches one delegated click and one hover handler to the wrapper.
|
|
807
|
+
* Colours use style attributes (not presentation attributes) because they are
|
|
808
|
+
* `var(--dsw-…)` expressions, which only resolve inside CSS.
|
|
809
|
+
*/
|
|
810
|
+
function renderRelationGraphSvg(layout, { theme, selectedId = null, hoveredId = null } = {}) {
|
|
811
|
+
const { nodes, edges, width, height } = layout
|
|
812
|
+
if (nodes.length === 0) return ''
|
|
813
|
+
const focus = hoveredId ?? selectedId
|
|
814
|
+
const linked = new Set()
|
|
815
|
+
if (focus) {
|
|
816
|
+
for (const edge of edges) {
|
|
817
|
+
if (edge.source === focus) linked.add(edge.target)
|
|
818
|
+
else if (edge.target === focus) linked.add(edge.source)
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
// Labels are contextual: no text by default, one line for the hovered or
|
|
822
|
+
// selected node. Permanent labels on sixty nodes either overlap or look
|
|
823
|
+
// arbitrary ("why does this one have text?"); the tooltip and the detail card
|
|
824
|
+
// carry the rest, and hover names the node being inspected.
|
|
825
|
+
const parts = []
|
|
826
|
+
for (const edge of edges) {
|
|
827
|
+
const active = focus ? (edge.source === focus || edge.target === focus) : true
|
|
828
|
+
const strokeWidth = 0.7 + edge.weight * 2.1
|
|
829
|
+
const opacity = focus ? (active ? 0.85 : 0.06) : 0.22 + edge.weight * 0.5
|
|
830
|
+
parts.push(`<line x1="${edge.x1.toFixed(1)}" y1="${edge.y1.toFixed(1)}" x2="${edge.x2.toFixed(1)}" y2="${edge.y2.toFixed(1)}" style="stroke:${theme.edge};stroke-width:${strokeWidth.toFixed(2)};opacity:${opacity.toFixed(2)};stroke-linecap:round"/>`)
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
for (const node of nodes) {
|
|
834
|
+
const color = theme.typeColor[node.type] ?? theme.idle
|
|
835
|
+
const selected = node.id === selectedId
|
|
836
|
+
const dim = focus !== null && focus !== undefined && !linked.has(node.id) && node.id !== focus
|
|
837
|
+
const style = [
|
|
838
|
+
`fill:${color}`,
|
|
839
|
+
`fill-opacity:${selected ? 0.34 : 0.26}`,
|
|
840
|
+
`stroke:${color}`,
|
|
841
|
+
`stroke-width:${selected ? 2.2 : 1.3}`,
|
|
842
|
+
]
|
|
843
|
+
if (dim) style.push('opacity:0.18')
|
|
844
|
+
parts.push(`<g data-id="${escapeXml(node.id)}" style="cursor:pointer">`)
|
|
845
|
+
parts.push(`<circle cx="${node.x.toFixed(1)}" cy="${node.y.toFixed(1)}" r="${node.r.toFixed(1)}" style="${style.join(';')}"/>`)
|
|
846
|
+
if (selected) {
|
|
847
|
+
parts.push(`<circle cx="${node.x.toFixed(1)}" cy="${node.y.toFixed(1)}" r="${(node.r + 4).toFixed(1)}" style="fill:none;stroke:${color};stroke-width:1;opacity:0.45"/>`)
|
|
848
|
+
}
|
|
849
|
+
if (node.flagged) {
|
|
850
|
+
parts.push(`<circle cx="${node.x.toFixed(1)}" cy="${node.y.toFixed(1)}" r="${(node.r + 2.6).toFixed(1)}" style="fill:none;stroke:${theme.warn};stroke-width:1.2;stroke-dasharray:2 2"/>`)
|
|
851
|
+
}
|
|
852
|
+
if (node.id === focus) {
|
|
853
|
+
const text = String(node.text ?? node.id)
|
|
854
|
+
const label = text.length > 22 ? `${text.slice(0, 22)}…` : text
|
|
855
|
+
const labelWidth = [...label].reduce((sum, ch) => sum + (ch.charCodeAt(0) > 0x2e80 ? 9.6 : 5.7), 0)
|
|
856
|
+
// Right first, then left/above/below: the first placement that neither
|
|
857
|
+
// leaves the canvas nor covers another node wins.
|
|
858
|
+
const spots = [
|
|
859
|
+
{ x: node.x + node.r + 6, y: node.y + 3.4, anchor: 'start' },
|
|
860
|
+
{ x: node.x - node.r - 6, y: node.y + 3.4, anchor: 'end' },
|
|
861
|
+
{ x: node.x, y: node.y - node.r - 8, anchor: 'middle' },
|
|
862
|
+
{ x: node.x, y: node.y + node.r + 14, anchor: 'middle' },
|
|
863
|
+
]
|
|
864
|
+
const overlapsNode = (box) => nodes.some((other) => other.id !== node.id
|
|
865
|
+
&& box.x < other.x + other.r + 2 && other.x - other.r - 2 < box.x + box.w
|
|
866
|
+
&& box.y < other.y + other.r + 2 && other.y - other.r - 2 < box.y + box.h)
|
|
867
|
+
let spot = spots[0]
|
|
868
|
+
for (const candidate of spots) {
|
|
869
|
+
const left = candidate.anchor === 'start' ? candidate.x
|
|
870
|
+
: candidate.anchor === 'end' ? candidate.x - labelWidth : candidate.x - labelWidth / 2
|
|
871
|
+
const box = { x: left, y: candidate.y - 8, w: labelWidth, h: 13 }
|
|
872
|
+
const inside = box.x >= 4 && box.x + box.w <= width - 4 && box.y >= 4 && box.y + box.h <= height - 2
|
|
873
|
+
if (inside && !overlapsNode(box)) { spot = candidate; break }
|
|
874
|
+
}
|
|
875
|
+
parts.push(`<text x="${spot.x.toFixed(1)}" y="${spot.y.toFixed(1)}" text-anchor="${spot.anchor}" style="font:10px ui-monospace,SFMono-Regular,Consolas,monospace;fill:${theme.fg};paint-order:stroke;stroke:${theme.bg};stroke-width:3.5;stroke-linejoin:round">${escapeXml(label)}</text>`)
|
|
876
|
+
}
|
|
877
|
+
parts.push(`<title>${escapeXml(`${node.text ?? node.id}\n${TYPE_LABEL[node.type] ?? node.type} · ${node.chars} 字符 · 度 ${node.degree}`)}</title>`)
|
|
878
|
+
parts.push('</g>')
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
return `<svg viewBox="0 0 ${width} ${height}" width="100%" role="img" style="display:block;max-width:${width}px;margin:0 auto">${parts.join('')}</svg>`
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
const GRAPH_THEME = {
|
|
885
|
+
bg: C.bg,
|
|
886
|
+
fg: C.fg,
|
|
887
|
+
muted: C.muted,
|
|
888
|
+
edge: C.border2,
|
|
889
|
+
idle: C.idle,
|
|
890
|
+
warn: C.warn,
|
|
891
|
+
typeColor: TYPE_COLOR,
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* The relation graph: packed islands, labels only where they help, and a hover
|
|
896
|
+
* that isolates one memory's neighbourhood instead of drawing everything at once.
|
|
897
|
+
*/
|
|
898
|
+
function RelationGraph({ graph, selectedId, onPick }) {
|
|
899
|
+
const [hovered, setHovered] = useState(null)
|
|
900
|
+
const layout = useMemo(() => layoutRelationGraph(graph), [graph])
|
|
901
|
+
const svg = useMemo(() => renderRelationGraphSvg(layout, {
|
|
902
|
+
theme: GRAPH_THEME,
|
|
903
|
+
selectedId,
|
|
904
|
+
hoveredId: hovered,
|
|
905
|
+
}), [layout, selectedId, hovered])
|
|
906
|
+
|
|
907
|
+
if (layout.nodes.length === 0) {
|
|
484
908
|
return h('div', { style: S.empty },
|
|
485
909
|
(graph?.isolated ?? 0) > 0
|
|
486
910
|
? `${graph.isolated} 条记忆之间没有任何关系边——它们不属于任何矛盾簇,所以这里没有东西可画。`
|
|
487
911
|
: '还没有关系边。矛盾关系是在写入时由 MemVault 判定的,所以这块空着通常只说明“还没写入过互相冲突的事实”。')
|
|
488
912
|
}
|
|
489
|
-
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
const selected = node.id === selectedId
|
|
511
|
-
const r = (selected ? 9 : 5.5) + Math.min(6, node.degree ?? 0)
|
|
512
|
-
return h('g', { key: node.id, onClick: () => onPick(node), style: { cursor: 'pointer' } },
|
|
513
|
-
h('circle', {
|
|
514
|
-
cx: point[0], cy: point[1], r,
|
|
515
|
-
fill: TYPE_COLOR[node.type] ?? C.idle,
|
|
516
|
-
stroke: selected ? C.fg : node.flagged ? C.warn : 'none',
|
|
517
|
-
strokeWidth: selected ? 3 : node.flagged ? 2 : 0,
|
|
518
|
-
}),
|
|
519
|
-
h('title', null, `${node.id}\n${TYPE_LABEL[node.type] ?? node.type} · ${node.chars} 字符 · 度 ${node.degree}\n${node.text}`),
|
|
520
|
-
)
|
|
913
|
+
|
|
914
|
+
const nearestNode = (event) => {
|
|
915
|
+
const group = event.target?.closest?.('g[data-id]')
|
|
916
|
+
if (!group) return null
|
|
917
|
+
const id = group.getAttribute('data-id')
|
|
918
|
+
return layout.nodes.find((node) => node.id === id) ?? null
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
return h(Fragment, null,
|
|
922
|
+
h('div', {
|
|
923
|
+
onClick: (event) => {
|
|
924
|
+
const node = nearestNode(event)
|
|
925
|
+
if (node) onPick(node)
|
|
926
|
+
},
|
|
927
|
+
onMouseMove: (event) => {
|
|
928
|
+
const node = nearestNode(event)
|
|
929
|
+
setHovered((current) => (current === (node?.id ?? null) ? current : (node?.id ?? null)))
|
|
930
|
+
},
|
|
931
|
+
onMouseLeave: () => setHovered(null),
|
|
932
|
+
style: { lineHeight: 0, padding: '6px 0 2px' },
|
|
933
|
+
dangerouslySetInnerHTML: { __html: svg },
|
|
521
934
|
}),
|
|
935
|
+
h('div', { style: { ...S.rowActions, marginTop: '10px', gap: '16px' } },
|
|
936
|
+
Object.entries(TYPE_COLOR).map(([type, color]) => h('span', {
|
|
937
|
+
key: type,
|
|
938
|
+
style: { ...S.hint, display: 'flex', gap: '5px', alignItems: 'center' },
|
|
939
|
+
},
|
|
940
|
+
h('span', { style: { width: '8px', height: '8px', borderRadius: '50%', background: color, opacity: 0.8 } }),
|
|
941
|
+
TYPE_LABEL[type] ?? type)),
|
|
942
|
+
h('span', { style: S.hint }, '悬停看名称与邻居 · 点击看详情 · 线宽 = 关系权重'),
|
|
943
|
+
),
|
|
522
944
|
)
|
|
523
945
|
}
|
|
524
946
|
|
|
@@ -573,7 +995,7 @@ function StructureView({ onError }) {
|
|
|
573
995
|
`图已截断:显示 ${graph.nodes.length} / ${graph.totalNodes} 个节点、${graph.edges.length} / ${graph.totalEdges} 条边(按度数取前若干,避免线团)。`),
|
|
574
996
|
h('div', { style: { ...S.rowActions, marginTop: '10px' } },
|
|
575
997
|
h('button', { style: S.small(C.brand), disabled: loading, onClick: load }, loading ? '刷新中…' : '刷新'),
|
|
576
|
-
h('span', { style: S.hint }, '
|
|
998
|
+
h('span', { style: S.hint }, '「维度」不是分区:同一行同时属于 user 与 agent,所以两栏的数字会重复计数——这就是隔离模型的真实形状。'),
|
|
577
999
|
),
|
|
578
1000
|
),
|
|
579
1001
|
|
|
@@ -613,11 +1035,13 @@ function StructureView({ onError }) {
|
|
|
613
1035
|
),
|
|
614
1036
|
|
|
615
1037
|
h(Card, { title: `关系图(${graph.nodes.length} 个节点 / ${graph.edges.length} 条边)` },
|
|
616
|
-
h(
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
'
|
|
1038
|
+
h(Details, { summary: '展开关系图' },
|
|
1039
|
+
h(RelationGraph, { graph, selectedId: selected?.id, onPick: setSelected }),
|
|
1040
|
+
h('div', { style: { ...S.hint, marginTop: '8px' } },
|
|
1041
|
+
'节点=记忆,连线=关系,权重决定线宽与不透明度;节点大小随度数。'),
|
|
1042
|
+
(graph.totalEdges ?? 0) > 0 && (graph.edges?.length ?? 0) >= 40 && h('div', { style: { ...S.hint, marginTop: '4px' } },
|
|
1043
|
+
`图中只画最重的 ${graph.edges.length} 条边(库中共 ${graph.totalEdges} 条),否则 120 个点会连成一个看不出关系的圆环。`),
|
|
1044
|
+
),
|
|
621
1045
|
),
|
|
622
1046
|
|
|
623
1047
|
selected && h(Card, { title: '选中的记忆' },
|
|
@@ -841,13 +1265,13 @@ function MemoriesView({ onError, initialIds = '' }) {
|
|
|
841
1265
|
}),
|
|
842
1266
|
`只看自动改型(${data?.retypedCount ?? 0} 条)`),
|
|
843
1267
|
h('span', { style: S.spacer }),
|
|
1268
|
+
h('span', { style: S.hint },
|
|
1269
|
+
total === 0 ? '没有匹配' : `${from}–${to} / ${total} 条 · 子串匹配(语义召回请让模型调 memory_search)`),
|
|
844
1270
|
ids && h('span', { style: S.badge(C.brand) },
|
|
845
1271
|
`只看某次抽取产出的 ${ids.split(',').filter(Boolean).length} 条`,
|
|
846
1272
|
' ',
|
|
847
1273
|
h('button', { style: S.small(C.muted), onClick: () => { setIds(''); setQuery((c) => ({ ...c, offset: 0 })) } }, '清除')),
|
|
848
1274
|
),
|
|
849
|
-
h('div', { style: S.hint },
|
|
850
|
-
`共 ${total} 条${total > 0 ? ` · 显示第 ${from}–${to} 条` : ''} · 子串匹配(非语义检索;语义召回请让模型调 memory_search)`),
|
|
851
1275
|
),
|
|
852
1276
|
error && h('div', { style: S.banner(C.err) }, `读取记忆失败:${error}`),
|
|
853
1277
|
!error && (data?.rows?.length ?? 0) === 0 && h('div', { style: S.empty }, '没有匹配的记忆。'),
|
|
@@ -975,8 +1399,7 @@ function MemoryPanel() {
|
|
|
975
1399
|
h('span', { style: S.hint }, state),
|
|
976
1400
|
h('span', { style: S.spacer }),
|
|
977
1401
|
h('span', { style: S.hint }, data?.at ? `快照 ${new Date(data.at).toLocaleTimeString()}` : ''),
|
|
978
|
-
h('button', { style: S.btn(
|
|
979
|
-
h('button', { style: S.btn(true), disabled: busy, onClick: () => load(true), title: '跳过 30 秒缓存,立刻重读核心块' }, '立即重读'),
|
|
1402
|
+
h('button', { style: S.btn(true), disabled: busy, onClick: () => load(true), title: '跳过缓存,立刻重读核心块' }, busy ? '读取中…' : '刷新'),
|
|
980
1403
|
h('button', { style: S.small(C.warn), disabled: busy || extract?.enabled === false, onClick: flushNow, title: '把当前待抽取的窗口立刻交给 MemVault' }, '立即抽取'),
|
|
981
1404
|
),
|
|
982
1405
|
|
|
@@ -993,11 +1416,11 @@ function MemoryPanel() {
|
|
|
993
1416
|
h('button', {
|
|
994
1417
|
style: S.small(view === 'blocks' ? C.brand : C.muted, view === 'blocks'),
|
|
995
1418
|
onClick: () => setView('blocks'),
|
|
996
|
-
}, '
|
|
1419
|
+
}, '概览'),
|
|
997
1420
|
h('button', {
|
|
998
1421
|
style: S.small(view === 'memories' ? C.brand : C.muted, view === 'memories'),
|
|
999
1422
|
onClick: () => setView('memories'),
|
|
1000
|
-
}, '
|
|
1423
|
+
}, '记忆'),
|
|
1001
1424
|
h('button', {
|
|
1002
1425
|
style: S.small(view === 'structure' ? C.brand : C.muted, view === 'structure'),
|
|
1003
1426
|
onClick: () => setView('structure'),
|
|
@@ -1005,71 +1428,77 @@ function MemoryPanel() {
|
|
|
1005
1428
|
),
|
|
1006
1429
|
|
|
1007
1430
|
view === 'blocks' ? h(Fragment, null,
|
|
1008
|
-
h(Card, { title: '
|
|
1431
|
+
h(Card, { title: '存储与注入' },
|
|
1009
1432
|
h('div', { style: S.grid },
|
|
1010
|
-
h(KV, { k: '上下文名', v: read?.name }),
|
|
1011
|
-
h(KV, { k: 'order', v: read?.order }),
|
|
1012
|
-
h(KV, { k: '块数 / 字符', v: read ? `${read.blockCount} 块 · ${read.renderedChars} / ${read.maxChars} 字符` : '—' }),
|
|
1013
|
-
h(KV, { k: '缓存年龄', v: read ? `${fmtAge(read.cacheAgeMs)}(TTL ${Math.round((read.refreshMs ?? 0) / 1000)} s)` : '—' }),
|
|
1014
|
-
h(KV, { k: '作用域', v: read?.scopes?.join(' ') }),
|
|
1015
|
-
h(KV, { k: 'label 过滤', v: read?.labels?.length ? read.labels.join(', ') : '全部' }),
|
|
1016
1433
|
h(KV, { k: '库文件', v: read?.dbPath }),
|
|
1017
|
-
),
|
|
1018
|
-
),
|
|
1019
|
-
|
|
1020
|
-
h(Card, { title: '位置与初始化' },
|
|
1021
|
-
h('div', null,
|
|
1022
|
-
h(KV, { k: '库文件(读)', v: read?.dbPath }),
|
|
1023
1434
|
h(KV, {
|
|
1024
|
-
k: '
|
|
1435
|
+
k: '文件',
|
|
1025
1436
|
v: data?.db
|
|
1026
1437
|
? (data.db.exists
|
|
1027
|
-
?
|
|
1028
|
-
: '
|
|
1438
|
+
? `${fmtBytes(data.db.bytes)} · ${data.db.modifiedAt ? `${fmtAge(Date.now() - Date.parse(data.db.modifiedAt))}前` : '—'}`
|
|
1439
|
+
: '不存在(写入第一条时创建)')
|
|
1029
1440
|
: '—',
|
|
1030
1441
|
}),
|
|
1031
|
-
h(KV, { k: '
|
|
1032
|
-
h(KV, { k: '
|
|
1033
|
-
|
|
1034
|
-
|
|
1442
|
+
h(KV, { k: '注入', v: read ? `${read.blockCount} 块 · ${read.renderedChars} / ${read.maxChars} 字符` : '—' }),
|
|
1443
|
+
h(KV, { k: '注入作用域', v: read?.scopes?.join(' ') }),
|
|
1444
|
+
// The write scope is derived from `scopes` by the host half; it used to be
|
|
1445
|
+
// three config keys the panel displayed and no code read.
|
|
1035
1446
|
h(KV, {
|
|
1036
1447
|
k: '写入作用域',
|
|
1037
|
-
v:
|
|
1448
|
+
v: (() => {
|
|
1449
|
+
const w = data?.writeScope
|
|
1450
|
+
if (!w) return '—'
|
|
1451
|
+
const parts = [w.userId ? `user/${w.userId}` : '', w.agentId ? `agent/${w.agentId}` : '', w.runId ? `run/${w.runId}` : '']
|
|
1452
|
+
return parts.filter(Boolean).join(' ') || '(仅由抽取器决定)'
|
|
1453
|
+
})(),
|
|
1038
1454
|
}),
|
|
1455
|
+
h(KV, { k: '抽取', v: extract?.enabled === false ? '已关闭(只读,工具仍可用)' : '开启' }),
|
|
1039
1456
|
),
|
|
1040
|
-
h(
|
|
1041
|
-
'
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
h('
|
|
1062
|
-
|
|
1457
|
+
h(Details, { summary: '路径、窗口与自检' },
|
|
1458
|
+
h('div', { style: S.grid },
|
|
1459
|
+
h(KV, { k: '状态文件', v: extract?.statePath }),
|
|
1460
|
+
h(KV, { k: '窗口策略', v: extract?.window ? `≥${extract.window.everyNTurns} 轮起,静默 ${Math.round((extract.window.idleMs ?? 0) / 1000)} s 或满 ${extract.window.windowTurns} 轮` : '—' }),
|
|
1461
|
+
h(KV, { k: '待抽取', v: extract?.window?.pending?.length
|
|
1462
|
+
? extract.window.pending.map((p) => `${String(p.sessionId).slice(-8)} · ${p.turns} 轮`).join(' ')
|
|
1463
|
+
: '空' }),
|
|
1464
|
+
h(KV, { k: '窗口丢弃', v: (() => {
|
|
1465
|
+
const d = extract?.window?.pendingDropped
|
|
1466
|
+
if (!d) return '未知'
|
|
1467
|
+
const caps = `上限 ${d.maxPendingSessions} 个 / ${Math.round(d.maxPendingAgeMs / 60000)} 分钟`
|
|
1468
|
+
return (d.overCap || d.expired)
|
|
1469
|
+
? `超上限 ${d.overCap} · 过期 ${d.expired}(${caps})`
|
|
1470
|
+
: `无(${caps})`
|
|
1471
|
+
})() }),
|
|
1472
|
+
h(KV, { k: '转写预算', v: extract ? `≤ ${extract.maxInputChars} 字符,< ${extract.minTranscriptChars} 跳过` : '—' }),
|
|
1473
|
+
h(KV, { k: '送助手/工具', v: `${extract?.includeAssistant ? '是' : '否'} / ${extract?.includeTools ? '是' : '否'}` }),
|
|
1474
|
+
h(KV, { k: '结束原因', v: extract?.endReasons?.join(' / ') }),
|
|
1475
|
+
h(KV, { k: '水位会话数', v: extract?.sessions }),
|
|
1476
|
+
),
|
|
1477
|
+
(data?.paths?.length > 0) && h('div', { style: { marginTop: '10px' } },
|
|
1478
|
+
h('div', { style: { ...S.hint, marginBottom: '4px' } },
|
|
1479
|
+
`路径来源:${data?.discovery?.source === 'env'
|
|
1480
|
+
? '环境变量 MEMVAULT_DIR'
|
|
1481
|
+
: data?.discovery?.source === 'discovered'
|
|
1482
|
+
? '自动发现(找到含 .env 的目录)'
|
|
1483
|
+
: '未找到(只用库旁与进程环境的 .env)'}`),
|
|
1484
|
+
data.paths.map((item) => h('div', {
|
|
1485
|
+
key: item.key,
|
|
1486
|
+
style: { display: 'flex', gap: '8px', alignItems: 'baseline', marginTop: '3px' },
|
|
1487
|
+
title: item.target || undefined,
|
|
1488
|
+
},
|
|
1489
|
+
h('span', { style: S.badge(item.ok ? C.ok : C.err) }, item.ok ? '✓' : '✗'),
|
|
1490
|
+
h('span', { style: { ...S.hint, minWidth: '110px' } }, item.label),
|
|
1491
|
+
h('span', { style: { ...S.hint, color: item.ok ? undefined : C.err } }, item.detail),
|
|
1492
|
+
)),
|
|
1493
|
+
),
|
|
1494
|
+
(data?.db && data.db.exists === false) && h('div', { style: { ...S.banner(C.warn), marginTop: '8px' } },
|
|
1495
|
+
'库文件还不存在:写入第一条记忆(或按一次「立即抽取」)就会创建。'),
|
|
1063
1496
|
),
|
|
1064
|
-
h('div', { style: { ...S.hint, marginTop: '8px' } },
|
|
1065
|
-
'插件把同一个库路径传给它启动的每个子进程(MEMVAULT_DB_PATH),所以“面板读的库”与“抽取写的库”不再靠两个默认值巧合相等;如果你在 extract.env 里显式写了 MEMVAULT_DB_PATH,那个会优先生效。'),
|
|
1066
|
-
(data?.db && data.db.exists === false) && h('div', { style: { ...S.banner(C.warn), marginTop: '8px' } },
|
|
1067
|
-
'库文件还不存在:写入第一条记忆(或按一次「立即抽取」)就会创建。如果这不是你期望的位置,去 DSH 插件配置里改 read.dbPath(服务端那半边看 .env 的 MEMVAULT_DB_PATH)。'),
|
|
1068
1497
|
),
|
|
1069
1498
|
|
|
1070
1499
|
h(Card, { title: `核心块(${data?.blocks?.length ?? 0})` },
|
|
1071
1500
|
(data?.blocks?.length ?? 0) === 0
|
|
1072
|
-
? h('div', { style: S.empty }, '没有块 ——
|
|
1501
|
+
? h('div', { style: S.empty }, '没有块 —— 空贡献会被组装器丢弃,轨迹里也就不会出现这一条。用下面的表单建一个。')
|
|
1073
1502
|
: data.blocks.map((b, i) => h(BlockRow, {
|
|
1074
1503
|
key: `${b.scope}/${b.label}/${i}`,
|
|
1075
1504
|
block: b,
|
|
@@ -1080,57 +1509,7 @@ function MemoryPanel() {
|
|
|
1080
1509
|
h(AddBlock, { writable, busy, onWrite: write, defaults }),
|
|
1081
1510
|
),
|
|
1082
1511
|
|
|
1083
|
-
h(
|
|
1084
|
-
h('div', { style: S.grid },
|
|
1085
|
-
h(KV, { k: '状态', v: extract?.enabled === false ? '已关闭(只读)' : '开启' }),
|
|
1086
|
-
h(KV, { k: '窗口策略', v: extract?.window ? `≥${extract.window.everyNTurns} 轮起,静默 ${Math.round((extract.window.idleMs ?? 0) / 1000)} s 或满 ${extract.window.windowTurns} 轮就抽取` : '—' }),
|
|
1087
|
-
h(KV, { k: '嵌入器重算', v: h('button', {
|
|
1088
|
-
type: 'button',
|
|
1089
|
-
onClick: async () => {
|
|
1090
|
-
// Dry run first, always: the report is the point, and it costs one call at most.
|
|
1091
|
-
const res = await fetch(REINDEX_URL, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' })
|
|
1092
|
-
const data = await res.json().catch(() => null)
|
|
1093
|
-
const r = data?.report
|
|
1094
|
-
window.alert(r
|
|
1095
|
-
? `共 ${r.total} 行 · 需重算 ${r.to_recompute} · 未记录 ${r.unrecorded} · 目标 ${r.target}`
|
|
1096
|
-
: `无法取得报告:${data?.error ?? res.status}`)
|
|
1097
|
-
},
|
|
1098
|
-
}, '检查(dry-run)') }),
|
|
1099
|
-
h(KV, { k: '按上面的数字执行', v: h('button', {
|
|
1100
|
-
type: 'button',
|
|
1101
|
-
onClick: async () => {
|
|
1102
|
-
// The cost is stated before it is paid: one embedding call per row that changes.
|
|
1103
|
-
if (!window.confirm('重算会为每一行需要更新的记忆调用一次嵌入模型(记忆原文会离开本机)。先点“检查(dry-run)”看数量。继续执行?')) return
|
|
1104
|
-
const res = await fetch(REINDEX_URL, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ apply: true }) })
|
|
1105
|
-
const data = await res.json().catch(() => null)
|
|
1106
|
-
const r = data?.report
|
|
1107
|
-
window.alert(r
|
|
1108
|
-
? `已重算 ${r.recomputed ?? 0} 行 · 已记录 ${r.recorded ?? 0} 行`
|
|
1109
|
-
: `失败:${data?.error ?? res.status}`)
|
|
1110
|
-
},
|
|
1111
|
-
}, '执行重算') }),
|
|
1112
|
-
h(KV, { k: '窗口丢弃', v: (() => {
|
|
1113
|
-
const d = extract?.window?.pendingDropped
|
|
1114
|
-
if (!d) return '未知'
|
|
1115
|
-
const caps = `上限 ${d.maxPendingSessions} 个 / ${Math.round(d.maxPendingAgeMs / 60000)} 分钟`
|
|
1116
|
-
// A cap that silently eats sessions is worse than no cap: say what was dropped, and
|
|
1117
|
-
// what the limits were when it happened.
|
|
1118
|
-
return (d.overCap || d.expired)
|
|
1119
|
-
? `超上限 ${d.overCap} · 过期 ${d.expired}(${caps})`
|
|
1120
|
-
: `无(${caps})`
|
|
1121
|
-
})() }),
|
|
1122
|
-
h(KV, { k: '待抽取', v: extract?.window?.pending?.length
|
|
1123
|
-
? extract.window.pending.map((p) => `${String(p.sessionId).slice(-8)} · ${p.turns} 轮`).join(' ')
|
|
1124
|
-
: '空' }),
|
|
1125
|
-
h(KV, { k: '接受的结束原因', v: extract?.endReasons?.join(' / ') }),
|
|
1126
|
-
h(KV, { k: '送助手/工具', v: `${extract?.includeAssistant ? '是' : '否'} / ${extract?.includeTools ? '是' : '否'}` }),
|
|
1127
|
-
h(KV, { k: '转写预算', v: extract ? `≤ ${extract.maxInputChars} 字符,< ${extract.minTranscriptChars} 跳过` : '—' }),
|
|
1128
|
-
h(KV, { k: '作用域', v: extract ? `user=${extract.user ?? '—'} agent=${extract.agent ?? '—'}` : '—' }),
|
|
1129
|
-
h(KV, { k: '解释器', v: extract?.pythonPath }),
|
|
1130
|
-
h(KV, { k: '水位会话数', v: extract?.sessions }),
|
|
1131
|
-
h(KV, { k: '状态文件', v: extract?.statePath }),
|
|
1132
|
-
),
|
|
1133
|
-
),
|
|
1512
|
+
h(VectorCard, { url: REINDEX_URL }),
|
|
1134
1513
|
|
|
1135
1514
|
h(Card, { title: `最近抽取(${extract?.diagnostics?.length ?? 0})` },
|
|
1136
1515
|
(extract?.diagnostics?.length ?? 0) === 0
|
|
@@ -1146,7 +1525,7 @@ function MemoryPanel() {
|
|
|
1146
1525
|
h('span', { key: `s${i}`, style: S.mono }, String(d.turns ?? '—')),
|
|
1147
1526
|
h('span', { key: `c${i}`, style: S.mono }, String(d.transcriptChars ?? '—')),
|
|
1148
1527
|
h('span', { key: `o${i}`, style: { ...S.mono, color: outcomeTone(d.outcome) }, title: d.outcome },
|
|
1149
|
-
`${d.recovered === true ? '↻ ' : ''}${d.outcome}`,
|
|
1528
|
+
`${d.recovered === true ? '↻ ' : ''}${d.extractor ? `[${d.extractor}] ` : ''}${d.outcome}`,
|
|
1150
1529
|
...(d.produced?.length
|
|
1151
1530
|
? [' ', h('button', {
|
|
1152
1531
|
key: 'p',
|
|
@@ -1163,10 +1542,157 @@ function MemoryPanel() {
|
|
|
1163
1542
|
)
|
|
1164
1543
|
}
|
|
1165
1544
|
|
|
1545
|
+
// ── memory tool cards (tool.call.toolview) ────────────────────────────────────
|
|
1546
|
+
//
|
|
1547
|
+
// DSH renders each atomic Tool call through the keyed `tool.call.toolview` slot,
|
|
1548
|
+
// keyed by the wire tool name. Without a registration the generic row prints the
|
|
1549
|
+
// tool's result text verbatim — for a memory search that is a JSON wall the user
|
|
1550
|
+
// cannot read at a glance, and for `memory_add` it is one object literal.
|
|
1551
|
+
//
|
|
1552
|
+
// The two views below are deliberately read-only and total: every reader tolerates
|
|
1553
|
+
// missing or malformed data and falls back to the raw text, because a card must
|
|
1554
|
+
// never be the reason a tool call renders wrong. Owner props used here are the
|
|
1555
|
+
// documented ToolCallOwnerProps (`phase`, `block`), whose `block.args` is the lazy
|
|
1556
|
+
// argument view and whose `block.content` carries the result blocks.
|
|
1557
|
+
|
|
1558
|
+
/** One tool argument as text; absent, partial or non-string reads as `''`. */
|
|
1559
|
+
function toolArg(block, key) {
|
|
1560
|
+
const reader = block?.args
|
|
1561
|
+
if (!reader || typeof reader.text !== 'function') return ''
|
|
1562
|
+
try {
|
|
1563
|
+
const value = reader.text(key)
|
|
1564
|
+
return typeof value === 'string' ? value : ''
|
|
1565
|
+
} catch {
|
|
1566
|
+
return ''
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* The tool result as `{ value, raw }`.
|
|
1572
|
+
*
|
|
1573
|
+
* `value` is the parsed object when the result's text blocks join into a JSON
|
|
1574
|
+
* object, and `null` otherwise; `raw` is always the joined text, so a card can
|
|
1575
|
+
* show what actually arrived instead of pretending there was nothing.
|
|
1576
|
+
*/
|
|
1577
|
+
function toolResult(block) {
|
|
1578
|
+
const parts = Array.isArray(block?.content) ? block.content : []
|
|
1579
|
+
const raw = parts
|
|
1580
|
+
.filter((part) => part?.type === 'text' && typeof part.text === 'string')
|
|
1581
|
+
.map((part) => part.text)
|
|
1582
|
+
.join('')
|
|
1583
|
+
if (raw.trim() === '') return { value: null, raw: '' }
|
|
1584
|
+
try {
|
|
1585
|
+
const parsed = JSON.parse(raw)
|
|
1586
|
+
return { value: parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null, raw }
|
|
1587
|
+
} catch {
|
|
1588
|
+
return { value: null, raw }
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
/** A scope label from up to three optional arguments. */
|
|
1593
|
+
function scopeLabel(block) {
|
|
1594
|
+
return [toolArg(block, 'user_id'), toolArg(block, 'agent_id'), toolArg(block, 'run_id')]
|
|
1595
|
+
.filter(Boolean)
|
|
1596
|
+
.join(' · ')
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/** Shared card chrome: a title chip, an optional context line, then the body. */
|
|
1600
|
+
function ToolShell({ title, tone, meta, children }) {
|
|
1601
|
+
return h('div', { style: S.toolCard },
|
|
1602
|
+
h('div', { style: S.toolHead },
|
|
1603
|
+
h('span', { style: S.badge(tone) }, title),
|
|
1604
|
+
meta ? h('span', { style: S.toolMeta }, meta) : null),
|
|
1605
|
+
children)
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
/** Raw fallback, used whenever the structured shape is not there. */
|
|
1609
|
+
function renderRaw(block, raw) {
|
|
1610
|
+
if (block?.isError) {
|
|
1611
|
+
return h('div', { style: { ...S.toolText, color: C.err } }, block.error?.reason ?? block.error?.code ?? '调用失败')
|
|
1612
|
+
}
|
|
1613
|
+
if (raw === '') return h('div', { style: S.hint }, '没有返回内容。')
|
|
1614
|
+
return h('pre', { style: S.toolRaw }, raw)
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
/**
|
|
1618
|
+
* `memory_search` card: the query, whether it was answerable, and the matched
|
|
1619
|
+
* memories instead of their JSON encoding.
|
|
1620
|
+
*/
|
|
1621
|
+
function MemorySearchCard({ phase, block }) {
|
|
1622
|
+
const query = toolArg(block, 'query')
|
|
1623
|
+
const scope = scopeLabel(block)
|
|
1624
|
+
|
|
1625
|
+
if (phase !== 'result') {
|
|
1626
|
+
return h(ToolShell, { title: '记忆检索', tone: C.brand, meta: scope },
|
|
1627
|
+
h('div', { style: S.toolText }, query || '(无查询)'),
|
|
1628
|
+
h('div', { style: { ...S.hint, marginTop: '4px' } }, phase === 'preparing' ? '等待参数…' : '检索中…'))
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
const { value, raw } = toolResult(block)
|
|
1632
|
+
if (block?.isError || value === null) {
|
|
1633
|
+
return h(ToolShell, { title: '记忆检索', tone: block?.isError ? C.err : C.warn, meta: scope },
|
|
1634
|
+
h('div', { style: S.toolText }, query || '(无查询)'),
|
|
1635
|
+
renderRaw(block, raw))
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
const results = Array.isArray(value.results) ? value.results.filter((row) => row && typeof row === 'object') : []
|
|
1639
|
+
const answerable = value.answerable !== false
|
|
1640
|
+
const shown = results.slice(0, 5)
|
|
1641
|
+
const hidden = results.length - shown.length
|
|
1642
|
+
const note = typeof value.note === 'string' ? value.note : ''
|
|
1643
|
+
|
|
1644
|
+
return h(ToolShell, {
|
|
1645
|
+
title: '记忆检索',
|
|
1646
|
+
tone: answerable ? C.brand : C.warn,
|
|
1647
|
+
meta: [scope, `${results.length} 条${answerable ? '' : ' · 不可直接回答'}`].filter(Boolean).join(' · '),
|
|
1648
|
+
},
|
|
1649
|
+
h('div', { style: S.toolText }, query || '(无查询)'),
|
|
1650
|
+
results.length === 0
|
|
1651
|
+
? h('div', { style: { ...S.hint, marginTop: '4px' } }, note || '这个作用域里没有命中。')
|
|
1652
|
+
: shown.map((row, index) => h('div', { key: row.id ?? index, style: S.toolRow },
|
|
1653
|
+
h('span', { style: { ...S.mono, color: C.muted, flex: '0 0 auto' } }, String(row.id ?? '').slice(0, 10)),
|
|
1654
|
+
h('span', { style: S.toolText }, String(row.text ?? '')),
|
|
1655
|
+
typeof row.score === 'number' ? h('span', { style: { ...S.hint, flex: '0 0 auto' } }, row.score.toFixed(3)) : null)),
|
|
1656
|
+
hidden > 0 ? h('div', { style: { ...S.hint, marginTop: '4px' } }, `还有 ${hidden} 条未展开。`) : null,
|
|
1657
|
+
note && results.length > 0 ? h('div', { style: { ...S.hint, marginTop: '4px' } }, note) : null)
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
/** `memory_add` card: the stored text and the id that came back. */
|
|
1661
|
+
function MemoryAddCard({ phase, block }) {
|
|
1662
|
+
const text = toolArg(block, 'text')
|
|
1663
|
+
const type = toolArg(block, 'type') || 'user'
|
|
1664
|
+
const scope = scopeLabel(block)
|
|
1665
|
+
|
|
1666
|
+
if (phase !== 'result') {
|
|
1667
|
+
return h(ToolShell, { title: '记住', tone: C.brand, meta: [type, scope].filter(Boolean).join(' · ') },
|
|
1668
|
+
h('div', { style: S.toolText }, text || '(空)'),
|
|
1669
|
+
h('div', { style: { ...S.hint, marginTop: '4px' } }, phase === 'preparing' ? '等待参数…' : '写入中…'))
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
const { value, raw } = toolResult(block)
|
|
1673
|
+
if (block?.isError || value === null) {
|
|
1674
|
+
return h(ToolShell, { title: '记住', tone: block?.isError ? C.err : C.warn, meta: scope },
|
|
1675
|
+
h('div', { style: S.toolText }, text || '(空)'),
|
|
1676
|
+
renderRaw(block, raw))
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
return h(ToolShell, {
|
|
1680
|
+
title: '记住',
|
|
1681
|
+
tone: C.ok,
|
|
1682
|
+
meta: [type, scope, typeof value.id === 'string' ? value.id.slice(0, 12) : ''].filter(Boolean).join(' · '),
|
|
1683
|
+
},
|
|
1684
|
+
h('div', { style: S.toolText }, String(value.text ?? text ?? '')))
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
/** Wire tool name → card component. Keys must match the registered tool names. */
|
|
1688
|
+
const MEMORY_TOOL_VIEWS = {
|
|
1689
|
+
memory_search: MemorySearchCard,
|
|
1690
|
+
memory_add: MemoryAddCard,
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1166
1693
|
/**
|
|
1167
1694
|
* Client plugin body: register the panel into the conversation view ring and the
|
|
1168
|
-
* Plugins settings section
|
|
1169
|
-
* wrapper, so unloading the plugin removes both.
|
|
1695
|
+
* Plugins settings section, plus one keyed card per memory tool.
|
|
1170
1696
|
* @param ctx - client root context.
|
|
1171
1697
|
*/
|
|
1172
1698
|
function apply(ctx) {
|
|
@@ -1183,9 +1709,20 @@ function apply(ctx) {
|
|
|
1183
1709
|
order: 30,
|
|
1184
1710
|
label: () => 'MemVault 记忆桥',
|
|
1185
1711
|
}, MemoryPanel))
|
|
1712
|
+
|
|
1713
|
+
// Distinct views rather than the shared generic row, because a memory search's
|
|
1714
|
+
// JSON is a wall of braces in the transcript. DSH 0.2.2 dispatches atomic tool
|
|
1715
|
+
// views through this keyed slot by wire name; an unclaimed name keeps the
|
|
1716
|
+
// generic card, so this is additive and cannot affect other tools.
|
|
1717
|
+
for (const [key, component] of Object.entries(MEMORY_TOOL_VIEWS)) {
|
|
1718
|
+
ctx.slots.inject('tool.call.toolview', () => ctx.slots.register({
|
|
1719
|
+
name: 'tool.call.toolview',
|
|
1720
|
+
key,
|
|
1721
|
+
}, component))
|
|
1722
|
+
}
|
|
1186
1723
|
}
|
|
1187
1724
|
|
|
1188
1725
|
module.exports = { name: NAME, inject: ['slots'], apply }
|
|
1189
1726
|
|
|
1190
1727
|
return module.exports; } });
|
|
1191
|
-
//# sourceSHA256=
|
|
1728
|
+
//# sourceSHA256=9e5dd40c233914b7
|