TypeScript SDK
@cmdop/sdk for Node 20 and later: your machines’ tools as typed methods.
Install
npm install @cmdop/sdkConnect
import { connect } from "@cmdop/sdk";
const cmdop = connect(); // CMDOP_TOKEN, CMDOP_URL
const machines = await cmdop.machines(); // name, id, online, os, tools| Option | Default | Meaning |
|---|---|---|
token | CMDOP_TOKEN | the agent token; the program can do what the agent’s grant allows |
url | CMDOP_URL | your page’s address, https://<address>.cmdop.dev |
contour | — | the address alone (m7xg5cgy) instead of url |
Run a command
const box = cmdop.machine("box"); // a name or id; no name: the one machine online
const { exit_code, stdout } = await box.run("uptime");
const run = box.stream("make test");
for await (const piece of run) process.stdout.write(piece.data);
console.log((await run.result).exit_code);piece.stream is stdout or stderr. Breaking out of the loop stops the command; its result is
then cancelled.
Any tool
Each tool on the tools reference is a method with the same arguments and result:
const file = await box.fs_read({ path: "/tmp/notes.txt" });
const task = await box.task_start({ command: "npm run dev" });
const out = await box.task_output({ task: task.task });
await cmdop.call("sys_info", {}, { machine: "box" }); // by nameEvery tool is also under its group, the part of its name before the first _:
box.fs.read({ path }) is box.fs_read({ path }), box.task.start(…) is box.task_start(…). Both spellings stay.
box.screenshot() returns the picture decoded (image.bytes, image.mime, geometry).
The tools about the whole contour, not one machine, are methods of the client:
const guide = await cmdop.showtime_guide({ language: "English" }); // the demo guide, as text
const { tasks } = await cmdop.tasks_list(); // everything running now
const rows = await cmdop.machines_list(); // machines with state and activity
const past = await cmdop.history_search({ text: "make test" });Deadlines, cancelling, errors
import { CmdopError } from "@cmdop/sdk";
const stop = new AbortController();
try {
await box.run("long-job", {}, { deadline: 30_000, signal: stop.signal });
} catch (e) {
if (e instanceof CmdopError && e.code === "timed_out") { /* retry later */ }
else throw e;
}deadline is in milliseconds; aborting signal cancels the call (cancelled). CmdopError.code
is one of the error codes; CarrierError means the connection broke and no
answer came.