clicodemode 0.6.2 → 0.7.0

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,151 +1,189 @@
1
- # clicodemode (experimental)
1
+ # clicodemode
2
2
 
3
- Code Mode for JavaScript and TypeScript CLIs. Register the commands your CLI
4
- already has as operations; an agent then writes one small JavaScript-like
5
- program that calls and composes them, instead of many separate tool calls.
3
+ **Your agent has Code Mode. Does your CLI?**
6
4
 
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.2. The Go project is the reference.
5
+ Give your Node CLI a `code` command. An agent sends it one small JavaScript
6
+ program; the program calls your CLI's own commands inside one process and
7
+ returns one JSON value. No extra Node process per call, no `child_process`,
8
+ no raw output flooding the model.
12
9
 
13
- ## Install (no npm registry needed)
10
+ ```sh
11
+ npm install clicodemode
12
+ ```
14
13
 
15
- The package is not published to the npm registry. Pick one:
14
+ Docs: https://muthuishere.github.io/clicodemode/
16
15
 
17
- **a) From a GitHub release asset** (the URL is valid once the v0.6.2 release
18
- exists; the release workflow attaches the tarball produced by `npm pack`
19
- after the WASM is built):
16
+ ## How it works
20
17
 
21
- ```sh
22
- npm install https://github.com/muthuishere/clicodemode/releases/download/v0.6.2/clicodemode-0.6.2.tgz
23
- ```
18
+ 1. You **register** the functions behind your commands as operations.
19
+ 2. An agent (or you) sends a program:
24
20
 
25
- **b) From source** (needs Go and Node >= 20):
21
+ ```js
22
+ const [status, issues] = await Promise.all([
23
+ ops.repo.status({ path: input.path }),
24
+ ops.issues.list({ state: "open" }),
25
+ ]);
26
+ ({ branch: status.branch, open: issues.length })
27
+ ```
26
28
 
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.2.tgz, install it anywhere with npm install <path>
32
- ```
29
+ 3. clicodemode checks it (unknown operations and bad arguments are reported
30
+ with `file:line:col` and nothing runs), runs it against your handlers, and
31
+ returns `{ ok, value }`.
33
32
 
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).
33
+ ## Quickstart
36
34
 
37
- ## Quickstart (TypeScript)
35
+ Save as `mycli.mjs`. `node mycli.mjs` runs a demo program; `echo '<program>' |
36
+ node mycli.mjs code run -` runs whatever an agent sends.
38
37
 
39
- <!-- quickstart-ts -->
40
- ```ts
38
+ <!-- quickstart-js -->
39
+ ```js
41
40
  import { createCodeMode } from "clicodemode";
42
41
 
43
- const cm = createCodeMode({ version: "1.0.0" });
42
+ // The functions your CLI already has.
43
+ async function repoStatus({ path }) { return { path, branch: "main", dirty: false }; }
44
+ async function listIssues({ state }) { return [{ id: 1, state }, { id: 2, state }]; }
45
+
46
+ const cm = createCodeMode({ version: "mycli 1.0.0" });
44
47
  cm.register({
45
- name: "health.check",
46
- description: "Report service health",
48
+ name: "repo.status",
49
+ description: "Show repo status.",
47
50
  sideEffects: "none",
48
- idempotent: true,
49
- inputSchema: { type: "object", properties: { verbose: { type: "boolean" } } },
50
- handler: async ({ verbose }) => ({ ok: true, verbose: !!verbose }),
51
+ inputSchema: { type: "object", required: ["path"], properties: { path: { type: "string" } } },
52
+ handler: repoStatus,
53
+ });
54
+ cm.register({
55
+ name: "issues.list",
56
+ description: "List issues by state.",
57
+ sideEffects: "none",
58
+ inputSchema: { type: "object", properties: { state: { enum: ["open", "closed"] } } },
59
+ handler: listIssues,
51
60
  });
52
61
 
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
-
57
- const r = await cm.run(
58
- "const h = await ops.health.check({verbose: input.v}); h.ok",
59
- { input: { v: true }, timeoutMs: 5000 },
60
- );
61
- console.log("run:", r.ok, r.value);
62
+ // `mycli code run -` : read a program from stdin, print one JSON result.
63
+ async function codeRun(src, input) {
64
+ const checked = await cm.check(src);
65
+ if (!checked.ok) return { ok: false, diagnostics: checked.diagnostics };
66
+ const r = await cm.run(src, { input, timeoutMs: 10_000 });
67
+ return { ok: r.ok, value: r.value, error: r.error };
68
+ }
69
+
70
+ const demo = `
71
+ const [status, issues] = await Promise.all([
72
+ ops.repo.status({ path: input.path }),
73
+ ops.issues.list({ state: "open" }),
74
+ ]);
75
+ ({ branch: status.branch, open: issues.length })
76
+ `;
77
+
78
+ const [cmd, sub] = process.argv.slice(2);
79
+ const src = cmd === "code" && sub === "run"
80
+ ? await new Promise((res) => { let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => res(s)); })
81
+ : demo;
82
+ console.log(JSON.stringify(await codeRun(src, { path: "." })));
83
+ // {"ok":true,"value":{"branch":"main","open":2}}
62
84
  ```
63
85
 
64
- ## Quickstart (JavaScript, ESM)
86
+ A wrong program never reaches your handlers:
65
87
 
66
- <!-- quickstart-js -->
67
88
  ```js
68
- import { createCodeMode } from "clicodemode";
89
+ await cm.check('ops.issues.list({ state: "all" })');
90
+ // { ok: false, diagnostics: [{ severity: "error", line: 1, col: 17,
91
+ // message: 'issues.list: state must be one of ["open","closed"]',
92
+ // hint: "see `code describe issues.list`" }] }
93
+ ```
94
+
95
+ ## TypeScript
96
+
97
+ Types ship with the package. The same CLI, typed:
98
+
99
+ <!-- quickstart-ts -->
100
+ ```ts
101
+ import { createCodeMode, type Result } from "clicodemode";
102
+
103
+ type Issue = { id: number; state: string };
69
104
 
70
- const cm = createCodeMode({ version: "1.0.0" });
105
+ const cm = createCodeMode({ version: "mycli 1.0.0" });
71
106
  cm.register({
72
- name: "health.check",
73
- description: "Report service health",
107
+ name: "repo.status",
108
+ description: "Show repo status.",
74
109
  sideEffects: "none",
75
- idempotent: true,
76
- inputSchema: { type: "object", properties: { verbose: { type: "boolean" } } },
77
- handler: async ({ verbose }) => ({ ok: true, verbose: !!verbose }),
110
+ inputSchema: { type: "object", required: ["path"], properties: { path: { type: "string" } } },
111
+ handler: async ({ path }) => ({ path, branch: "main", dirty: false }),
112
+ });
113
+ cm.register({
114
+ name: "issues.list",
115
+ description: "List issues by state.",
116
+ sideEffects: "none",
117
+ inputSchema: { type: "object", properties: { state: { enum: ["open", "closed"] } } },
118
+ handler: async ({ state }): Promise<Issue[]> => [{ id: 1, state }, { id: 2, state }],
78
119
  });
79
120
 
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);
121
+ const r: Result = await cm.run(`
122
+ const [status, issues] = await Promise.all([
123
+ ops.repo.status({ path: input.path }),
124
+ ops.issues.list({ state: "open" }),
125
+ ]);
126
+ ({ branch: status.branch, open: issues.length })
127
+ `, { input: { path: "." }, timeoutMs: 10_000 });
128
+ console.log(JSON.stringify({ ok: r.ok, value: r.value, error: r.error }));
129
+ ```
83
130
 
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);
131
+ ## Tell the agent what it can call
132
+
133
+ ```js
134
+ const caps = await cm.capabilities(); // every operation + a fingerprint to cache on
135
+ const skill = await cm.skill(); // a SKILL.md describing your CLI's operations
136
+ const docs = await cm.docs(); // the language reference
89
137
  ```
90
138
 
139
+ Print `skill` from a `mycli code skill` command and agents learn your CLI in
140
+ one read.
141
+
91
142
  ## API
92
143
 
93
- `register`, `operations`, `describe`, `capabilities`, `docs`, `skill`,
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.
139
-
140
- ## Not supported
141
-
142
- The program language is a JavaScript subset: no `class`, `new`, `this`,
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.
146
-
147
- ## Other runtimes
148
-
149
- The Node loader lives in `src/wasm.ts`; `loadWasm(bytes)` boots the engine from
150
- bytes once `wasm_exec.js` has defined `globalThis.Go`. A browser or bundler
151
- entry can fetch the wasm and call it; not shipped yet.
144
+ | method | returns |
145
+ |---|---|
146
+ | `register(op)` | adds an operation: `name` (dotted, lower case), `description`, `handler`, optional `inputSchema`, `sideEffects` (`none` / `local` / `external`), `idempotent` |
147
+ | `check(src)` / `compile(src)` | `{ ok, diagnostics }`, nothing runs |
148
+ | `run(src, { input, timeoutMs, signal, file })` | `{ ok, value, error, diagnostics, ms, stdout }` |
149
+ | `eval(expr, opts)` | same as `run`, for one expression |
150
+ | `operations()` / `describe(name)` / `capabilities()` | the registered contract |
151
+ | `docs()` / `skill()` | text for humans and agents |
152
+
153
+ - A handler that throws rejects the program's call; the program can
154
+ `try/catch` it.
155
+ - `timeoutMs` defaults to 30,000 (0 = none); `signal` is an `AbortSignal`.
156
+ A pure CPU loop that never awaits is stopped by the engine's step limit
157
+ (10M steps), not by the timeout.
158
+ - Create one engine per process and reuse it.
159
+
160
+ ## The language
161
+
162
+ A JavaScript subset: `const`/`let`, functions and arrows, destructuring,
163
+ spread, template literals, `async`/`await`, `Promise.all`, `try`/`catch`,
164
+ loops, `switch`, classes with `new`/`this`/`extends`, regexes (RE2, so no
165
+ catastrophic backtracking), tagged templates, `Map`/`Set`/`Date`, `pmap` for
166
+ bounded parallel maps. Not supported (a check error, never silent):
167
+ generators, `delete`, getters/setters, regex lookaround and backreferences,
168
+ `eval`, dynamic `import()`. Full spec: [SPEC.md](https://github.com/muthuishere/clicodemode/blob/main/docs/SPEC.md).
169
+
170
+ ## Security
171
+
172
+ - A program can call only the operations you registered. Validate arguments
173
+ and check permissions inside each handler: **your handlers own
174
+ authorization and file/process security.**
175
+ - `exec` and `shell` are denied in this package.
176
+ - It is not a sandbox for hostile code, and WebAssembly does not make it one.
177
+
178
+ ## About this package
179
+
180
+ The engine is the Go library [clicodemode](https://github.com/muthuishere/clicodemode)
181
+ compiled to WebAssembly; this package loads it and passes your handlers in.
182
+ Go is the primary implementation; the Node package is **experimental** and
183
+ follows the same version (0.7.0). The `.wasm` is about 2.1 MB gzipped; a cold
184
+ start (load plus one run) takes about 55 ms on Apple Silicon with Node 24.
185
+ Node 20 or newer.
186
+
187
+ Also for [Go](https://pkg.go.dev/github.com/muthuishere/clicodemode),
188
+ [Python](https://pypi.org/project/clicodemode/) and
189
+ [Rust](https://crates.io/crates/clicodemode). MIT licensed.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicodemode",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
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",