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,142 @@
1
+ ---
2
+ name: eplan-symbols
3
+ description: EPLAN 符号库清单、常用元件的引脚几何(相对坐标与朝向)、变体的含义,以及用 symbol_catalog(action="search") / symbol_catalog(action="pins") / page_inspect(format="structure") 查证符号的方法。要选元件或排查连线时读它。
4
+ ---
5
+
6
+ # EPLAN 符号与引脚几何
7
+
8
+ ## 符号库
9
+
10
+ `workspace(action="status")` 的 `currentProject.symbolLibraries` 给出**当前项目实际可用**的库,以它为准。
11
+
12
+ 本机实测存在的:`SPECIAL`、`GB_symbol`、`GB_single_symbol`、`GRAPHICS`、`OS_SYM_ESS`,以及若干 `IEC_*`。
13
+
14
+ | 库 | 内容 |
15
+ |---|---|
16
+ | `GB_symbol` | 823 个国标元件:熔断器 `F1`、三极熔断器组 `FA3`、各类开关继电器 |
17
+ | `SPECIAL` | 443 个通用符号:连接点 `CO`、T 节点 `TLRU` / `TOUR` / `TLRO` / `TOUL`、电源引入点 `CDPST`、中断点 `BP` |
18
+ | `GRAPHICS` | 纯图形符号,不带电气含义 |
19
+
20
+ **容易找错**:`CO`、`DCP`、`DC`、`BP` 不在 `GB_symbol`(在 `SPECIAL`)。`GB_symbol` 里**没有** `L24` / `0V` 这种电位符号。
21
+
22
+ ## 引脚几何(实测,单位 mm,相对符号原点)
23
+
24
+ | 符号 | 变体 | 引脚 | 用途 |
25
+ |---|---|---|---|
26
+ | `CO` | 0 | Right(0,0), Down(0,0) | 转角:右来下走 |
27
+ | `CO` | 1 | Right(0,0), Up(0,0) | 转角 |
28
+ | `CO` | 2 | Left(0,0), Up(0,0) | 转角 |
29
+ | `CO` | 3 | Left(0,0), Down(0,0) | 转角:左来下走(总线右端) |
30
+ | `TLRU` | 0 | Down(0,0), Left(0,0), Right(2,0) | T 节点 |
31
+ | `TLRU` | **8** | Right(0,0), Left(0,0), Down(0,0) | **最干净的 T 节点:三引脚都在原点,拼总线优先用这个变体** |
32
+ | `TLRO` | 8 | Right, Left, Up | 向上分支的 T 节点 |
33
+ | `TOUR` | 0 | Down(0,0), Up(0,0), Right(2,0) | 带上支路的 T 节点 |
34
+ | `TOUL` | 0 | Down(0,0), Up(0,0), Left(-2,0) | 对称版本 |
35
+ | `CDPST` | — | Up(0,+4), Down(0,-4) | 电源引入点 |
36
+ | `F1` | — | Up(0,+4), Down(0,-4) | 单极熔断器,8 mm 高 |
37
+ | `FA3` | — | (0,±4), (8,±4), (16,±4) | 三极熔断器组,跨 16 mm |
38
+ | `G22` | — | (0,±8), (16,±8) | 16 mm 高元件 |
39
+ | `XU` | — | Up(0,4), Up(8,4), Up(16,4) | 三个上连接点 |
40
+
41
+ **熔断器尺寸改不了**:`GB_symbol` 里所有单极熔断器(`F1` `FA1` `FS` `FT1` `F_AB_1P` `F_SNH_1P`)引脚半高都是 **4 mm**,即 8 mm 高。EPLAN 符号不可缩放,想要更大的图形只能去改符号库定义。
42
+
43
+ ## 查证方法
44
+
45
+ ```
46
+ symbol_catalog(action="search") {"library":"GB_symbol","filter":"F"} 列符号名,防瞎编
47
+ symbol_catalog(action="pins") {"library":"SPECIAL","symbols":["TLRU"]} 查变体数与每个引脚的相对坐标/朝向
48
+ page_inspect(format="structure") {"page":"=MCP/8"} 查页面上元件实际的绝对引脚坐标
49
+ ```
50
+
51
+ - **永远先查再插**。符号名写错报 `SYMBOL_NOT_FOUND`,比你多查一次的成本高得多。
52
+ - `symbol_catalog(action="pins")` 给的是**相对**坐标;`page_inspect(format="structure")` 给的是**绝对**坐标。判断两个元件会不会自动连线,看 `page_inspect(format="structure")` 的绝对坐标。
53
+ - `page_inspect(format="structure")` 的 `members` 参数可以转储任意 EPLAN 类型的方法表,探索不熟悉的 API 时很有用。
54
+
55
+ ### 中文搜不到怎么办(2026-11-23 实测)
56
+
57
+ 库里全是 GB 老编码,按中文搜(`filter="按钮"`)只会拿到空或者 823 条全量 —— 别在这上面耗时间,走两条路:
58
+
59
+ 1. 常见概念的对应关系已经内置,`search` 会直接回在 `concepts` 字段里(空开→`FA3`、接地→`ERDE`、
60
+ 中断点→`BP`、电源引入→`CDPST`、连接点/T 节点→`CO`/`TLRU`…)。有就直接用。
61
+ 2. 表里没有的,用 `symbol_catalog(action="visual", symbols=["SON","SSA","KM2"])`:
62
+ 它把这些符号真画到一张临时页上、每个上面标好名字、裁好区域渲一张图给你,页用完自动删。
63
+ **一次调用**就能看清形状,比「建试画页 → 渲染 → 截图 → 放大裁图」省十几次往返。
64
+
65
+ ## 变体 = 方向(EPLAN 里按 Tab 转 90°,工具里就是换变体)
66
+
67
+ 用户说的「按 Tab 转 90°」在 API 里没有对应的旋转参数:`SymbolReference` 只有 `Location`
68
+ 和 `SymbolVariant`,没有角度属性。`page_create` / `page_edit(action="insert_symbol")` 的
69
+ `variant` 就是唯一能控制方向的东西,默认 0。
70
+
71
+ - 选之前先 `symbol_catalog(action="pins")`:每个变体都列出引脚相对坐标和 `direction`
72
+ (引脚朝外的方向)。要接从上方来的导线,就挑引脚朝 `Up` 的变体,以此类推。
73
+ - 顺序**不保证**是 0/90/180/270,每个符号库各自定义。实测 `GB_symbol` 的 `F1`(单极熔断器):
74
+ v0 竖放、v1 横放、v2 竖放(镜像)、v3 横放 —— 不能按「每加 1 转 90°」推。
75
+ - 库里没有你要的方向(只有竖没有横),换一个符号,不要硬搬坐标或者用文字凑。
76
+
77
+ ## GB_symbol 里能当「供电主回路」用的符号(2026-11 实测渲染确认)
78
+
79
+ GB_symbol 的命名是 GB 老式编码(`QSF3`、`BOSW`、`BET_01`),不是 IEC 的 `QF`/`T`/`H`,所以按
80
+ 「断路器」「变压器」这种中文或 IEC 前缀去搜是搜不到的。下面这些是**插进图里渲染确认过**的:
81
+
82
+ | 符号 | 实际是什么 | 对应图纸里的 | 引脚 |
83
+ |---|---|---|---|
84
+ | `QSF3` | 三极带熔断器隔离开关(三个极各带熔断器) | `-QF0` 电源切断开关 32A | 上下各 3 个 |
85
+ | `QSF4` | 三极 + N 极带熔断器隔离开关 | `-QF1` 3PDA | 上下各 4 个 |
86
+ | `HLED` | LED 指示灯 | `-H1` 电源指示灯 | 上 |
87
+ | `KM2` / `KM3` / `KM4` | 接触器线圈 | `-KM1` 总急停接触器 | 左右 |
88
+ | `CDPST` | 电源引入点(圆点带上下引线) | 进线端子 / 电位引入 | `Up(0,+4)`、`Down(0,-4)` |
89
+ | `TLRU` v8 / `TOUR` v0 / `CO` v3 | 三引脚节点,拼母线和分支 | 母线 T 接 | 见上表 |
90
+ | `F1` / `FA1` / `FA2` | 单极熔断器,半高 4 mm | 支路熔断器 | `Up/Down` |
91
+
92
+ **没有**的三样(别浪费时间找):断路器(`QF`)、隔离变压器(`T`)、插座(`XS`)。
93
+ 变压器要自己搭(两个线圈符号拼),插座用 `PLC_CBOX_*` 之类的连接器符号凑,或者直接
94
+ 用 `Graphics.Rectangle` + 文字画一个示意框 —— 用户能接受示意,但不能接受符号张冠李戴。
95
+
96
+ **搜索陷阱**:`symbol_catalog(action="search")` 一次最多回 **200** 条,默认 50;传更大的 `limit` 会被直接拒。
97
+ 所以搜一个字母(比如 `T`)只能看到前 200 个,**不能因为没看到就断定库里没有**。
98
+ 判断「库里有没有 X」的正确做法是搜更具体的名字片段(`TS`、`T11`),或者先用 `filter` 缩窄再翻页 `offset`。
99
+ 要看全量只能靠分页翻,不是靠传大 limit。
100
+
101
+ ### 变压器、端子、整流/逆变(样张页 =MCP/14 渲染确认)
102
+
103
+ | 符号 | 实际是什么 | 用在哪 |
104
+ |---|---|---|
105
+ | `T11` | **三相变压器**(三组初级 + 三组次级线圈) | 三相隔离变压器 |
106
+ | `TS11` | **单相变压器**(一组初级 + 一组次级) | `-T1` 隔离变压器 380V/220V 1kVA |
107
+ | `TS12` / `TS22` | 单相变压器变体(绕组布局不同) | 按引线方向需求挑 |
108
+ | `TST_1` / `TST_11` | 带屏蔽层的变压器 | 需要屏蔽时 |
109
+ | `TISPQ` | 整流器(方框里 U) | 整流桥 |
110
+ | `TISTQ` | 逆变器(方框里 I) | 逆变输出 |
111
+ | `X` / `X2` / `X3_1` / `X3_B` | 端子排 / 端子(带连接圆点) | 端子排、跨页连接点 |
112
+ | `X2_ST` / `X3_BU1` | 带屏蔽或带插头的端子 | 屏蔽端子 |
113
+
114
+ **没有**「插座」符号(`XS`)。三孔插座只能用 `X` 系列端子拼,或者用矩形 + 文字画示意。
115
+ 不要拿别的符号冒充 —— 用户一眼能看出来。
116
+
117
+ ## 怎么快速摸清一个库(别一个个猜)
118
+
119
+ 把候选符号成批摆到一张临时页上、每个旁边标上符号名,然后 `page_inspect(format="image")` 出图,一张图看十几个:
120
+
121
+ ```
122
+ page_create { "page": "=TMP/1", "nodes": [ {"symbol":"T11","at":[40,230]}, ... ],
123
+ "texts": [ {"at":[31,246],"text":"T11"} ] }
124
+ page_inspect { "format": "image", "page": "=TMP/1", "width": 2000 }
125
+ ```
126
+
127
+ 比逐个插入 + 逐个渲染快一个数量级。实测一次能认 16 个符号。
128
+ 注意间距要大于 40 mm,否则符号会叠在一起看不清。
129
+
130
+ ## 连接点代号:谁显示、谁不显示(2026-11 实测)
131
+
132
+ - **DCP**:自己渲染连接点代号(`deviceTag` 冒号后的部分)。再手写一条文字就是
133
+ 「R R」「PEPE」重影;只能改 `deviceTag`。
134
+ - **BOSW**(常开触点)、**FA3**(三极熔断器组):**不显示** 13/14、1/3/5 这些代号,
135
+ 要用文字补;补之前给符号 `showDeviceTag:false`,否则和自动器件标识叠成两条。
136
+ - **M3**(三相电机):引脚 `(0,+8)(8,+8)(16,+8)` 朝上,PE `(16,-8)` 朝下;
137
+ 标 U1/V1/W1 用 `y = 符号y + 19`、`x = 引脚x - 6`(`+14` 会撞自动标识 `-M1`)。
138
+
139
+ ## 中断点 BP 的朝向
140
+
141
+ `SPECIAL` 的 BP:`v1`/`v5` 上箭头,`v3`/`v7` 下箭头,引脚都在 `(0,0)`/`(0,-1)`,
142
+ 换变体只改图形方向。竖在端头、另一侧不接东西的 BP 会被自动调整删掉(见 eplan-drawing)。
@@ -0,0 +1,106 @@
1
+ // 文档与工具面的一致性检查。
2
+ //
3
+ // 为什么需要:工具改名之后,人设、四份知识文件、README 里到处是旧名字,
4
+ // 模型照着旧名字调用会直接失败,而这类漂移不会让任何测试变红。
5
+ // 这个脚本把「文档里提到的工具名」和「server.mjs 实际注册的工具名」对一遍。
6
+ //
7
+ // node test/check-docs.mjs
8
+ import { readFileSync, readdirSync, existsSync } from 'node:fs'
9
+ import { fileURLToPath } from 'node:url'
10
+ import path from 'node:path'
11
+
12
+ const here = path.dirname(fileURLToPath(import.meta.url))
13
+ const root = path.join(here, '..')
14
+
15
+ // 1) 实际注册的工具名
16
+ const server = readFileSync(path.join(root, 'server', 'server.mjs'), 'utf8')
17
+ const registered = [...server.matchAll(/(?:regTool|registerTool)\(\s*'([a-z_]+)'/g)].map((m) => m[1])
18
+ const known = new Set(registered)
19
+
20
+ // 2) 改名后不许再出现的旧名字
21
+ const RETIRED = [
22
+ 'symbol_info',
23
+ 'symbol_insert',
24
+ 'page_open',
25
+ 'project_open',
26
+ 'project_close',
27
+ 'inspect',
28
+ 'look',
29
+ 'props',
30
+ 'check',
31
+ 'pages',
32
+ 'draw',
33
+ 'pdf',
34
+ 'status',
35
+ 'symbols',
36
+ ]
37
+
38
+ const files = []
39
+ const skillsDir = path.join(root, 'skills')
40
+ if (existsSync(skillsDir)) {
41
+ for (const name of readdirSync(skillsDir)) {
42
+ const file = path.join(skillsDir, name, 'SKILL.md')
43
+ if (existsSync(file)) files.push(file)
44
+ }
45
+ }
46
+ files.push(path.join(root, 'cordis.patch.yml'))
47
+ files.push(path.join(root, 'README.md'))
48
+ files.push(path.join(root, 'docs', 'INSTALL.md'))
49
+
50
+ const problems = []
51
+
52
+ for (const file of files) {
53
+ if (!existsSync(file)) continue
54
+ const label = path.relative(root, file)
55
+ const lines = readFileSync(file, 'utf8').split('\n')
56
+
57
+ lines.forEach((line, index) => {
58
+ // 三种「看起来像旧名字、其实不是」的情况要先遮掉:
59
+ // 1. 已经迁移的写法:workspace(action="status")
60
+ // 2. JSON 里的参数名和取值:{"action":"status"}、{"symbols":[...]}
61
+ // 3. 引号里的桥接器 op 名:call('status', ...)
62
+ // 4. 文件名:symbols.json 里的 symbols 是旧工具名,但这里它只是个文件名
63
+ const masked = line
64
+ .replace(/\b(?:workspace|symbol_catalog|page_edit|page_inspect)\((?:action|format)="[a-z_]+"\)/g, '§')
65
+ .replace(/"[^"]*"/g, '§')
66
+ .replace(/'[^']*'/g, '§')
67
+ .replace(/\b[A-Za-z0-9_-]+\.(?:jsonl?|md|mjs|cs|ps1|exe|dll|yml|png)\b/g, '§')
68
+
69
+ for (const name of RETIRED) {
70
+ // 前后不能是字母数字下划线连字符:避免 eplan-symbols、page_delete 之类误报。
71
+ const pattern = new RegExp('(?<![A-Za-z0-9_-])' + name + '(?![A-Za-z0-9_-])')
72
+ if (!pattern.test(masked)) continue
73
+ // 退役的工具名后来又被用成了**参数名**(page_edit 的 draw[]、symbol_catalog 的
74
+ // symbols[])。按 JSON 写法出现(后面紧跟 : = [ 或引号)的不算「还在用旧工具名」;
75
+ // 只要出现一次像工具调用的写法(draw( / symbols 空格…)就仍然报。
76
+ const asParam = new RegExp('(?<![A-Za-z0-9_-])' + name + '(?=\\s*[:=\\[(])', 'g')
77
+ const all = masked.match(new RegExp('(?<![A-Za-z0-9_-])' + name + '(?![A-Za-z0-9_-])', 'g')) ?? []
78
+ const asParamCount = (masked.match(asParam) ?? []).length
79
+ // 参数写法里也包含 `name(`,那种更像工具调用,单独排除掉。
80
+ const bareCall = new RegExp('(?<![A-Za-z0-9_-])' + name + '\\s*\\(', 'g')
81
+ const callCount = (masked.match(bareCall) ?? []).length
82
+ if (callCount === 0 && all.length > 0 && asParamCount === all.length) continue
83
+ problems.push(`${label}:${index + 1} 还在用旧名字 "${name}"`)
84
+ }
85
+
86
+ // mcp__eplan__xxx 必须真的注册过
87
+ const prefixed = line.matchAll(/mcp__eplan__([a-z_]+)/g)
88
+ for (const match of prefixed) {
89
+ if (!known.has(match[1]) && match[1] !== '*') {
90
+ problems.push(`${label}:${index + 1} 提到了未注册的工具 mcp__eplan__${match[1]}`)
91
+ }
92
+ }
93
+ })
94
+ }
95
+
96
+ console.log(`实际注册工具(${registered.length}):${registered.join(', ')}`)
97
+
98
+ if (problems.length === 0) {
99
+ console.log(`\n文档一致性:通过(检查了 ${files.length} 个文件)`)
100
+ process.exit(0)
101
+ }
102
+
103
+ console.log(`\n文档一致性:${problems.length} 处不一致`)
104
+ for (const problem of problems.slice(0, 40)) console.log(' ✗ ' + problem)
105
+ if (problems.length > 40) console.log(` …还有 ${problems.length - 40} 处`)
106
+ process.exit(1)
@@ -0,0 +1,21 @@
1
+ # Print the current foreground window (pid + title) as one line of JSON.
2
+ # Used by test/smoke-all.mjs to prove that EPLAN does not steal focus.
3
+ [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding $false
4
+ Add-Type @"
5
+ using System;
6
+ using System.Runtime.InteropServices;
7
+ using System.Text;
8
+ public static class FgWin {
9
+ [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
10
+ [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowTextW(IntPtr hWnd, StringBuilder text, int count);
11
+ [DllImport("user32.dll")] public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);
12
+ }
13
+ "@
14
+
15
+ $handle = [FgWin]::GetForegroundWindow()
16
+ $buffer = New-Object System.Text.StringBuilder 512
17
+ [void][FgWin]::GetWindowTextW($handle, $buffer, 512)
18
+ $procId = 0
19
+ [void][FgWin]::GetWindowThreadProcessId($handle, [ref]$procId)
20
+
21
+ @{ pid = $procId; title = $buffer.ToString() } | ConvertTo-Json -Compress
@@ -0,0 +1,229 @@
1
+ // 预设自检:把 12 个工具挨个走一遍,只做安全操作。
2
+ //
3
+ // 目的不是测 EPLAN,而是测「这条链路还通不通」:
4
+ // 换了机器、升级了 DSH、改了 server.mjs,跑一遍就知道有没有断。
5
+ //
6
+ // node test/smoke-all.mjs 只读,不碰用户的图
7
+ // node test/smoke-all.mjs --write 额外跑 dryRun 和建页(会建一张 =SMOKE 临时页)
8
+ import { spawn, execFileSync } from 'node:child_process'
9
+ import { fileURLToPath } from 'node:url'
10
+ import path from 'node:path'
11
+
12
+ const here = path.dirname(fileURLToPath(import.meta.url))
13
+ const serverPath = path.join(here, '..', 'server', 'server.mjs')
14
+ const bundleRoot = path.join(here, '..')
15
+ const withWrite = process.argv.includes('--write')
16
+
17
+ const child = spawn(process.execPath, [serverPath], { stdio: ['pipe', 'pipe', 'inherit'] })
18
+
19
+ let nextId = 1
20
+ const pending = new Map()
21
+ let buffer = ''
22
+
23
+ child.stdout.on('data', (chunk) => {
24
+ buffer += chunk.toString('utf8')
25
+ let index
26
+ while ((index = buffer.indexOf('\n')) >= 0) {
27
+ const line = buffer.slice(0, index).trim()
28
+ buffer = buffer.slice(index + 1)
29
+ if (!line) continue
30
+ let message
31
+ try {
32
+ message = JSON.parse(line)
33
+ } catch {
34
+ continue
35
+ }
36
+ const resolver = pending.get(message.id)
37
+ if (resolver) {
38
+ pending.delete(message.id)
39
+ resolver(message)
40
+ }
41
+ }
42
+ })
43
+
44
+ function rpc(method, params) {
45
+ const id = nextId++
46
+ const payload = JSON.stringify({ jsonrpc: '2.0', id, method, params }) + '\n'
47
+ return new Promise((resolve, reject) => {
48
+ pending.set(id, resolve)
49
+ child.stdin.write(payload)
50
+ setTimeout(() => reject(new Error(`超时:${method} #${id}`)), 120000)
51
+ })
52
+ }
53
+
54
+ async function call(name, args = {}) {
55
+ const reply = await rpc('tools/call', { name, arguments: args })
56
+ if (reply.error) throw new Error(reply.error.message || JSON.stringify(reply.error))
57
+ const text = reply.result?.content?.[0]?.text ?? ''
58
+ if (reply.result?.isError) {
59
+ let detail = text
60
+ try {
61
+ detail = JSON.parse(text).message || text
62
+ } catch {}
63
+ throw new Error(detail)
64
+ }
65
+ return text
66
+ }
67
+
68
+ const results = []
69
+
70
+ // 读当前前台窗口的 pid。抢焦点是「用户能感觉到」的那一类 bug,
71
+ // 但这个测试跑在后台进程里,光看 API 返回看不出来,只能问系统。
72
+ function foregroundPid() {
73
+ if (process.platform !== 'win32') return null
74
+ try {
75
+ const raw = execFileSync(
76
+ 'powershell',
77
+ ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', path.join(bundleRoot, 'test', 'foreground.ps1')],
78
+ { encoding: 'utf8', timeout: 20000 },
79
+ )
80
+ const parsed = JSON.parse(raw.trim())
81
+ return typeof parsed.pid === 'number' ? parsed.pid : null
82
+ } catch {
83
+ return null
84
+ }
85
+ }
86
+
87
+ async function probe(label, name, args, { optional = false } = {}) {
88
+ const started = Date.now()
89
+ try {
90
+ const text = await call(name, args)
91
+ const ms = Date.now() - started
92
+ let brief = text.replace(/\s+/g, ' ').slice(0, 90)
93
+ try {
94
+ const parsed = JSON.parse(text)
95
+ const keys = Object.keys(parsed).slice(0, 6).join(',')
96
+ brief = keys || brief
97
+ } catch {}
98
+ results.push({ label, ok: true, optional, ms, brief })
99
+ console.log(` ✓ ${label.padEnd(20)} ${String(ms).padStart(5)} ms ${brief}`)
100
+ return text
101
+ } catch (error) {
102
+ results.push({ label, ok: false, optional, error: error.message })
103
+ const mark = optional ? '·' : '✗'
104
+ console.log(` ${mark} ${label.padEnd(20)} 失败:${error.message}`)
105
+ return undefined
106
+ }
107
+ }
108
+
109
+ await rpc('initialize', {
110
+ protocolVersion: '2025-11-25',
111
+ capabilities: {},
112
+ clientInfo: { name: 'eplan-smoke', version: '1' },
113
+ })
114
+
115
+ const listed = await rpc('tools/list', {})
116
+ const toolNames = (listed.result?.tools ?? []).map((tool) => tool.name)
117
+ console.log(`工具数:${toolNames.length}`)
118
+ console.log(`工具:${toolNames.join(', ')}\n`)
119
+
120
+ console.log('只读检查:')
121
+ const statusText = await probe('workspace.status', 'workspace', { action: 'status' })
122
+ // status 现在即使 EPLAN 不可达也会成功返回(ready:false + 经验),
123
+ // 所以不能再用「第一次探测失败」来判断环境。要看 ready 字段。
124
+ let eplanReady = results[0].ok
125
+ try {
126
+ if (statusText) eplanReady = JSON.parse(statusText)?.ready !== false
127
+ } catch {
128
+ // 解析不出来就按失败处理,别假装环境是好的。
129
+ eplanReady = false
130
+ }
131
+ // EPLAN 没开的话,后面的探针会全部退化成「参数校验失败」这种误导性错误,
132
+ // 看起来像是代码坏了。直接说清楚,别让人去查不存在的 bug。
133
+ if (!eplanReady) {
134
+ console.log('\nEPLAN 没在运行,后面的检查全部跳过。')
135
+ console.log('先打开 EPLAN Electric P8 并载入项目,再跑这个脚本。')
136
+ console.log('(经验库不依赖 EPLAN,lesson 仍然可用 —— 上面那一项已经验过。)')
137
+ child.kill()
138
+ process.exit(1)
139
+ }
140
+ await probe('workspace.list_pages', 'workspace', { action: 'list_pages' })
141
+ await probe('symbol_catalog.search', 'symbol_catalog', { action: 'search', filter: 'F', limit: 5 })
142
+ await probe('symbol_catalog.pins', 'symbol_catalog', {
143
+ action: 'pins',
144
+ library: 'GB_symbol',
145
+ symbols: ['F1'],
146
+ })
147
+ await probe('symbol_catalog.lookup', 'symbol_catalog', { action: 'lookup', query: '断路器' }, { optional: true })
148
+ await probe('operation_history', 'operation_history', {})
149
+ // 只读地读经验库:不写记录,避免自检本身往用户的经验文件里塞垃圾。
150
+ await probe('lesson.list', 'lesson', { action: 'list', limit: 3 }, { optional: true })
151
+
152
+ // 有页才能检查;拿第一页来试。
153
+ let firstPage = null
154
+ try {
155
+ const text = await call('workspace', { action: 'list_pages' })
156
+ firstPage = JSON.parse(text)?.pages?.[0]?.name ?? null
157
+ } catch {}
158
+
159
+ if (firstPage) {
160
+ await probe('validate', 'validate', { page: firstPage })
161
+ await probe('page_inspect.structure', 'page_inspect', { format: 'structure', page: firstPage })
162
+ await probe('page_inspect.image', 'page_inspect', { format: 'image', page: firstPage, width: 900 })
163
+ await probe('export', 'export', { pages: [firstPage], kind: 'png' })
164
+ } else {
165
+ console.log(' · 没有可用的页,跳过 validate/page_inspect/export')
166
+ }
167
+
168
+ if (withWrite) {
169
+ console.log('\n写入检查(dryRun 与临时页):')
170
+ await probe('page_create', 'page_create', { name: '=SMOKE/1', dryRun: true })
171
+ await probe('page_edit.draw_graphics', 'page_edit', {
172
+ action: 'draw_graphics',
173
+ page: firstPage,
174
+ dryRun: true,
175
+ items: [{ kind: 'text', at: [10, 10], text: 'smoke' }],
176
+ })
177
+ await probe('page_edit.insert_symbol', 'page_edit', {
178
+ action: 'insert_symbol',
179
+ page: firstPage,
180
+ symbol: 'F1',
181
+ at: [20, 20],
182
+ dryRun: true,
183
+ })
184
+ await probe('page_edit.set_properties', 'page_edit', {
185
+ action: 'set_properties',
186
+ page: firstPage,
187
+ get: ['PAGE_COUNTER'],
188
+ }, { optional: true })
189
+ await probe('manual.outline', 'manual', { action: 'outline' }, { optional: true })
190
+ // open_page 放最后:它一旦卡住(EPLAN 会弹「硬件错误:存储器访问受阻」模态框把主线程锁死),
191
+ // 后面的探针会跟着报「桥接器已退出」,看不出是谁真的坏了。
192
+ // open_page 不该抢焦点。取样只能包在一次调用外面,所以前后各读一次前台 pid。
193
+ const focusBefore = foregroundPid()
194
+ await probe(
195
+ 'workspace.open_page',
196
+ 'workspace',
197
+ { action: 'open_page', page: firstPage ?? '=SMOKE/1' },
198
+ { optional: true },
199
+ )
200
+ const focusAfter = foregroundPid()
201
+ if (focusBefore != null && focusAfter != null) {
202
+ const same = focusBefore === focusAfter
203
+ results.push({
204
+ label: 'focus.unchanged',
205
+ ok: same,
206
+ optional: false,
207
+ ms: 0,
208
+ brief: `前台 pid ${focusBefore} → ${focusAfter}`,
209
+ })
210
+ console.log(
211
+ same
212
+ ? ` ✓ ${'focus.unchanged'.padEnd(20)} 前台 pid 仍是 ${focusBefore}`
213
+ : ` ✗ ${'focus.unchanged'.padEnd(20)} 失败:前台 pid ${focusBefore} → ${focusAfter}` +
214
+ '(若你当时正在切换窗口,这一项可能是误报,请无人值守地重跑一次)',
215
+ )
216
+ }
217
+ } else {
218
+ console.log('\n(写入类工具已跳过;加 --write 一并检查)')
219
+ }
220
+
221
+ // optional 的失败不算失败:那是环境不具备(比如没给 PDF 文件),不是链路断了。
222
+ const failed = results.filter((item) => !item.ok && !item.optional)
223
+ const skipped = results.filter((item) => !item.ok && item.optional).length
224
+ const passed = results.length - skipped
225
+ const verdict = failed.length === 0 ? '全部通过' : `${failed.length} 项失败`
226
+ console.log(`\n${verdict}:${passed} 项通过` + (skipped > 0 ? `,${skipped} 项跳过` : ''))
227
+
228
+ child.kill()
229
+ process.exit(failed.length === 0 ? 0 : 1)