@turingcode/cli 0.2.13 → 0.3.5

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 (185) 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 +17 -4
  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/exact/fraction.js +2 -0
  11. package/dist/core/files.d.ts +44 -0
  12. package/dist/core/files.js +48 -3
  13. package/dist/core/json-shape.d.ts +45 -0
  14. package/dist/core/json-shape.js +1 -0
  15. package/dist/core/mcp.d.ts +92 -0
  16. package/dist/core/mcp.js +56 -15
  17. package/dist/core/query.d.ts +69 -0
  18. package/dist/core/query.js +2 -2
  19. package/dist/core/report.d.ts +11 -0
  20. package/dist/core/tables/csv.d.ts +29 -0
  21. package/dist/core/tables/csv.js +54 -25
  22. package/dist/core/tables/markdown.d.ts +22 -0
  23. package/dist/core/tables/markdown.js +8 -2
  24. package/dist/core/tables/types.d.ts +25 -0
  25. package/dist/features/contract/check.d.ts +30 -0
  26. package/dist/features/contract/command.d.ts +12 -0
  27. package/dist/features/contract/command.js +64 -64
  28. package/dist/features/contract/describe.d.ts +22 -0
  29. package/dist/features/contract/diagnostics.d.ts +111 -0
  30. package/dist/features/contract/diagnostics.js +7 -4
  31. package/dist/features/contract/load.d.ts +58 -0
  32. package/dist/features/contract/load.js +4 -4
  33. package/dist/features/contract/model.d.ts +67 -0
  34. package/dist/features/contract/rules.d.ts +149 -0
  35. package/dist/features/contract/rules.js +31 -4
  36. package/dist/features/debug/clear_breakpoint.d.ts +3 -0
  37. package/dist/features/debug/clear_breakpoint.js +24 -0
  38. package/dist/features/debug/command.d.ts +5 -0
  39. package/dist/features/debug/command.js +66 -0
  40. package/dist/features/debug/continue.d.ts +3 -0
  41. package/dist/features/debug/continue.js +22 -0
  42. package/dist/features/debug/debug_start.d.ts +3 -0
  43. package/dist/features/debug/debug_start.js +22 -0
  44. package/dist/features/debug/debug_stop.d.ts +3 -0
  45. package/dist/features/debug/debug_stop.js +11 -0
  46. package/dist/features/debug/evaluate.d.ts +3 -0
  47. package/dist/features/debug/evaluate.js +48 -0
  48. package/dist/features/debug/get_stack.d.ts +3 -0
  49. package/dist/features/debug/get_stack.js +14 -0
  50. package/dist/features/debug/get_variables.d.ts +3 -0
  51. package/dist/features/debug/get_variables.js +26 -0
  52. package/dist/features/debug/halt.d.ts +3 -0
  53. package/dist/features/debug/halt.js +16 -0
  54. package/dist/features/debug/set_breakpoint.d.ts +3 -0
  55. package/dist/features/debug/set_breakpoint.js +30 -0
  56. package/dist/features/debug/step.d.ts +3 -0
  57. package/dist/features/debug/step.js +23 -0
  58. package/dist/features/install-mods/command.d.ts +6 -0
  59. package/dist/features/install-mods/command.js +18 -0
  60. package/dist/features/install-mods/install.d.ts +23 -0
  61. package/dist/features/install-mods/install.js +49 -0
  62. package/dist/features/install-skills/command.d.ts +7 -0
  63. package/dist/features/install-skills/command.js +22 -0
  64. package/dist/features/install-skills/install.d.ts +14 -0
  65. package/dist/features/install-skills/install.js +29 -0
  66. package/dist/features/probe/command.d.ts +5 -0
  67. package/dist/features/probe/command.js +112 -0
  68. package/dist/features/probe/kill_all_debuggers.d.ts +3 -0
  69. package/dist/features/probe/kill_all_debuggers.js +50 -0
  70. package/dist/features/probe/list_probes.d.ts +3 -0
  71. package/dist/features/probe/list_probes.js +8 -0
  72. package/dist/features/probe/program_project.d.ts +3 -0
  73. package/dist/features/probe/program_project.js +29 -0
  74. package/dist/features/probe/read_fault_status.d.ts +3 -0
  75. package/dist/features/probe/read_fault_status.js +19 -0
  76. package/dist/features/probe/read_memory.d.ts +3 -0
  77. package/dist/features/probe/read_memory.js +36 -0
  78. package/dist/features/probe/read_peripheral.d.ts +3 -0
  79. package/dist/features/probe/read_peripheral.js +60 -0
  80. package/dist/features/probe/reset_probe_usb.d.ts +3 -0
  81. package/dist/features/probe/reset_probe_usb.js +17 -0
  82. package/dist/features/probe/reset_target.d.ts +3 -0
  83. package/dist/features/probe/reset_target.js +16 -0
  84. package/dist/features/probe/target_info.d.ts +3 -0
  85. package/dist/features/probe/target_info.js +34 -0
  86. package/dist/features/project/analysis_static.d.ts +3 -0
  87. package/dist/features/project/analysis_static.js +19 -0
  88. package/dist/features/project/analysis_syntax_check.d.ts +3 -0
  89. package/dist/features/project/analysis_syntax_check.js +21 -0
  90. package/dist/features/project/command.d.ts +5 -0
  91. package/dist/features/project/command.js +64 -0
  92. package/dist/features/project/project_build.d.ts +3 -0
  93. package/dist/features/project/project_build.js +22 -0
  94. package/dist/features/project/project_download_template.d.ts +3 -0
  95. package/dist/features/project/project_download_template.js +34 -0
  96. package/dist/features/project/project_regenerate.d.ts +2 -0
  97. package/dist/features/project/project_regenerate.js +20 -0
  98. package/dist/features/project/project_sync_from_server.d.ts +2 -0
  99. package/dist/features/project/project_sync_from_server.js +13 -0
  100. package/dist/features/project/project_sync_to_server.d.ts +2 -0
  101. package/dist/features/project/project_sync_to_server.js +51 -0
  102. package/dist/features/project/stm32_cubetools_download_drivers.d.ts +3 -0
  103. package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
  104. package/dist/features/status/command.d.ts +2 -0
  105. package/dist/features/status/command.js +6 -0
  106. package/dist/features/status/get_status.d.ts +3 -0
  107. package/dist/features/status/get_status.js +44 -0
  108. package/dist/features/table/check.d.ts +38 -0
  109. package/dist/features/table/check.js +5 -5
  110. package/dist/features/table/command.d.ts +3 -0
  111. package/dist/features/table/read.d.ts +28 -0
  112. package/dist/features/table/read.js +9 -9
  113. package/dist/features/tool/command.d.ts +35 -0
  114. package/dist/features/tool/command.js +95 -66
  115. package/dist/lib.d.ts +15 -0
  116. package/dist/lib.js +11 -0
  117. package/dist/stm32/actions/DebugAction.d.ts +65 -0
  118. package/dist/stm32/actions/DebugAction.js +106 -0
  119. package/dist/stm32/actions/DebugActionInfo.d.ts +11 -0
  120. package/dist/stm32/actions/DebugActionInfo.js +11 -0
  121. package/dist/stm32/actions/DebugActionListProbes.d.ts +10 -0
  122. package/dist/stm32/actions/DebugActionListProbes.js +10 -0
  123. package/dist/stm32/actions/DebugActionProgram.d.ts +34 -0
  124. package/dist/stm32/actions/DebugActionProgram.js +81 -0
  125. package/dist/stm32/actions/DebugActionRead.d.ts +39 -0
  126. package/dist/stm32/actions/DebugActionRead.js +61 -0
  127. package/dist/stm32/actions/DebugActionReset.d.ts +12 -0
  128. package/dist/stm32/actions/DebugActionReset.js +9 -0
  129. package/dist/stm32/actions/DebugActionServer.d.ts +11 -0
  130. package/dist/stm32/actions/DebugActionServer.js +11 -0
  131. package/dist/stm32/analysis.d.ts +20 -0
  132. package/dist/stm32/analysis.js +21 -0
  133. package/dist/stm32/build.d.ts +34 -0
  134. package/dist/stm32/build.js +79 -0
  135. package/dist/stm32/context.d.ts +23 -0
  136. package/dist/stm32/context.js +42 -0
  137. package/dist/stm32/daemon.d.ts +65 -0
  138. package/dist/stm32/daemon.js +209 -0
  139. package/dist/stm32/regen.d.ts +68 -0
  140. package/dist/stm32/regen.js +231 -0
  141. package/dist/stm32/session.d.ts +137 -0
  142. package/dist/stm32/session.js +375 -0
  143. package/dist/stm32/sync.d.ts +25 -0
  144. package/dist/stm32/sync.js +74 -0
  145. package/dist/stm32/tool.d.ts +45 -0
  146. package/dist/stm32/tool.js +20 -0
  147. package/dist/stm32/toolCommand.d.ts +50 -0
  148. package/dist/stm32/toolCommand.js +100 -0
  149. package/dist/stm32/utils/discover_tools.d.ts +25 -0
  150. package/dist/stm32/utils/discover_tools.js +121 -0
  151. package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
  152. package/dist/stm32/utils/downloadzip_utils.js +180 -0
  153. package/dist/stm32/utils/faultUtils.d.ts +15 -0
  154. package/dist/stm32/utils/faultUtils.js +69 -0
  155. package/dist/stm32/utils/findElf.d.ts +9 -0
  156. package/dist/stm32/utils/findElf.js +19 -0
  157. package/dist/stm32/utils/hash_utils.d.ts +19 -0
  158. package/dist/stm32/utils/hash_utils.js +28 -0
  159. package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
  160. package/dist/stm32/utils/listSyncFiles.js +100 -0
  161. package/dist/stm32/utils/mcu_utils.d.ts +58 -0
  162. package/dist/stm32/utils/mcu_utils.js +100 -0
  163. package/dist/stm32/utils/project_utils.d.ts +54 -0
  164. package/dist/stm32/utils/project_utils.js +104 -0
  165. package/dist/stm32/utils/svdUtils.d.ts +31 -0
  166. package/dist/stm32/utils/svdUtils.js +30 -0
  167. package/dist/stm32/utils/truncate.d.ts +7 -0
  168. package/dist/stm32/utils/truncate.js +9 -0
  169. package/dist/stm32/utils/usb_utils.d.ts +5 -0
  170. package/dist/stm32/utils/usb_utils.js +23 -0
  171. package/dist/stm32/utils/user_code.d.ts +17 -0
  172. package/dist/stm32/utils/user_code.js +45 -0
  173. package/dist/stm32/utils/utils_mutex.d.ts +15 -0
  174. package/dist/stm32/utils/utils_mutex.js +113 -0
  175. package/dist/stm32/validate.d.ts +17 -0
  176. package/dist/stm32/validate.js +46 -0
  177. package/mods/claude/.claude-plugin/marketplace.json +8 -0
  178. package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
  179. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +44 -0
  180. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +47 -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 +21 -0
  184. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
  185. package/package.json +11 -3
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Base of the debug actions: each one knows the command line of every tool that can run it.
3
+ */
4
+ import { execFile } from 'node:child_process';
5
+ import { promisify } from 'node:util';
6
+ import { discover_system_tools, toolHint, } from '../utils/discover_tools.js';
7
+ import { claimProbe, releaseProbe } from '../utils/utils_mutex.js';
8
+ const run = promisify(execFile);
9
+ /**
10
+ * A debug operation (program, read memory, reset, ...) and the command line of each tool that can do it.
11
+ * The first installed tool in `commands` order runs it, with the placeholders filled from the action params.
12
+ * Everything is static: subclasses set the fields and are used without an instance (DebugActionRead.run_action(...)).
13
+ */
14
+ export class DebugAction {
15
+ static title; /** e.g. "Reading memory", for error messages */
16
+ static commands;
17
+ static timeout = 30_000;
18
+ static exclusive = true; /** the run opens the probe, so only one such run happens at a time */
19
+ /**
20
+ * Converts the action params to the placeholder values; override to add derived or formatted values.
21
+ * @param params action params
22
+ * @returns the placeholder values
23
+ */
24
+ static values(params) {
25
+ return params;
26
+ }
27
+ /**
28
+ * Picks the first installed tool that has a command line for this action.
29
+ * @returns the tool
30
+ */
31
+ static async pickSystemTool() {
32
+ const ids = Object.keys(this.commands);
33
+ const first = async (refresh) => {
34
+ const installed = await discover_system_tools(refresh);
35
+ return ids.map((id) => installed.find((t) => t.id === id)).find((t) => t);
36
+ };
37
+ /** Look again before failing, in case a tool was installed after the cached discovery */
38
+ const tool = (await first(false)) ?? (await first(true));
39
+ if (!tool)
40
+ throw new Error(`${this.title} needs ${ids.map((id) => `${id} (${toolHint(id)})`).join(' or ')}`);
41
+ return tool;
42
+ }
43
+ /**
44
+ * Builds the command line of a tool for this action.
45
+ * @param params action params
46
+ * @param tool tool to use, default the one pickSystemTool returns
47
+ * @returns the tool and its arguments
48
+ */
49
+ static async build_command(params, tool) {
50
+ tool ??= await this.pickSystemTool();
51
+ const template = this.commands[tool.id];
52
+ if (!template)
53
+ throw new Error(`${this.title} is not supported by ${tool.id}`);
54
+ const values = this.values(params);
55
+ const fill = (arg) => arg.replace(/\{(\w+)\}/g, (_all, name) => {
56
+ if (!(name in values))
57
+ throw new Error(`${this.title}: no value for {${name}} in the ${tool.id} command`);
58
+ return String(values[name]);
59
+ });
60
+ const args = template.flatMap((a) => typeof a === 'string' ? [fill(a)] : values[a.if] === true ? a.args.map(fill) : []);
61
+ return { tool, args };
62
+ }
63
+ /**
64
+ * Runs the action's command line (no shell) and returns its full output.
65
+ * @param params action params
66
+ * @param tool tool to use, default the one pickSystemTool returns
67
+ * @returns the tool that ran and its stdout and stderr
68
+ */
69
+ static async run_action(params, tool) {
70
+ const cmd = await this.build_command(params, tool);
71
+ if (this.exclusive)
72
+ claimProbe(this.title);
73
+ try {
74
+ const { stdout, stderr } = await run(cmd.tool.path, cmd.args, {
75
+ timeout: this.timeout,
76
+ windowsHide: true,
77
+ maxBuffer: 16 * 1024 * 1024,
78
+ });
79
+ return { tool: cmd.tool.id, stdout, stderr };
80
+ }
81
+ catch (e) {
82
+ throw new Error(`${this.title} failed (${cmd.tool.id}): ${(`${e.stdout ?? ''}${e.stderr ?? ''}` || e.message).slice(-1500)}`);
83
+ }
84
+ finally {
85
+ if (this.exclusive)
86
+ releaseProbe();
87
+ }
88
+ }
89
+ /**
90
+ * Runs the action and returns the tail of its output, for tool replies.
91
+ * @param params action params
92
+ * @param tool tool to use, default the one pickSystemTool returns
93
+ * @returns the last 1500 characters of stdout and stderr
94
+ */
95
+ static async run_action_get_tail(params, tool) {
96
+ return this.tail(await this.run_action(params, tool));
97
+ }
98
+ /**
99
+ * Returns the tail of a run's output, for tool replies.
100
+ * @param out output of exec
101
+ * @returns the last 1500 characters of stdout and stderr
102
+ */
103
+ static tail(out) {
104
+ return `${out.stdout}${out.stderr}`.slice(-1500);
105
+ }
106
+ }
@@ -0,0 +1,11 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /**
3
+ * Connects to the target without resetting it and reports what STM32_Programmer_CLI identifies
4
+ * (device ID, name, flash size, core). probe-rs cannot name a chip it is not told, so it has no command here.
5
+ */
6
+ export declare class DebugActionInfo extends DebugAction {
7
+ static readonly title = "Identifying the target";
8
+ static readonly commands: {
9
+ STM32_Programmer_CLI: string[];
10
+ };
11
+ }
@@ -0,0 +1,11 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /**
3
+ * Connects to the target without resetting it and reports what STM32_Programmer_CLI identifies
4
+ * (device ID, name, flash size, core). probe-rs cannot name a chip it is not told, so it has no command here.
5
+ */
6
+ export class DebugActionInfo extends DebugAction {
7
+ static title = 'Identifying the target';
8
+ static commands = {
9
+ STM32_Programmer_CLI: ['-c', 'port=SWD', 'mode=HOTPLUG'],
10
+ };
11
+ }
@@ -0,0 +1,10 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /** Lists the connected debug probes. Does not open a probe, so it skips the busy lock. */
3
+ export declare class DebugActionListProbes extends DebugAction {
4
+ static readonly title = "Listing probes";
5
+ static readonly exclusive = false;
6
+ static readonly commands: {
7
+ 'probe-rs': string[];
8
+ STM32_Programmer_CLI: string[];
9
+ };
10
+ }
@@ -0,0 +1,10 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /** Lists the connected debug probes. Does not open a probe, so it skips the busy lock. */
3
+ export class DebugActionListProbes extends DebugAction {
4
+ static title = 'Listing probes';
5
+ static exclusive = false;
6
+ static commands = {
7
+ 'probe-rs': ['list'],
8
+ STM32_Programmer_CLI: ['--list'],
9
+ };
10
+ }
@@ -0,0 +1,34 @@
1
+ import { DebugAction, ToolOutput } from './DebugAction.js';
2
+ import { DiscoveredSystemTool } from '../utils/discover_tools.js';
3
+ export type ProgramParams = {
4
+ chip: string; /** probe-rs target name (e.g. STM32G474RETx) */
5
+ elf: string; /** path of the ELF to flash */
6
+ };
7
+ /**
8
+ * Writes an ELF over SWD, verifies it and resets the target.
9
+ * ponytail: uses the only connected probe
10
+ * (probe-rs --non-interactive fails if there are several);
11
+ * pin the probe once the sidebar has a probe picker
12
+ */
13
+ export declare class DebugActionProgram extends DebugAction {
14
+ static readonly title = "Programming";
15
+ static readonly timeout = 180000;
16
+ static readonly commands: {
17
+ 'probe-rs': (string | {
18
+ if: string;
19
+ args: string[];
20
+ })[];
21
+ STM32_Programmer_CLI: (string | {
22
+ if: string;
23
+ args: string[];
24
+ })[];
25
+ };
26
+ /**
27
+ * Programs the ELF. If probe-rs fails on a dual-bank G47x/G48x part it has no algorithm for, retries once
28
+ * with the whole chip erased first, which also clears data kept in flash.
29
+ * @param p chip and ELF
30
+ * @param tool tool to use, default the one pickSystemTool returns
31
+ * @returns the tool output, with a note at the end when the whole chip was erased
32
+ */
33
+ static run_action(p: ProgramParams, tool?: DiscoveredSystemTool): Promise<ToolOutput>;
34
+ }
@@ -0,0 +1,81 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ import { DebugActionRead } from './DebugActionRead.js';
3
+ import { hasSingleBankFlashAlgo } from '../utils/mcu_utils.js';
4
+ /** STM32G4 FLASH_OPTR and its DBANK bit (dual-bank mode, 2 KB pages, bank 2 at 0x08040000). */
5
+ const G4_FLASH_OPTR = 0x40022020;
6
+ const G4_OPTR_DBANK = 1 << 22;
7
+ /**
8
+ * Tells whether the target runs in a flash mode that probe-rs's per-sector erase gets wrong:
9
+ * a 128/256 KB STM32G47x/G48x with DBANK set, where probe-rs assumes the single-bank page layout.
10
+ * @param chip probe-rs target name (e.g. STM32G474CCTx)
11
+ * @returns true when the whole chip must be erased before programming
12
+ */
13
+ async function needsChipErase(chip) {
14
+ if (!hasSingleBankFlashAlgo(chip))
15
+ return false;
16
+ const { words: [optr], } = await DebugActionRead.run_action({
17
+ chip,
18
+ address: G4_FLASH_OPTR,
19
+ width: 32,
20
+ count: 1,
21
+ });
22
+ return (optr & G4_OPTR_DBANK) !== 0;
23
+ }
24
+ /**
25
+ * Writes an ELF over SWD, verifies it and resets the target.
26
+ * ponytail: uses the only connected probe
27
+ * (probe-rs --non-interactive fails if there are several);
28
+ * pin the probe once the sidebar has a probe picker
29
+ */
30
+ export class DebugActionProgram extends DebugAction {
31
+ static title = 'Programming';
32
+ static timeout = 180_000;
33
+ static commands = {
34
+ 'probe-rs': [
35
+ 'download',
36
+ '--chip',
37
+ '{chip}',
38
+ '--protocol',
39
+ 'swd',
40
+ '--non-interactive',
41
+ '--disable-progressbars',
42
+ { if: 'chipErase', args: ['--chip-erase'] },
43
+ '--verify',
44
+ '--reset',
45
+ '{elf}',
46
+ ],
47
+ STM32_Programmer_CLI: [
48
+ '-c',
49
+ 'port=SWD',
50
+ { if: 'chipErase', args: ['-e', 'all'] },
51
+ '-w',
52
+ '{elf}',
53
+ '-v',
54
+ '-rst',
55
+ ],
56
+ };
57
+ /**
58
+ * Programs the ELF. If probe-rs fails on a dual-bank G47x/G48x part it has no algorithm for, retries once
59
+ * with the whole chip erased first, which also clears data kept in flash.
60
+ * @param p chip and ELF
61
+ * @param tool tool to use, default the one pickSystemTool returns
62
+ * @returns the tool output, with a note at the end when the whole chip was erased
63
+ */
64
+ static async run_action(p, tool) {
65
+ tool ??= await this.pickSystemTool();
66
+ try {
67
+ return await super.run_action({ ...p, chipErase: false }, tool);
68
+ }
69
+ catch (e) {
70
+ /** A failed OPTR read (e.g. probe unplugged) means no retry, and the programming error is the one to report */
71
+ if (tool.id !== 'probe-rs' || !(await needsChipErase(p.chip).catch(() => false)))
72
+ throw e;
73
+ const out = await super.run_action({ ...p, chipErase: true }, tool);
74
+ /** appended to stderr so tail() keeps it */
75
+ return {
76
+ ...out,
77
+ stderr: `${out.stderr}\nDual-bank flash (DBANK=1): first attempt failed, erased the whole chip and programmed again. First error:\n${e.message.slice(-500)}`,
78
+ };
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,39 @@
1
+ import { DebugAction, ToolOutput } from './DebugAction.js';
2
+ import { DiscoveredSystemTool } from '../utils/discover_tools.js';
3
+ export type ReadParams = {
4
+ chip: string; /** probe-rs target name (e.g. STM32G474RETx) */
5
+ address: number; /** start address, aligned to the word width */
6
+ width: 8 | 16 | 32; /** word width in bits */
7
+ count: number; /** number of words to read */
8
+ };
9
+ /** Reads target memory without halting the core. Never writes to the target. */
10
+ export declare class DebugActionRead extends DebugAction {
11
+ static readonly title = "Reading memory";
12
+ static readonly commands: {
13
+ 'probe-rs': string[];
14
+ STM32_Programmer_CLI: string[];
15
+ };
16
+ protected static values(p: ReadParams): {
17
+ chip: string; /** probe-rs target name (e.g. STM32G474RETx) */
18
+ width: 8 | 16 | 32; /** word width in bits */
19
+ count: number; /** number of words to read */
20
+ address: string;
21
+ bytes: number;
22
+ };
23
+ /**
24
+ * Reads target memory and parses the words from the tool output.
25
+ * @param p chip, address, width and word count
26
+ * @param tool tool to use, default the one pickSystemTool returns
27
+ * @returns the tool output and the words read, in address order
28
+ */
29
+ static run_action(p: ReadParams, tool?: DiscoveredSystemTool): Promise<ToolOutput & {
30
+ words: number[];
31
+ }>;
32
+ /**
33
+ * Reads target memory and returns the whole tool output, not trimmed.
34
+ * @param p chip, address, width and word count
35
+ * @param tool tool to use, default the one pickSystemTool returns
36
+ * @returns stdout and stderr
37
+ */
38
+ static run_action_get_tail(p: ReadParams, tool?: DiscoveredSystemTool): Promise<string>;
39
+ }
@@ -0,0 +1,61 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /** Reads target memory without halting the core. Never writes to the target. */
3
+ export class DebugActionRead extends DebugAction {
4
+ static title = 'Reading memory';
5
+ static commands = {
6
+ 'probe-rs': [
7
+ 'read',
8
+ '--chip',
9
+ '{chip}',
10
+ '--protocol',
11
+ 'swd',
12
+ '--non-interactive',
13
+ 'b{width}',
14
+ '0x{address}',
15
+ '{count}',
16
+ ],
17
+ STM32_Programmer_CLI: [
18
+ '-c',
19
+ 'port=SWD',
20
+ 'mode=HOTPLUG',
21
+ '-r{width}',
22
+ '0x{address}',
23
+ '{bytes}',
24
+ ],
25
+ };
26
+ static values(p) {
27
+ return { ...p, address: p.address.toString(16), bytes: (p.count * p.width) / 8 };
28
+ }
29
+ /**
30
+ * Reads target memory and parses the words from the tool output.
31
+ * @param p chip, address, width and word count
32
+ * @param tool tool to use, default the one pickSystemTool returns
33
+ * @returns the tool output and the words read, in address order
34
+ */
35
+ static async run_action(p, tool) {
36
+ const out = await super.run_action(p, tool);
37
+ /**
38
+ * probe-rs prints the words, newer versions after an "40022020: " address prefix;
39
+ * STM32_Programmer_CLI prints "0x40022020 : FFEFF8AA ..." lines after its connection info
40
+ */
41
+ const data = out.tool === 'probe-rs'
42
+ ? out.stdout.replace(/^\s*(0x)?[0-9a-fA-F]+\s*:/gm, '')
43
+ : [...out.stdout.matchAll(/^0x[0-9A-Fa-f]+\s*:(.*)$/gm)]
44
+ .map((m) => m[1])
45
+ .join(' ');
46
+ const words = data.match(new RegExp(`\\b[0-9a-fA-F]{${p.width / 4}}\\b`, 'g')) ?? [];
47
+ if (words.length !== p.count)
48
+ throw new Error(`Reading memory failed: expected ${p.count} words, got: ${out.stdout}`);
49
+ return { ...out, words: words.map((w) => parseInt(w, 16)) };
50
+ }
51
+ /**
52
+ * Reads target memory and returns the whole tool output, not trimmed.
53
+ * @param p chip, address, width and word count
54
+ * @param tool tool to use, default the one pickSystemTool returns
55
+ * @returns stdout and stderr
56
+ */
57
+ static async run_action_get_tail(p, tool) {
58
+ const out = await this.run_action(p, tool);
59
+ return `${out.stdout}${out.stderr}`;
60
+ }
61
+ }
@@ -0,0 +1,12 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ export type ResetParams = {
3
+ chip: string; /** probe-rs target name (e.g. STM32G474RETx) */
4
+ };
5
+ /** Resets the target through the only connected probe. */
6
+ export declare class DebugActionReset extends DebugAction {
7
+ static readonly title = "Reset";
8
+ static readonly commands: {
9
+ 'probe-rs': string[];
10
+ STM32_Programmer_CLI: string[];
11
+ };
12
+ }
@@ -0,0 +1,9 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /** Resets the target through the only connected probe. */
3
+ export class DebugActionReset extends DebugAction {
4
+ static title = 'Reset';
5
+ static commands = {
6
+ 'probe-rs': ['reset', '--chip', '{chip}', '--protocol', 'swd', '--non-interactive'],
7
+ STM32_Programmer_CLI: ['-c', 'port=SWD', 'mode=HOTPLUG', '-rst'],
8
+ };
9
+ }
@@ -0,0 +1,11 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /**
3
+ * Debug adapter that speaks DAP over stdio; debug_utils spawns it and sends the session requests (breakpoints, step, ...).
4
+ * Only probe-rs has one; the launch config in debug_utils is probe-rs specific.
5
+ */
6
+ export declare class DebugActionServer extends DebugAction {
7
+ static readonly title = "Debug server";
8
+ static readonly commands: {
9
+ 'probe-rs': string[];
10
+ };
11
+ }
@@ -0,0 +1,11 @@
1
+ import { DebugAction } from './DebugAction.js';
2
+ /**
3
+ * Debug adapter that speaks DAP over stdio; debug_utils spawns it and sends the session requests (breakpoints, step, ...).
4
+ * Only probe-rs has one; the launch config in debug_utils is probe-rs specific.
5
+ */
6
+ export class DebugActionServer extends DebugAction {
7
+ static title = 'Debug server';
8
+ static commands = {
9
+ 'probe-rs': ['dap-server'],
10
+ };
11
+ }
@@ -0,0 +1,20 @@
1
+ /** Server syntax check and static analysis of the STM32 project. Used by the project commands and the VS Code extension. */
2
+ import { ServerCall } from './sync.js';
3
+ import { Project } from './tool.js';
4
+ /**
5
+ * Syntax-checks files of the project's server copy.
6
+ * @param call server tool caller
7
+ * @param project project root and name
8
+ * @param files project-relative files, globs or folders (default: Core and App)
9
+ * @returns `file:line:col: severity: message` lines
10
+ */
11
+ export declare function syntaxCheckProject(call: ServerCall, project: Project, files?: string[]): Promise<string>;
12
+ /**
13
+ * Runs static analysis on the project's server copy (its own sources, never Drivers/).
14
+ * @param call server tool caller
15
+ * @param project project root and name
16
+ * @param profile quick, full or misra
17
+ * @param paths comma-separated files, globs or folders to limit the check to; empty for the whole project
18
+ * @returns `file:line:col: severity: message [check id]` lines
19
+ */
20
+ export declare function analyzeProject(call: ServerCall, project: Project, profile?: string, paths?: string): Promise<string>;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Syntax-checks files of the project's server copy.
3
+ * @param call server tool caller
4
+ * @param project project root and name
5
+ * @param files project-relative files, globs or folders (default: Core and App)
6
+ * @returns `file:line:col: severity: message` lines
7
+ */
8
+ export function syntaxCheckProject(call, project, files = ['Core', 'App']) {
9
+ return call('analysis_syntax_check', { files: files.map((f) => `projects/${project.name}/${f}`) }, 125_000);
10
+ }
11
+ /**
12
+ * Runs static analysis on the project's server copy (its own sources, never Drivers/).
13
+ * @param call server tool caller
14
+ * @param project project root and name
15
+ * @param profile quick, full or misra
16
+ * @param paths comma-separated files, globs or folders to limit the check to; empty for the whole project
17
+ * @returns `file:line:col: severity: message [check id]` lines
18
+ */
19
+ export function analyzeProject(call, project, profile = 'quick', paths = '') {
20
+ return call('analysis_static', { project_name: project.name, profile, paths }, 125_000);
21
+ }
@@ -0,0 +1,34 @@
1
+ import { ServerCall } from './sync.js';
2
+ import { Project } from './tool.js';
3
+ export declare const BUILD_PRESETS: string[];
4
+ export interface BuildResult {
5
+ text: string; /** the server's build reply */
6
+ ok: boolean; /** the build succeeded */
7
+ elf?: string; /** local path of the downloaded ELF */
8
+ note: string; /** where the ELF went, or why it was not downloaded; empty for a failed build */
9
+ }
10
+ /**
11
+ * Downloads the .elf next to the build reply's map file into the same path in the local project.
12
+ * @param call server tool caller
13
+ * @param project project root and name
14
+ * @param text server build reply
15
+ * @returns the local ELF path
16
+ * @throws when the reply names no map file inside the project, or the download fails
17
+ */
18
+ export declare function downloadElf(call: ServerCall, project: Project, text: string): Promise<string>;
19
+ /**
20
+ * Shortens a successful build reply for agents: the result line, warnings outside Drivers/ (ST's code, counted
21
+ * instead), sizes, the clock audit summary and findings, and the credits line. Compiler, map file, stack usage,
22
+ * build log and clock tables are left out.
23
+ * @param text server build reply
24
+ * @returns the short reply
25
+ */
26
+ export declare function compactBuildReply(text: string): string;
27
+ /**
28
+ * Builds the server copy of the project and, when the build succeeds, downloads its ELF into build/<preset>/.
29
+ * @param call server tool caller
30
+ * @param project project root and name
31
+ * @param preset CMake preset, one of BUILD_PRESETS
32
+ * @returns the build reply, its outcome and the ELF download note
33
+ */
34
+ export declare function buildProject(call: ServerCall, project: Project, preset: string): Promise<BuildResult>;
@@ -0,0 +1,79 @@
1
+ /** Server build of the STM32 project and download of its ELF. Used by the project commands and the VS Code extension. */
2
+ import { join } from 'node:path';
3
+ import { replyBody } from '../core/mcp.js';
4
+ import { downloadFile } from './utils/downloadzip_utils.js';
5
+ export const BUILD_PRESETS = ['Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel'];
6
+ /**
7
+ * Downloads the .elf next to the build reply's map file into the same path in the local project.
8
+ * @param call server tool caller
9
+ * @param project project root and name
10
+ * @param text server build reply
11
+ * @returns the local ELF path
12
+ * @throws when the reply names no map file inside the project, or the download fails
13
+ */
14
+ export async function downloadElf(call, project, text) {
15
+ const base = `projects/${project.name}/`;
16
+ const map = text.match(/^## Map file\n(\S+\.map)$/m)?.[1];
17
+ if (!map?.startsWith(base) || map.includes('..'))
18
+ throw new Error('no map file in build reply');
19
+ const elf = map.replace(/\.map$/, '.elf');
20
+ const url = replyBody(await call('download_file', { path: elf }));
21
+ if (!/^https?:\/\/\S+$/.test(url))
22
+ throw new Error(url);
23
+ const dest = join(project.root, ...elf.slice(base.length).split('/'));
24
+ await downloadFile(url, dest);
25
+ return dest;
26
+ }
27
+ /**
28
+ * Shortens a successful build reply for agents: the result line, warnings outside Drivers/ (ST's code, counted
29
+ * instead), sizes, the clock audit summary and findings, and the credits line. Compiler, map file, stack usage,
30
+ * build log and clock tables are left out.
31
+ * @param text server build reply
32
+ * @returns the short reply
33
+ */
34
+ export function compactBuildReply(text) {
35
+ const out = [];
36
+ let driverWarnings = 0;
37
+ for (const section of text.trim().split(/\r?\n(?=#{1,2} )/)) {
38
+ const [head, ...body] = section.split(/\r?\n/);
39
+ const lines = body.filter((l) => l.trim() && !l.startsWith('Credits:'));
40
+ if (!head.startsWith('#'))
41
+ out.push(head);
42
+ else if (head === '## Warnings') {
43
+ const own = lines.filter((l) => !l.includes('/Drivers/'));
44
+ driverWarnings += lines.length - own.length;
45
+ if (own.length)
46
+ out.push([head, ...own].join('\n'));
47
+ }
48
+ else if (head === '## Sizes')
49
+ out.push([head, ...lines].join('\n'));
50
+ else if (head.startsWith('# Clock audit'))
51
+ out.push([head, ...lines.slice(0, 1)].join('\n'));
52
+ else if (head === '## Findings' && !/^No errors or warnings\.?$/.test(lines.join(' ').trim()))
53
+ out.push([head, ...lines].join('\n'));
54
+ }
55
+ if (driverWarnings)
56
+ out.splice(1, 0, `${driverWarnings} warning(s) in Drivers/ (ST code) not shown; --verbose shows the full reply`);
57
+ const credits = text.match(/^Credits:.*$/m)?.[0];
58
+ return [...out, ...(credits ? [credits] : [])].join('\n');
59
+ }
60
+ /**
61
+ * Builds the server copy of the project and, when the build succeeds, downloads its ELF into build/<preset>/.
62
+ * @param call server tool caller
63
+ * @param project project root and name
64
+ * @param preset CMake preset, one of BUILD_PRESETS
65
+ * @returns the build reply, its outcome and the ELF download note
66
+ */
67
+ export async function buildProject(call, project, preset) {
68
+ const text = await call('project_build', { project_name: project.name, preset }, 125_000);
69
+ const ok = text.startsWith('Built ');
70
+ if (!ok)
71
+ return { text, ok, note: '' };
72
+ try {
73
+ const elf = await downloadElf(call, project, text);
74
+ return { text, ok, elf, note: `ELF downloaded to ${elf}` };
75
+ }
76
+ catch (e) {
77
+ return { text, ok, note: `ELF not downloaded: ${e.message}` };
78
+ }
79
+ }
@@ -0,0 +1,23 @@
1
+ import { Project } from './tool.js';
2
+ /**
3
+ * Reads the CLI version from its package.json.
4
+ * @returns the version, or "unknown" when package.json is not next to the build (e.g. in tests)
5
+ */
6
+ export declare function cliVersion(): string;
7
+ /**
8
+ * Calls a Turing Code server tool with the token from TURINGCODE_TOKEN.
9
+ * @param name tool name
10
+ * @param args tool arguments
11
+ * @param timeoutMs time before the request fails
12
+ * @returns the tool's text
13
+ * @throws when the token is missing, the request fails or the tool reports an error
14
+ */
15
+ export declare function callServer(name: string, args: object, timeoutMs?: number): Promise<string>;
16
+ /**
17
+ * Builds the project on the server and downloads its ELF.
18
+ * @param project project root and name
19
+ * @param preset CMake preset
20
+ * @param verbose keep the full reply of a successful build, not the short one
21
+ * @returns the build reply, followed by where the ELF went or why it was not downloaded
22
+ */
23
+ export declare function runBuild(project: Project, preset: string, verbose?: boolean): Promise<string>;
@@ -0,0 +1,42 @@
1
+ /** Server calls, builds and the CLI version for the STM32 tools, run in the CLI and in the debug daemon. */
2
+ import { readFileSync } from 'node:fs';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { callToolText, getGatewayConfig } from '../core/mcp.js';
5
+ import { buildProject, compactBuildReply } from './build.js';
6
+ /**
7
+ * Reads the CLI version from its package.json.
8
+ * @returns the version, or "unknown" when package.json is not next to the build (e.g. in tests)
9
+ */
10
+ export function cliVersion() {
11
+ try {
12
+ return JSON.parse(readFileSync(fileURLToPath(new URL('../../package.json', import.meta.url)), 'utf8')).version;
13
+ }
14
+ catch {
15
+ return 'unknown';
16
+ }
17
+ }
18
+ /**
19
+ * Calls a Turing Code server tool with the token from TURINGCODE_TOKEN.
20
+ * @param name tool name
21
+ * @param args tool arguments
22
+ * @param timeoutMs time before the request fails
23
+ * @returns the tool's text
24
+ * @throws when the token is missing, the request fails or the tool reports an error
25
+ */
26
+ export function callServer(name, args, timeoutMs) {
27
+ const { token, url } = getGatewayConfig();
28
+ return callToolText(name, args, token, url, timeoutMs);
29
+ }
30
+ /**
31
+ * Builds the project on the server and downloads its ELF.
32
+ * @param project project root and name
33
+ * @param preset CMake preset
34
+ * @param verbose keep the full reply of a successful build, not the short one
35
+ * @returns the build reply, followed by where the ELF went or why it was not downloaded
36
+ */
37
+ export async function runBuild(project, preset, verbose = false) {
38
+ const r = await buildProject(callServer, project, preset);
39
+ /** a failed build keeps the full reply, so no error is hidden */
40
+ const text = r.ok && !verbose ? compactBuildReply(r.text) : r.text;
41
+ return r.note ? `${text}\n\n${r.note}` : text;
42
+ }