dsh-wiki-notes 1.3.0 → 1.4.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/README.md +104 -24
- package/docs/README.md +70 -0
- package/docs//345/245/221/347/272/246-stage0/knowledge-gateway.v1.schema.json +1180 -0
- package/docs//345/245/221/347/272/246-stage0/samples/_verify_probe.json +46 -0
- package/docs//345/245/221/347/272/246-stage0/samples/context.cq-shenlun.json +612 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.context-corrupt.json +20 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.context-expired.json +20 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.fabricated-quote.json +31 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.right-with-limit.json +14 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.scope-mismatch.json +27 -0
- package/docs//345/245/221/347/272/246-stage0/samples/verify.wrong-five.json +43 -0
- package/docs//345/245/221/347/272/246-stage0//350/247/202/345/257/237/346/234/237/346/226/271/346/241/210.md +81 -0
- 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 +204 -0
- 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 +107 -0
- 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 +84 -0
- 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 +98 -0
- 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 +143 -0
- 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 +109 -0
- package/lib/bridge.js +210 -0
- package/lib/client.js +1 -1
- package/lib/config.js +59 -0
- package/lib/index.js +268 -4
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 为什么只有一个工具:profile 里已经装了 26 个插件,模型每回合都要在工具表里选,
|
|
5
5
|
* 多一个工具就是多一份选择成本。四种查询压在同一个工具里用 mode 区分。
|
|
6
6
|
*
|
|
7
|
-
* 为什么用 node:fs 而不是 ctx.fs
|
|
7
|
+
* 为什么用 node:fs 而不是 ctx.fs:库在笔记仓库(会话工作区之外),抽象 fs 会先被
|
|
8
8
|
* 沙箱策略拦一道;这里只需要「读一个用户明确授权的目录」,用 node:fs 更直接,
|
|
9
9
|
* 而且这一步只在扫描时发生,不涉及任何写操作。
|
|
10
10
|
*
|
|
@@ -19,9 +19,10 @@ 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, readTopicConfig, writeTopicConfig } from './config.js'
|
|
22
|
+
import { mergePromptConfig, mergeTopicConfig, normalizeTopicConfig, readBridgeConfig, readTopicConfig, writeTopicConfig } from './config.js'
|
|
23
23
|
import { MODES, freshnessOf, lint, run } from './engine.js'
|
|
24
24
|
import { configPath, DEFAULT_WIKI_ROOT, NOT_CONFIGURED_HINT, readConfigFile, normalizePromptConfig, readPromptConfig, resolveWikiRootInfo, writeConfigFile, writePromptConfig } from './config.js'
|
|
25
|
+
import { errorEnvelope, runBridge } from './bridge.js'
|
|
25
26
|
|
|
26
27
|
export const name = 'wiki-notes'
|
|
27
28
|
|
|
@@ -668,9 +669,256 @@ export function apply(ctx, config = {}) {
|
|
|
668
669
|
ctx.logger?.warn?.(`[wiki-notes] 主题提示注册失败:${String(error?.message ?? error)}`)
|
|
669
670
|
}
|
|
670
671
|
|
|
672
|
+
/**
|
|
673
|
+
* 知识网关的两个工具:把判断交给 Python 管线。
|
|
674
|
+
*
|
|
675
|
+
* 为什么另开工具而不是塞进 wiki_recall:wiki_recall 是「给我看几条候选」,
|
|
676
|
+
* 而这两个是「给我一份带证据、缺口与约束的上下文」和「拿它核验我的答案」。
|
|
677
|
+
* 混在一起会让模型分不清「我拿到的是线索还是可引用的证据」。
|
|
678
|
+
*/
|
|
679
|
+
|
|
680
|
+
/** 读插件自己的版本号,用于契约里的 plugin_version。读不到就写 unknown。 */
|
|
681
|
+
function readPluginVersion(pluginRoot) {
|
|
682
|
+
try {
|
|
683
|
+
const pkg = JSON.parse(readFileSync(path.join(pluginRoot, 'package.json'), 'utf8'))
|
|
684
|
+
return typeof pkg.version === 'string' ? pkg.version : 'unknown'
|
|
685
|
+
} catch {
|
|
686
|
+
return 'unknown'
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/** 插件版本:契约里要填,读一次就够。 */
|
|
691
|
+
const PLUGIN_VERSION = readPluginVersion(pluginRoot)
|
|
692
|
+
|
|
693
|
+
/** context 结果的一行摘要:先给结论面,细节交给结构化字段。 */
|
|
694
|
+
function renderContext(args, value) {
|
|
695
|
+
if (!value || typeof value !== 'object') return '(没有结果)'
|
|
696
|
+
if (value.kind === 'error') {
|
|
697
|
+
const e = value.error ?? {}
|
|
698
|
+
return `知识网关调用失败:${e.code ?? '?'} — ${e.message ?? ''}`
|
|
699
|
+
}
|
|
700
|
+
const status = value.status ?? {}
|
|
701
|
+
const lines = [
|
|
702
|
+
`问题:${value.query ?? args?.query ?? ''}`,
|
|
703
|
+
`状态:检索 ${status.retrieval ?? '?'} / 支持 ${status.support ?? '?'} / 阶段 ${status.stage ?? '?'}`
|
|
704
|
+
+ (status.degraded ? `(降级:${(status.degraded_sources ?? []).join('、') || '未知引擎'})` : ''),
|
|
705
|
+
]
|
|
706
|
+
|
|
707
|
+
const reason = value.support_reason
|
|
708
|
+
if (reason?.primary_label) lines.push(`依据:${reason.primary_label}`)
|
|
709
|
+
|
|
710
|
+
const evidence = value.evidence ?? []
|
|
711
|
+
if (evidence.length > 0) {
|
|
712
|
+
lines.push(`证据页 ${evidence.length} 张:`)
|
|
713
|
+
for (const page of evidence.slice(0, 6)) {
|
|
714
|
+
const supports = page.supports ?? []
|
|
715
|
+
const grade = page.evidence_grade ?? '?'
|
|
716
|
+
lines.push(` · [${grade}] ${page.title ?? page.page ?? ''}(${page.page ?? ''})`
|
|
717
|
+
+ (supports.length ? `,支撑 ${supports.map((s) => s.slot).join('/')}` : ''))
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
lines.push('证据页:0 张')
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const missing = value.missing ?? []
|
|
724
|
+
if (missing.length > 0) {
|
|
725
|
+
lines.push(`缺口 ${missing.length} 项:${missing.map((m) => `${m.slot}(${m.reason})`).join('、')}`)
|
|
726
|
+
}
|
|
727
|
+
const conflicts = value.conflicts ?? []
|
|
728
|
+
if (conflicts.length > 0) lines.push(`冲突候选 ${conflicts.length} 项(未裁决,别当成已确认的结论)`)
|
|
729
|
+
|
|
730
|
+
if (value.answer?.contract_text) lines.push('', value.answer.contract_text.trim())
|
|
731
|
+
if (value.context_id) lines.push('', `context_id:${value.context_id}(可交给 wiki_verify 核验回答)`)
|
|
732
|
+
return lines.join('\n')
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** verify 结果的一行摘要:违规逐条列出,别只给个数。 */
|
|
736
|
+
function renderVerify(args, value) {
|
|
737
|
+
if (!value || typeof value !== 'object') return '(没有结果)'
|
|
738
|
+
if (value.kind === 'error') {
|
|
739
|
+
const e = value.error ?? {}
|
|
740
|
+
return `核验没跑成:${e.code ?? '?'} — ${e.message ?? ''}`
|
|
741
|
+
}
|
|
742
|
+
const lines = [`核验状态:${value.status ?? '?'}`]
|
|
743
|
+
const violations = value.violations ?? []
|
|
744
|
+
if (violations.length === 0) lines.push('没有发现违规。')
|
|
745
|
+
else {
|
|
746
|
+
lines.push(`违规 ${violations.length} 条:`)
|
|
747
|
+
for (const v of violations) {
|
|
748
|
+
lines.push(` · ${v.type ?? v.rule ?? '?'}:${v.claim ?? ''} — ${v.reason ?? ''}`.trim())
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
const notices = value.notices ?? []
|
|
752
|
+
if (notices.length > 0) {
|
|
753
|
+
lines.push(`提示 ${notices.length} 条:${notices.map((n) => n.rule ?? '').join('、')}`)
|
|
754
|
+
}
|
|
755
|
+
return lines.join('\n')
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/** observe 统计摘要:只报分布,不下结论,也不参与回答。 */
|
|
759
|
+
function renderObserve(args, value) {
|
|
760
|
+
if (!value || typeof value !== 'object') return '(没有结果)'
|
|
761
|
+
if (value.kind === 'error') {
|
|
762
|
+
const e = value.error ?? {}
|
|
763
|
+
return `观察统计没跑成:${e.code ?? '?'} — ${e.message ?? ''}`
|
|
764
|
+
}
|
|
765
|
+
const totals = value.totals ?? {}
|
|
766
|
+
const window = value.window ?? {}
|
|
767
|
+
const dist = (obj) => {
|
|
768
|
+
const pairs = Object.entries(obj ?? {})
|
|
769
|
+
return pairs.length === 0 ? '—' : pairs.map(([k, v]) => `${k}×${v}`).join('、')
|
|
770
|
+
}
|
|
771
|
+
const lines = [
|
|
772
|
+
`观察窗口:最近 ${window.days ?? '?'} 天(context ${totals.contexts ?? 0} 次 / verify ${totals.verifies ?? 0} 次)`,
|
|
773
|
+
`支持状态:${dist(value.support_distribution)}`,
|
|
774
|
+
`检索状态:${dist(value.retrieval_distribution)}`,
|
|
775
|
+
`违规类型:${dist(value.violation_types)}`,
|
|
776
|
+
]
|
|
777
|
+
// 修正情况:这一块专门看「发现违规之后改没改」,比违规总数更能反映核验有没有用
|
|
778
|
+
const rev = value.revision
|
|
779
|
+
if (rev && typeof rev === 'object') {
|
|
780
|
+
const rate = rev.correction_rate
|
|
781
|
+
const rateText = typeof rate === 'number' ? `${(rate * 100).toFixed(0)}%` : '暂无可算样本'
|
|
782
|
+
lines.push(
|
|
783
|
+
`修正情况:核验过的 context ${rev.contexts_verified ?? 0} 个,`
|
|
784
|
+
+ `已改正 ${rev.corrected ?? 0} / 仍未改正 ${rev.unresolved ?? 0},修正率 ${rateText}`,
|
|
785
|
+
)
|
|
786
|
+
const seqs = Array.isArray(rev.sequences) ? rev.sequences.slice(0, 3) : []
|
|
787
|
+
for (const s of seqs) {
|
|
788
|
+
const mark = s.corrected ? '已改正' : (s.unresolved ? '仍未改正' : '单次通过')
|
|
789
|
+
const types = Array.isArray(s.types_seen) && s.types_seen.length ? `(${s.types_seen.join('、')})` : ''
|
|
790
|
+
lines.push(` · ${s.query ?? '(无问题)'} — ${s.attempts ?? 0} 次核验 / ${mark}${types}`)
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (totals.legacy) lines.push(`另有 ${totals.legacy} 条早期记录(无事件字段,未计入统计)`)
|
|
794
|
+
return lines.join('\n')
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** 统一的 Bridge 调用包装:配置从插件配置读,错就错在明处。 */
|
|
798
|
+
async function callBridge(command, payload) {
|
|
799
|
+
const cfg = readBridgeConfig(pluginRoot)
|
|
800
|
+
const rootInfo = resolveWikiRootInfo(pluginRoot, root)
|
|
801
|
+
if (rootInfo.root === '') {
|
|
802
|
+
return errorEnvelope('E_WIKI_ROOT_MISSING', NOT_CONFIGURED_HINT, 'validate')
|
|
803
|
+
}
|
|
804
|
+
const result = await runBridge({
|
|
805
|
+
command,
|
|
806
|
+
payload: { ...payload, root: rootInfo.root },
|
|
807
|
+
python: cfg.python || 'python',
|
|
808
|
+
toolsRoot: cfg.toolsRoot,
|
|
809
|
+
timeoutMs: cfg.timeoutMs,
|
|
810
|
+
// 观察日志单独落盘:正式使用与测试数据物理隔离,免得修正率被自测记录带歪
|
|
811
|
+
env: cfg.observeLog ? { WIKI_OBSERVE_LOG: cfg.observeLog } : {},
|
|
812
|
+
pluginVersion: PLUGIN_VERSION,
|
|
813
|
+
})
|
|
814
|
+
if (!result.ok) return result.error
|
|
815
|
+
return result.data ?? errorEnvelope('E_BRIDGE_CRASH', 'Bridge 返回了空结果', 'bridge')
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
const registerGateway = () => {
|
|
819
|
+
const disposers = []
|
|
820
|
+
|
|
821
|
+
disposers.push(ctx.tools.register({
|
|
822
|
+
name: 'wiki_context',
|
|
823
|
+
description:
|
|
824
|
+
'向本地知识库要一份「带证据的上下文」:检索、抽证据、标缺口、给回答约束,一次完成。'
|
|
825
|
+
+ '要引用笔记原文回答、或需要知道「有没有依据 / 依据够不够 / 哪里是缺口」时用它,'
|
|
826
|
+
+ '而不是自己连续调 wiki_recall。返回里 status.support 与 status.degraded 必须分别看待:'
|
|
827
|
+
+ 'support 说的是「知识够不够」,degraded 说的是「引擎有没有坏」,后者为真时不能把 support 当结论。'
|
|
828
|
+
+ '返回的 context_id 可交给 wiki_verify 核验后续回答。',
|
|
829
|
+
parameters: {
|
|
830
|
+
type: 'object',
|
|
831
|
+
properties: {
|
|
832
|
+
query: { type: 'string', description: '要查的问题,或核心名词组合(不要整段散文)' },
|
|
833
|
+
limit: { type: 'number', description: '最多读几页证据,1–12,默认 5' },
|
|
834
|
+
engine: {
|
|
835
|
+
type: 'string',
|
|
836
|
+
enum: ['both', 'search', 'wiki_recall'],
|
|
837
|
+
description: '用哪个检索通道;默认 both(两个都跑,各自保留出处,不混排名)',
|
|
838
|
+
},
|
|
839
|
+
ttl_seconds: { type: 'number', description: '这份上下文的快照保留多久,默认 1800 秒' },
|
|
840
|
+
},
|
|
841
|
+
required: ['query'],
|
|
842
|
+
additionalProperties: false,
|
|
843
|
+
},
|
|
844
|
+
output: {
|
|
845
|
+
schema: { type: 'object', additionalProperties: true },
|
|
846
|
+
render: (args, value) => [{ type: 'text', text: renderContext(args, value) }],
|
|
847
|
+
},
|
|
848
|
+
async execute(args) {
|
|
849
|
+
const query = typeof args?.query === 'string' ? args.query.trim() : ''
|
|
850
|
+
if (query === '') {
|
|
851
|
+
return errorEnvelope('E_QUERY_EMPTY', 'query 不能为空', 'validate')
|
|
852
|
+
}
|
|
853
|
+
return callBridge('context', {
|
|
854
|
+
query: query.slice(0, MAX_QUERY_CHARS * 4),
|
|
855
|
+
limit: args?.limit,
|
|
856
|
+
engine: args?.engine,
|
|
857
|
+
ttl_seconds: args?.ttl_seconds,
|
|
858
|
+
})
|
|
859
|
+
},
|
|
860
|
+
}))
|
|
861
|
+
|
|
862
|
+
disposers.push(ctx.tools.register({
|
|
863
|
+
name: 'wiki_verify',
|
|
864
|
+
description:
|
|
865
|
+
'拿一份已有的 wiki_context 结果去核验一段回答,不重新检索。'
|
|
866
|
+
+ '写完答案后自查(尤其含数字、范围、年份、题量分值这类主张时)用它。'
|
|
867
|
+
+ '返回 status 与 violations:violations 非空说明回答越过了证据允许的边界;'
|
|
868
|
+
+ 'context_expired / context_missing 是快照问题,不等于回答错了,需要重新取上下文再核验。',
|
|
869
|
+
parameters: {
|
|
870
|
+
type: 'object',
|
|
871
|
+
properties: {
|
|
872
|
+
context_id: { type: 'string', description: 'wiki_context 返回的 context_id' },
|
|
873
|
+
answer: { type: 'string', description: '要核验的回答原文' },
|
|
874
|
+
},
|
|
875
|
+
required: ['context_id', 'answer'],
|
|
876
|
+
additionalProperties: false,
|
|
877
|
+
},
|
|
878
|
+
output: {
|
|
879
|
+
schema: { type: 'object', additionalProperties: true },
|
|
880
|
+
render: (args, value) => [{ type: 'text', text: renderVerify(args, value) }],
|
|
881
|
+
},
|
|
882
|
+
async execute(args) {
|
|
883
|
+
const contextId = typeof args?.context_id === 'string' ? args.context_id.trim() : ''
|
|
884
|
+
if (contextId === '') {
|
|
885
|
+
return errorEnvelope('E_CONTEXT_NOT_FOUND', '缺少 context_id', 'validate')
|
|
886
|
+
}
|
|
887
|
+
const answer = typeof args?.answer === 'string' ? args.answer : ''
|
|
888
|
+
if (answer.trim() === '') {
|
|
889
|
+
return errorEnvelope('E_VERIFY_EMPTY_ANSWER', 'answer 不能为空', 'validate')
|
|
890
|
+
}
|
|
891
|
+
return callBridge('verify', { context_id: contextId, answer })
|
|
892
|
+
},
|
|
893
|
+
}))
|
|
894
|
+
|
|
895
|
+
disposers.push(ctx.tools.register({
|
|
896
|
+
name: 'wiki_observe',
|
|
897
|
+
description:
|
|
898
|
+
'查观察日志的统计:这段时间 wiki_context 被问过多少次、支持状态怎么分布、'
|
|
899
|
+
+ 'wiki_verify 挑出过哪些违规类型。只读诊断,不参与回答,也不改检索行为。',
|
|
900
|
+
parameters: {
|
|
901
|
+
type: 'object',
|
|
902
|
+
properties: {
|
|
903
|
+
days: { type: 'integer', minimum: 1, maximum: 365, description: '统计窗口天数,默认 7' },
|
|
904
|
+
limit: { type: 'integer', minimum: 1, maximum: 50, description: '最近记录条数,默认 10' },
|
|
905
|
+
},
|
|
906
|
+
additionalProperties: false,
|
|
907
|
+
},
|
|
908
|
+
output: {
|
|
909
|
+
schema: { type: 'object', additionalProperties: true },
|
|
910
|
+
render: (args, value) => [{ type: 'text', text: renderObserve(args, value) }],
|
|
911
|
+
},
|
|
912
|
+
async execute(args) {
|
|
913
|
+
return callBridge('observe', { days: args?.days, limit: args?.limit })
|
|
914
|
+
},
|
|
915
|
+
}))
|
|
916
|
+
|
|
917
|
+
return disposers
|
|
918
|
+
}
|
|
919
|
+
|
|
671
920
|
const register = () => ctx.tools.register({
|
|
672
|
-
name: 'wiki_recall',
|
|
673
|
-
description: DESCRIPTION,
|
|
921
|
+
name: 'wiki_recall', description: DESCRIPTION,
|
|
674
922
|
parameters: PARAMETERS,
|
|
675
923
|
output: {
|
|
676
924
|
schema: { type: 'object', additionalProperties: true },
|
|
@@ -718,6 +966,15 @@ export function apply(ctx, config = {}) {
|
|
|
718
966
|
return
|
|
719
967
|
}
|
|
720
968
|
|
|
969
|
+
// 网关工具单独注册:Python 侧没装好、或者配置里没填工具目录,
|
|
970
|
+
// 都不该连累 wiki_recall —— 那是两条独立的通路。
|
|
971
|
+
const gatewayDisposers = []
|
|
972
|
+
try {
|
|
973
|
+
gatewayDisposers.push(...registerGateway())
|
|
974
|
+
} catch (error) {
|
|
975
|
+
ctx.logger?.warn?.(`LLM-Wiki 网关:注册工具失败 —— ${String(error && error.message || error)}`)
|
|
976
|
+
}
|
|
977
|
+
|
|
721
978
|
ctx.on?.('dispose', () => {
|
|
722
979
|
stopWatch()
|
|
723
980
|
try {
|
|
@@ -730,6 +987,13 @@ export function apply(ctx, config = {}) {
|
|
|
730
987
|
} catch {
|
|
731
988
|
// 同上
|
|
732
989
|
}
|
|
990
|
+
for (const item of gatewayDisposers) {
|
|
991
|
+
try {
|
|
992
|
+
item?.()
|
|
993
|
+
} catch {
|
|
994
|
+
// 同上
|
|
995
|
+
}
|
|
996
|
+
}
|
|
733
997
|
})
|
|
734
998
|
}
|
|
735
999
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-wiki-notes",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
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. 中文:本地 Markdown
|
|
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 管线,插件只负责调用与错误隔离。",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"deepseek",
|
|
8
8
|
"deepseek-harness",
|