@turingcode/cli 0.2.13 → 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.
- package/README.md +2 -0
- package/dist/cli/create-cli.d.ts +10 -0
- package/dist/cli/create-cli.js +12 -1
- package/dist/cli/index.d.ts +9 -0
- package/dist/cli/index.js +5 -1
- package/dist/core/bound-error.d.ts +10 -0
- package/dist/core/date.d.ts +12 -0
- package/dist/core/exact/expression.d.ts +49 -0
- package/dist/core/exact/fraction.d.ts +85 -0
- package/dist/core/files.d.ts +44 -0
- package/dist/core/files.js +46 -1
- package/dist/core/json-shape.d.ts +45 -0
- package/dist/core/mcp.d.ts +92 -0
- package/dist/core/mcp.js +56 -15
- package/dist/core/query.d.ts +69 -0
- package/dist/core/report.d.ts +11 -0
- package/dist/core/tables/csv.d.ts +18 -0
- package/dist/core/tables/markdown.d.ts +22 -0
- package/dist/core/tables/types.d.ts +25 -0
- package/dist/features/contract/check.d.ts +30 -0
- package/dist/features/contract/command.d.ts +12 -0
- package/dist/features/contract/command.js +64 -64
- package/dist/features/contract/describe.d.ts +22 -0
- package/dist/features/contract/diagnostics.d.ts +108 -0
- package/dist/features/contract/load.d.ts +58 -0
- package/dist/features/contract/model.d.ts +67 -0
- package/dist/features/contract/rules.d.ts +137 -0
- package/dist/features/debug/clear_breakpoint.d.ts +2 -0
- package/dist/features/debug/clear_breakpoint.js +24 -0
- package/dist/features/debug/command.d.ts +5 -0
- package/dist/features/debug/command.js +66 -0
- package/dist/features/debug/continue.d.ts +2 -0
- package/dist/features/debug/continue.js +22 -0
- package/dist/features/debug/debug_start.d.ts +2 -0
- package/dist/features/debug/debug_start.js +22 -0
- package/dist/features/debug/debug_stop.d.ts +2 -0
- package/dist/features/debug/debug_stop.js +11 -0
- package/dist/features/debug/evaluate.d.ts +2 -0
- package/dist/features/debug/evaluate.js +48 -0
- package/dist/features/debug/get_stack.d.ts +2 -0
- package/dist/features/debug/get_stack.js +14 -0
- package/dist/features/debug/get_variables.d.ts +2 -0
- package/dist/features/debug/get_variables.js +26 -0
- package/dist/features/debug/halt.d.ts +2 -0
- package/dist/features/debug/halt.js +16 -0
- package/dist/features/debug/set_breakpoint.d.ts +2 -0
- package/dist/features/debug/set_breakpoint.js +30 -0
- package/dist/features/debug/step.d.ts +2 -0
- package/dist/features/debug/step.js +23 -0
- package/dist/features/install-mods/command.d.ts +6 -0
- package/dist/features/install-mods/command.js +18 -0
- package/dist/features/install-mods/install.d.ts +23 -0
- package/dist/features/install-mods/install.js +48 -0
- package/dist/features/install-skills/command.d.ts +7 -0
- package/dist/features/install-skills/command.js +22 -0
- package/dist/features/install-skills/install.d.ts +13 -0
- package/dist/features/install-skills/install.js +29 -0
- package/dist/features/probe/command.d.ts +5 -0
- package/dist/features/probe/command.js +44 -0
- package/dist/features/probe/kill_all_debuggers.d.ts +2 -0
- package/dist/features/probe/kill_all_debuggers.js +50 -0
- package/dist/features/probe/list_probes.d.ts +2 -0
- package/dist/features/probe/list_probes.js +8 -0
- package/dist/features/probe/program_project.d.ts +2 -0
- package/dist/features/probe/program_project.js +29 -0
- package/dist/features/probe/read_fault_status.d.ts +2 -0
- package/dist/features/probe/read_fault_status.js +19 -0
- package/dist/features/probe/read_memory.d.ts +2 -0
- package/dist/features/probe/read_memory.js +36 -0
- package/dist/features/probe/read_peripheral.d.ts +2 -0
- package/dist/features/probe/read_peripheral.js +58 -0
- package/dist/features/probe/reset_probe_usb.d.ts +2 -0
- package/dist/features/probe/reset_probe_usb.js +17 -0
- package/dist/features/probe/reset_target.d.ts +2 -0
- package/dist/features/probe/reset_target.js +16 -0
- package/dist/features/probe/target_info.d.ts +2 -0
- package/dist/features/probe/target_info.js +34 -0
- package/dist/features/project/analysis_static.d.ts +2 -0
- package/dist/features/project/analysis_static.js +19 -0
- package/dist/features/project/analysis_syntax_check.d.ts +2 -0
- package/dist/features/project/analysis_syntax_check.js +21 -0
- package/dist/features/project/command.d.ts +5 -0
- package/dist/features/project/command.js +64 -0
- package/dist/features/project/project_build.d.ts +2 -0
- package/dist/features/project/project_build.js +22 -0
- package/dist/features/project/project_download_template.d.ts +2 -0
- package/dist/features/project/project_download_template.js +34 -0
- package/dist/features/project/project_regenerate.d.ts +2 -0
- package/dist/features/project/project_regenerate.js +20 -0
- package/dist/features/project/project_sync_from_server.d.ts +2 -0
- package/dist/features/project/project_sync_from_server.js +13 -0
- package/dist/features/project/project_sync_to_server.d.ts +2 -0
- package/dist/features/project/project_sync_to_server.js +51 -0
- package/dist/features/project/stm32_cubetools_download_drivers.d.ts +2 -0
- package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
- package/dist/features/status/command.d.ts +2 -0
- package/dist/features/status/command.js +6 -0
- package/dist/features/status/get_status.d.ts +2 -0
- package/dist/features/status/get_status.js +44 -0
- package/dist/features/table/check.d.ts +38 -0
- package/dist/features/table/command.d.ts +3 -0
- package/dist/features/table/read.d.ts +28 -0
- package/dist/features/tool/command.d.ts +35 -0
- package/dist/lib.d.ts +11 -0
- package/dist/lib.js +9 -0
- package/dist/stm32/actions/DebugAction.d.ts +63 -0
- package/dist/stm32/actions/DebugAction.js +105 -0
- package/dist/stm32/actions/DebugActionInfo.d.ts +7 -0
- package/dist/stm32/actions/DebugActionInfo.js +9 -0
- package/dist/stm32/actions/DebugActionListProbes.d.ts +9 -0
- package/dist/stm32/actions/DebugActionListProbes.js +10 -0
- package/dist/stm32/actions/DebugActionProgram.d.ts +28 -0
- package/dist/stm32/actions/DebugActionProgram.js +79 -0
- package/dist/stm32/actions/DebugActionRead.d.ts +38 -0
- package/dist/stm32/actions/DebugActionRead.js +59 -0
- package/dist/stm32/actions/DebugActionReset.d.ts +11 -0
- package/dist/stm32/actions/DebugActionReset.js +9 -0
- package/dist/stm32/actions/DebugActionServer.d.ts +7 -0
- package/dist/stm32/actions/DebugActionServer.js +9 -0
- package/dist/stm32/analysis.d.ts +19 -0
- package/dist/stm32/analysis.js +21 -0
- package/dist/stm32/build.d.ts +34 -0
- package/dist/stm32/build.js +79 -0
- package/dist/stm32/context.d.ts +23 -0
- package/dist/stm32/context.js +42 -0
- package/dist/stm32/daemon.d.ts +63 -0
- package/dist/stm32/daemon.js +202 -0
- package/dist/stm32/regen.d.ts +68 -0
- package/dist/stm32/regen.js +225 -0
- package/dist/stm32/session.d.ts +134 -0
- package/dist/stm32/session.js +374 -0
- package/dist/stm32/sync.d.ts +24 -0
- package/dist/stm32/sync.js +72 -0
- package/dist/stm32/tool.d.ts +43 -0
- package/dist/stm32/tool.js +20 -0
- package/dist/stm32/toolCommand.d.ts +50 -0
- package/dist/stm32/toolCommand.js +102 -0
- package/dist/stm32/utils/discover_tools.d.ts +24 -0
- package/dist/stm32/utils/discover_tools.js +121 -0
- package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
- package/dist/stm32/utils/downloadzip_utils.js +180 -0
- package/dist/stm32/utils/faultUtils.d.ts +14 -0
- package/dist/stm32/utils/faultUtils.js +69 -0
- package/dist/stm32/utils/findElf.d.ts +9 -0
- package/dist/stm32/utils/findElf.js +19 -0
- package/dist/stm32/utils/hash_utils.d.ts +19 -0
- package/dist/stm32/utils/hash_utils.js +28 -0
- package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
- package/dist/stm32/utils/listSyncFiles.js +98 -0
- package/dist/stm32/utils/mcu_utils.d.ts +59 -0
- package/dist/stm32/utils/mcu_utils.js +99 -0
- package/dist/stm32/utils/project_utils.d.ts +54 -0
- package/dist/stm32/utils/project_utils.js +102 -0
- package/dist/stm32/utils/svdUtils.d.ts +31 -0
- package/dist/stm32/utils/svdUtils.js +30 -0
- package/dist/stm32/utils/truncate.d.ts +7 -0
- package/dist/stm32/utils/truncate.js +9 -0
- package/dist/stm32/utils/usb_utils.d.ts +5 -0
- package/dist/stm32/utils/usb_utils.js +23 -0
- package/dist/stm32/utils/user_code.d.ts +14 -0
- package/dist/stm32/utils/user_code.js +43 -0
- package/dist/stm32/utils/utils_mutex.d.ts +15 -0
- package/dist/stm32/utils/utils_mutex.js +93 -0
- package/dist/stm32/validate.d.ts +16 -0
- package/dist/stm32/validate.js +46 -0
- package/mods/claude/.claude-plugin/marketplace.json +8 -0
- package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +50 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +22 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +19 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +9 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +43 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +34 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
- package/package.json +11 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DebugAction } from './DebugAction.js';
|
|
2
|
+
// Debug adapter that speaks DAP over stdio; debug_utils spawns it and sends the session requests (breakpoints, step, ...).
|
|
3
|
+
// Only probe-rs has one; the launch config in debug_utils is probe-rs specific.
|
|
4
|
+
export class DebugActionServer extends DebugAction {
|
|
5
|
+
static title = 'Debug server';
|
|
6
|
+
static commands = {
|
|
7
|
+
'probe-rs': ['dap-server'],
|
|
8
|
+
};
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ServerCall } from './sync.js';
|
|
2
|
+
import { Project } from './tool.js';
|
|
3
|
+
/**
|
|
4
|
+
* Syntax-checks files of the project's server copy.
|
|
5
|
+
* @param call server tool caller
|
|
6
|
+
* @param project project root and name
|
|
7
|
+
* @param files project-relative files, globs or folders (default: Core and App)
|
|
8
|
+
* @returns `file:line:col: severity: message` lines
|
|
9
|
+
*/
|
|
10
|
+
export declare function syntaxCheckProject(call: ServerCall, project: Project, files?: string[]): Promise<string>;
|
|
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 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;
|
|
6
|
+
ok: boolean;
|
|
7
|
+
elf?: string;
|
|
8
|
+
note: string;
|
|
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
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ToolDef } from './tool.js';
|
|
2
|
+
export interface DaemonPaths {
|
|
3
|
+
dir: string;
|
|
4
|
+
token: string;
|
|
5
|
+
socket: string;
|
|
6
|
+
lock: string;
|
|
7
|
+
log: string;
|
|
8
|
+
}
|
|
9
|
+
export interface DaemonRequest {
|
|
10
|
+
id: number;
|
|
11
|
+
token: string;
|
|
12
|
+
cwd: string;
|
|
13
|
+
tool: string;
|
|
14
|
+
args: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export type DaemonResponse = {
|
|
17
|
+
id: number;
|
|
18
|
+
ok: true;
|
|
19
|
+
text: string;
|
|
20
|
+
} | {
|
|
21
|
+
id: number;
|
|
22
|
+
ok: false;
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
export declare const PING = "ping";
|
|
26
|
+
/**
|
|
27
|
+
* Daemon files of one project, all inside <project>/.turingcode. Windows paths are lowercased so "c:" and "C:" give the same pipe.
|
|
28
|
+
* shortcut: Unix socket paths are limited to ~104 chars, so a very deep project folder fails to listen; move the socket to a short dir if that bites.
|
|
29
|
+
* @param root project root
|
|
30
|
+
* @returns the daemon's folder, token file, socket and probe lock file
|
|
31
|
+
*/
|
|
32
|
+
export declare function daemonPaths(root: string): DaemonPaths;
|
|
33
|
+
/**
|
|
34
|
+
* Sends one tool call to the project's daemon.
|
|
35
|
+
* @param paths daemon files of the project
|
|
36
|
+
* @param tool tool name, or PING
|
|
37
|
+
* @param args tool arguments
|
|
38
|
+
* @param cwd the caller's working directory
|
|
39
|
+
* @returns the daemon's response, or undefined when no daemon is running
|
|
40
|
+
*/
|
|
41
|
+
export declare function forwardToDaemon(paths: DaemonPaths, tool: string, args: Record<string, unknown>, cwd: string): Promise<DaemonResponse | undefined>;
|
|
42
|
+
/**
|
|
43
|
+
* Asks the project's daemon whether it runs and holds a debug session.
|
|
44
|
+
* @param paths daemon files of the project
|
|
45
|
+
* @returns undefined when no daemon answers, else whether a debug session is active
|
|
46
|
+
*/
|
|
47
|
+
export declare function pingDaemon(paths: DaemonPaths): Promise<{
|
|
48
|
+
session: boolean;
|
|
49
|
+
} | undefined>;
|
|
50
|
+
/**
|
|
51
|
+
* Launches the project's daemon in the background and waits until it answers.
|
|
52
|
+
* @param root project root; the daemon runs with it as its working directory
|
|
53
|
+
* @throws when the daemon does not answer within START_TIMEOUT_MS, with the end of its log
|
|
54
|
+
*/
|
|
55
|
+
export declare function startDaemon(root: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Runs the daemon for a project until it is idle without a debug session: serves the given tools over the socket,
|
|
58
|
+
* one request at a time per connection, with the probe lock file held while the probe is in use.
|
|
59
|
+
* @param root project root
|
|
60
|
+
* @param tools the tools the daemon may run
|
|
61
|
+
* @returns resolves once the daemon has stopped
|
|
62
|
+
*/
|
|
63
|
+
export declare function runDaemon(root: string, tools: ToolDef[]): Promise<void>;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Debug daemon: a background turingcode process per project that holds the probe-rs debug session between CLI runs.
|
|
2
|
+
// `debug start` launches it; probe and debug commands forward their tool call to it over a user-only local socket
|
|
3
|
+
// (Unix socket in <project>/.turingcode, a per-user, per-project named pipe on Windows; access is checked with the
|
|
4
|
+
// token file next to it). It exits on its own once no debug session is running.
|
|
5
|
+
import { spawn } from 'node:child_process';
|
|
6
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
7
|
+
import { closeSync, mkdirSync, openSync } from 'node:fs';
|
|
8
|
+
import { chmod, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
9
|
+
import { connect, createServer } from 'node:net';
|
|
10
|
+
import { userInfo } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { debugSessionActive, stopSession } from './session.js';
|
|
14
|
+
import { sha256 } from './utils/hash_utils.js';
|
|
15
|
+
import { setProbeLockFile } from './utils/utils_mutex.js';
|
|
16
|
+
import { safetyCheck, validateArgs } from './validate.js';
|
|
17
|
+
export const PING = 'ping'; // reserved tool: replies {"session": <true while a debug session runs>}
|
|
18
|
+
const IDLE_EXIT_MS = 5000; // the daemon exits once no request ran for this long without a debug session
|
|
19
|
+
const START_TIMEOUT_MS = 10_000;
|
|
20
|
+
/**
|
|
21
|
+
* Daemon files of one project, all inside <project>/.turingcode. Windows paths are lowercased so "c:" and "C:" give the same pipe.
|
|
22
|
+
* shortcut: Unix socket paths are limited to ~104 chars, so a very deep project folder fails to listen; move the socket to a short dir if that bites.
|
|
23
|
+
* @param root project root
|
|
24
|
+
* @returns the daemon's folder, token file, socket and probe lock file
|
|
25
|
+
*/
|
|
26
|
+
export function daemonPaths(root) {
|
|
27
|
+
const dir = join(root, '.turingcode');
|
|
28
|
+
const id = sha256(process.platform === 'win32' ? dir.toLowerCase() : dir).slice(0, 16);
|
|
29
|
+
const socket = process.platform === 'win32' ? `\\\\.\\pipe\\turingcode-daemon-${userInfo().username}-${id}` : join(dir, 'daemon.sock');
|
|
30
|
+
return { dir, token: join(dir, 'daemon.token'), socket, lock: join(dir, 'probe.lock'), log: join(dir, 'daemon.log') };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sends one tool call to the project's daemon.
|
|
34
|
+
* @param paths daemon files of the project
|
|
35
|
+
* @param tool tool name, or PING
|
|
36
|
+
* @param args tool arguments
|
|
37
|
+
* @param cwd the caller's working directory
|
|
38
|
+
* @returns the daemon's response, or undefined when no daemon is running
|
|
39
|
+
*/
|
|
40
|
+
export async function forwardToDaemon(paths, tool, args, cwd) {
|
|
41
|
+
const token = (await readFile(paths.token, 'utf8').catch(() => '')).trim();
|
|
42
|
+
if (!token)
|
|
43
|
+
return undefined;
|
|
44
|
+
const req = { id: 1, token, cwd, tool, args };
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
const sock = connect(paths.socket);
|
|
47
|
+
let buf = '';
|
|
48
|
+
sock.setEncoding('utf8');
|
|
49
|
+
sock.on('connect', () => sock.write(JSON.stringify(req) + '\n'));
|
|
50
|
+
sock.on('data', (d) => {
|
|
51
|
+
buf += d;
|
|
52
|
+
const nl = buf.indexOf('\n');
|
|
53
|
+
if (nl < 0)
|
|
54
|
+
return;
|
|
55
|
+
sock.end();
|
|
56
|
+
try {
|
|
57
|
+
resolve(JSON.parse(buf.slice(0, nl)));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
resolve({ id: 1, ok: false, error: 'Bad reply from the debug daemon' });
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
sock.on('error', () => resolve(undefined));
|
|
64
|
+
sock.on('close', () => resolve(undefined));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Asks the project's daemon whether it runs and holds a debug session.
|
|
69
|
+
* @param paths daemon files of the project
|
|
70
|
+
* @returns undefined when no daemon answers, else whether a debug session is active
|
|
71
|
+
*/
|
|
72
|
+
export async function pingDaemon(paths) {
|
|
73
|
+
const res = await forwardToDaemon(paths, PING, {}, process.cwd());
|
|
74
|
+
return res?.ok ? JSON.parse(res.text) : undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Launches the project's daemon in the background and waits until it answers.
|
|
78
|
+
* @param root project root; the daemon runs with it as its working directory
|
|
79
|
+
* @throws when the daemon does not answer within START_TIMEOUT_MS, with the end of its log
|
|
80
|
+
*/
|
|
81
|
+
export async function startDaemon(root) {
|
|
82
|
+
const cli = fileURLToPath(new URL('../cli/index.js', import.meta.url));
|
|
83
|
+
const paths = daemonPaths(root);
|
|
84
|
+
mkdirSync(paths.dir, { recursive: true, mode: 0o700 });
|
|
85
|
+
const log = openSync(paths.log, 'w');
|
|
86
|
+
try {
|
|
87
|
+
spawn(process.execPath, [cli, 'debug', 'daemon', '--root', root], { cwd: root, detached: true, stdio: ['ignore', 'ignore', log], windowsHide: true }).unref();
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
closeSync(log);
|
|
91
|
+
}
|
|
92
|
+
for (const end = Date.now() + START_TIMEOUT_MS; Date.now() < end; await new Promise((r) => setTimeout(r, 100)))
|
|
93
|
+
if (await pingDaemon(paths))
|
|
94
|
+
return;
|
|
95
|
+
const tail = (await readFile(paths.log, 'utf8').catch(() => '')).trim().slice(-1500);
|
|
96
|
+
throw new Error(`The debug daemon did not start within ${START_TIMEOUT_MS / 1000} s${tail ? `:
|
|
97
|
+
${tail}` : ''}`);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Runs the daemon for a project until it is idle without a debug session: serves the given tools over the socket,
|
|
101
|
+
* one request at a time per connection, with the probe lock file held while the probe is in use.
|
|
102
|
+
* @param root project root
|
|
103
|
+
* @param tools the tools the daemon may run
|
|
104
|
+
* @returns resolves once the daemon has stopped
|
|
105
|
+
*/
|
|
106
|
+
export async function runDaemon(root, tools) {
|
|
107
|
+
const paths = daemonPaths(root);
|
|
108
|
+
const byName = new Map(tools.map((t) => [t.name, t]));
|
|
109
|
+
await mkdir(paths.dir, { recursive: true, mode: 0o700 });
|
|
110
|
+
if (process.platform !== 'win32')
|
|
111
|
+
await chmod(paths.dir, 0o700);
|
|
112
|
+
if (await pingDaemon(paths))
|
|
113
|
+
return; // another daemon serves this project
|
|
114
|
+
if (process.platform !== 'win32')
|
|
115
|
+
await unlink(paths.socket).catch(() => { }); // stale socket of a crashed daemon
|
|
116
|
+
setProbeLockFile(paths.lock);
|
|
117
|
+
const token = randomBytes(32).toString('hex');
|
|
118
|
+
let running = 0;
|
|
119
|
+
let lastActivity = Date.now();
|
|
120
|
+
let stopping = false; // set once idle: requests on still-open connections are dropped, so no session starts in a closing daemon
|
|
121
|
+
// Checks the token and arguments, then runs one tool call.
|
|
122
|
+
const handle = async (req) => {
|
|
123
|
+
const given = Buffer.from(String(req.token));
|
|
124
|
+
if (given.length !== token.length || !timingSafeEqual(given, Buffer.from(token)))
|
|
125
|
+
return { id: req.id, ok: false, error: 'Bad token' };
|
|
126
|
+
if (req.tool === PING)
|
|
127
|
+
return { id: req.id, ok: true, text: JSON.stringify({ session: debugSessionActive() }) };
|
|
128
|
+
const def = byName.get(req.tool);
|
|
129
|
+
if (!def)
|
|
130
|
+
return { id: req.id, ok: false, error: `Unknown tool: ${req.tool}` };
|
|
131
|
+
const invalid = validateArgs(def, req.args) ?? safetyCheck(def, debugSessionActive());
|
|
132
|
+
if (invalid)
|
|
133
|
+
return { id: req.id, ok: false, error: invalid };
|
|
134
|
+
const ctx = { cwd: typeof req.cwd === 'string' ? req.cwd : root, socketPath: paths.socket, debugActive: debugSessionActive };
|
|
135
|
+
running++;
|
|
136
|
+
try {
|
|
137
|
+
return { id: req.id, ok: true, text: await def.run(req.args, ctx) };
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
return { id: req.id, ok: false, error: e?.message ?? String(e) };
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
running--;
|
|
144
|
+
lastActivity = Date.now();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const server = createServer((sock) => {
|
|
148
|
+
let buf = '';
|
|
149
|
+
let queue = Promise.resolve(); // replies go out in request order
|
|
150
|
+
sock.setEncoding('utf8');
|
|
151
|
+
sock.on('error', () => { });
|
|
152
|
+
sock.on('data', (d) => {
|
|
153
|
+
if (stopping)
|
|
154
|
+
return sock.destroy();
|
|
155
|
+
buf += d;
|
|
156
|
+
if (buf.length > 1_000_000)
|
|
157
|
+
return sock.destroy();
|
|
158
|
+
let nl;
|
|
159
|
+
while ((nl = buf.indexOf('\n')) >= 0) {
|
|
160
|
+
const line = buf.slice(0, nl);
|
|
161
|
+
buf = buf.slice(nl + 1);
|
|
162
|
+
queue = queue.then(async () => {
|
|
163
|
+
let res;
|
|
164
|
+
try {
|
|
165
|
+
const req = JSON.parse(line);
|
|
166
|
+
res = req && typeof req === 'object' ? await handle(req) : { id: 0, ok: false, error: 'Bad request' };
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
res = { id: 0, ok: false, error: 'Bad request' };
|
|
170
|
+
}
|
|
171
|
+
if (!sock.destroyed)
|
|
172
|
+
sock.write(JSON.stringify(res) + '\n');
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
try {
|
|
178
|
+
await new Promise((resolve, reject) => (server.once('error', reject), server.listen(paths.socket, resolve)));
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return; // another daemon won the race to listen; its token file stays untouched
|
|
182
|
+
}
|
|
183
|
+
if (process.platform !== 'win32')
|
|
184
|
+
await chmod(paths.socket, 0o600);
|
|
185
|
+
// The token goes in only once listening, by rename, so clients never read a half-written or another daemon's token.
|
|
186
|
+
const tmp = `${paths.token}.${process.pid}`;
|
|
187
|
+
await writeFile(tmp, token, { mode: 0o600 });
|
|
188
|
+
await rename(tmp, paths.token);
|
|
189
|
+
await new Promise((resolve) => {
|
|
190
|
+
const timer = setInterval(() => {
|
|
191
|
+
if (running || debugSessionActive() || Date.now() - lastActivity < IDLE_EXIT_MS)
|
|
192
|
+
return;
|
|
193
|
+
clearInterval(timer);
|
|
194
|
+
resolve();
|
|
195
|
+
}, 500);
|
|
196
|
+
});
|
|
197
|
+
stopping = true;
|
|
198
|
+
const closed = new Promise((resolve) => server.close(() => resolve())); // stops accepting now, frees the socket name
|
|
199
|
+
await unlink(paths.token).catch(() => { });
|
|
200
|
+
await stopSession();
|
|
201
|
+
await closed;
|
|
202
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ServerCall } from './sync.js';
|
|
2
|
+
import { Project } from './tool.js';
|
|
3
|
+
/**
|
|
4
|
+
* Returns the folder holding the baseline: each generated file with its USER CODE section bodies emptied.
|
|
5
|
+
* @param root STM32 project root
|
|
6
|
+
* @returns the baseline folder
|
|
7
|
+
*/
|
|
8
|
+
export declare function baselineDir(root: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Replaces the body of every well-formed USER CODE section of a file.
|
|
11
|
+
* @param text file contents
|
|
12
|
+
* @param body gets the section name and current body, returns the new body
|
|
13
|
+
* @returns the new contents
|
|
14
|
+
*/
|
|
15
|
+
export declare function mapUserSections(text: string, body: (name: string, old: string) => string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Saves the baseline: every file with USER CODE markers, with its section bodies emptied, and the .ioc as it is (to
|
|
18
|
+
* tell later which side changed it). Call it right after CubeMX output reached the project (template, create, regen).
|
|
19
|
+
* shortcut: user-written files with markers are saved too, so later edits outside their sections are reported
|
|
20
|
+
* even though CubeMX leaves those files alone; skip files CubeMX does not generate if that gets noisy.
|
|
21
|
+
* @param root STM32 project root
|
|
22
|
+
* @returns the number of files with markers saved
|
|
23
|
+
*/
|
|
24
|
+
export declare function recordBaseline(root: string): Promise<number>;
|
|
25
|
+
/**
|
|
26
|
+
* Makes sure the regen uses the newest .ioc: the sync before it would otherwise overwrite an .ioc that a server
|
|
27
|
+
* tool (pins, clocks, set_params) changed. Takes the server's .ioc when only the server changed it since the baseline,
|
|
28
|
+
* keeps the local one (the sync sends it) when only it changed.
|
|
29
|
+
* @param call server tool caller
|
|
30
|
+
* @param project STM32 project root and name
|
|
31
|
+
* @returns a note per .ioc taken from the server
|
|
32
|
+
* @throws when both sides changed the .ioc, or it differs and there is no baseline to tell which side did
|
|
33
|
+
*/
|
|
34
|
+
export declare function takeServerIoc(call: ServerCall, project: Project): Promise<string[]>;
|
|
35
|
+
/**
|
|
36
|
+
* Finds the code a regen would remove from the local files, before running it: broken USER CODE markers, and code
|
|
37
|
+
* outside the sections that differs from the baseline.
|
|
38
|
+
* @param root STM32 project root
|
|
39
|
+
* @returns one diff or message per affected file (broken markers first), the broken-marker messages alone, the local
|
|
40
|
+
* marked files, and those without a baseline
|
|
41
|
+
*/
|
|
42
|
+
export declare function checkBeforeRegen(root: string): Promise<{
|
|
43
|
+
findings: string[];
|
|
44
|
+
broken: string[];
|
|
45
|
+
local: Map<string, string>;
|
|
46
|
+
unbaselined: Set<string>;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Finds the code a regen removed, comparing a local file with the regenerated one: USER CODE sections with code that
|
|
50
|
+
* no longer exist (e.g. a peripheral was removed), and, for files without a baseline, every change regen made.
|
|
51
|
+
* @param f posix relative path, for the headers
|
|
52
|
+
* @param local local contents
|
|
53
|
+
* @param regenerated contents after regen
|
|
54
|
+
* @param hasBaseline false: show the whole diff, since your code cannot be told apart from CubeMX's changes
|
|
55
|
+
* @returns the diff, or "" when nothing is removed
|
|
56
|
+
*/
|
|
57
|
+
export declare function checkAfterRegen(f: string, local: string, regenerated: string, hasBaseline: boolean): string;
|
|
58
|
+
/**
|
|
59
|
+
* Regenerates the project's code from its .ioc on the server and pulls the result, refusing to remove local code:
|
|
60
|
+
* checks the local files, takes the server's .ioc if only it changed, syncs the files up, regenerates, checks the
|
|
61
|
+
* regenerated files before overwriting local ones, pulls them and saves a new baseline.
|
|
62
|
+
* @param call server tool caller
|
|
63
|
+
* @param project STM32 project root and name
|
|
64
|
+
* @param force regenerate and pull even when code would be removed (the removed code is still shown)
|
|
65
|
+
* @returns the removed code (with force), then the sync, regen and pull summaries
|
|
66
|
+
* @throws with the code that would be removed when force is not set; nothing local changes then
|
|
67
|
+
*/
|
|
68
|
+
export declare function regenerateProject(call: ServerCall, project: Project, force?: boolean): Promise<string>;
|