@cmdop/sdk 1.0.2 → 2.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 CHANGED
@@ -1,148 +1,133 @@
1
- # @cmdop/sdk (Node)
1
+ # @cmdop/sdk 2.0 (Node)
2
2
 
3
- ![@cmdop/sdk — Node SDK for CMDOP](https://raw.githubusercontent.com/commandoperator/cmdop-sdk/main/assets/hero-cmdop-node.webp)
4
-
5
- Typed Node SDK for CMDOP — manage your machines, fleets, tunnels, and schedules,
6
- stream a machine's AI agent, and drive the skills marketplace, all from typed
7
- TypeScript.
8
-
9
- 📚 **Docs: [docs.cmdop.com](https://docs.cmdop.com)** · [SDK](https://cmdop.com/sdk) ·
10
- [Bots](https://cmdop.com/bots) · [Connect](https://cmdop.com/connect)
11
-
12
- ![How the CMDOP SDK connects your app to your machines](https://raw.githubusercontent.com/commandoperator/cmdop-sdk/main/assets/diagram-cmdop-sdk.webp)
13
-
14
- - **One install, zero dependencies** — `npm i @cmdop/sdk` is everything. No
15
- native build step, no `optionalDependencies`, nothing fetched on first run.
16
- - **Works anywhere, offline-ready** — a single self-contained package runs the
17
- same on macOS, Linux, and Windows, including air-gapped hosts.
18
- - **Typed end to end** — full TypeScript types for every resource and response,
19
- with one clean async streaming API for live agent output.
3
+ Typed Node SDK for the current Cmdop machine roster, agent stream, and durable
4
+ messages. Requires Node 18 or newer.
20
5
 
21
6
  ## Install
22
7
 
23
8
  ```bash
24
- npm i @cmdop/sdk # or: pnpm add @cmdop/sdk / yarn add @cmdop/sdk
9
+ npm install @cmdop/sdk
25
10
  ```
26
11
 
27
- ESM + CJS, Node ≥ 18.
12
+ The package includes `cmdop-core` for supported macOS, Linux, and Windows
13
+ hosts. It does not download a runtime on first use.
28
14
 
29
15
  ## Quick start
30
16
 
17
+ `new Client()` is the canonical local configuration. The core discovers the
18
+ active Cmdop server and credentials from the local Cmdop configuration.
19
+
31
20
  ```ts
32
21
  import { Client } from "@cmdop/sdk";
33
22
 
34
- const c = new Client({ token: "..." }); // or Client.fromEnv()
23
+ const client = new Client();
24
+ try {
25
+ const bootstrap = await client.system.bootstrap();
26
+ console.log(bootstrap.version, bootstrap.capabilities);
35
27
 
36
- const page = await c.machines.list({ presence: "online" });
37
- for (const m of page.items) console.log(m.hostname, m.presence);
28
+ const snapshot = await client.machines.list();
29
+ for (const machine of snapshot.machines) {
30
+ console.log(machine.machineId, machine.displayName, machine.presence);
31
+ }
32
+ } finally {
33
+ await client.close();
34
+ }
35
+ ```
38
36
 
39
- const text = await c.machines.ask(machineId, "uptime").collect();
40
- console.log(text);
37
+ For an explicit remote relay, provide both its URL and bearer token. Constructor
38
+ options take precedence over environment variables.
41
39
 
42
- await c.close(); // release resources
40
+ ```ts
41
+ const client = new Client({ baseUrl: "https://relay.example.com", token: "..." });
43
42
  ```
44
43
 
45
- `await using c = new Client(...)` works too (`Symbol.asyncDispose`).
44
+ The equivalent environment variables are `CMDOP_BASE_URL` and `CMDOP_TOKEN`.
45
+ A remote URL without a token fails closed. An explicit loopback URL can mint a
46
+ local loopback token.
47
+
48
+ ## Public API
49
+
50
+ | Namespace | Method | Result |
51
+ |---|---|---|
52
+ | `system` | `await client.system.bootstrap()` | version, bundle, contract range, capabilities, update state |
53
+ | `machines` | `await client.machines.list()` | authoritative `MachineRosterSnapshot` |
54
+ | `machines` | `client.machines.ask(machineId, prompt, options)` | `AskStream` |
55
+ | `machines` | `await client.machines.messages(machineId, options)` | durable `MachineMessageList` |
56
+
57
+ ## Ask streaming and PIN
46
58
 
47
- ## Namespaces
59
+ Pass a connection PIN up front, before consuming the headless stream.
48
60
 
49
- The same surface as the relay, mirrored exactly by the Python SDK (camelCase
50
- here, snake_case there):
61
+ ```ts
62
+ const stream = client.machines.ask(
63
+ machineId,
64
+ "Summarize the current system load.",
65
+ { pin: "1234", session: "2" },
66
+ );
51
67
 
52
- | Namespace | Methods |
53
- |---|---|
54
- | `machines` | `list · get · update · disable · info · spend · ask · messages · clearMessages · activeSession` |
55
- | `fleets` | `list · get · create · update · disable` |
56
- | `fleets.members` | `list · add · setRole · remove` |
57
- | `fleets.machines` | `list · attach · detach` |
58
- | `tunnels` | `open · close · list · get · sessions` |
59
- | `schedules` | `list · get · create · update · delete · trigger · runs` |
60
- | `keys` | `list · issue · revoke` |
61
- | `skills` | `list · get · my · install · star · versions · reviews · create · update · delete · publish · publishStatus · categories · tags` |
68
+ for await (const frame of stream) {
69
+ if (frame.type === "event") console.log(frame.payload);
70
+ else if (frame.type === "error") console.log(frame.code, frame.message);
71
+ else console.log(frame.success, frame.text);
72
+ }
73
+ ```
62
74
 
63
- List endpoints also expose `iter(...)` (an async generator over every item,
64
- following cursors) and `pages(...)` (an async generator over each page).
75
+ For the common one-shot case, `collect()` drains the stream and returns final
76
+ text. An unsuccessful `done` throws `AgentStreamError`.
65
77
 
66
- > **Two planes, one client.** `machines / fleets / tunnels / schedules / keys`
67
- > use your relay token (`CMDOP_TOKEN`); the **`skills`** marketplace uses your
68
- > platform API key (`CMDOP_API_KEY`). Set whichever you need — the client routes
69
- > each call to the right plane for you.
78
+ ```ts
79
+ const text = await client.machines.ask(
80
+ machineId,
81
+ "Reply with: pong",
82
+ { pin: "1234" },
83
+ ).collect();
84
+ ```
70
85
 
71
- ## Streaming: `machines.ask()`
86
+ Ask options also include `attachmentIds`, `engine`, `workingDir`,
87
+ `projectRootId`, and `model`. A stream is single-consumer and yields only typed
88
+ `event`, `error`, and `done` frames.
72
89
 
73
- `ask()` returns an `AskStream` — an async iterable of typed frames with `pin()` /
74
- `confirm()` / `collect()`:
90
+ ## Durable messages
75
91
 
76
92
  ```ts
77
- const stream = c.machines.ask(machineId, "df -h", { sessionId: "s1" });
78
- for await (const frame of stream) {
79
- if (frame.type === "event") {
80
- process.stdout.write(String((frame.payload as { delta?: string })?.delta ?? ""));
81
- } else if (frame.type === "pin_required") { // machine asked for its connect PIN
82
- await stream.pin(frame.challengeId, "1234");
83
- } else if (frame.type === "confirm_required") { // a dangerous plan awaits approval
84
- await stream.confirm(frame.token, true);
85
- } else if (frame.type === "pin_denied") {
86
- console.log("PIN rejected:", frame.reason);
87
- } else if (frame.type === "done") {
88
- console.log("\n->", frame.text);
89
- }
93
+ const history = await client.machines.messages(machineId, { session: "2", limit: 100 });
94
+ for (const message of history.messages) {
95
+ console.log(message.seq, message.role, message.content);
90
96
  }
91
-
92
- // one-shot: drain to the final text
93
- const text = await c.machines.ask(machineId, "uptime").collect();
94
97
  ```
95
98
 
96
- Frame types: `event · done · error · confirm_required · pin_required ·
97
- pin_denied`. An `error` outcome throws `AgentStreamError`.
99
+ `limit` must be between 1 and 500.
98
100
 
99
- ## Environment variables
101
+ ## Configuration
100
102
 
101
- | Var | Meaning | Default |
102
- |---|---|---|
103
- | `CMDOP_TOKEN` | relay Bearer token (machines/fleets/…) | — (required for relay ops) |
104
- | `CMDOP_BASE_URL` | relay REST root | `https://cloud.cmdop.com` |
105
- | `CMDOP_API_KEY` | platform `UserAPIKey` (for `skills`) | — (required for skills) |
106
- | `CMDOP_API_BASE_URL` | platform REST root | `https://api.cmdop.com` |
107
- | `CMDOP_FLEET_ID` | default fleet for fleet-scoped ops | none |
108
- | `CMDOP_TIMEOUT_MS` | per-call timeout | `30000` |
103
+ | Setting | Constructor | Environment | Default |
104
+ |---|---|---|---|
105
+ | Relay URL | `baseUrl` | `CMDOP_BASE_URL` | local Cmdop config |
106
+ | Bearer token | `token` | `CMDOP_TOKEN` | local Cmdop credentials |
107
+ | Timeout | `timeoutMs` | `CMDOP_TIMEOUT_MS` | `30000` |
108
+ | Development binary override | `binaryPath` | `CMDOP_CORE_BINARY` | packaged host binary |
109
109
 
110
- Precedence is always explicit option > env var > default. Pass a string to the
111
- constructor (`new Client("token")`) as a shorthand for `{ token }`.
110
+ Call `await client.close()` when finished. `await using` is also supported on
111
+ runtimes configured for explicit resource management.
112
112
 
113
- ## Error handling
113
+ ## Typed errors
114
114
 
115
- Every failure surfaces as a typed error carrying a stable `code` (all extend
116
- `CmdopError`):
115
+ Every protocol failure is a `CmdopError` with stable `code`, optional HTTP
116
+ `status`, `retryable`, and redacted `details` fields. Common subclasses include
117
+ `AuthError`, `PermissionError`, `ValidationError`, `NotFoundError`,
118
+ `UnavailableError`, `TimeoutError`, `ConnectionError`, and `AgentStreamError`.
117
119
 
118
120
  ```ts
119
- import {
120
- AuthError, PermissionError, NotFoundError, ConflictError,
121
- ValidationError, RateLimitError, ServerError, ConnectionError,
122
- TimeoutError, UnavailableError, AgentStreamError, CmdopError,
123
- } from "@cmdop/sdk";
121
+ import { CmdopError } from "@cmdop/sdk";
124
122
 
125
123
  try {
126
- await c.machines.get("missing-id");
127
- } catch (e) {
128
- if (e instanceof NotFoundError) { /* ... */ }
129
- else if (e instanceof CmdopError) {
130
- if (e.retryable) { /* true on TimeoutError — safe to retry */ }
131
- console.log(e.code, e.message);
124
+ await client.machines.messages("missing");
125
+ } catch (error) {
126
+ if (error instanceof CmdopError) {
127
+ console.log(error.code, error.status, error.retryable);
132
128
  }
133
129
  }
134
130
  ```
135
131
 
136
- `ConnectionError` covers a lost connection mid-call (pending promises reject);
137
- `TimeoutError` (a retryable subclass) is a deadline/handshake timeout;
138
- `UnavailableError` means the relay is up but the target agent/machine is offline.
139
- `AgentStreamError` is the streaming-`ask` error outcome.
140
-
141
- ## Links
142
-
143
- - **Docs** → [docs.cmdop.com](https://docs.cmdop.com)
144
- - **SDK** → [cmdop.com/sdk](https://cmdop.com/sdk) · **Bots** →
145
- [cmdop.com/bots](https://cmdop.com/bots) · **Connect** →
146
- [cmdop.com/connect](https://cmdop.com/connect)
147
- - [`@cmdop/sdk` on npm](https://www.npmjs.com/package/@cmdop/sdk) ·
148
- [source](https://github.com/commandoperator/cmdop-sdk)
132
+ Documentation: [docs.cmdop.com](https://docs.cmdop.com) · Source:
133
+ [commandoperator/cmdop-sdk](https://github.com/commandoperator/cmdop-sdk)
Binary file
Binary file
Binary file
Binary file
Binary file