@turingcode/cli 0.2.12 → 0.3.4

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 (186) hide show
  1. package/README.md +2 -0
  2. package/dist/cli/create-cli.d.ts +10 -0
  3. package/dist/cli/create-cli.js +12 -2
  4. package/dist/cli/index.d.ts +9 -0
  5. package/dist/cli/index.js +5 -1
  6. package/dist/core/bound-error.d.ts +10 -0
  7. package/dist/core/date.d.ts +12 -0
  8. package/dist/core/exact/expression.d.ts +49 -0
  9. package/dist/core/exact/fraction.d.ts +85 -0
  10. package/dist/core/files.d.ts +44 -0
  11. package/dist/core/files.js +46 -1
  12. package/dist/core/json-shape.d.ts +45 -0
  13. package/dist/core/json-shape.js +0 -43
  14. package/dist/core/mcp.d.ts +92 -0
  15. package/dist/core/mcp.js +57 -30
  16. package/dist/core/query.d.ts +69 -0
  17. package/dist/core/query.js +11 -0
  18. package/dist/core/report.d.ts +11 -0
  19. package/dist/core/tables/csv.d.ts +18 -0
  20. package/dist/core/tables/markdown.d.ts +22 -0
  21. package/dist/core/tables/types.d.ts +25 -0
  22. package/dist/features/contract/check.d.ts +30 -0
  23. package/dist/features/contract/check.js +2 -3
  24. package/dist/features/contract/command.d.ts +12 -0
  25. package/dist/features/contract/command.js +64 -64
  26. package/dist/features/contract/describe.d.ts +22 -0
  27. package/dist/features/contract/diagnostics.d.ts +108 -0
  28. package/dist/features/contract/load.d.ts +58 -0
  29. package/dist/features/contract/load.js +24 -17
  30. package/dist/features/contract/model.d.ts +67 -0
  31. package/dist/features/contract/rules.d.ts +137 -0
  32. package/dist/features/contract/rules.js +11 -12
  33. package/dist/features/debug/clear_breakpoint.d.ts +2 -0
  34. package/dist/features/debug/clear_breakpoint.js +24 -0
  35. package/dist/features/debug/command.d.ts +5 -0
  36. package/dist/features/debug/command.js +66 -0
  37. package/dist/features/debug/continue.d.ts +2 -0
  38. package/dist/features/debug/continue.js +22 -0
  39. package/dist/features/debug/debug_start.d.ts +2 -0
  40. package/dist/features/debug/debug_start.js +22 -0
  41. package/dist/features/debug/debug_stop.d.ts +2 -0
  42. package/dist/features/debug/debug_stop.js +11 -0
  43. package/dist/features/debug/evaluate.d.ts +2 -0
  44. package/dist/features/debug/evaluate.js +48 -0
  45. package/dist/features/debug/get_stack.d.ts +2 -0
  46. package/dist/features/debug/get_stack.js +14 -0
  47. package/dist/features/debug/get_variables.d.ts +2 -0
  48. package/dist/features/debug/get_variables.js +26 -0
  49. package/dist/features/debug/halt.d.ts +2 -0
  50. package/dist/features/debug/halt.js +16 -0
  51. package/dist/features/debug/set_breakpoint.d.ts +2 -0
  52. package/dist/features/debug/set_breakpoint.js +30 -0
  53. package/dist/features/debug/step.d.ts +2 -0
  54. package/dist/features/debug/step.js +23 -0
  55. package/dist/features/install-mods/command.d.ts +6 -0
  56. package/dist/features/install-mods/command.js +18 -0
  57. package/dist/features/install-mods/install.d.ts +23 -0
  58. package/dist/features/install-mods/install.js +48 -0
  59. package/dist/features/install-skills/command.d.ts +7 -0
  60. package/dist/features/install-skills/command.js +22 -0
  61. package/dist/features/install-skills/install.d.ts +13 -0
  62. package/dist/features/install-skills/install.js +29 -0
  63. package/dist/features/probe/command.d.ts +5 -0
  64. package/dist/features/probe/command.js +44 -0
  65. package/dist/features/probe/kill_all_debuggers.d.ts +2 -0
  66. package/dist/features/probe/kill_all_debuggers.js +50 -0
  67. package/dist/features/probe/list_probes.d.ts +2 -0
  68. package/dist/features/probe/list_probes.js +8 -0
  69. package/dist/features/probe/program_project.d.ts +2 -0
  70. package/dist/features/probe/program_project.js +29 -0
  71. package/dist/features/probe/read_fault_status.d.ts +2 -0
  72. package/dist/features/probe/read_fault_status.js +19 -0
  73. package/dist/features/probe/read_memory.d.ts +2 -0
  74. package/dist/features/probe/read_memory.js +36 -0
  75. package/dist/features/probe/read_peripheral.d.ts +2 -0
  76. package/dist/features/probe/read_peripheral.js +58 -0
  77. package/dist/features/probe/reset_probe_usb.d.ts +2 -0
  78. package/dist/features/probe/reset_probe_usb.js +17 -0
  79. package/dist/features/probe/reset_target.d.ts +2 -0
  80. package/dist/features/probe/reset_target.js +16 -0
  81. package/dist/features/probe/target_info.d.ts +2 -0
  82. package/dist/features/probe/target_info.js +34 -0
  83. package/dist/features/project/analysis_static.d.ts +2 -0
  84. package/dist/features/project/analysis_static.js +19 -0
  85. package/dist/features/project/analysis_syntax_check.d.ts +2 -0
  86. package/dist/features/project/analysis_syntax_check.js +21 -0
  87. package/dist/features/project/command.d.ts +5 -0
  88. package/dist/features/project/command.js +64 -0
  89. package/dist/features/project/project_build.d.ts +2 -0
  90. package/dist/features/project/project_build.js +22 -0
  91. package/dist/features/project/project_download_template.d.ts +2 -0
  92. package/dist/features/project/project_download_template.js +34 -0
  93. package/dist/features/project/project_regenerate.d.ts +2 -0
  94. package/dist/features/project/project_regenerate.js +20 -0
  95. package/dist/features/project/project_sync_from_server.d.ts +2 -0
  96. package/dist/features/project/project_sync_from_server.js +13 -0
  97. package/dist/features/project/project_sync_to_server.d.ts +2 -0
  98. package/dist/features/project/project_sync_to_server.js +51 -0
  99. package/dist/features/project/stm32_cubetools_download_drivers.d.ts +2 -0
  100. package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
  101. package/dist/features/status/command.d.ts +2 -0
  102. package/dist/features/status/command.js +6 -0
  103. package/dist/features/status/get_status.d.ts +2 -0
  104. package/dist/features/status/get_status.js +44 -0
  105. package/dist/features/table/check.d.ts +38 -0
  106. package/dist/features/table/check.js +16 -23
  107. package/dist/features/table/command.d.ts +3 -0
  108. package/dist/features/table/command.js +4 -5
  109. package/dist/features/table/read.d.ts +28 -0
  110. package/dist/features/table/read.js +23 -3
  111. package/dist/features/tool/command.d.ts +35 -0
  112. package/dist/lib.d.ts +11 -0
  113. package/dist/lib.js +9 -0
  114. package/dist/stm32/actions/DebugAction.d.ts +63 -0
  115. package/dist/stm32/actions/DebugAction.js +105 -0
  116. package/dist/stm32/actions/DebugActionInfo.d.ts +7 -0
  117. package/dist/stm32/actions/DebugActionInfo.js +9 -0
  118. package/dist/stm32/actions/DebugActionListProbes.d.ts +9 -0
  119. package/dist/stm32/actions/DebugActionListProbes.js +10 -0
  120. package/dist/stm32/actions/DebugActionProgram.d.ts +28 -0
  121. package/dist/stm32/actions/DebugActionProgram.js +79 -0
  122. package/dist/stm32/actions/DebugActionRead.d.ts +38 -0
  123. package/dist/stm32/actions/DebugActionRead.js +59 -0
  124. package/dist/stm32/actions/DebugActionReset.d.ts +11 -0
  125. package/dist/stm32/actions/DebugActionReset.js +9 -0
  126. package/dist/stm32/actions/DebugActionServer.d.ts +7 -0
  127. package/dist/stm32/actions/DebugActionServer.js +9 -0
  128. package/dist/stm32/analysis.d.ts +19 -0
  129. package/dist/stm32/analysis.js +21 -0
  130. package/dist/stm32/build.d.ts +34 -0
  131. package/dist/stm32/build.js +79 -0
  132. package/dist/stm32/context.d.ts +23 -0
  133. package/dist/stm32/context.js +42 -0
  134. package/dist/stm32/daemon.d.ts +63 -0
  135. package/dist/stm32/daemon.js +202 -0
  136. package/dist/stm32/regen.d.ts +68 -0
  137. package/dist/stm32/regen.js +225 -0
  138. package/dist/stm32/session.d.ts +134 -0
  139. package/dist/stm32/session.js +374 -0
  140. package/dist/stm32/sync.d.ts +24 -0
  141. package/dist/stm32/sync.js +72 -0
  142. package/dist/stm32/tool.d.ts +43 -0
  143. package/dist/stm32/tool.js +20 -0
  144. package/dist/stm32/toolCommand.d.ts +50 -0
  145. package/dist/stm32/toolCommand.js +102 -0
  146. package/dist/stm32/utils/discover_tools.d.ts +24 -0
  147. package/dist/stm32/utils/discover_tools.js +121 -0
  148. package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
  149. package/dist/stm32/utils/downloadzip_utils.js +180 -0
  150. package/dist/stm32/utils/faultUtils.d.ts +14 -0
  151. package/dist/stm32/utils/faultUtils.js +69 -0
  152. package/dist/stm32/utils/findElf.d.ts +9 -0
  153. package/dist/stm32/utils/findElf.js +19 -0
  154. package/dist/stm32/utils/hash_utils.d.ts +19 -0
  155. package/dist/stm32/utils/hash_utils.js +28 -0
  156. package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
  157. package/dist/stm32/utils/listSyncFiles.js +98 -0
  158. package/dist/stm32/utils/mcu_utils.d.ts +59 -0
  159. package/dist/stm32/utils/mcu_utils.js +99 -0
  160. package/dist/stm32/utils/project_utils.d.ts +54 -0
  161. package/dist/stm32/utils/project_utils.js +102 -0
  162. package/dist/stm32/utils/svdUtils.d.ts +31 -0
  163. package/dist/stm32/utils/svdUtils.js +30 -0
  164. package/dist/stm32/utils/truncate.d.ts +7 -0
  165. package/dist/stm32/utils/truncate.js +9 -0
  166. package/dist/stm32/utils/usb_utils.d.ts +5 -0
  167. package/dist/stm32/utils/usb_utils.js +23 -0
  168. package/dist/stm32/utils/user_code.d.ts +14 -0
  169. package/dist/stm32/utils/user_code.js +43 -0
  170. package/dist/stm32/utils/utils_mutex.d.ts +15 -0
  171. package/dist/stm32/utils/utils_mutex.js +93 -0
  172. package/dist/stm32/validate.d.ts +16 -0
  173. package/dist/stm32/validate.js +46 -0
  174. package/mods/claude/.claude-plugin/marketplace.json +8 -0
  175. package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
  176. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +50 -0
  177. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +22 -0
  178. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +19 -0
  179. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +9 -0
  180. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +43 -0
  181. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
  182. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
  183. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +34 -0
  184. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
  185. package/package.json +11 -3
  186. package/dist/features/clock/command.js +0 -63
@@ -0,0 +1,58 @@
1
+ import { callServer } from '../../stm32/context.js';
2
+ import { _defineTool } from '../../stm32/tool.js';
3
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
4
+ import { DebugActionRead } from '../../stm32/actions/DebugActionRead.js';
5
+ import { hex } from '../../stm32/utils/faultUtils.js';
6
+ import { getMcuFromIoc } from '../../stm32/utils/mcu_utils.js';
7
+ import { loadPeripheral } from '../../stm32/utils/svdUtils.js';
8
+ // Reads a peripheral's registers and decodes them with the MCU's SVD data from the TuringCode server.
9
+ export default _defineTool({
10
+ name: 'read_peripheral',
11
+ description: "Read and decode a peripheral's registers (e.g. GPIOA, USART2, TIM1, RCC) over SWD (probe-rs or STM32_Programmer_CLI), using the MCU's SVD data from the TuringCode server (needs TURINGCODE_TOKEN the first time per peripheral, then cached in .turingcode/svd/ and works offline). Does not halt the core. Data/FIFO registers whose read has side effects are skipped. Lists non-zero fields only.",
12
+ properties: { name: { type: 'string', pattern: '^[A-Za-z0-9_]{1,32}$' } },
13
+ required: ['name'],
14
+ needsProbe: true,
15
+ run: async (args) => {
16
+ const project = await findStm32Project();
17
+ const chip = await getMcuFromIoc(project.root);
18
+ // The server sorts registers by offset and flags those not safe to read; cached after the first download
19
+ const { peripheral: p } = await loadPeripheral(project.root, chip, args.name, callServer);
20
+ const regs = p.registers.filter((r) => r.safeToRead);
21
+ const skipped = p.registers.filter((r) => !r.safeToRead).map((r) => r.name);
22
+ // Group registers into runs with gaps of at most 0x40 bytes, so each run is one memory read.
23
+ // A register not safe to read ends the run, so no read spans it.
24
+ const runs = [];
25
+ let open = false;
26
+ for (const r of p.registers) {
27
+ if (!r.safeToRead) {
28
+ open = false;
29
+ continue;
30
+ }
31
+ const last = runs.at(-1);
32
+ if (open && last && r.addressOffsetInt - last[last.length - 1].addressOffsetInt <= 0x40)
33
+ last.push(r);
34
+ else
35
+ runs.push([r]);
36
+ open = true;
37
+ }
38
+ const values = new Map();
39
+ for (const run of runs) {
40
+ // Read whole words; a 16-bit register at offset 2 is the upper half of its word
41
+ const start = run[0].addressOffsetInt & ~3;
42
+ const { words } = await DebugActionRead.run_action({ chip, address: p.baseAddressInt + start, width: 32, count: ((run[run.length - 1].addressOffsetInt - start) >> 2) + 1 });
43
+ for (const r of run)
44
+ values.set(r, words[(r.addressOffsetInt - start) >> 2] >>> ((r.addressOffsetInt & 3) * 8));
45
+ }
46
+ const lines = regs.map((r) => {
47
+ const v = values.get(r);
48
+ const fields = r.fields
49
+ .map((f) => ({ ...f, value: f.bitWidth >= 32 ? v : (v >>> f.bitOffset) & ((1 << f.bitWidth) - 1) }))
50
+ .filter((f) => f.value && !(r.fields.length === 1 && f.name === r.name))
51
+ .map((f) => `${f.name}=${f.bitWidth === 1 ? f.value : hex(f.value, 1)}`);
52
+ return `${r.name.padEnd(10)} ${hex(p.baseAddressInt + r.addressOffsetInt)} = ${hex(v)}${fields.length ? ` ${fields.join(' ')}` : ''}`;
53
+ });
54
+ if (skipped.length)
55
+ lines.push('', `Not read: ${skipped.join(', ')} (write-only or reading has side effects)`);
56
+ return `${p.name} @ ${hex(p.baseAddressInt)} (${chip}, ${p.svd})\n\n${lines.join('\n')}`;
57
+ },
58
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,17 @@
1
+ import { _defineTool } from '../../stm32/tool.js';
2
+ import { claimProbe, releaseProbe } from '../../stm32/utils/utils_mutex.js';
3
+ import { resetStlinkUsb } from '../../stm32/utils/usb_utils.js';
4
+ // Wakes an ST-LINK whose USB link went to sleep, which makes programming fail.
5
+ export default _defineTool({
6
+ name: 'reset_probe_usb',
7
+ description: 'Restart the USB connection of every connected ST-LINK probe (Windows, pnputil). Use when programming or debugging fails \
8
+ because the probe does not answer (e.g. it is in USB sleep, DEV_USB_COMM_ERR, "probe not found"). Needs administrator rights.',
9
+ needsProbe: true,
10
+ run: async () => {
11
+ claimProbe('reset_probe_usb');
12
+ const restarted = await resetStlinkUsb().finally(releaseProbe);
13
+ return restarted.length
14
+ ? `Restarted: ${restarted.join(', ')}`
15
+ : 'No ST-LINK USB device is connected';
16
+ },
17
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,16 @@
1
+ import { _defineTool } from '../../stm32/tool.js';
2
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
3
+ import { getMcuFromIoc } from '../../stm32/utils/mcu_utils.js';
4
+ import { DebugActionReset } from '../../stm32/actions/DebugActionReset.js';
5
+ // Resets the board over SWD; the chip comes from the project's .ioc.
6
+ export default _defineTool({
7
+ name: 'reset_target',
8
+ description: "Hardware reset of the project's target board over SWD (probe-rs or STM32_Programmer_CLI).",
9
+ needsProbe: true,
10
+ run: async (_args, ctx) => {
11
+ const project = await findStm32Project();
12
+ const chip = await getMcuFromIoc(project.root);
13
+ const reset_result = await DebugActionReset.run_action_get_tail({ chip });
14
+ return `Reset ${chip}\n\n${reset_result}`;
15
+ },
16
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import { DebugActionInfo } from '../../stm32/actions/DebugActionInfo.js';
2
+ import { callServer } from '../../stm32/context.js';
3
+ import { _defineTool } from '../../stm32/tool.js';
4
+ import { deviceLines, lineFamily, matchMcuNames, parseTargetInfo } from '../../stm32/utils/mcu_utils.js';
5
+ // Identifies the connected chip and lists the CubeMX names that fit it, for `project template --mcu`.
6
+ export default _defineTool({
7
+ name: 'target_info',
8
+ description: 'Identify the chip connected to the debug probe (STM32_Programmer_CLI, without resetting it): device ID, name, flash size, core and voltage, ' +
9
+ 'and the CubeMX MCU names that match it for `turingcode project template --mcu` (needs TURINGCODE_TOKEN; the package and pin count cannot be read over SWD). Needs no project.',
10
+ needsProbe: true,
11
+ run: async () => {
12
+ const out = await DebugActionInfo.run_action({});
13
+ const info = parseTargetInfo(out.stdout + out.stderr);
14
+ if (!info.name)
15
+ throw new Error(`No target found: ${DebugActionInfo.tail(out)}`);
16
+ const lines = [
17
+ `Device: ${info.name} (ID ${info.deviceId ?? '?'})`,
18
+ `Flash: ${info.flashKb ?? '?'} KB, CPU: ${info.cpu ?? '?'}, voltage: ${info.voltage ?? '?'}`,
19
+ ];
20
+ const families = [...new Set(deviceLines(info.name).map(lineFamily))];
21
+ try {
22
+ const names = (await Promise.all(families.map((family) => callServer('list_mcu_names', { family }))))
23
+ .flatMap((text) => text.split('\n').map((l) => l.trim()).filter((l) => /^STM32\w+$/.test(l)));
24
+ const matches = matchMcuNames(names, info);
25
+ lines.push(matches.length
26
+ ? `MCU names for \`project template --mcu\` (pick the package; it can't be read over SWD): ${matches.join(', ')}`
27
+ : `No CubeMX MCU name matches; list them with \`turingcode tool call list_mcu_names --args '{"family":"${families[0]}"}'\``);
28
+ }
29
+ catch (e) {
30
+ lines.push(`MCU names not listed: ${e.message.split('\n')[0]}`);
31
+ }
32
+ return lines.join('\n');
33
+ },
34
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,19 @@
1
+ import { analyzeProject } from '../../stm32/analysis.js';
2
+ import { callServer } from '../../stm32/context.js';
3
+ import { _defineTool } from '../../stm32/tool.js';
4
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
5
+ // Static analysis of the server copy of the project's own sources (never Drivers/).
6
+ export default _defineTool({
7
+ name: 'analysis_static',
8
+ description: 'Static analysis of your stm32 project on the TuringCode server (its own sources, never Drivers/HAL/CMSIS; \
9
+ run `turingcode project sync` first to include local changes). Profiles: quick (cppcheck), full (cppcheck + clang-tidy) \
10
+ or misra (cppcheck MISRA C:2012). Returns `file:line:col: severity: message [check id]` lines.',
11
+ properties: {
12
+ profile: { type: 'string', enum: ['quick', 'full', 'misra'], description: 'default: quick' },
13
+ paths: { type: 'string', description: 'Comma-separated files, globs or folders to limit the check to (default: whole project)' },
14
+ },
15
+ run: async (args) => {
16
+ const project = await findStm32Project();
17
+ return analyzeProject(callServer, project, args.profile, args.paths);
18
+ },
19
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,21 @@
1
+ import { syntaxCheckProject } from '../../stm32/analysis.js';
2
+ import { callServer } from '../../stm32/context.js';
3
+ import { _defineTool } from '../../stm32/tool.js';
4
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
5
+ // Syntax-checks files of the server copy; local project-relative paths are mapped to projects/<name>/.
6
+ export default _defineTool({
7
+ name: 'analysis_syntax_check',
8
+ description: 'Check .c/.h/.cpp/.hpp files of your stm32 project for syntax errors on the TuringCode server \
9
+ (run `turingcode project sync` first to include local changes). Returns `file:line:col: severity: message` lines.',
10
+ properties: {
11
+ files: {
12
+ type: 'string',
13
+ description: 'Comma-separated project-relative files, globs or folders, e.g. Core/Src/main.c (default: Core,App)',
14
+ },
15
+ },
16
+ run: async (args) => {
17
+ const project = await findStm32Project();
18
+ const files = (args.files || 'Core,App').split(',').map(f => f.trim()).filter(Boolean);
19
+ return syntaxCheckProject(callServer, project, files);
20
+ },
21
+ });
@@ -0,0 +1,5 @@
1
+ import { CommandModule } from "yargs";
2
+ /** How the commands find the project; shown in the help of every STM32 command group. */
3
+ export declare const PROJECT_HELP: string;
4
+ declare const command: CommandModule;
5
+ export default command;
@@ -0,0 +1,64 @@
1
+ import { TOKEN_HELP } from "../../core/mcp.js";
2
+ import { toolCommand } from "../../stm32/toolCommand.js";
3
+ import analysisStatic from "./analysis_static.js";
4
+ import analysisSyntaxCheck from "./analysis_syntax_check.js";
5
+ import projectBuild from "./project_build.js";
6
+ import projectDownloadTemplate from "./project_download_template.js";
7
+ import projectRegenerate from "./project_regenerate.js";
8
+ import projectSyncFromServer from "./project_sync_from_server.js";
9
+ import projectSyncToServer from "./project_sync_to_server.js";
10
+ import downloadDrivers from "./stm32_cubetools_download_drivers.js";
11
+ /** How the commands find the project; shown in the help of every STM32 command group. */
12
+ export const PROJECT_HELP = [
13
+ "Project: the nearest folder at or above the working directory that holds .turingcode/ (made by the",
14
+ "VS Code extension and the debug daemon; not the home folder's) or a .ioc or .mxproject file, else the",
15
+ "working directory. The STM32 project is that folder or the one subfolder holding a .ioc or .mxproject file.",
16
+ ].join("\n");
17
+ const subcommands = [
18
+ toolCommand({ command: "sync", tool: projectSyncToServer, mode: "local", server: true, aliases: { build: "b", analyse: "a" }, examples: [
19
+ ["$0 project sync", "upload the files the server lacks or has a different version of"],
20
+ ["$0 project sync -b", "sync, then build with the Debug preset"],
21
+ ["$0 project sync -b Release -a misra", "sync, build Release, then MISRA static analysis"],
22
+ ] }),
23
+ toolCommand({ command: "pull", tool: projectSyncFromServer, mode: "local", server: true, examples: [
24
+ ["$0 project pull", "download the files that changed on the server, overwriting local copies"],
25
+ ] }),
26
+ toolCommand({ command: "build", tool: projectBuild, mode: "local", server: true, examples: [
27
+ ["$0 project build", "build the Debug preset and download the .elf into build/Debug/"],
28
+ ["$0 project build --preset Release", "build another preset"],
29
+ ] }),
30
+ toolCommand({ command: "check", tool: analysisSyntaxCheck, mode: "local", server: true, examples: [
31
+ ["$0 project check", "syntax-check Core/ and App/ of the synced project"],
32
+ ["$0 project check --files Core/Src/main.c", "one file"],
33
+ ] }),
34
+ toolCommand({ command: "analyze", tool: analysisStatic, mode: "local", server: true, examples: [
35
+ ["$0 project analyze", "quick static analysis (cppcheck) of the synced project"],
36
+ ["$0 project analyze --profile misra --paths Core/Src", "MISRA C:2012 check of one folder"],
37
+ ] }),
38
+ toolCommand({ command: "regen", tool: projectRegenerate, mode: "local", server: true, examples: [
39
+ ["$0 project regen", "sync, regenerate from the .ioc and pull; stops with a diff if code outside USER CODE sections would be removed"],
40
+ ["$0 project regen --force", "regenerate anyway, still showing the diff of the removed code"],
41
+ ] }),
42
+ toolCommand({ command: "template", tool: projectDownloadTemplate, mode: "local", server: true, examples: [
43
+ ["$0 project template --mcu STM32G474RETx", "download a barebone project into ./STM32G474RETx/"],
44
+ ] }),
45
+ toolCommand({ command: "drivers", tool: downloadDrivers, mode: "local", server: true, examples: [
46
+ ["$0 project drivers", "restore missing Drivers/ files for the .ioc's MCU"],
47
+ ] }),
48
+ ];
49
+ const command = {
50
+ command: "project",
51
+ describe: "Sync, build and edit the local STM32 project with the Turing Code server",
52
+ builder: cli => subcommands.reduce((c, s) => c.command(s), cli)
53
+ .demandCommand(1, "project needs a subcommand: sync, pull, build, check, analyze, regen, template or drivers")
54
+ .epilogue([
55
+ "Typical use: edit files, `project sync` (or `project sync -b -a` to also build and analyse), `project build`,",
56
+ "then `probe program`. `project check` (syntax) and `project analyze` (cppcheck, clang-tidy, MISRA) check the synced code.",
57
+ "Other server tools (pins, clocks, .ioc edits) go through `tool call`; after one that",
58
+ "changes the server copy, `project pull` brings the changes back, and `project regen` regenerates the code.",
59
+ PROJECT_HELP,
60
+ TOKEN_HELP,
61
+ ].join("\n")),
62
+ handler: () => { },
63
+ };
64
+ export default command;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,22 @@
1
+ import { runBuild } from '../../stm32/context.js';
2
+ import { _defineTool } from '../../stm32/tool.js';
3
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
4
+ // Builds the server copy and downloads the resulting ELF into the local build/<preset>/ folder.
5
+ export default _defineTool({
6
+ name: 'project_build',
7
+ description: 'Build the project on the TuringCode server \
8
+ (run `turingcode project sync` first to include local changes), then download the .elf ' +
9
+ 'into the local build/<preset>/ folder for `turingcode probe program`. \
10
+ Prints the result, warnings in the project code, sizes and the clock audit summary (`--verbose` for the full reply).',
11
+ properties: {
12
+ preset: {
13
+ type: 'string',
14
+ enum: ['Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel'],
15
+ },
16
+ verbose: {
17
+ type: 'boolean',
18
+ description: 'Print the full build reply (compiler, Drivers/ warnings, map file, stack usage, build log, clock tables); a failed build always prints it',
19
+ },
20
+ },
21
+ run: async (args) => runBuild(await findStm32Project(), args.preset ?? 'Debug', !!args.verbose),
22
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,34 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { callServer } from '../../stm32/context.js';
4
+ import { recordBaseline } from '../../stm32/regen.js';
5
+ import { downloadAndUnzip, zipLink } from '../../stm32/utils/downloadzip_utils.js';
6
+ import { _defineTool } from '../../stm32/tool.js';
7
+ import { getMcuFromText } from '../../stm32/utils/mcu_utils.js';
8
+ // Downloads the server's barebone project for an MCU into the agent's working directory (which must be inside the project folder).
9
+ export default _defineTool({
10
+ name: 'project_download_template',
11
+ description: 'Download a barebone STM32 project template (CMake)' +
12
+ ' for an MCU into the current working directory, as a <MCU>/ folder. ' +
13
+ ' only use when user asked to create a new project.',
14
+ properties: {
15
+ mcu: {
16
+ type: 'string',
17
+ pattern: '^[A-Za-z0-9_-]{4,40}$',
18
+ description: 'CubeMX part number, e.g. STM32G474RETx',
19
+ },
20
+ },
21
+ required: ['mcu'],
22
+ run: async (args, ctx) => {
23
+ const text = await callServer('project_template', { mcu: args.mcu }, 125_000);
24
+ const mcu = getMcuFromText(text);
25
+ if (!mcu)
26
+ throw new Error(text); // e.g. unknown or ambiguous MCU, with the server's choices
27
+ const dest = join(ctx.cwd, mcu); // the zip holds one <MCU>/ folder
28
+ if (existsSync(dest))
29
+ throw new Error(`${dest} already exists; move or delete it first`);
30
+ await downloadAndUnzip(zipLink(text), ctx.cwd);
31
+ await recordBaseline(dest); // what CubeMX generated, for `project regen`'s check
32
+ return `Template for ${mcu} downloaded to ${dest}`;
33
+ },
34
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,20 @@
1
+ import { callServer } from '../../stm32/context.js';
2
+ import { regenerateProject } from '../../stm32/regen.js';
3
+ import { _defineTool } from '../../stm32/tool.js';
4
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
5
+ export default _defineTool({
6
+ name: 'project_regenerate',
7
+ description: 'Regenerate the project code from its .ioc file with CubeMX on the TuringCode server, then pull the result ' +
8
+ '(run after pin, clock or peripheral edits). CubeMX keeps only the code inside USER CODE sections, so it first ' +
9
+ 'compares each file with USER CODE markers with the code CubeMX generated last time (.turingcode/generated/) and ' +
10
+ 'stops with a diff of the code that would be removed; force regenerates anyway and still shows that diff. ' +
11
+ 'Local changes are synced to the server first. ' +
12
+ 'The project is the folder with a .ioc or .mxproject file inside the project folder (the nearest folder holding .turingcode/ or a .ioc file, else the working directory); fails if there are several.',
13
+ properties: {
14
+ force: {
15
+ type: 'boolean',
16
+ description: 'Regenerate and pull even when code outside USER CODE sections would be removed',
17
+ },
18
+ },
19
+ run: async (args) => regenerateProject(callServer, await findStm32Project(), !!args.force),
20
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,13 @@
1
+ import { callServer } from "../../stm32/context.js";
2
+ import { syncProjectFromServer } from "../../stm32/sync.js";
3
+ import { _defineTool } from "../../stm32/tool.js";
4
+ import { findStm32Project } from "../../stm32/utils/project_utils.js";
5
+ export default _defineTool({
6
+ name: "project_sync_from_server",
7
+ description: "Download the files of your stm32 project that changed on the TuringCode server (e.g. after project_regenerate " +
8
+ "or stm32_cubetools_* edits) and overwrite the local copies; local files absent on the server are kept. " +
9
+ "build/ and git-ignored files (except Drivers/) are skipped. Local edits not yet synced to the server are lost, " +
10
+ "so run `turingcode project sync` first if you have any. " +
11
+ "The project is the folder with a .ioc or .mxproject file inside the project folder (the nearest folder holding .turingcode/ or a .ioc file, else the working directory); fails if there are several.",
12
+ run: async () => syncProjectFromServer(callServer, await findStm32Project()),
13
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,51 @@
1
+ import analysisStatic from './analysis_static.js';
2
+ import { callServer, runBuild } from '../../stm32/context.js';
3
+ import { checkBeforeRegen } from '../../stm32/regen.js';
4
+ import { syncProjectToServer } from '../../stm32/sync.js';
5
+ import { _defineTool } from '../../stm32/tool.js';
6
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
7
+ export default _defineTool({
8
+ name: 'project_sync_to_server',
9
+ description: 'Sync your local stm32 project files (except build/ and git-ignored files; Drivers/ is always sent) ' +
10
+ 'to the TuringCode server; only files the server lacks or has a different version of are uploaded. \
11
+ To restore deleted Drivers/ files use `turingcode project drivers`. \
12
+ Stops before uploading when a USER CODE BEGIN/END marker is unmatched, and warns about code outside USER CODE sections that `project regen` would remove. \
13
+ The project is the folder with a .ioc or .mxproject file inside the project folder (the nearest folder holding .turingcode/ or a .ioc file, else the working directory); fails if there are several.',
14
+ properties: {
15
+ force_full_sync: {
16
+ type: 'boolean',
17
+ description: 'Upload every file, skipping the check for which files the server already has (server files deleted locally are kept)',
18
+ },
19
+ build: {
20
+ type: 'string',
21
+ enum: ['', 'Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel'],
22
+ description: 'After a successful sync, build with this preset like `turingcode project build`; empty = Debug',
23
+ },
24
+ analyse: {
25
+ type: 'string',
26
+ enum: ['', 'quick', 'full', 'misra'],
27
+ description: 'After a successful sync (and build), run static analysis with this profile like `turingcode project analyze`; empty = quick',
28
+ },
29
+ verbose: {
30
+ type: 'boolean',
31
+ description: 'Print the full build reply (compiler, Drivers/ warnings, map file, stack usage, build log, clock tables); a failed build always prints it',
32
+ },
33
+ },
34
+ run: async (args, ctx) => {
35
+ const project = await findStm32Project();
36
+ const { findings, broken } = await checkBeforeRegen(project.root);
37
+ if (broken.length)
38
+ throw new Error(`Sync stopped, nothing uploaded. Fix the USER CODE markers first:\n\n${broken.join('\n\n')}`);
39
+ const synced = await syncProjectToServer(callServer, project, !!args.force_full_sync);
40
+ // warn, not stop: user-written files with markers are reported too (see recordBaseline)
41
+ const outside = findings.length
42
+ ? `\n\nWarning: code outside USER CODE sections differs from what CubeMX generated; \`project regen\` would stop on it. Move it into a USER CODE section:\n\n${findings.join('\n\n')}`
43
+ : '';
44
+ const parts = [`# Sync\n${synced}${outside}`];
45
+ if (args.build !== undefined)
46
+ parts.push(`# Build\n${await runBuild(project, args.build || 'Debug', !!args.verbose)}`);
47
+ if (args.analyse !== undefined)
48
+ parts.push(`# Static analysis\n${await analysisStatic.run({ profile: args.analyse || 'quick' }, ctx)}`);
49
+ return parts.length > 1 ? parts.join('\n') : synced + outside;
50
+ },
51
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,19 @@
1
+ import { callServer } from '../../stm32/context.js';
2
+ import { downloadAndUnzip, zipLink } from '../../stm32/utils/downloadzip_utils.js';
3
+ import { _defineTool } from '../../stm32/tool.js';
4
+ import { findStm32Project } from '../../stm32/utils/project_utils.js';
5
+ import { getMcuFromIoc } from '../../stm32/utils/mcu_utils.js';
6
+ // Restores the project's Drivers/ files from the server's copy for the .ioc's MCU; existing files are kept.
7
+ export default _defineTool({
8
+ name: 'stm32_cubetools_download_drivers',
9
+ description: 'Restore missing Drivers/ files (CMSIS, HAL) of your local stm32 project: downloads the complete Drivers/ folder ' +
10
+ 'STM32CubeMX makes for the MCU in the .ioc and adds the files the project lacks; existing files are not overwritten. ' +
11
+ 'The project is the folder with a .ioc or .mxproject file inside the project folder (the nearest folder holding .turingcode/ or a .ioc file, else the working directory); fails if there are several.',
12
+ run: async () => {
13
+ const project = await findStm32Project();
14
+ const mcu = await getMcuFromIoc(project.root);
15
+ const text = await callServer('stm32_cubetools_download_drivers', { mcu }, 125_000);
16
+ await downloadAndUnzip(zipLink(text), project.root, false); // throws with the server reply when it has no zip link
17
+ return `Restored missing Drivers/ files for ${mcu} into ${project.root}`;
18
+ },
19
+ });
@@ -0,0 +1,2 @@
1
+ declare const command: import("yargs").CommandModule<{}, {}>;
2
+ export default command;
@@ -0,0 +1,6 @@
1
+ import { toolCommand } from "../../stm32/toolCommand.js";
2
+ import getStatus from "./get_status.js";
3
+ const command = toolCommand({ command: "status", tool: getStatus, mode: "local", examples: [
4
+ ["$0 status", "project, last build, debug session and installed debug tools"],
5
+ ] });
6
+ export default command;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;
@@ -0,0 +1,44 @@
1
+ import { DEFAULT_URL } from "../../core/mcp.js";
2
+ import { cliVersion } from "../../stm32/context.js";
3
+ import { _defineTool } from "../../stm32/tool.js";
4
+ import { findElf } from "../../stm32/utils/findElf.js";
5
+ import { discover_system_tools } from "../../stm32/utils/discover_tools.js";
6
+ import { findProject, findStm32Project } from "../../stm32/utils/project_utils.js";
7
+ import { scanUserSections } from "../../stm32/utils/user_code.js";
8
+ import { readFile } from "node:fs/promises";
9
+ import { join } from "node:path";
10
+ // Reports the CLI, server token, project, its newest built ELF, debug session and hardware tool state.
11
+ export default _defineTool({
12
+ name: "get_status",
13
+ description: "Check the server token, project and hardware tool setup: current project and STM32 project, its newest .elf in build/, debug session, USER CODE section mismatches in Core/Src/main.c and Core/Inc/main.h, and the installed debug tools (probe-rs, STM32_Programmer_CLI, STM32CubeIDE, STM32CubeMX).",
14
+ run: async (_args, ctx) => {
15
+ const p = await findProject(ctx.cwd);
16
+ const stm = await findStm32Project(ctx.cwd).catch((e) => e);
17
+ const stmText = stm instanceof Error ? `none (${stm.message})` : `${stm.name} (${stm.root})`;
18
+ const elf = stm instanceof Error ? undefined : await findElf(stm.root);
19
+ const build = elf ? `${elf.path} (${elf.mtime.toISOString()})` : "none";
20
+ const tools = await discover_system_tools(true);
21
+ const userCode = await Promise.all(["Core/Src/main.c", "Core/Inc/main.h"].map(async (f) => {
22
+ const text = stm instanceof Error ? undefined : await readFile(join(stm.root, f), "utf8").catch(() => undefined);
23
+ if (text === undefined)
24
+ return ` ${f}: not found`;
25
+ const { sections, errors } = scanUserSections(text);
26
+ if (!errors.length)
27
+ return ` ${f}: ${sections.length} sections, ok`;
28
+ return [` ${f}: ${errors.length} mismatch(es)`, ...errors.map((e) => ` ${e}`)].join("\n");
29
+ }));
30
+ return [
31
+ `turingcode ${cliVersion()}`,
32
+ `Server: ${process.env.TURINGCODE_URL || DEFAULT_URL}`,
33
+ `Token: ${process.env.TURINGCODE_TOKEN?.trim() ? "set" : "missing, set TURINGCODE_TOKEN"}`,
34
+ `Project: ${p.name} (${p.root})`,
35
+ `STM32 project: ${stmText}`,
36
+ `Last build: ${build}`,
37
+ `Debug session: ${ctx.debugActive() ? `active (probe owned by the debug daemon, ${ctx.socketPath})` : "none"}`,
38
+ "USER CODE sections:",
39
+ ...userCode,
40
+ `Debug tools: ${tools.length ? "" : "none found (install probe-rs from https://probe.rs, or STM32CubeProgrammer)"}`,
41
+ ...tools.map((t) => ` ${t.id}: ${t.path}`),
42
+ ].join("\n");
43
+ },
44
+ });
@@ -0,0 +1,38 @@
1
+ import { Diagnostic } from "../contract/diagnostics.js";
2
+ export interface TableCheckInput {
3
+ /** Contract file. */
4
+ contract: string;
5
+ /** Markdown file, folder of `<Table>.csv`, or one `<Table>.csv`. */
6
+ tables: string;
7
+ /** Dependency alias -> its tables file. */
8
+ deps?: Record<string, string>;
9
+ }
10
+ export interface TableCheckResult {
11
+ status: "PASS" | "FAIL" | "INVALID_CONTRACT" | "INVALID_INPUT" | "MISSING_DATA";
12
+ contract: string;
13
+ summary: {
14
+ tables: number;
15
+ rows: number;
16
+ fails: number;
17
+ warnings: number;
18
+ };
19
+ diagnostics: Diagnostic[];
20
+ }
21
+ /**
22
+ * Checks tables against a contract: tables and columns, every cell's type,
23
+ * every column's `match`, then the rules.
24
+ *
25
+ * @param {TableCheckInput} input - Contract, tables and dependency tables.
26
+ * @returns {TableCheckResult} Status, summary and diagnostics.
27
+ *
28
+ * @example
29
+ * tableCheck({ contract: "orders.contract.json", tables: "orders.md" }).status; // "PASS"
30
+ */
31
+ export declare function tableCheck(input: TableCheckInput): TableCheckResult;
32
+ /**
33
+ * Compact text for a table check result.
34
+ *
35
+ * @param {TableCheckResult} r - The result.
36
+ * @returns {string} Status line, then the diagnostics.
37
+ */
38
+ export declare function formatTableCheck(r: TableCheckResult): string;