lingshu-skill 0.0.0-stage → 3.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2605 -0
- package/LICENSE +21 -0
- package/README.md +698 -3
- package/SKILL-lite.md +45 -0
- package/SKILL-tiny.md +37 -0
- package/SKILL.md +379 -0
- package/assets/panel.html +5616 -0
- package/cordis-plugin/lingshu-cron/cordis.patch.yml +3 -0
- package/cordis-plugin/lingshu-cron/index.js +4 -0
- package/cordis-plugin/lingshu-cron/package.json +34 -0
- package/cordis-plugin/lingshu-cron/plugin.js +1040 -0
- package/cordis-plugin/lingshu-guard/plugin.js +473 -0
- package/cordis-plugin/lingshu-im/plugin.js +635 -0
- package/cordis-plugin/lingshu-startup/README.md +123 -0
- package/cordis-plugin/lingshu-startup/cordis.patch.yml +3 -0
- package/cordis-plugin/lingshu-startup/index.js +4 -0
- package/cordis-plugin/lingshu-startup/package.json +25 -0
- package/cordis-plugin/lingshu-startup/plugin.js +352 -0
- package/cordis.patch.yml +3 -0
- package/docs/commands.md +243 -0
- package/docs/mechanics.md +610 -0
- package/docs/models.md +52 -0
- package/docs/npm-/345/217/221/345/270/203/350/257/264/346/230/216.md +49 -0
- package/docs/panel-api.md +198 -0
- package/docs/recovery.md +90 -0
- package/docs/tgz-/345/256/211/350/243/205/350/257/264/346/230/216.md +53 -0
- package/docs//350/277/233/345/261/225/344/270/216/345/276/205/345/212/236.md +804 -0
- package/index.js +102 -0
- package/package.json +52 -6
- package/scripts/_panel_smoke_v299.py +27 -0
- package/scripts/_smoke_v299.py +31 -0
- package/scripts/_sync_trae_cn.py +34 -0
- package/scripts/assembler.py +197 -0
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +53 -0
- package/scripts/astrbot_plugin_lingshu/PATCH_NOTES.md +28 -0
- package/scripts/astrbot_plugin_lingshu/README.md +58 -0
- package/scripts/astrbot_plugin_lingshu/_conf_schema.json +92 -0
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +2605 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +698 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +45 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +37 -0
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +379 -0
- package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +5616 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/commands.md +243 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/mechanics.md +610 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/models.md +52 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/panel-api.md +198 -0
- package/scripts/astrbot_plugin_lingshu/engine/docs/recovery.md +90 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_panel_smoke_v299.py +27 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_smoke_v299.py +31 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_sync_trae_cn.py +34 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/assembler.py +197 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/behavior_baseline.py +280 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/bench.py +192 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/brain.py +46 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/check_pressure_sync.py +55 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/check_tag.py +215 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/checkpoint.py +165 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +4867 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/context_ab.py +260 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +2028 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +1137 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/emotion.py +1180 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/failures.py +107 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/fallback_embed.py +114 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/growth.py +4745 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/growth_parts.py +263 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/insights.py +131 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/make_dsh_tgz.py +92 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_dialogue.py +242 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_embed.py +159 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_lesson.py +160 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_score.py +210 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_topic.py +177 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +4417 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/npm_prepack.py +31 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_build.py +115 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_cloud.py +609 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +2703 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panels.py +591 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +2309 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/retriever.py +195 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/safety.py +368 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_import.py +736 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_rag.py +228 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_summary.py +123 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/summarizer.py +252 -0
- package/scripts/astrbot_plugin_lingshu/main.py +703 -0
- package/scripts/astrbot_plugin_lingshu/make_checksums.py +87 -0
- package/scripts/astrbot_plugin_lingshu/metadata.yaml +7 -0
- package/scripts/behavior_baseline.py +280 -0
- package/scripts/bench.py +192 -0
- package/scripts/brain.py +46 -0
- package/scripts/check_pressure_sync.py +55 -0
- package/scripts/check_tag.py +215 -0
- package/scripts/check_version.py +265 -0
- package/scripts/checkpoint.py +165 -0
- package/scripts/cli.py +4867 -0
- package/scripts/context_ab.py +260 -0
- package/scripts/core.py +2028 -0
- package/scripts/dream.py +1137 -0
- package/scripts/emotion.py +1180 -0
- package/scripts/failures.py +107 -0
- package/scripts/fallback_embed.py +114 -0
- package/scripts/growth.py +4745 -0
- package/scripts/growth_parts.py +263 -0
- package/scripts/hooks/pre-commit +18 -0
- package/scripts/hooks/pre-push +20 -0
- package/scripts/insights.py +131 -0
- package/scripts/make_dsh_tgz.py +92 -0
- package/scripts/make_manifest.py +254 -0
- package/scripts/make_plugin_zip.py +88 -0
- package/scripts/mem_dialogue.py +242 -0
- package/scripts/mem_embed.py +159 -0
- package/scripts/mem_lesson.py +160 -0
- package/scripts/mem_score.py +210 -0
- package/scripts/mem_topic.py +177 -0
- package/scripts/memory.py +4417 -0
- package/scripts/npm_prepack.py +31 -0
- package/scripts/panel_build.py +115 -0
- package/scripts/panel_cloud.py +609 -0
- package/scripts/panel_server.py +2703 -0
- package/scripts/panels.py +591 -0
- package/scripts/persona.py +2309 -0
- package/scripts/recreate_push.ps1 +117 -0
- package/scripts/retriever.py +195 -0
- package/scripts/run_tests.py +91 -0
- package/scripts/safety.py +368 -0
- package/scripts/session_import.py +736 -0
- package/scripts/session_rag.py +228 -0
- package/scripts/session_summary.py +123 -0
- package/scripts/summarizer.py +252 -0
- package/scripts/sync_engine.py +139 -0
- package/scripts/update_dsh_copy.ps1 +107 -0
- package/start-panel.bat +60 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# dsh-lingshu-startup · 灵枢启动注入插件
|
|
2
|
+
|
|
3
|
+
把"模型自觉读 SKILL.md"变成"机制自动注入":每个 DSH 新会话(`agent/created`,
|
|
4
|
+
source ∈ startup / resume)时自动运行灵枢的 `brain.py startup`,把人格、记忆、
|
|
5
|
+
工作记忆、行为指引在 `agent/pre-step` 钩子里追加进当步 messages,直接进入模型
|
|
6
|
+
上下文(不走 inbox,实测 inbox 三条路都有坑)。
|
|
7
|
+
|
|
8
|
+
## 为什么要有这个插件
|
|
9
|
+
|
|
10
|
+
DSH 的技能 catalog 只把 name + description 给模型,正文要不要读完全凭模型自觉。
|
|
11
|
+
没有任何机制保证下一次会话的模型一定会加载灵枢。这个插件补上最后一环:
|
|
12
|
+
**不依赖模型自觉,会话一开始记忆就在上下文里。**
|
|
13
|
+
|
|
14
|
+
## 目录约定
|
|
15
|
+
|
|
16
|
+
`cordis-plugin/` 下**一个插件一个子目录**——`lingshu-startup/` 就是本插件,
|
|
17
|
+
后续新增插件(如定时做梦、每日摘要)各自建子目录,互不干扰,方便单独
|
|
18
|
+
`pnpm pack` 与单独装进 profile。
|
|
19
|
+
|
|
20
|
+
## 安装(本机)
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
# 1) 确认灵枢已全局化(~/.dsh/skills/lingshu/scripts/brain.py 存在)
|
|
24
|
+
# 2) 把本插件子目录打包安装进 DSH profile:
|
|
25
|
+
cd cordis-plugin/lingshu-startup
|
|
26
|
+
pnpm pack # 得到 dsh-lingshu-startup-0.1.0.tgz
|
|
27
|
+
# 在 DSH 插件市场/配置里安装该 tgz(或按 DSH 文档把包放进 profile 的 node_modules
|
|
28
|
+
# 并在 profile package.json 的 dsh.profile.bundles 里加 "dsh-lingshu-startup")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
发布形态推荐 tgz(实体安装,解析基准在 profile 下,最稳)。
|
|
32
|
+
**开发迭代**:本插件已零外部依赖(见下节),`dsh plugin add <目录>` / link 安装
|
|
33
|
+
也能工作——改 `plugin.js` 后重启 DSH 即生效,不必每次 `pnpm pack`。
|
|
34
|
+
|
|
35
|
+
## 实测修复记录(为什么现在的写法长这样)
|
|
36
|
+
|
|
37
|
+
三个真实踩坑,每条都有事件日志/报错坐实,别"优化"回去:
|
|
38
|
+
|
|
39
|
+
| 问题 | 现象 | 修法 |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `inject()` 写 `next-step` 队列 | 注入发生了,但 `turn/start` 时被清空(`agent/inbox/spliced removed=1`),模型永远看不到 | 换投递通道(见下) |
|
|
42
|
+
| `send(msg,'next-turn')` 进待发送队列 | send 语义是"发一条消息"——UI 上排队,模型仍看不到 | 换投递通道(见下) |
|
|
43
|
+
| runtime-context 有"取代"语义 | DSH 运行时快照声明会 supersede 之前的 runtime-context 消息,注入可能被顺手清掉 | `source` 改 **`skill-invocation` + `form:'instructions'`** |
|
|
44
|
+
| link 安装时内置包解析失败 | `Cannot find package '@deepseek-ai/schemastery'`(模块真实路径在技能目录,向上找不到 node_modules)→ cordis **静默跳过插件** | 去掉 schemastery,配置改 **`DEFAULTS + mergeConfig`** 普通对象,零外部依赖 |
|
|
45
|
+
|
|
46
|
+
**最终投递通道:`agent/pre-step` 钩子**(前三种 inbox 方案全部实测失败后采用)。
|
|
47
|
+
`agent/created` 里只跑 startup 并把文本存进 pending Map;`agent/pre-step`
|
|
48
|
+
(每步构建 messages 之后、发起请求之前)**先 `await next()`**(保住其他 wrapping
|
|
49
|
+
监听器的 `startsRequestSeries`/batch 语义),再把文本追加进 `enter` 分支的
|
|
50
|
+
messages。这条通道不排队、不依赖 source 的渲染规则、不经过 inbox 消费逻辑——
|
|
51
|
+
`enter` 分支的 messages 就是模型看到的。`pending.delete(agent)` 保证只注入一次。
|
|
52
|
+
|
|
53
|
+
另有一条 ESM 链接期坑:`index.js` re-export 了 `plugin.js` 已删除的 `Config`,
|
|
54
|
+
`SyntaxError: does not provide an export named 'Config'` 在模块链接期就炸、同样
|
|
55
|
+
被静默吞掉——**re-export 的名字必须与导出方严格对齐**,且 `node --check` 查不出
|
|
56
|
+
这类错误,验证要用动态 `import()`。
|
|
57
|
+
|
|
58
|
+
## ⚠️ 开发调试别用 `link:` 安装(踩过的坑)
|
|
59
|
+
|
|
60
|
+
`link:` 安装会把模块的真实路径留在技能目录,Node 从那里向上找 `node_modules`
|
|
61
|
+
一路到盘符根——找不到 DSH 内置包,`import` 失败 → cordis **静默跳过整个插件**
|
|
62
|
+
(UI 里装了、bundles 里有、配置树里也有,就是不动)。实体 tgz 安装(如上)的
|
|
63
|
+
解析基准在 profile 下,没这个问题。
|
|
64
|
+
|
|
65
|
+
本插件因此**刻意零外部依赖**:不 import `@deepseek-ai/schemastery`(详见上节
|
|
66
|
+
实测记录),配置用普通对象 `DEFAULTS` + `mergeConfig` 合并(默认值 budget 1400 /
|
|
67
|
+
timeoutMs 3000 / injectResume true)。这样 link 安装也能工作、改 `plugin.js`
|
|
68
|
+
即时生效,不必每次重打包——只是别忘了 Host 仍不会热加载 bundle(见下节),
|
|
69
|
+
改完还是要重启 DSH。
|
|
70
|
+
|
|
71
|
+
排错技巧:desktop profile 被 Electron 独占,无法直连时,可用
|
|
72
|
+
`dsh --profile probe` 开临时 profile 把静默失败变成 stderr 显式错误;
|
|
73
|
+
`--dump-config` 能看到插件在配置树里 → 配置没问题 → 问题必然在 import。
|
|
74
|
+
|
|
75
|
+
## ⚠️ 装完必须重启 DSH(Host 不会热加载 bundle)
|
|
76
|
+
|
|
77
|
+
**Host 的插件树(cordis profile bundles)是主进程启动时一次性组合的**——装完/改完
|
|
78
|
+
插件不重启,新开的会话跑的还是旧 Host,它压根不知道有这个插件。实测踩坑:
|
|
79
|
+
12:11 启动 Host,12:22 装插件,之后所有新会话都只有 skill catalog 摘要、没有注入。
|
|
80
|
+
|
|
81
|
+
对照记忆(为什么 skill 能热更新而插件不能):
|
|
82
|
+
|
|
83
|
+
| | 生效时机 | 原因 |
|
|
84
|
+
|---|---|---|
|
|
85
|
+
| skill catalog | **实时热更新** | skill-filesystem 用 chokidar 监视技能目录,运行时感知增删 |
|
|
86
|
+
| cordis 插件 | **必须重启 Host** | bundle 组合在主进程启动时一次性完成 |
|
|
87
|
+
|
|
88
|
+
(`dshmarket` 不出现在设置里也是同一个原因——它是 bundle,不是 skill。)
|
|
89
|
+
|
|
90
|
+
### 装完后的验证清单
|
|
91
|
+
|
|
92
|
+
1. **完全退出并重启 DSH**(主进程重启,不是只关窗口/新开会话)。
|
|
93
|
+
2. 设置里能看见 `dsh-lingshu-startup`(看不见 = bundle 没挂上:检查 profile
|
|
94
|
+
`package.json` 的 `dsh.profile.bundles` 是否含 `dsh-lingshu-startup`,以及
|
|
95
|
+
node_modules 里包是否存在)。
|
|
96
|
+
3. 开一个新会话,随便说句话——上下文里应出现灵枢 startup 注入的人格/记忆段。
|
|
97
|
+
4. 没注入时按下面排错(插件日志会 warn 一次,不刷屏):
|
|
98
|
+
- `未找到 brain.py` → 灵枢没全局化,先 `growth.py install lingshu`;
|
|
99
|
+
- `startup 未产出注入文本` → python 问题:本机 PATH 只有 python.cmd shim
|
|
100
|
+
(如 ServBay)时 cmd 兜底也救不了,确认 DSH 自带运行时
|
|
101
|
+
`~/.dsh/dsh-runtimes/dsh-primary-runtime/dependencies/python/python.exe`
|
|
102
|
+
存在(探测优先命中它),或在配置里手填 `python`(exe 全路径);
|
|
103
|
+
- 都没有 warn 也没注入 → 插件没挂上,回到第 2 步。
|
|
104
|
+
|
|
105
|
+
## 配置项
|
|
106
|
+
|
|
107
|
+
| 项 | 默认 | 说明 |
|
|
108
|
+
|---|---|---|
|
|
109
|
+
| brainPy | 自动探测 | brain.py 绝对路径;留空则依次找 `~/.dsh/skills/lingshu`、`~/.trae-cn/skills/lingshu` |
|
|
110
|
+
| python | 自动探测 | **python.exe 绝对路径**。spawn 不解析 PATHEXT(PATH 里只有 python.cmd 的机器上 `spawn('python')` 直接 ENOENT),留空则优先探测 DSH 自带运行时 `<DSH_HOME>/dsh-runtimes/dsh-primary-runtime/dependencies/python/python.exe`,都没找到退回 `ComSpec` 让 cmd 解析 `.cmd` |
|
|
111
|
+
| budget | `1400` | startup 注入 token 预算 |
|
|
112
|
+
| timeoutMs | `3000` | 子进程超时;超时/失败**静默放行**,绝不阻塞会话(`agent/created` 是串行事件,默认值压到 3s 免拖慢新会话) |
|
|
113
|
+
| injectResume | `true` | 续聊(resume)的会话是否也注入;`clear` / `compact` 一律不注入(同会话上下文重建,重复注入浪费 token) |
|
|
114
|
+
|
|
115
|
+
## 数据安全
|
|
116
|
+
|
|
117
|
+
- 只读语义:`brain.py startup` 本身只读记忆库(仅更新 last_startup 时间戳);
|
|
118
|
+
HOME 不可写时 brain.py 自动只读降级照样注入。
|
|
119
|
+
- 失败宽容:灵枢没装、Python 不在、超时——降级跳过会话照常开始;配置类失败会
|
|
120
|
+
`ctx.logger.warn` 打一次日志(不重复刷屏),不静默吞错。监听器有顶层兜底,
|
|
121
|
+
任何异常都不冒泡(serial `agent/created` 失败会回滚会话创建,绝不允许)。
|
|
122
|
+
- 注入身份:消息 `source` 用 `runtime-context`,不冒充用户说的话。
|
|
123
|
+
- 与技能更新解耦:本插件不复制 brain.py,永远调用全局技能目录里的最新版。
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-lingshu-startup",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "灵枢记忆插件:会话启动时自动运行 lingshu 的 brain.py startup,把人格/记忆/待办注入每个新会话,不依赖模型自觉读 SKILL.md。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"plugin.js",
|
|
10
|
+
"cordis.patch.yml"
|
|
11
|
+
],
|
|
12
|
+
"dsh": {
|
|
13
|
+
"bundle": {
|
|
14
|
+
"patch": "./cordis.patch.yml"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"config": {
|
|
18
|
+
"brainPy": "运行 brain.py 的绝对路径;留空则自动从 ~/.dsh/skills/lingshu 或 ~/.trae-cn/skills/lingshu 探测",
|
|
19
|
+
"python": "python.exe 绝对路径;留空自动探测 DSH 自带运行时等位置(spawn 不解析 PATHEXT,PATH 只有 python.cmd 时必须留空走探测或写 exe 全路径)",
|
|
20
|
+
"budget": "startup 注入的 token 预算,默认 1400",
|
|
21
|
+
"timeoutMs": "startup 子进程超时毫秒数,默认 3000(agent/created 是串行事件,太久会拖慢新会话);超时/失败静默放行,不阻塞会话",
|
|
22
|
+
"injectResume": "resume 来源的会话(续聊)是否也注入,默认 true;clear/compact 一律不注入"
|
|
23
|
+
},
|
|
24
|
+
"zeroDeps": "刻意零外部依赖:不 import @deepseek-ai/schemastery(link: 安装时该内置包从插件真实路径解析不到,import 失败会被 cordis 静默跳过)。配置以普通对象 DEFAULTS 提供,用户配置经 mergeConfig 合并。"
|
|
25
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// 灵枢 · DSH/Cordis 启动注入插件
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:DSH 的技能 catalog 只把 name+description 给模型,正文要不要读
|
|
4
|
+
// 全凭模型自觉。本插件把"自觉"变成"机制"——每个新会话(agent/created,
|
|
5
|
+
// source ∈ startup/resume)时自动跑灵枢的 `brain.py startup`,把人格、记忆、
|
|
6
|
+
// 待办、行为指引注入会话上下文,模型想忘都忘不掉。
|
|
7
|
+
//
|
|
8
|
+
// 事件与注入(对齐 DSH 真实 API):
|
|
9
|
+
// - 监听 `agent/created`(serial 事件,监听器会被 await,失败会回滚会话创建——
|
|
10
|
+
// 所以本插件任何路径都不允许抛异常),payload 为
|
|
11
|
+
// { agent, source: 'startup'|'resume'|'clear'|'compact', signal? }。
|
|
12
|
+
// 只对 startup/resume 注入;clear/compact 是同会话重建上下文,不重复注入。
|
|
13
|
+
// - 投递用 agent/pre-step 钩子:agent/created 里只跑 startup 并把文本存进
|
|
14
|
+
// pending Map;pre-step(构建 messages 之后、请求之前)await next() 后把文本
|
|
15
|
+
// 追加进 enter 分支的 messages——不排队、不经过 inbox 消费逻辑,必然进模型
|
|
16
|
+
// 上下文。此前试过 inject(写 next-step,turn/start 清空丢注入)与
|
|
17
|
+
// send('next-turn')(进待发送队列,模型看不到),都实测失败。
|
|
18
|
+
//
|
|
19
|
+
// 数据安全:本插件只读记忆库(startup 内部只读+写 last_startup),不碰
|
|
20
|
+
// 技能目录;brain.py 自身保证只读降级(HOME 不可写时照样注入)。
|
|
21
|
+
|
|
22
|
+
import { spawn } from 'node:child_process'
|
|
23
|
+
import { existsSync, rmSync, writeFileSync } from 'node:fs'
|
|
24
|
+
import { homedir } from 'node:os'
|
|
25
|
+
import { delimiter, dirname, join } from 'node:path'
|
|
26
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
27
|
+
import { fileURLToPath } from 'node:url'
|
|
28
|
+
|
|
29
|
+
// 刻意零外部依赖:不 import @deepseek-ai/schemastery。原因——link: 安装时模块
|
|
30
|
+
// 真实路径留在技能目录,Node 从那里向上找 node_modules 一路到盘符根都找不到
|
|
31
|
+
// DSH 内置包,import 失败 → cordis 静默跳过整个插件(UI 里装了却不动)。
|
|
32
|
+
// 去掉依赖后 link 安装也能工作,改 plugin.js 即时生效,不必每次重打包。
|
|
33
|
+
|
|
34
|
+
export const name = 'dsh-lingshu-startup'
|
|
35
|
+
|
|
36
|
+
/** 注入消息的 source.name:与 cron 的 'lingshu-cron'、im 的 'lingshu-im' 区分开。 */
|
|
37
|
+
const INJECT_SOURCE = 'lingshu-startup'
|
|
38
|
+
|
|
39
|
+
// 本插件文件在 <包根>/cordis-plugin/lingshu-startup/plugin.js,
|
|
40
|
+
// 往上一级是 cordis-plugin/,再上一级就是包根(含 scripts/brain.py 与 SKILL.md)。
|
|
41
|
+
const PLUGIN_DIR = dirname(fileURLToPath(import.meta.url))
|
|
42
|
+
const PACKAGE_ROOT = join(PLUGIN_DIR, '..', '..')
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 把"找不到 brain.py / python"这类静默失败写进 ~/.dsh/lingshu-diag.json。
|
|
46
|
+
* 为什么需要:插件里所有失败都必须吞掉(事件串行、抛异常会回滚会话),
|
|
47
|
+
* 结果就是"记忆不录入了"却没有任何痕迹可查——2026-11-20 那次排障就卡在这。
|
|
48
|
+
*/
|
|
49
|
+
export function writeDiag(kind, detail) {
|
|
50
|
+
try {
|
|
51
|
+
const base = process.env.APP_DSH || join(homedir(), '.dsh')
|
|
52
|
+
const payload = {
|
|
53
|
+
ts: new Date().toISOString(),
|
|
54
|
+
plugin: 'lingshu-startup',
|
|
55
|
+
kind,
|
|
56
|
+
detail: detail && typeof detail === 'object' ? detail : { message: String(detail ?? '') },
|
|
57
|
+
}
|
|
58
|
+
writeFileSync(join(base, 'lingshu-diag.json'), JSON.stringify(payload, null, 2), 'utf8')
|
|
59
|
+
} catch {}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 默认配置(纯普通对象,零依赖)。DSH 对导出 Config 的插件会喂用户配置,
|
|
63
|
+
// 不导出 Config 时喂默认空对象——两种情况本插件都能工作。
|
|
64
|
+
const DEFAULTS = {
|
|
65
|
+
brainPy: '', // brain.py 绝对路径;留空自动探测 ~/.dsh/skills/lingshu、~/.trae-cn/skills/lingshu
|
|
66
|
+
python: '', // python.exe 绝对路径;留空自动探测 DSH 自带运行时等位置(spawn 不解析 PATHEXT)
|
|
67
|
+
budget: 1400, // startup 注入 token 预算
|
|
68
|
+
timeoutMs: 3000, // startup 子进程超时;agent/created 是串行事件,超时太久拖慢新会话
|
|
69
|
+
resumeBudget: 400, // v3.59:resume 用极简版预算(人格+待办+水位,约 640 字符)。
|
|
70
|
+
// 为什么:会话历史里已有一份完整记忆块,resume 再注入一整份
|
|
71
|
+
// 就是同一段上下文出现两遍(实测 1214 tokens x 2)。
|
|
72
|
+
injectResume: true, // resume 来源(续聊)是否也注入
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 合并用户配置与默认值(用户值非空/存在才覆盖) */
|
|
76
|
+
function mergeConfig(user) {
|
|
77
|
+
const merged = { ...DEFAULTS }
|
|
78
|
+
if (user && typeof user === 'object') {
|
|
79
|
+
for (const k of Object.keys(DEFAULTS)) {
|
|
80
|
+
const v = user[k]
|
|
81
|
+
if (v !== undefined && v !== null && v !== '') merged[k] = v
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return merged
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 依次探测常见安装位置的 brain.py */
|
|
88
|
+
async function findBrainPy(configured, ctx) {
|
|
89
|
+
if (configured) return existsSync(configured) ? configured : null
|
|
90
|
+
// ① 先看本包自带:插件就在 <包根>/cordis-plugin/lingshu-startup/ 里,
|
|
91
|
+
// ../../scripts/brain.py 一定和它同包发布。这一条不依赖任何宿主状态,
|
|
92
|
+
// 也不依赖"技能注册表带路径"(v3.53 之前正是缺了这一条:注册 runtime 技能
|
|
93
|
+
// 时没给 resourceBase/path,子插件就永远找不到 brain.py,整条自动注入/
|
|
94
|
+
// 即记即存/天级任务全部静默停摆)。
|
|
95
|
+
for (const p of brainPyCandidates()) {
|
|
96
|
+
if (existsSync(p)) return p
|
|
97
|
+
}
|
|
98
|
+
// ② 再问技能注册表(装了全局技能副本时更准;字段名跨版本不同,逐个试)
|
|
99
|
+
try {
|
|
100
|
+
const registry = ctx?.get?.('skills') ?? ctx?.skills
|
|
101
|
+
let items = registry?.list?.()
|
|
102
|
+
if (items?.then) items = await items
|
|
103
|
+
const hit = Array.isArray(items) ? items.find((s) => s?.name === 'lingshu') : null
|
|
104
|
+
if (hit) {
|
|
105
|
+
const bases = [
|
|
106
|
+
[hit.path, true], [hit.location, true],
|
|
107
|
+
[hit?.resourceBase?.path, false], // DSH 的 runtime 技能可带资源根目录
|
|
108
|
+
[hit.dir, false], [hit.baseDir, false], [hit.directory, false],
|
|
109
|
+
]
|
|
110
|
+
for (const [raw, isFile] of bases) {
|
|
111
|
+
if (typeof raw !== 'string' || !raw) continue
|
|
112
|
+
const root = isFile ? dirname(raw) : raw
|
|
113
|
+
const p = join(root, 'scripts', 'brain.py')
|
|
114
|
+
if (existsSync(p)) return p
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} catch (e) {
|
|
118
|
+
// 技能注册表探测失败不该静默——留一条日志方便排障(但不在群/会话里打扰)
|
|
119
|
+
try { ctx?.logger?.warn?.('lingshu-startup: 技能注册表探测异常,走环境变量/默认路径兜底', e) } catch {}
|
|
120
|
+
}
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 有可能藏着 brain.py 的绝对路径清单(纯函数,便于测试与排障打印) */
|
|
125
|
+
export function brainPyCandidates() {
|
|
126
|
+
const out = [join(PACKAGE_ROOT, 'scripts', 'brain.py')]
|
|
127
|
+
const envDir = (process.env.LINGSHU_SKILL_DIR || '').trim()
|
|
128
|
+
if (envDir) out.push(join(envDir, 'scripts', 'brain.py'))
|
|
129
|
+
const home = homedir()
|
|
130
|
+
out.push(
|
|
131
|
+
join(home, '.dsh', 'skills', 'lingshu', 'scripts', 'brain.py'),
|
|
132
|
+
join(home, '.trae-cn', 'skills', 'lingshu', 'scripts', 'brain.py'),
|
|
133
|
+
)
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 有可能可用的 python 可执行文件清单(纯函数,便于测试与排障打印) */
|
|
138
|
+
export function pythonCandidates() {
|
|
139
|
+
const home = homedir()
|
|
140
|
+
const out = []
|
|
141
|
+
for (const app of [process.env.APP_DSH, join(home, '.dsh')].filter(Boolean)) {
|
|
142
|
+
out.push(join(app, 'app-runtimes', 'app-primary-runtime', 'dependencies', 'python', 'python.exe'))
|
|
143
|
+
out.push(join(app, 'runtimes', 'app-primary-runtime', 'dependencies', 'python', 'python.exe'))
|
|
144
|
+
}
|
|
145
|
+
// PATH 里的 python.exe / python3.exe(ServBay、Store、conda 等布局都能命中)。
|
|
146
|
+
// 刻意不选 .cmd shim:spawn 走 CreateProcess 不解析 PATHEXT。
|
|
147
|
+
for (const dir of (process.env.PATH || '').split(delimiter)) {
|
|
148
|
+
if (!dir) continue
|
|
149
|
+
out.push(join(dir, 'python.exe'), join(dir, 'python3.exe'))
|
|
150
|
+
}
|
|
151
|
+
out.push(
|
|
152
|
+
join(home, 'AppData', 'Local', 'Programs', 'Python', 'python.exe'),
|
|
153
|
+
'C:\\Python313\\python.exe',
|
|
154
|
+
'C:\\Python312\\python.exe',
|
|
155
|
+
'C:\\Program Files\\Python313\\python.exe',
|
|
156
|
+
)
|
|
157
|
+
return out
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 探测可用的 python.exe。spawn 走 CreateProcess,不解析 PATHEXT——
|
|
162
|
+
* PATH 里只有 python.cmd 的机器上 spawn('python') 直接 ENOENT。
|
|
163
|
+
* 优先级:用户配置 > DSH 自带运行时(确定的 exe)> 常见安装位置 > 系统常见位置。
|
|
164
|
+
* 都没命中返回 null(由调用方走 cmd 兜底)。
|
|
165
|
+
*/
|
|
166
|
+
function findPythonExe(configured) {
|
|
167
|
+
if (configured) return existsSync(configured) ? configured : null
|
|
168
|
+
for (const p of pythonCandidates()) {
|
|
169
|
+
if (existsSync(p)) return p
|
|
170
|
+
}
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 跑 brain.py startup,返回注入文本;失败返回 null(绝不抛异常、绝不阻塞会话)。
|
|
176
|
+
* 找不到 python.exe 时退回 ComSpec 让 cmd 解析 PATHEXT(python.cmd 也能跑)。
|
|
177
|
+
*/
|
|
178
|
+
function runStartup(pythonExe, pythonCmdFallback, brainPy, budget, timeoutMs) {
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
let settled = false
|
|
181
|
+
const done = (v) => { if (!settled) { settled = true; resolve(v) } }
|
|
182
|
+
const args = [brainPy, 'startup', '--budget', String(budget)]
|
|
183
|
+
let child
|
|
184
|
+
try {
|
|
185
|
+
if (pythonExe) {
|
|
186
|
+
child = spawn(pythonExe, args, {
|
|
187
|
+
windowsHide: true,
|
|
188
|
+
env: { ...process.env, PYTHONIOENCODING: 'utf-8' },
|
|
189
|
+
// 显式把工作目录挪到用户主目录:绝不继承宿主 cwd。宿主 cwd 若在插件
|
|
190
|
+
// 目录内,Windows 会因"目录被进程占用"拒绝 pnpm 的目录 rename,
|
|
191
|
+
// 导致市场更新(remove+add)报"文件被占用、请退出 DSH"。
|
|
192
|
+
cwd: homedir(),
|
|
193
|
+
// 不用 spawn 的 timeout 选项:它只杀直接子进程,见下方手动兜底说明
|
|
194
|
+
})
|
|
195
|
+
} else {
|
|
196
|
+
// 兜底:让 cmd 去解析 .cmd(/d 关 autorun,/s 规范引号解析,/c 执行后退出)。
|
|
197
|
+
// 注意命令行必须带上 python 命令本身(pythonExe 没找到时用裸 'python',
|
|
198
|
+
// 由 cmd 按 PATHEXT 解析成 python.cmd)——只拼 brain.py 会依赖 .py 文件关联。
|
|
199
|
+
const comspec = process.env.ComSpec ?? 'cmd.exe'
|
|
200
|
+
const pythonCmd = pythonCmdFallback || 'python'
|
|
201
|
+
const cmdline = [`"${pythonCmd}"`, ...args.map((a) => `"${a}"`)].join(' ')
|
|
202
|
+
child = spawn(comspec, ['/d', '/s', '/c', cmdline], {
|
|
203
|
+
windowsHide: true,
|
|
204
|
+
windowsVerbatimArguments: true, // 命令行已手工加引号,阻止 Node 再转义一层
|
|
205
|
+
env: { ...process.env, PYTHONIOENCODING: 'utf-8' },
|
|
206
|
+
cwd: homedir(), // 同上:不把宿主 cwd(可能在插件目录内)占给子进程
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
} catch {
|
|
210
|
+
return done(null)
|
|
211
|
+
}
|
|
212
|
+
let out = ''
|
|
213
|
+
child.stdout?.on('data', (d) => { out += d.toString('utf8') })
|
|
214
|
+
child.stderr?.resume() // 必须 drain:traceback 超 64KB 会塞满管道缓冲,子进程写阻塞永不退出
|
|
215
|
+
child.on('error', () => done(null))
|
|
216
|
+
// 手动超时兜底(不靠 spawn 的 timeout 选项,它只杀直接子进程):
|
|
217
|
+
// taskkill /T /F 杀整棵进程树——cmd 兜底分支里直接子进程是 cmd.exe,
|
|
218
|
+
// 只杀它会留下 python.exe 孙进程残留。
|
|
219
|
+
const killer = setTimeout(() => {
|
|
220
|
+
try {
|
|
221
|
+
if (child.pid) {
|
|
222
|
+
const tk = spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' })
|
|
223
|
+
tk.on('error', () => {}) // 无 taskkill(非 Windows)时静默,下面还有 child.kill() 托底
|
|
224
|
+
}
|
|
225
|
+
} catch {}
|
|
226
|
+
try { child.kill() } catch {}
|
|
227
|
+
done(null)
|
|
228
|
+
}, timeoutMs + 500)
|
|
229
|
+
killer.unref?.()
|
|
230
|
+
child.on('close', (code) => {
|
|
231
|
+
clearTimeout(killer) // 正常退出清掉兜底定时器,别留着空转
|
|
232
|
+
// 返回码 0=正常注入;10=只读降级注入(照样有效);2=未初始化(跳过)
|
|
233
|
+
done(code === 0 || code === 10 ? out.trim() : null)
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function apply(ctx, config) {
|
|
239
|
+
// v3.23:重复挂载守卫。profile 同时启用独立包与根包时,同一进程会出现两份 startup,
|
|
240
|
+
// 每会话注入两遍人格/记忆(白烧 ~3k token,且两份可能来自不同版本引擎)。
|
|
241
|
+
if (globalThis.__lingshuStartupApplied) {
|
|
242
|
+
try { ctx.logger?.warn?.('lingshu-startup: 已有一份灵枢 startup 在运行,跳过重复挂载') } catch {}
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
globalThis.__lingshuStartupApplied = true
|
|
246
|
+
const cfg = mergeConfig(config)
|
|
247
|
+
// pending:agent -> 注入文本。agent/created 只负责把文本存进来,
|
|
248
|
+
// 真正投递在 agent/pre-step 里做(见下)。
|
|
249
|
+
const pending = new Map()
|
|
250
|
+
// v3.59:每个 agent 已注入过的内容指纹(sha256 前 16 位)。
|
|
251
|
+
// 解决同一对话里注入第二份一模一样内容——resume、agent 重建、
|
|
252
|
+
// 宿主重复派发 agent/created 都可能触发重注入。同一份内容只进一次上下文,
|
|
253
|
+
// 这也正是提示词缓存友好的做法(只在末尾追加、不重复历史)。
|
|
254
|
+
const injectedDigests = new Map()
|
|
255
|
+
// 配置类失败只告警一次:静默吞掉配置错误会让插件"看似在工作、实际没注入"
|
|
256
|
+
let warnedOnce = false
|
|
257
|
+
|
|
258
|
+
ctx.on('agent/created', async (payload) => {
|
|
259
|
+
try { // 顶层兜底:serial 监听器 reject 会回滚会话创建,这里绝不允许冒泡
|
|
260
|
+
const { agent, source } = payload || {}
|
|
261
|
+
if (!agent) return
|
|
262
|
+
// 白名单:只有新开会话(startup)与续聊(resume)注入;
|
|
263
|
+
// clear / compact 是同会话上下文重建,重复注入只会浪费 token。
|
|
264
|
+
if (source !== 'startup' && source !== 'resume') return
|
|
265
|
+
if (source === 'resume' && !cfg.injectResume) return
|
|
266
|
+
if (pending.has(agent)) return
|
|
267
|
+
|
|
268
|
+
const brainPy = await findBrainPy(cfg.brainPy, ctx)
|
|
269
|
+
if (!brainPy) {
|
|
270
|
+
// 静默失败必须有痕迹:doctor / 面板 / 手工排障都读这个文件
|
|
271
|
+
writeDiag('brainPy-not-found', {
|
|
272
|
+
tried: brainPyCandidates(),
|
|
273
|
+
packageRoot: PACKAGE_ROOT,
|
|
274
|
+
configured: cfg.brainPy || null,
|
|
275
|
+
})
|
|
276
|
+
if (!warnedOnce) { warnedOnce = true; try { ctx.logger?.warn?.('lingshu-startup: 未找到 brain.py(未安装灵枢?),跳过注入') } catch {} }
|
|
277
|
+
return
|
|
278
|
+
}
|
|
279
|
+
const pythonExe = findPythonExe(cfg.python)
|
|
280
|
+
// v3.59:resume 走极简版预算——历史里已有完整记忆块,不必再来一份
|
|
281
|
+
const budget = source === 'resume'
|
|
282
|
+
? (Number(cfg.resumeBudget) > 0 ? Number(cfg.resumeBudget) : cfg.budget)
|
|
283
|
+
: cfg.budget
|
|
284
|
+
const text = await runStartup(pythonExe, cfg.python, brainPy, budget, cfg.timeoutMs)
|
|
285
|
+
if (!text) {
|
|
286
|
+
writeDiag('startup-no-output', {
|
|
287
|
+
brainPy,
|
|
288
|
+
pythonExe: pythonExe || null,
|
|
289
|
+
pythonConfigured: cfg.python || null,
|
|
290
|
+
pythonTried: pythonCandidates().slice(0, 12),
|
|
291
|
+
})
|
|
292
|
+
if (!warnedOnce) { warnedOnce = true; try { ctx.logger?.warn?.('lingshu-startup: brain.py startup 未产出注入文本(python 不在/超时/未初始化),跳过注入') } catch {} }
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
// 成功时清掉上一次的失败痕迹,避免旧 diag 一直误导排障
|
|
296
|
+
try {
|
|
297
|
+
const base = process.env.APP_DSH || join(homedir(), '.dsh')
|
|
298
|
+
rmSync(join(base, 'lingshu-diag.json'), { force: true })
|
|
299
|
+
} catch {}
|
|
300
|
+
// v3.59:内容指纹去重——同一份文本在同一 agent 只注入一次。
|
|
301
|
+
// 场景:resume、agent 重建、宿主重复派发 agent/created。不加这道闸,
|
|
302
|
+
// 同一个对话里会出现两份一样的记忆块(实测 1214 tokens x 2),
|
|
303
|
+
// 既费 token 又让提示词缓存从重复处开始失效。
|
|
304
|
+
const digest = createHash('sha256').update(text).digest('hex').slice(0, 16)
|
|
305
|
+
let seen = injectedDigests.get(agent)
|
|
306
|
+
if (seen === undefined) {
|
|
307
|
+
seen = new Set()
|
|
308
|
+
injectedDigests.set(agent, seen)
|
|
309
|
+
}
|
|
310
|
+
if (seen.has(digest)) {
|
|
311
|
+
// 已经注入过一模一样的内容:直接跳过,不占上下文(也算成功,清掉旧 diag)
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
seen.add(digest)
|
|
315
|
+
// 只存不投递:send/inject 走 inbox 都有生命周期坑(实测坐实),真正进模型
|
|
316
|
+
// 上下文要在 agent/pre-step 里追加 messages(见下)。
|
|
317
|
+
pending.set(agent, text)
|
|
318
|
+
} catch (e) {
|
|
319
|
+
// 任何意外都不能让监听器 reject(agent/created 失败会回滚会话创建)
|
|
320
|
+
try { ctx.logger?.warn?.('lingshu-startup: 未捕获异常', e) } catch {}
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
// 每步构建 messages 之后、发起请求之前:把注入文本追加进这批 messages。
|
|
325
|
+
// 这条通道不排队、不经过 inbox 消费逻辑——enter 分支的 messages 就是模型看到的。
|
|
326
|
+
ctx.on('agent/pre-step', async (payload, next) => {
|
|
327
|
+
try {
|
|
328
|
+
// 先 await next():让其他(wrapping)监听器先改完,保住它们的
|
|
329
|
+
// startsRequestSeries / batch 语义,再在结果上追加。
|
|
330
|
+
const decision = await next()
|
|
331
|
+
if (!decision || decision.kind !== 'enter') return decision
|
|
332
|
+
const { agent } = payload || {}
|
|
333
|
+
const text = agent && pending.get(agent)
|
|
334
|
+
if (!text) return decision
|
|
335
|
+
pending.delete(agent) // 只注入一次:不删的话每条 pre-step 都会重复塞
|
|
336
|
+
return {
|
|
337
|
+
...decision,
|
|
338
|
+
messages: [...decision.messages, {
|
|
339
|
+
id: randomUUID(),
|
|
340
|
+
role: 'user',
|
|
341
|
+
content: [{ type: 'text', text }],
|
|
342
|
+
// v3.59:用自己的 source.name,别和 cron 的 'lingshu-cron' 撞。
|
|
343
|
+
// 撞了会被 cron 的 pruneInjected 当成本插件自己的旧提醒删掉。
|
|
344
|
+
source: { kind: 'skill-invocation', name: INJECT_SOURCE, form: 'instructions' },
|
|
345
|
+
}],
|
|
346
|
+
}
|
|
347
|
+
} catch (e) {
|
|
348
|
+
try { ctx.logger?.warn?.('lingshu-startup: pre-step 注入失败', e) } catch {}
|
|
349
|
+
// next() 可能已经跑过,无法安全重放;异常只能吞掉,不能冒泡
|
|
350
|
+
}
|
|
351
|
+
})
|
|
352
|
+
}
|
package/cordis.patch.yml
ADDED