@parcha/agentrun-kernel 0.1.0-beta.31 → 0.1.0-beta.32

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.
Files changed (102) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/README.md +5 -0
  3. package/dist/errors.d.ts +7 -0
  4. package/dist/errors.js +10 -0
  5. package/dist/host/box.d.ts +0 -58
  6. package/dist/host/box.js +7 -0
  7. package/dist/host/index.d.ts +4 -10
  8. package/dist/host/index.js +2 -5
  9. package/dist/host/mcp-route.d.ts +3 -0
  10. package/dist/host/mcp-route.js +6 -2
  11. package/dist/host/proxy.d.ts +96 -5
  12. package/dist/host/proxy.js +518 -43
  13. package/dist/host/serial.js +2 -1
  14. package/dist/index.d.ts +5 -4
  15. package/dist/index.js +3 -2
  16. package/dist/kernel/cap/admit.d.ts +35 -0
  17. package/dist/kernel/cap/admit.js +186 -0
  18. package/dist/kernel/cap/errors.d.ts +20 -0
  19. package/dist/kernel/cap/errors.js +25 -0
  20. package/dist/kernel/cap/index.d.ts +14 -0
  21. package/dist/kernel/cap/index.js +15 -0
  22. package/dist/kernel/cap/ledger.d.ts +37 -0
  23. package/dist/kernel/cap/ledger.js +83 -0
  24. package/dist/kernel/cap/marks.d.ts +50 -0
  25. package/dist/kernel/cap/marks.js +28 -0
  26. package/dist/kernel/cap/park.d.ts +80 -0
  27. package/dist/kernel/cap/park.js +57 -0
  28. package/dist/kernel/cap/port.d.ts +21 -0
  29. package/dist/kernel/cap/port.js +95 -0
  30. package/dist/kernel/cap/price.d.ts +54 -0
  31. package/dist/kernel/cap/price.js +117 -0
  32. package/dist/kernel/cap/recover.d.ts +18 -0
  33. package/dist/kernel/cap/recover.js +71 -0
  34. package/dist/kernel/cap/rule.d.ts +55 -0
  35. package/dist/kernel/cap/rule.js +93 -0
  36. package/dist/kernel/cap/run-cap.d.ts +80 -0
  37. package/dist/kernel/cap/run-cap.js +132 -0
  38. package/dist/kernel/cap/steps.d.ts +36 -0
  39. package/dist/kernel/cap/steps.js +64 -0
  40. package/dist/kernel/cap/stopped.d.ts +17 -0
  41. package/dist/kernel/cap/stopped.js +34 -0
  42. package/dist/kernel/cap/types.d.ts +151 -0
  43. package/dist/kernel/cap/types.js +1 -0
  44. package/dist/kernel/cap/words.d.ts +39 -0
  45. package/dist/kernel/cap/words.js +75 -0
  46. package/dist/kernel/cap/wrap-up.d.ts +17 -0
  47. package/dist/kernel/cap/wrap-up.js +37 -0
  48. package/dist/kernel/drive.d.ts +0 -1
  49. package/dist/kernel/drive.js +9 -2
  50. package/dist/kernel/index.d.ts +1 -0
  51. package/dist/kernel/index.js +1 -0
  52. package/dist/kernel/registry/background.d.ts +12 -0
  53. package/dist/kernel/registry/background.js +342 -0
  54. package/dist/kernel/registry/bash.d.ts +3 -0
  55. package/dist/kernel/registry/bash.js +23 -0
  56. package/dist/kernel/registry/browser.d.ts +11 -0
  57. package/dist/kernel/registry/browser.js +14 -0
  58. package/dist/kernel/registry/defs.d.ts +42 -0
  59. package/dist/kernel/registry/defs.js +153 -0
  60. package/dist/kernel/registry/desktop.d.ts +11 -0
  61. package/dist/kernel/registry/desktop.js +90 -0
  62. package/dist/kernel/registry/gate.d.ts +20 -0
  63. package/dist/kernel/registry/gate.js +32 -0
  64. package/dist/kernel/registry/handoff.d.ts +11 -0
  65. package/dist/kernel/registry/handoff.js +51 -0
  66. package/dist/kernel/registry/hands.d.ts +280 -0
  67. package/dist/kernel/registry/hands.js +28 -0
  68. package/dist/kernel/registry/index.d.ts +17 -0
  69. package/dist/kernel/registry/index.js +14 -0
  70. package/dist/kernel/registry/limits.d.ts +27 -0
  71. package/dist/kernel/registry/limits.js +29 -0
  72. package/dist/kernel/registry/phone.d.ts +15 -0
  73. package/dist/kernel/registry/phone.js +135 -0
  74. package/dist/kernel/registry/registry.d.ts +22 -0
  75. package/dist/kernel/registry/registry.js +75 -0
  76. package/dist/kernel/types.d.ts +0 -7
  77. package/dist/process/builtins.js +1 -1
  78. package/dist/process/index.d.ts +1 -0
  79. package/dist/process/index.js +1 -0
  80. package/dist/process/journal.js +1 -1
  81. package/dist/process/recovery.js +1 -1
  82. package/dist/process/resume.d.ts +20 -0
  83. package/dist/process/resume.js +16 -0
  84. package/dist/process/tail.js +1 -1
  85. package/dist/process/tools.d.ts +8 -1
  86. package/dist/process/tools.js +20 -3
  87. package/dist/process/zod.js +1 -1
  88. package/dist/run-booking.d.ts +9 -0
  89. package/dist/run-booking.js +50 -0
  90. package/dist/runner.d.ts +91 -17
  91. package/dist/runner.js +132 -41
  92. package/dist/testing.d.ts +79 -12
  93. package/dist/testing.js +106 -13
  94. package/dist/types.d.ts +204 -10
  95. package/package.json +8 -4
  96. package/vendor/durable-harness/UPSTREAM.json +0 -9
  97. package/dist/host/box-slot.d.ts +0 -26
  98. package/dist/host/box-slot.js +0 -112
  99. package/dist/host/egress-proxy.d.ts +0 -45
  100. package/dist/host/egress-proxy.js +0 -416
  101. package/dist/host/egress-table.d.ts +0 -24
  102. package/dist/host/egress-table.js +0 -165
@@ -0,0 +1,153 @@
1
+ // Every model-visible host tool's definition: its name, words, schema and replay, as main shows them today. read, write,
2
+ // edit and bash are pi's (pi 1.5.0's coding tools, MIT), with serve's additions: bash's timeout words and read's image
3
+ // sentence on a computer. The five background tools are serve's (background.ts). A test holds every one of them, in
4
+ // order, to serve's own on main. A computer of a kind past files and a shell adds its own tools after them
5
+ // (capabilityToolDefs): the desktop's, the phone's, the browser's.
6
+ import { browserToolDefs } from "./browser.js";
7
+ import { DESKTOP_TOOL_DEFS } from "./desktop.js";
8
+ import { PHONE_TOOL_DEFS } from "./phone.js";
9
+ import { BACKGROUND_INPUT_BYTES, BACKGROUND_LIST_MAX, BACKGROUND_LOG_BYTES, BACKGROUND_MAX, BACKGROUND_PAGE_BYTES, BACKGROUND_SIGNALS, BASH_DEFAULT_TIMEOUT_S } from "./limits.js";
10
+ const READ_WORDS = "Read the contents of a file. Supports text files and images (jpg, png, gif, webp). Images are sent as attachments. For text files, output is truncated to 2000 lines or 50KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.";
11
+ /** read's sentence on a computer: it opens an image anywhere in the computer. */
12
+ const READ_IMAGE_SENTENCE = " It also opens an image (png, jpg, gif, webp) at any path in the run's box, such as one rendered to /tmp, and returns it as an image; any other file only inside work/.";
13
+ const READ_PARAMETERS = {
14
+ type: "object",
15
+ properties: {
16
+ path: { type: "string", description: "Path to the file to read (relative or absolute)" },
17
+ offset: { type: "number", description: "Line number to start reading from (1-indexed)" },
18
+ limit: { type: "number", description: "Maximum number of lines to read" },
19
+ },
20
+ required: ["path"],
21
+ };
22
+ const WRITE_WORDS = "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.";
23
+ const WRITE_PARAMETERS = {
24
+ type: "object",
25
+ properties: {
26
+ path: { type: "string", description: "Path to the file to write (relative or absolute)" },
27
+ content: { type: "string", description: "Content to write to the file" },
28
+ },
29
+ required: ["path", "content"],
30
+ };
31
+ const EDIT_WORDS = "Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.";
32
+ const EDIT_PARAMETERS = {
33
+ type: "object",
34
+ properties: {
35
+ path: { type: "string", description: "Path to the file to edit (relative or absolute)" },
36
+ edits: {
37
+ type: "array",
38
+ items: {
39
+ type: "object",
40
+ properties: {
41
+ oldText: { type: "string", description: "Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call." },
42
+ newText: { type: "string", description: "Replacement text for this targeted edit." },
43
+ },
44
+ required: ["oldText", "newText"],
45
+ },
46
+ description: "One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead.",
47
+ },
48
+ },
49
+ required: ["path", "edits"],
50
+ };
51
+ const BASH_WORDS = "Execute a bash command in the current working directory. Returns combined stdout and stderr. Output is truncated to last 2000 lines or 50KB (whichever is hit first). If truncated, full output is saved to a temp file. Optionally provide a timeout in seconds.";
52
+ /** bash's timeout, said: the default and this run's ceiling, and, where background processes run, where longer work goes. */
53
+ export function bashTimeoutWords(maxExecS, background) {
54
+ const longer = background ? "; for longer work use bash_background" : "";
55
+ return {
56
+ sentence: ` Each command is stopped after ${BASH_DEFAULT_TIMEOUT_S} s by default, at most ${maxExecS} s${longer}.`,
57
+ parameter: `Timeout in seconds: ${BASH_DEFAULT_TIMEOUT_S} s by default, at most ${maxExecS} s${longer}.`,
58
+ };
59
+ }
60
+ const MIB = BACKGROUND_LOG_BYTES / (1024 * 1024);
61
+ const ID_SCHEMA = { type: "string", pattern: "^[1-9][0-9]*$", description: "The process's id, as bash_background answered it (\"1\")." };
62
+ const schema = (properties, required) => ({ type: "object", additionalProperties: false, properties, required });
63
+ const BACKGROUND_DEFS = [
64
+ {
65
+ name: "bash_background",
66
+ description: `Start a shell command in the background in the run's box, from its work/, and answer at once with its id. Each call is a fresh shell (bash, else sh). The command runs until it and every child it left in its process group (a server started with &) have exited, bash_signal ends it, or the run ends, which ends it. Its stdout and stderr go together to work/background/<id>/output.log, which the run's files keep: the first ${MIB} MiB of them, later output dropped while it runs on. Its stdin is a pipe that bash_input writes; tty: true gives it a terminal instead. At most ${BACKGROUND_MAX} run at once in a run's box. Read its output with bash_output, list them with bash_list.`,
67
+ parameters: schema({
68
+ command: { type: "string", minLength: 1, description: "The command, as you would give bash." },
69
+ tty: { type: "boolean", description: "Run it on a terminal (a pty, through script in the box), for a program that needs one: a prompt, a REPL, a progress display. Its log then holds what the terminal shows (your input echoed, lines ending in \\r\\n); bash_input types into the terminal, and bash_signal INT is Ctrl-C to its foreground job." },
70
+ }, ["command"]),
71
+ replaySafe: false,
72
+ },
73
+ {
74
+ name: "bash_output",
75
+ description: `Read a background process's output (stdout and stderr together) from a byte offset: at most max_bytes (default and most ${BACKGROUND_PAGE_BYTES / 1024} KiB), with whether it still runs, its exit code once it has ended, the log's size, and the offset to read from next. Start at 0; pass the next offset to read on, also to see what a running process wrote since.`,
76
+ parameters: schema({
77
+ id: ID_SCHEMA,
78
+ offset: { type: "integer", minimum: 0, description: "The byte to read from; 0, the start, when absent." },
79
+ max_bytes: { type: "integer", minimum: 1, maximum: BACKGROUND_PAGE_BYTES, description: `The most bytes to read; ${BACKGROUND_PAGE_BYTES.toLocaleString("en-US")} when absent.` },
80
+ }, ["id"]),
81
+ replaySafe: true,
82
+ },
83
+ {
84
+ name: "bash_input",
85
+ description: `Write text to a running background process's stdin, as given (end a line with \\n), at most ${BACKGROUND_INPUT_BYTES / 1024} KiB a call. close: true closes its stdin after the text, so the process reads the end of its input. To interrupt it, send bash_signal INT; a control character written here is only a byte.`,
86
+ parameters: schema({
87
+ id: ID_SCHEMA,
88
+ text: { type: "string", description: "The bytes to write, as UTF-8; empty with close: true closes stdin alone." },
89
+ close: { type: "boolean", description: "Close the process's stdin after the text." },
90
+ }, ["id", "text"]),
91
+ replaySafe: false,
92
+ },
93
+ {
94
+ name: "bash_signal",
95
+ description: "Send a signal to a background process's process group: TERM asks it to stop, INT is Ctrl-C, KILL ends it at once. Answers its state after up to 2 s: a process that handles the signal may run on.",
96
+ parameters: schema({ id: ID_SCHEMA, signal: { type: "string", enum: [...BACKGROUND_SIGNALS], description: "TERM, INT or KILL." } }, ["id", "signal"]),
97
+ replaySafe: false,
98
+ },
99
+ {
100
+ name: "bash_list",
101
+ description: `List this run's background processes: each one's id, whether it runs or how it ended, its output's size and its command's start, every running one first, then the newest that ended (${BACKGROUND_LIST_MAX} at most).`,
102
+ parameters: schema({}, []),
103
+ replaySafe: true,
104
+ },
105
+ ];
106
+ const host = (name, toolkit, description, parameters, replaySafe, watcher) => ({
107
+ name,
108
+ source: "host",
109
+ gateName: `host:${toolkit}/${name}`,
110
+ settingsKey: toolkit === "shell" ? "bash" : name,
111
+ description,
112
+ parameters,
113
+ replaySafe,
114
+ ...(toolkit === "shell" ? { shell: { family: "bash", watcher: watcher === true } } : {}),
115
+ });
116
+ /** The model-visible host tools of a run, in the order the model sees them: read, write and edit always; bash where
117
+ * the run has a shell, its words bounded by `maxExecS`; and the five background tools on a computer. */
118
+ export function hostToolDefs(options) {
119
+ const { maxExecS, shell } = options;
120
+ const defs = [
121
+ host("read", "fs", shell === "computer" ? `${READ_WORDS}${READ_IMAGE_SENTENCE}` : READ_WORDS, READ_PARAMETERS, true),
122
+ host("write", "fs", WRITE_WORDS, WRITE_PARAMETERS, false),
123
+ host("edit", "fs", EDIT_WORDS, EDIT_PARAMETERS, false),
124
+ ];
125
+ if (shell === "none")
126
+ return defs;
127
+ const words = bashTimeoutWords(maxExecS, shell === "computer");
128
+ const bashParameters = {
129
+ type: "object",
130
+ properties: {
131
+ command: { type: "string", description: "Bash command to execute" },
132
+ timeout: { type: "number", description: words.parameter },
133
+ },
134
+ required: ["command"],
135
+ };
136
+ defs.push(host("bash", "shell", `${BASH_WORDS}${words.sentence}`, bashParameters, false));
137
+ if (shell === "local")
138
+ return defs;
139
+ for (const tool of BACKGROUND_DEFS)
140
+ defs.push(host(tool.name, "shell", tool.description, tool.parameters, tool.replaySafe, tool.name === "bash_output" || tool.name === "bash_list" || tool.name === "bash_signal"));
141
+ return defs;
142
+ }
143
+ /** The kinds, in the order their tools follow the host tools. */
144
+ export const CAPABILITY_KINDS = Object.freeze(["desktop", "phone", "browser"]);
145
+ /** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts), the
146
+ * phone's (phone.ts), or the browser's, pi-browser's contracts the host passes (browser.ts). */
147
+ export function capabilityToolDefs(kind, options = {}) {
148
+ if (kind === "desktop")
149
+ return DESKTOP_TOOL_DEFS;
150
+ if (kind === "phone")
151
+ return PHONE_TOOL_DEFS;
152
+ return browserToolDefs(options.browserTools);
153
+ }
@@ -0,0 +1,11 @@
1
+ import type { ToolDef } from "./defs.js";
2
+ import { type Executor } from "./hands.js";
3
+ /** computerd's key table (protocol.ts KEYS): a key is one of these names, or one printable ASCII character. */
4
+ export declare const DESKTOP_KEYS: readonly ["enter", "tab", "space", "backspace", "delete", "escape", "insert", "up", "down", "left", "right", "home", "end", "pageup", "pagedown", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "shift", "ctrl", "alt", "super", "capslock"];
5
+ /** The most actions one desktop_input call holds: computerd's MAX_ACTIONS. */
6
+ export declare const DESKTOP_MAX_ACTIONS = 100;
7
+ /** The actions desktop_input offers, of computerd's input.actions. */
8
+ export declare const DESKTOP_ACTIONS: readonly ["move", "click", "drag", "scroll", "type", "key"];
9
+ export declare const DESKTOP_TOOL_DEFS: readonly ToolDef[];
10
+ /** The desktop's tools' runs, on the hands' screen and input. */
11
+ export declare const DESKTOP_EXECUTORS: Readonly<Record<string, Executor>>;
@@ -0,0 +1,90 @@
1
+ import { answerOf, needsKindWords, shotAnswer } from "./hands.js";
2
+ import { HANDOFF_TOOL, handoffDef, handoffExecutor } from "./handoff.js";
3
+ /** computerd's key table (protocol.ts KEYS): a key is one of these names, or one printable ASCII character. */
4
+ export const DESKTOP_KEYS = [
5
+ "enter", "tab", "space", "backspace", "delete", "escape", "insert",
6
+ "up", "down", "left", "right", "home", "end", "pageup", "pagedown",
7
+ "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
8
+ "shift", "ctrl", "alt", "super", "capslock",
9
+ ];
10
+ /** The most actions one desktop_input call holds: computerd's MAX_ACTIONS. */
11
+ export const DESKTOP_MAX_ACTIONS = 100;
12
+ /** The actions desktop_input offers, of computerd's input.actions. */
13
+ export const DESKTOP_ACTIONS = ["move", "click", "drag", "scroll", "type", "key"];
14
+ const def = (name, description, parameters, replaySafe) => ({
15
+ name, source: "host", gateName: `host:desktop/${name}`, settingsKey: "desktop", description, parameters, replaySafe,
16
+ });
17
+ const object = (properties, required) => ({ type: "object", additionalProperties: false, properties, required });
18
+ const POINT = { type: "array", items: { type: "number" }, minItems: 2, maxItems: 2, description: "[x, y] in screenshot pixels, origin top left." };
19
+ const BUTTON = { type: "string", enum: ["left", "right", "middle"], description: "left when absent." };
20
+ const KEY = { type: "string", description: "A key name (enter, tab, space, backspace, delete, escape, insert, up, down, left, right, home, end, pageup, pagedown, f1 to f12, shift, ctrl, alt, super, capslock) or one printable character (\"a\", \"A\", \"/\")." };
21
+ const action = (type, properties, required, description) => ({ ...object({ type: { const: type }, ...properties }, ["type", ...required]), description });
22
+ export const DESKTOP_TOOL_DEFS = Object.freeze([
23
+ def("desktop_screen", "Take a screenshot of the desktop computer's screen, as an image. Its pixels are the coordinates desktop_input takes, origin top left. With region, only that part of the screen, still in the whole screen's coordinates.", object({
24
+ region: { ...object({ x: { type: "integer", minimum: 0 }, y: { type: "integer", minimum: 0 }, w: { type: "integer", minimum: 1 }, h: { type: "integer", minimum: 1 } }, ["x", "y", "w", "h"]), description: "Only this part of the screen: its top left corner and its size, in screenshot pixels." },
25
+ }, []), true),
26
+ def("desktop_input", `Act on the desktop computer with a list of actions, run in order: move the pointer, click, drag, scroll, type text, press keys. Points are [x, y] in the pixels of a desktop_screen screenshot, origin top left. Several keys in one key action press together, as a combo ("ctrl", "c"). A list with a malformed action runs nothing; one that fails partway says how many ran. It answers no image: take a screenshot to see what the actions did. At most ${DESKTOP_MAX_ACTIONS} actions a call.`, object({
27
+ actions: {
28
+ type: "array",
29
+ minItems: 1,
30
+ maxItems: DESKTOP_MAX_ACTIONS,
31
+ items: {
32
+ oneOf: [
33
+ action("move", { at: POINT }, ["at"], "Move the pointer to at."),
34
+ action("click", { at: POINT, button: BUTTON, count: { type: "integer", enum: [1, 2, 3], description: "1 when absent; 2 is a double click." } }, [], "Click at at (where the pointer is when absent)."),
35
+ action("drag", { path: { type: "array", items: POINT, minItems: 2, description: "The points to drag through, from the first to the last." }, button: BUTTON }, ["path"], "Press at the path's first point, move through it, release at its last."),
36
+ action("scroll", { at: POINT, dx: { type: "number" }, dy: { type: "number", description: "Above 0 scrolls down." }, unit: { type: "string", enum: ["clicks", "px"], description: "Wheel clicks, or pixels where the computer scrolls by pixels." } }, ["unit"], "Scroll at at (where the pointer is when absent)."),
37
+ action("type", { text: { type: "string" } }, ["text"], "Type text, as given, into what has focus."),
38
+ action("key", { keys: { type: "array", items: KEY, minItems: 1 } }, ["keys"], "Press a key, or several together as a combo."),
39
+ ],
40
+ },
41
+ },
42
+ }, ["actions"]), false),
43
+ // The agent's hand-over of the desktop to the person (handoff.ts), under the desktop's settings key.
44
+ handoffDef("desktop"),
45
+ ]);
46
+ const isWhole = (value, min) => Number.isSafeInteger(value) && value >= min;
47
+ const regionOf = (value) => {
48
+ if (value === undefined)
49
+ return undefined;
50
+ const region = value;
51
+ if (typeof region !== "object" || region === null || !isWhole(region.x, 0) || !isWhole(region.y, 0) || !isWhole(region.w, 1) || !isWhole(region.h, 1)) {
52
+ throw new Error("region is {x, y, w, h}: x and y whole numbers from 0, w and h whole numbers above 0");
53
+ }
54
+ return { x: region.x, y: region.y, w: region.w, h: region.h };
55
+ };
56
+ /** The batch as given, once each action is one of the six: the computer checks the rest, whole, before anything runs. */
57
+ const actionsOf = (value) => {
58
+ if (!Array.isArray(value) || value.length === 0 || value.length > DESKTOP_MAX_ACTIONS)
59
+ throw new Error(`actions is a list of 1 to ${DESKTOP_MAX_ACTIONS} actions`);
60
+ for (const [index, one] of value.entries()) {
61
+ const type = one?.type;
62
+ if (!DESKTOP_ACTIONS.includes(type))
63
+ throw new Error(`action ${index} is a ${JSON.stringify(type)}: an action is one of ${DESKTOP_ACTIONS.join(", ")}`);
64
+ }
65
+ return value;
66
+ };
67
+ const port = (hands, key, tool) => {
68
+ const found = hands[key];
69
+ if (!found)
70
+ throw new Error(needsKindWords(tool, "desktop"));
71
+ return found;
72
+ };
73
+ /** The desktop's tools' runs, on the hands' screen and input. */
74
+ export const DESKTOP_EXECUTORS = Object.freeze({
75
+ desktop_screen: (args, call) => answerOf(call, async () => {
76
+ const screen = port(call.hands, "screen", "desktop_screen");
77
+ const region = regionOf(args?.region);
78
+ const shot = await screen.shot(region ? { region } : {});
79
+ return shotAnswer(region
80
+ ? `A screenshot of the screen's region at (${region.x}, ${region.y}), ${region.w}x${region.h} pixels, in the whole screen's coordinates.`
81
+ : `A screenshot of the whole screen, ${shot.width}x${shot.height} pixels.`, shot);
82
+ }),
83
+ desktop_input: (args, call) => answerOf(call, async () => {
84
+ const input = port(call.hands, "input", "desktop_input");
85
+ const actions = actionsOf(args?.actions);
86
+ await input.actions(actions);
87
+ return { text: `Ran ${actions.length} action${actions.length === 1 ? "" : "s"}. desktop_screen shows what ${actions.length === 1 ? "it" : "they"} did.` };
88
+ }),
89
+ [HANDOFF_TOOL]: handoffExecutor("desktop"),
90
+ });
@@ -0,0 +1,20 @@
1
+ export type Permission = "allow" | "ask" | "deny";
2
+ /** The tools that run a command on the run's computer (SHELL_TOOLS), each held to bash's setting: bash: deny refuses
3
+ * bash_background as it refuses bash, and none of them is a settings key of its own. Under bash: ask the watchers
4
+ * (SHELL_WATCHERS) run unasked. */
5
+ export declare const SHELL_FAMILY: Readonly<Record<string, string>>;
6
+ /** The shell tools that only watch or end a process the run already started, by its own id (an id is digits under the
7
+ * run's own work/background/, and another's is "no background process"): under bash: ask they run unasked, so watching
8
+ * an approved process costs no approval per page. bash: deny still refuses them. */
9
+ export declare const SHELL_WATCHERS: ReadonlySet<string>;
10
+ /** Shell tool `name`'s permission, given bash's: a watcher's ask is allow; any other keeps bash's. */
11
+ export declare const shellPermission: (name: string, bash: Permission) => Permission;
12
+ /** The shell tool a call named `name` is, under claude-agent's host-tool name too (`mcp__hands__bash_background`), when
13
+ * it is one held to another's setting: its own name, which its request, its refusal and its approved call keep. */
14
+ export declare const shellToolOf: (name: string) => string | undefined;
15
+ /** A host tool's permission when no setting names it, by where the run's shell is. On a computer every host tool is
16
+ * allowed, as main's defaults are. With no computer, bash runs in the run's own folder on this machine, so it is asked
17
+ * first, and the background tools, which need a computer, are refused. */
18
+ export declare function defaultPermissionOf(name: string, where: {
19
+ computer: boolean;
20
+ }): Permission;
@@ -0,0 +1,32 @@
1
+ // The tool gate's shell family, moved from serve's tool-gate.ts unchanged, and the host tools' default permissions by
2
+ // where the run's shell is (the cutover's ruled change: with no computer, bash runs in the run's folder and is asked
3
+ // first, and the background tools are refused).
4
+ import { BACKGROUND_TOOLS, SHELL_TOOLS } from "./limits.js";
5
+ /** The tools that run a command on the run's computer (SHELL_TOOLS), each held to bash's setting: bash: deny refuses
6
+ * bash_background as it refuses bash, and none of them is a settings key of its own. Under bash: ask the watchers
7
+ * (SHELL_WATCHERS) run unasked. */
8
+ export const SHELL_FAMILY = Object.freeze(Object.fromEntries(SHELL_TOOLS.map((name) => [name, "bash"])));
9
+ /** The shell tools that only watch or end a process the run already started, by its own id (an id is digits under the
10
+ * run's own work/background/, and another's is "no background process"): under bash: ask they run unasked, so watching
11
+ * an approved process costs no approval per page. bash: deny still refuses them. */
12
+ export const SHELL_WATCHERS = new Set(["bash_output", "bash_list", "bash_signal"]);
13
+ /** Shell tool `name`'s permission, given bash's: a watcher's ask is allow; any other keeps bash's. */
14
+ export const shellPermission = (name, bash) => (bash === "ask" && SHELL_WATCHERS.has(name) ? "allow" : bash);
15
+ /** The shell tool a call named `name` is, under claude-agent's host-tool name too (`mcp__hands__bash_background`), when
16
+ * it is one held to another's setting: its own name, which its request, its refusal and its approved call keep. */
17
+ export const shellToolOf = (name) => {
18
+ const own = name.replace(/^mcp__hands__/, "");
19
+ return Object.hasOwn(SHELL_FAMILY, own) && SHELL_FAMILY[own] !== own ? own : undefined;
20
+ };
21
+ /** A host tool's permission when no setting names it, by where the run's shell is. On a computer every host tool is
22
+ * allowed, as main's defaults are. With no computer, bash runs in the run's own folder on this machine, so it is asked
23
+ * first, and the background tools, which need a computer, are refused. */
24
+ export function defaultPermissionOf(name, where) {
25
+ if (where.computer)
26
+ return "allow";
27
+ if (name === "bash")
28
+ return "ask";
29
+ if (BACKGROUND_TOOLS.includes(name))
30
+ return "deny";
31
+ return "allow";
32
+ }
@@ -0,0 +1,11 @@
1
+ import type { ToolDef } from "./defs.js";
2
+ import { type Executor } from "./hands.js";
3
+ export declare const HANDOFF_TOOL = "computer_request_handoff";
4
+ /** How long an ask lasts when the call names no expiry, and the bounds it may name, in seconds. */
5
+ export declare const HANDOFF_DEFAULT_S = 600;
6
+ export declare const HANDOFF_MIN_S = 30;
7
+ export declare const HANDOFF_MAX_S = 3600;
8
+ /** The hand-over's definition in `kind`, under its settings key. Never replayed: a crash after the ask is not asked again. */
9
+ export declare const handoffDef: (kind: "desktop" | "phone") => ToolDef;
10
+ /** The hand-over's run in `kind`: on the hands' handoff port, refused in the kind's words where there is none. */
11
+ export declare const handoffExecutor: (kind: "desktop" | "phone") => Executor;
@@ -0,0 +1,51 @@
1
+ import { answerOf, needsKindWords } from "./hands.js";
2
+ export const HANDOFF_TOOL = "computer_request_handoff";
3
+ /** How long an ask lasts when the call names no expiry, and the bounds it may name, in seconds. */
4
+ export const HANDOFF_DEFAULT_S = 600;
5
+ export const HANDOFF_MIN_S = 30;
6
+ export const HANDOFF_MAX_S = 3600;
7
+ const DESCRIPTION = `Hand the computer to the person for a step you cannot or should not do yourself: a sign-in with their own password, a captcha, a payment, a choice only they can make. Say what they should do (instruction) and how you both will know it is done (completion_condition). You wait until they answer, done or cancelled, or the ask expires (expires_in_s, ${HANDOFF_DEFAULT_S} s when absent). Look at the screen again after it: the person may have changed it.`;
8
+ const PARAMETERS = {
9
+ type: "object",
10
+ additionalProperties: false,
11
+ properties: {
12
+ instruction: { type: "string", minLength: 1, description: "What the person should do, in words they can follow." },
13
+ completion_condition: { type: "string", minLength: 1, description: "How you and the person will know the step is done: what the screen shows then." },
14
+ expires_in_s: { type: "integer", minimum: HANDOFF_MIN_S, maximum: HANDOFF_MAX_S, description: `How long the person has, in seconds: ${HANDOFF_DEFAULT_S} when absent.` },
15
+ },
16
+ required: ["instruction", "completion_condition"],
17
+ };
18
+ /** The hand-over's definition in `kind`, under its settings key. Never replayed: a crash after the ask is not asked again. */
19
+ export const handoffDef = (kind) => ({
20
+ name: HANDOFF_TOOL,
21
+ source: "host",
22
+ gateName: `host:${kind}/${HANDOFF_TOOL}`,
23
+ settingsKey: kind,
24
+ description: DESCRIPTION,
25
+ parameters: PARAMETERS,
26
+ replaySafe: false,
27
+ });
28
+ const words = (outcome, completion, seconds) => {
29
+ if (outcome === "done")
30
+ return `The person says the hand-over is done: ${completion} Look at the screen before you go on: it may have changed.`;
31
+ if (outcome === "cancelled")
32
+ return "The person cancelled the hand-over. Go on without it, or say why you can't.";
33
+ return `The agent's hand-over expired: the person did not answer within ${seconds} s. Go on without it, or ask again.`;
34
+ };
35
+ /** The hand-over's run in `kind`: on the hands' handoff port, refused in the kind's words where there is none. */
36
+ export const handoffExecutor = (kind) => (args, call) => answerOf(call, async () => {
37
+ const given = (typeof args === "object" && args !== null ? args : {});
38
+ if (typeof given.instruction !== "string" || given.instruction.trim() === "")
39
+ throw new Error("instruction is what the person should do, in words");
40
+ if (typeof given.completion_condition !== "string" || given.completion_condition.trim() === "")
41
+ throw new Error("completion_condition is how you and the person will know the step is done, in words");
42
+ const seconds = given.expires_in_s ?? HANDOFF_DEFAULT_S;
43
+ if (!Number.isSafeInteger(seconds) || seconds < HANDOFF_MIN_S || seconds > HANDOFF_MAX_S)
44
+ throw new Error(`expires_in_s is a whole number of seconds from ${HANDOFF_MIN_S} to ${HANDOFF_MAX_S}`);
45
+ const handoff = call.hands.handoff;
46
+ if (!handoff)
47
+ throw new Error(needsKindWords(HANDOFF_TOOL, kind));
48
+ const expires_at = new Date(Date.now() + seconds * 1000).toISOString();
49
+ const { outcome } = await handoff.ask({ instruction: given.instruction, completion_condition: given.completion_condition, expires_at }, call.signal);
50
+ return { text: words(outcome, given.completion_condition, seconds) };
51
+ });