dsh-stm32 0.1.7 → 0.1.9

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 (153) hide show
  1. package/README.md +567 -516
  2. package/README_EN.md +544 -510
  3. package/client/client.js +7033 -682
  4. package/docs/reading-guide.md +921 -859
  5. package/lib/client.js +7033 -682
  6. package/lib/commands/register.js +2 -0
  7. package/lib/data/firmware-packages.js +35 -0
  8. package/lib/data/firmware-pins.js +7 -0
  9. package/lib/data/install-guides.js +110 -0
  10. package/lib/data/ioc-presets.js +118 -0
  11. package/lib/engine/artifacts.js +123 -0
  12. package/lib/engine/breakpoint-hits.js +73 -0
  13. package/lib/engine/build.js +161 -5
  14. package/lib/engine/channel-tap.js +227 -0
  15. package/lib/engine/channels.js +48 -0
  16. package/lib/engine/clocks.js +192 -0
  17. package/lib/engine/datasheet/auto.js +74 -0
  18. package/lib/engine/datasheet/digest.js +74 -0
  19. package/lib/engine/datasheet/lcsc.js +60 -0
  20. package/lib/engine/datasheet/pinout.js +119 -0
  21. package/lib/engine/datasheet/price.js +152 -0
  22. package/lib/engine/datasheet/search.js +147 -0
  23. package/lib/engine/datasheet/store.js +371 -0
  24. package/lib/engine/debug-session.js +48 -2
  25. package/lib/engine/ecad/autobuild.js +156 -0
  26. package/lib/engine/ecad/bom.js +238 -0
  27. package/lib/engine/ecad/brief.js +205 -0
  28. package/lib/engine/ecad/checks.js +239 -0
  29. package/lib/engine/ecad/design.js +177 -0
  30. package/lib/engine/ecad/dfm.js +314 -0
  31. package/lib/engine/ecad/easyeda.js +454 -0
  32. package/lib/engine/ecad/fabrication.js +104 -0
  33. package/lib/engine/ecad/gen/footprint.js +159 -0
  34. package/lib/engine/ecad/gen/symbol.js +103 -0
  35. package/lib/engine/ecad/import.js +372 -0
  36. package/lib/engine/ecad/jlcpcb.js +141 -0
  37. package/lib/engine/ecad/libs.js +316 -0
  38. package/lib/engine/ecad/models3d.js +89 -0
  39. package/lib/engine/ecad/netgate.js +286 -0
  40. package/lib/engine/ecad/normalize.js +182 -0
  41. package/lib/engine/ecad/package.js +126 -0
  42. package/lib/engine/ecad/padmerge.js +128 -0
  43. package/lib/engine/ecad/padprecheck.js +123 -0
  44. package/lib/engine/ecad/part-import.js +257 -0
  45. package/lib/engine/ecad/pcb/apply.js +53 -0
  46. package/lib/engine/ecad/pcb/emc-deep.js +396 -0
  47. package/lib/engine/ecad/pcb/emc.js +275 -0
  48. package/lib/engine/ecad/pcb/geometry.js +63 -0
  49. package/lib/engine/ecad/pcb/gerber.js +499 -0
  50. package/lib/engine/ecad/pcb/hand-sync.js +242 -0
  51. package/lib/engine/ecad/pcb/impedance.js +119 -0
  52. package/lib/engine/ecad/pcb/ipc-open.js +78 -0
  53. package/lib/engine/ecad/pcb/ipc-script.js +276 -0
  54. package/lib/engine/ecad/pcb/ipc.js +129 -0
  55. package/lib/engine/ecad/pcb/krt.js +182 -0
  56. package/lib/engine/ecad/pcb/links.js +393 -0
  57. package/lib/engine/ecad/pcb/mech.js +201 -0
  58. package/lib/engine/ecad/pcb/model-fix.js +178 -0
  59. package/lib/engine/ecad/pcb/netlist.js +146 -0
  60. package/lib/engine/ecad/pcb/pcb-svg.js +199 -0
  61. package/lib/engine/ecad/pcb/placement.js +140 -0
  62. package/lib/engine/ecad/pcb/power.js +203 -0
  63. package/lib/engine/ecad/pcb/render3d.js +208 -0
  64. package/lib/engine/ecad/pcb/route.js +167 -0
  65. package/lib/engine/ecad/pcb/router.js +469 -0
  66. package/lib/engine/ecad/pcb/rules.js +122 -0
  67. package/lib/engine/ecad/pcb/scene.js +513 -0
  68. package/lib/engine/ecad/pcb/stackup.js +154 -0
  69. package/lib/engine/ecad/pcb/swap.js +222 -0
  70. package/lib/engine/ecad/pcb/tracks.js +70 -0
  71. package/lib/engine/ecad/runtime.js +227 -0
  72. package/lib/engine/ecad/toolchain.js +146 -0
  73. package/lib/engine/ecad/tsci.js +362 -0
  74. package/lib/engine/ecad/zip.js +98 -0
  75. package/lib/engine/event-bus.js +158 -0
  76. package/lib/engine/event-log.js +335 -0
  77. package/lib/engine/fault-hints.js +146 -0
  78. package/lib/engine/generate-script.js +4 -1
  79. package/lib/engine/generate.js +145 -5
  80. package/lib/engine/interrupts.js +247 -0
  81. package/lib/engine/ioc-diff.js +369 -0
  82. package/lib/engine/ioc-plan.js +12 -2
  83. package/lib/engine/ioc-sync.js +58 -0
  84. package/lib/engine/mcu-info.js +134 -0
  85. package/lib/engine/msys2-install.js +31 -6
  86. package/lib/engine/panel-actions.js +103 -0
  87. package/lib/engine/pdf/curve.js +321 -0
  88. package/lib/engine/pdf/png.js +83 -0
  89. package/lib/engine/pdf/reader.js +336 -0
  90. package/lib/engine/pdf/schematic.js +93 -0
  91. package/lib/engine/pdf/tables.js +249 -0
  92. package/lib/engine/pdf/toolchain.js +50 -0
  93. package/lib/engine/pinout.js +140 -0
  94. package/lib/engine/probe-info.js +95 -0
  95. package/lib/engine/probe.js +45 -0
  96. package/lib/engine/progress.js +99 -0
  97. package/lib/engine/project-conf.js +254 -0
  98. package/lib/engine/project-layout.js +198 -0
  99. package/lib/engine/project-repair.js +205 -0
  100. package/lib/engine/schematic/checks.js +153 -0
  101. package/lib/engine/schematic/draw.js +390 -0
  102. package/lib/engine/schematic/edit.js +229 -0
  103. package/lib/engine/schematic/kicad-sch.js +225 -0
  104. package/lib/engine/schematic/model.js +442 -0
  105. package/lib/engine/schematic/nets.js +219 -0
  106. package/lib/engine/schematic/pcb.js +144 -0
  107. package/lib/engine/schematic/review.js +134 -0
  108. package/lib/engine/schematic/sexpr.js +134 -0
  109. package/lib/engine/schematic/to-ioc.js +197 -0
  110. package/lib/engine/schematic/transform.js +25 -0
  111. package/lib/engine/session-context.js +106 -0
  112. package/lib/engine/source-slice.js +59 -0
  113. package/lib/engine/tool-events.js +119 -0
  114. package/lib/engine/toolchain-gate.js +8 -0
  115. package/lib/engine/watch-admin.js +101 -0
  116. package/lib/engine/watch-record.js +39 -0
  117. package/lib/engine/watch-recorder.js +116 -0
  118. package/lib/engine/web-api.js +1123 -4
  119. package/lib/engine/web-routes.js +18 -1
  120. package/lib/engine/workspace-memo.js +41 -0
  121. package/lib/index.js +118 -24
  122. package/lib/skill/stm32-workflow.js +1 -1
  123. package/lib/tools/build.js +10 -2
  124. package/lib/tools/code-patch.js +2 -2
  125. package/lib/tools/datasheet.js +447 -0
  126. package/lib/tools/debug-session.js +9 -4
  127. package/lib/tools/debug.js +2 -2
  128. package/lib/tools/doctor.js +2 -2
  129. package/lib/tools/ecad.js +903 -0
  130. package/lib/tools/flash.js +2 -2
  131. package/lib/tools/gen.js +166 -0
  132. package/lib/tools/generate.js +7 -5
  133. package/lib/tools/import-circuit.js +146 -0
  134. package/lib/tools/ioc-apply.js +11 -2
  135. package/lib/tools/ioc-inspect.js +2 -2
  136. package/lib/tools/ioc-plan.js +2 -2
  137. package/lib/tools/ioc-verify.js +2 -2
  138. package/lib/tools/layout.js +92 -0
  139. package/lib/tools/mcu-info.js +163 -0
  140. package/lib/tools/part-import.js +182 -0
  141. package/lib/tools/pcb.js +2316 -0
  142. package/lib/tools/pdf.js +423 -0
  143. package/lib/tools/project-create.js +28 -0
  144. package/lib/tools/project-dir.js +25 -1
  145. package/lib/tools/regs.js +2 -2
  146. package/lib/tools/route-plan.js +2 -2
  147. package/lib/tools/rtt.js +10 -2
  148. package/lib/tools/sch-draw.js +433 -0
  149. package/lib/tools/sch-edit.js +145 -0
  150. package/lib/tools/schematic.js +817 -0
  151. package/lib/tools/serial.js +22 -0
  152. package/lib/tools/watch.js +24 -2
  153. package/package.json +1 -1
@@ -9,7 +9,7 @@ import { runFlash } from "../engine/flash.js";
9
9
  import { PROBE_ERROR_SCHEMA, formatProbeError } from "../engine/probe-error.js";
10
10
  import { releaseProbeFor } from "../engine/debug-session.js";
11
11
  import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
12
- import { resolveProjectDir } from "./project-dir.js";
12
+ import { resolveFirmwareDir } from "./project-dir.js";
13
13
  import { describeFacts, mcuFromIoc } from "../engine/facts.js";
14
14
  import { ensureFacts } from "../engine/facts-acquire.js";
15
15
  export const FLASH_TOOL = "stm32_flash";
@@ -124,7 +124,7 @@ export function registerFlash(ctx) {
124
124
  const report = emptyReport("宿主缺少 subprocess 服务,无法烧录");
125
125
  return { message: report.message, report: flashReportForHost(report) };
126
126
  }
127
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
127
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
128
128
  // 门禁:烧录只需要 openocd。缺件且 envCheck=block 时连 openocd 都不起,
129
129
  // 免得"探针被占 / 连不上"这类假象盖住真正的原因。
130
130
  const gate = await ensureToolchain(ctx, {
@@ -0,0 +1,166 @@
1
+ /**
2
+ * L3:符号与封装生成工具(S164)。
3
+ *
4
+ * stm32_symbol_footprint action=symbol 出 `.kicad_sym`;action=footprint 出 `.kicad_mod`
5
+ *
6
+ * 为什么单独成工具:它不读工程、不依赖 KiCad,只吃「引脚表」或「封装尺寸」这两组数。
7
+ * 数据来自手册(`stm32_pdf_read` 的 table)或人量的尺寸,生成只是排版与坐标算术;
8
+ * 尺寸缺一个就报错,不拿型号去猜。
9
+ */
10
+ import { mkdirSync, writeFileSync } from "node:fs";
11
+ import { dirname, isAbsolute, resolve } from "node:path";
12
+ import { buildFootprint } from "../engine/ecad/gen/footprint.js";
13
+ import { buildSymbol } from "../engine/ecad/gen/symbol.js";
14
+ import { resolveProjectDir } from "./project-dir.js";
15
+ export const GEN_TOOL = "stm32_symbol_footprint";
16
+ const PIN_KINDS = ["input", "output", "bidirectional", "power_in", "passive", "unspecified"];
17
+ const FOOTPRINT_KINDS = ["qfp", "soic", "passive"];
18
+ export function registerGenTools(ctx) {
19
+ ctx.tools.register({
20
+ name: GEN_TOOL,
21
+ description: "按引脚表生成 KiCad 符号(.kicad_sym),或按封装尺寸生成封装(.kicad_mod)。只做矩形本体 + 左右两列引脚、QFP/SOIC/两脚小件三类封装;尺寸与引脚号必须由调用方给出,缺一个就报错,不按型号猜。",
22
+ parameters: {
23
+ type: "object",
24
+ additionalProperties: false,
25
+ properties: {
26
+ action: { type: "string", enum: ["symbol", "footprint"], description: "生成符号还是封装。" },
27
+ projectDir: { type: "string", description: "工程根目录(saveAs 的相对路径按它解析);缺省为当前调试会话的工程。" },
28
+ name: { type: "string", description: "符号名或封装名,例如 STM32F103C8T6、LQFP-48_7x7_P0.5mm。" },
29
+ pins: {
30
+ type: "array",
31
+ description: "action=symbol 时的引脚表;顺序即画法顺序(左列先,右列接续)。",
32
+ items: {
33
+ type: "object",
34
+ additionalProperties: false,
35
+ properties: {
36
+ number: { type: "string", description: "引脚编号,例如 1、A1。" },
37
+ name: { type: "string", description: "引脚名,例如 PA0、VDD。" },
38
+ kind: { type: "string", enum: [...PIN_KINDS], description: "引脚类型;不给写 unspecified。" },
39
+ },
40
+ required: ["number", "name"],
41
+ },
42
+ },
43
+ bodyWidthMm: { type: "number", description: "本体宽(mm);符号不给按最长引脚名推,封装必填。" },
44
+ bodyHeightMm: { type: "number", description: "本体高(mm);双列封装不给按 pitch 推。" },
45
+ kind: { type: "string", enum: [...FOOTPRINT_KINDS], description: "封装类型:qfp / soic / passive。" },
46
+ pinCount: { type: "number", description: "封装引脚总数(qfp 要 4 的倍数、soic 要 2 的倍数)。" },
47
+ pitch: { type: "number", description: "引脚间距(mm)。" },
48
+ padWidthMm: { type: "number", description: "焊盘沿引脚方向的长度(mm)。" },
49
+ padHeightMm: { type: "number", description: "焊盘宽度(mm)。" },
50
+ padOffsetMm: { type: "number", description: "焊盘中心到本体中心的距离(mm);不给按本体与焊盘推。" },
51
+ referencePrefix: { type: "string", description: "符号的参考位号前缀,默认 U。" },
52
+ saveAs: { type: "string", description: "落盘路径(相对工程根),例如 libs/mcu.kicad_sym;不给只回文本。" },
53
+ },
54
+ required: ["action", "name"],
55
+ },
56
+ output: {
57
+ schema: {
58
+ type: "object",
59
+ additionalProperties: false,
60
+ properties: {
61
+ ok: { type: "boolean" },
62
+ error: { type: "string" },
63
+ action: { type: "string" },
64
+ name: { type: "string" },
65
+ savedTo: { type: "string" },
66
+ bytes: { type: "number" },
67
+ pinCount: { type: "number" },
68
+ bodyWidthMm: { type: "number" },
69
+ bodyHeightMm: { type: "number" },
70
+ text: { type: "string" },
71
+ notes: { type: "array", items: { type: "string" } },
72
+ },
73
+ required: ["ok", "error", "action", "name", "savedTo", "bytes", "pinCount", "bodyWidthMm", "bodyHeightMm", "text", "notes"],
74
+ },
75
+ render: (_args, value) => {
76
+ const result = value;
77
+ if (!result.ok)
78
+ return [{ type: "text", text: "生成失败:" + result.error }];
79
+ const what = result.action === "symbol" ? "符号" : "封装";
80
+ const lines = [
81
+ what + " " + result.name + " · 引脚 " + String(result.pinCount) + " 个" +
82
+ (result.bodyWidthMm > 0 ? " · 本体 " + String(result.bodyWidthMm) + " × " + String(result.bodyHeightMm) + " mm" : ""),
83
+ ];
84
+ if (result.savedTo !== "")
85
+ lines.push("已写:" + result.savedTo + "(" + String(result.bytes) + " B)");
86
+ for (const note of result.notes)
87
+ lines.push("· " + note);
88
+ return [{ type: "text", text: lines.join("\n") }];
89
+ },
90
+ },
91
+ timeoutMs: 30_000,
92
+ isConcurrencySafe: () => true,
93
+ execute: async (args, exec) => {
94
+ const action = typeof args.action === "string" ? args.action : "";
95
+ const name = typeof args.name === "string" ? args.name.trim() : "";
96
+ const base = {
97
+ ok: false,
98
+ error: "",
99
+ action,
100
+ name,
101
+ savedTo: "",
102
+ bytes: 0,
103
+ pinCount: 0,
104
+ bodyWidthMm: 0,
105
+ bodyHeightMm: 0,
106
+ text: "",
107
+ notes: [],
108
+ };
109
+ if (action !== "symbol" && action !== "footprint") {
110
+ return { ...base, error: "action 只能是 symbol 或 footprint" };
111
+ }
112
+ if (action === "footprint" && (args.kind === undefined || !FOOTPRINT_KINDS.includes(args.kind))) {
113
+ return { ...base, error: "生成封装要指定 kind:qfp / soic / passive" };
114
+ }
115
+ const built = action === "symbol"
116
+ ? buildSymbol({
117
+ name,
118
+ pins: Array.isArray(args.pins) ? args.pins : [],
119
+ ...(typeof args.bodyWidthMm === "number" ? { bodyWidthMm: args.bodyWidthMm } : {}),
120
+ ...(typeof args.referencePrefix === "string" ? { referencePrefix: args.referencePrefix } : {}),
121
+ })
122
+ : buildFootprint(footprintSpecOf(args, name));
123
+ if (!built.ok)
124
+ return { ...base, error: built.error };
125
+ const result = {
126
+ ...base,
127
+ ok: true,
128
+ pinCount: built.pinCount,
129
+ bodyWidthMm: "bodyWidthMm" in built ? built.bodyWidthMm : 0,
130
+ bodyHeightMm: "bodyHeightMm" in built ? built.bodyHeightMm : 0,
131
+ text: built.text,
132
+ notes: [...built.notes],
133
+ };
134
+ if (typeof args.saveAs !== "string" || args.saveAs.trim() === "")
135
+ return result;
136
+ const project = await resolveProjectDir(ctx, args.projectDir, exec);
137
+ const root = project.projectDir !== "" ? project.projectDir : process.cwd();
138
+ const target = isAbsolute(args.saveAs.trim()) ? args.saveAs.trim() : resolve(root, args.saveAs.trim());
139
+ try {
140
+ mkdirSync(dirname(target), { recursive: true });
141
+ writeFileSync(target, built.text, "utf8");
142
+ }
143
+ catch (error) {
144
+ return { ...result, ok: false, error: "写不进 " + target + ":" + (error instanceof Error ? error.message : String(error)) };
145
+ }
146
+ return { ...result, savedTo: target, bytes: Buffer.byteLength(built.text, "utf8") };
147
+ },
148
+ });
149
+ }
150
+ function footprintSpecOf(args, name) {
151
+ const kind = args.kind;
152
+ if (kind === undefined || !FOOTPRINT_KINDS.includes(kind)) {
153
+ return { kind: "qfp", name, pins: 0 };
154
+ }
155
+ return {
156
+ kind,
157
+ name,
158
+ ...(typeof args.pinCount === "number" ? { pins: args.pinCount } : {}),
159
+ ...(typeof args.pitch === "number" ? { pitch: args.pitch } : {}),
160
+ ...(typeof args.bodyWidthMm === "number" ? { bodyWidthMm: args.bodyWidthMm } : {}),
161
+ ...(typeof args.bodyHeightMm === "number" ? { bodyHeightMm: args.bodyHeightMm } : {}),
162
+ ...(typeof args.padWidthMm === "number" ? { padWidthMm: args.padWidthMm } : {}),
163
+ ...(typeof args.padHeightMm === "number" ? { padHeightMm: args.padHeightMm } : {}),
164
+ ...(typeof args.padOffsetMm === "number" ? { padOffsetMm: args.padOffsetMm } : {}),
165
+ };
166
+ }
@@ -10,7 +10,7 @@
10
10
  import { runGenerate } from "../engine/generate.js";
11
11
  import { releaseProbeFor } from "../engine/debug-session.js";
12
12
  import { ensureToolchain, gateReportNotes } from "../engine/toolchain-gate.js";
13
- import { resolveProjectDir } from "./project-dir.js";
13
+ import { resolveFirmwareDir } from "./project-dir.js";
14
14
  import { generationRiskNotes, mcuFromIoc } from "../engine/facts.js";
15
15
  import { ensureFacts } from "../engine/facts-acquire.js";
16
16
  export const GENERATE_TOOL = "stm32_generate";
@@ -184,19 +184,21 @@ export function registerGenerate(ctx) {
184
184
  foreground: { type: "boolean", description: "同步执行并返回完整报告(默认 false,走后台 job)。" },
185
185
  cubeMxRoots: { type: "array", items: { type: "string" }, description: "STM32CubeMX 安装根目录候选,优先于默认值/环境变量。" },
186
186
  envCheck: ENV_CHECK_SCHEMA,
187
- timeoutMs: { type: "number", description: "超时毫秒数,默认 600000(10 分钟)。" },
187
+ timeoutMs: { type: "number", description: "超时毫秒数,默认 1800000(30 分钟)。冷启动首次建索引实测 8–12 分钟,10 分钟会把正常的一轮判成超时。" },
188
188
  },
189
189
  required: [],
190
190
  },
191
- timeoutMs: 20 * 60_000,
191
+ timeoutMs: 35 * 60_000,
192
192
  isConcurrencySafe: () => false,
193
193
  output: {
194
194
  schema: outputSchema,
195
195
  render: (_args, value) => {
196
196
  const typed = value;
197
197
  const lines = typed.report === undefined ? [typed.message] : formatReport(typed.report);
198
- if (typed.mode === "job")
198
+ if (typed.mode === "job") {
199
199
  lines.push("job:" + typed.jobId + "(用 job_output 读进度,job_kill 停止)");
200
+ lines.push("等待方式:job_output 一次最多等 10 分钟;没结束就再等一次,但不要在参数完全相同的情况下连续轮询(宿主会判成重复调用直接拒)。");
201
+ }
200
202
  return [{ type: "text", text: lines.join("\n") }];
201
203
  },
202
204
  },
@@ -206,7 +208,7 @@ export function registerGenerate(ctx) {
206
208
  if (subprocess === undefined) {
207
209
  return { mode: "report", jobId: "", message: "宿主缺少 subprocess 服务,无法生成代码" };
208
210
  }
209
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
211
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
210
212
  const projectDir = resolution.projectDir;
211
213
  // R5:生成前把「这块板烧录门禁会怎样」摆出来——CubeMX 跑 3–4 分钟之前,
212
214
  // 用户/agent 就该知道 facts 缺不缺、烧录会不会被只读门禁拦。
@@ -0,0 +1,146 @@
1
+ /**
2
+ * S172:把 KiCad 里的改动吸收回 tscircuit 源。
3
+ *
4
+ * 只做两件事:读板子得出「元件与网络」,按 B 方案把新源写回 `index.circuit.tsx`。
5
+ * 不吸收坐标与画法——构建会重排,输出里每次都提示。
6
+ */
7
+ import { existsSync } from "node:fs";
8
+ import { dirname, join } from "node:path";
9
+ import { absorbFromKicad } from "../engine/ecad/import.js";
10
+ import { findLatestKicadBuild, planEcadWorkspace } from "../engine/ecad/tsci.js";
11
+ import { resolveProjectDir } from "./project-dir.js";
12
+ export const SCH_IMPORT_TOOL = "stm32_sch_import";
13
+ function renderImport(result) {
14
+ if (!result.ok)
15
+ return [{ type: "text", text: "吸收失败:" + result.error }];
16
+ const lines = [];
17
+ lines.push((result.written ? "已写回:" : "预览(未写盘):") + result.sourceFile);
18
+ lines.push("元件 " + String(result.parts.length) + " 个:" + result.parts.map((part) => part.ref + "(" + part.kind + ")").join("、"));
19
+ lines.push("命名网络 " + String(result.nets.length) + " 条" + (result.nets.length > 0 ? ":" + result.nets.map((net) => net.name + "(" + String(net.pins.length) + " 脚)").join("、") : ""));
20
+ if (result.diff.added.length > 0)
21
+ lines.push("新增:" + result.diff.added.join("、"));
22
+ if (result.diff.removed.length > 0)
23
+ lines.push("移除:" + result.diff.removed.join("、"));
24
+ if (result.diff.changed.length > 0)
25
+ lines.push("改值:" + result.diff.changed.join("、"));
26
+ for (const note of result.notes)
27
+ lines.push("说明:" + note);
28
+ lines.push("—— 生成的新源 ——");
29
+ lines.push(result.source);
30
+ return [{ type: "text", text: lines.join("\n") }];
31
+ }
32
+ const importOutput = {
33
+ type: "object",
34
+ additionalProperties: false,
35
+ properties: {
36
+ ok: { type: "boolean" },
37
+ error: { type: "string" },
38
+ projectDir: { type: "string" },
39
+ sourceFile: { type: "string" },
40
+ written: { type: "boolean" },
41
+ parts: {
42
+ type: "array",
43
+ items: {
44
+ type: "object",
45
+ additionalProperties: false,
46
+ properties: {
47
+ ref: { type: "string" },
48
+ kind: { type: "string" },
49
+ value: { type: "string" },
50
+ footprint: { type: "string" },
51
+ suppliers: { type: "array", items: { type: "string" } },
52
+ },
53
+ required: ["ref", "kind", "value", "footprint", "suppliers"],
54
+ },
55
+ },
56
+ nets: {
57
+ type: "array",
58
+ items: {
59
+ type: "object",
60
+ additionalProperties: false,
61
+ properties: { name: { type: "string" }, pins: { type: "array", items: { type: "string" } } },
62
+ required: ["name", "pins"],
63
+ },
64
+ },
65
+ diff: {
66
+ type: "object",
67
+ additionalProperties: false,
68
+ properties: {
69
+ added: { type: "array", items: { type: "string" } },
70
+ removed: { type: "array", items: { type: "string" } },
71
+ changed: { type: "array", items: { type: "string" } },
72
+ previousParts: { type: "number" },
73
+ previousNets: { type: "number" },
74
+ },
75
+ required: ["added", "removed", "changed", "previousParts", "previousNets"],
76
+ },
77
+ source: { type: "string" },
78
+ notes: { type: "array", items: { type: "string" } },
79
+ },
80
+ required: ["ok", "error", "projectDir", "sourceFile", "written", "parts", "nets", "diff", "source", "notes"],
81
+ };
82
+ export function registerImportTools(ctx) {
83
+ const tool = {
84
+ name: SCH_IMPORT_TOOL,
85
+ description: "把 KiCad 里的改动吸收回 tscircuit 源:读最近一次构建的 .kicad_pcb,得出元件清单与网络,生成新的 hardware/circuit/index.circuit.tsx。默认只出差异与新源(diff),write 才落盘(先留 .bak)。坐标与画法不保留,构建时会重排。",
86
+ parameters: {
87
+ type: "object",
88
+ additionalProperties: false,
89
+ properties: {
90
+ projectDir: { type: "string", description: "工程根目录;缺省为会话工作目录。" },
91
+ mode: { type: "string", enum: ["diff", "write"], description: "diff(默认)只出结果;write 写回源文件。" },
92
+ boardFile: { type: "string", description: "指定 .kicad_pcb;缺省取最近一次构建的板子。" },
93
+ sourceFile: { type: "string", description: "要写的 tscircuit 源;缺省 hardware/circuit/index.circuit.tsx。" },
94
+ },
95
+ },
96
+ output: { schema: importOutput, render: (_args, value) => renderImport(value) },
97
+ timeoutMs: 30_000,
98
+ isConcurrencySafe: () => true,
99
+ execute: async (args, exec) => {
100
+ const empty = {
101
+ ok: false,
102
+ error: "",
103
+ projectDir: "",
104
+ sourceFile: "",
105
+ written: false,
106
+ parts: [],
107
+ nets: [],
108
+ diff: { added: [], removed: [], changed: [], previousParts: 0, previousNets: 0 },
109
+ source: "",
110
+ notes: [],
111
+ };
112
+ const resolved = await resolveProjectDir(ctx, args.projectDir, exec);
113
+ if (resolved.projectDir === "")
114
+ return { ...empty, error: "projectDir 不能为空:从工具栏「工程」下拉里选一个,或先打开调试会话" };
115
+ const workspace = planEcadWorkspace(resolved.projectDir, join(resolved.projectDir, "hardware"));
116
+ const latest = findLatestKicadBuild(workspace);
117
+ const boardFile = args.boardFile !== undefined && args.boardFile !== "" ? args.boardFile : latest.ok ? latest.pcbFile : "";
118
+ if (boardFile === "" || !existsSync(boardFile)) {
119
+ return { ...empty, error: "还没有 .kicad_pcb:先跑一次 stm32_ecad_build,或在 KiCad 里存一次板子", projectDir: resolved.projectDir };
120
+ }
121
+ const sourceFile = args.sourceFile !== undefined && args.sourceFile !== "" ? args.sourceFile : join(workspace.circuitDir, "index.circuit.tsx");
122
+ const circuitJsonFile = join(dirname(boardFile), "..", "circuit.json");
123
+ const report = absorbFromKicad(resolved.projectDir, {
124
+ boardFile,
125
+ sourceFile,
126
+ circuitJsonFile,
127
+ write: args.mode === "write",
128
+ });
129
+ if (!report.ok)
130
+ return { ...empty, projectDir: resolved.projectDir, sourceFile, error: report.error, notes: report.notes };
131
+ return {
132
+ ok: true,
133
+ error: "",
134
+ projectDir: resolved.projectDir,
135
+ sourceFile: report.sourceFile,
136
+ written: report.written,
137
+ parts: report.state.parts.map((part) => ({ ref: part.ref, kind: part.kind, value: part.value, footprint: part.footprint, suppliers: part.suppliers })),
138
+ nets: report.state.nets.map((net) => ({ name: net.name, pins: net.pins.map((pin) => pin.ref + "." + pin.pin) })),
139
+ diff: report.diff,
140
+ source: report.source,
141
+ notes: report.notes,
142
+ };
143
+ },
144
+ };
145
+ ctx.tools.register(tool);
146
+ }
@@ -8,7 +8,8 @@
8
8
  * 不需要设备事实(只搬 .ioc 字节),所以不 ensureAssets。
9
9
  */
10
10
  import { applyIocPlan, rollbackIoc } from "../engine/ioc-plan.js";
11
- import { resolveProjectDir } from "./project-dir.js";
11
+ import { saveIocSnapshot } from "../engine/ioc-diff.js";
12
+ import { resolveFirmwareDir } from "./project-dir.js";
12
13
  export const IOC_APPLY_TOOL = "stm32_ioc_apply";
13
14
  export const outputSchema = {
14
15
  type: "object",
@@ -75,7 +76,7 @@ export function registerIocApply(ctx) {
75
76
  // 宿主可能不传 arguments(工具无参数调用)→ rawArgs 是 undefined。
76
77
  // 此处回退为 {},不假定 args 一定有值:缺参数的正常表现是"报告说缺 planId",而非 TypeError(审计 A31)。
77
78
  const args = rawArgs ?? {};
78
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
79
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
79
80
  const projectDir = resolution.projectDir;
80
81
  const extraNotes = resolution.note === null ? [] : [resolution.note];
81
82
  if (args.rollbackFrom !== undefined && args.rollbackFrom !== "") {
@@ -106,6 +107,14 @@ export function registerIocApply(ctx) {
106
107
  };
107
108
  }
108
109
  const applied = applyIocPlan({ projectDir, planId });
110
+ // 写入成功后自动记一次基线:变更页的「基线」就是上一次写入的结果,不必人手点「记下当前配置」。
111
+ if (applied.ok) {
112
+ const snapshot = saveIocSnapshot(projectDir);
113
+ if (snapshot.ok)
114
+ extraNotes.push("已把写入后的 .ioc 记成新基线:" + snapshot.report.iocPath);
115
+ else
116
+ extraNotes.push("基线未更新:" + snapshot.error);
117
+ }
109
118
  return {
110
119
  message: applied.message,
111
120
  mode: "apply",
@@ -11,7 +11,7 @@ import { readFileSync } from "node:fs";
11
11
  import { resolve } from "node:path";
12
12
  import { findIocFile } from "../engine/generate.js";
13
13
  import { summarizeIoc } from "../engine/ioc.js";
14
- import { resolveProjectDir } from "./project-dir.js";
14
+ import { resolveFirmwareDir } from "./project-dir.js";
15
15
  export const IOC_INSPECT_TOOL = "stm32_ioc_inspect";
16
16
  const pinSchema = {
17
17
  type: "object",
@@ -186,7 +186,7 @@ export function registerIocInspect(ctx) {
186
186
  },
187
187
  async execute(rawArgs, exec) {
188
188
  const args = rawArgs ?? {};
189
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
189
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
190
190
  const projectDir = resolution.projectDir;
191
191
  const iocPath = args.iocPath === undefined ? findIocFile(projectDir) : resolve(projectDir, args.iocPath);
192
192
  if (iocPath === null) {
@@ -12,7 +12,7 @@
12
12
  */
13
13
  import { planIocChanges } from "../engine/ioc-plan.js";
14
14
  import { formatHz } from "../engine/clock-plan.js";
15
- import { resolveProjectDir } from "./project-dir.js";
15
+ import { resolveFirmwareDir } from "./project-dir.js";
16
16
  export const IOC_PLAN_TOOL = "stm32_ioc_plan";
17
17
  const changeSchema = {
18
18
  type: "object",
@@ -375,7 +375,7 @@ export function registerIocPlan(ctx) {
375
375
  // 宿主可能不传 arguments(无参数调用)→ 兜成 {}:缺参数要表现成"报告说没事可做",
376
376
  // 而不是 rawArgs.projectDir 上的 TypeError(审计 A31)。
377
377
  const args = rawArgs ?? {};
378
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
378
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
379
379
  const report = planIocChanges({
380
380
  projectDir: resolution.projectDir,
381
381
  ...(args.iocPath === undefined ? {} : { iocPath: args.iocPath }),
@@ -11,7 +11,7 @@
11
11
  import { resolve } from "node:path";
12
12
  import { findIocFile } from "../engine/generate.js";
13
13
  import { runIocVerify } from "../engine/ioc-verify.js";
14
- import { resolveProjectDir } from "./project-dir.js";
14
+ import { resolveFirmwareDir } from "./project-dir.js";
15
15
  export const IOC_VERIFY_TOOL = "stm32_ioc_verify";
16
16
  const valueChangeList = {
17
17
  type: "array",
@@ -206,7 +206,7 @@ export function registerIocVerify(ctx) {
206
206
  async execute(rawArgs, exec) {
207
207
  const args = rawArgs ?? {};
208
208
  const subprocess = ctx.get("subprocess");
209
- const resolution = await resolveProjectDir(ctx, args.projectDir, exec);
209
+ const resolution = await resolveFirmwareDir(ctx, args.projectDir, exec);
210
210
  const empty = {
211
211
  ok: false, projectDir: resolution.projectDir, iocPath: "", snapshotPath: "", durationMs: 0,
212
212
  message: "", notes: [], errors: [], diff: {
@@ -0,0 +1,92 @@
1
+ /**
2
+ * 工程结构与配置文件归位(S128)。
3
+ *
4
+ * 默认只规划(dryRun),加 `apply=true` 才动文件;每份被改写的文件先备份成 `.bak`。
5
+ */
6
+ import { applyLayoutFix, planLayoutFix } from "../engine/project-repair.js";
7
+ import { resolveProjectDir } from "./project-dir.js";
8
+ export const LAYOUT_FIX_TOOL = "stm32_layout_fix";
9
+ const actionSchema = {
10
+ type: "object",
11
+ additionalProperties: false,
12
+ properties: {
13
+ kind: { type: "string", enum: ["index-move", "index-clean", "project-conf-move", "none"] },
14
+ from: { type: "string" },
15
+ to: { type: "string" },
16
+ note: { type: "string" },
17
+ },
18
+ required: ["kind", "from", "to", "note"],
19
+ };
20
+ export function registerLayoutTools(ctx) {
21
+ ctx.tools.register({
22
+ name: LAYOUT_FIX_TOOL,
23
+ description: "检查并把工程结构与配置文件归位:工作区索引应在 <工作区>/.dsh-stm32/workspace.conf," +
24
+ "工程描述应在工程根的 dsh-stm32.conf(ioc/build 相对工程根)。默认只报计划,apply=true 才改文件," +
25
+ "改动前备份成 .bak。",
26
+ parameters: {
27
+ type: "object",
28
+ additionalProperties: false,
29
+ properties: {
30
+ projectDir: { type: "string", description: "工作区或工程目录;缺省为会话工作目录。" },
31
+ apply: { type: "boolean", description: "true 才真正改文件;默认只规划。" },
32
+ },
33
+ required: [],
34
+ },
35
+ output: {
36
+ schema: {
37
+ type: "object",
38
+ additionalProperties: false,
39
+ properties: {
40
+ ok: { type: "boolean" },
41
+ applied: { type: "boolean" },
42
+ error: { type: "string" },
43
+ workspaceRoot: { type: "string" },
44
+ actions: { type: "array", items: actionSchema },
45
+ notes: { type: "array", items: { type: "string" } },
46
+ },
47
+ required: ["ok", "applied", "error", "workspaceRoot", "actions", "notes"],
48
+ },
49
+ render: (_args, value) => {
50
+ const result = value;
51
+ const lines = [result.applied ? "已归位:" : "计划(未改文件):", "工作区:" + result.workspaceRoot];
52
+ if (result.actions.length === 0)
53
+ lines.push("结构已符合约定,无需调整。");
54
+ for (const action of result.actions)
55
+ lines.push("- " + action.kind + ":" + action.note);
56
+ for (const note of result.notes)
57
+ lines.push("· " + note);
58
+ if (!result.ok)
59
+ lines.push("有失败项:" + result.error);
60
+ return [{ type: "text", text: lines.join("\n") }];
61
+ },
62
+ },
63
+ timeoutMs: 60_000,
64
+ isConcurrencySafe: () => false,
65
+ execute: async (args, exec) => {
66
+ const project = await resolveProjectDir(ctx, args.projectDir, exec);
67
+ const notes = project.note === null ? [] : [project.note];
68
+ if (project.fallbackToProcessCwd && project.note === null)
69
+ notes.push("没有会话工作目录,按插件进程目录处理");
70
+ if (args.apply === true) {
71
+ const fixed = applyLayoutFix(project.projectDir);
72
+ return {
73
+ ok: fixed.ok,
74
+ applied: true,
75
+ error: fixed.error,
76
+ workspaceRoot: fixed.workspaceRoot,
77
+ actions: fixed.actions.map((item) => ({ kind: item.kind, from: item.from, to: item.to, note: item.note })),
78
+ notes: [...notes, ...fixed.notes],
79
+ };
80
+ }
81
+ const plan = planLayoutFix(project.projectDir);
82
+ return {
83
+ ok: plan.ok,
84
+ applied: false,
85
+ error: plan.error,
86
+ workspaceRoot: plan.workspaceRoot,
87
+ actions: plan.actions.map((item) => ({ kind: item.kind, from: item.from, to: item.to, note: item.note })),
88
+ notes: [...notes, ...plan.notes],
89
+ };
90
+ },
91
+ });
92
+ }