@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.
- package/README.md +2 -0
- package/dist/cli/create-cli.d.ts +10 -0
- package/dist/cli/create-cli.js +12 -2
- 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/json-shape.js +0 -43
- package/dist/core/mcp.d.ts +92 -0
- package/dist/core/mcp.js +57 -30
- package/dist/core/query.d.ts +69 -0
- package/dist/core/query.js +11 -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/check.js +2 -3
- 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/load.js +24 -17
- package/dist/features/contract/model.d.ts +67 -0
- package/dist/features/contract/rules.d.ts +137 -0
- package/dist/features/contract/rules.js +11 -12
- 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/check.js +16 -23
- package/dist/features/table/command.d.ts +3 -0
- package/dist/features/table/command.js +4 -5
- package/dist/features/table/read.d.ts +28 -0
- package/dist/features/table/read.js +23 -3
- 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
- package/dist/features/clock/command.js +0 -63
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ npm install -g @turingcode/cli
|
|
|
16
16
|
| `turingcode tool list` | Lists the Turing Code server tools your token can call, one line each. |
|
|
17
17
|
| `turingcode tool describe <name>... [--json]` | Shows the full description and arguments of the named tools (`--json`: raw schemas). |
|
|
18
18
|
| `turingcode tool call <name> [--args json\|file]` | Calls a server tool and prints its text result, e.g. `> result.md` to save it. |
|
|
19
|
+
| `turingcode install-skills [names..] [--target dir]` | Copies the shipped agent skills into `~/.agents/skills` (Codex, OpenCode). |
|
|
20
|
+
| `turingcode install-mods [names..]` | Installs the shipped Claude Code plugins (`turingcode-skills`) via `claude plugin`. |
|
|
19
21
|
|
|
20
22
|
Output is compact text: a status line, one line per finding (the same error on several rows is one
|
|
21
23
|
line), and a fix hint per code. `--json` prints the full result. Exit codes: `0` PASS/VALID,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the yargs parser with every feature command registered.
|
|
3
|
+
*
|
|
4
|
+
* @param {string[]} args - Arguments after `turingcode`.
|
|
5
|
+
* @returns The parser; call `parseAsync()` to run it.
|
|
6
|
+
* @throws {Error} When the first word is not a command (yargs alone would print help and exit 0 for `<unknown> --help`).
|
|
7
|
+
* It and the argument errors yargs finds (missing option, unknown subcommand, bad flag value) carry
|
|
8
|
+
* `exitCode` 2, so they differ from a failed check or request (1).
|
|
9
|
+
*/
|
|
10
|
+
export declare function createCli(args: string[]): import("yargs").Argv<{}>;
|
package/dist/cli/create-cli.js
CHANGED
|
@@ -2,8 +2,13 @@ import yargs from "yargs/yargs";
|
|
|
2
2
|
import contract from "../features/contract/command.js";
|
|
3
3
|
import table from "../features/table/command.js";
|
|
4
4
|
import tool from "../features/tool/command.js";
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import project from "../features/project/command.js";
|
|
6
|
+
import probe from "../features/probe/command.js";
|
|
7
|
+
import debug from "../features/debug/command.js";
|
|
8
|
+
import status from "../features/status/command.js";
|
|
9
|
+
import installSkills from "../features/install-skills/command.js";
|
|
10
|
+
import installMods from "../features/install-mods/command.js";
|
|
11
|
+
const commands = [contract, table, tool, project, probe, debug, status, installSkills, installMods];
|
|
7
12
|
const names = commands.map(c => String(c.command).split(" ")[0]);
|
|
8
13
|
/** Exit code of every argument error, as each command's help documents. */
|
|
9
14
|
const BAD_ARGUMENTS = 2;
|
|
@@ -31,6 +36,11 @@ export function createCli(args) {
|
|
|
31
36
|
"- Tables: check Markdown or CSV tables against a contract (a JSON file of tables, columns and",
|
|
32
37
|
" MongoDB-style checks): `contract describe` shows the tables to write; `table check` until PASS.",
|
|
33
38
|
" Contract format: $0 contract --help.",
|
|
39
|
+
"- STM32 projects: `project sync`, `project build` on the server, `probe program` to flash the board,",
|
|
40
|
+
" `probe read` and `probe peripheral` to inspect it, `debug start` ... `debug stop` for a live debug",
|
|
41
|
+
" session; `status` shows the project and the installed debug tools.",
|
|
42
|
+
"- Agent skills: `install-skills` copies them into ~/.agents/skills (Codex, OpenCode);",
|
|
43
|
+
" `install-mods` installs them into Claude Code as the turingcode-skills plugin.",
|
|
34
44
|
].join("\n"))
|
|
35
45
|
.command(commands)
|
|
36
46
|
.demandCommand(1, `Name a command: ${names.slice(0, -1).join(", ")} or ${names.at(-1)}`)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runs the CLI. Errors propagate here, are printed, and exit with code 1, or with the error's
|
|
4
|
+
* own `exitCode` when it has one (e.g. 2 for bad input to a check command).
|
|
5
|
+
*
|
|
6
|
+
* @param {string[]} [args] - Arguments after `turingcode`.
|
|
7
|
+
* @returns {Promise<void>} Resolves when the command finished.
|
|
8
|
+
*/
|
|
9
|
+
export declare function main(args?: string[]): Promise<void>;
|
package/dist/cli/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { hideBin } from "yargs/helpers";
|
|
3
|
+
import { loadDotEnv } from "../core/files.js";
|
|
3
4
|
import { createCli } from "./create-cli.js";
|
|
4
5
|
/**
|
|
5
6
|
* Runs the CLI. Errors propagate here, are printed, and exit with code 1, or with the error's
|
|
@@ -10,11 +11,14 @@ import { createCli } from "./create-cli.js";
|
|
|
10
11
|
*/
|
|
11
12
|
export async function main(args = hideBin(process.argv)) {
|
|
12
13
|
try {
|
|
14
|
+
loadDotEnv(".env");
|
|
13
15
|
await createCli(args).parseAsync();
|
|
14
16
|
}
|
|
15
17
|
catch (error) {
|
|
16
|
-
process.stderr.write(`turingcode: ${error.message}\nRun "turingcode <command> --help" for usage.\n`);
|
|
17
18
|
process.exitCode = error.exitCode ?? 1;
|
|
19
|
+
// the usage hint only helps with bad arguments, not after a failed request or a stopped regen
|
|
20
|
+
const hint = process.exitCode === 2 ? `Run "turingcode <command> --help" for usage.\n` : "";
|
|
21
|
+
process.stderr.write(`turingcode: ${error.message}\n${hint}`);
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
void main();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Raised when a search or size bound is exceeded (maps to SEARCH_BOUND_EXCEEDED). */
|
|
2
|
+
export declare class BoundError extends Error {
|
|
3
|
+
readonly bound: string;
|
|
4
|
+
readonly limit: number;
|
|
5
|
+
/**
|
|
6
|
+
* @param {string} bound - Name of the bound that was exceeded, e.g. `max_table_rows`.
|
|
7
|
+
* @param {number} limit - Its value.
|
|
8
|
+
*/
|
|
9
|
+
constructor(bound: string, limit: number);
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a text is a real calendar date written `YYYY-MM-DD`. Such dates sort
|
|
3
|
+
* correctly as plain text, so they can be compared without parsing.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} v - The text to test.
|
|
6
|
+
* @returns {boolean} True for a valid date, false otherwise (including `2026-02-30`).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* isDate("2026-02-28"); // true
|
|
10
|
+
* isDate("2026-02-30"); // false
|
|
11
|
+
*/
|
|
12
|
+
export declare function isDate(v: string): boolean;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Fraction } from "./fraction.js";
|
|
2
|
+
/** Raised when an expression cannot be parsed or evaluated. */
|
|
3
|
+
export declare class AstError extends Error {
|
|
4
|
+
}
|
|
5
|
+
export type Node = {
|
|
6
|
+
k: "num";
|
|
7
|
+
v: Fraction;
|
|
8
|
+
} | {
|
|
9
|
+
k: "name";
|
|
10
|
+
name: string;
|
|
11
|
+
} | {
|
|
12
|
+
k: "neg";
|
|
13
|
+
a: Node;
|
|
14
|
+
} | {
|
|
15
|
+
k: "bin";
|
|
16
|
+
op: "+" | "-" | "*" | "/" | "**";
|
|
17
|
+
a: Node;
|
|
18
|
+
b: Node;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Parses an exact-arithmetic expression (spec §3.4): literals, names,
|
|
22
|
+
* unary +/-, + - * / and right-associative **, with parentheses.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} text - The expression, e.g. `180/pi`.
|
|
25
|
+
* @param {number} maxNodes - `bounds.max_ast_nodes`.
|
|
26
|
+
* @param {ReadonlySet<string>} names - Names the expression may use.
|
|
27
|
+
* @returns {Node} The tree.
|
|
28
|
+
* @throws {AstError} On a syntax error or unknown name.
|
|
29
|
+
* @throws {BoundError} When the tree would exceed maxNodes.
|
|
30
|
+
*/
|
|
31
|
+
export declare function parseExpression(text: string, maxNodes: number, names: ReadonlySet<string>): Node;
|
|
32
|
+
/**
|
|
33
|
+
* Evaluates a tree exactly. A name bound to null (an irrational constant
|
|
34
|
+
* such as `pi`) makes the result null; division by zero is still caught
|
|
35
|
+
* wherever both sides are exact.
|
|
36
|
+
*
|
|
37
|
+
* @param {Node} n - The tree.
|
|
38
|
+
* @param {Readonly<Record<string, Fraction | null>>} env - Values of the names.
|
|
39
|
+
* @returns {Fraction | null} The exact value, or null when it involves an irrational name.
|
|
40
|
+
* @throws {AstError} On division by zero or a bad exponent.
|
|
41
|
+
*/
|
|
42
|
+
export declare function evaluate(n: Node, env: Readonly<Record<string, Fraction | null>>): Fraction | null;
|
|
43
|
+
/**
|
|
44
|
+
* The names an expression uses.
|
|
45
|
+
*
|
|
46
|
+
* @param {Node} n - The tree.
|
|
47
|
+
* @returns {Set<string>} Distinct names.
|
|
48
|
+
*/
|
|
49
|
+
export declare function namesIn(n: Node): Set<string>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Largest integer exponent magnitude allowed anywhere in exact arithmetic (spec §3.4). */
|
|
2
|
+
export declare const MAX_EXPONENT = 1000;
|
|
3
|
+
/** Raised for any arithmetic the exact engine refuses (bad literal, division by zero, bad exponent). */
|
|
4
|
+
export declare class ArithmeticError extends Error {
|
|
5
|
+
}
|
|
6
|
+
/** An exact rational number on BigInt; never touches a JS float. */
|
|
7
|
+
export declare class Fraction {
|
|
8
|
+
readonly n: bigint;
|
|
9
|
+
readonly d: bigint;
|
|
10
|
+
/**
|
|
11
|
+
* Builds a reduced fraction with a positive denominator.
|
|
12
|
+
*
|
|
13
|
+
* @param {bigint} n - Numerator.
|
|
14
|
+
* @param {bigint} [d=1n] - Denominator.
|
|
15
|
+
* @throws {ArithmeticError} When the denominator is zero.
|
|
16
|
+
*/
|
|
17
|
+
constructor(n: bigint, d?: bigint);
|
|
18
|
+
/**
|
|
19
|
+
* Parses a number literal: integer, decimal (with optional exponent),
|
|
20
|
+
* `0x..` / `..h` hex, or `0b..` / `..b` binary.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} text - The literal.
|
|
23
|
+
* @returns {Fraction} Its exact value.
|
|
24
|
+
* @throws {ArithmeticError} When the text is not a literal or its exponent exceeds MAX_EXPONENT.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* Fraction.parse("1e-3").toString(); // "1/1000"
|
|
28
|
+
*/
|
|
29
|
+
static parse(text: string): Fraction;
|
|
30
|
+
/**
|
|
31
|
+
* Parses a signed decimal number as written in a table cell: `-3`, `+0.25`, `1.5e3`.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} text - The cell text.
|
|
34
|
+
* @returns {Fraction | null} Its exact value, or null when it is not a decimal number.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* Fraction.parseDecimal("-0.5")?.toString(); // "-1/2"
|
|
38
|
+
*/
|
|
39
|
+
static parseDecimal(text: string): Fraction | null;
|
|
40
|
+
/**
|
|
41
|
+
* Compares with another fraction.
|
|
42
|
+
*
|
|
43
|
+
* @param {Fraction} o - The other value.
|
|
44
|
+
* @returns {number} -1, 0 or 1.
|
|
45
|
+
*/
|
|
46
|
+
cmp(o: Fraction): number;
|
|
47
|
+
/** @returns {Fraction} |this|. */
|
|
48
|
+
abs(): Fraction;
|
|
49
|
+
/** @returns {boolean} Whether the value is zero. */
|
|
50
|
+
isZero(): boolean;
|
|
51
|
+
/** @returns {boolean} Whether the value is a whole number. */
|
|
52
|
+
isInteger(): boolean;
|
|
53
|
+
/** @param {Fraction} o - Addend. @returns {Fraction} this + o. */
|
|
54
|
+
add(o: Fraction): Fraction;
|
|
55
|
+
/** @param {Fraction} o - Subtrahend. @returns {Fraction} this - o. */
|
|
56
|
+
sub(o: Fraction): Fraction;
|
|
57
|
+
/** @param {Fraction} o - Factor. @returns {Fraction} this * o. */
|
|
58
|
+
mul(o: Fraction): Fraction;
|
|
59
|
+
/**
|
|
60
|
+
* @param {Fraction} o - Divisor.
|
|
61
|
+
* @returns {Fraction} this / o.
|
|
62
|
+
* @throws {ArithmeticError} When o is zero.
|
|
63
|
+
*/
|
|
64
|
+
div(o: Fraction): Fraction;
|
|
65
|
+
/** @returns {Fraction} -this. */
|
|
66
|
+
neg(): Fraction;
|
|
67
|
+
/**
|
|
68
|
+
* Raises to a whole exponent.
|
|
69
|
+
*
|
|
70
|
+
* @param {Fraction} e - Exponent; must be an integer with |e| <= MAX_EXPONENT.
|
|
71
|
+
* @returns {Fraction} this ** e.
|
|
72
|
+
* @throws {ArithmeticError} For a non-integer or too-large exponent, or 0 ** negative.
|
|
73
|
+
*/
|
|
74
|
+
pow(e: Fraction): Fraction;
|
|
75
|
+
/**
|
|
76
|
+
* Decimal text: exact when the fraction terminates, else rounded to `places`
|
|
77
|
+
* decimals and prefixed with `~`.
|
|
78
|
+
*
|
|
79
|
+
* @param {number} [places=10] - Decimals for a non-terminating value.
|
|
80
|
+
* @returns {string} e.g. `12.5`, `~0.3333333333`.
|
|
81
|
+
*/
|
|
82
|
+
toDecimal(places?: number): string;
|
|
83
|
+
/** @returns {string} `n` or `n/d`. */
|
|
84
|
+
toString(): string;
|
|
85
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies the TURINGCODE_* settings of a `.env` file (`KEY=value` lines, optional `export ` and quotes)
|
|
3
|
+
* into an environment, keeping variables that are already set.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} file - The .env path; a missing or unreadable file is ignored.
|
|
6
|
+
* @param {NodeJS.ProcessEnv} [env=process.env] - Environment to fill.
|
|
7
|
+
* @returns {string[]} The keys it set.
|
|
8
|
+
*/
|
|
9
|
+
export declare function loadDotEnv(file: string, env?: NodeJS.ProcessEnv): string[];
|
|
10
|
+
/**
|
|
11
|
+
* The CLI's package folder: the nearest folder above this module that holds a package.json
|
|
12
|
+
* (works from dist/ and from the test build).
|
|
13
|
+
*
|
|
14
|
+
* @returns {string} Its path.
|
|
15
|
+
* @throws {Error} When no package.json is found.
|
|
16
|
+
*/
|
|
17
|
+
export declare function packageRoot(): string;
|
|
18
|
+
export type JsonFile = {
|
|
19
|
+
ok: true;
|
|
20
|
+
data: unknown;
|
|
21
|
+
} | {
|
|
22
|
+
ok: false;
|
|
23
|
+
error: "unreadable" | "bad_json";
|
|
24
|
+
message: string;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Reads and parses a UTF-8 JSON file (a leading BOM is ignored).
|
|
28
|
+
*
|
|
29
|
+
* @param {string} path - File path.
|
|
30
|
+
* @returns {JsonFile} The parsed value, or why it could not be read.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const file = readJsonFile("contract.json");
|
|
34
|
+
* if (file.ok) use(file.data);
|
|
35
|
+
*/
|
|
36
|
+
export declare function readJsonFile(path: string): JsonFile;
|
|
37
|
+
/**
|
|
38
|
+
* Reads a JSON flag: inline JSON when it starts with `{` or `[`, otherwise a path to a .json file.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} value - Raw flag value.
|
|
41
|
+
* @returns {unknown} The parsed JSON.
|
|
42
|
+
* @throws {Error} When the value is neither inline JSON nor an existing file, the file cannot be read, or the JSON is invalid.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readJsonArg(value: string): unknown;
|
package/dist/core/files.js
CHANGED
|
@@ -1,4 +1,49 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* Copies the TURINGCODE_* settings of a `.env` file (`KEY=value` lines, optional `export ` and quotes)
|
|
6
|
+
* into an environment, keeping variables that are already set.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} file - The .env path; a missing or unreadable file is ignored.
|
|
9
|
+
* @param {NodeJS.ProcessEnv} [env=process.env] - Environment to fill.
|
|
10
|
+
* @returns {string[]} The keys it set.
|
|
11
|
+
*/
|
|
12
|
+
export function loadDotEnv(file, env = process.env) {
|
|
13
|
+
let text;
|
|
14
|
+
try {
|
|
15
|
+
text = readFileSync(file, "utf8").replace(/^/, "");
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
const set = [];
|
|
21
|
+
for (const line of text.split(/\r?\n/)) {
|
|
22
|
+
const m = line.match(/^\s*(?:export\s+)?(TURINGCODE_\w+)\s*=\s*(.*?)\s*$/);
|
|
23
|
+
if (!m || env[m[1]]?.trim())
|
|
24
|
+
continue;
|
|
25
|
+
env[m[1]] = m[2].replace(/^(["'])(.*)\1$/, "$2");
|
|
26
|
+
set.push(m[1]);
|
|
27
|
+
}
|
|
28
|
+
return set;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The CLI's package folder: the nearest folder above this module that holds a package.json
|
|
32
|
+
* (works from dist/ and from the test build).
|
|
33
|
+
*
|
|
34
|
+
* @returns {string} Its path.
|
|
35
|
+
* @throws {Error} When no package.json is found.
|
|
36
|
+
*/
|
|
37
|
+
export function packageRoot() {
|
|
38
|
+
let dir = dirname(fileURLToPath(import.meta.url));
|
|
39
|
+
while (!existsSync(join(dir, "package.json"))) {
|
|
40
|
+
const parent = dirname(dir);
|
|
41
|
+
if (parent === dir)
|
|
42
|
+
throw new Error("turingcode package root not found");
|
|
43
|
+
dir = parent;
|
|
44
|
+
}
|
|
45
|
+
return dir;
|
|
46
|
+
}
|
|
2
47
|
/**
|
|
3
48
|
* Reads and parses a UTF-8 JSON file (a leading BOM is ignored).
|
|
4
49
|
*
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A JSON shape. Object keys ending in `?` are optional; all other keys are
|
|
3
|
+
* required, and keys not listed are rejected.
|
|
4
|
+
*/
|
|
5
|
+
export type Shape = "string" | "bool" | "any" | "string_or_list" | {
|
|
6
|
+
enum: readonly string[];
|
|
7
|
+
} | {
|
|
8
|
+
list: Shape;
|
|
9
|
+
min?: number;
|
|
10
|
+
} | {
|
|
11
|
+
obj: Record<string, Shape>;
|
|
12
|
+
} | {
|
|
13
|
+
map: Shape;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* One mismatch: `type` (wrong JSON type), `value` (right type, value not
|
|
17
|
+
* allowed), `missing` (required key absent) or `unknown` (key not in the shape).
|
|
18
|
+
*/
|
|
19
|
+
export interface ShapeIssue {
|
|
20
|
+
kind: "type" | "value" | "missing" | "unknown";
|
|
21
|
+
/** JSON path of the value, e.g. `$.tables[0].name`. */
|
|
22
|
+
path: string;
|
|
23
|
+
message: string;
|
|
24
|
+
found: string;
|
|
25
|
+
expected: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Short, single-line JSON rendering of any value.
|
|
29
|
+
*
|
|
30
|
+
* @param {unknown} v - Value.
|
|
31
|
+
* @returns {string} At most 80 characters.
|
|
32
|
+
*/
|
|
33
|
+
export declare function show(v: unknown): string;
|
|
34
|
+
/**
|
|
35
|
+
* Checks a JSON value against a shape.
|
|
36
|
+
*
|
|
37
|
+
* @param {unknown} value - The value.
|
|
38
|
+
* @param {Shape} shape - Its shape.
|
|
39
|
+
* @param {string} path - JSON path for messages, e.g. `$.tables[0]`.
|
|
40
|
+
* @returns {ShapeIssue[]} Every mismatch; empty when the value matches.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* checkShape({ a: 1 }, { obj: { a: "string" } }, "$")[0].message; // "$.a: expected a non-empty string, got number"
|
|
44
|
+
*/
|
|
45
|
+
export declare function checkShape(value: unknown, shape: Shape, path: string): ShapeIssue[];
|
package/dist/core/json-shape.js
CHANGED
|
@@ -10,34 +10,16 @@ export function show(v) {
|
|
|
10
10
|
const s = v === undefined ? "(absent)" : JSON.stringify(v);
|
|
11
11
|
return s.length > 80 ? s.slice(0, 77) + "..." : s;
|
|
12
12
|
}
|
|
13
|
-
/**
|
|
14
|
-
* Compiles a regex given as data.
|
|
15
|
-
*
|
|
16
|
-
* @param {string} pattern - The pattern.
|
|
17
|
-
* @returns {RegExp | string} The regex, or the error message.
|
|
18
|
-
*/
|
|
19
|
-
export function compileRegex(pattern) {
|
|
20
|
-
try {
|
|
21
|
-
return new RegExp(pattern);
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
return e.message;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
13
|
/** @param {Shape} shape - A shape. @returns {string} A short description for `expected`. */
|
|
28
14
|
function describe(shape) {
|
|
29
15
|
if (typeof shape === "string")
|
|
30
16
|
return shape.replace(/_/g, " ");
|
|
31
17
|
if ("enum" in shape)
|
|
32
18
|
return `one of ${shape.enum.join(", ")}`;
|
|
33
|
-
if ("literal" in shape)
|
|
34
|
-
return JSON.stringify(shape.literal);
|
|
35
19
|
if ("list" in shape)
|
|
36
20
|
return `array of ${describe(shape.list)}`;
|
|
37
21
|
if ("map" in shape)
|
|
38
22
|
return `object of ${describe(shape.map)}`;
|
|
39
|
-
if ("nullable" in shape)
|
|
40
|
-
return `${describe(shape.nullable)} or null`;
|
|
41
23
|
return "object";
|
|
42
24
|
}
|
|
43
25
|
/**
|
|
@@ -66,23 +48,6 @@ export function checkShape(value, shape, path) {
|
|
|
66
48
|
if (typeof value !== "boolean")
|
|
67
49
|
fail("type", `expected a boolean, got ${typeOf(value)}`);
|
|
68
50
|
break;
|
|
69
|
-
case "int":
|
|
70
|
-
case "pos_int":
|
|
71
|
-
if (!Number.isInteger(value) || value < (shape === "int" ? 0 : 1)) {
|
|
72
|
-
fail("type", `expected a ${shape === "int" ? "non-negative" : "positive"} integer`);
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
case "regex": {
|
|
76
|
-
const compiled = typeof value === "string" ? compileRegex(value) : "not a string";
|
|
77
|
-
if (typeof compiled === "string")
|
|
78
|
-
fail("regex", `invalid regex (${compiled})`);
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
case "number_or_string":
|
|
82
|
-
if (!(typeof value === "number" && Number.isFinite(value)) && (typeof value !== "string" || value === "")) {
|
|
83
|
-
fail("type", `expected a number or a string, got ${typeOf(value)}`);
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
51
|
case "string_or_list":
|
|
87
52
|
if (typeof value !== "string" && !(Array.isArray(value) && value.every(v => typeof v === "string"))) {
|
|
88
53
|
fail("type", "expected a string or an array of strings");
|
|
@@ -94,14 +59,6 @@ export function checkShape(value, shape, path) {
|
|
|
94
59
|
if (typeof value !== "string" || !shape.enum.includes(value))
|
|
95
60
|
fail(typeof value === "string" ? "value" : "type", "value not allowed");
|
|
96
61
|
}
|
|
97
|
-
else if ("literal" in shape) {
|
|
98
|
-
if (value !== shape.literal)
|
|
99
|
-
fail("value", "value not allowed");
|
|
100
|
-
}
|
|
101
|
-
else if ("nullable" in shape) {
|
|
102
|
-
if (value !== null)
|
|
103
|
-
issues.push(...checkShape(value, shape.nullable, path));
|
|
104
|
-
}
|
|
105
62
|
else if ("list" in shape) {
|
|
106
63
|
if (!Array.isArray(value)) {
|
|
107
64
|
fail("type", `expected an array, got ${typeOf(value)}`);
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/** Default public MCP gateway (public-mcp-tools in the turingcode repo). */
|
|
2
|
+
export declare const DEFAULT_URL = "https://mcp.turingcode.ai/mcp";
|
|
3
|
+
/** Where to put the gateway token; shown in the help of server commands and when the token is missing. */
|
|
4
|
+
export declare const TOKEN_HELP: string;
|
|
5
|
+
export interface ToolResult {
|
|
6
|
+
text: string;
|
|
7
|
+
isError: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface ToolInfo {
|
|
10
|
+
name: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
inputSchema?: unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Calls one tool on the Turing Code MCP gateway.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} name - Tool name, e.g. `stm32_cubetools_clock_solve`.
|
|
18
|
+
* @param {Record<string, unknown>} args - Tool arguments.
|
|
19
|
+
* @param {string} token - Bearer token for the gateway.
|
|
20
|
+
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
21
|
+
* @param {number} [timeoutMs=125000] - Time before the request fails.
|
|
22
|
+
* @returns {Promise<ToolResult>} The tool's text content and error flag.
|
|
23
|
+
* @throws {Error} On HTTP errors, a timeout or a JSON-RPC error reply.
|
|
24
|
+
*/
|
|
25
|
+
export declare function callTool(name: string, args: Record<string, unknown>, token: string, url?: string, timeoutMs?: number): Promise<ToolResult>;
|
|
26
|
+
/**
|
|
27
|
+
* Calls one tool on the gateway and returns its text, throwing when the tool reports an error.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} name - Tool name.
|
|
30
|
+
* @param {object} args - Tool arguments.
|
|
31
|
+
* @param {string} token - Bearer token for the gateway.
|
|
32
|
+
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
33
|
+
* @param {number} [timeoutMs] - Time before the request fails.
|
|
34
|
+
* @returns {Promise<string>} The tool's text content.
|
|
35
|
+
* @throws {Error} With the tool's text when it reports an error, or on a failed request.
|
|
36
|
+
*/
|
|
37
|
+
export declare function callToolText(name: string, args: object, token: string, url?: string, timeoutMs?: number): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* A tool reply without the server's trailing footer lines (`Credits: ...`), for replies that are parsed
|
|
40
|
+
* (a link, JSON) rather than shown.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} text - Tool reply.
|
|
43
|
+
* @returns {string} The reply body, trimmed.
|
|
44
|
+
*/
|
|
45
|
+
export declare function replyBody(text: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Lists the tools the gateway token can reach (the gateway applies its allow/deny policy).
|
|
48
|
+
*
|
|
49
|
+
* @param {string} token - Bearer token for the gateway.
|
|
50
|
+
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
51
|
+
* @returns {Promise<ToolInfo[]>} Name, description and input schema of each tool.
|
|
52
|
+
* @throws {Error} On HTTP errors or a JSON-RPC error reply.
|
|
53
|
+
*/
|
|
54
|
+
export declare function listTools(token: string, url?: string): Promise<ToolInfo[]>;
|
|
55
|
+
/**
|
|
56
|
+
* Extracts the JSON-RPC `result` from a gateway reply (plain JSON or an SSE stream).
|
|
57
|
+
*
|
|
58
|
+
* @param {string} body - Raw response body.
|
|
59
|
+
* @returns {any} The reply's `result`.
|
|
60
|
+
* @throws {Error} When the reply holds no JSON-RPC response or carries an error.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* parseRpcReply('data: {"jsonrpc":"2.0","id":1,"result":{"tools":[]}}'); // { tools: [] }
|
|
64
|
+
*/
|
|
65
|
+
export declare function parseRpcReply(body: string): any;
|
|
66
|
+
/**
|
|
67
|
+
* Joins a `tools/call` result's text content.
|
|
68
|
+
*
|
|
69
|
+
* @param {any} result - The JSON-RPC result of `tools/call`.
|
|
70
|
+
* @returns {ToolResult} The joined text content and error flag.
|
|
71
|
+
*/
|
|
72
|
+
export declare function toToolResult(result: any): ToolResult;
|
|
73
|
+
/**
|
|
74
|
+
* Reads the gateway token and endpoint from TURINGCODE_TOKEN and TURINGCODE_URL.
|
|
75
|
+
*
|
|
76
|
+
* @returns {{token: string, url: string}} The token and gateway URL.
|
|
77
|
+
* @throws {Error} When TURINGCODE_TOKEN is not set.
|
|
78
|
+
*/
|
|
79
|
+
export declare function getGatewayConfig(): {
|
|
80
|
+
token: string;
|
|
81
|
+
url: string;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Runs a server command: calls the tool with the gateway token from
|
|
85
|
+
* TURINGCODE_TOKEN and prints its text; a tool error sets exit code 1.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} name - Tool name.
|
|
88
|
+
* @param {Record<string, unknown>} args - Tool arguments; undefined entries are dropped.
|
|
89
|
+
* @returns {Promise<void>} Resolves once the reply is printed.
|
|
90
|
+
* @throws {Error} When TURINGCODE_TOKEN is not set, or the gateway call fails.
|
|
91
|
+
*/
|
|
92
|
+
export declare function runServerTool(name: string, args: Record<string, unknown>): Promise<void>;
|