@cmdop/sdk 1.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/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # @cmdop/sdk (Node)
2
+
3
+ Typed Node SDK for CMDOP. A **thin** client: it spawns the baked-in `cmdop-core`
4
+ Go binary and speaks a protobuf-length-delimited `Envelope` protocol over its
5
+ stdio. All relay logic (REST, the `/ask` SSE stream, pin/confirm side-channels,
6
+ retries, error mapping) lives in the Go core.
7
+
8
+ - **No Go toolchain, no `cmdop_go` install** — every platform's `cmdop-core`
9
+ binary is baked into the one package; the right one is selected at runtime
10
+ (no `optionalDependencies`). `npm i @cmdop/sdk` is everything.
11
+ - **Pure stdio** — no local port, no socket. Token via `CMDOP_TOKEN` env at spawn.
12
+ - **No macOS notarization** — npm-extracted binaries are unquarantined (ad-hoc
13
+ signed only).
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm i @cmdop/sdk # or: pnpm add @cmdop/sdk / yarn add @cmdop/sdk
19
+ ```
20
+
21
+ ESM + CJS, Node ≥ 18.
22
+
23
+ ## Quick start
24
+
25
+ ```ts
26
+ import { Client } from "@cmdop/sdk";
27
+
28
+ const c = new Client({ token: "..." }); // or Client.fromEnv()
29
+
30
+ const page = await c.machines.list({ presence: "online" });
31
+ for (const m of page.items) console.log(m.hostname, m.presence);
32
+
33
+ const text = await c.machines.ask(machineId, "uptime").collect();
34
+ console.log(text);
35
+
36
+ await c.close(); // reap the core subprocess
37
+ ```
38
+
39
+ `await using c = new Client(...)` works too (`Symbol.asyncDispose`).
40
+
41
+ ## Namespaces
42
+
43
+ The same surface as the relay, mirrored exactly by the Python SDK (camelCase
44
+ here, snake_case there):
45
+
46
+ | Namespace | Methods |
47
+ |---|---|
48
+ | `machines` | `list · get · update · disable · info · spend · ask · messages · clearMessages · activeSession` |
49
+ | `fleets` | `list · get · create · update · disable` |
50
+ | `fleets.members` | `list · add · setRole · remove` |
51
+ | `fleets.machines` | `list · attach · detach` |
52
+ | `tunnels` | `open · close · list · get · sessions` |
53
+ | `schedules` | `list · get · create · update · delete · trigger · runs` |
54
+ | `keys` | `list · issue · revoke` |
55
+ | `skills` | `list · get · my · install · star · versions · reviews · create · update · delete · publish · publishStatus · categories · tags` |
56
+
57
+ List endpoints also expose `iter(...)` (an async generator over every item,
58
+ following cursors) and `pages(...)` (an async generator over each page).
59
+
60
+ > **Two planes, one client.** `machines / fleets / tunnels / schedules / keys`
61
+ > talk to the relay with `CMDOP_TOKEN`; the **`skills`** marketplace lives on the
62
+ > platform (Django) and uses the `UserAPIKey` (`CMDOP_API_KEY`). Both are passed
63
+ > to the core at spawn; you only set whichever you use.
64
+
65
+ ## Streaming: `machines.ask()`
66
+
67
+ `ask()` returns an `AskStream` — an async iterable of typed frames with `pin()` /
68
+ `confirm()` / `collect()`:
69
+
70
+ ```ts
71
+ const stream = c.machines.ask(machineId, "df -h", { sessionId: "s1" });
72
+ for await (const frame of stream) {
73
+ if (frame.type === "event") {
74
+ process.stdout.write(String((frame.payload as { delta?: string })?.delta ?? ""));
75
+ } else if (frame.type === "pin_required") { // machine asked for its connect PIN
76
+ await stream.pin(frame.challengeId, "1234");
77
+ } else if (frame.type === "confirm_required") { // a dangerous plan awaits approval
78
+ await stream.confirm(frame.token, true);
79
+ } else if (frame.type === "pin_denied") {
80
+ console.log("PIN rejected:", frame.reason);
81
+ } else if (frame.type === "done") {
82
+ console.log("\n->", frame.text);
83
+ }
84
+ }
85
+
86
+ // one-shot: drain to the final text
87
+ const text = await c.machines.ask(machineId, "uptime").collect();
88
+ ```
89
+
90
+ Frame types: `event · done · error · confirm_required · pin_required ·
91
+ pin_denied`. An `error` outcome throws `AgentStreamError`.
92
+
93
+ ## Environment variables
94
+
95
+ | Var | Meaning | Default |
96
+ |---|---|---|
97
+ | `CMDOP_TOKEN` | relay Bearer token (machines/fleets/…) | — (required for relay ops) |
98
+ | `CMDOP_BASE_URL` | relay REST root | `https://cloud.cmdop.com` |
99
+ | `CMDOP_API_KEY` | platform `UserAPIKey` (for `skills`) | — (required for skills) |
100
+ | `CMDOP_API_BASE_URL` | platform REST root | `https://api.cmdop.com` |
101
+ | `CMDOP_FLEET_ID` | default fleet for fleet-scoped ops | none |
102
+ | `CMDOP_TIMEOUT_MS` | per-call timeout | `30000` |
103
+ | `CMDOP_CORE_BINARY` | override the baked binary (dev / offline) | baked binary |
104
+
105
+ Precedence is always explicit option > env var > default. Pass a string to the
106
+ constructor (`new Client("token")`) as a shorthand for `{ token }`.
107
+
108
+ ## Error handling
109
+
110
+ The core emits a terminal `ERROR` frame; the client maps its `code` to a typed
111
+ error (all extend `CmdopError`):
112
+
113
+ ```ts
114
+ import {
115
+ AuthError, PermissionError, NotFoundError, ConflictError,
116
+ ValidationError, RateLimitError, ServerError, ConnectionError,
117
+ AgentStreamError, CmdopError,
118
+ } from "@cmdop/sdk";
119
+
120
+ try {
121
+ await c.machines.get("missing-id");
122
+ } catch (e) {
123
+ if (e instanceof NotFoundError) { /* ... */ }
124
+ else if (e instanceof CmdopError) console.log(e.code, e.message);
125
+ }
126
+ ```
127
+
128
+ `ConnectionError` also covers the core process dying mid-call (pending
129
+ promises reject). `AgentStreamError` is the streaming-`ask` error outcome.
130
+
131
+ ## Local development
132
+
133
+ Released packages bake in every platform's binary. For local work, build the
134
+ core and point the client at it with `CMDOP_CORE_BINARY` (also the offline
135
+ escape hatch in production):
136
+
137
+ ```bash
138
+ cd ../../cmdop_go/go && go build -o /tmp/cmdop-core ./cmd/cmdop-core
139
+ CMDOP_CORE_BINARY=/tmp/cmdop-core CMDOP_TOKEN=... node ...
140
+ ```
141
+
142
+ Tests: `pnpm exec vitest run`. The cross-language parity gate:
143
+ `python ../../core/parity/check.py`.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,63 @@
1
+ // src/errors.ts
2
+ var CmdopError = class extends Error {
3
+ status;
4
+ code;
5
+ body;
6
+ constructor(message, status, code, body) {
7
+ super(message);
8
+ this.name = new.target.name;
9
+ this.status = status;
10
+ this.code = code;
11
+ this.body = body;
12
+ Object.setPrototypeOf(this, new.target.prototype);
13
+ }
14
+ };
15
+ var AuthError = class extends CmdopError {
16
+ };
17
+ var PermissionError = class extends CmdopError {
18
+ };
19
+ var NotFoundError = class extends CmdopError {
20
+ };
21
+ var ConflictError = class extends CmdopError {
22
+ };
23
+ var ValidationError = class extends CmdopError {
24
+ };
25
+ var RateLimitError = class extends CmdopError {
26
+ retryAfter;
27
+ constructor(message, code, retryAfter, body) {
28
+ super(message, void 0, code, body);
29
+ this.retryAfter = retryAfter;
30
+ }
31
+ };
32
+ var ServerError = class extends CmdopError {
33
+ };
34
+ var ConnectionError = class extends CmdopError {
35
+ };
36
+ var AgentStreamError = class extends CmdopError {
37
+ constructor(code, message) {
38
+ super(message, void 0, code);
39
+ }
40
+ };
41
+ var CODE_MAP = {
42
+ auth: AuthError,
43
+ permission: PermissionError,
44
+ not_found: NotFoundError,
45
+ conflict: ConflictError,
46
+ validation: ValidationError,
47
+ server: ServerError,
48
+ connection: ConnectionError
49
+ };
50
+ function mapCoreError(code, message) {
51
+ if (code === "rate_limit") {
52
+ return new RateLimitError(message, code);
53
+ }
54
+ const Ctor = CODE_MAP[code];
55
+ if (Ctor) {
56
+ return new Ctor(message, void 0, code);
57
+ }
58
+ return new CmdopError(message, void 0, code);
59
+ }
60
+
61
+ export { AgentStreamError, AuthError, CmdopError, ConflictError, ConnectionError, NotFoundError, PermissionError, RateLimitError, ServerError, ValidationError, mapCoreError };
62
+ //# sourceMappingURL=chunk-HVTWZ3OM.js.map
63
+ //# sourceMappingURL=chunk-HVTWZ3OM.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";AAUO,IAAM,UAAA,GAAN,cAAyB,KAAA,CAAM;AAAA,EAC3B,MAAA;AAAA,EACA,IAAA;AAAA,EACA,IAAA;AAAA,EAET,WAAA,CAAY,OAAA,EAAiB,MAAA,EA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,EAAW,aAAA;AAAA,EACX,QAAA,EAAU,aAAA;AAAA,EACV,UAAA,EAAY,eAAA;AAAA,EACZ,MAAA,EAAQ,WAAA;AAAA,EACR,UAAA,EAAY;AACd,CAAA;AAGO,SAAS,YAAA,CAAa,MAAc,OAAA,EAA6B;AACtE,EAAA,IAAI,SAAS,YAAA,EAAc;AACzB,IAAA,OAAO,IAAI,cAAA,CAAe,OAAA,EAAS,IAAI,CAAA;AAAA,EACzC;AACA,EAAA,MAAM,IAAA,GAAO,SAAS,IAAI,CAAA;AAC1B,EAAA,IAAI,IAAA,EAAM;AACR,IAAA,OAAO,IAAI,IAAA,CAAK,OAAA,EAAS,MAAA,EAAW,IAAI,CAAA;AAAA,EAC1C;AAEA,EAAA,OAAO,IAAI,UAAA,CAAW,OAAA,EAAS,MAAA,EAAW,IAAI,CAAA;AAChD","file":"chunk-HVTWZ3OM.js","sourcesContent":["/**\n * Error hierarchy + `ErrorInfo.code` -> typed error mapper.\n *\n * Mirror of the Python `CmdopError` tree. The Go core emits a terminal `ERROR`\n * frame carrying an `ErrorInfo{code, message}`; {@link mapCoreError} maps that\n * `code` to the right typed exception. Codes are owned by the core\n * (`internal/core/errmap.go`): `auth / permission / not_found / conflict /\n * validation / rate_limit / server / connection` (+ catch-alls).\n */\n\nexport class CmdopError extends Error {\n readonly status?: number;\n readonly code?: string;\n readonly body?: unknown;\n\n constructor(message: string, status?: number, code?: string, body?: unknown) {\n super(message);\n this.name = new.target.name;\n this.status = status;\n this.code = code;\n this.body = body;\n // Restore prototype chain for instanceof across transpile targets.\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\nexport class AuthError extends CmdopError {}\nexport class PermissionError extends CmdopError {}\nexport class NotFoundError extends CmdopError {}\nexport class ConflictError extends CmdopError {}\nexport class ValidationError extends CmdopError {}\n\nexport class RateLimitError extends CmdopError {\n readonly retryAfter?: number;\n constructor(message: string, code?: string, retryAfter?: number, body?: unknown) {\n super(message, undefined, code, body);\n this.retryAfter = retryAfter;\n }\n}\n\nexport class ServerError extends CmdopError {}\nexport class ConnectionError extends CmdopError {}\n\n/** An `error` outcome from `machines.ask` (machine_offline/timeout/internal). */\nexport class AgentStreamError extends CmdopError {\n constructor(code: string, message: string) {\n super(message, undefined, code);\n }\n}\n\ntype CmdopErrorCtor = new (\n message: string,\n status?: number,\n code?: string,\n body?: unknown,\n) => CmdopError;\n\nconst CODE_MAP: Record<string, CmdopErrorCtor> = {\n auth: AuthError,\n permission: PermissionError,\n not_found: NotFoundError,\n conflict: ConflictError,\n validation: ValidationError,\n server: ServerError,\n connection: ConnectionError,\n};\n\n/** Map a core `ErrorInfo{code, message}` to a typed {@link CmdopError}. */\nexport function mapCoreError(code: string, message: string): CmdopError {\n if (code === \"rate_limit\") {\n return new RateLimitError(message, code);\n }\n const Ctor = CODE_MAP[code];\n if (Ctor) {\n return new Ctor(message, undefined, code);\n }\n // internal / unknown_op / unsupported / anything new -> base error.\n return new CmdopError(message, undefined, code);\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export { AgentStreamError, AuthError, CmdopError, ConflictError, ConnectionError, NotFoundError, PermissionError, RateLimitError, ServerError, ValidationError, mapCoreError } from './chunk-HVTWZ3OM.js';
2
+ //# sourceMappingURL=errors-4ITNIIVL.js.map
3
+ //# sourceMappingURL=errors-4ITNIIVL.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"errors-4ITNIIVL.js"}