@furongjun1999/dsh-memory 0.9.10 → 0.9.13
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 +195 -150
- package/lib/hooks.d.ts +23 -2
- package/lib/hooks.js +96 -22
- package/lib/index.d.ts +3 -0
- package/lib/index.js +12 -1
- package/lib/tools.d.ts +8 -0
- package/lib/tools.js +32 -5
- package/md_cg/config_registry_bulk.py +6 -0
- package/md_cg/config_validate.py +6 -0
- package/md_cg/mcp_server.py +147 -11
- package/md_cg/mdcg.py +67 -3
- package/md_cg/mdcos.py +108 -1
- package/md_cg/refindex.py +11 -4
- package/md_cg/test_c8_search_rrf_gates.py +511 -502
- package/md_cg/test_domain_granularity.py +68 -0
- package/md_cg/test_exact_text_cache.py +12 -4
- package/md_cg/test_issue623_fullwidth_symmetry.py +66 -0
- package/md_cg/test_issue644_637_636.py +84 -0
- package/md_cg/test_p28_refcheck.py +7 -7
- package/md_cg/test_retr_gates_prodpath.py +11 -1
- package/md_cg/test_search_async.py +84 -0
- package/md_cg/test_search_prefilter.py +85 -0
- package/md_cg/tokens.py +5 -1
- package/package.json +113 -113
- package/scripts/workspace_index.py +2 -0
- package/src/hooks.ts +105 -23
- package/src/index.ts +15 -1
- package/src/tools.ts +37 -6
package/package.json
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@furongjun1999/dsh-memory",
|
|
3
|
-
"version": "0.9.
|
|
4
|
-
"description": "灵枢(Lingshu·líng shū)DeepSeek Harness 插件:完整大脑——长期记忆/知识飞轮/自我认知/递归反思接入 DSH,对话自动沉淀进 md_cg 认知图(md 文档)",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "lib/index.js",
|
|
7
|
-
"types": "lib/index.d.ts",
|
|
8
|
-
"bin": {
|
|
9
|
-
"lingshu": "./lib/cli.js",
|
|
10
|
-
"lingshu-init": "./lib/init.js",
|
|
11
|
-
"dsh-memory": "./lib/cli.js"
|
|
12
|
-
},
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"types": "./lib/index.d.ts",
|
|
16
|
-
"import": "./lib/index.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"files": [
|
|
20
|
-
"lib",
|
|
21
|
-
"src",
|
|
22
|
-
"md_cg",
|
|
23
|
-
"utf8_boot.py",
|
|
24
|
-
"data/policy.json",
|
|
25
|
-
"hive/id_charset_blocks.txt",
|
|
26
|
-
"skills",
|
|
27
|
-
"README.md",
|
|
28
|
-
"dsh",
|
|
29
|
-
"codebuddy",
|
|
30
|
-
"zcode",
|
|
31
|
-
"docs/规范/AI编程通用规范.md",
|
|
32
|
-
"docs/规范/AI编程通用规范_灵枢适配.md",
|
|
33
|
-
"docs/规范/AI编程通用规范_自检清单.md",
|
|
34
|
-
"docs/discipline",
|
|
35
|
-
"docs/工作纪律_认知图条目_v1.1.json",
|
|
36
|
-
"scripts/workspace_index.py"
|
|
37
|
-
],
|
|
38
|
-
"keywords": [
|
|
39
|
-
"deepseek",
|
|
40
|
-
"dsh",
|
|
41
|
-
"dsh-plugin",
|
|
42
|
-
"cordis",
|
|
43
|
-
"memory",
|
|
44
|
-
"agent",
|
|
45
|
-
"aeis",
|
|
46
|
-
"lingshu",
|
|
47
|
-
"multi-agent",
|
|
48
|
-
"spatiotemporal",
|
|
49
|
-
"knowledge-graph"
|
|
50
|
-
],
|
|
51
|
-
"license": "MIT",
|
|
52
|
-
"peerDependenciesMeta": {
|
|
53
|
-
"@deepseek-ai/cordis": {
|
|
54
|
-
"optional": true
|
|
55
|
-
},
|
|
56
|
-
"@deepseek-ai/dsh-llm": {
|
|
57
|
-
"optional": true
|
|
58
|
-
},
|
|
59
|
-
"@deepseek-ai/dsh-session": {
|
|
60
|
-
"optional": true
|
|
61
|
-
},
|
|
62
|
-
"@deepseek-ai/dsh-system-prompt": {
|
|
63
|
-
"optional": true
|
|
64
|
-
},
|
|
65
|
-
"@deepseek-ai/dsh-tools": {
|
|
66
|
-
"optional": true
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
"peerDependencies": {
|
|
70
|
-
"@deepseek-ai/cordis": ">=4.0.1",
|
|
71
|
-
"@deepseek-ai/dsh-llm": ">=0.1.0-rc.6",
|
|
72
|
-
"@deepseek-ai/dsh-session": ">=0.1.0-rc.6",
|
|
73
|
-
"@deepseek-ai/dsh-system-prompt": ">=0.1.0-rc.6",
|
|
74
|
-
"@deepseek-ai/dsh-tools": ">=0.1.0-rc.6"
|
|
75
|
-
},
|
|
76
|
-
"dependencies": {
|
|
77
|
-
"@deepseek-ai/schemastery": "^3.18.1"
|
|
78
|
-
},
|
|
79
|
-
"devDependencies": {
|
|
80
|
-
"@deepseek-ai/cordis": "^4.0.1",
|
|
81
|
-
"@deepseek-ai/dsh-llm": "^0.2.0-rc.2",
|
|
82
|
-
"@deepseek-ai/dsh-session": "^0.2.0-rc.2",
|
|
83
|
-
"@deepseek-ai/dsh-system-prompt": "^0.2.0-rc.2",
|
|
84
|
-
"@deepseek-ai/dsh-tools": "^0.2.0-rc.2",
|
|
85
|
-
"@deepseek-ai/schemastery": "^3.0.0",
|
|
86
|
-
"@types/node": "^26.6.5",
|
|
87
|
-
"tsx": "^4.19.0",
|
|
88
|
-
"typescript": "^7.0.2"
|
|
89
|
-
},
|
|
90
|
-
"scripts": {
|
|
91
|
-
"build": "node -e \"require('node:fs').rmSync('lib',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
92
|
-
"test": "npm run build && node --import tsx --test \"test/*.test.ts\"",
|
|
93
|
-
"prepare": "node -e \"const fs=require('fs');const tsc='node_modules/typescript/bin/tsc';if(fs.existsSync(tsc)){fs.rmSync('lib',{recursive:true,force:true});require('child_process').execFileSync(process.execPath,[tsc,'-p','tsconfig.json'],{stdio:'inherit'})}else if(fs.existsSync('lib/index.js')){console.log('[prepare] 跳过构建:typescript 未安装(NODE_ENV=production 或 --omit=dev 会省略 devDependencies)——需要构建请用 npm install --include=dev')}else{console.error('[prepare] 本安装会得到一个不可用的包(issue #161):包入口 main 指向 lib/index.js,但 lib/ 被 .gitignore 忽略、git 归档里没有它,此处 typescript 又未安装(NODE_ENV=production 或 --omit=dev 会省略 devDependencies)⇒ prepare 无法构建,lib/index.js 不存在。修法:用 npm install --include=dev 安装(带上 devDependencies,让 prepare 能构建),或改用已带构建产物的发布包。');process.exitCode=1}\"",
|
|
94
|
-
"gate": "python scripts/check_unreachable.py && python scripts/check_publish_artifact.py && python scripts/check_publish_smoke.py && python scripts/cogmap_sync.py check && python scripts/link_check.py && python scripts/check_line_anchors.py && python scripts/workspace_index.py --check && python scripts/discipline_nodes.py --init --write --cg-root .tmp/discipline-cg && python scripts/verify_discipline.py --allow-missing --cg-root .tmp/discipline-cg && python scripts/gate_rust_crate_test.py && python scripts/gate_rust_parity.py && python scripts/check_local_paths.py && python scripts/body_e2e_smoke.py",
|
|
95
|
-
"prepublishOnly": "npm run build && npm test && npm run gate"
|
|
96
|
-
},
|
|
97
|
-
"repository": {
|
|
98
|
-
"type": "git",
|
|
99
|
-
"url": "git+https://github.com/FuRongJun-1999/dsh-memory.git"
|
|
100
|
-
},
|
|
101
|
-
"engines": {
|
|
102
|
-
"node": ">=22.19"
|
|
103
|
-
},
|
|
104
|
-
"dsh": {
|
|
105
|
-
"bundle": {
|
|
106
|
-
"patch": "./dsh/cordis.patch.yml"
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
"publishConfig": {
|
|
110
|
-
"access": "public",
|
|
111
|
-
"registry": "https://registry.npmjs.org/"
|
|
112
|
-
}
|
|
113
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@furongjun1999/dsh-memory",
|
|
3
|
+
"version": "0.9.13",
|
|
4
|
+
"description": "灵枢(Lingshu·líng shū)DeepSeek Harness 插件:完整大脑——长期记忆/知识飞轮/自我认知/递归反思接入 DSH,对话自动沉淀进 md_cg 认知图(md 文档)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"lingshu": "./lib/cli.js",
|
|
10
|
+
"lingshu-init": "./lib/init.js",
|
|
11
|
+
"dsh-memory": "./lib/cli.js"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"import": "./lib/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"src",
|
|
22
|
+
"md_cg",
|
|
23
|
+
"utf8_boot.py",
|
|
24
|
+
"data/policy.json",
|
|
25
|
+
"hive/id_charset_blocks.txt",
|
|
26
|
+
"skills",
|
|
27
|
+
"README.md",
|
|
28
|
+
"dsh",
|
|
29
|
+
"codebuddy",
|
|
30
|
+
"zcode",
|
|
31
|
+
"docs/规范/AI编程通用规范.md",
|
|
32
|
+
"docs/规范/AI编程通用规范_灵枢适配.md",
|
|
33
|
+
"docs/规范/AI编程通用规范_自检清单.md",
|
|
34
|
+
"docs/discipline",
|
|
35
|
+
"docs/工作纪律_认知图条目_v1.1.json",
|
|
36
|
+
"scripts/workspace_index.py"
|
|
37
|
+
],
|
|
38
|
+
"keywords": [
|
|
39
|
+
"deepseek",
|
|
40
|
+
"dsh",
|
|
41
|
+
"dsh-plugin",
|
|
42
|
+
"cordis",
|
|
43
|
+
"memory",
|
|
44
|
+
"agent",
|
|
45
|
+
"aeis",
|
|
46
|
+
"lingshu",
|
|
47
|
+
"multi-agent",
|
|
48
|
+
"spatiotemporal",
|
|
49
|
+
"knowledge-graph"
|
|
50
|
+
],
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@deepseek-ai/cordis": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"@deepseek-ai/dsh-llm": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@deepseek-ai/dsh-session": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"@deepseek-ai/dsh-system-prompt": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"@deepseek-ai/dsh-tools": {
|
|
66
|
+
"optional": true
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"@deepseek-ai/cordis": ">=4.0.1",
|
|
71
|
+
"@deepseek-ai/dsh-llm": ">=0.1.0-rc.6",
|
|
72
|
+
"@deepseek-ai/dsh-session": ">=0.1.0-rc.6",
|
|
73
|
+
"@deepseek-ai/dsh-system-prompt": ">=0.1.0-rc.6",
|
|
74
|
+
"@deepseek-ai/dsh-tools": ">=0.1.0-rc.6"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"@deepseek-ai/schemastery": "^3.18.1"
|
|
78
|
+
},
|
|
79
|
+
"devDependencies": {
|
|
80
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
81
|
+
"@deepseek-ai/dsh-llm": "^0.2.0-rc.2",
|
|
82
|
+
"@deepseek-ai/dsh-session": "^0.2.0-rc.2",
|
|
83
|
+
"@deepseek-ai/dsh-system-prompt": "^0.2.0-rc.2",
|
|
84
|
+
"@deepseek-ai/dsh-tools": "^0.2.0-rc.2",
|
|
85
|
+
"@deepseek-ai/schemastery": "^3.0.0",
|
|
86
|
+
"@types/node": "^26.6.5",
|
|
87
|
+
"tsx": "^4.19.0",
|
|
88
|
+
"typescript": "^7.0.2"
|
|
89
|
+
},
|
|
90
|
+
"scripts": {
|
|
91
|
+
"build": "node -e \"require('node:fs').rmSync('lib',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
92
|
+
"test": "npm run build && node --import tsx --test \"test/*.test.ts\"",
|
|
93
|
+
"prepare": "node -e \"const fs=require('fs');const tsc='node_modules/typescript/bin/tsc';if(fs.existsSync(tsc)){fs.rmSync('lib',{recursive:true,force:true});require('child_process').execFileSync(process.execPath,[tsc,'-p','tsconfig.json'],{stdio:'inherit'})}else if(fs.existsSync('lib/index.js')){console.log('[prepare] 跳过构建:typescript 未安装(NODE_ENV=production 或 --omit=dev 会省略 devDependencies)——需要构建请用 npm install --include=dev')}else{console.error('[prepare] 本安装会得到一个不可用的包(issue #161):包入口 main 指向 lib/index.js,但 lib/ 被 .gitignore 忽略、git 归档里没有它,此处 typescript 又未安装(NODE_ENV=production 或 --omit=dev 会省略 devDependencies)⇒ prepare 无法构建,lib/index.js 不存在。修法:用 npm install --include=dev 安装(带上 devDependencies,让 prepare 能构建),或改用已带构建产物的发布包。');process.exitCode=1}\"",
|
|
94
|
+
"gate": "python scripts/check_unreachable.py && python scripts/check_publish_artifact.py && python scripts/check_publish_smoke.py && python scripts/cogmap_sync.py check && python scripts/link_check.py && python scripts/check_line_anchors.py && python scripts/workspace_index.py --check && python scripts/discipline_nodes.py --init --write --cg-root .tmp/discipline-cg && python scripts/verify_discipline.py --allow-missing --cg-root .tmp/discipline-cg && python scripts/gate_rust_crate_test.py && python scripts/gate_rust_parity.py && python scripts/check_local_paths.py && python scripts/body_e2e_smoke.py",
|
|
95
|
+
"prepublishOnly": "npm run build && npm test && npm run gate"
|
|
96
|
+
},
|
|
97
|
+
"repository": {
|
|
98
|
+
"type": "git",
|
|
99
|
+
"url": "git+https://github.com/FuRongJun-1999/dsh-memory.git"
|
|
100
|
+
},
|
|
101
|
+
"engines": {
|
|
102
|
+
"node": ">=22.19"
|
|
103
|
+
},
|
|
104
|
+
"dsh": {
|
|
105
|
+
"bundle": {
|
|
106
|
+
"patch": "./dsh/cordis.patch.yml"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"publishConfig": {
|
|
110
|
+
"access": "public",
|
|
111
|
+
"registry": "https://registry.npmjs.org/"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -44,6 +44,8 @@ CI_WORKFLOW = ".github/workflows/workspace-index-check.yml"
|
|
|
44
44
|
# —— 职责真源(本脚本即真源;新增顶层目录 / 根级文件必须在此登记,否则 --check 亮红)——
|
|
45
45
|
# 值 = (职责一句话, [关键入口,相对本目录;目录名亦可])
|
|
46
46
|
DIR_ROLES = {
|
|
47
|
+
"ui": ("灵枢极简前端增量包(可选:给已装插件补可视化侧栏;不随 npm 包分发)",
|
|
48
|
+
["minimal/install.ps1", "minimal/payload/web/app.js"]),
|
|
47
49
|
#: 收据目录(receipt 机制):`完成` 必须带可复现命令输出(见 docs/state/会话状态.md 顶部用法)。
|
|
48
50
|
#: 登记于此是按纪律 #18「新增顶层目录须在 DIR_ROLES 登记后重生成」——本条属**机械连带产物**。
|
|
49
51
|
"receipts": ("收据(可复现命令输出的证据面)—— scripts/receipt.py 的 --write/--check/--list 落盘目录",
|
package/src/hooks.ts
CHANGED
|
@@ -92,6 +92,20 @@ export interface MemoryHooksOptions {
|
|
|
92
92
|
autoRecall: boolean
|
|
93
93
|
/** 自动召回条数(默认 4)。 */
|
|
94
94
|
autoRecallLimit: number
|
|
95
|
+
/** 逢十轮提醒(纪律第 16 条;issue #645)。缺省 10 = 每 10 个真实用户回合提醒一次;
|
|
96
|
+
* 数字 = 自定义间隔(1~100);false = 关闭。
|
|
97
|
+
*
|
|
98
|
+
* 计轮单位是「用户消息条数」,不是 assemble 次数——实测一次模型请求约 10 次 assemble,
|
|
99
|
+
* 按 assemble 计数会把「每 10 轮」退化成「每一步」。 */
|
|
100
|
+
turnReminder?: boolean | number
|
|
101
|
+
/** 身份锚点独立注入块(issue #635,默认 false = 保持既有行为)。
|
|
102
|
+
*
|
|
103
|
+
* 背景:永久层(self/anchor)被 RECALL_SKIP_LAYERS 跳过,用户写完「我是谁」永远
|
|
104
|
+
* 不会被自动注入,只能去改 stg.py(升级即被整包抹掉)。
|
|
105
|
+
* 开启后:额外取若干条 self/anchor 节点,单独渲染成「【身份锚点】」块,
|
|
106
|
+
* **独立预算**(不占 recent 块),条数与字数都小(存在性提示,不承载知识)。
|
|
107
|
+
*/
|
|
108
|
+
autoRecallIdentity?: boolean
|
|
95
109
|
/** 自动记忆脱敏:写入前过滤敏感信息(密钥/密码/令牌/身份证/手机号,默认 true)。 */
|
|
96
110
|
desensitize: boolean
|
|
97
111
|
/** 落盘审计文件路径(issue #56,诊断面:哪些消息被设计滤除、source.kind 分布、
|
|
@@ -409,7 +423,7 @@ const RECALL_SKIP_LAYERS = new Set(['anchor', 'self'])
|
|
|
409
423
|
* 9 条全部保留。(注意:整块仍远小于一条知识节点 500~800 tok,故本块定位是
|
|
410
424
|
* 「存在性索引/提醒」,不承载知识本身——要知识请显式 mdcg_recall 并给足预算。) */
|
|
411
425
|
// issue #563:导出仅为可测(注入面脱敏顺序的回归用例需要直接喂 payload)。
|
|
412
|
-
export function formatTimelineDecayed(payload: unknown): string {
|
|
426
|
+
export function formatTimelineDecayed(payload: unknown, opts?: { includeIdentity?: boolean }): string {
|
|
413
427
|
const items = (payload && typeof payload === 'object'
|
|
414
428
|
&& Array.isArray((payload as { items?: unknown }).items))
|
|
415
429
|
? (payload as { items: Array<Record<string, unknown>> }).items
|
|
@@ -418,6 +432,9 @@ export function formatTimelineDecayed(payload: unknown): string {
|
|
|
418
432
|
let rank = 0
|
|
419
433
|
for (const it of items) {
|
|
420
434
|
const layer = String(it['layer'] ?? '')
|
|
435
|
+
// issue #635:includeIdentity=true 时,永久层**在本块仍不渲染**(它们走独立块),
|
|
436
|
+
// 这里只放宽"跳过"以便调用方取到它们——真正的分流在 renderIdentityAnchors。
|
|
437
|
+
if (RECALL_SKIP_LAYERS.has(layer) && !opts?.includeIdentity) continue
|
|
421
438
|
if (RECALL_SKIP_LAYERS.has(layer)) continue
|
|
422
439
|
const preview = String(it['preview'] ?? '').replace(/\s+/g, ' ').trim()
|
|
423
440
|
if (!preview) continue
|
|
@@ -439,6 +456,30 @@ export function formatTimelineDecayed(payload: unknown): string {
|
|
|
439
456
|
return out.join('\n').slice(0, RECALL_MAX_CHARS)
|
|
440
457
|
}
|
|
441
458
|
|
|
459
|
+
/** 身份锚点块(issue #635):只取永久层(self/anchor),独立预算。 */
|
|
460
|
+
export const IDENTITY_MAX_ITEMS = 3
|
|
461
|
+
export const IDENTITY_MAX_CHARS = 640
|
|
462
|
+
export const IDENTITY_ITEM_CHARS = 160
|
|
463
|
+
|
|
464
|
+
export function renderIdentityAnchors(payload: unknown): string {
|
|
465
|
+
const items = (payload && typeof payload === 'object'
|
|
466
|
+
&& Array.isArray((payload as { items?: unknown }).items))
|
|
467
|
+
? (payload as { items: Array<Record<string, unknown>> }).items
|
|
468
|
+
: []
|
|
469
|
+
const out: string[] = []
|
|
470
|
+
for (const it of items) {
|
|
471
|
+
const layer = String(it['layer'] ?? '')
|
|
472
|
+
if (!RECALL_SKIP_LAYERS.has(layer)) continue
|
|
473
|
+
const preview = String(it['preview'] ?? '').replace(/\s+/g, ' ').trim()
|
|
474
|
+
if (!preview) continue
|
|
475
|
+
const safe = redactKeepPlaceholders(preview)
|
|
476
|
+
out.push(' - [' + layer + '] ' + (safe.length > IDENTITY_ITEM_CHARS ? safe.slice(0, IDENTITY_ITEM_CHARS) + '…' : safe))
|
|
477
|
+
if (out.length >= IDENTITY_MAX_ITEMS) break
|
|
478
|
+
if (out.join('\n').length >= IDENTITY_MAX_CHARS) break
|
|
479
|
+
}
|
|
480
|
+
return out.join('\n').slice(0, IDENTITY_MAX_CHARS)
|
|
481
|
+
}
|
|
482
|
+
|
|
442
483
|
// ---------------------------------------------------------------- 会话窗口渲染
|
|
443
484
|
// 「短期会话窗口」(contextWindow)的取数/渲染常量。与上方 RECALL_* 同款纪律:
|
|
444
485
|
// 常量写死在此处(而非 config schema——未知键会被 schema 剥离)。
|
|
@@ -729,10 +770,21 @@ export function installMemoryHooks(ctx: Context, mdcg: MdcgClient | null, opts:
|
|
|
729
770
|
// 因此本实现把「要不要补」交还给宿主:压缩归档后宿主会把 retained 置空并重新投影快照
|
|
730
771
|
// (RuntimeContextProjection 的 retained === null 分支),本块自然跟着回来——
|
|
731
772
|
// 不需要插件自己数步数做自愈。
|
|
732
|
-
/**
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
const
|
|
773
|
+
/** 真实轮次计数(session → 用户消息条数)。issue #645:计轮单位必须是「用户回合」,
|
|
774
|
+
* 不是 assemble 次数——实测一步约 10 次 assemble,按 assemble 计数会把「每 10 轮」
|
|
775
|
+
* 退化成「每一步」,提醒于是常驻上下文、agent 反复空转归档。 */
|
|
776
|
+
const userTurns = new Map<string, number>()
|
|
777
|
+
/** 待推送提醒(session → 文本):用户回合计数命中时写入,assemble 时只消费一次;
|
|
778
|
+
* 不做 sticky 重推(那是提醒常驻的直接原因)。 */
|
|
779
|
+
const pendingReminder = new Map<string, string>()
|
|
780
|
+
/** 提醒间隔:false → 0(关闭);数字 → clamp 1..100;未设 → 10。 */
|
|
781
|
+
const reminderInterval = (() => {
|
|
782
|
+
const cfg = opts.turnReminder
|
|
783
|
+
if (cfg === false) return 0
|
|
784
|
+
const n = typeof cfg === 'number' ? cfg : 10
|
|
785
|
+
if (!Number.isFinite(n)) return 10
|
|
786
|
+
return Math.max(1, Math.min(100, Math.floor(n)))
|
|
787
|
+
})()
|
|
736
788
|
if (opts.autoRecall) {
|
|
737
789
|
const recallLimit = Math.max(1, Math.min(10, opts.autoRecallLimit || 4))
|
|
738
790
|
ctx.on('system-prompt/assemble', async (assembly, _ctx, next) => {
|
|
@@ -755,23 +807,23 @@ export function installMemoryHooks(ctx: Context, mdcg: MdcgClient | null, opts:
|
|
|
755
807
|
// 真机单实例下两者逐位等价(差异窗口不可达)。
|
|
756
808
|
const sid = sessionIdOf(hostCtx?.agent?.session)
|
|
757
809
|
|| (observedSession ? currentSession() : '')
|
|
758
|
-
// 逢十轮提醒(纪律第 16
|
|
759
|
-
//
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
}
|
|
769
|
-
const sticky = remindText.get(turnKey)
|
|
770
|
-
if (sticky) {
|
|
771
|
-
assembly.contexts.push({ name: 'lingshu:turn-reminder', text: sticky })
|
|
810
|
+
// 逢十轮提醒(纪律第 16 条;issue #645 修正):计数挪到 session/event 的真实
|
|
811
|
+
// 用户回合上,这里只消费一次待推文本。旧实现把 assemble 当轮次 + 段内永久重推
|
|
812
|
+
// ⇒ 提醒几乎常驻、agent 反复空转归档;sid 缺失时还回落共用键 __no_sid__,
|
|
813
|
+
// 并行会话互相污染频率。现改为:无 sid 不提醒;提醒只在本回合第一次 assemble 出现。
|
|
814
|
+
if (sid) {
|
|
815
|
+
const pending = pendingReminder.get(sid)
|
|
816
|
+
if (pending) {
|
|
817
|
+
pendingReminder.delete(sid)
|
|
818
|
+
assembly.contexts.push({ name: 'lingshu:turn-reminder', text: pending })
|
|
819
|
+
}
|
|
772
820
|
}
|
|
773
|
-
|
|
774
|
-
|
|
821
|
+
// 非永久层 → 既有「最近记忆」块;self/anchor → 独立「身份锚点」块(独立预算)。
|
|
822
|
+
const identityOn = opts.autoRecallIdentity === true
|
|
823
|
+
const timelinePayload = await graph.timeline(
|
|
824
|
+
identityOn ? recallLimit + IDENTITY_MAX_ITEMS : recallLimit,
|
|
825
|
+
sid ? { session: sid } : {})
|
|
826
|
+
const text = formatTimelineDecayed(timelinePayload)
|
|
775
827
|
if (text) {
|
|
776
828
|
// 注入边界转义(issue #16):宿主 system-prompt 对 context 文本做严格
|
|
777
829
|
// `{{variable}}` 插值,裸 `{{` 会 throw → 该轮请求整体失败。记忆原文
|
|
@@ -804,6 +856,23 @@ export function installMemoryHooks(ctx: Context, mdcg: MdcgClient | null, opts:
|
|
|
804
856
|
})
|
|
805
857
|
}
|
|
806
858
|
}
|
|
859
|
+
if (identityOn) {
|
|
860
|
+
try {
|
|
861
|
+
const idText = renderIdentityAnchors(timelinePayload)
|
|
862
|
+
if (idText) {
|
|
863
|
+
assembly.contexts.push({
|
|
864
|
+
name: 'lingshu:identity-anchor',
|
|
865
|
+
text: escapePromptBraces(renderUntrustedMemoryBlock(
|
|
866
|
+
'【灵枢身份锚点】(self/anchor 层,独立预算)\n' + idText)),
|
|
867
|
+
})
|
|
868
|
+
}
|
|
869
|
+
} catch (err) {
|
|
870
|
+
// issue #128 同款纪律:fail-soft 但不静默——空 catch 会被
|
|
871
|
+
// test/session-declaration-report ③ 判红,也会让「身份块从未注入成功」
|
|
872
|
+
// 与「确实没有身份节点」不可分辨。失败不越过会话流边界(不抛)。
|
|
873
|
+
ctx.logger.warn(`dsh-memory: 身份锚点注入失败(已跳过该块): ${redactForLog(err)}`)
|
|
874
|
+
}
|
|
875
|
+
}
|
|
807
876
|
// ── 独立第二块:「【本会话近期对话】」(contextWindow 注入面)──
|
|
808
877
|
// 与上方 timeline 块**块名/取数/开关各自独立**(既有块一字未动)。
|
|
809
878
|
// 稳定性口径与既有块同等:**每步都 push**(内容随新轮增长属预期——
|
|
@@ -893,6 +962,16 @@ export function installMemoryHooks(ctx: Context, mdcg: MdcgClient | null, opts:
|
|
|
893
962
|
}
|
|
894
963
|
const text = extractText(event.data.content)
|
|
895
964
|
if (!text) return
|
|
965
|
+
// 逢十轮提醒的**计轮点**(issue #645):真实用户回合 = 用户消息条数。
|
|
966
|
+
// 放在脱敏之前——被脱敏丢弃的消息也是一次真实交互,不应影响计数。
|
|
967
|
+
// 无 sid 不计数、不提醒(旧实现回落共用键 __no_sid__,并行会话会互相污染频率)。
|
|
968
|
+
if (sid && reminderInterval > 0) {
|
|
969
|
+
const n = (userTurns.get(sid) ?? 0) + 1
|
|
970
|
+
userTurns.set(sid, n)
|
|
971
|
+
if (shouldRemindTurn(n, reminderInterval)) {
|
|
972
|
+
pendingReminder.set(sid, escapePromptBraces(formatTurnReminder(n)))
|
|
973
|
+
}
|
|
974
|
+
}
|
|
896
975
|
const safe = sanitize(text) // 脱敏:纯凭据消息 → null → 跳过写入
|
|
897
976
|
if (safe === null) { audit.skipped('sanitized', 'user'); return }
|
|
898
977
|
memorize('user', 'user', (g) => g.remember(safe, {
|
|
@@ -971,8 +1050,11 @@ export function installMemoryHooks(ctx: Context, mdcg: MdcgClient | null, opts:
|
|
|
971
1050
|
* 不等收尾」。zcode 端由 `scripts/zcode_window_hook.py` 注入提醒;**DSH 端此前
|
|
972
1051
|
* 完全没有实现**——纪律在产物里、机制不在,代理只能靠自觉(实测:整场长会话
|
|
973
1052
|
* 一次归档都没做)。本函数是该缺口的判定单点。 */
|
|
974
|
-
export function shouldRemindTurn(n: number): boolean {
|
|
975
|
-
|
|
1053
|
+
export function shouldRemindTurn(n: number, interval = 10): boolean {
|
|
1054
|
+
if (!Number.isFinite(n) || !Number.isFinite(interval)) return false
|
|
1055
|
+
const step = Math.floor(interval)
|
|
1056
|
+
if (step <= 0) return false
|
|
1057
|
+
return n >= step && n % step === 0
|
|
976
1058
|
}
|
|
977
1059
|
|
|
978
1060
|
/** 提醒文本(不含注入边界转义——转义在 push 站点做,与其它块同规)。 */
|
package/src/index.ts
CHANGED
|
@@ -95,6 +95,9 @@ export const inject = ['tools']
|
|
|
95
95
|
export interface Config {
|
|
96
96
|
/** 内部标识(provider/日志用;**不再用于工具名前缀**,工具名与 MCP 服务同名)。 */
|
|
97
97
|
serverName: string
|
|
98
|
+
/** issue #634:是否同时注册 lingshu_* 兼容别名(缺省 true)。
|
|
99
|
+
* 0.9.9 起工具名去前缀,旧会话/旧脚本硬编码旧名会 unknown tool。 */
|
|
100
|
+
toolAliases: boolean
|
|
98
101
|
/** Python 可执行文件(或 md_cg-mcp console script)。
|
|
99
102
|
* 缺省 = 平台感知(issue #19):Windows `python` / Linux·macOS `python3`,
|
|
100
103
|
* 可用 env `MDCG_PYTHON` 覆盖;显式配置本项优先级最高。 */
|
|
@@ -149,6 +152,9 @@ export interface Config {
|
|
|
149
152
|
|
|
150
153
|
export const Config: z<Config> = z.object({
|
|
151
154
|
serverName: z.string().default('lingshu'),
|
|
155
|
+
/** issue #634:0.9.9 起工具名去前缀(lingshu_cg -> cg)。
|
|
156
|
+
* 缺省 true = 同时注册 lingshu_* 兼容别名;设 false 则只注册裸名。 */
|
|
157
|
+
toolAliases: z.boolean().default(true),
|
|
152
158
|
// issue #19:默认值按平台解析(Windows: python / 其它: python3;env MDCG_PYTHON 可覆盖)。
|
|
153
159
|
// 写死 'python' 时 Linux/macOS 首装即 spawn ENOENT(PEP 394 只有 python3),
|
|
154
160
|
// 且表现为「装好了但记忆永远为空」——最难定位的失效形态。
|
|
@@ -168,6 +174,11 @@ export const Config: z<Config> = z.object({
|
|
|
168
174
|
importance: z.number().default(0.6),
|
|
169
175
|
autoRecall: z.boolean().default(true),
|
|
170
176
|
autoRecallLimit: z.number().default(4),
|
|
177
|
+
/** issue #635:self/anchor 层单列「身份锚点」块(默认 false = 维持既有行为)。 */
|
|
178
|
+
autoRecallIdentity: z.boolean().default(false),
|
|
179
|
+
/** 逢十轮提醒(纪律第 16 条;issue #645):缺省 10 = 每 10 个真实用户回合;
|
|
180
|
+
* false = 关闭;数字 = 自定义间隔(1~100)。 */
|
|
181
|
+
turnReminder: z.union([z.boolean(), z.number()]).default(10),
|
|
171
182
|
desensitize: z.boolean().default(true),
|
|
172
183
|
/** **短期会话窗口**(滑动窗口,2026-10-06)——「短期保留近 N 条对话记录」
|
|
173
184
|
* 的机制实现(长期记忆仍走灵枢的显式调用)。
|
|
@@ -191,7 +202,7 @@ export const Config: z<Config> = z.object({
|
|
|
191
202
|
})
|
|
192
203
|
.default({ enabled: true, turns: 10 }),
|
|
193
204
|
})
|
|
194
|
-
.default({ userMessage: true, assistantMessage: false, toolResult: false, importance: 0.6, autoRecall: true, autoRecallLimit: 4, desensitize: true, contextWindow: { enabled: true, turns: 10 } }),
|
|
205
|
+
.default({ userMessage: true, assistantMessage: false, toolResult: false, importance: 0.6, autoRecall: true, autoRecallLimit: 4, autoRecallIdentity: false, turnReminder: 10, desensitize: true, contextWindow: { enabled: true, turns: 10 } }),
|
|
195
206
|
toolCallTimeoutMs: z.number().default(60_000),
|
|
196
207
|
maxRetryDelayMs: z.number().default(30_000),
|
|
197
208
|
failOnStartupError: z.boolean().default(false),
|
|
@@ -430,6 +441,9 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
|
430
441
|
// `cg` / `stg` / `mdcg_*`。此前按 `${config.serverName}_` 拼成 `cg`,造成「同一个
|
|
431
442
|
// MCP 服务、不同端看到不同工具名」的初次识别错误。serverName 仍用于内部 provider/日志标识。
|
|
432
443
|
toolPrefix: '',
|
|
444
|
+
// issue #634:默认补注册 lingshu_* 兼容别名(旧会话/旧脚本硬编码旧名时不至于 unknown tool)。
|
|
445
|
+
// 需要「只要裸名」的部署可设 tools.legacyAliases=false。
|
|
446
|
+
legacyAliases: config.toolAliases !== false,
|
|
433
447
|
})
|
|
434
448
|
disposers.push(dispose)
|
|
435
449
|
toolsRegistered = true
|
package/src/tools.ts
CHANGED
|
@@ -186,10 +186,26 @@ export function extractText(content: Array<{ type: string; text?: string; [key:
|
|
|
186
186
|
* import 本包」在模块求值前就失败。惰性导入后:不在 DSH 宿主内时包仍可加载,
|
|
187
187
|
* 只有真正注册工具才报错,且错误消息点名缺哪个包。
|
|
188
188
|
*/
|
|
189
|
+
/** 工具对外名(issue #634):正式名 + 可选兼容别名。
|
|
190
|
+
*
|
|
191
|
+
* 0.9.9 起去掉了 `${serverName}_` 前缀(lingshu_cg -> cg),升级瞬间
|
|
192
|
+
* 已存在会话里的旧名调用全部变成 unknown tool。这里给出「一个工具要注册哪些名字」的
|
|
193
|
+
* 纯函数,注册面与测试共用,避免两处各写一份判据。
|
|
194
|
+
*/
|
|
195
|
+
export function toolPublicNames(
|
|
196
|
+
toolName: string,
|
|
197
|
+
prefix: string,
|
|
198
|
+
legacyAliases = true,
|
|
199
|
+
): string[] {
|
|
200
|
+
const primary = `${prefix}${toolName}`
|
|
201
|
+
if (prefix || !legacyAliases || toolName.startsWith("lingshu_")) return [primary]
|
|
202
|
+
return [primary, `lingshu_${toolName}`]
|
|
203
|
+
}
|
|
204
|
+
|
|
189
205
|
export async function registerLingshuTools(
|
|
190
206
|
ctx: Context,
|
|
191
207
|
bridge: LingshuBridge,
|
|
192
|
-
opts: { selection: ToolSelection; toolPrefix: string },
|
|
208
|
+
opts: { selection: ToolSelection; toolPrefix: string; legacyAliases?: boolean },
|
|
193
209
|
): Promise<() => void> {
|
|
194
210
|
let defineTool: typeof import('@deepseek-ai/dsh-tools').defineTool
|
|
195
211
|
try {
|
|
@@ -205,12 +221,21 @@ export async function registerLingshuTools(
|
|
|
205
221
|
const disposers: Array<() => void> = []
|
|
206
222
|
const registered: string[] = []
|
|
207
223
|
try {
|
|
224
|
+
// issue #634(2026-10-22):0.9.9 起工具名去掉 serverName 前缀(lingshu_cg -> cg),
|
|
225
|
+
// 升级瞬间已存在的会话里旧名调用全变 unknown tool,宿主与插件都没有提示。
|
|
226
|
+
// 这里加兼容别名:裸名照常注册,同时补注册 lingshu_<name>(legacyAliases=false 可关)。
|
|
227
|
+
// 别名与正式名指向同一个 execute,语义不变;描述标注 deprecated 供模型与人识别。
|
|
208
228
|
for (const tool of tools) {
|
|
209
229
|
if (!wanted.has(tool.name)) continue
|
|
210
|
-
const
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
230
|
+
const names = toolPublicNames(tool.name, opts.toolPrefix, opts.legacyAliases !== false)
|
|
231
|
+
const publicName = names[0] as string
|
|
232
|
+
const aliasName = names[1] ?? ""
|
|
233
|
+
const makeDefinition = (registeredName: string, deprecated: boolean) => defineTool({
|
|
234
|
+
name: registeredName,
|
|
235
|
+
description: deprecated
|
|
236
|
+
? '【已弃用别名】旧版工具名(升级兼容用),请改用 ' + publicName + '。原始描述:'
|
|
237
|
+
+ (tool.description || '灵枢 ' + tool.name)
|
|
238
|
+
: (tool.description || '灵枢 ' + tool.name),
|
|
214
239
|
parameters: schemaToParameters(tool.inputSchema),
|
|
215
240
|
output: {
|
|
216
241
|
schema: { type: 'json' } as never,
|
|
@@ -253,8 +278,14 @@ export async function registerLingshuTools(
|
|
|
253
278
|
return { content: result.content } as never
|
|
254
279
|
},
|
|
255
280
|
})
|
|
256
|
-
disposers.push(ctx.tools.register(
|
|
281
|
+
disposers.push(ctx.tools.register(makeDefinition(publicName, false)))
|
|
257
282
|
registered.push(publicName)
|
|
283
|
+
if (aliasName) {
|
|
284
|
+
// 兼容别名:指向同一个 execute(makeDefinition 复用了同一份定义体),
|
|
285
|
+
// 标记 deprecated;下一轮 minor 再评估是否移除。
|
|
286
|
+
disposers.push(ctx.tools.register(makeDefinition(aliasName, true)))
|
|
287
|
+
registered.push(aliasName)
|
|
288
|
+
}
|
|
258
289
|
}
|
|
259
290
|
} catch (err) {
|
|
260
291
|
for (const dispose of disposers) dispose()
|