dsh-eplan 0.0.0-stage → 0.14.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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -3
  3. package/cordis.patch.yml +182 -0
  4. package/docs/INSTALL.md +86 -0
  5. package/docs/LESSONS.md +224 -0
  6. package/docs/interfaces.md +183 -0
  7. package/docs/merged-skills/eplan-setup.md +63 -0
  8. package/docs/merged-skills/eplan-troubleshooting.md +149 -0
  9. package/icon.png +0 -0
  10. package/icon.svg +13 -0
  11. package/knowledge/KNOWLEDGE.md +413 -0
  12. package/knowledge/symbols/BP.png +0 -0
  13. package/knowledge/symbols/CDPST.png +0 -0
  14. package/knowledge/symbols/CO.png +0 -0
  15. package/knowledge/symbols/FA3.png +0 -0
  16. package/knowledge/symbols/TLRO.png +0 -0
  17. package/knowledge/symbols/TLRU.png +0 -0
  18. package/knowledge/symbols/TOUL.png +0 -0
  19. package/knowledge/symbols/TOUR.png +0 -0
  20. package/knowledge/symbols/manifest.json +91 -0
  21. package/locale/en.json +6 -0
  22. package/locale/zh.json +6 -0
  23. package/native/bridge/build.ps1 +73 -0
  24. package/native/bridge/src/Json.cs +383 -0
  25. package/native/bridge/src/Program.cs +1133 -0
  26. package/native/host/build.ps1 +74 -0
  27. package/native/host/src/ActionsTool.cs +225 -0
  28. package/native/host/src/BoxTool.cs +196 -0
  29. package/native/host/src/CheckTool.cs +186 -0
  30. package/native/host/src/Context.cs +169 -0
  31. package/native/host/src/DarkScheme.cs +111 -0
  32. package/native/host/src/DiagnoseTool.cs +115 -0
  33. package/native/host/src/Dispatcher.cs +51 -0
  34. package/native/host/src/DrawTool.cs +316 -0
  35. package/native/host/src/Entry.cs +111 -0
  36. package/native/host/src/ExportTool.cs +213 -0
  37. package/native/host/src/InspectTool.cs +367 -0
  38. package/native/host/src/KnowledgeTool.cs +166 -0
  39. package/native/host/src/LookTool.cs +257 -0
  40. package/native/host/src/OpLog.cs +289 -0
  41. package/native/host/src/PageCreateTool.cs +521 -0
  42. package/native/host/src/PageOpenTool.cs +62 -0
  43. package/native/host/src/PagesTool.cs +95 -0
  44. package/native/host/src/Paths.cs +91 -0
  45. package/native/host/src/PlacementEditTool.cs +234 -0
  46. package/native/host/src/ProbePropsTool.cs +152 -0
  47. package/native/host/src/ProjectCreateTool.cs +176 -0
  48. package/native/host/src/ProjectTool.cs +167 -0
  49. package/native/host/src/PropWriter.cs +70 -0
  50. package/native/host/src/PropsTool.cs +257 -0
  51. package/native/host/src/StatusTool.cs +168 -0
  52. package/native/host/src/SymbolInfoTool.cs +144 -0
  53. package/native/host/src/SymbolInsertTool.cs +261 -0
  54. package/native/host/src/SymbolsTool.cs +94 -0
  55. package/native/host/src/TagLocTool.cs +108 -0
  56. package/native/script/EplanMcpBootstrap.cs +221 -0
  57. package/native/script/build-symbol-gallery.mjs +231 -0
  58. package/native/script/prepare-install.ps1 +57 -0
  59. package/native/script/prune.mjs +132 -0
  60. package/native/script/verify-bootstrap.ps1 +62 -0
  61. package/package.json +53 -4
  62. package/preset/bootstrap.js +498 -0
  63. package/preset/experience-nudge.js +47 -0
  64. package/preset/image-budget.js +175 -0
  65. package/preset/knowledge-tool.js +180 -0
  66. package/preset/mcp-link.js +100 -0
  67. package/preset/prompt-surface.js +76 -0
  68. package/preset/restrict-tools.js +141 -0
  69. package/preset/skill-docs.js +181 -0
  70. package/preset/video-digest.js +294 -0
  71. package/preset/video-tool.js +92 -0
  72. package/server/compact.mjs +623 -0
  73. package/server/edit-args.mjs +67 -0
  74. package/server/fuzzy.mjs +57 -0
  75. package/server/launch-decision.mjs +26 -0
  76. package/server/measure.mjs +320 -0
  77. package/server/pdf.mjs +185 -0
  78. package/server/pdf2png.ps1 +99 -0
  79. package/server/server.mjs +3797 -0
  80. package/server/video.mjs +857 -0
  81. package/setup.mjs +223 -0
  82. package/skills/eplan-annotations/SKILL.md +55 -0
  83. package/skills/eplan-drawing/SKILL.md +206 -0
  84. package/skills/eplan-project/SKILL.md +41 -0
  85. package/skills/eplan-review/SKILL.md +80 -0
  86. package/skills/eplan-symbols/SKILL.md +142 -0
  87. package/test/check-docs.mjs +106 -0
  88. package/test/foreground.ps1 +21 -0
  89. package/test/smoke-all.mjs +229 -0
@@ -0,0 +1,62 @@
1
+ # 校验引导脚本能不能被 EPLAN 的脚本编译器编过。
2
+ #
3
+ # 这一步很重要:EPLAN 自己的脚本编译器引用集是写死的(见 EplanEplApiScriptLogu.erx),
4
+ # 和普通 csc 编译完全不是一回事。本地先用一模一样的引用集编一遍,
5
+ # 就能在把脚本塞进 EPLAN 之前发现 CS0234 那类"命名空间不存在"的错误。
6
+ #
7
+ # 用法: powershell -NoProfile -ExecutionPolicy Bypass -File native\script\verify-bootstrap.ps1
8
+ $ErrorActionPreference = 'Stop'
9
+
10
+ $root = Split-Path -Parent $PSScriptRoot
11
+ $eplanBin = 'D:\EPLAN\Platform\2.9.4\Bin'
12
+ $csc = Join-Path $env:WINDIR 'Microsoft.NET\Framework64\v4.0.30319\csc.exe'
13
+ $script = Join-Path $PSScriptRoot 'EplanMcpBootstrap.cs'
14
+ $hostDll = Join-Path $root 'host\bin\EplanMcpHost.dll'
15
+ $tempDir = Join-Path $env:TEMP ('eplan-bootstrap-verify-' + [guid]::NewGuid().ToString('N'))
16
+ New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
17
+
18
+ try {
19
+ # 把 HostPath 换成真实路径,和安装时的做法一致。
20
+ $source = Get-Content -LiteralPath $script -Raw -Encoding UTF8
21
+ $source = $source.Replace('__HOST_DLL_PATH__', $hostDll)
22
+ $patched = Join-Path $tempDir 'EplanMcpBootstrap.cs'
23
+ [System.IO.File]::WriteAllText($patched, $source, (New-Object System.Text.UTF8Encoding($true)))
24
+
25
+ # EPLAN 脚本编译器的引用集,逐字抄自 EplanEplApiScriptLogu.erx 的字符串表。
26
+ $frameworkRefs = @(
27
+ 'mscorlib.dll'
28
+ 'System.dll'
29
+ 'System.Core.dll'
30
+ 'System.Drawing.dll'
31
+ 'System.XML.dll'
32
+ 'System.Xml.Linq.dll'
33
+ 'System.Windows.Forms.dll'
34
+ 'System.IO.Compression.FileSystem.dll'
35
+ ) | ForEach-Object {
36
+ Join-Path $env:WINDIR ('Microsoft.NET\Framework64\v4.0.30319\' + $_)
37
+ }
38
+ $eplanRefs = @(
39
+ 'Eplan.EplApi.Baseu.dll'
40
+ 'Eplan.EplApi.AFu.dll'
41
+ 'Eplan.EplApi.Guiu.dll'
42
+ 'Eplan.EplApi.RecorderToolsu.dll'
43
+ 'Eplan.EplApi.EServicesu.dll'
44
+ ) | ForEach-Object { Join-Path $eplanBin $_ }
45
+
46
+ $args = New-Object System.Collections.ArrayList
47
+ foreach ($a in @('-nologo', '-target:library', '-platform:x64', '-langversion:5')) { [void]$args.Add($a) }
48
+ [void]$args.Add('-out:' + (Join-Path $tempDir 'bootstrap-check.dll'))
49
+ foreach ($r in ($frameworkRefs + $eplanRefs)) {
50
+ if (Test-Path -LiteralPath $r) { [void]$args.Add('-r:' + $r) }
51
+ else { Write-Host "跳过(本机没有): $r" -ForegroundColor DarkYellow }
52
+ }
53
+ [void]$args.Add($patched)
54
+
55
+ Write-Host '用 EPLAN 脚本编译器的引用集编译引导脚本…'
56
+ & $csc $args.ToArray()
57
+ if ($LASTEXITCODE -ne 0) { throw "引导脚本编译失败(退出码 $LASTEXITCODE),EPLAN 里也会失败。" }
58
+ Write-Host 'OK 引导脚本可通过 EPLAN 脚本编译器' -ForegroundColor Green
59
+ }
60
+ finally {
61
+ Remove-Item -LiteralPath $tempDir -Recurse -Force -ErrorAction SilentlyContinue
62
+ }
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "dsh-eplan",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.14.0",
4
+ "description": "EPLAN Electric P8 drawing automation for DeepSeek Harness: an eplan preset whose only direct tool is run_code, 12 mcp__eplan__* tools that create pages, place symbols, draw and validate schematics, plus a video digest plugin that turns a screen recording into staged full-page frames.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "monta-6969",
8
+ "homepage": "https://github.com/monta-6969/dsh-eplan",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/monta-6969/dsh-eplan.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/monta-6969/dsh-eplan/issues"
15
+ },
16
+ "keywords": [
17
+ "deepseek-harness",
18
+ "dsh",
19
+ "dsh-plugin",
20
+ "eplan",
21
+ "eplan-electric-p8",
22
+ "cad",
23
+ "schematic",
24
+ "mcp",
25
+ "electrical"
26
+ ],
27
+ "os": [
28
+ "win32"
29
+ ],
30
+ "engines": {
31
+ "node": ">=20",
32
+ "dsh": ">=0.2.2-rc.1 <0.3.0-0"
33
+ },
34
+ "exports": {
35
+ "./cordis.patch.yml": "./cordis.patch.yml",
36
+ "./locale/*.json": "./locale/*.json",
37
+ "./icon": "./icon.svg",
38
+ "./preset/*.js": "./preset/*.js",
39
+ "./server/server.mjs": "./server/server.mjs",
40
+ "./server/video.mjs": "./server/video.mjs",
41
+ "./package.json": "./package.json"
42
+ },
43
+ "dsh": {
44
+ "bundle": {
45
+ "patch": "./cordis.patch.yml"
46
+ }
47
+ },
48
+ "dependencies": {
49
+ "@modelcontextprotocol/server": "2.2.0",
50
+ "pdfjs-dist": "^4.10.38"
51
+ },
52
+ "peerDependencies": {
53
+ "@deepseek-ai/dsh-mcp-client": ">=0.2.0-rc.1 <0.3.0-0"
54
+ }
55
+ }
@@ -0,0 +1,498 @@
1
+ // 开工注入:在第一次进入模型之前,把「环境事实、当前页、可用符号、技能清单、下一步」
2
+ // 一次性算好塞进去,模型不再自己去试 focus / brief / status / 翻源码。
3
+ //
4
+ // 为什么放在插件里而不是提示词里:
5
+ // 提示词是静态文本,写不出「这台机器现在 EPLAN 通不通、项目叫什么、当前是哪一页」。
6
+ // 2026-11-27 那次会话 33 步里有 22 步在查环境(focus 报错 → 绑定丢失 → brief 未就绪
7
+ // → 翻 README/INSTALL/server.mjs/Program.cs → 直连桥接器 install → 才拿到项目),
8
+ // 根因就是把「侦察」交给了模型。这里改成插件替它做完。
9
+ //
10
+ // 注入形态:一条 runtime-context 形式的 user 消息(不是 tool 结果 —— 开局还没有工具调用)。
11
+ // source.kind = 'eplan-bootstrap',客户端可以据此折叠展示,用户看到的是一张环境卡片,
12
+ // 不是一段「伪装成用户说话」的文字。
13
+ import { existsSync, readdirSync, readFileSync } from 'node:fs'
14
+ import { dirname, join } from 'node:path'
15
+ import { fileURLToPath } from 'node:url'
16
+
17
+ export const name = 'eplan-bootstrap'
18
+
19
+ // 只需要 tools 之外的东西都通过 ctx.get 可选取:拿不到就降级,绝不让预设起不来。
20
+ export const inject = ['tools']
21
+
22
+ const here = dirname(fileURLToPath(import.meta.url))
23
+ const root = join(here, '..')
24
+
25
+ /** 「常用符号 → 库」短表最多列这么几条:模型要的是「DCP/BP 去哪找」,不是一张长表。 */
26
+ const SYMBOL_MAP_LIMIT = 10
27
+
28
+ /**
29
+ * 除了 manifest 之外,仓库里已经确认过的归属。
30
+ *
31
+ * manifest 只收「图鉴里画过的」几个符号,`DCP` 恰恰不在里面 —— 而 DCP 正是这次踩坑的符号
32
+ * (模型去 GB_symbol 查 DCP,拿到 SYMBOL_NOT_FOUND「库里有相近的名字:RCP」,连查 4 个库)。
33
+ * 这些归属有两处独立记载,采信时为每一条都留了出处:
34
+ * - knowledge/KNOWLEDGE.md「符号库归属」表:DCP / BP / CO / DC / CDPST 在 SPECIAL
35
+ * - skills/eplan-symbols/SKILL.md:CO、DCP、DC、BP 不在 GB_symbol(在 SPECIAL)
36
+ * manifest 里的实测行优先(它会覆盖同名的种子),这里只补 manifest 没画过的常用符号。
37
+ */
38
+ const SYMBOL_LIBRARY_SEED = [
39
+ { symbol: 'DCP', library: 'SPECIAL' },
40
+ { symbol: 'BP', library: 'SPECIAL' },
41
+ { symbol: 'CO', library: 'SPECIAL' },
42
+ { symbol: 'DC', library: 'SPECIAL' },
43
+ { symbol: 'CDPST', library: 'SPECIAL' },
44
+ ]
45
+
46
+ /** 本机固定事实:不依赖 EPLAN 是否可达。 */
47
+ function localFacts() {
48
+ const facts = { unit: 'mm', grid: 4 }
49
+ const config = join(process.env.LOCALAPPDATA ?? '', 'eplan-mcp', 'eplan.config.json')
50
+ try {
51
+ if (existsSync(config)) {
52
+ const parsed = JSON.parse(readFileSync(config, 'utf8'))
53
+ // 曾经的字段名是 defaultLibrary,渲染成「默认符号库:GB_symbol」。这句话把「不传
54
+ // library 时工具的落点」说成了「常用符号都在这」:模型照它去 GB_symbol 查 DCP,
55
+ // 拿到 SYMBOL_NOT_FOUND 又连查 4 个库(session-0d24b269)。配置值本身没错,
56
+ // 错的是把它当环境结论 —— 所以改名 configuredLibrary,只在文案里解释「工具默认」。
57
+ if (parsed.symbolLibrary) facts.configuredLibrary = parsed.symbolLibrary
58
+ if (parsed.renderTheme) facts.renderTheme = parsed.renderTheme
59
+ }
60
+ } catch {
61
+ // 配置读不到就用默认值,不影响开工。
62
+ }
63
+ // 符号图鉴:既给「有哪些形状可以看」,也给「这个符号在哪个库」。
64
+ let measured = []
65
+ try {
66
+ const manifest = join(root, 'knowledge', 'symbols', 'manifest.json')
67
+ if (existsSync(manifest)) {
68
+ const parsed = JSON.parse(readFileSync(manifest, 'utf8'))
69
+ // manifest 的形状是 { generatedAt, page, paper, pxPerMm, symbols:[{key,library,variant,file,box}] }。
70
+ // 每行的 library 是本机真渲染过的库名 —— 这是仓库里唯一带「符号归属」的事实来源,
71
+ // 比配置里的 symbolLibrary 可靠:配置只说明工具的默认库,不说明符号住在哪。
72
+ const rows = Array.isArray(parsed?.symbols) ? parsed.symbols : []
73
+ facts.symbolGallery = rows
74
+ .map((row) => (typeof row === 'string' ? row : row?.key))
75
+ .filter(Boolean)
76
+ .slice(0, 16)
77
+ measured = rows
78
+ .filter((row) => row !== null && typeof row === 'object')
79
+ .filter((row) => typeof row.key === 'string' && row.key.length > 0)
80
+ .filter((row) => typeof row.library === 'string' && row.library.length > 0)
81
+ .map((row) => ({ symbol: row.key, library: row.library }))
82
+ }
83
+ } catch {
84
+ // 没有图鉴就不提。
85
+ }
86
+ // 实测行覆盖同名的种子;种子补 manifest 没画过的(例如 DCP)。
87
+ const bySymbol = new Map()
88
+ for (const row of [...SYMBOL_LIBRARY_SEED, ...measured]) bySymbol.set(row.symbol, row)
89
+ const merged = [...bySymbol.values()]
90
+ // 只按「SPECIAL 优先」排,其余保持原顺序(Array.sort 是稳定排序)。
91
+ // 比较器必须可传递,所以不写 left/right 互推 -1/1 那种写法。
92
+ .sort((left, right) => Number(right.library === 'SPECIAL') - Number(left.library === 'SPECIAL'))
93
+ .slice(0, SYMBOL_MAP_LIMIT)
94
+ if (merged.length > 0) facts.symbolLibraryMap = merged
95
+ // 「查参数 schema」要读的那个文件。给绝对路径,read 才能一步到位 ——
96
+ // 只说「server/server.mjs」模型还得先猜装在哪。
97
+ try {
98
+ const serverSource = join(root, 'server', 'server.mjs')
99
+ if (existsSync(serverSource)) facts.serverSource = serverSource
100
+ } catch {
101
+ // 路径算不出来就不提,文案会退回相对路径。
102
+ }
103
+ // 只有「怎么画」类技能在这里列;装机/排错那两篇已经并进经验手册,
104
+ // 目录还留在磁盘上只是为了排障时能直接看,不再作为技能加载。
105
+ try {
106
+ const merged = new Set(['eplan-setup', 'eplan-troubleshooting'])
107
+ facts.skillDocs = readdirSync(join(root, 'skills'), { withFileTypes: true })
108
+ .filter((entry) => entry.isDirectory())
109
+ .map((entry) => entry.name)
110
+ .filter((name) => !merged.has(name) && existsSync(join(root, 'skills', name, 'SKILL.md')))
111
+ } catch {
112
+ facts.skillDocs = []
113
+ }
114
+ try {
115
+ facts.knowledgeManual = existsSync(join(root, 'knowledge', 'KNOWLEDGE.md'))
116
+ } catch {
117
+ facts.knowledgeManual = false
118
+ }
119
+ return facts
120
+ }
121
+
122
+ // ── 运行时侦察:走工具注册表的真实分发路径 ───────────────────────────────
123
+ //
124
+ // 这里以前读的是 `tools?.mcp__eplan__workspace` —— 那是死代码,也是「EPLAN 明明
125
+ // 就绪却报没就绪」的根因。ctx.tools 是 ToolsService 实例,MCP 工具只以「注册定义」
126
+ // 的形式存在于注册表里,读属性永远是 undefined,于是每一轮都走「没挂上」分支。
127
+ // 真实会话(session-0d24b269)里模型自己去调同一个工具是成功的,返回了
128
+ // ready:true + 项目名 + 当前页,开局却被告知工具不可用,白花几步自证。
129
+ //
130
+ // 正确路径是 ToolsService 的两个公开方法(见 @deepseek-ai/dsh-tools 的 index.ts):
131
+ // get(name, scope?, toolSet?) -> ToolDefinition | undefined // 判断在不在注册表
132
+ // execute({ calls, signal, ... }) -> ToolBatchResult // 真正分发一次调用
133
+ // execute() 把调用交给当前作用域选中的执行器(原生或 PTC),返回
134
+ // results[i].result:成功时带 value / content,失败时带 error.info.code。
135
+ // 这条路径不依赖「本轮模型步」,在 agent/pre-step 里可以安全调用;不传 log 就
136
+ // 不会写进会话 —— 侦察是我们替模型做的,不该伪装成模型自己发起的工具调用。
137
+ const BRIEF_TOOL = 'mcp__eplan__workspace'
138
+ const BRIEF_TIMEOUT_MS = 15000
139
+ const BRIEF_CALL_ID = 'eplan-bootstrap-brief'
140
+
141
+ /** 错误对象 → 一行能读的原因。 */
142
+ function reasonOf(error) {
143
+ if (error instanceof Error) return error.message
144
+ if (error !== null && typeof error === 'object' && typeof error.message === 'string') return error.message
145
+ return String(error)
146
+ }
147
+
148
+ /** 从 content 块数组里取第一段文本(MCP 桥的 render 投影就是这个形状)。 */
149
+ function firstText(content) {
150
+ if (!Array.isArray(content)) return undefined
151
+ for (const block of content) {
152
+ if (block !== null && typeof block === 'object' && block.type === 'text' && typeof block.text === 'string') {
153
+ return block.text
154
+ }
155
+ }
156
+ return undefined
157
+ }
158
+
159
+ /**
160
+ * 把一次 brief 的返回解析成对象。
161
+ * 依次试:canonical value 里的 structuredContent → value.content 的文本 → render 后的 content 文本。
162
+ * 解析不出来返回 null(调用方据此报「探测不明」,绝不据此报「没就绪」)。
163
+ */
164
+ function parseBriefValue(result) {
165
+ const candidates = []
166
+ const value = result?.value
167
+ if (value !== null && typeof value === 'object') {
168
+ if (value.structuredContent !== null && typeof value.structuredContent === 'object') {
169
+ candidates.push(value.structuredContent)
170
+ }
171
+ const fromValue = firstText(value.content)
172
+ if (typeof fromValue === 'string') candidates.push(fromValue)
173
+ }
174
+ const rendered = firstText(result?.content)
175
+ if (typeof rendered === 'string') candidates.push(rendered)
176
+ for (const candidate of candidates) {
177
+ if (typeof candidate !== 'string') return candidate
178
+ try {
179
+ const parsed = JSON.parse(candidate)
180
+ if (parsed !== null && typeof parsed === 'object') return parsed
181
+ } catch {
182
+ // 这个候选不是 JSON,试下一个。
183
+ }
184
+ }
185
+ return null
186
+ }
187
+
188
+ /**
189
+ * 开工侦察:登记表里有没有 EPLAN 工具、调一次 workspace(action="brief") 通不通。
190
+ *
191
+ * 返回的 ready 是三态,不许含糊:
192
+ * true —— 调用成功且 EPLAN 说项目已加载,带真实 project / visiblePage / pages / locks
193
+ * false —— 工具真的不在注册表(MCP 没挂上),或工具在但调用真的失败了(带真实 code)
194
+ * null —— 插件这一侧探测不出来(没有 API、超时、返回无法解析)。这不代表 EPLAN 没就绪,
195
+ * 渲染时会明说「未探测」,让模型照常调一次,绝不重演假阴性。
196
+ */
197
+ async function runtimeFacts(tools) {
198
+ const get = typeof tools?.get === 'function' ? (name) => tools.get(name) : null
199
+ const execute = typeof tools?.execute === 'function' ? (input) => tools.execute(input) : null
200
+ if (get === null || execute === null) {
201
+ return {
202
+ ready: null,
203
+ reason: '插件没能探测:这版 tools 服务没有 get/execute 方法。这不代表 EPLAN 没就绪,工具照常可用。',
204
+ }
205
+ }
206
+
207
+ let definition
208
+ try {
209
+ definition = get(BRIEF_TOOL)
210
+ } catch (error) {
211
+ return {
212
+ ready: null,
213
+ reason: `插件没能探测:读工具注册表时抛错(${reasonOf(error)})。这不代表 EPLAN 没就绪。`,
214
+ }
215
+ }
216
+ if (definition === undefined || definition === null) {
217
+ return {
218
+ ready: false,
219
+ code: 'TOOL_NOT_REGISTERED',
220
+ reason: `EPLAN 工具这次没有挂上(注册表里没有 ${BRIEF_TOOL},MCP 服务不可用),只能离线查符号与经验。`,
221
+ }
222
+ }
223
+
224
+ // 真的分发一次调用。带上超时:MCP 卡住不该把整轮开工拖死。
225
+ //
226
+ // 为什么必须走 tools.execute() 而不是 definition.execute(args, exec):
227
+ // 这个预设的最后一层是 tool-executor-ptc。注册表是按「定义」存在的,但分发
228
+ // 权在执行器手里 —— 在 PTC 模式下,没有 parent 的「模型直呼原生工具名」会被
229
+ // 直接拒成 UNKNOWN_TOOL(见 pipeline.ts / execute 的约定)。走 execute() 才是
230
+ // 执行器自己的分发,原生和 PTC 两种模式都成立。
231
+ // 也不传 agent / log:这次侦察是我们替模型做的,不该进会话、也不受 agent 作用域
232
+ // 收窄(预设本身可能就注册在 agent 作用域里,传了反而可能查不到工具)。
233
+ const controller = new AbortController()
234
+ const timer = setTimeout(() => controller.abort(new Error('brief 超时')), BRIEF_TIMEOUT_MS)
235
+ let batch
236
+ try {
237
+ batch = await execute({
238
+ calls: [
239
+ {
240
+ id: BRIEF_CALL_ID,
241
+ type: 'tool-call',
242
+ name: BRIEF_TOOL,
243
+ arguments: JSON.stringify({ action: 'brief' }),
244
+ },
245
+ ],
246
+ signal: controller.signal,
247
+ })
248
+ } catch (error) {
249
+ return {
250
+ ready: null,
251
+ reason: `插件没能探测:调用没能完成(${reasonOf(error)})。这不代表 EPLAN 没就绪,照常调 workspace 就行。`,
252
+ }
253
+ } finally {
254
+ clearTimeout(timer)
255
+ }
256
+
257
+ const settled = batch?.results?.[0]?.result
258
+ if (settled === undefined || settled === null) {
259
+ return { ready: null, reason: '插件没能探测:执行器没有返回结果。这不代表 EPLAN 没就绪。' }
260
+ }
261
+
262
+ if (settled.isError === true) {
263
+ const code = settled.error?.info?.code
264
+ if (code === 'ABORTED' || code === 'ABORTED_BEFORE_DISPATCH') {
265
+ return {
266
+ ready: null,
267
+ code,
268
+ reason: `插件没能探测:超过 ${BRIEF_TIMEOUT_MS / 1000} 秒没回来,已放弃。这不代表 EPLAN 没就绪,直接调 workspace({action:"brief"}) 即可。`,
269
+ }
270
+ }
271
+ const detail = String(settled.error?.message ?? firstText(settled.content) ?? '未知错误')
272
+ return {
273
+ ready: false,
274
+ code,
275
+ message: detail,
276
+ reason: `EPLAN 工具在注册表里,但这次调用失败了${code ? `(${code})` : ''}:${detail}`,
277
+ }
278
+ }
279
+
280
+ const facts = parseBriefValue(settled)
281
+ if (facts === null) {
282
+ return {
283
+ ready: null,
284
+ reason: '插件没能探测:brief 的内容解析不了。这不代表 EPLAN 没就绪,直接调 workspace({action:"brief"}) 即可。',
285
+ }
286
+ }
287
+ if (facts.ready === true) return facts
288
+
289
+ // 调用成功,但 EPLAN 自己说没就绪(例如没开、没加载项目):这确实是「否」。
290
+ return {
291
+ ...facts,
292
+ ready: false,
293
+ code: facts.code ?? facts.eplan?.code,
294
+ message: facts.message ?? facts.reason ?? facts.eplan?.message,
295
+ reason: facts.reason ?? facts.message ?? facts.eplan?.message ?? 'brief 说还没就绪,但没给原因。',
296
+ }
297
+ }
298
+
299
+ /**
300
+ * 把 locks 渲染成「页名(谁占着)」。
301
+ *
302
+ * 真实 brief 返回的是对象:{"=EB3+ET1/1":{"owner":"mcp:3512","at":"..."}}。
303
+ * 以前只判断 Array.isArray,对象形状永远不渲染,模型因此不知道某页被 lock 着,
304
+ * 会直接上去改。这里以对象为主、兼容将来变回数组。
305
+ */
306
+ function renderLocks(locks) {
307
+ if (locks === null || locks === undefined) return []
308
+ if (Array.isArray(locks)) {
309
+ return locks
310
+ .map((entry) => {
311
+ if (typeof entry === 'string') return entry
312
+ if (entry !== null && typeof entry === 'object') {
313
+ const page = entry.page ?? entry.name
314
+ const owner = entry.owner
315
+ if (typeof page === 'string') return owner ? `${page}(owner ${owner})` : page
316
+ }
317
+ return null
318
+ })
319
+ .filter((row) => typeof row === 'string' && row.length > 0)
320
+ }
321
+ if (typeof locks === 'object') {
322
+ return Object.entries(locks)
323
+ .map(([page, holder]) => {
324
+ if (typeof holder === 'string') return `${page}(${holder})`
325
+ if (holder !== null && typeof holder === 'object' && holder.owner) return `${page}(owner ${holder.owner})`
326
+ return page
327
+ })
328
+ .filter((row) => row.length > 0)
329
+ }
330
+ return []
331
+ }
332
+
333
+ /** 一道工序一行,直接把「现在该做什么」写死,不留解释空间。 */
334
+ function renderBootstrap(facts, runtime, taskHint) {
335
+ const lines = []
336
+ lines.push('<eplan-bootstrap>')
337
+ lines.push('这条不是用户说的话:是本机在开工前代你查好的环境事实。已经查过了,不要重复查。')
338
+ lines.push('')
339
+
340
+ lines.push('## 现在必须做的第一件事')
341
+ if (runtime?.ready === true && runtime?.visiblePage) {
342
+ lines.push(`把绘图板呈现出来:present(page="${runtime.visiblePage}")。`)
343
+ lines.push('它会切到软件内的当前页并出一张渲染图 —— 先看这张图,再决定画什么。')
344
+ lines.push('如果这页不是要画的目标,先 page_create 建新页,再 present 新页。')
345
+ } else if (runtime?.ready === true) {
346
+ lines.push('EPLAN 已就绪但没有可见页:先 list_pages 看现有页,再 present 或 page_create。')
347
+ } else if (runtime?.ready === false) {
348
+ lines.push('EPLAN 还没就绪,先看下面「环境」那几行,按 nextStep 走一步,不要自己去找原因。')
349
+ } else {
350
+ // 探测不明 ≠ 没就绪。让模型自己去调一次,而不是被一句错误结论带着兜圈。
351
+ lines.push('开局先说一句「开工侦察」的结论就好:调一次 workspace({action:"brief"}),')
352
+ lines.push('按它返回的 ready / project / visiblePage 直接干活 —— 不要为了确认环境连着调好几次。')
353
+ }
354
+ lines.push('')
355
+
356
+ lines.push('## 环境')
357
+ lines.push(`- 坐标:${facts.unit},原点在页面左下角,X 向右、Y 向上;元件坐标取 ${facts.grid} mm 的倍数。`)
358
+ // 符号库那行以前是「默认符号库:GB_symbol(换库前先 symbol_catalog search 确认存在)」。
359
+ // 那句话是错误引导:配置里的 symbolLibrary 只说明「不传 library 时工具落在哪」,
360
+ // 不说明符号住在哪。常用小符号 DCP/BP/CO 全在 SPECIAL,模型照那句话去 GB_symbol 查,
361
+ // 拿到 SYMBOL_NOT_FOUND 再连查 4 个库。现在改成「归属表 + 查法」,不给强断言。
362
+ const libraryMap = Array.isArray(facts.symbolLibraryMap) ? facts.symbolLibraryMap : []
363
+ if (libraryMap.length > 0) {
364
+ const rows = libraryMap.map((row) => `${row.symbol}→${row.library}`).join(' ')
365
+ lines.push(`- 符号住哪个库(本机实测过,别猜):${rows}。`)
366
+ lines.push(' - 表里没有的符号:symbol_catalog{action:"visual", symbols:[...]} 画出来认(不传 library 时工具按默认库找,找不到就换库再查,别只在一个库里反复搜)。')
367
+ } else if (facts.configuredLibrary) {
368
+ // 拿不到归属表就别硬编一张:只陈述「配置里是什么」,并明说它不代表符号归属。
369
+ lines.push(`- 符号库:配置里的默认库是 ${facts.configuredLibrary},但那只决定「不传 library 时工具落在哪个库」,不代表符号都在这。`)
370
+ lines.push(' - 常用的小符号 DCP / BP / CO / TOUL 通常在 SPECIAL,不在 GB_symbol;查不到先换库,别只在一个库里反复搜。')
371
+ } else {
372
+ lines.push('- 符号库:常用的小符号 DCP / BP / CO / TOUL 通常在 SPECIAL,不在 GB_symbol;')
373
+ lines.push(' 查不到先换库(SPECIAL ↔ GB_symbol),换完还没有才说明真没有。')
374
+ }
375
+ lines.push(`- EPLAN 就绪:${runtime?.ready === true ? '是' : runtime?.ready === false ? '否' : '未探测(不代表没就绪)'}`)
376
+ if (runtime?.project) lines.push(`- 当前项目:${runtime.project}${runtime.pageCount != null ? `(${runtime.pageCount} 页)` : ''}`)
377
+ if (runtime?.visiblePage) lines.push(`- 界面上那一页:${runtime.visiblePage}`)
378
+ if (Array.isArray(runtime?.pages) && runtime.pages.length > 0) {
379
+ lines.push(`- 页清单:${runtime.pages.slice(0, 40).join('、')}${runtime.pages.length > 40 ? ` …共 ${runtime.pages.length} 页` : ''}`)
380
+ }
381
+ const locked = renderLocks(runtime?.locks)
382
+ if (locked.length > 0) {
383
+ lines.push(`- 被占用的页(别动):${locked.join('、')}`)
384
+ }
385
+ if (typeof runtime?.readOnly === 'boolean') lines.push(`- 项目只读:${runtime.readOnly ? '是(只能看,不能写)' : '否'}`)
386
+ if (Array.isArray(runtime?.eplanProcesses) && runtime.eplanProcesses.length > 1) {
387
+ lines.push(`- 本机有 ${runtime.eplanProcesses.length} 个 EPLAN 在跑(pid ${runtime.eplanProcesses.join('、')}),只连其中一个。`)
388
+ }
389
+ if (runtime?.ready === false) {
390
+ lines.push(`- 没就绪的原因:${runtime?.message ?? runtime?.reason ?? '未知'}`)
391
+ if (runtime?.code) lines.push(`- 错误码:${runtime.code}`)
392
+ if (Array.isArray(runtime?.nextSteps)) {
393
+ for (const step of runtime.nextSteps.slice(0, 4)) lines.push(` - ${step}`)
394
+ }
395
+ if (runtime?.doNotRetry) lines.push(` - ${runtime.doNotRetry}`)
396
+ } else if (runtime?.ready !== true) {
397
+ lines.push(`- 插件没探到的原因:${runtime?.reason ?? '未知'} —— EPLAN 可能完全是好的。`)
398
+ }
399
+ lines.push('')
400
+
401
+ lines.push('## 手上有哪些东西')
402
+ if (Array.isArray(facts.skillDocs) && facts.skillDocs.length > 0) {
403
+ lines.push(`- 技能(整篇读,照着做):${facts.skillDocs.join('、')} —— 用 skill(name) 加载。`)
404
+ }
405
+ if (facts.knowledgeManual) {
406
+ lines.push('- 经验手册:handbook(action="search", query=...)。任何不认识的错误码、卡住、画出来不对,先查它。')
407
+ }
408
+ if (Array.isArray(facts.symbolGallery) && facts.symbolGallery.length > 0) {
409
+ lines.push(`- 符号图鉴(EPLAN 真渲染):${facts.symbolGallery.join('、')} —— 要看形状用 symbol_catalog visual{symbols:[...]} 一次出图。`)
410
+ }
411
+ lines.push('- 常用元件引脚在 symbol_catalog action="pins" 里,一次查一批,不要逐个试。')
412
+ lines.push('')
413
+
414
+ if (taskHint) {
415
+ lines.push('## 这次的活儿')
416
+ lines.push(taskHint)
417
+ lines.push('')
418
+ }
419
+
420
+ lines.push('## 不要做的事')
421
+ lines.push('- 不要再调 focus / brief / status 确认环境:上面就是全部答案。')
422
+ // 这条以前是「不要读源码或说明文件找原因」—— 一刀切,把唯一可靠的查证途径也堵死了。
423
+ // 模型要知道 insert_symbol 的 insert 接受哪些字段,handbook 里查不到(connectionPoint 零命中),
424
+ // 又不敢读源码,只好传空参数试探(8 次报错里 5 次是这个原因)。现在分成两件事讲清楚:
425
+ // 「查环境」不许读源码,「查参数 schema」鼓励读源码。
426
+ lines.push('- 不要读源码查环境:就绪状态、项目、页、符号库上面都给了,翻代码只会更慢。')
427
+ if (facts.serverSource) {
428
+ lines.push(`- 但要知道某个工具接受哪些参数,先 handbook(action="search");查不到就直接 read ${facts.serverSource} 里的 schema 定义 —— 那比传空参数试错快得多。`)
429
+ } else {
430
+ lines.push('- 但要知道某个工具接受哪些参数,先 handbook(action="search");查不到就直接 read server/server.mjs 里的 schema 定义 —— 那比传空参数试错快得多。')
431
+ }
432
+ lines.push('- 不要用传空参数 / 省略必填项的方式试探 schema:报错只说缺哪个,不会告诉你完整字段。')
433
+ lines.push('- 不要在同一步里连开多个页面:先画一页,present 这一页,再画下一页。')
434
+ lines.push('</eplan-bootstrap>')
435
+ return lines.join('\n')
436
+ }
437
+
438
+ /** 一条用户消息里有没有 @ 到视频/图片:有就说明这是「复刻参考」的活儿。 */
439
+ function taskHintOf(messages) {
440
+ const text = (messages ?? [])
441
+ .flatMap((message) => (Array.isArray(message?.content) ? message.content : []))
442
+ .filter((block) => block?.type === 'text')
443
+ .map((block) => String(block.text ?? ''))
444
+ .join('\n')
445
+ if (/<video-digest>/.test(text)) {
446
+ return '用户发了一段录屏,摘要已随消息附上(阶段 + 每阶段一张整页图 + 讲解文字)。先读摘要里的单元表,再动手。'
447
+ }
448
+ if (/\.(png|jpe?g|webp|bmp)\b/i.test(text)) {
449
+ return '用户给了参考图:先拆单元表(一行一个器件),再画。'
450
+ }
451
+ return null
452
+ }
453
+
454
+ export function apply(ctx) {
455
+ const facts = localFacts()
456
+ /** sessionId → 已经注入过,避免每一步都塞一遍。 */
457
+ const bootstrapped = new Set()
458
+ /** sessionId → 同一轮里只真正查一次 brief。 */
459
+ const inflight = new Map()
460
+
461
+ ctx.on('agent/pre-step', async (payload, next) => {
462
+ const decision = await next()
463
+ if (decision.kind !== 'enter') return decision
464
+
465
+ const agent = payload?.agent
466
+ const key = String(agent?.session?.id ?? 'unknown')
467
+ if (bootstrapped.has(key)) return decision
468
+
469
+ let runtime
470
+ try {
471
+ runtime = inflight.get(key) ?? runtimeFacts(ctx.tools)
472
+ inflight.set(key, runtime)
473
+ runtime = await runtime
474
+ } catch (error) {
475
+ // 侦察自己崩了,只说明「没探到」,不能让模型以为 EPLAN 挂了。
476
+ runtime = { ready: null, reason: `插件没能探测:${reasonOf(error)}。这不代表 EPLAN 没就绪。` }
477
+ } finally {
478
+ inflight.delete(key)
479
+ }
480
+
481
+ bootstrapped.add(key)
482
+ const text = renderBootstrap(facts, runtime, taskHintOf(decision.messages))
483
+ return {
484
+ ...decision,
485
+ messages: [
486
+ ...decision.messages,
487
+ {
488
+ role: 'user',
489
+ source: { kind: 'eplan-bootstrap', form: 'snapshot' },
490
+ content: [{ type: 'text', text }],
491
+ },
492
+ ],
493
+ }
494
+ }, { prepend: true })
495
+ }
496
+
497
+ /** 测试用:渲染器是纯函数,直接喂假事实就能验;runtimeFacts 喂假 tools 就能验四分支。 */
498
+ export const __testing = { renderBootstrap, localFacts, taskHintOf, runtimeFacts, renderLocks }
@@ -0,0 +1,47 @@
1
+ // 目标结束时的经验提醒。
2
+ //
3
+ // 为什么需要它:人设里写着「踩到坑就记一条」,但那是一句软指令 —— 模型做完事、
4
+ // 目标一标完成,就顺着收尾了,多半想不起去写经验。而经验库的价值全在于「当时写下」,
5
+ // 隔一轮再补,细节已经丢了。
6
+ //
7
+ // 为什么不用 goal/changed 事件 + agent.followup():
8
+ // 1. followup 需要 createUserMessage(@deepseek-ai/dsh-llm),而 link: 类型的 bundle
9
+ // 里 import 不到那个包,只能手工复刻消息结构,形状一变就静默坏掉;
10
+ // 2. followup 会多起一整轮对话,还可能搅乱目标轮次计数。
11
+ // 挂在 tools/execute 上只包一层、就地追加一段文字,同一步内生效,不碰目标状态机。
12
+ //
13
+ // 提醒失败绝不能影响工具本身的返回:所以改写包在 try 里,出错就原样返回。
14
+ export const name = 'experience-nudge'
15
+
16
+ const NUDGE = [
17
+ '',
18
+ '---',
19
+ '📝 结束前把经验留下来。',
20
+ '这次如果踩到坑、发现某个参数/动作名和预想的不一样、或者验证出一条可行的做法,',
21
+ '现在就用 `lesson { action: "add", text: "...", kind: "pitfall|recipe|decision" }` 记下来。',
22
+ '写法是「做了什么 → 看到什么现象 → 结论」,给下一个会话看,别写「见上文」。',
23
+ '确实没有值得记的就直接回一句「无新增经验」,不要为了凑数编。',
24
+ ].join('\n')
25
+
26
+ export function apply(ctx) {
27
+ // 只认 update_goal 的 complete / blocked:这两个动作意味着这次目标工作收尾了。
28
+ // 注意是 blocked 不是 block —— 枚举值来自 dsh-tool-goal 的 UPDATE_ACTIONS。
29
+ ctx.on('tools/execute', async (exec, next) => {
30
+ const result = await next()
31
+
32
+ try {
33
+ if (exec?.name !== 'update_goal') return result
34
+ const action = exec.arguments?.action
35
+ if (action !== 'complete' && action !== 'blocked') return result
36
+ // 目标没更新成功(比如 revision 过期)就没有收尾可言,不打扰。
37
+ if (!result || result.isError) return result
38
+
39
+ const content = Array.isArray(result.content) ? [...result.content] : []
40
+ content.push({ type: 'text', text: NUDGE })
41
+ return { ...result, content }
42
+ } catch (error) {
43
+ ctx.logger?.warn?.(`experience-nudge: 追加提醒失败,已忽略:${error?.message ?? error}`)
44
+ return result
45
+ }
46
+ })
47
+ }