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