@cmdop/sdk 3.6.0 → 3.7.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 +42 -22
- package/dist/generated/call.gen.js +1 -1
- package/dist/generated/server.gen.js +1 -1
- package/dist/generated/surface.gen.d.ts +6 -0
- package/dist/generated/surface.gen.js +3 -3
- package/dist/generated/tools.gen.d.ts +258 -3
- package/dist/generated/tools.gen.js +8 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/upload.d.ts +41 -0
- package/dist/upload.js +209 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,41 +1,61 @@
|
|
|
1
1
|
# @cmdop/sdk
|
|
2
2
|
|
|
3
|
-
Your
|
|
3
|
+
Your computers' tools from TypeScript. Use the same tools AI agents call through MCP, with typed
|
|
4
|
+
arguments and results, your agent's grant and a record of each call.
|
|
4
5
|
|
|
5
6
|
```bash
|
|
6
7
|
npm install @cmdop/sdk
|
|
7
8
|
```
|
|
8
9
|
|
|
10
|
+
Requires Node.js 20+. [Install CMDOP](https://cmdop.ai/download) on a computer, bring it online on
|
|
11
|
+
your page and create an agent token in the Agents tab. Set `CMDOP_URL` to your page's address
|
|
12
|
+
(`https://<address>.cmdop.dev`) and `CMDOP_TOKEN` to that token through your environment.
|
|
13
|
+
The SDK package does not install the machine program. Start with the `read` risk class.
|
|
14
|
+
|
|
9
15
|
```ts
|
|
10
16
|
import { connect } from "@cmdop/sdk";
|
|
11
17
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
18
|
+
const client = connect();
|
|
19
|
+
try {
|
|
20
|
+
const online = (await client.machines()).find((machine) => machine.online);
|
|
21
|
+
if (!online) throw new Error("Bring a machine online on your CMDOP page.");
|
|
22
|
+
const info = await client.machine(online.id).sys.info({});
|
|
23
|
+
console.log(info.os, info.arch, info.cpus);
|
|
24
|
+
} finally {
|
|
25
|
+
await client.close();
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The program prints the selected computer's OS, architecture and CPU count.
|
|
30
|
+
Use an explicit machine id when the target matters.
|
|
31
|
+
|
|
32
|
+
## Commands and streaming
|
|
15
33
|
|
|
16
|
-
|
|
34
|
+
Inside the connected client's `try` block, with `exec` allowed for the agent and machine:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
const box = client.machine("<machine>");
|
|
38
|
+
console.log((await box.run("uptime")).stdout);
|
|
39
|
+
const run = box.stream("printf 'hello\\n'");
|
|
17
40
|
for await (const piece of run) process.stdout.write(piece.data);
|
|
18
41
|
console.log((await run.result).exit_code);
|
|
19
42
|
```
|
|
20
43
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
`code`; `deadline` and `signal` bound and cancel any call.
|
|
25
|
-
|
|
26
|
-
The tools the server answers itself are methods of the client, with no machine: `cmdop.machines_list()`,
|
|
27
|
-
`cmdop.tasks_list()`, `cmdop.history_search({ text })`, `cmdop.docs_read({ path })` (the docs, contents first) and
|
|
28
|
-
`cmdop.showtime_guide({ language })`, the guide for a short live demo of what your machines can do.
|
|
44
|
+
`box.fs.read({ path })` and `box.fs_read({ path })` call the same typed tool.
|
|
45
|
+
`box.screenshot()` decodes the image. `client.tasks_list()` and `client.docs_read()` need no
|
|
46
|
+
machine. Installed AI agents are available through `box.agent.*` with the `agent` risk class.
|
|
29
47
|
|
|
30
|
-
|
|
31
|
-
`box.agent.list({})` names them, `box.agent.prompt({ prompt, session? })` returns the answer and a `session` to
|
|
32
|
-
continue, `box.agent.sessions({})` and `box.agent.cancel({ task })`. The agents answer in words or read files, never
|
|
33
|
-
run commands; the token needs the `agent` class and the machine's `ceiling.json` its `agents` key.
|
|
48
|
+
## Deadlines and errors
|
|
34
49
|
|
|
35
|
-
|
|
50
|
+
Pass `{ deadline: 5_000, signal: controller.signal }` as call options: deadlines use milliseconds
|
|
51
|
+
and `AbortSignal` cancels a call. For `run`, options are its third argument:
|
|
52
|
+
`box.run("uptime", {}, { deadline: 5_000 })`.
|
|
53
|
+
Branch on `CmdopError.code`; `CarrierError` means a connection failed before an answer arrived.
|
|
54
|
+
Python's deadline units differ; check the language reference.
|
|
36
55
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
[Quickstart](https://docs.cmdop.ai/reference/sdk/quickstart) ·
|
|
57
|
+
[TypeScript API](https://docs.cmdop.ai/reference/sdk/typescript) ·
|
|
58
|
+
[MCP client setup](https://docs.cmdop.ai/guides/connect) ·
|
|
59
|
+
[Source and issues](https://github.com/commandoperator/cmdop-sdk)
|
|
40
60
|
|
|
41
|
-
|
|
61
|
+
License: MIT.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated from cmdop/crates/tools/tools.schema.json (sha256
|
|
1
|
+
// Generated from cmdop/crates/tools/tools.schema.json (sha256 d1db4922f4b6dd14, cmdop 0.1.18) by exports/sdk/gen/gen.ts. Do not edit.
|
|
2
2
|
/** Every error code, in the order Rust declares them. */
|
|
3
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", "tool_refused", "sandbox_unavailable"];
|
|
4
4
|
/** The size limits every carrier keeps. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated from cmdop/crates/tools/tools.schema.json (sha256
|
|
1
|
+
// Generated from cmdop/crates/tools/tools.schema.json (sha256 d1db4922f4b6dd14, cmdop 0.1.18) by exports/sdk/gen/gen.ts. Do not edit.
|
|
2
2
|
/** Title and description of every server tool, as the server lists them. */
|
|
3
3
|
export const SERVER_TOOLS = {
|
|
4
4
|
docs_read: { title: "Read the docs", description: "Call before answering how CMDOP works, is set up or is priced: no `path` lists every page of docs.cmdop.ai (path and title); a `path` returns that page as Markdown." },
|
|
@@ -30,6 +30,11 @@ export declare const GROUPS: {
|
|
|
30
30
|
readonly read: "fs_read";
|
|
31
31
|
readonly search: "fs_search";
|
|
32
32
|
readonly stat: "fs_stat";
|
|
33
|
+
readonly upload_abort: "fs_upload_abort";
|
|
34
|
+
readonly upload_begin: "fs_upload_begin";
|
|
35
|
+
readonly upload_chunk: "fs_upload_chunk";
|
|
36
|
+
readonly upload_commit: "fs_upload_commit";
|
|
37
|
+
readonly upload_status: "fs_upload_status";
|
|
33
38
|
readonly write: "fs_write";
|
|
34
39
|
};
|
|
35
40
|
readonly journal: {
|
|
@@ -57,6 +62,7 @@ export declare const GROUPS: {
|
|
|
57
62
|
};
|
|
58
63
|
readonly sys: {
|
|
59
64
|
readonly info: "sys_info";
|
|
65
|
+
readonly metrics: "sys_metrics";
|
|
60
66
|
};
|
|
61
67
|
readonly task: {
|
|
62
68
|
readonly output: "task_output";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated from cmdop/crates/tools/tools.schema.json (sha256
|
|
1
|
+
// Generated from cmdop/crates/tools/tools.schema.json (sha256 d1db4922f4b6dd14, cmdop 0.1.18) by exports/sdk/gen/gen.ts. Do not edit.
|
|
2
2
|
/** Group, then method, then the tool it calls. */
|
|
3
3
|
export const GROUPS = {
|
|
4
4
|
agent: { cancel: "agent_cancel", list: "agent_list", prompt: "agent_prompt", sessions: "agent_sessions" },
|
|
@@ -6,14 +6,14 @@ export const GROUPS = {
|
|
|
6
6
|
control: { release: "control_release" },
|
|
7
7
|
disk: { clean: "disk_clean", usage: "disk_usage" },
|
|
8
8
|
exec: { run: "exec_run" },
|
|
9
|
-
fs: { delete: "fs_delete", edit: "fs_edit", list: "fs_list", read: "fs_read", search: "fs_search", stat: "fs_stat", write: "fs_write" },
|
|
9
|
+
fs: { delete: "fs_delete", edit: "fs_edit", list: "fs_list", read: "fs_read", search: "fs_search", stat: "fs_stat", upload_abort: "fs_upload_abort", upload_begin: "fs_upload_begin", upload_chunk: "fs_upload_chunk", upload_commit: "fs_upload_commit", upload_status: "fs_upload_status", write: "fs_write" },
|
|
10
10
|
journal: { search: "journal_search" },
|
|
11
11
|
key: { press: "key_press" },
|
|
12
12
|
pointer: { click: "pointer_click", drag: "pointer_drag", move: "pointer_move", scroll: "pointer_scroll" },
|
|
13
13
|
proc: { kill: "proc_kill", list: "proc_list" },
|
|
14
14
|
screen: { capture: "screen_capture", info: "screen_info" },
|
|
15
15
|
stage: { show: "stage_show" },
|
|
16
|
-
sys: { info: "sys_info" },
|
|
16
|
+
sys: { info: "sys_info", metrics: "sys_metrics" },
|
|
17
17
|
task: { output: "task_output", start: "task_start", stop: "task_stop" },
|
|
18
18
|
text: { type: "text_type" },
|
|
19
19
|
ui: { act: "ui_act", snapshot: "ui_snapshot", wait: "ui_wait" },
|
|
@@ -314,7 +314,8 @@ export type FsSearchOut = {
|
|
|
314
314
|
/** Arguments of `fs_stat`. */
|
|
315
315
|
export type FsStatArgs = {
|
|
316
316
|
/**
|
|
317
|
-
* Also return the file's SHA-256
|
|
317
|
+
* Also return the file's SHA-256: up to 64 MiB on program 0.1.16,
|
|
318
|
+
* or 256 MiB from program 0.1.18. Pass it as `expect_sha256` to
|
|
318
319
|
* `fs_write` or `fs_edit` to refuse a write when the file changed in between.
|
|
319
320
|
*/
|
|
320
321
|
hash?: boolean;
|
|
@@ -334,13 +335,160 @@ export type FsStatOut = {
|
|
|
334
335
|
modified?: number | null;
|
|
335
336
|
/** Resolved absolute path (symlinks followed). */
|
|
336
337
|
path: string;
|
|
337
|
-
/**
|
|
338
|
+
/**
|
|
339
|
+
* Lower-case hex when `hash` was asked: files up to 64 MiB on program 0.1.16,
|
|
340
|
+
* or 256 MiB from program 0.1.18.
|
|
341
|
+
*/
|
|
338
342
|
sha256?: string | null;
|
|
339
343
|
/** Bytes; 0 for folders. */
|
|
340
344
|
size: number;
|
|
341
345
|
/** Whether the user the machine program runs as may write it. */
|
|
342
346
|
writable: boolean;
|
|
343
347
|
};
|
|
348
|
+
/** Arguments of `fs_upload_abort`. Stop an unfinished upload and remove only its owned temporary data. */
|
|
349
|
+
export type FsUploadAbortArgs = {
|
|
350
|
+
/** The id bound by `fs_upload_begin`, owned by this same authenticated caller. */
|
|
351
|
+
transfer: string;
|
|
352
|
+
};
|
|
353
|
+
/** What `fs_upload_abort` returns. */
|
|
354
|
+
export type FsUploadAbortOut = {
|
|
355
|
+
/** Maximum decoded bytes in a chunk. */
|
|
356
|
+
chunk_max_bytes: number;
|
|
357
|
+
/** Fixed expiry as Unix seconds. */
|
|
358
|
+
expires_at: number;
|
|
359
|
+
/** Durable confirmed byte offset. Only continue from this value. */
|
|
360
|
+
offset: number;
|
|
361
|
+
/** Bound canonical absolute destination. */
|
|
362
|
+
path: string;
|
|
363
|
+
/** Present only after verified publication. */
|
|
364
|
+
receipt?: FsWriteOut | null;
|
|
365
|
+
/** Expected whole-file SHA-256; a committed receipt confirms verification. */
|
|
366
|
+
sha256: string;
|
|
367
|
+
/** Expected whole-file decoded bytes. */
|
|
368
|
+
size: number;
|
|
369
|
+
/** Current durable state. */
|
|
370
|
+
state: UploadState;
|
|
371
|
+
/** Client-generated upload id. */
|
|
372
|
+
transfer: string;
|
|
373
|
+
};
|
|
374
|
+
/** Arguments of `fs_upload_begin`. Reserve a new destination and bind the expected content to this caller. */
|
|
375
|
+
export type FsUploadBeginArgs = {
|
|
376
|
+
/** Create missing parent folders. Default: false. */
|
|
377
|
+
mkdirs?: boolean;
|
|
378
|
+
/** Destination on the machine. Existing files are never replaced. */
|
|
379
|
+
path: string;
|
|
380
|
+
/** Expected whole-file SHA-256: 64 lowercase hexadecimal characters. */
|
|
381
|
+
sha256: string;
|
|
382
|
+
/** Exact decoded byte count, at most 268435456 (256 MiB). */
|
|
383
|
+
size: number;
|
|
384
|
+
/** Client-generated id: 32 lowercase hexadecimal characters. Reuse only for this upload. */
|
|
385
|
+
transfer: string;
|
|
386
|
+
};
|
|
387
|
+
/** What `fs_upload_begin` returns. */
|
|
388
|
+
export type FsUploadBeginOut = {
|
|
389
|
+
/** Maximum decoded bytes in a chunk. */
|
|
390
|
+
chunk_max_bytes: number;
|
|
391
|
+
/** Fixed expiry as Unix seconds. */
|
|
392
|
+
expires_at: number;
|
|
393
|
+
/** Durable confirmed byte offset. Only continue from this value. */
|
|
394
|
+
offset: number;
|
|
395
|
+
/** Bound canonical absolute destination. */
|
|
396
|
+
path: string;
|
|
397
|
+
/** Present only after verified publication. */
|
|
398
|
+
receipt?: FsWriteOut | null;
|
|
399
|
+
/** Expected whole-file SHA-256; a committed receipt confirms verification. */
|
|
400
|
+
sha256: string;
|
|
401
|
+
/** Expected whole-file decoded bytes. */
|
|
402
|
+
size: number;
|
|
403
|
+
/** Current durable state. */
|
|
404
|
+
state: UploadState;
|
|
405
|
+
/** Client-generated upload id. */
|
|
406
|
+
transfer: string;
|
|
407
|
+
};
|
|
408
|
+
/** Arguments of `fs_upload_chunk`. Supply the next verified chunk, or reconcile an exact duplicate. */
|
|
409
|
+
export type FsUploadChunkArgs = {
|
|
410
|
+
/** Base64-encoded bytes, at most 1048576 (1 MiB) decoded. Produce them in code, not by hand. */
|
|
411
|
+
data: string;
|
|
412
|
+
/** Start at the receiver-confirmed decoded byte offset. */
|
|
413
|
+
offset: number;
|
|
414
|
+
/** SHA-256 of these decoded bytes: 64 lowercase hexadecimal characters. */
|
|
415
|
+
sha256: string;
|
|
416
|
+
/** The id bound by `fs_upload_begin`, owned by this same authenticated caller. */
|
|
417
|
+
transfer: string;
|
|
418
|
+
};
|
|
419
|
+
/** What `fs_upload_chunk` returns. */
|
|
420
|
+
export type FsUploadChunkOut = {
|
|
421
|
+
/** Maximum decoded bytes in a chunk. */
|
|
422
|
+
chunk_max_bytes: number;
|
|
423
|
+
/** Fixed expiry as Unix seconds. */
|
|
424
|
+
expires_at: number;
|
|
425
|
+
/** Durable confirmed byte offset. Only continue from this value. */
|
|
426
|
+
offset: number;
|
|
427
|
+
/** Bound canonical absolute destination. */
|
|
428
|
+
path: string;
|
|
429
|
+
/** Present only after verified publication. */
|
|
430
|
+
receipt?: FsWriteOut | null;
|
|
431
|
+
/** Expected whole-file SHA-256; a committed receipt confirms verification. */
|
|
432
|
+
sha256: string;
|
|
433
|
+
/** Expected whole-file decoded bytes. */
|
|
434
|
+
size: number;
|
|
435
|
+
/** Current durable state. */
|
|
436
|
+
state: UploadState;
|
|
437
|
+
/** Client-generated upload id. */
|
|
438
|
+
transfer: string;
|
|
439
|
+
};
|
|
440
|
+
/** Arguments of `fs_upload_commit`. Verify the complete file and publish it without replacing a destination. */
|
|
441
|
+
export type FsUploadCommitArgs = {
|
|
442
|
+
/** The id bound by `fs_upload_begin`, owned by this same authenticated caller. */
|
|
443
|
+
transfer: string;
|
|
444
|
+
};
|
|
445
|
+
/** What `fs_upload_commit` returns. */
|
|
446
|
+
export type FsUploadCommitOut = {
|
|
447
|
+
/** Maximum decoded bytes in a chunk. */
|
|
448
|
+
chunk_max_bytes: number;
|
|
449
|
+
/** Fixed expiry as Unix seconds. */
|
|
450
|
+
expires_at: number;
|
|
451
|
+
/** Durable confirmed byte offset. Only continue from this value. */
|
|
452
|
+
offset: number;
|
|
453
|
+
/** Bound canonical absolute destination. */
|
|
454
|
+
path: string;
|
|
455
|
+
/** Present only after verified publication. */
|
|
456
|
+
receipt?: FsWriteOut | null;
|
|
457
|
+
/** Expected whole-file SHA-256; a committed receipt confirms verification. */
|
|
458
|
+
sha256: string;
|
|
459
|
+
/** Expected whole-file decoded bytes. */
|
|
460
|
+
size: number;
|
|
461
|
+
/** Current durable state. */
|
|
462
|
+
state: UploadState;
|
|
463
|
+
/** Client-generated upload id. */
|
|
464
|
+
transfer: string;
|
|
465
|
+
};
|
|
466
|
+
/** Arguments of `fs_upload_status`. Inspect an owned upload's durable offset or terminal receipt. */
|
|
467
|
+
export type FsUploadStatusArgs = {
|
|
468
|
+
/** The id bound by `fs_upload_begin`, owned by this same authenticated caller. */
|
|
469
|
+
transfer: string;
|
|
470
|
+
};
|
|
471
|
+
/** What `fs_upload_status` returns. */
|
|
472
|
+
export type FsUploadStatusOut = {
|
|
473
|
+
/** Maximum decoded bytes in a chunk. */
|
|
474
|
+
chunk_max_bytes: number;
|
|
475
|
+
/** Fixed expiry as Unix seconds. */
|
|
476
|
+
expires_at: number;
|
|
477
|
+
/** Durable confirmed byte offset. Only continue from this value. */
|
|
478
|
+
offset: number;
|
|
479
|
+
/** Bound canonical absolute destination. */
|
|
480
|
+
path: string;
|
|
481
|
+
/** Present only after verified publication. */
|
|
482
|
+
receipt?: FsWriteOut | null;
|
|
483
|
+
/** Expected whole-file SHA-256; a committed receipt confirms verification. */
|
|
484
|
+
sha256: string;
|
|
485
|
+
/** Expected whole-file decoded bytes. */
|
|
486
|
+
size: number;
|
|
487
|
+
/** Current durable state. */
|
|
488
|
+
state: UploadState;
|
|
489
|
+
/** Client-generated upload id. */
|
|
490
|
+
transfer: string;
|
|
491
|
+
};
|
|
344
492
|
/** Arguments of `fs_write`. */
|
|
345
493
|
export type FsWriteArgs = {
|
|
346
494
|
/** Refuse when the file already exists. */
|
|
@@ -655,6 +803,30 @@ export type SysInfoOut = {
|
|
|
655
803
|
/** The user the machine program runs as. */
|
|
656
804
|
user?: string | null;
|
|
657
805
|
};
|
|
806
|
+
/** Arguments of `sys_metrics`. One bounded read; no persistent sampling or caller-selected paths. */
|
|
807
|
+
export type SysMetricsArgs = Record<string, never>;
|
|
808
|
+
/** What `sys_metrics` returns. */
|
|
809
|
+
export type SysMetricsOut = {
|
|
810
|
+
/** Unix milliseconds when the observation request completed. */
|
|
811
|
+
observed_at_ms: number;
|
|
812
|
+
/**
|
|
813
|
+
* Raw charged CPU-time delta and local quota, only for a qualified visible
|
|
814
|
+
* cgroup-v2 root. This is not a host percentage or effective ancestor entitlement.
|
|
815
|
+
*/
|
|
816
|
+
visible_cgroup_cpu?: CgroupCpuObservation | null;
|
|
817
|
+
/**
|
|
818
|
+
* Current process's cgroup memory limits across the visible hierarchy, if known.
|
|
819
|
+
* Neither a host memory reading nor a guarantee of all hidden ancestor limits.
|
|
820
|
+
*/
|
|
821
|
+
visible_cgroup_memory?: CgroupMemoryObservation | null;
|
|
822
|
+
/**
|
|
823
|
+
* CPU execution percentage across the visible system, not the caller's cgroup quota.
|
|
824
|
+
* Absent if a valid counter delta could not be established.
|
|
825
|
+
*/
|
|
826
|
+
visible_system_cpu?: CpuObservation | null;
|
|
827
|
+
/** Visible system memory, as reported by the OS/library; not container entitlement. */
|
|
828
|
+
visible_system_memory?: MemoryObservation | null;
|
|
829
|
+
};
|
|
658
830
|
/** Arguments of `task_output`. */
|
|
659
831
|
export type TaskOutputArgs = {
|
|
660
832
|
/** Return only the last this many lines (default 200, 0 for all the machine keeps). */
|
|
@@ -809,6 +981,23 @@ export type AgentSession = {
|
|
|
809
981
|
updated: number;
|
|
810
982
|
};
|
|
811
983
|
export type Button = "left" | "right" | "middle";
|
|
984
|
+
export type CgroupCpuObservation = {
|
|
985
|
+
interval_usec: number;
|
|
986
|
+
period_usec: number;
|
|
987
|
+
quota_usec: number;
|
|
988
|
+
usage_usec_delta: number;
|
|
989
|
+
};
|
|
990
|
+
export type CgroupMemoryObservation = {
|
|
991
|
+
/** Remaining charge headroom under the observed limits, not free/reclaimable RAM. */
|
|
992
|
+
headroom_bytes: number;
|
|
993
|
+
limit_bytes: number;
|
|
994
|
+
};
|
|
995
|
+
export type CpuObservation = {
|
|
996
|
+
/** Monotonic elapsed time between the two successful reads, in milliseconds. */
|
|
997
|
+
interval_ms: number;
|
|
998
|
+
/** Rounded 0–100 percentage spent executing, excluding idle, I/O wait and steal. */
|
|
999
|
+
percent: number;
|
|
1000
|
+
};
|
|
812
1001
|
export type Disk = {
|
|
813
1002
|
available: number;
|
|
814
1003
|
mount: string;
|
|
@@ -883,6 +1072,10 @@ export type Image = {
|
|
|
883
1072
|
/** Which journal lines [`JournalSearch`] returns. */
|
|
884
1073
|
export type JournalKind = "call" | "task";
|
|
885
1074
|
export type KillSignal = "term" | "int" | "hup" | "kill";
|
|
1075
|
+
export type MemoryObservation = {
|
|
1076
|
+
available_bytes: number;
|
|
1077
|
+
total_bytes: number;
|
|
1078
|
+
};
|
|
886
1079
|
export type Modifier = ("shift" | "ctrl" | "alt") | "meta";
|
|
887
1080
|
/** The state after an action, as asked with `observe`. */
|
|
888
1081
|
export type Observation = {
|
|
@@ -951,6 +1144,8 @@ export type StageOutcome = "answered" | "closed" | "timeout" | "paused";
|
|
|
951
1144
|
export type Tier = "text_only" | "read_only";
|
|
952
1145
|
export type TypeMethod = "auto" | "keys" | "paste";
|
|
953
1146
|
export type UiAction = ("focus" | "toggle" | "expand" | "collapse") | "invoke" | "set_value" | "select_text";
|
|
1147
|
+
/** The durable stage of an upload, independent of an individual call. */
|
|
1148
|
+
export type UploadState = "uploading" | "committed" | "aborted" | "expired";
|
|
954
1149
|
export type WaitFor = ("element_gone" | "text_gone") | "element_appears" | "text_appears" | "window_appears" | "screen_stable";
|
|
955
1150
|
export type WindowInfo = {
|
|
956
1151
|
app: string;
|
|
@@ -1046,6 +1241,26 @@ export interface ToolMap {
|
|
|
1046
1241
|
args: FsStatArgs;
|
|
1047
1242
|
out: FsStatOut;
|
|
1048
1243
|
};
|
|
1244
|
+
fs_upload_abort: {
|
|
1245
|
+
args: FsUploadAbortArgs;
|
|
1246
|
+
out: FsUploadAbortOut;
|
|
1247
|
+
};
|
|
1248
|
+
fs_upload_begin: {
|
|
1249
|
+
args: FsUploadBeginArgs;
|
|
1250
|
+
out: FsUploadBeginOut;
|
|
1251
|
+
};
|
|
1252
|
+
fs_upload_chunk: {
|
|
1253
|
+
args: FsUploadChunkArgs;
|
|
1254
|
+
out: FsUploadChunkOut;
|
|
1255
|
+
};
|
|
1256
|
+
fs_upload_commit: {
|
|
1257
|
+
args: FsUploadCommitArgs;
|
|
1258
|
+
out: FsUploadCommitOut;
|
|
1259
|
+
};
|
|
1260
|
+
fs_upload_status: {
|
|
1261
|
+
args: FsUploadStatusArgs;
|
|
1262
|
+
out: FsUploadStatusOut;
|
|
1263
|
+
};
|
|
1049
1264
|
fs_write: {
|
|
1050
1265
|
args: FsWriteArgs;
|
|
1051
1266
|
out: FsWriteOut;
|
|
@@ -1098,6 +1313,10 @@ export interface ToolMap {
|
|
|
1098
1313
|
args: SysInfoArgs;
|
|
1099
1314
|
out: SysInfoOut;
|
|
1100
1315
|
};
|
|
1316
|
+
sys_metrics: {
|
|
1317
|
+
args: SysMetricsArgs;
|
|
1318
|
+
out: SysMetricsOut;
|
|
1319
|
+
};
|
|
1101
1320
|
task_output: {
|
|
1102
1321
|
args: TaskOutputArgs;
|
|
1103
1322
|
out: TaskOutputOut;
|
|
@@ -1224,6 +1443,36 @@ export declare const TOOLS: {
|
|
|
1224
1443
|
readonly timeoutSecs: 30;
|
|
1225
1444
|
readonly description: "Facts about a file or folder without reading it: kind, size, times, mode, writable, symlink target. hash=true adds its SHA-256 for expect_sha256. Paths holding keys and credentials are refused.";
|
|
1226
1445
|
};
|
|
1446
|
+
readonly fs_upload_abort: {
|
|
1447
|
+
readonly risk: "write";
|
|
1448
|
+
readonly added: "0.1.18";
|
|
1449
|
+
readonly timeoutSecs: 60;
|
|
1450
|
+
readonly description: "Stop your unfinished upload and clean its temporary bytes. A completed upload returns its receipt and keeps the destination file.";
|
|
1451
|
+
};
|
|
1452
|
+
readonly fs_upload_begin: {
|
|
1453
|
+
readonly risk: "write";
|
|
1454
|
+
readonly added: "0.1.18";
|
|
1455
|
+
readonly timeoutSecs: 60;
|
|
1456
|
+
readonly description: "Begin a resumable create-only upload, up to 256 MiB. Bind a client-generated id, size and SHA-256; expires in 15 minutes. Use SDK upload helpers for bytes.";
|
|
1457
|
+
};
|
|
1458
|
+
readonly fs_upload_chunk: {
|
|
1459
|
+
readonly risk: "write";
|
|
1460
|
+
readonly added: "0.1.18";
|
|
1461
|
+
readonly timeoutSecs: 60;
|
|
1462
|
+
readonly description: "Supply up to 1 MiB of verified file bytes at the confirmed offset. After an uncertain reply, inspect upload status before continuing; never guess or replay blindly.";
|
|
1463
|
+
};
|
|
1464
|
+
readonly fs_upload_commit: {
|
|
1465
|
+
readonly risk: "write";
|
|
1466
|
+
readonly added: "0.1.18";
|
|
1467
|
+
readonly timeoutSecs: 120;
|
|
1468
|
+
readonly description: "Verify size and whole-file SHA-256, then create the destination without overwriting. A lost reply is reconciled through upload status; success includes a file receipt.";
|
|
1469
|
+
};
|
|
1470
|
+
readonly fs_upload_status: {
|
|
1471
|
+
readonly risk: "write";
|
|
1472
|
+
readonly added: "0.1.18";
|
|
1473
|
+
readonly timeoutSecs: 60;
|
|
1474
|
+
readonly description: "Inspect your upload's durable offset or final receipt, including after a lost reply or machine restart. Requires the original caller and current write permission.";
|
|
1475
|
+
};
|
|
1227
1476
|
readonly fs_write: {
|
|
1228
1477
|
readonly risk: "write";
|
|
1229
1478
|
readonly added: "0.0.1";
|
|
@@ -1302,6 +1551,12 @@ export declare const TOOLS: {
|
|
|
1302
1551
|
readonly timeoutSecs: 15;
|
|
1303
1552
|
readonly description: "What the machine is: host name, OS and version, architecture, CPUs, memory, disks with free space, uptime, the user cmdop runs as and its home folder. Call it before choosing commands for a machine.";
|
|
1304
1553
|
};
|
|
1554
|
+
readonly sys_metrics: {
|
|
1555
|
+
readonly risk: "read";
|
|
1556
|
+
readonly added: "0.1.18";
|
|
1557
|
+
readonly timeoutSecs: 10;
|
|
1558
|
+
readonly description: "CPU and RAM sample on Linux/macOS. System values are not container limits; cgroup quota and memory headroom are separate. Missing fields are unavailable.";
|
|
1559
|
+
};
|
|
1305
1560
|
readonly task_output: {
|
|
1306
1561
|
readonly risk: "read";
|
|
1307
1562
|
readonly added: "0.0.1";
|
|
@@ -1351,4 +1606,4 @@ export declare const TOOLS: {
|
|
|
1351
1606
|
readonly description: "Windows of the apps on screen, front to back: id, app, title, bounds in screen points, focus, whether visible, and whether `control` is allowed for the app.";
|
|
1352
1607
|
};
|
|
1353
1608
|
};
|
|
1354
|
-
export declare const TOOL_NAMES: readonly ["agent_cancel", "agent_list", "agent_prompt", "agent_sessions", "app_open", "control_release", "disk_clean", "disk_usage", "exec_run", "fs_delete", "fs_edit", "fs_list", "fs_read", "fs_search", "fs_stat", "fs_write", "journal_search", "key_press", "pointer_click", "pointer_drag", "pointer_move", "pointer_scroll", "proc_kill", "proc_list", "screen_capture", "screen_info", "stage_show", "sys_info", "task_output", "task_start", "task_stop", "text_type", "ui_act", "ui_snapshot", "ui_wait", "window_list"];
|
|
1609
|
+
export declare const TOOL_NAMES: readonly ["agent_cancel", "agent_list", "agent_prompt", "agent_sessions", "app_open", "control_release", "disk_clean", "disk_usage", "exec_run", "fs_delete", "fs_edit", "fs_list", "fs_read", "fs_search", "fs_stat", "fs_upload_abort", "fs_upload_begin", "fs_upload_chunk", "fs_upload_commit", "fs_upload_status", "fs_write", "journal_search", "key_press", "pointer_click", "pointer_drag", "pointer_move", "pointer_scroll", "proc_kill", "proc_list", "screen_capture", "screen_info", "stage_show", "sys_info", "sys_metrics", "task_output", "task_start", "task_stop", "text_type", "ui_act", "ui_snapshot", "ui_wait", "window_list"];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated from cmdop/crates/tools/tools.schema.json (sha256
|
|
1
|
+
// Generated from cmdop/crates/tools/tools.schema.json (sha256 d1db4922f4b6dd14, cmdop 0.1.18) by exports/sdk/gen/gen.ts. Do not edit.
|
|
2
2
|
/** Risk class, version and description of every tool, as the server lists them. */
|
|
3
3
|
export const TOOLS = {
|
|
4
4
|
agent_cancel: { risk: "agent", added: "0.1.13", timeoutSecs: 15, description: "Stops a running agent_prompt by its task. What the agent did until then stays done." },
|
|
@@ -16,6 +16,11 @@ export const TOOLS = {
|
|
|
16
16
|
fs_read: { risk: "read", added: "0.0.1", timeoutSecs: 30, description: "Read a file as stored: text as text, other bytes as base64, 256 KiB per call by default. `truncated` means there is more: read on with offset. Paths holding keys and credentials are refused." },
|
|
17
17
|
fs_search: { risk: "read", added: "0.0.1", timeoutSecs: 30, description: "Find files under a folder by name pattern (`*`, `?`) and/or plain text inside them: paths, and with content the first matching line. Bounded by depth, match count and 20 s." },
|
|
18
18
|
fs_stat: { risk: "read", added: "0.0.1", timeoutSecs: 30, description: "Facts about a file or folder without reading it: kind, size, times, mode, writable, symlink target. hash=true adds its SHA-256 for expect_sha256. Paths holding keys and credentials are refused." },
|
|
19
|
+
fs_upload_abort: { risk: "write", added: "0.1.18", timeoutSecs: 60, description: "Stop your unfinished upload and clean its temporary bytes. A completed upload returns its receipt and keeps the destination file." },
|
|
20
|
+
fs_upload_begin: { risk: "write", added: "0.1.18", timeoutSecs: 60, description: "Begin a resumable create-only upload, up to 256 MiB. Bind a client-generated id, size and SHA-256; expires in 15 minutes. Use SDK upload helpers for bytes." },
|
|
21
|
+
fs_upload_chunk: { risk: "write", added: "0.1.18", timeoutSecs: 60, description: "Supply up to 1 MiB of verified file bytes at the confirmed offset. After an uncertain reply, inspect upload status before continuing; never guess or replay blindly." },
|
|
22
|
+
fs_upload_commit: { risk: "write", added: "0.1.18", timeoutSecs: 120, description: "Verify size and whole-file SHA-256, then create the destination without overwriting. A lost reply is reconciled through upload status; success includes a file receipt." },
|
|
23
|
+
fs_upload_status: { risk: "write", added: "0.1.18", timeoutSecs: 60, description: "Inspect your upload's durable offset or final receipt, including after a lost reply or machine restart. Requires the original caller and current write permission." },
|
|
19
24
|
fs_write: { risk: "write", added: "0.0.1", timeoutSecs: 60, description: "Create a file or replace it whole, atomically; text or base64, up to 8 MiB. Replacing one needs expect_sha256, or the answer is hash_required. Paths holding keys and credentials are refused." },
|
|
20
25
|
journal_search: { risk: "read", added: "0.0.2", timeoutSecs: 20, description: "What was done on one machine, kept on it: tool calls with their arguments as kept and task starts and ends, by author, tool, task, time and text. No output." },
|
|
21
26
|
key_press: { risk: "control", added: "0.0.2", timeoutSecs: 20, description: "Press one key with optional modifiers, to the frontmost app. For text use `text_type`. Shortcuts: `key: \"s\", modifiers: [\"meta\"]`." },
|
|
@@ -29,6 +34,7 @@ export const TOOLS = {
|
|
|
29
34
|
screen_info: { risk: "screen", added: "0.0.2", timeoutSecs: 10, description: "Displays with size and scale, the frontmost app, the cursor, screen lock, whether capture and input are granted and whether control is paused. Text only. Call it first in a session." },
|
|
30
35
|
stage_show: { risk: "control", added: "0.1.16", timeoutSecs: 55, description: "Plays CMDOP's opening on the machine's screens, then shows your text and question as buttons. Waits up to 45 s for a pick; returns it and the machine's details." },
|
|
31
36
|
sys_info: { risk: "read", added: "0.0.1", timeoutSecs: 15, description: "What the machine is: host name, OS and version, architecture, CPUs, memory, disks with free space, uptime, the user cmdop runs as and its home folder. Call it before choosing commands for a machine." },
|
|
37
|
+
sys_metrics: { risk: "read", added: "0.1.18", timeoutSecs: 10, description: "CPU and RAM sample on Linux/macOS. System values are not container limits; cgroup quota and memory headroom are separate. Missing fields are unavailable." },
|
|
32
38
|
task_output: { risk: "read", added: "0.0.1", timeoutSecs: 10, description: "A task's screen as text, whether it runs. The last 200 lines by default; the machine keeps 1000 lines of its last 50 tasks until it restarts. What ran: journal_search." },
|
|
33
39
|
task_start: { risk: "exec", added: "0.0.1", timeoutSecs: 10, description: "Start a command in a terminal on the machine; it keeps running after the call and the conversation end. Read it with task_output, end it with task_stop. For an exit code, use exec_run." },
|
|
34
40
|
task_stop: { risk: "exec", added: "0.0.1", timeoutSecs: 10, description: "Stop a task: SIGTERM to its process group, SIGKILL after 5 seconds. The ending shows in tasks_list and history_search a moment later." },
|
|
@@ -38,4 +44,4 @@ export const TOOLS = {
|
|
|
38
44
|
ui_wait: { risk: "screen", added: "0.0.2", timeoutSecs: 35, description: "Wait until an element, text or window appears or is gone, or the screen is stable, for at most 30 s. A timeout is an answer (`met: false`), not an error." },
|
|
39
45
|
window_list: { risk: "screen", added: "0.0.2", timeoutSecs: 10, description: "Windows of the apps on screen, front to back: id, app, title, bounds in screen points, focus, whether visible, and whether `control` is allowed for the app." },
|
|
40
46
|
};
|
|
41
|
-
export const TOOL_NAMES = ["agent_cancel", "agent_list", "agent_prompt", "agent_sessions", "app_open", "control_release", "disk_clean", "disk_usage", "exec_run", "fs_delete", "fs_edit", "fs_list", "fs_read", "fs_search", "fs_stat", "fs_write", "journal_search", "key_press", "pointer_click", "pointer_drag", "pointer_move", "pointer_scroll", "proc_kill", "proc_list", "screen_capture", "screen_info", "stage_show", "sys_info", "task_output", "task_start", "task_stop", "text_type", "ui_act", "ui_snapshot", "ui_wait", "window_list"];
|
|
47
|
+
export const TOOL_NAMES = ["agent_cancel", "agent_list", "agent_prompt", "agent_sessions", "app_open", "control_release", "disk_clean", "disk_usage", "exec_run", "fs_delete", "fs_edit", "fs_list", "fs_read", "fs_search", "fs_stat", "fs_upload_abort", "fs_upload_begin", "fs_upload_chunk", "fs_upload_commit", "fs_upload_status", "fs_write", "journal_search", "key_press", "pointer_click", "pointer_drag", "pointer_move", "pointer_scroll", "proc_kill", "proc_list", "screen_capture", "screen_info", "stage_show", "sys_info", "sys_metrics", "task_output", "task_start", "task_stop", "text_type", "ui_act", "ui_snapshot", "ui_wait", "window_list"];
|
package/dist/index.d.ts
CHANGED
|
@@ -8,3 +8,4 @@ export { GROUPS, type GroupName } from "./generated/surface.gen.ts";
|
|
|
8
8
|
export { SERVER_TOOLS, SERVER_TOOL_NAMES, type ServerToolMap, type ServerToolName } from "./generated/server.gen.ts";
|
|
9
9
|
export { decodeImage, toScreenshot, type DecodedImage, type Screenshot } from "./screen.ts";
|
|
10
10
|
export { anthropicToCalls, openaiToCalls, performCalls, UnsupportedAction, type Step, type VendorOptions } from "./vendor.ts";
|
|
11
|
+
export { uploadFile, UploadError, type UploadOptions, type UploadProgress } from "./upload.ts";
|
package/dist/index.js
CHANGED
|
@@ -8,3 +8,4 @@ export { GROUPS } from "./generated/surface.gen.js";
|
|
|
8
8
|
export { SERVER_TOOLS, SERVER_TOOL_NAMES } from "./generated/server.gen.js";
|
|
9
9
|
export { decodeImage, toScreenshot } from "./screen.js";
|
|
10
10
|
export { anthropicToCalls, openaiToCalls, performCalls, UnsupportedAction } from "./vendor.js";
|
|
11
|
+
export { uploadFile, UploadError } from "./upload.js";
|
package/dist/upload.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Bounded local-file upload for Node.js. The generated tools own the wire contract. */
|
|
2
|
+
import { type Machine } from "./client.ts";
|
|
3
|
+
import type { FsUploadBeginOut, FsUploadChunkOut, FsUploadCommitOut, FsUploadStatusOut } from "./generated/tools.gen.ts";
|
|
4
|
+
type UploadOut = FsUploadBeginOut | FsUploadChunkOut | FsUploadCommitOut | FsUploadStatusOut;
|
|
5
|
+
/** Metadata only. "prepared" is local; "committed" requires a verified receiver receipt. */
|
|
6
|
+
export interface UploadProgress {
|
|
7
|
+
readonly transfer: string;
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly size: number;
|
|
10
|
+
readonly sha256: string;
|
|
11
|
+
readonly offset: number;
|
|
12
|
+
readonly state: "prepared" | UploadOut["state"];
|
|
13
|
+
}
|
|
14
|
+
export interface UploadOptions {
|
|
15
|
+
/** Save and reuse this 32-character lowercase hex id with the same caller, machine, source and destination. */
|
|
16
|
+
transfer?: string;
|
|
17
|
+
mkdirs?: boolean;
|
|
18
|
+
/** Receives metadata, never file bytes. The first event runs before begin. */
|
|
19
|
+
onProgress?: (progress: UploadProgress) => void;
|
|
20
|
+
/** Per-call deadline in milliseconds, 30 seconds by default. */
|
|
21
|
+
deadline?: number;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
/** The upload stopped. last is observed metadata, not proof a timed-out mutation did nothing. */
|
|
25
|
+
export declare class UploadError extends Error {
|
|
26
|
+
readonly transfer: string;
|
|
27
|
+
readonly last?: UploadProgress;
|
|
28
|
+
constructor(message: string, transfer: string, last?: UploadProgress, cause?: unknown);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a remote file from a local regular file using at most 1 MiB per chunk.
|
|
32
|
+
*
|
|
33
|
+
* Requires Node.js, an upload-capable machine and write authority. Destination must be
|
|
34
|
+
* absolute and must not exist. The file is read twice, never loaded whole. Files above
|
|
35
|
+
* this helper's compiled 256 MiB budget are refused before hashing. Save transfer,
|
|
36
|
+
* or the first progress event, before begin so another process can resume explicitly.
|
|
37
|
+
* A lost chunk/commit answer triggers status; this helper never replays those mutations.
|
|
38
|
+
* Node builtins load only when this helper is called.
|
|
39
|
+
*/
|
|
40
|
+
export declare function uploadFile(machine: Machine, sourcePath: string, destinationPath: string, options?: UploadOptions): Promise<FsUploadCommitOut>;
|
|
41
|
+
export {};
|
package/dist/upload.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/** Bounded local-file upload for Node.js. The generated tools own the wire contract. */
|
|
2
|
+
import { CmdopError } from "./client.js";
|
|
3
|
+
// Local memory budget; a smaller receiver limit always wins.
|
|
4
|
+
const BLOCK_BYTES = 1024 * 1024;
|
|
5
|
+
// Compiled preflight budget for this helper; the receiver still enforces its own limit.
|
|
6
|
+
const FILE_MAX_BYTES = 256 * BLOCK_BYTES;
|
|
7
|
+
/** The upload stopped. last is observed metadata, not proof a timed-out mutation did nothing. */
|
|
8
|
+
export class UploadError extends Error {
|
|
9
|
+
transfer;
|
|
10
|
+
last;
|
|
11
|
+
constructor(message, transfer, last, cause) {
|
|
12
|
+
super(message, cause === undefined ? undefined : { cause });
|
|
13
|
+
this.name = "UploadError";
|
|
14
|
+
this.transfer = transfer;
|
|
15
|
+
this.last = last;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function uncertain(error) {
|
|
19
|
+
// Definitive refusals cannot become success of a previous transfer with the same id.
|
|
20
|
+
return !(error instanceof CmdopError) || error.details?.upload_outcome === "unknown"
|
|
21
|
+
|| ["timed_out", "cancelled", "failed", "offline"].includes(error.code);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create a remote file from a local regular file using at most 1 MiB per chunk.
|
|
25
|
+
*
|
|
26
|
+
* Requires Node.js, an upload-capable machine and write authority. Destination must be
|
|
27
|
+
* absolute and must not exist. The file is read twice, never loaded whole. Files above
|
|
28
|
+
* this helper's compiled 256 MiB budget are refused before hashing. Save transfer,
|
|
29
|
+
* or the first progress event, before begin so another process can resume explicitly.
|
|
30
|
+
* A lost chunk/commit answer triggers status; this helper never replays those mutations.
|
|
31
|
+
* Node builtins load only when this helper is called.
|
|
32
|
+
*/
|
|
33
|
+
export async function uploadFile(machine, sourcePath, destinationPath, options = {}) {
|
|
34
|
+
const { open } = await import("node:fs/promises");
|
|
35
|
+
const { createHash, randomBytes } = await import("node:crypto");
|
|
36
|
+
const { posix, win32 } = await import("node:path");
|
|
37
|
+
const absolute = (path) => posix.isAbsolute(path) || win32.isAbsolute(path);
|
|
38
|
+
const transfer = options.transfer ?? randomBytes(16).toString("hex");
|
|
39
|
+
if (!/^[0-9a-f]{32}$/.test(transfer))
|
|
40
|
+
throw new TypeError("transfer must be 32 lowercase hexadecimal characters");
|
|
41
|
+
if (!absolute(destinationPath))
|
|
42
|
+
throw new TypeError("upload destination must be an absolute path on the machine");
|
|
43
|
+
const deadline = options.deadline ?? 30_000;
|
|
44
|
+
if (!Number.isFinite(deadline) || deadline <= 0)
|
|
45
|
+
throw new TypeError("upload deadline must be positive and finite");
|
|
46
|
+
const callOptions = { deadline, signal: options.signal };
|
|
47
|
+
let last;
|
|
48
|
+
let boundPath;
|
|
49
|
+
let size = 0;
|
|
50
|
+
let sha256 = "";
|
|
51
|
+
const fail = (message, cause) => new UploadError(message, transfer, last, cause);
|
|
52
|
+
const checkAbort = () => {
|
|
53
|
+
if (options.signal?.aborted)
|
|
54
|
+
throw fail("upload stopped by its caller; inspect status before resuming", options.signal.reason);
|
|
55
|
+
};
|
|
56
|
+
const emit = () => {
|
|
57
|
+
if (last)
|
|
58
|
+
options.onProgress?.(last);
|
|
59
|
+
};
|
|
60
|
+
function accept(out) {
|
|
61
|
+
if (!out || (out.transfer !== transfer || out.size !== size || out.sha256 !== sha256)) {
|
|
62
|
+
throw fail("receiver upload identity does not match the source");
|
|
63
|
+
}
|
|
64
|
+
if (typeof out.path !== "string" || !absolute(out.path) || (boundPath !== undefined && out.path !== boundPath)) {
|
|
65
|
+
throw fail("receiver upload destination changed");
|
|
66
|
+
}
|
|
67
|
+
if (!Number.isSafeInteger(out.offset) || out.offset < 0 || out.offset > size
|
|
68
|
+
|| !Number.isSafeInteger(out.chunk_max_bytes) || out.chunk_max_bytes <= 0) {
|
|
69
|
+
throw fail("receiver returned invalid upload limits or offset");
|
|
70
|
+
}
|
|
71
|
+
if (out.state === "committed") {
|
|
72
|
+
const receipt = out.receipt;
|
|
73
|
+
if (!receipt || receipt.path !== out.path || receipt.size !== size || receipt.sha256 !== sha256
|
|
74
|
+
|| receipt.created !== true || out.offset !== size) {
|
|
75
|
+
throw fail("receiver commit receipt does not match the source");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (out.receipt != null) {
|
|
79
|
+
throw fail("receiver returned a receipt for an uncommitted upload");
|
|
80
|
+
}
|
|
81
|
+
if (!["uploading", "committed", "aborted", "expired"].includes(out.state)) {
|
|
82
|
+
throw fail("receiver returned an unknown upload state");
|
|
83
|
+
}
|
|
84
|
+
boundPath = out.path;
|
|
85
|
+
last = Object.freeze({ transfer, path: out.path, size, sha256, offset: out.offset, state: out.state });
|
|
86
|
+
if (out.state === "aborted" || out.state === "expired")
|
|
87
|
+
throw fail("upload is " + out.state + "; begin a new authorized transfer");
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
async function call(operation, mutate, expected) {
|
|
91
|
+
checkAbort();
|
|
92
|
+
try {
|
|
93
|
+
return await mutate();
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (error instanceof CmdopError) {
|
|
97
|
+
const details = error.details ?? {};
|
|
98
|
+
if ("transfer" in details && details.transfer !== transfer)
|
|
99
|
+
throw fail("receiver error names a different transfer", error);
|
|
100
|
+
if (details.upload_outcome === "unknown" && (details.transfer !== transfer || details.action !== "status")) {
|
|
101
|
+
throw fail("receiver returned an invalid upload recovery marker", error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!uncertain(error))
|
|
105
|
+
throw fail("upload was refused; inspect the original error before resuming", error);
|
|
106
|
+
let out;
|
|
107
|
+
try {
|
|
108
|
+
// A cancelled mutation still needs observation; its aborted signal must not cancel status.
|
|
109
|
+
out = accept(await machine.fs_upload_status({ transfer }, { deadline }));
|
|
110
|
+
}
|
|
111
|
+
catch (statusError) {
|
|
112
|
+
if (statusError instanceof UploadError)
|
|
113
|
+
throw statusError;
|
|
114
|
+
throw fail("upload outcome is unknown; query status with this transfer id", statusError);
|
|
115
|
+
}
|
|
116
|
+
if (out.state === "committed" && operation !== "begin")
|
|
117
|
+
return out;
|
|
118
|
+
checkAbort();
|
|
119
|
+
if (operation === "begin") {
|
|
120
|
+
// Status cannot prove how the requested absolute path resolved. Idempotent begin
|
|
121
|
+
// rechecks that binding only after status confirmed the existing transfer.
|
|
122
|
+
try {
|
|
123
|
+
return await mutate();
|
|
124
|
+
}
|
|
125
|
+
catch (bindingError) {
|
|
126
|
+
throw fail("upload destination binding is unconfirmed; retain this transfer id", bindingError);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (operation === "chunk" && out.offset === expected)
|
|
130
|
+
return out;
|
|
131
|
+
throw fail("upload outcome is not confirmed; resume with this transfer id after checking status", error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
checkAbort();
|
|
135
|
+
let file;
|
|
136
|
+
try {
|
|
137
|
+
file = await open(sourcePath, "r");
|
|
138
|
+
const source = file;
|
|
139
|
+
const initial = await source.stat({ bigint: true });
|
|
140
|
+
if (!initial.isFile())
|
|
141
|
+
throw fail("upload source must be a regular file");
|
|
142
|
+
size = Number(initial.size);
|
|
143
|
+
if (!Number.isSafeInteger(size) || size < 0)
|
|
144
|
+
throw fail("upload source size cannot be represented safely");
|
|
145
|
+
if (size > FILE_MAX_BYTES)
|
|
146
|
+
throw fail("upload source exceeds this helper's 256 MiB file budget");
|
|
147
|
+
const signature = (info) => [info.dev, info.ino, info.size, info.mtimeNs, info.ctimeNs].join(":");
|
|
148
|
+
const unchanged = async () => {
|
|
149
|
+
if (signature(await source.stat({ bigint: true })) !== signature(initial)) {
|
|
150
|
+
throw fail("upload source changed; keep the original file before resuming");
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const buffer = Buffer.alloc(BLOCK_BYTES);
|
|
154
|
+
const digest = createHash("sha256");
|
|
155
|
+
let position = 0;
|
|
156
|
+
while (position < size) {
|
|
157
|
+
checkAbort();
|
|
158
|
+
const { bytesRead } = await source.read(buffer, 0, Math.min(BLOCK_BYTES, size - position), position);
|
|
159
|
+
if (bytesRead === 0)
|
|
160
|
+
throw fail("upload source changed while it was being read");
|
|
161
|
+
digest.update(buffer.subarray(0, bytesRead));
|
|
162
|
+
position += bytesRead;
|
|
163
|
+
}
|
|
164
|
+
await unchanged();
|
|
165
|
+
sha256 = digest.digest("hex");
|
|
166
|
+
last = Object.freeze({ transfer, path: destinationPath, size, sha256, offset: 0, state: "prepared" });
|
|
167
|
+
emit();
|
|
168
|
+
const begin = () => machine.fs_upload_begin({ transfer, path: destinationPath, size, sha256, mkdirs: options.mkdirs ?? false }, callOptions);
|
|
169
|
+
let out = accept(await call("begin", begin));
|
|
170
|
+
emit();
|
|
171
|
+
while (out.state !== "committed" && out.offset < size) {
|
|
172
|
+
checkAbort();
|
|
173
|
+
await unchanged();
|
|
174
|
+
const length = Math.min(BLOCK_BYTES, out.chunk_max_bytes, size - out.offset);
|
|
175
|
+
let read = 0;
|
|
176
|
+
while (read < length) {
|
|
177
|
+
const { bytesRead } = await source.read(buffer, read, length - read, out.offset + read);
|
|
178
|
+
if (bytesRead === 0)
|
|
179
|
+
throw fail("upload source changed while it was being read");
|
|
180
|
+
read += bytesRead;
|
|
181
|
+
}
|
|
182
|
+
await unchanged();
|
|
183
|
+
const data = buffer.subarray(0, length);
|
|
184
|
+
const args = { transfer, offset: out.offset, data: data.toString("base64"), sha256: createHash("sha256").update(data).digest("hex") };
|
|
185
|
+
const end = out.offset + length;
|
|
186
|
+
out = accept(await call("chunk", () => machine.fs_upload_chunk(args, callOptions), end));
|
|
187
|
+
if (out.state !== "committed" && out.offset !== end) {
|
|
188
|
+
throw fail("receiver offset differs from the acknowledged chunk; inspect status before resuming");
|
|
189
|
+
}
|
|
190
|
+
emit();
|
|
191
|
+
}
|
|
192
|
+
if (out.state !== "committed") {
|
|
193
|
+
await unchanged();
|
|
194
|
+
out = accept(await call("commit", () => machine.fs_upload_commit({ transfer }, callOptions)));
|
|
195
|
+
emit();
|
|
196
|
+
}
|
|
197
|
+
if (out.state !== "committed")
|
|
198
|
+
throw fail("commit was not confirmed; inspect status before resuming");
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
if (error instanceof UploadError)
|
|
203
|
+
throw error;
|
|
204
|
+
throw fail("upload stopped locally; retain this transfer id and inspect the original error", error);
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
await file?.close();
|
|
208
|
+
}
|
|
209
|
+
}
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED