clicodemode 0.6.0 → 0.6.1

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 CHANGED
@@ -1,17 +1,42 @@
1
- # clicodemode
1
+ # clicodemode (experimental)
2
2
 
3
3
  Code Mode for JavaScript and TypeScript CLIs. Register the commands your CLI
4
4
  already has as operations; an agent then writes one small JavaScript-like
5
5
  program that calls and composes them, instead of many separate tool calls.
6
- The engine is the Go interpreter from this repo compiled to WebAssembly.
7
- Zero runtime dependencies, Node >= 20, ESM only.
6
+
7
+ This package is a **thin wrapper around the Go engine compiled to WebAssembly**.
8
+ It is not a native TypeScript interpreter. The Go engine owns parsing,
9
+ language semantics and validation (diagnostics with file:line:col); this
10
+ package only loads it, passes your handlers in and returns its results.
11
+ Status: **experimental**, version 0.6.1. The Go project is the reference.
12
+
13
+ ## Install (no npm registry needed)
14
+
15
+ The package is not published to the npm registry. Pick one:
16
+
17
+ **a) From a GitHub release asset** (the URL is valid once the v0.6.1 release
18
+ exists; the release workflow attaches the tarball produced by `npm pack`
19
+ after the WASM is built):
8
20
 
9
21
  ```sh
10
- npm install clicodemode
22
+ npm install https://github.com/muthuishere/clicodemode/releases/download/v0.6.1/clicodemode-0.6.1.tgz
11
23
  ```
12
24
 
13
- ## TypeScript
25
+ **b) From source** (needs Go and Node >= 20):
26
+
27
+ ```sh
28
+ git clone https://github.com/muthuishere/clicodemode && cd clicodemode
29
+ bash scripts/build-wasm.sh
30
+ cd npm && npm ci && npm run build
31
+ npm pack # -> clicodemode-0.6.1.tgz, install it anywhere with npm install <path>
32
+ ```
33
+
34
+ `npm run verify-pack` packs the tarball, installs it into a fresh directory
35
+ and runs the two quickstarts below (JS executed, TS type-checked and executed).
36
+
37
+ ## Quickstart (TypeScript)
14
38
 
39
+ <!-- quickstart-ts -->
15
40
  ```ts
16
41
  import { createCodeMode } from "clicodemode";
17
42
 
@@ -25,41 +50,99 @@ cm.register({
25
50
  handler: async ({ verbose }) => ({ ok: true, verbose: !!verbose }),
26
51
  });
27
52
 
28
- const caps = await cm.capabilities(); // cache by caps.fingerprint
29
- const bad = await cm.check("class A {}"); // bad.diagnostics[0] has file:line:col
53
+ const caps = await cm.capabilities(); // cache by caps.fingerprint
54
+ const bad = await cm.check("class A {}"); // diagnostics carry file:line:col
55
+ console.log("fingerprint:", caps.fingerprint.length > 0, "check ok:", bad.ok);
56
+
30
57
  const r = await cm.run(
31
58
  "const h = await ops.health.check({verbose: input.v}); h.ok",
32
- { input: { v: true } },
59
+ { input: { v: true }, timeoutMs: 5000 },
33
60
  );
34
- console.log(r.ok, r.value);
61
+ console.log("run:", r.ok, r.value);
35
62
  ```
36
63
 
37
- ## JavaScript
64
+ ## Quickstart (JavaScript, ESM)
38
65
 
66
+ <!-- quickstart-js -->
39
67
  ```js
40
68
  import { createCodeMode } from "clicodemode";
69
+
41
70
  const cm = createCodeMode({ version: "1.0.0" });
42
- cm.register({ name: "demo.add", description: "add", handler: async ({ a, b }) => a + b });
43
- console.log((await cm.run("await ops.demo.add({a: 1, b: 2})")).value); // 3
44
- ```
71
+ cm.register({
72
+ name: "health.check",
73
+ description: "Report service health",
74
+ sideEffects: "none",
75
+ idempotent: true,
76
+ inputSchema: { type: "object", properties: { verbose: { type: "boolean" } } },
77
+ handler: async ({ verbose }) => ({ ok: true, verbose: !!verbose }),
78
+ });
79
+
80
+ const caps = await cm.capabilities(); // cache by caps.fingerprint
81
+ const bad = await cm.check("class A {}"); // diagnostics carry file:line:col
82
+ console.log("fingerprint:", caps.fingerprint.length > 0, "check ok:", bad.ok);
45
83
 
46
- Handlers that throw reject the program's call; the program can `try/catch` it.
47
- Operations may be registered before the WASM loads; it loads lazily on the
48
- first call (one WASM module per process, one engine per `createCodeMode`).
49
- Registering after the first call rebuilds the engine with the full op set.
84
+ const r = await cm.run(
85
+ "const h = await ops.health.check({verbose: input.v}); h.ok",
86
+ { input: { v: true }, timeoutMs: 5000 },
87
+ );
88
+ console.log("run:", r.ok, r.value);
89
+ ```
50
90
 
51
91
  ## API
52
92
 
53
93
  `register`, `operations`, `describe`, `capabilities`, `docs`, `skill`,
54
- `check`, `compile`, `run(src, {file, input})`, `eval(expr, {input})`.
55
- `run`/`eval` return `{ok, value, error, diagnostics, ms}`.
94
+ `check`, `compile`, `run(src, {file, input, timeoutMs, signal})`,
95
+ `eval(expr, {input, timeoutMs, signal})`. `run`/`eval` return
96
+ `{ok, value, error, diagnostics, ms, stdout}`.
97
+
98
+ - Handlers that throw or reject make the program's call reject; the program can `try/catch` it.
99
+ - Object results from handlers keep key order as the program sees it.
100
+ - Operations may be registered before the WASM loads; it loads lazily on the
101
+ first call. Registering after the first call rebuilds the engine with the full op set.
102
+
103
+ ### Timeouts and cancellation
104
+
105
+ `timeoutMs` defaults to **30000** ms per call (`0` = no per-call timeout).
106
+ `signal` is an `AbortSignal`. On timeout or abort the call resolves with
107
+ `ok: false` and any host handler promise still pending is abandoned (a
108
+ handler that never resolves cannot hang the call or keep the process alive).
109
+ The engine stays usable afterwards, and other concurrent calls are unaffected.
110
+
111
+ Limit: WASM has no preemption. Timeout and abort take effect when the program
112
+ is waiting on a host operation (or between calls into one); they cannot
113
+ interrupt a pure CPU loop that never awaits. CPU-bound loops are stopped by
114
+ `limits.max_steps` (engine default 10,000,000 steps, an error, not a hang).
115
+ Set `limits.max_steps` for untrusted programs. `limits.timeout_ms` is also
116
+ available but, being a Go-side timer, can keep the Node process alive until it
117
+ expires.
118
+
119
+ ## Security model
120
+
121
+ - The engine owns language validation: unsupported syntax, unknown operations
122
+ and bad `exec` argv are rejected before anything runs.
123
+ - **The host (your handlers) owns authorization, path and exec security.**
124
+ A program can call exactly the operations you registered, with arguments it
125
+ chooses; validate arguments and enforce permissions inside each handler.
126
+ - `exec` / `shell` are **denied** in this build: the program gets a diagnostic
127
+ such as `running "echo" is not permitted: this host allows no external
128
+ programs`. Run commands, if you need them, from your own handlers.
129
+
130
+ ## Size, startup, and reuse
131
+
132
+ - `dist/codemode.wasm` is about 7.8 MB raw, 2.1 MB gzipped (the npm tarball is
133
+ compressed). Node memory after loading the engine is roughly 120 MB RSS.
134
+ - Cold start (load WASM and run one program) measured about 55 ms on an Apple
135
+ Silicon laptop with Node 24. Treat this as indicative only.
136
+ - **Reuse one engine (`createCodeMode`) per process.** Each engine instance is
137
+ cheap, but program runs are the unit of work; do not create one per call.
138
+ The WASM module itself is loaded once per process.
56
139
 
57
140
  ## Not supported
58
141
 
59
142
  The program language is a JavaScript subset: no `class`, `new`, `this`,
60
- generators, `switch`, regex, dynamic `import()`. `exec` and `shell` do not run
61
- inside WASM. See [docs/SPEC.md](../docs/SPEC.md) for the full language and
62
- semantics.
143
+ generators, `switch`, regex, dynamic `import()`. See
144
+ [docs/SPEC.md](../docs/SPEC.md) for the full language and semantics. The
145
+ `policy` option of the Go API is not exposed here.
63
146
 
64
147
  ## Other runtimes
65
148
 
Binary file
package/dist/index.d.ts CHANGED
@@ -1,6 +1,14 @@
1
1
  import type { Capabilities, CheckResult, CodeModeOptions, Operation, OperationSpec, Result } from "./types.js";
2
2
  export * from "./types.js";
3
3
  export { loadWasm, type Bridge } from "./wasm.js";
4
+ /** Structural subset of the standard AbortSignal (no DOM or @types/node needed). */
5
+ export interface AbortSignalLike {
6
+ readonly aborted: boolean;
7
+ addEventListener(type: "abort", fn: () => void, opts?: {
8
+ once?: boolean;
9
+ }): void;
10
+ removeEventListener(type: "abort", fn: () => void): void;
11
+ }
4
12
  /** Default wall-clock bound for one run/eval call, in milliseconds. */
5
13
  export declare const DEFAULT_TIMEOUT_MS = 30000;
6
14
  export interface CallOptions {
@@ -15,7 +23,7 @@ export interface CallOptions {
15
23
  */
16
24
  timeoutMs?: number;
17
25
  /** Aborting resolves the call with ok:false and abandons pending host handlers. Like timeoutMs it takes effect when the program awaits a host operation, not inside a pure CPU loop. */
18
- signal?: AbortSignal;
26
+ signal?: AbortSignalLike;
19
27
  }
20
28
  export interface RunOptions extends CallOptions {
21
29
  file?: string;
package/dist/index.js CHANGED
@@ -75,10 +75,19 @@ export function createCodeMode(options) {
75
75
  throw new Error("clicodemode: timeoutMs must be >= 0");
76
76
  const onAbort = () => { b.cancel(callId); };
77
77
  signal?.addEventListener("abort", onAbort, { once: true });
78
+ // The timer lives on the JS side: a Go-side timer would keep the Node
79
+ // process alive until it expires even after the call finished.
80
+ let timedOut = false;
81
+ const timer = t > 0 ? setTimeout(() => { timedOut = true; b.cancel(callId); }, t) : undefined;
78
82
  try {
79
- return parse(await start(b, id, callId, t > 0 ? String(Math.ceil(t)) : ""));
83
+ const r = parse(await start(b, id, callId));
84
+ if (timedOut && !r.ok)
85
+ r.error = `timed out after ${t}ms (${r.error ?? "cancelled"})`;
86
+ return r;
80
87
  }
81
88
  finally {
89
+ if (timer !== undefined)
90
+ clearTimeout(timer);
82
91
  signal?.removeEventListener("abort", onAbort);
83
92
  }
84
93
  }
@@ -101,10 +110,10 @@ export function createCodeMode(options) {
101
110
  async check(src, file = "<program>") { const { b, id } = await ready(); return withOk(parse(b.check(id, src, file))); },
102
111
  async compile(src, file = "<program>") { const { b, id } = await ready(); return withOk(parse(b.compile(id, src, file))); },
103
112
  run(src, opts = {}) {
104
- return call(opts, (b, id, callId, t) => b.run(id, src, opts.file ?? "<program>", JSON.stringify(opts.input ?? null), callId, t));
113
+ return call(opts, (b, id, callId) => b.run(id, src, opts.file ?? "<program>", JSON.stringify(opts.input ?? null), callId));
105
114
  },
106
115
  eval(expr, opts = {}) {
107
- return call(opts, (b, id, callId, t) => b.eval(id, expr, JSON.stringify(opts.input ?? null), callId, t));
116
+ return call(opts, (b, id, callId) => b.eval(id, expr, JSON.stringify(opts.input ?? null), callId));
108
117
  },
109
118
  };
110
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicodemode",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Code Mode for JS and TS CLIs: register your commands as operations, agents run a small JavaScript-like program that calls them. Engine is Go compiled to WASM.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,5 +32,14 @@
32
32
  "devDependencies": {
33
33
  "typescript": "^5.6.0"
34
34
  },
35
- "license": "MIT"
35
+ "license": "MIT",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/muthuishere/clicodemode.git",
39
+ "directory": "npm"
40
+ },
41
+ "homepage": "https://muthuishere.github.io/clicodemode/",
42
+ "bugs": {
43
+ "url": "https://github.com/muthuishere/clicodemode/issues"
44
+ }
36
45
  }