clicodemode 0.6.2 → 0.7.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 +158 -120
- package/dist/codemode.wasm +0 -0
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,151 +1,189 @@
|
|
|
1
|
-
# clicodemode
|
|
1
|
+
# clicodemode
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
10
|
+
```sh
|
|
11
|
+
npm install clicodemode
|
|
12
|
+
```
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
Docs: https://muthuishere.github.io/clicodemode/
|
|
16
15
|
|
|
17
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
```
|
|
18
|
+
1. You **register** the functions behind your commands as operations.
|
|
19
|
+
2. An agent (or you) sends a program:
|
|
24
20
|
|
|
25
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
-
and runs the two quickstarts below (JS executed, TS type-checked and executed).
|
|
33
|
+
## Quickstart
|
|
36
34
|
|
|
37
|
-
|
|
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-
|
|
40
|
-
```
|
|
38
|
+
<!-- quickstart-js -->
|
|
39
|
+
```js
|
|
41
40
|
import { createCodeMode } from "clicodemode";
|
|
42
41
|
|
|
43
|
-
|
|
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: "
|
|
46
|
-
description: "
|
|
48
|
+
name: "repo.status",
|
|
49
|
+
description: "Show repo status.",
|
|
47
50
|
sideEffects: "none",
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const r = await cm.run(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
86
|
+
A wrong program never reaches your handlers:
|
|
65
87
|
|
|
66
|
-
<!-- quickstart-js -->
|
|
67
88
|
```js
|
|
68
|
-
|
|
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: "
|
|
73
|
-
description: "
|
|
107
|
+
name: "repo.status",
|
|
108
|
+
description: "Show repo status.",
|
|
74
109
|
sideEffects: "none",
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
|
81
|
-
const
|
|
82
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
`
|
|
96
|
-
`{ok,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
The
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
## Security
|
|
120
|
-
|
|
121
|
-
-
|
|
122
|
-
and
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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.1). 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.
|
package/dist/codemode.wasm
CHANGED
|
Binary file
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ export { loadWasm } from "./wasm.js";
|
|
|
4
4
|
/** Default wall-clock bound for one run/eval call, in milliseconds. */
|
|
5
5
|
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
6
6
|
let callSeq = 0;
|
|
7
|
-
const NAME_RE = /^[
|
|
7
|
+
const NAME_RE = /^[A-Za-z][A-Za-z0-9_-]*(\.[A-Za-z][A-Za-z0-9_-]*)*$/;
|
|
8
8
|
function parse(s) {
|
|
9
9
|
const v = JSON.parse(s);
|
|
10
10
|
if (v && typeof v === "object" && !Array.isArray(v) && typeof v.error === "string" && Object.keys(v).length === 1) {
|
|
@@ -94,7 +94,7 @@ export function createCodeMode(options) {
|
|
|
94
94
|
return {
|
|
95
95
|
register(op) {
|
|
96
96
|
if (!NAME_RE.test(op.name))
|
|
97
|
-
throw new Error(`clicodemode: invalid operation name "${op.name}" (want dotted
|
|
97
|
+
throw new Error(`clicodemode: invalid operation name "${op.name}" (want dotted segments of letters, digits, _ and -, e.g. health.check)`);
|
|
98
98
|
if (typeof op.handler !== "function")
|
|
99
99
|
throw new Error(`clicodemode: operation "${op.name}" has no handler`);
|
|
100
100
|
if (specs.has(op.name))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clicodemode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.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",
|