@cmdop/sdk 1.1.0 → 3.0.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/LICENSE +21 -0
- package/README.md +14 -149
- package/dist/carriers/carrier.d.ts +40 -0
- package/dist/carriers/carrier.js +16 -0
- package/dist/carriers/http.d.ts +15 -0
- package/dist/carriers/http.js +114 -0
- package/dist/carriers/memory.d.ts +11 -0
- package/dist/carriers/memory.js +35 -0
- package/dist/carriers/ndjson.d.ts +16 -0
- package/dist/carriers/ndjson.js +31 -0
- package/dist/client.d.ts +113 -0
- package/dist/client.js +220 -0
- package/dist/generated/call.gen.d.ts +78 -0
- package/dist/generated/call.gen.js +5 -0
- package/dist/generated/tools.gen.d.ts +1046 -0
- package/dist/generated/tools.gen.js +34 -0
- package/dist/index.d.ts +8 -4245
- package/dist/index.js +8 -1113
- package/dist/screen.d.ts +19 -0
- package/dist/screen.js +16 -0
- package/dist/vendor.d.ts +52 -0
- package/dist/vendor.js +291 -0
- package/package.json +11 -42
- package/bin/cmdop-core-darwin-arm64 +0 -0
- package/bin/cmdop-core-darwin-x64 +0 -0
- package/bin/cmdop-core-linux-arm64 +0 -0
- package/bin/cmdop-core-linux-x64 +0 -0
- package/bin/cmdop-core-win32-x64.exe +0 -0
- package/dist/chunk-HVTWZ3OM.js +0 -63
- package/dist/chunk-HVTWZ3OM.js.map +0 -1
- package/dist/errors-4ITNIIVL.js +0 -3
- package/dist/errors-4ITNIIVL.js.map +0 -1
- package/dist/index.cjs +0 -1218
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -4245
- package/dist/index.js.map +0 -1
package/dist/client.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cmdop client (developer SDK plan, section 4): your machines' tools from a program.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* const cmdop = connect(); // CMDOP_TOKEN and CMDOP_URL
|
|
6
|
+
* const box = cmdop.machine("box");
|
|
7
|
+
* const { exit_code, stdout } = await box.run("uptime");
|
|
8
|
+
* const run = box.stream("make test");
|
|
9
|
+
* for await (const piece of run) process.stdout.write(piece.data);
|
|
10
|
+
* const done = await run.result;
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Every tool, its arguments and result, the error codes and the call shapes are generated from the Rust workspace
|
|
14
|
+
* (`generated/`). This file speaks only the call contract; which channel carries it is the carrier's business
|
|
15
|
+
* (`carriers/`), and nothing here knows it.
|
|
16
|
+
*
|
|
17
|
+
* End-to-end calls come through a `Sealer` (`connect({ e2e })`): this file holds no key and reads no sealed text.
|
|
18
|
+
*/
|
|
19
|
+
import { TOOL_NAMES } from "./generated/tools.gen.js";
|
|
20
|
+
import { CarrierError } from "./carriers/carrier.js";
|
|
21
|
+
import { httpCarrier } from "./carriers/http.js";
|
|
22
|
+
import { toScreenshot } from "./screen.js";
|
|
23
|
+
/** Why a call returned no result. Decide by `code`, never by `message`. */
|
|
24
|
+
export class CmdopError extends Error {
|
|
25
|
+
code;
|
|
26
|
+
/**
|
|
27
|
+
* Machine-readable facts that come with some codes. `permission_missing` carries
|
|
28
|
+
* `{ permission, state, requires_human, machine, human_action, retry }`: a person must act on that machine, do not
|
|
29
|
+
* retry until they have.
|
|
30
|
+
*/
|
|
31
|
+
details;
|
|
32
|
+
constructor(code, message, details) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.name = "CmdopError";
|
|
35
|
+
this.code = code;
|
|
36
|
+
if (details !== undefined)
|
|
37
|
+
this.details = details;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const env = (name) => globalThis.process?.env?.[name];
|
|
41
|
+
/** `url` as given, else `https://<contour>.cmdop.dev`; `CMDOP_URL` when neither is given. */
|
|
42
|
+
export function urlOf(opts) {
|
|
43
|
+
if (opts.url && opts.contour)
|
|
44
|
+
throw new Error("connect: give url or contour, not both");
|
|
45
|
+
if (opts.contour) {
|
|
46
|
+
if (!/^[a-z0-9-]+$/.test(opts.contour))
|
|
47
|
+
throw new Error(`connect: ${JSON.stringify(opts.contour)} is not a contour address`);
|
|
48
|
+
return `https://${opts.contour}.cmdop.dev`;
|
|
49
|
+
}
|
|
50
|
+
const url = opts.url ?? env("CMDOP_URL");
|
|
51
|
+
if (!url)
|
|
52
|
+
throw new Error("connect: no address; set CMDOP_URL (your page's address, https://<address>.cmdop.dev) or pass url");
|
|
53
|
+
return url.replace(/\/+$/, "");
|
|
54
|
+
}
|
|
55
|
+
function carrierOf(opts) {
|
|
56
|
+
const want = opts.carrier ?? env("CMDOP_CARRIER") ?? "auto";
|
|
57
|
+
if (typeof want === "object")
|
|
58
|
+
return want;
|
|
59
|
+
const token = opts.token ?? env("CMDOP_TOKEN");
|
|
60
|
+
if (!token)
|
|
61
|
+
throw new Error("connect: no token; set CMDOP_TOKEN or pass token");
|
|
62
|
+
// One door exists today, so `auto` is HTTP. A second carrier adds its probe here, never a branch in the client.
|
|
63
|
+
if (want === "auto" || want === "http")
|
|
64
|
+
return httpCarrier({ url: urlOf(opts), token, ...(opts.fetch ? { fetch: opts.fetch } : {}) });
|
|
65
|
+
throw new Error(`connect: unknown carrier ${JSON.stringify(want)} (auto, http)`);
|
|
66
|
+
}
|
|
67
|
+
function errorOf(e) {
|
|
68
|
+
const d = e.details;
|
|
69
|
+
const details = typeof d === "object" && d !== null && !Array.isArray(d) ? d : undefined;
|
|
70
|
+
return new CmdopError(e.code, e.message, details);
|
|
71
|
+
}
|
|
72
|
+
/** The tool's result, or the refusal as a `CmdopError`. */
|
|
73
|
+
function settle(a) {
|
|
74
|
+
if ("ok" in a)
|
|
75
|
+
return a.ok;
|
|
76
|
+
if ("error" in a)
|
|
77
|
+
throw errorOf(a.error);
|
|
78
|
+
throw new CmdopError("not_sealed", "a sealed answer reached a call that was not sealed");
|
|
79
|
+
}
|
|
80
|
+
const isAnswer = (l) => "ok" in l || "error" in l || "sealed" in l;
|
|
81
|
+
export function connect(opts = {}) {
|
|
82
|
+
const carrier = carrierOf(opts);
|
|
83
|
+
async function machines(o = {}) {
|
|
84
|
+
return settle(await carrier.machines(o));
|
|
85
|
+
}
|
|
86
|
+
async function prepare(tool, args, o) {
|
|
87
|
+
const deadline = o.deadline === undefined ? {} : { deadline_ms: Math.max(0, Math.round(o.deadline)) };
|
|
88
|
+
const req = { tool, args: args ?? {}, ...(o.machine === undefined ? {} : { machine: o.machine }), ...deadline };
|
|
89
|
+
return opts.e2e ? opts.e2e.seal(req, () => machines()) : { req };
|
|
90
|
+
}
|
|
91
|
+
/** The tool's result from an answer, opened when the call was sealed. */
|
|
92
|
+
async function result(p, a) {
|
|
93
|
+
if (!p.open)
|
|
94
|
+
return settle(a);
|
|
95
|
+
if ("sealed" in a)
|
|
96
|
+
return settle(await p.open.answer(a.sealed));
|
|
97
|
+
// Refusals come in clear (they hold nothing secret); a result never does.
|
|
98
|
+
if ("ok" in a)
|
|
99
|
+
throw new CmdopError("not_sealed", "a sealed call came back in clear; refusing it");
|
|
100
|
+
return settle(a);
|
|
101
|
+
}
|
|
102
|
+
/** A carrier failure the caller caused (abort) is the contract's `cancelled`. */
|
|
103
|
+
function cancelledOr(e, signal) {
|
|
104
|
+
return signal?.aborted ? new CmdopError("cancelled", "the call was stopped by its caller") : e;
|
|
105
|
+
}
|
|
106
|
+
async function call(tool, args, o = {}) {
|
|
107
|
+
const p = await prepare(tool, args, o);
|
|
108
|
+
let a;
|
|
109
|
+
try {
|
|
110
|
+
a = await carrier.call(p.req, o.signal ? { signal: o.signal } : {});
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
throw cancelledOr(e, o.signal);
|
|
114
|
+
}
|
|
115
|
+
return result(p, a);
|
|
116
|
+
}
|
|
117
|
+
/** Pieces of one streamed call in order, opened when sealed; the answer last. */
|
|
118
|
+
async function* lines(p, o) {
|
|
119
|
+
try {
|
|
120
|
+
for await (const l of carrier.stream(p.req, o.signal ? { signal: o.signal } : {})) {
|
|
121
|
+
if (isAnswer(l))
|
|
122
|
+
return l;
|
|
123
|
+
if ("chunk" in l && !p.open)
|
|
124
|
+
yield { ...l.chunk, seq: l.seq };
|
|
125
|
+
else if ("sealed_chunk" in l && p.open)
|
|
126
|
+
yield { ...(await p.open.piece(l.seq, l.sealed_chunk)), seq: l.seq };
|
|
127
|
+
else
|
|
128
|
+
throw new CmdopError("not_sealed", "a piece came back in a form this call did not ask for");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
throw cancelledOr(e, o.signal);
|
|
133
|
+
}
|
|
134
|
+
throw new CarrierError(carrier.name, "the stream ended without an answer");
|
|
135
|
+
}
|
|
136
|
+
function stream(tool, args, o = {}) {
|
|
137
|
+
// Breaking out of the loop aborts the request, which cancels the call on the machine.
|
|
138
|
+
const stop = new AbortController();
|
|
139
|
+
const signal = o.signal ? AbortSignal.any([o.signal, stop.signal]) : stop.signal;
|
|
140
|
+
const opts = { ...o, signal };
|
|
141
|
+
let gen;
|
|
142
|
+
let iterated = false;
|
|
143
|
+
let resolveDone;
|
|
144
|
+
let settled = false;
|
|
145
|
+
const done = new Promise((resolve) => (resolveDone = resolve));
|
|
146
|
+
// Nobody may await the result of a run they stopped; its rejection is still theirs to read.
|
|
147
|
+
done.catch(() => { });
|
|
148
|
+
// A thunk, so a rejection is only made when it settles the result (an unread one would be reported).
|
|
149
|
+
const settleResult = (a) => {
|
|
150
|
+
if (settled)
|
|
151
|
+
return;
|
|
152
|
+
settled = true;
|
|
153
|
+
resolveDone(a());
|
|
154
|
+
};
|
|
155
|
+
const prepared = prepare(tool, args, opts);
|
|
156
|
+
const start = async () => (gen ??= lines(await prepared, opts));
|
|
157
|
+
const finish = async (a) => settleResult(() => result$(a));
|
|
158
|
+
const result$ = async (a) => result(await prepared, a);
|
|
159
|
+
return {
|
|
160
|
+
async *[Symbol.asyncIterator]() {
|
|
161
|
+
iterated = true;
|
|
162
|
+
const g = await start();
|
|
163
|
+
try {
|
|
164
|
+
for (;;) {
|
|
165
|
+
const n = await g.next();
|
|
166
|
+
if (n.done) {
|
|
167
|
+
await finish(n.value);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
yield n.value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
settleResult(() => Promise.reject(e));
|
|
175
|
+
throw e;
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
// Left the loop before the answer: the call is stopped, and its result says so.
|
|
179
|
+
settleResult(() => Promise.reject(new CmdopError("cancelled", "the call was stopped by its caller")));
|
|
180
|
+
stop.abort();
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
get result() {
|
|
184
|
+
if (!iterated) {
|
|
185
|
+
iterated = true;
|
|
186
|
+
void (async () => {
|
|
187
|
+
try {
|
|
188
|
+
const g = await start();
|
|
189
|
+
for (;;) {
|
|
190
|
+
const n = await g.next();
|
|
191
|
+
if (n.done)
|
|
192
|
+
return finish(n.value);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
settleResult(() => Promise.reject(e));
|
|
197
|
+
}
|
|
198
|
+
})();
|
|
199
|
+
}
|
|
200
|
+
return done;
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function machine(name) {
|
|
205
|
+
const at = (o) => ({ ...o, ...(name === undefined ? {} : { machine: name }) });
|
|
206
|
+
const out = {};
|
|
207
|
+
for (const tool of TOOL_NAMES)
|
|
208
|
+
out[tool] = (args, o) => call(tool, args, at(o));
|
|
209
|
+
out.run = (command, args = {}, o) => call("exec_run", { ...args, command }, at(o));
|
|
210
|
+
out.stream = (command, args = {}, o) => stream("exec_run", { ...args, command }, at(o));
|
|
211
|
+
out.screenshot = async (args = {}, o) => toScreenshot(await call("screen_capture", args, at(o)));
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
async function publicKey() {
|
|
215
|
+
if (!opts.e2e)
|
|
216
|
+
throw new CmdopError("bad_args", "this client was made without e2e");
|
|
217
|
+
return opts.e2e.publicKey();
|
|
218
|
+
}
|
|
219
|
+
return { carrier: carrier.name, machines, machine, call, stream, publicKey, close: () => carrier.close() };
|
|
220
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export type ErrorCode = "other" | "denied" | "unknown_tool" | "bad_args" | "not_found" | "busy" | "permission_missing" | "screen_locked" | "stale_capture" | "element_gone" | "app_not_allowed" | "lease_paused" | "paused" | "offline" | "rate_limited" | "quota_exceeded" | "not_dispatched" | "display_gone" | "timed_out" | "cancelled" | "too_large" | "hash_required" | "failed" | "needs_presence" | "unauthorized";
|
|
2
|
+
/**
|
|
3
|
+
* One piece of a running command's output, as a streaming `exec_run` sends it (the payload of
|
|
4
|
+
* a `CallChunk`). The machine writes it and every client reads it, so it is defined here once.
|
|
5
|
+
* Fields are in the order the text has always had them; `dropped` is absent when nothing was.
|
|
6
|
+
*/
|
|
7
|
+
export type OutputChunk = {
|
|
8
|
+
/** The output, as text (invalid UTF-8 replaced). */
|
|
9
|
+
data: string;
|
|
10
|
+
/**
|
|
11
|
+
* Bytes left out just before this piece because the reader fell behind; the call's final
|
|
12
|
+
* result still has the whole tail.
|
|
13
|
+
*/
|
|
14
|
+
dropped?: number | null;
|
|
15
|
+
stream: OutputStream;
|
|
16
|
+
};
|
|
17
|
+
/** One call, as a caller sends it. */
|
|
18
|
+
export type CallRequest = {
|
|
19
|
+
/** The arguments, as the tool's input schema says. Absent means `{}`. */
|
|
20
|
+
args?: unknown;
|
|
21
|
+
/**
|
|
22
|
+
* How long the caller waits, in milliseconds. The call keeps the smaller of this and the
|
|
23
|
+
* tool's own time; past it the answer is `timed_out`. Absent: the tool's time.
|
|
24
|
+
*/
|
|
25
|
+
deadline_ms?: number | null;
|
|
26
|
+
/** Machine id or unique name; optional when exactly one machine is online. */
|
|
27
|
+
machine?: string | null;
|
|
28
|
+
/** The arguments sealed for the machine, instead of `args`. */
|
|
29
|
+
sealed?: string | null;
|
|
30
|
+
/** The tool's name, in clear: the server routes and checks the grant by it. */
|
|
31
|
+
tool: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* How a call ended: its result, why there is none, or a sealed answer with its outcome in
|
|
35
|
+
* clear (`ok` or an error code).
|
|
36
|
+
*/
|
|
37
|
+
export type Answer = {
|
|
38
|
+
ok: unknown;
|
|
39
|
+
} | {
|
|
40
|
+
error: ToolError;
|
|
41
|
+
} | {
|
|
42
|
+
outcome: string;
|
|
43
|
+
sealed: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* One line of a streamed answer: a piece of output while the call runs, and the answer last.
|
|
47
|
+
* `seq` counts the pieces of one call from 0, in the order they were made.
|
|
48
|
+
*/
|
|
49
|
+
export type StreamLine = {
|
|
50
|
+
chunk: OutputChunk;
|
|
51
|
+
seq: number;
|
|
52
|
+
} | {
|
|
53
|
+
sealed_chunk: string;
|
|
54
|
+
seq: number;
|
|
55
|
+
} | Answer;
|
|
56
|
+
/** Which output of the command a piece comes from. */
|
|
57
|
+
export type OutputStream = "stdout" | "stderr";
|
|
58
|
+
/**
|
|
59
|
+
* Why a call did not return a result. The machine answers with this; the server and the SDK
|
|
60
|
+
* decide by `code`, never by `message`.
|
|
61
|
+
*/
|
|
62
|
+
export type ToolError = {
|
|
63
|
+
code: ErrorCode;
|
|
64
|
+
/**
|
|
65
|
+
* Machine-readable facts for the caller, per code (`permission_missing` carries the
|
|
66
|
+
* permission, who must act and where). Absent for most errors; never required to read
|
|
67
|
+
* `code`.
|
|
68
|
+
*/
|
|
69
|
+
details?: unknown;
|
|
70
|
+
message: string;
|
|
71
|
+
};
|
|
72
|
+
/** Every error code, in the order Rust declares them. */
|
|
73
|
+
export declare const ERROR_CODES: readonly ["other", "denied", "unknown_tool", "bad_args", "not_found", "busy", "permission_missing", "screen_locked", "stale_capture", "element_gone", "app_not_allowed", "lease_paused", "paused", "offline", "rate_limited", "quota_exceeded", "not_dispatched", "display_gone", "timed_out", "cancelled", "too_large", "hash_required", "failed", "needs_presence", "unauthorized"];
|
|
74
|
+
/** The size limits every carrier keeps. */
|
|
75
|
+
export declare const LIMITS: {
|
|
76
|
+
readonly envelopeMaxBytes: 16777216;
|
|
77
|
+
readonly pieceMaxBytes: 65536;
|
|
78
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Generated from cmdop/crates/tools/tools.schema.json (sha256 459e32fe9bbff178, cmdop 0.1.9) by exports/sdk/gen/gen.ts. Do not edit.
|
|
2
|
+
/** Every error code, in the order Rust declares them. */
|
|
3
|
+
export const ERROR_CODES = ["other", "denied", "unknown_tool", "bad_args", "not_found", "busy", "permission_missing", "screen_locked", "stale_capture", "element_gone", "app_not_allowed", "lease_paused", "paused", "offline", "rate_limited", "quota_exceeded", "not_dispatched", "display_gone", "timed_out", "cancelled", "too_large", "hash_required", "failed", "needs_presence", "unauthorized"];
|
|
4
|
+
/** The size limits every carrier keeps. */
|
|
5
|
+
export const LIMITS = { envelopeMaxBytes: 16777216, pieceMaxBytes: 65536 };
|