dsh-wiki-notes 1.4.2 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (20) hide show
  1. package/README.md +43 -0
  2. package/docs/README.md +1 -1
  3. package/docs/superpowers/plans/2026-11-05-wiki-call-audit.md +553 -0
  4. package/docs/superpowers/specs/2026-11-05-wiki-call-audit-design.md +164 -0
  5. package/docs//345/245/221/347/272/246-stage0/samples/_verify_probe.json +1 -1
  6. package/docs//345/245/221/347/272/246-stage0/samples/{context.cq-shenlun.json → context.sample-cache.json} +80 -80
  7. package/docs//345/245/221/347/272/246-stage0/samples/verify.fabricated-quote.json +3 -3
  8. package/docs//345/245/221/347/272/246-stage0/samples/verify.scope-mismatch.json +3 -3
  9. package/docs//345/245/221/347/272/246-stage0/samples/verify.wrong-five.json +9 -9
  10. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2650-/345/256/241/350/256/241/344/270/216/345/245/221/347/272/246.md +10 -10
  11. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2651-/346/225/260/345/200/274/345/210/244/346/215/256/344/270/216/345/274/225/346/223/216/347/212/266/346/200/201.md +9 -9
  12. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2652-/345/277/253/347/205/247/344/270/216TTL.md +3 -3
  13. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2653-/346/224/257/346/214/201/345/216/237/345/233/240.md +3 -3
  14. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2654-/346/217/222/344/273/266/346/216/245/345/205/245.md +1 -1
  15. package/docs//345/245/221/347/272/246-stage0//351/230/266/346/256/2655-/350/247/202/345/257/237/351/227/255/347/216/257/344/270/216/345/256/236/346/210/230/351/252/214/346/224/266.md +10 -10
  16. package/lib/calls.js +250 -0
  17. package/lib/client.js +350 -11
  18. package/lib/config.js +33 -0
  19. package/lib/index.js +96 -4
  20. package/package.json +6 -4
package/lib/client.js CHANGED
@@ -209,6 +209,13 @@ window.__ModuleLoader__.load({
209
209
  var promptDraft = promptTuple[0]
210
210
  var setPromptDraft = promptTuple[1]
211
211
 
212
+ // 页内视图:概览(路径、体检、提示词)与调用记录。
213
+ // 不做成两个一级入口 —— 它们回答的是同一个问题「这个库现在怎么样」,
214
+ // 拆成两个左侧入口只会让人多点一次,还得判断该进哪个。
215
+ var tabTuple = useState('overview')
216
+ var tab = tabTuple[0]
217
+ var setTab = tabTuple[1]
218
+
212
219
  var apply = useCallback(function (data) {
213
220
  setState(data)
214
221
  setDraft(data && typeof data.root === 'string' ? data.root : '')
@@ -382,20 +389,50 @@ window.__ModuleLoader__.load({
382
389
  // 空状态:一次都没配过库路径(区别于「配了但扫不到」——那是另一条提示)
383
390
  var isUnset = state !== null && (state.source === 'unset' || state.root === '')
384
391
 
385
- return h(
392
+ var head = h(
386
393
  'div',
387
- { className: 'wn-root' },
388
-
394
+ { className: 'wn-head' },
395
+ h('div', { className: 'wn-title' }, '知识库'),
389
396
  h(
390
397
  'div',
391
- { className: 'wn-head' },
392
- h('div', { className: 'wn-title' }, '知识库'),
393
- h(
394
- 'div',
395
- { className: 'wn-sub' },
396
- '模型通过 wiki_recall 工具检索这个库(只读)。这里只改「库在哪」,不改「怎么检索」。',
397
- ),
398
+ { className: 'wn-sub' },
399
+ '模型通过 wiki_recall 工具检索这个库(只读)。这里只改「库在哪」,不改「怎么检索」。',
398
400
  ),
401
+ )
402
+
403
+ var tabs = h(
404
+ 'div',
405
+ { className: 'wn-tabs' },
406
+ h(
407
+ 'button',
408
+ {
409
+ type: 'button',
410
+ className: tab === 'overview' ? 'wn-tab wn-tab-on' : 'wn-tab',
411
+ onClick: function () {
412
+ setTab('overview')
413
+ },
414
+ },
415
+ '概览',
416
+ ),
417
+ h(
418
+ 'button',
419
+ {
420
+ type: 'button',
421
+ className: tab === 'calls' ? 'wn-tab wn-tab-on' : 'wn-tab',
422
+ onClick: function () {
423
+ setTab('calls')
424
+ },
425
+ },
426
+ '调用记录',
427
+ ),
428
+ )
429
+
430
+ var overview = h(
431
+ 'div',
432
+ { className: 'wn-root' },
433
+
434
+ head,
435
+ tabs,
399
436
 
400
437
  state === null
401
438
  ? h(
@@ -737,7 +774,14 @@ window.__ModuleLoader__.load({
737
774
  ),
738
775
 
739
776
  h('div', { className: 'wn-foot' }, '只读:这个页面不会改知识库里的任何文件。'),
740
- ) }
777
+ )
778
+
779
+ // 同一个页面的另一面:谁在什么时候问过这个库、问到没有。
780
+ if (tab === 'calls') {
781
+ return h('div', { className: 'wn-root' }, head, tabs, h(WikiCallsSection, { embedded: true }))
782
+ }
783
+ return overview
784
+ }
741
785
 
742
786
  /* ------------------------------ 检索面板 ------------------------------ */
743
787
 
@@ -1330,6 +1374,281 @@ window.__ModuleLoader__.load({
1330
1374
  )
1331
1375
  }
1332
1376
 
1377
+ /* -------------------------- 调用审计(第二页签) -------------------------- */
1378
+
1379
+ /** 轮询间隔。2 秒够看清「模型刚刚查了没有」,又不至于把本机 HTTP 当心跳打。 */
1380
+ var CALLS_POLL_MS = 2000
1381
+ var CALLS_LIMIT = 200
1382
+
1383
+ function callsSourceLabel(source) {
1384
+ return source === 'panel' ? '面板' : '模型'
1385
+ }
1386
+
1387
+ /** 把 setState 的结果合并进旧对象:stub 环境里 setter 是空函数,这里也不依赖它。 */
1388
+ function patchState(prev, patch) {
1389
+ var next = {}
1390
+ Object.keys(prev).forEach(function (key) {
1391
+ next[key] = prev[key]
1392
+ })
1393
+ Object.keys(patch).forEach(function (key) {
1394
+ next[key] = patch[key]
1395
+ })
1396
+ return next
1397
+ }
1398
+
1399
+ /**
1400
+ * 「知识库 · 调用」。
1401
+ *
1402
+ * 存在的理由:wiki_observe 只看网关那三次调用,wiki_recall 压根不写观察日志。
1403
+ * 所以「模型到底查没查库」这件事,之前只能去翻会话转录。这个页面就是那本账。
1404
+ */
1405
+ function WikiCallsSection(props) {
1406
+ var stateTuple = useState({ rows: [], totals: null, enabled: true, window: 0, error: '', loading: true })
1407
+ var state = stateTuple[0]
1408
+ var setState = stateTuple[1]
1409
+
1410
+ var filterTuple = useState('all')
1411
+ var filter = filterTuple[0]
1412
+ var setFilter = filterTuple[1]
1413
+
1414
+ var missTuple = useState(false)
1415
+ var onlyMiss = missTuple[0]
1416
+ var setOnlyMiss = missTuple[1]
1417
+
1418
+ // 清空要按两次:第一次只把按钮变成「确认清空?」,4 秒内不确认就复原。
1419
+ // 不用 window.confirm —— 系统对话框在自己页面里显得突兀,也不好统一风格。
1420
+ var confirmClearTuple = useState(false)
1421
+ var confirmClear = confirmClearTuple[0]
1422
+ var setConfirmClear = confirmClearTuple[1]
1423
+ var confirmTimerRef = useRef(null)
1424
+
1425
+ // 切走页面后别再 setState:轮询还在跑,往已卸载的组件写 state 只会刷警告。
1426
+ var aliveRef = useRef(true)
1427
+ // 同一时刻只发一个写请求,别让轮询和手点清空撞在一起。
1428
+ var busyRef = useRef('')
1429
+
1430
+ var load = useCallback(
1431
+ function () {
1432
+ var path = '/calls?limit=' + CALLS_LIMIT
1433
+ if (filter !== 'all') path = path + '&source=' + filter
1434
+ if (onlyMiss) path = path + '&only=miss'
1435
+ return apiGet(path).then(
1436
+ function (data) {
1437
+ if (aliveRef.current !== true) return
1438
+ setState({
1439
+ rows: Array.isArray(data && data.rows) ? data.rows : [],
1440
+ totals: (data && data.totals) || null,
1441
+ enabled: !(data && data.enabled === false),
1442
+ window: data && typeof data.window === 'number' ? data.window : 0,
1443
+ error: '',
1444
+ loading: false,
1445
+ })
1446
+ },
1447
+ function (error) {
1448
+ if (aliveRef.current !== true) return
1449
+ setState(function (prev) {
1450
+ return patchState(prev, { loading: false, error: String((error && error.message) || error) })
1451
+ })
1452
+ },
1453
+ )
1454
+ },
1455
+ [filter, onlyMiss],
1456
+ )
1457
+
1458
+ useEffect(
1459
+ function () {
1460
+ aliveRef.current = true
1461
+ load()
1462
+ var timer = setInterval(function () {
1463
+ // 页面在后台时跳过:没人看,白花一次请求。
1464
+ if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return
1465
+ load()
1466
+ }, CALLS_POLL_MS)
1467
+ return function () {
1468
+ aliveRef.current = false
1469
+ clearInterval(timer)
1470
+ if (confirmTimerRef.current !== null) {
1471
+ clearTimeout(confirmTimerRef.current)
1472
+ confirmTimerRef.current = null
1473
+ }
1474
+ }
1475
+ },
1476
+ [load],
1477
+ )
1478
+
1479
+ function runAction(path, body, label) {
1480
+ if (busyRef.current !== '') return
1481
+ busyRef.current = label
1482
+ apiPost(path, body || {}).then(
1483
+ function () {
1484
+ busyRef.current = ''
1485
+ load()
1486
+ },
1487
+ function (error) {
1488
+ busyRef.current = ''
1489
+ setState(function (prev) {
1490
+ return patchState(prev, { error: String((error && error.message) || error) })
1491
+ })
1492
+ },
1493
+ )
1494
+ }
1495
+
1496
+ function clearCalls() {
1497
+ if (confirmClear !== true) {
1498
+ setConfirmClear(true)
1499
+ if (confirmTimerRef.current !== null) clearTimeout(confirmTimerRef.current)
1500
+ confirmTimerRef.current = setTimeout(function () {
1501
+ confirmTimerRef.current = null
1502
+ setConfirmClear(false)
1503
+ }, 4000)
1504
+ return
1505
+ }
1506
+ if (confirmTimerRef.current !== null) {
1507
+ clearTimeout(confirmTimerRef.current)
1508
+ confirmTimerRef.current = null
1509
+ }
1510
+ setConfirmClear(false)
1511
+ runAction('/calls/clear', {}, 'clear')
1512
+ }
1513
+
1514
+ function toggleEnabled() {
1515
+ runAction('/calls/config', { enabled: state.enabled !== true }, 'toggle')
1516
+ }
1517
+
1518
+ var totals = state.totals || {}
1519
+ var hitRate = typeof totals.hitRate === 'number' ? Math.round(totals.hitRate * 100) + '%' : '—'
1520
+
1521
+ var filterButtons = [
1522
+ ['all', '全部'],
1523
+ ['model', '模型'],
1524
+ ['panel', '面板'],
1525
+ ].map(function (item) {
1526
+ return h(
1527
+ 'button',
1528
+ {
1529
+ key: item[0],
1530
+ type: 'button',
1531
+ className: filter === item[0] ? 'wn-btn wn-btn-on' : 'wn-btn',
1532
+ onClick: function () {
1533
+ setFilter(item[0])
1534
+ },
1535
+ },
1536
+ item[1],
1537
+ )
1538
+ })
1539
+
1540
+ var rows = (state.rows || []).map(function (row, index) {
1541
+ var slow = typeof row.ms === 'number' && row.ms > 1000
1542
+ return h(
1543
+ 'tr',
1544
+ { key: String(row.ts) + '-' + String(index) },
1545
+ h('td', { className: 'wn-calls-time' }, typeof row.ts === 'string' ? row.ts.slice(11, 16) : '—'),
1546
+ h('td', null, callsSourceLabel(row.source)),
1547
+ h('td', null, row.tool === '' || row.tool === undefined ? '—' : row.tool),
1548
+ h(
1549
+ 'td',
1550
+ { className: 'wn-calls-query', title: typeof row.query === 'string' ? row.query : '' },
1551
+ row.query === '' || row.query === undefined ? '(无查询词)' : row.query,
1552
+ ),
1553
+ h('td', { className: 'wn-calls-hits' }, row.hits === null || row.hits === undefined ? '—' : String(row.hits)),
1554
+ h('td', { className: slow ? 'wn-calls-ms wn-slow' : 'wn-calls-ms' }, typeof row.ms === 'number' ? row.ms + 'ms' : '—'),
1555
+ )
1556
+ })
1557
+
1558
+ var body =
1559
+ rows.length === 0
1560
+ ? h('div', { className: 'wn-calls-empty' }, '还没有调用记录。让模型查一次库,或在「概览」页的搜索框里查一次 —— 两条路都会记进同一个账本。')
1561
+ : h(
1562
+ 'table',
1563
+ { className: 'wn-calls-table' },
1564
+ h(
1565
+ 'thead',
1566
+ null,
1567
+ h(
1568
+ 'tr',
1569
+ null,
1570
+ h('th', null, '时间'),
1571
+ h('th', null, '来源'),
1572
+ h('th', null, '工具'),
1573
+ h('th', null, '问题'),
1574
+ h('th', null, '命中'),
1575
+ h('th', null, '耗时'),
1576
+ ),
1577
+ ),
1578
+ h('tbody', null, rows),
1579
+ )
1580
+
1581
+ var embedded = props !== null && props !== undefined && props.embedded === true
1582
+
1583
+ return h(
1584
+ 'div',
1585
+ // 嵌进「知识库」页时不再套一层 .wn-root —— 那会把间距和 padding 叠加两遍。
1586
+ { className: embedded === true ? 'wn-calls' : 'wn-root wn-calls' },
1587
+ // 标题与页签由外层给,这里不重复一个头。
1588
+ embedded === true
1589
+ ? null
1590
+ : h(
1591
+ 'div',
1592
+ { className: 'wn-head' },
1593
+ h('div', { className: 'wn-title' }, '调用记录'),
1594
+ h(
1595
+ 'div',
1596
+ { className: 'wn-sub' },
1597
+ '模型和面板每一次查库都记在这儿。这是唯一能回答「我这句问话,它到底查了没有」的地方。',
1598
+ ),
1599
+ ),
1600
+ h(
1601
+ 'div',
1602
+ { className: 'wn-calls-stats' },
1603
+ h('div', { className: 'wn-calls-stat' }, h('b', null, String(totals.calls === undefined ? 0 : totals.calls)), h('span', null, '今日调用')),
1604
+ h('div', { className: 'wn-calls-stat' }, h('b', null, hitRate), h('span', null, '命中率')),
1605
+ h('div', { className: 'wn-calls-stat' }, h('b', null, String(totals.misses === undefined ? 0 : totals.misses)), h('span', null, '零命中')),
1606
+ ),
1607
+ h(
1608
+ 'div',
1609
+ { className: 'wn-calls-note' },
1610
+ '统计基于最近 ' + String(state.window === undefined ? 0 : state.window) + ' 条记录;命中率分母不含 verify / observe。',
1611
+ ),
1612
+ h(
1613
+ 'div',
1614
+ { className: 'wn-calls-bar' },
1615
+ filterButtons,
1616
+ h(
1617
+ 'label',
1618
+ { className: 'wn-calls-check' },
1619
+ h('input', {
1620
+ type: 'checkbox',
1621
+ checked: onlyMiss === true,
1622
+ onChange: function () {
1623
+ setOnlyMiss(onlyMiss !== true)
1624
+ },
1625
+ }),
1626
+ '只看零命中',
1627
+ ),
1628
+ h(
1629
+ 'label',
1630
+ { className: 'wn-calls-check' },
1631
+ h('input', {
1632
+ type: 'checkbox',
1633
+ checked: state.enabled === true,
1634
+ onChange: toggleEnabled,
1635
+ }),
1636
+ '记录开关',
1637
+ ),
1638
+ h(
1639
+ 'button',
1640
+ { type: 'button', className: confirmClear === true ? 'wn-btn wn-btn-danger' : 'wn-btn', onClick: clearCalls },
1641
+ confirmClear === true ? '确认清空?' : '清空',
1642
+ ),
1643
+ ),
1644
+ state.error === '' || state.error === undefined
1645
+ ? null
1646
+ : h('div', { className: 'wn-notice wn-notice-bad' }, h('span', { className: 'wn-notice-mark' }, '!'), h('span', { className: 'wn-notice-text' }, state.error)),
1647
+ body,
1648
+ h('div', { className: 'wn-foot' }, '账本只记这 8 个字段:时间、来源、工具、模式、问题、命中、耗时、成败。页面路径与正文一律不进。'),
1649
+ )
1650
+ }
1651
+
1333
1652
  /* ------------------------------ 样式 ------------------------------ */
1334
1653
 
1335
1654
  var CSS = [
@@ -1496,6 +1815,26 @@ window.__ModuleLoader__.load({
1496
1815
  '.wn-notice-mark{flex:0 0 auto;font-weight:700;opacity:.85;}',
1497
1816
  '.wn-notice-text{flex:1 1 auto;}',
1498
1817
  '.wn-foot{font-size:11.5px;color:var(--wn-muted);border-top:1px solid var(--wn-line-soft);padding-top:10px;}',
1818
+ '.wn-tabs{display:flex;gap:4px;border-bottom:1px solid var(--wn-line);padding-bottom:0;}',
1819
+ '.wn-tab{appearance:none;border:0;background:transparent;padding:7px 12px;font-size:12.5px;color:var(--wn-muted);cursor:pointer;border-bottom:2px solid transparent;margin-bottom:-1px;}',
1820
+ '.wn-tab:hover{color:var(--wn-fg);}',
1821
+ '.wn-tab-on{color:var(--wn-fg);font-weight:600;border-bottom-color:var(--wn-brand);}',
1822
+ '.wn-calls-stats{display:flex;gap:10px;flex-wrap:wrap;}',
1823
+ '.wn-calls-stat{flex:1 1 120px;display:flex;flex-direction:column;gap:2px;padding:10px 12px;background:var(--wn-card);border:1px solid var(--wn-line-soft);border-radius:10px;}',
1824
+ '.wn-calls-stat b{font-size:20px;line-height:1.1;}',
1825
+ '.wn-calls-stat span{font-size:11.5px;color:var(--wn-muted);}',
1826
+ '.wn-calls-note{font-size:11.5px;color:var(--wn-faint);}',
1827
+ '.wn-calls-bar{display:flex;gap:8px;align-items:center;flex-wrap:wrap;}',
1828
+ '.wn-calls-check{display:inline-flex;gap:5px;align-items:center;font-size:12px;color:var(--wn-muted);}',
1829
+ '.wn-calls-table{width:100%;border-collapse:collapse;font-size:12px;}',
1830
+ '.wn-calls-table th{text-align:left;font-weight:600;color:var(--wn-muted);border-bottom:1px solid var(--wn-line);padding:6px 8px;}',
1831
+ '.wn-calls-table td{border-bottom:1px solid var(--wn-line-soft);padding:6px 8px;vertical-align:top;}',
1832
+ '.wn-calls-time{color:var(--wn-muted);white-space:nowrap;}',
1833
+ '.wn-calls-query{max-width:320px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}',
1834
+ '.wn-calls-hits{text-align:right;font-variant-numeric:tabular-nums;}',
1835
+ '.wn-calls-ms{text-align:right;font-variant-numeric:tabular-nums;color:var(--wn-muted);}',
1836
+ '.wn-calls-ms.wn-slow{color:#b4552d;}',
1837
+ '.wn-calls-empty{padding:18px 12px;text-align:center;color:var(--wn-muted);font-size:12.5px;background:var(--wn-card);border:1px dashed var(--wn-line);border-radius:10px;}',
1499
1838
  '}',
1500
1839
  ].join('')
1501
1840
 
package/lib/config.js CHANGED
@@ -161,6 +161,39 @@ export function writeTopicConfig(pluginRoot, next) {
161
161
  return { file: writeRecord(pluginRoot, record), config: clean }
162
162
  }
163
163
 
164
+ /* --------------------- 调用记录:开关与账本位置 --------------------- */
165
+
166
+ /**
167
+ * 调用记录配置。
168
+ *
169
+ * enabled 只认显式 false —— 这个字段的语义是「要不要记账」,
170
+ * 一个拼错的字符串不该让审计悄悄停工。要关它的唯一写法是传 false。
171
+ * log 空串表示「用插件根目录下的 calls.jsonl」。
172
+ */
173
+ export const DEFAULT_CALLS_CONFIG = { enabled: true, log: '' }
174
+
175
+ /** 把任意输入规整成调用记录配置。不是对象就返回 null。 */
176
+ export function normalizeCallsConfig(raw) {
177
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return null
178
+ return {
179
+ enabled: raw.enabled !== false,
180
+ log: typeof raw.log === 'string' ? raw.log.trim() : '',
181
+ }
182
+ }
183
+
184
+ /** 读调用记录配置。缺字段按默认补齐。 */
185
+ export function readCallsConfig(pluginRoot) {
186
+ return normalizeCallsConfig(readRecord(pluginRoot).calls) ?? { ...DEFAULT_CALLS_CONFIG }
187
+ }
188
+
189
+ /** 写调用记录配置。整块替换 `calls` 字段,其余字段原样保留。 */
190
+ export function writeCallsConfig(pluginRoot, next) {
191
+ const record = readRecord(pluginRoot)
192
+ const clean = normalizeCallsConfig(next) ?? { ...DEFAULT_CALLS_CONFIG }
193
+ record.calls = clean
194
+ return { file: writeRecord(pluginRoot, record), config: clean }
195
+ }
196
+
164
197
  /* --------------------- 提示词那一行:可关、可改、可加 --------------------- */
165
198
 
166
199
  /** 提示词配置:默认开着(自动主题词),可整段换成自己的,也可在末尾追加一句。 */
package/lib/index.js CHANGED
@@ -19,7 +19,8 @@ import { mkdirSync, readFileSync, statSync, watch, writeFileSync } from 'node:fs
19
19
  import { fileURLToPath } from 'node:url'
20
20
 
21
21
  import { asList, loadCorpus } from './corpus.js'
22
- import { mergePromptConfig, mergeTopicConfig, normalizeTopicConfig, readBridgeConfig, readTopicConfig, writeTopicConfig } from './config.js'
22
+ import { createCallLog } from './calls.js'
23
+ import { mergePromptConfig, mergeTopicConfig, normalizeTopicConfig, readBridgeConfig, readCallsConfig, readTopicConfig, writeCallsConfig, writeTopicConfig } from './config.js'
23
24
  import { MODES, freshnessOf, lint, run } from './engine.js'
24
25
  import { configPath, DEFAULT_WIKI_ROOT, NOT_CONFIGURED_HINT, readConfigFile, normalizePromptConfig, readPromptConfig, resolveWikiRootInfo, writeConfigFile, writePromptConfig } from './config.js'
25
26
  import { errorEnvelope, runBridge } from './bridge.js'
@@ -262,6 +263,30 @@ export function apply(ctx, config = {}) {
262
263
  let promptCfg = normalizePromptConfig(config?.prompt) ?? readPromptConfig(pluginRoot)
263
264
  let topicCfg = normalizeTopicConfig(config?.topics) ?? readTopicConfig(pluginRoot)
264
265
 
266
+ /**
267
+ * 调用账本。
268
+ *
269
+ * 位置优先级:DSH_WIKI_CALLS_LOG(测试与临时排查)> 配置里的 calls.log > 插件目录旁的
270
+ * calls.jsonl。绝不写进知识库本体 —— 库是用户的,插件只能在库外面留账。
271
+ */
272
+ let callsCfg = readCallsConfig(pluginRoot)
273
+ const calls = createCallLog({
274
+ file: process.env.DSH_WIKI_CALLS_LOG || callsCfg.log || path.join(pluginRoot, 'calls.jsonl'),
275
+ enabled: callsCfg.enabled,
276
+ })
277
+
278
+ /**
279
+ * 记一条调用。旁路:记账失败绝不能变成工具失败 ——
280
+ * 审计的价值远小于「查库还能用」,坏了就让它坏在暗处。
281
+ */
282
+ function recordCall(entry) {
283
+ try {
284
+ calls.record(entry)
285
+ } catch {
286
+ // 吞掉:账本写不进去不该影响检索
287
+ }
288
+ }
289
+
265
290
  let cache = null
266
291
  let cachedAt = 0
267
292
  let inflight = null
@@ -610,6 +635,15 @@ export function apply(ctx, config = {}) {
610
635
  limit: clampLimit(parsed.searchParams.get('limit')),
611
636
  explain: true,
612
637
  })
638
+ recordCall({
639
+ source: 'panel',
640
+ tool: 'wiki_recall',
641
+ mode: 'search',
642
+ query,
643
+ hits: Array.isArray(result?.hits) ? result.hits.length : 0,
644
+ ms: Date.now() - started,
645
+ ok: true,
646
+ })
613
647
  return sendJson(res, 200, {
614
648
  ...result,
615
649
  // 面板要的分母:扫了多少页、扫描本身花了多久、这次查询花了多久
@@ -618,6 +652,27 @@ export function apply(ctx, config = {}) {
618
652
  elapsedMs: Date.now() - started,
619
653
  })
620
654
  }
655
+ // 调用审计:账本只读得回最近的调用,够回答「模型到底查没查、查了什么、查到没有」。
656
+ if (route === '/calls' && req.method === 'GET') {
657
+ const parsed = new URL(req.url ?? '/', 'http://127.0.0.1')
658
+ const limit = Number(parsed.searchParams.get('limit'))
659
+ return sendJson(res, 200, calls.read({
660
+ limit: Number.isFinite(limit) ? limit : undefined,
661
+ source: parsed.searchParams.get('source') ?? 'all',
662
+ only: parsed.searchParams.get('only') ?? 'all',
663
+ }))
664
+ }
665
+ if (route === '/calls/config' && req.method === 'POST') {
666
+ const body = await readBody(req)
667
+ if (body === undefined) return sendJson(res, 400, { error: '请求体不是合法 JSON' })
668
+ callsCfg = writeCallsConfig(pluginRoot, { ...callsCfg, ...body }).config
669
+ calls.setEnabled(callsCfg.enabled)
670
+ return sendJson(res, 200, { enabled: callsCfg.enabled })
671
+ }
672
+ if (route === '/calls/clear' && req.method === 'POST') {
673
+ calls.clear()
674
+ return sendJson(res, 200, { ok: true })
675
+ }
621
676
  if (route === '/check' && req.method === 'POST') {
622
677
  const body = await readBody(req)
623
678
  return sendJson(res, 200, await probeRoot(body?.root))
@@ -832,9 +887,22 @@ function renderObserve(args, value) {
832
887
 
833
888
  /** 统一的 Bridge 调用包装:配置从插件配置读,错就错在明处。 */
834
889
  async function callBridge(command, payload) {
890
+ const started = Date.now()
835
891
  const cfg = readBridgeConfig(pluginRoot)
836
892
  const rootInfo = resolveWikiRootInfo(pluginRoot, root)
893
+ /** 网关这条路上「一次调用」的记账:三个工具都从这里过,记在一处不散。 */
894
+ const note = (hits, ok) => recordCall({
895
+ tool: `wiki_${command}`,
896
+ mode: typeof payload?.engine === 'string' ? payload.engine : '',
897
+ query: typeof payload?.query === 'string' ? payload.query : '',
898
+ // 只有 context 会产生「命中了几页证据」;verify 是核对、observe 是读统计,
899
+ // 记成 0 会把命中率的分母悄悄稀释,所以记 null。
900
+ hits: command === 'context' ? hits : null,
901
+ ms: Date.now() - started,
902
+ ok,
903
+ })
837
904
  if (rootInfo.root === '') {
905
+ note(null, false)
838
906
  return errorEnvelope('E_WIKI_ROOT_MISSING', NOT_CONFIGURED_HINT, 'validate')
839
907
  }
840
908
  const result = await runBridge({
@@ -847,8 +915,13 @@ async function callBridge(command, payload) {
847
915
  env: cfg.observeLog ? { WIKI_OBSERVE_LOG: cfg.observeLog } : {},
848
916
  pluginVersion: PLUGIN_VERSION,
849
917
  })
850
- if (!result.ok) return result.error
851
- return result.data ?? errorEnvelope('E_BRIDGE_CRASH', 'Bridge 返回了空结果', 'bridge')
918
+ if (!result.ok) {
919
+ note(null, false)
920
+ return result.error
921
+ }
922
+ const data = result.data ?? errorEnvelope('E_BRIDGE_CRASH', 'Bridge 返回了空结果', 'bridge')
923
+ note(Array.isArray(data?.evidence) ? data.evidence.length : 0, true)
924
+ return data
852
925
  }
853
926
 
854
927
  const registerGateway = () => {
@@ -999,8 +1072,27 @@ const registerGateway = () => {
999
1072
  if (typeof corpus.drilledFrom === 'string') result.drilledFrom = corpus.drilledFrom
1000
1073
  result.ms = Date.now() - started
1001
1074
  }
1002
- return result ?? { mode: 'error', error: '查询没有返回结果(mode 可能传错了)' }
1075
+ const value = result ?? { mode: 'error', error: '查询没有返回结果(mode 可能传错了)' }
1076
+ recordCall({
1077
+ tool: 'wiki_recall',
1078
+ mode: typeof value?.mode === 'string' ? value.mode : '',
1079
+ query: typeof args?.query === 'string' ? args.query : '',
1080
+ // 只有 search 有「命中几页」这回事。page/list 是「读到了就是读到了」,
1081
+ // 记成 0 会把命中率的分子分母一起带歪,所以记 null 让它不进统计。
1082
+ hits: value?.mode === 'search' && Array.isArray(value.hits) ? value.hits.length : null,
1083
+ ms: Date.now() - started,
1084
+ ok: value?.mode !== 'error',
1085
+ })
1086
+ return value
1003
1087
  } catch (error) {
1088
+ recordCall({
1089
+ tool: 'wiki_recall',
1090
+ mode: typeof args?.mode === 'string' ? args.mode : '',
1091
+ query: typeof args?.query === 'string' ? args.query : '',
1092
+ hits: null,
1093
+ ms: Date.now() - started,
1094
+ ok: false,
1095
+ })
1004
1096
  // 工具层不往上抛:把「库读不出来」变成一条模型看得懂、能转告用户的错误,
1005
1097
  // 而不是一次工具调用失败。库里 0 页和库里没有这个词,是两回事。
1006
1098
  // 这里只剩两类:没配路径(用户该去设置页),以及真正的程序异常(该转告)。
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dsh-wiki-notes",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "type": "module",
5
- "description": "Read-only Markdown knowledge-base retrieval for DeepSeek Harness: one wiki_recall tool (search/page/list/overview/lint/draft) returning the matching section's full text with page-level citations, plus three knowledge-gateway tools (wiki_context / wiki_verify / wiki_observe) that delegate evidence judgement to the local LLM-Wiki Python pipeline. 中文:本地 Markdown 笔记库检索插件,一次调用返回命中小节正文与出处,另带健康检查与回写骨架;新增知识网关三工具(wiki_context / wiki_verify / wiki_observe),证据判定交给本地 Python 管线,插件只负责调用与错误隔离。",
5
+ "description": "Read-only knowledge gateway over a local Markdown vault for DeepSeek Harness — recall, evidence pack, answer verification, call audit | 本地 Markdown 笔记库的只读知识网关:检索、证据包、核验、调用审计",
6
6
  "keywords": [
7
7
  "deepseek",
8
8
  "deepseek-harness",
@@ -12,7 +12,9 @@
12
12
  "notes",
13
13
  "markdown",
14
14
  "retrieval",
15
- "obsidian"
15
+ "obsidian",
16
+ "knowledge-base",
17
+ "llm"
16
18
  ],
17
19
  "main": "lib/index.js",
18
20
  "exports": {
@@ -37,7 +39,7 @@
37
39
  "node": ">=20.13.0"
38
40
  },
39
41
  "scripts": {
40
- "test": "node tests/smoke.mjs && node tests/status.mjs && node tests/params.mjs && node tests/fields.mjs && node tests/robust.mjs && node tests/lint.mjs && node tests/lintdiff.mjs && node tests/config.mjs && node tests/explain.mjs && node tests/cli.mjs && node tests/api.mjs && node tests/help.mjs && node tests/contract.mjs && node tests/topics.mjs && node tests/draft.mjs && node tests/watch.mjs && node tests/client.mjs",
42
+ "test": "node tests/smoke.mjs && node tests/status.mjs && node tests/params.mjs && node tests/fields.mjs && node tests/robust.mjs && node tests/lint.mjs && node tests/lintdiff.mjs && node tests/config.mjs && node tests/explain.mjs && node tests/cli.mjs && node tests/api.mjs && node tests/help.mjs && node tests/contract.mjs && node tests/topics.mjs && node tests/draft.mjs && node tests/calls.mjs && node tests/watch.mjs && node tests/client.mjs",
41
43
  "cli": "node bin/cli.mjs",
42
44
  "probe": "node tools/probe.mjs"
43
45
  },