dsh-wiki-notes 1.6.2 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +188 -0
- package/README.md +102 -6
- package/docs/preview-overview.png +0 -0
- package/docs/preview.png +0 -0
- package/icon.svg +4 -0
- package/lib/calls.js +24 -4
- package/lib/client.js +1366 -11
- package/lib/engine.js +56 -0
- package/lib/index.js +409 -7
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +13 -6
package/lib/engine.js
CHANGED
|
@@ -449,6 +449,58 @@ function coveragePass(corpus, tokens, { typeFilter, tagFilter, statusFilter = nu
|
|
|
449
449
|
return out
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
+
/** 零命中时最多列几页沾边候选。三页够指方向,再多就变成「另一个结果列表」。 */
|
|
453
|
+
const NEAR_MISS_LIMIT = 3
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* 零命中时的「最接近的几页」。
|
|
457
|
+
*
|
|
458
|
+
* 命中为空不等于库里没有:词对不上、中文没分词、过滤条件太窄,三种都会让分数归零,
|
|
459
|
+
* 而它们该采取的动作完全不同(换词 / 拆词 / 放开过滤)。只回一句「没命中」,
|
|
460
|
+
* 模型只能瞎猜;给出沾边候选,它才知道该往哪边走。
|
|
461
|
+
*
|
|
462
|
+
* 两条纪律:
|
|
463
|
+
* 1. 这一栏从不参与 hits 的生成 —— 它只在 hits 为空时才算,不可能挤掉任何真结果。
|
|
464
|
+
* 2. 被过滤掉的候选要标出来(filtered):「我筛掉了它」和「库里没有它」是两件事。
|
|
465
|
+
*/
|
|
466
|
+
export function nearMissOf(corpus, tokens, { typeFilter = null, tagFilter = [], statusFilter = null, limit = NEAR_MISS_LIMIT } = {}) {
|
|
467
|
+
const atoms = coverageAtomsOf(tokens.join(' '))
|
|
468
|
+
const totalWeight = atoms.reduce((sum, item) => sum + item.weight, 0)
|
|
469
|
+
if (atoms.length === 0 || totalWeight === 0) return []
|
|
470
|
+
const tags = Array.isArray(tagFilter) ? tagFilter : []
|
|
471
|
+
const out = []
|
|
472
|
+
for (const page of corpus.pages) {
|
|
473
|
+
if (page.oversized === true) continue
|
|
474
|
+
const body = String(page.body ?? '').toLowerCase()
|
|
475
|
+
const title = String(page.name ?? '').toLowerCase()
|
|
476
|
+
let hitWeight = 0
|
|
477
|
+
const hitAtoms = []
|
|
478
|
+
for (const { atom, weight } of atoms) {
|
|
479
|
+
if (!body.includes(atom) && !title.includes(atom)) continue
|
|
480
|
+
hitWeight += weight
|
|
481
|
+
hitAtoms.push(atom)
|
|
482
|
+
}
|
|
483
|
+
if (hitWeight === 0) continue
|
|
484
|
+
const type = String(page.fields['类型'] ?? '')
|
|
485
|
+
const status = String(page.fields['状态'] ?? '')
|
|
486
|
+
const pageTags = asList(page.fields['标签'])
|
|
487
|
+
out.push({
|
|
488
|
+
name: page.name,
|
|
489
|
+
type,
|
|
490
|
+
file: page.file,
|
|
491
|
+
// 覆盖率按同一套权重算,和覆盖兜底同量级 —— 85% 还没过 45% 线是不可能的,
|
|
492
|
+
// 所以这里出现的一定是「差一点」而不是「本该命中」。
|
|
493
|
+
coverage: Math.round((hitWeight / totalWeight) * 100),
|
|
494
|
+
words: hitAtoms.slice(0, 4),
|
|
495
|
+
filtered: (typeFilter !== null && type !== typeFilter)
|
|
496
|
+
|| (statusFilter !== null && status !== statusFilter)
|
|
497
|
+
|| (tags.length > 0 && !tags.some((tag) => matchesTag(pageTags, tag))),
|
|
498
|
+
})
|
|
499
|
+
}
|
|
500
|
+
out.sort((a, b) => b.coverage - a.coverage || a.name.localeCompare(b.name, 'zh'))
|
|
501
|
+
return out.slice(0, limit)
|
|
502
|
+
}
|
|
503
|
+
|
|
452
504
|
/**
|
|
453
505
|
* mode 2:search
|
|
454
506
|
* 返回 hits(按分数降序),每项带索引摘要、命中小节正文、citation、断链。
|
|
@@ -614,6 +666,10 @@ export function search(corpus, query, options = {}) {
|
|
|
614
666
|
// 不参与命中与排序 —— 它只是元数据线索,不是正文检索结果
|
|
615
667
|
oversizedRelevant: oversized.total,
|
|
616
668
|
oversizedRelevantVia: oversized.via,
|
|
669
|
+
// 零命中才有这一栏:库可能在,只是这次的说法对不上。
|
|
670
|
+
nearMiss: hits.length === 0
|
|
671
|
+
? nearMissOf(corpus, tokens, { typeFilter, tagFilter, statusFilter })
|
|
672
|
+
: [],
|
|
617
673
|
hits,
|
|
618
674
|
note: filterNote
|
|
619
675
|
|| (coverageFallback
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import path from 'node:path'
|
|
18
|
+
import { spawn } from 'node:child_process'
|
|
18
19
|
import { mkdirSync, statSync, watch, writeFileSync } from 'node:fs'
|
|
19
20
|
import { fileURLToPath } from 'node:url'
|
|
20
21
|
|
|
@@ -135,8 +136,28 @@ function blockedOrigin(req) {
|
|
|
135
136
|
}
|
|
136
137
|
}
|
|
137
138
|
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
/**
|
|
140
|
+
* 把文件交给系统默认程序打开(.md 通常是 Obsidian / Typora / VS Code,看用户设的关联)。
|
|
141
|
+
*
|
|
142
|
+
* 为什么自己 spawn 而不走 DSH 的 openWorkspacePath:那条通道只认工作区内的路径,
|
|
143
|
+
* 而知识库按设计就在会话工作区之外(这是本插件存在的前提),送进去只会被拒。
|
|
144
|
+
* 代价是这条路绕过了宿主的 nativeOpen 策略,所以上面那道「必须落在库内」的闸
|
|
145
|
+
* 得自己拧紧 —— 这条路由只该是「看笔记」的一只手。
|
|
146
|
+
*
|
|
147
|
+
* detached + stdio ignore:不要让孩子进程的管道挂住这次 HTTP 请求,
|
|
148
|
+
* 编辑器开着的时候用户还得能继续点别的。失败只当没打开,不让请求 500。
|
|
149
|
+
*/
|
|
150
|
+
function openNative(target) {
|
|
151
|
+
const child =
|
|
152
|
+
process.platform === 'win32'
|
|
153
|
+
? // start 的第一个引号参数是窗口标题,少了它,带空格的路径会被当成标题吃掉。
|
|
154
|
+
spawn('cmd', ['/c', 'start', '', target], { windowsHide: true, stdio: 'ignore', detached: true })
|
|
155
|
+
: spawn(process.platform === 'darwin' ? 'open' : 'xdg-open', [target], { stdio: 'ignore', detached: true })
|
|
156
|
+
child.on('error', () => {})
|
|
157
|
+
child.unref()
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const PARAMETERS = { type: 'object',
|
|
140
161
|
properties: {
|
|
141
162
|
mode: {
|
|
142
163
|
type: 'string',
|
|
@@ -620,6 +641,277 @@ export function apply(ctx, config = {}) {
|
|
|
620
641
|
}
|
|
621
642
|
}
|
|
622
643
|
|
|
644
|
+
/**
|
|
645
|
+
* 链接解析:`[[名]]` → 页名。规则与 corpus.js 造 brokenLinks 时那套一致
|
|
646
|
+
* (整名命中 → 别名小写命中;`index` / `log` 是结构文件)。
|
|
647
|
+
*
|
|
648
|
+
* 之所以提成一组函数给三处用(概览的入链、关系窗的页面关联、红链归因),
|
|
649
|
+
* 是因为「同一件事两套口径」在这个仓库已经咬过一次:入链数 0 却报 0 红链,
|
|
650
|
+
* 两边各自都「算对了」,合起来才看得出矛盾。口径只留一份。
|
|
651
|
+
*/
|
|
652
|
+
const STRUCTURAL_LINKS = new Set(['index', 'log'])
|
|
653
|
+
|
|
654
|
+
function resolveLinkTarget(corpus, link) {
|
|
655
|
+
const byExact = corpus?.byName?.get?.(link)
|
|
656
|
+
if (byExact !== undefined) return byExact.name
|
|
657
|
+
const byAliased = corpus?.byAlias?.get?.(String(link).toLowerCase())
|
|
658
|
+
if (Array.isArray(byAliased) && byAliased.length > 0) return byAliased[0].name
|
|
659
|
+
return ''
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/** 一页的出链(去重、已解析);顺带把解析不到的收集出来当红链。 */
|
|
663
|
+
function outLinksOf(corpus, page) {
|
|
664
|
+
const out = []
|
|
665
|
+
const broken = []
|
|
666
|
+
const seen = new Set()
|
|
667
|
+
for (const link of asList(page?.links)) {
|
|
668
|
+
if (link === page?.name || STRUCTURAL_LINKS.has(String(link).toLowerCase())) continue
|
|
669
|
+
const target = resolveLinkTarget(corpus, link)
|
|
670
|
+
if (target === '' || target === page.name) {
|
|
671
|
+
const name = String(link)
|
|
672
|
+
if (!broken.some((item) => item.name === name)) broken.push({ name })
|
|
673
|
+
continue
|
|
674
|
+
}
|
|
675
|
+
if (seen.has(target)) continue
|
|
676
|
+
seen.add(target)
|
|
677
|
+
const hit = corpus?.byName?.get?.(target)
|
|
678
|
+
out.push({ name: target, file: hit?.file ?? '', type: hit?.fields?.['类型'] ?? '' })
|
|
679
|
+
}
|
|
680
|
+
return { out, broken }
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* 关系窗的「页面关联」:某一页的 出链 / 入链 / 双向链 / 红链。
|
|
685
|
+
*
|
|
686
|
+
* 入链必须逐页扫一遍才算得准 —— 页里没有反向索引。库是几百页的量级,
|
|
687
|
+
* 这个代价远小于「算错一次让人去补一个其实存在的链接」。
|
|
688
|
+
*/
|
|
689
|
+
function linkGraphOf(corpus, name) {
|
|
690
|
+
const pages = Array.isArray(corpus?.pages) ? corpus.pages : []
|
|
691
|
+
const page = corpus?.byName?.get?.(name)
|
|
692
|
+
if (page === undefined) return null
|
|
693
|
+
const { out, broken } = outLinksOf(corpus, page)
|
|
694
|
+
const outNames = new Set(out.map((item) => item.name))
|
|
695
|
+
const inn = []
|
|
696
|
+
for (const other of pages) {
|
|
697
|
+
if (other.name === page.name) continue
|
|
698
|
+
const targets = outLinksOf(corpus, other).out
|
|
699
|
+
if (!targets.some((item) => item.name === page.name)) continue
|
|
700
|
+
inn.push({ name: other.name, file: other.file ?? '', type: other.fields?.['类型'] ?? '' })
|
|
701
|
+
}
|
|
702
|
+
// 双向 = 我链它、它也链我。这一列才是「这两页真的互相当回事」的证据,
|
|
703
|
+
// 单独一列因为单向链接太容易是「索引页顺手带的」。
|
|
704
|
+
const both = inn.filter((item) => outNames.has(item.name))
|
|
705
|
+
return {
|
|
706
|
+
name: page.name,
|
|
707
|
+
file: page.file ?? '',
|
|
708
|
+
type: page.fields?.['类型'] ?? '',
|
|
709
|
+
out,
|
|
710
|
+
inn,
|
|
711
|
+
both,
|
|
712
|
+
broken,
|
|
713
|
+
totals: { out: out.length, inn: inn.length, both: both.length, broken: broken.length },
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** 概览页最多列几个标签 / 几个枢纽页 / 几个断链目标。列表是给人扫的,不是给人找的。 */
|
|
718
|
+
const OVERVIEW_TAGS = 12
|
|
719
|
+
const OVERVIEW_RELATIONS = 5
|
|
720
|
+
/** 每个高频标签带几个「一起出现」的邻居。三行以内扫得完,再多就得开矩阵了。 */
|
|
721
|
+
const TAG_NEIGHBOURS = 3
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* 概览:知识库页「概览」页签要的那组数字。
|
|
725
|
+
*
|
|
726
|
+
* 全部由宿主从**这一份 corpus** 现算,前端不再二次加工。这不是洁癖:
|
|
727
|
+
* 一旦客户端自己数标签或自己算百分比,它和工具输出就会在某次改动后悄悄分家,
|
|
728
|
+
* 而分家那天两边都「看起来正常」—— 谁也证明不了谁错。
|
|
729
|
+
*
|
|
730
|
+
* 两个容易被冒充的字段,单独说清楚:
|
|
731
|
+
* - pages.searchable 是「读进正文的页」(pages − oversized)。超大页只留了空壳,
|
|
732
|
+
* 把它算进「可检索」就等于报了一个查不到的数字。
|
|
733
|
+
* - relations.orphans 是「入链为 0 的页」,不是「没有出链的页」。前者是没人引它,
|
|
734
|
+
* 后者是它没引别人,修的方向完全不同。
|
|
735
|
+
*/
|
|
736
|
+
function overviewOf(corpus) {
|
|
737
|
+
const stats = corpus?.stats ?? {}
|
|
738
|
+
const pages = Array.isArray(corpus?.pages) ? corpus.pages : []
|
|
739
|
+
const brokenLinks = Array.isArray(corpus?.brokenLinks) ? corpus.brokenLinks : []
|
|
740
|
+
|
|
741
|
+
// 标签:按页数计。同一页里重复写同一个标签只算一次(那不算两页)。
|
|
742
|
+
const tagCounts = new Map()
|
|
743
|
+
const tagsPerPage = []
|
|
744
|
+
for (const page of pages) {
|
|
745
|
+
const seen = new Set()
|
|
746
|
+
for (const tag of asList(page?.fields?.['标签'])) {
|
|
747
|
+
const key = String(tag).trim()
|
|
748
|
+
if (key === '' || seen.has(key)) continue
|
|
749
|
+
seen.add(key)
|
|
750
|
+
tagCounts.set(key, (tagCounts.get(key) ?? 0) + 1)
|
|
751
|
+
}
|
|
752
|
+
if (seen.size > 0) tagsPerPage.push(seen)
|
|
753
|
+
}
|
|
754
|
+
const rankedTags = Array.from(tagCounts.entries())
|
|
755
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0], 'zh'))
|
|
756
|
+
.slice(0, OVERVIEW_TAGS)
|
|
757
|
+
|
|
758
|
+
// 共现:这些高频标签各自最爱跟谁一起出现。
|
|
759
|
+
// 用途是「从标签走到相邻主题」—— 给人看的列表不需要全矩阵,每个标签三个邻居够了。
|
|
760
|
+
const coCounts = new Map()
|
|
761
|
+
for (const tag of rankedTags.map(([name]) => name)) coCounts.set(tag, new Map())
|
|
762
|
+
for (const set of tagsPerPage) {
|
|
763
|
+
for (const tag of set) {
|
|
764
|
+
const bucket = coCounts.get(tag)
|
|
765
|
+
if (bucket === undefined) continue
|
|
766
|
+
for (const other of set) {
|
|
767
|
+
if (other === tag) continue
|
|
768
|
+
bucket.set(other, (bucket.get(other) ?? 0) + 1)
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const tags = rankedTags.map(([tag, count]) => {
|
|
773
|
+
const bucket = coCounts.get(tag) ?? new Map()
|
|
774
|
+
const withTags = Array.from(bucket.entries())
|
|
775
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0], 'zh'))
|
|
776
|
+
.slice(0, TAG_NEIGHBOURS)
|
|
777
|
+
.map(([other, times]) => ({ tag: other, count: times }))
|
|
778
|
+
return { tag, count, with: withTags }
|
|
779
|
+
})
|
|
780
|
+
|
|
781
|
+
// 关系:入链数 = 有多少不同页面指向它(同一页重复链同一目标只算一次)。
|
|
782
|
+
// 解析规则走上面那组共享函数,和关系窗、红链归因同一份口径。
|
|
783
|
+
const inlinks = new Map()
|
|
784
|
+
let links = 0
|
|
785
|
+
for (const page of pages) {
|
|
786
|
+
const targets = new Set()
|
|
787
|
+
for (const link of asList(page?.links)) {
|
|
788
|
+
if (link === page.name || STRUCTURAL_LINKS.has(link.toLowerCase())) continue
|
|
789
|
+
const target = resolveLinkTarget(corpus, link)
|
|
790
|
+
// 指向不存在的页不是「关系」,那是内容问题 —— 交给 problems / brokenTargets。
|
|
791
|
+
if (target === '' || target === page.name) continue
|
|
792
|
+
targets.add(target)
|
|
793
|
+
}
|
|
794
|
+
links += targets.size
|
|
795
|
+
for (const target of targets) inlinks.set(target, (inlinks.get(target) ?? 0) + 1)
|
|
796
|
+
}
|
|
797
|
+
// file 一起带出去:概览里点「枢纽页 / 孤岛」要能真的打开那个 .md。
|
|
798
|
+
// 只给页名的话,客户端只能去猜路径 —— 猜错了就是又一个「点了没反应」。
|
|
799
|
+
const named = pages.map((page) => ({
|
|
800
|
+
name: page.name,
|
|
801
|
+
file: page.file,
|
|
802
|
+
inlinks: inlinks.get(page.name) ?? 0,
|
|
803
|
+
}))
|
|
804
|
+
const orphans = named.filter((item) => item.inlinks === 0)
|
|
805
|
+
const hubs = named
|
|
806
|
+
.filter((item) => item.inlinks > 0)
|
|
807
|
+
.sort((a, b) => b.inlinks - a.inlinks || a.name.localeCompare(b.name, 'zh'))
|
|
808
|
+
.slice(0, OVERVIEW_RELATIONS)
|
|
809
|
+
|
|
810
|
+
// 断链目标:同一个不存在的页名被几页引用 —— 它就是「最该先补的那一页」。
|
|
811
|
+
// 只报条数(3 处断链)修起来没方向;报出目标名才有下一步。
|
|
812
|
+
const brokenTargets = new Map()
|
|
813
|
+
for (const item of brokenLinks) {
|
|
814
|
+
for (const missing of asList(item?.missing)) {
|
|
815
|
+
brokenTargets.set(missing, (brokenTargets.get(missing) ?? 0) + 1)
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
const topBroken = Array.from(brokenTargets.entries())
|
|
819
|
+
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0], 'zh'))
|
|
820
|
+
.slice(0, OVERVIEW_RELATIONS)
|
|
821
|
+
.map(([name, count]) => ({ name, count }))
|
|
822
|
+
|
|
823
|
+
const all = Number.isFinite(stats.pages) ? stats.pages : pages.length
|
|
824
|
+
const oversized = Number.isFinite(stats.oversized) ? stats.oversized : 0
|
|
825
|
+
return {
|
|
826
|
+
pages: {
|
|
827
|
+
all,
|
|
828
|
+
searchable: Math.max(0, all - oversized),
|
|
829
|
+
resources: Number.isFinite(stats.resources) ? stats.resources : 0,
|
|
830
|
+
sections: Number.isFinite(stats.sections) ? stats.sections : 0,
|
|
831
|
+
bytes: Number.isFinite(stats.bytes) ? stats.bytes : 0,
|
|
832
|
+
},
|
|
833
|
+
index: {
|
|
834
|
+
scannedAt: typeof stats.scannedAt === 'string' ? stats.scannedAt : '',
|
|
835
|
+
scannedMs: Number.isFinite(stats.scannedMs) ? stats.scannedMs : 0,
|
|
836
|
+
discovered: Number.isFinite(stats.discovered) ? stats.discovered : all,
|
|
837
|
+
skipped: Number.isFinite(stats.skipped) ? stats.skipped : 0,
|
|
838
|
+
},
|
|
839
|
+
problems: {
|
|
840
|
+
brokenPages: Number.isFinite(stats.brokenPages) ? stats.brokenPages : 0,
|
|
841
|
+
brokenTotal: Number.isFinite(stats.brokenTotal) ? stats.brokenTotal : 0,
|
|
842
|
+
unreadable: Number.isFinite(stats.unreadable) ? stats.unreadable : 0,
|
|
843
|
+
oversized,
|
|
844
|
+
duplicates: Array.isArray(stats.duplicateNames) ? stats.duplicateNames.length : 0,
|
|
845
|
+
},
|
|
846
|
+
tags,
|
|
847
|
+
tagTotal: tagCounts.size,
|
|
848
|
+
// 「有标签的页 / 没标签的页」分开报:只报标签分布时,一个 90% 页面没打标签的库
|
|
849
|
+
// 看起来和标签整理得很好的库一样整齐。
|
|
850
|
+
tagPages: {
|
|
851
|
+
tagged: tagsPerPage.length,
|
|
852
|
+
untagged: Math.max(0, pages.length - tagsPerPage.length),
|
|
853
|
+
},
|
|
854
|
+
relations: {
|
|
855
|
+
links,
|
|
856
|
+
orphans: orphans.length,
|
|
857
|
+
hubPages: hubs,
|
|
858
|
+
brokenTargets: topBroken,
|
|
859
|
+
orphanSample: orphans.slice(0, OVERVIEW_RELATIONS).map((item) => ({ name: item.name, file: item.file })),
|
|
860
|
+
},
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// 概览里的「使用情况」:把调用账本按工具和来源分一分。
|
|
865
|
+
// 这里**不新建统计系统** —— 读的就是 calls.jsonl 那本账,
|
|
866
|
+
// 和「调用记录」页签看到的是同一批行,所以两处数字不会打架。
|
|
867
|
+
const USAGE_ROWS = 500
|
|
868
|
+
const USAGE_TOOLS = ['wiki_recall', 'wiki_context', 'wiki_verify', 'wiki_observe']
|
|
869
|
+
|
|
870
|
+
function usageOf() {
|
|
871
|
+
let read = null
|
|
872
|
+
try {
|
|
873
|
+
read = calls.read({ limit: USAGE_ROWS })
|
|
874
|
+
} catch (error) {
|
|
875
|
+
// 账本读不出来只影响这一块,不影响检索。给 null 让界面说「读不到」,
|
|
876
|
+
// 别退回一排 0 —— 0 次调用和「不知道几次」是两句话。
|
|
877
|
+
console.warn('[wiki-notes] 调用账本读不出来:', error?.message ?? error)
|
|
878
|
+
return {
|
|
879
|
+
ok: false, rows: 0, byTool: [], bySource: null,
|
|
880
|
+
enabled: null, hitsKnown: 0, zeroHit: 0, zeroRate: null, lastAt: '', today: null,
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
const rows = Array.isArray(read.rows) ? read.rows : []
|
|
884
|
+
const counted = new Map()
|
|
885
|
+
for (const row of rows) {
|
|
886
|
+
const tool = typeof row.tool === 'string' ? row.tool : ''
|
|
887
|
+
counted.set(tool, (counted.get(tool) ?? 0) + 1)
|
|
888
|
+
}
|
|
889
|
+
const byTool = USAGE_TOOLS.map((tool) => ({ tool, count: counted.get(tool) ?? 0 }))
|
|
890
|
+
// 账本里可能有别的工具名(早期版本写进去的、或人手抄的行):不认识的也要露出来,
|
|
891
|
+
// 否则「按工具分的数」加起来对不上总数,而这个差额永远查不到原因。
|
|
892
|
+
for (const [tool, count] of counted) {
|
|
893
|
+
if (!USAGE_TOOLS.includes(tool)) byTool.push({ tool, count })
|
|
894
|
+
}
|
|
895
|
+
const model = rows.filter((row) => row.source === 'model').length
|
|
896
|
+
const panel = rows.filter((row) => row.source === 'panel').length
|
|
897
|
+
const known = rows.filter((row) => typeof row.hits === 'number')
|
|
898
|
+
const zeroHit = known.filter((row) => row.hits === 0).length
|
|
899
|
+
return {
|
|
900
|
+
ok: true,
|
|
901
|
+
rows: rows.length,
|
|
902
|
+
byTool,
|
|
903
|
+
// 模型调的和你自己在面板里查的必须分开算:混在一起就等于把「我随手试了一下」
|
|
904
|
+
// 记成「模型用知识库的次数」—— 那正是这份记录最容易撒谎的地方。
|
|
905
|
+
bySource: { model, panel, other: rows.length - model - panel },
|
|
906
|
+
enabled: read.enabled !== false,
|
|
907
|
+
hitsKnown: known.length,
|
|
908
|
+
zeroHit,
|
|
909
|
+
zeroRate: known.length === 0 ? null : zeroHit / known.length,
|
|
910
|
+
lastAt: rows.length === 0 ? '' : String(rows[0]?.ts ?? ''),
|
|
911
|
+
today: read.totals ?? null,
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
623
915
|
function baseState() {
|
|
624
916
|
return {
|
|
625
917
|
root,
|
|
@@ -646,9 +938,27 @@ export function apply(ctx, config = {}) {
|
|
|
646
938
|
async function buildState() {
|
|
647
939
|
try {
|
|
648
940
|
const corpus = await getCorpus()
|
|
649
|
-
return {
|
|
941
|
+
return {
|
|
942
|
+
...baseState(),
|
|
943
|
+
ok: true,
|
|
944
|
+
// 三态里最容易做错的是「未配置」:以前它和「扫不出来」共用一条 ok:false,
|
|
945
|
+
// 于是界面只能说「不可用」,用户不知道该去设置页填路径、还是该查目录权限。
|
|
946
|
+
connection: root === '' ? 'unconfigured' : 'connected',
|
|
947
|
+
stats: statsOf(corpus),
|
|
948
|
+
overview: overviewOf(corpus),
|
|
949
|
+
// 与扫描结果分成两个字段:库是库,用得多不多是用得多不多。
|
|
950
|
+
// 合成一个「dashboard 数据」,读的人就分不清哪个数需要重扫才更新。
|
|
951
|
+
usage: usageOf(),
|
|
952
|
+
scale: corpus.scaleLine ?? '',
|
|
953
|
+
}
|
|
650
954
|
} catch (error) {
|
|
651
|
-
return {
|
|
955
|
+
return {
|
|
956
|
+
...baseState(),
|
|
957
|
+
ok: false,
|
|
958
|
+
connection: root === '' ? 'unconfigured' : 'unavailable',
|
|
959
|
+
usage: usageOf(),
|
|
960
|
+
error: String(error?.message ?? error),
|
|
961
|
+
}
|
|
652
962
|
}
|
|
653
963
|
}
|
|
654
964
|
|
|
@@ -728,6 +1038,15 @@ export function apply(ctx, config = {}) {
|
|
|
728
1038
|
hits: Array.isArray(result?.hits) ? result.hits.length : 0,
|
|
729
1039
|
ms: Date.now() - started,
|
|
730
1040
|
ok: true,
|
|
1041
|
+
// 和工具那条路记同一组字段:面板里查的一遍也是「一次调用」,
|
|
1042
|
+
// 少了这五项,账本里就有一半的行说不出为什么命中得勉强。
|
|
1043
|
+
status: typeof result?.status === 'string' ? result.status : '',
|
|
1044
|
+
fallback: result?.coverageFallback === true ? 'coverage' : result?.degraded === true ? 'or' : '',
|
|
1045
|
+
considered: Number.isFinite(result?.considered) ? result.considered : null,
|
|
1046
|
+
warnings: Array.isArray(result?.warnings) ? result.warnings.length : null,
|
|
1047
|
+
topScore: Array.isArray(result?.hits) && result.hits.length > 0 && Number.isFinite(result.hits[0]?.score)
|
|
1048
|
+
? result.hits[0].score
|
|
1049
|
+
: null,
|
|
731
1050
|
})
|
|
732
1051
|
return sendJson(res, 200, {
|
|
733
1052
|
...result,
|
|
@@ -737,6 +1056,53 @@ export function apply(ctx, config = {}) {
|
|
|
737
1056
|
elapsedMs: Date.now() - started,
|
|
738
1057
|
})
|
|
739
1058
|
}
|
|
1059
|
+
// 打开文件本体。「最近访问」里点一行,人要的是那个 .md 在编辑器里真开,
|
|
1060
|
+
// 不是在面板里再读一遍 —— 面板读的是正文,改不了、也接不上笔记软件的上下文。
|
|
1061
|
+
// 路径一律按库根解析并校验落在库内:这条路由要是能开任意路径,
|
|
1062
|
+
// 它就成了「本机任意文件 + 系统关联程序」的执行口,而它只是个看笔记的按钮。
|
|
1063
|
+
if (route === '/open' && req.method === 'POST') {
|
|
1064
|
+
const body = await readBody(req)
|
|
1065
|
+
if (body === undefined) return sendJson(res, 400, { error: '请求体不是合法 JSON' })
|
|
1066
|
+
const typed = typeof body?.path === 'string' ? body.path.trim() : ''
|
|
1067
|
+
if (typed === '') return sendJson(res, 400, { error: '缺少 path 字段(要打开哪个文件)' })
|
|
1068
|
+
const corpus = await getCorpus()
|
|
1069
|
+
const root = path.resolve(corpus.wikiDir ?? '')
|
|
1070
|
+
const target = path.resolve(root, typed)
|
|
1071
|
+
const rel = path.relative(root, target)
|
|
1072
|
+
if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
1073
|
+
return sendJson(res, 400, { error: '只能打开库里的文件:' + path.basename(typed) })
|
|
1074
|
+
}
|
|
1075
|
+
let bytes = 0
|
|
1076
|
+
try {
|
|
1077
|
+
bytes = statSync(target).size
|
|
1078
|
+
} catch {
|
|
1079
|
+
// 笔记被删掉/改名之后,回 200 只会让面板安静地什么都不发生 ——
|
|
1080
|
+
// 那正是「点了没反应」最难查的形态,宁可 404 带一句话。
|
|
1081
|
+
return sendJson(res, 404, { error: '这个文件不在了:' + path.basename(target) })
|
|
1082
|
+
}
|
|
1083
|
+
// 页名只在这里当「回执」用:面板不读它,但打不开的时候人得知道是哪个文件。
|
|
1084
|
+
const name = path.basename(target).replace(/\.md$/i, '')
|
|
1085
|
+
// 测试与 CI 里不能真弹窗:给一条明确的旁路,回的还是同一份结构。
|
|
1086
|
+
if (process.env.DSH_WIKI_OPEN_DRYRUN === '1') {
|
|
1087
|
+
return sendJson(res, 200, { ok: true, via: 'dryrun', name, bytes })
|
|
1088
|
+
}
|
|
1089
|
+
openNative(target)
|
|
1090
|
+
return sendJson(res, 200, { ok: true, via: 'system', name, bytes })
|
|
1091
|
+
}
|
|
1092
|
+
// 关系窗的「页面关联」:一页的出链 / 入链 / 双向链 / 红链。
|
|
1093
|
+
// 入链没有反向索引,只能逐页比 —— 但这件事必须宿主算:客户端手里的只有摘要,
|
|
1094
|
+
// 自己拼一份「谁指向谁」就是第二套口径,迟早和工具输出对不上。
|
|
1095
|
+
if (route === '/links' && req.method === 'GET') {
|
|
1096
|
+
const parsed = new URL(req.url ?? '/', 'http://127.0.0.1')
|
|
1097
|
+
const name = (parsed.searchParams.get('name') ?? '').trim()
|
|
1098
|
+
if (name === '') return sendJson(res, 400, { error: '缺少 name 参数(要看哪一页)' })
|
|
1099
|
+
const corpus = await getCorpus()
|
|
1100
|
+
const graph = linkGraphOf(corpus, name)
|
|
1101
|
+
// 找不到就说找不到。回一个空的四列表会让人以为「这页没链接」,
|
|
1102
|
+
// 而真相是「这页不在库里」—— 两种情况的下一步动作完全不同。
|
|
1103
|
+
if (graph === null) return sendJson(res, 404, { error: '库里没有这一页:' + name })
|
|
1104
|
+
return sendJson(res, 200, graph)
|
|
1105
|
+
}
|
|
740
1106
|
// 调用审计:账本只读得回最近的调用,够回答「模型到底查没查、查了什么、查到没有」。
|
|
741
1107
|
// governance 是同一份插件侧统计,页面与 wiki_observe 看到的是同一个对象形状。
|
|
742
1108
|
if (route === '/calls' && req.method === 'GET') {
|
|
@@ -1130,8 +1496,16 @@ async function callBridge(command, payload) {
|
|
|
1130
1496
|
python: cfg.python || 'python',
|
|
1131
1497
|
toolsRoot: cfg.toolsRoot,
|
|
1132
1498
|
timeoutMs: cfg.timeoutMs,
|
|
1133
|
-
//
|
|
1134
|
-
|
|
1499
|
+
// 观察日志单独落盘:正式使用与测试数据物理隔离,免得修正率被自测记录带歪。
|
|
1500
|
+
// WIKI_PLUGIN_DIR 必须一起给:Python 管线里有一个 `wiki_recall` 引擎是回头调
|
|
1501
|
+
// 本插件的 `node bin/cli.mjs search`(context_builder.py 的 run_plugin),它按
|
|
1502
|
+
// 这个变量定位插件目录;不给就 cwd 落到工具目录、`bin/cli.mjs` 不存在,
|
|
1503
|
+
// node 的错误只进 stderr,stdout 空 → 那边 json.loads('') 抛
|
|
1504
|
+
// 「Expecting value: line 1 column 1 (char 0)」→ 每次 context 都被标成
|
|
1505
|
+
// degraded / degraded_sources:["wiki_recall"],两个引擎等于只跑了一个。
|
|
1506
|
+
env: cfg.observeLog
|
|
1507
|
+
? { WIKI_OBSERVE_LOG: cfg.observeLog, WIKI_PLUGIN_DIR: pluginRoot }
|
|
1508
|
+
: { WIKI_PLUGIN_DIR: pluginRoot },
|
|
1135
1509
|
pluginVersion: PLUGIN_VERSION,
|
|
1136
1510
|
})
|
|
1137
1511
|
if (!result.ok) {
|
|
@@ -1315,15 +1689,32 @@ const registerGateway = () => {
|
|
|
1315
1689
|
result.ms = Date.now() - started
|
|
1316
1690
|
}
|
|
1317
1691
|
const value = result ?? { mode: 'error', error: '查询没有返回结果(mode 可能传错了)' }
|
|
1692
|
+
const searchable = value?.mode === 'search'
|
|
1318
1693
|
recordCall({
|
|
1319
1694
|
tool: 'wiki_recall',
|
|
1320
1695
|
mode: typeof value?.mode === 'string' ? value.mode : '',
|
|
1321
1696
|
query: typeof args?.query === 'string' ? args.query : '',
|
|
1322
1697
|
// 只有 search 有「命中几页」这回事。page/list 是「读到了就是读到了」,
|
|
1323
1698
|
// 记成 0 会把命中率的分子分母一起带歪,所以记 null 让它不进统计。
|
|
1324
|
-
hits:
|
|
1699
|
+
hits: searchable && Array.isArray(value.hits) ? value.hits.length : null,
|
|
1325
1700
|
ms: Date.now() - started,
|
|
1326
1701
|
ok: value?.mode !== 'error',
|
|
1702
|
+
// 诊断三元组:这次结果能不能直接用(status)、用到哪层兜底(fallback)、
|
|
1703
|
+
// 以及最接近的那条得了多少分(topScore)。只记 hits=0 时,
|
|
1704
|
+
// 复盘的人看不出「该换词」和「该放开过滤」的区别。
|
|
1705
|
+
status: typeof value?.status === 'string' ? value.status : '',
|
|
1706
|
+
fallback: !searchable
|
|
1707
|
+
? ''
|
|
1708
|
+
: value?.coverageFallback === true
|
|
1709
|
+
? 'coverage'
|
|
1710
|
+
: value?.degraded === true
|
|
1711
|
+
? 'or'
|
|
1712
|
+
: '',
|
|
1713
|
+
considered: searchable && Number.isFinite(value?.considered) ? value.considered : null,
|
|
1714
|
+
warnings: Array.isArray(value?.warnings) ? value.warnings.length : null,
|
|
1715
|
+
topScore: searchable && Array.isArray(value?.hits) && value.hits.length > 0 && Number.isFinite(value.hits[0]?.score)
|
|
1716
|
+
? value.hits[0].score
|
|
1717
|
+
: null,
|
|
1327
1718
|
})
|
|
1328
1719
|
return value
|
|
1329
1720
|
} catch (error) {
|
|
@@ -1334,6 +1725,9 @@ const registerGateway = () => {
|
|
|
1334
1725
|
hits: null,
|
|
1335
1726
|
ms: Date.now() - started,
|
|
1336
1727
|
ok: false,
|
|
1728
|
+
// 抛出来的这一条也要说得清是哪一类:没配路径 vs 程序异常,
|
|
1729
|
+
// 两者的下一步动作差着一整个设置页。
|
|
1730
|
+
status: root === '' ? 'unconfigured' : 'error',
|
|
1337
1731
|
})
|
|
1338
1732
|
// 工具层不往上抛:把「库读不出来」变成一条模型看得懂、能转告用户的错误,
|
|
1339
1733
|
// 而不是一次工具调用失败。库里 0 页和库里没有这个词,是两回事。
|
|
@@ -1650,6 +2044,14 @@ function renderSearch(value) {
|
|
|
1650
2044
|
// 超大页的正文从来没读过,标题和摘要不沾边也推不出正文不沾边。
|
|
1651
2045
|
out.push(`当前可检索范围内没有命中:已比对 ${value.considered ?? scanned} 页的内容${gap}。`)
|
|
1652
2046
|
out.push('这只说明参与比对的页面里没有,不能反推全库没有。')
|
|
2047
|
+
// 沾边候选:不是「勉强凑几条」,是告诉模型「库里最接近的是哪几页、差在哪」。
|
|
2048
|
+
// 没有它,零命中和「库里根本没有这块」在模型看来长得一模一样。
|
|
2049
|
+
const near = Array.isArray(value.nearMiss) ? value.nearMiss : []
|
|
2050
|
+
if (near.length > 0) {
|
|
2051
|
+
const shown = near.map((item) => `${item.name}(字面沾边 ${item.coverage}%${item.filtered === true ? ',被这次的过滤条件筛掉了' : ''})`)
|
|
2052
|
+
out.push(`最接近的 ${near.length} 页:${shown.join('、')}`)
|
|
2053
|
+
out.push('这些只是字面最沾边,不等于它们回答了问题;可以按它们的措辞换词再搜,或直接读一页看看。')
|
|
2054
|
+
}
|
|
1653
2055
|
out.push('下一步:① 用页面标题或术语原文重试(别用整句问话);② 换同义词 / 英文 / 缩写;③ mode:"list" 看这个库的命名习惯。')
|
|
1654
2056
|
out.push('三条都试过仍然没有,才可以说「笔记里没有这个主题」。')
|
|
1655
2057
|
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Knowledge Base",
|
|
4
|
+
"description": "Turns a local Markdown vault into four read-only tools the model can query. One call returns the full text of the matching section plus its source, so it no longer has to read an index and reopen pages. Three entry points: the book in the left sidebar, Settings → Knowledge Base, and the Knowledge Base tab in the right sidebar. Read-only, no network."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-wiki-notes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
4
4
|
"type": "module",
|
|
5
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": [
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
".": "./lib/index.js",
|
|
22
22
|
"./client": "./lib/client.js",
|
|
23
23
|
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
24
|
-
"./package.json": "./package.json"
|
|
24
|
+
"./package.json": "./package.json",
|
|
25
|
+
"./icon": "./icon.svg",
|
|
26
|
+
"./locale/*.json": "./locale/*.json"
|
|
25
27
|
},
|
|
26
28
|
"bin": {
|
|
27
29
|
"wiki-notes": "bin/cli.mjs"
|
|
@@ -34,15 +36,19 @@
|
|
|
34
36
|
"cordis.patch.yml",
|
|
35
37
|
"README.md",
|
|
36
38
|
"LICENSE",
|
|
37
|
-
"CHANGELOG.md"
|
|
39
|
+
"CHANGELOG.md",
|
|
40
|
+
"locale",
|
|
41
|
+
"icon.svg"
|
|
38
42
|
],
|
|
39
43
|
"engines": {
|
|
40
44
|
"node": ">=20.13.0"
|
|
41
45
|
},
|
|
42
46
|
"scripts": {
|
|
43
|
-
"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/governance.mjs && node tests/inject.mjs && node tests/watch.mjs && node tests/client.mjs && node tests/tier.mjs && node tests/render.mjs && node tests/calls-read.mjs && node tests/store.mjs && node tests/bridge.mjs && node tests/text.mjs && node tests/parse.mjs && node tests/fingerprint.mjs && node tests/bom.mjs",
|
|
47
|
+
"test": "node tests/smoke.mjs && node tests/status.mjs && node tests/overview.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/governance.mjs && node tests/inject.mjs && node tests/watch.mjs && node tests/client.mjs && node tests/tier.mjs && node tests/render.mjs && node tests/nearmiss.mjs && node tests/calls-read.mjs && node tests/store.mjs && node tests/bridge.mjs && node tests/text.mjs && node tests/parse.mjs && node tests/fingerprint.mjs && node tests/bom.mjs",
|
|
44
48
|
"cli": "node bin/cli.mjs",
|
|
45
|
-
"probe": "node tools/probe.mjs"
|
|
49
|
+
"probe": "node tools/probe.mjs",
|
|
50
|
+
"verify:host": "node tools/verify-host.mjs --self",
|
|
51
|
+
"verify:bridge": "node tools/verify-bridge.mjs"
|
|
46
52
|
},
|
|
47
53
|
"peerDependencies": {
|
|
48
54
|
"@deepseek-ai/cordis": "^4.0.1",
|
|
@@ -66,5 +72,6 @@
|
|
|
66
72
|
"url": "https://github.com/xyzOvO/dsh-wiki-notes/issues"
|
|
67
73
|
},
|
|
68
74
|
"author": "xyzOvO",
|
|
69
|
-
"license": "MIT"
|
|
75
|
+
"license": "MIT",
|
|
76
|
+
"icon": "icon.svg"
|
|
70
77
|
}
|