@parcha/agentrun-kernel 0.1.0-beta.36 → 0.1.0-beta.37

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 (38) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/host/proxy.d.ts +20 -4
  3. package/dist/host/proxy.js +53 -4
  4. package/dist/index.d.ts +1 -1
  5. package/dist/kernel/cap/index.d.ts +2 -2
  6. package/dist/kernel/cap/index.js +2 -2
  7. package/dist/kernel/cap/port.js +6 -3
  8. package/dist/kernel/cap/price.d.ts +6 -0
  9. package/dist/kernel/cap/price.js +14 -0
  10. package/dist/kernel/cap/rule.d.ts +8 -0
  11. package/dist/kernel/cap/rule.js +9 -0
  12. package/dist/kernel/cap/types.d.ts +2 -0
  13. package/dist/kernel/registry/computer-surface.generated.d.ts +9 -0
  14. package/dist/kernel/registry/computer-surface.generated.js +611 -0
  15. package/dist/kernel/registry/computer.d.ts +14 -0
  16. package/dist/kernel/registry/computer.js +67 -0
  17. package/dist/kernel/registry/defs.d.ts +1 -4
  18. package/dist/kernel/registry/defs.js +1 -13
  19. package/dist/kernel/registry/handoff.js +4 -4
  20. package/dist/kernel/registry/hands.d.ts +22 -115
  21. package/dist/kernel/registry/hands.js +0 -2
  22. package/dist/kernel/registry/index.d.ts +4 -4
  23. package/dist/kernel/registry/index.js +5 -5
  24. package/dist/kernel/registry/registry.d.ts +4 -5
  25. package/dist/kernel/registry/registry.js +27 -29
  26. package/dist/kernel/types.d.ts +2 -1
  27. package/dist/process/tools.d.ts +3 -1
  28. package/dist/process/tools.js +3 -3
  29. package/dist/run-booking.d.ts +3 -0
  30. package/dist/run-booking.js +7 -0
  31. package/dist/runner.d.ts +21 -2
  32. package/dist/runner.js +27 -10
  33. package/dist/types.d.ts +16 -1
  34. package/package.json +2 -2
  35. package/dist/kernel/registry/desktop.d.ts +0 -11
  36. package/dist/kernel/registry/desktop.js +0 -90
  37. package/dist/kernel/registry/phone.d.ts +0 -15
  38. package/dist/kernel/registry/phone.js +0 -135
@@ -0,0 +1,67 @@
1
+ import { ACT_STEPS, COMPUTER_TOOL_SCHEMAS, computerDeclarations } from "./computer-surface.generated.js";
2
+ import { answerOf, needsKindWords } from "./hands.js";
3
+ import { HANDOFF_TOOL, handoffDef, handoffExecutor } from "./handoff.js";
4
+ /** The computer's tools, in the order a run lists them (the hand-over last). */
5
+ export const COMPUTER_TOOLS = ["computer_observe", "computer_act", "computer_screenshot", "computer_script", HANDOFF_TOOL];
6
+ /** The tool a host runs itself: a script runs in the host's isolate, over the same port. */
7
+ export const SCRIPT_TOOL = "computer_script";
8
+ // The words are short on purpose: every request of a run with a computer carries them. The kind skill (desktop-computer,
9
+ // phone-computer) teaches how to work; a description says only what its tool does.
10
+ const OBSERVE = `An app's window as its elements, one line each (@e12 button "Save"), under a head line naming the state (s7). With no ui group: a screenshot.`;
11
+ const ACT = `Run steps on an app, each on an element's ref or at a point in the window (refused as stale once the window changed under it). The answer says how each step ran and whether the window changed. The first refused step stops the rest. steps: UiStep[]\n\`\`\`ts\n${ACT_STEPS}\n\`\`\``;
12
+ const SCREENSHOT = "The whole screen as an image, or a region of it.";
13
+ /** computer_script's words: what a script is, and the `computer` object's declarations for these groups. */
14
+ const scriptWords = (groups) => `Run JavaScript with \`computer\` bound: many steps in one call. Return only what you need; text() and image() show words and pictures. A refused step throws.\n\`\`\`ts\n${computerDeclarations(groups)}\n\`\`\``;
15
+ /** The tools a computer with `groups` offers: observe and act with the ui group, or a screen (observe) and input (act)
16
+ * without it; the screenshot with a screen; the script with any of them. */
17
+ export function computerToolsFor(groups) {
18
+ const has = (group) => groups.includes(group);
19
+ const input = has("input.pointer") || has("input.touch") || has("input.keys");
20
+ const observe = has("ui") || has("screen");
21
+ const act = has("ui") || input;
22
+ return [...(observe ? ["computer_observe"] : []), ...(act ? ["computer_act"] : []), ...(has("screen") ? ["computer_screenshot"] : []), ...(observe || act ? [SCRIPT_TOOL] : []), HANDOFF_TOOL];
23
+ }
24
+ const def = (kind, name, description, replaySafe) => ({
25
+ name,
26
+ source: "host",
27
+ gateName: `host:${kind}/${name}`,
28
+ settingsKey: kind,
29
+ description,
30
+ parameters: COMPUTER_TOOL_SCHEMAS[name],
31
+ replaySafe,
32
+ });
33
+ /** The computer's tools in `kind`, under its settings key, for a computer with `groups` (the script's declarations
34
+ * hold only what they offer). A script and an act change the computer: never replayed. */
35
+ export function computerToolDefs(kind, groups) {
36
+ return Object.freeze([
37
+ def(kind, "computer_observe", OBSERVE, true),
38
+ def(kind, "computer_act", ACT, false),
39
+ def(kind, "computer_screenshot", SCREENSHOT, true),
40
+ def(kind, "computer_script", scriptWords(groups), false),
41
+ handoffDef(kind),
42
+ ]);
43
+ }
44
+ /** A value with its images' data left out: they travel in the answer's images. */
45
+ const withoutImageData = (value) => {
46
+ if (Array.isArray(value))
47
+ return value.map(withoutImageData);
48
+ if (value === null || typeof value !== "object")
49
+ return value;
50
+ const record = value;
51
+ return Object.fromEntries(Object.entries(record).filter(([key]) => !(key === "data" && record.type === "image")).map(([key, entry]) => [key, withoutImageData(entry)]));
52
+ };
53
+ /** A tool's run on the hands' computer port: the surface's op, answered as its text, its images and its value. */
54
+ const surfaceExecutor = (kind, tool, op) => (args, call) => answerOf(call, async () => {
55
+ const port = call.hands.computer;
56
+ if (!port)
57
+ throw new Error(needsKindWords(tool, kind));
58
+ const answer = await port.call(op, args ?? {}, call.signal);
59
+ return { text: answer.text, ...(answer.images.length > 0 ? { images: answer.images } : {}), structured: (withoutImageData(answer.value) ?? null) };
60
+ });
61
+ /** The kernel's own executors of the computer's tools in `kind`: all but the script, which the host runs. */
62
+ export const computerExecutors = (kind) => ({
63
+ computer_observe: surfaceExecutor(kind, "computer_observe", "observe"),
64
+ computer_act: surfaceExecutor(kind, "computer_act", "act"),
65
+ computer_screenshot: surfaceExecutor(kind, "computer_screenshot", "screenshot"),
66
+ [HANDOFF_TOOL]: handoffExecutor(kind),
67
+ });
@@ -32,8 +32,5 @@ export declare function hostToolDefs(options: {
32
32
  maxExecS: number;
33
33
  shell: ShellPlace;
34
34
  }): readonly ToolDef[];
35
- /** The kinds, in the order their tools follow the host tools. */
35
+ /** The kinds whose computer brings the computer's tools, each under its own settings key. */
36
36
  export declare const CAPABILITY_KINDS: readonly CapabilityKind[];
37
- /** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts) or the
38
- * phone's (phone.ts). */
39
- export declare function capabilityToolDefs(kind: CapabilityKind): readonly ToolDef[];
@@ -1,10 +1,3 @@
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.
6
- import { DESKTOP_TOOL_DEFS } from "./desktop.js";
7
- import { PHONE_TOOL_DEFS } from "./phone.js";
8
1
  import { BACKGROUND_INPUT_BYTES, BACKGROUND_LIST_MAX, BACKGROUND_LOG_BYTES, BACKGROUND_MAX, BACKGROUND_PAGE_BYTES, BACKGROUND_SIGNALS, BASH_DEFAULT_TIMEOUT_S } from "./limits.js";
9
2
  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.";
10
3
  /** read's sentence on a computer: it opens an image anywhere in the computer. */
@@ -139,10 +132,5 @@ export function hostToolDefs(options) {
139
132
  defs.push(host(tool.name, "shell", tool.description, tool.parameters, tool.replaySafe, tool.name === "bash_output" || tool.name === "bash_list" || tool.name === "bash_signal"));
140
133
  return defs;
141
134
  }
142
- /** The kinds, in the order their tools follow the host tools. */
135
+ /** The kinds whose computer brings the computer's tools, each under its own settings key. */
143
136
  export const CAPABILITY_KINDS = Object.freeze(["desktop", "phone"]);
144
- /** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts) or the
145
- * phone's (phone.ts). */
146
- export function capabilityToolDefs(kind) {
147
- return kind === "desktop" ? DESKTOP_TOOL_DEFS : PHONE_TOOL_DEFS;
148
- }
@@ -4,14 +4,14 @@ export const HANDOFF_TOOL = "computer_request_handoff";
4
4
  export const HANDOFF_DEFAULT_S = 600;
5
5
  export const HANDOFF_MIN_S = 30;
6
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.`;
7
+ const DESCRIPTION = `Hand the computer to the person for a step only they should do (their password, a captcha, a payment, their choice), and wait until they answer or the ask expires. The screen may have changed after.`;
8
8
  const PARAMETERS = {
9
9
  type: "object",
10
10
  additionalProperties: false,
11
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.` },
12
+ instruction: { type: "string", minLength: 1, description: "What the person should do." },
13
+ completion_condition: { type: "string", minLength: 1, description: "What the screen shows once it is done." },
14
+ expires_in_s: { type: "integer", minimum: HANDOFF_MIN_S, maximum: HANDOFF_MAX_S, description: `Default ${HANDOFF_DEFAULT_S}.` },
15
15
  },
16
16
  required: ["instruction", "completion_condition"],
17
17
  };
@@ -1,4 +1,4 @@
1
- import type { ToolImage } from "../../types.js";
1
+ import type { JsonValue, ToolImage } from "../../types.js";
2
2
  /** How one exec ended: its exit code, or the signal that ended it (exitCode null), and whether its timeout did. */
3
3
  export type ExecResult = {
4
4
  readonly exitCode: number | null;
@@ -60,118 +60,23 @@ export type Processes = {
60
60
  }>;
61
61
  list(): Promise<readonly ProcessInfo[]>;
62
62
  };
63
- /** A computer's kind past files and a shell: each adds its own tools to the run's (defs.ts capabilityToolDefs). */
63
+ /** A computer's kind past files and a shell: each adds the computer's tools to the run's (computer.ts COMPUTER_TOOLS). */
64
64
  export type CapabilityKind = "desktop" | "phone";
65
65
  /** A kind's tool refused in a run whose computer is not of that kind (or lacks the port): the words name the setting. */
66
66
  export declare const needsKindWords: (tool: string, kind: CapabilityKind) => string;
67
- /** A screenshot as a tool's answer: the words, and the PNG in the answer's images. */
68
- export declare const shotAnswer: (text: string, shot: Shot) => ToolAnswer;
69
- /** A point in a screenshot's pixels, origin top left, as computerd's input.actions takes it. */
70
- export type ScreenPoint = readonly [x: number, y: number];
71
- /** A screenshot: its PNG's bytes, and its size in pixels (the coordinates input takes). */
72
- export type Shot = {
73
- readonly png: Uint8Array;
74
- readonly width: number;
75
- readonly height: number;
76
- };
77
- /** A desktop's screen (computerd's screen.shot): the whole screen, or a region of it in the whole screen's pixels. */
78
- export type Screen = {
79
- shot(options?: {
80
- readonly region?: {
81
- readonly x: number;
82
- readonly y: number;
83
- readonly w: number;
84
- readonly h: number;
85
- };
86
- }): Promise<Shot>;
87
- };
88
- /** One action of a desktop's input, as computerd's input.actions takes it: the pointer's and the keyboard's, and
89
- * nothing more. A key is a name of computerd's key table or one printable ASCII character. */
90
- export type InputAction = {
91
- readonly type: "move";
92
- readonly at: ScreenPoint;
93
- } | {
94
- readonly type: "click";
95
- readonly at?: ScreenPoint;
96
- readonly button?: "left" | "right" | "middle";
97
- readonly count?: 1 | 2 | 3;
98
- } | {
99
- readonly type: "drag";
100
- readonly path: readonly ScreenPoint[];
101
- readonly button?: "left" | "right" | "middle";
102
- } | {
103
- readonly type: "scroll";
104
- readonly at?: ScreenPoint;
105
- readonly dx?: number;
106
- readonly dy?: number;
107
- readonly unit: "clicks" | "px";
108
- } | {
109
- readonly type: "type";
67
+ /** One answer of the run's computer: the value a script gets, the text a model reads, and the images beside it. */
68
+ export type ComputerAnswer = {
69
+ readonly value: unknown;
110
70
  readonly text: string;
111
- } | {
112
- readonly type: "key";
113
- readonly keys: readonly string[];
114
- };
115
- /** A desktop's input (computerd's input.actions): the batch runs in order; a refusal throws its code and words. */
116
- export type Input = {
117
- actions(list: readonly InputAction[]): Promise<void>;
71
+ readonly images: readonly ToolImage[];
118
72
  };
119
- /** A point on a phone's screen, in its screenshot's pixels. */
120
- export type PhonePoint = {
121
- readonly x: number;
122
- readonly y: number;
123
- };
124
- /** A node of a phone screen's structure, flattened: its platform class, its id, text and label where it has them, its
125
- * bounds in screenshot pixels, and whether a model can tap it or type into it. */
126
- export type PhoneNode = {
127
- readonly role: string;
128
- readonly id?: string;
129
- readonly text?: string;
130
- readonly label?: string;
131
- readonly bounds: {
132
- readonly x: number;
133
- readonly y: number;
134
- readonly w: number;
135
- readonly h: number;
136
- };
137
- readonly clickable: boolean;
138
- readonly editable: boolean;
139
- readonly focused?: boolean;
140
- /** A node with no text or label of its own, named by its first inner text (else label): an Android list row, a
141
- * clickable layout whose title sits on a child that is not clickable. */
142
- readonly inner?: string;
143
- };
144
- /** A key a phone presses: its back and home buttons, and enter. */
145
- export type PhoneKey = "back" | "home" | "enter";
146
- /** A phone: its screen and the screen's structure, a tap, a swipe, typing into the field in focus, its keys, and its apps. */
147
- export type Phone = {
148
- shot(): Promise<Shot>;
149
- /** The screen's structure, flattened (`total` the nodes the phone gave, `truncated` when it gave only part); absent on a
150
- * phone that does not offer it. */
151
- tree?(): Promise<{
152
- readonly nodes: readonly PhoneNode[];
153
- readonly total: number;
154
- readonly truncated: boolean;
155
- }>;
156
- tap(at: PhonePoint): Promise<void>;
157
- swipe(spec: {
158
- readonly from: PhonePoint;
159
- readonly to: PhonePoint;
160
- readonly durationMs: number;
161
- }): Promise<void>;
162
- type(text: string): Promise<void>;
163
- /** Absent on a phone that does not offer its keys. */
164
- key?(name: PhoneKey): Promise<void>;
165
- readonly apps: {
166
- list(): Promise<{
167
- readonly apps: readonly {
168
- readonly id: string;
169
- readonly name?: string;
170
- }[];
171
- readonly foreground: string | null;
172
- }>;
173
- open(id: string): Promise<void>;
174
- };
73
+ /** The run's computer as its tools reach it: its groups (its hello's), and one call per op (observe, act, screenshot,
74
+ * input.act, apps.list, apps.open, shell.run, files.read, files.write, files.list, files.stat), which
75
+ * @parcha/agentrun-computer's surface answers the same for a per-step tool, a script and the claim's MCP endpoint. A
76
+ * refusal throws an error with a `kind` (stale, unsupported, ...) and the computer's words; an abort throws. */
77
+ export type ComputerPort = {
78
+ readonly groups: readonly string[];
79
+ call(op: string, args: unknown, signal?: AbortSignal): Promise<ComputerAnswer>;
175
80
  };
176
81
  /** The agent's hand-over of a desktop or a phone to the person, through the run's hold: ask opens it (what the person is
177
82
  * to do, how both will know it is done, when it expires, ISO time) and resolves with its first answer, or expired. An
@@ -222,11 +127,9 @@ export type Hands = {
222
127
  }, signal?: AbortSignal): Promise<ExecResult>;
223
128
  /** The attached computer's processes; absent with no computer, where the background tools are refused. */
224
129
  readonly processes?: Processes;
225
- /** A desktop's screen and input; absent where the computer has none, and their tools are refused in words. */
226
- readonly screen?: Screen;
227
- readonly input?: Input;
228
- /** A phone's screen, touch, typing and apps; absent where the computer is no phone. */
229
- readonly phone?: Phone;
130
+ /** A desktop's or a phone's surface (computer_observe, computer_act, computer_screenshot, computer_script); absent
131
+ * where the computer is neither, and those tools are refused in words. */
132
+ readonly computer?: ComputerPort;
230
133
  /** A desktop's or a phone's hand-over to the person (the run's hold); absent where there is none. */
231
134
  readonly handoff?: Handoff;
232
135
  };
@@ -237,6 +140,9 @@ export type ToolAnswer = {
237
140
  readonly text: string;
238
141
  readonly error?: string;
239
142
  readonly images?: readonly ToolImage[];
143
+ /** The answer as a value, for a script that called the tool (Codex's code mode, an MCP client's structuredContent):
144
+ * the computer's tools give the surface's value, its images' data left out (they travel in `images`). */
145
+ readonly structured?: JsonValue;
240
146
  readonly details?: unknown;
241
147
  readonly isError?: boolean;
242
148
  readonly diagnostics?: readonly {
@@ -257,5 +163,6 @@ export type ToolCall = {
257
163
  export declare function answerOf(call: ToolCall, run: () => Promise<ToolAnswer>): Promise<ToolAnswer>;
258
164
  /** A host tool's run. */
259
165
  export type Executor = (args: unknown, call: ToolCall) => Promise<ToolAnswer>;
260
- /** What the host gives the registry, by tool name: read, write and edit, and bash's own run, which the registry bounds. */
261
- export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash", Executor>>>;
166
+ /** What the host gives the registry, by tool name: read, write and edit, bash's own run, which the registry bounds, and
167
+ * computer_script's run in the host's isolate (listed only where the host gives it). */
168
+ export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash" | "computer_script", Executor>>>;
@@ -13,8 +13,6 @@ export function exitCodeOf(result) {
13
13
  }
14
14
  /** A kind's tool refused in a run whose computer is not of that kind (or lacks the port): the words name the setting. */
15
15
  export const needsKindWords = (tool, kind) => `${tool} needs a ${kind} computer: set this agent's computer to ${kind} (Settings → Computers).`;
16
- /** A screenshot as a tool's answer: the words, and the PNG in the answer's images. */
17
- export const shotAnswer = (text, shot) => ({ text, images: [{ mimeType: "image/png", data: Buffer.from(shot.png.buffer, shot.png.byteOffset, shot.png.byteLength).toString("base64") }] });
18
16
  /** An executor's failure as an answer: an abort (the call's signal aborted) throws; any other error is its words. */
19
17
  export async function answerOf(call, run) {
20
18
  try {
@@ -1,13 +1,13 @@
1
1
  export { BACKGROUND_EXECUTORS, endBackground, NEEDS_A_COMPUTER, NO_SHELL } from "./background.js";
2
2
  export { boundedBash } from "./bash.js";
3
- export { bashTimeoutWords, CAPABILITY_KINDS, capabilityToolDefs, hostToolDefs } from "./defs.js";
3
+ export { bashTimeoutWords, CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
4
4
  export type { ShellPlace, ToolDef } from "./defs.js";
5
- export { DESKTOP_ACTIONS, DESKTOP_EXECUTORS, DESKTOP_KEYS, DESKTOP_MAX_ACTIONS, DESKTOP_TOOL_DEFS } from "./desktop.js";
5
+ export { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor, SCRIPT_TOOL } from "./computer.js";
6
+ export { COMPUTER_TOOL_SCHEMAS, computerDeclarations } from "./computer-surface.generated.js";
6
7
  export { defaultPermissionOf, SHELL_FAMILY, SHELL_WATCHERS, shellPermission, shellToolOf } from "./gate.js";
7
8
  export type { Permission } from "./gate.js";
8
9
  export { answerOf, exitCodeOf, needsKindWords } from "./hands.js";
9
- export type { CapabilityKind, ExecResult, Executor, Hands, HostExecutors, Input, InputAction, Phone, PhoneKey, PhoneNode, PhonePoint, ProcessInfo, Processes, Screen, ScreenPoint, Shot, ToolAnswer, ToolCall, } from "./hands.js";
10
- export { PHONE_EXECUTORS, PHONE_KEYS, PHONE_SWIPE_MAX_MS, PHONE_SWIPE_MS, PHONE_TOOL_DEFS, PHONE_TREE_MAX_CHARS, PHONE_TREE_MAX_NODES } from "./phone.js";
10
+ export type { CapabilityKind, ComputerAnswer, ComputerPort, ExecResult, Executor, Hands, HostExecutors, ProcessInfo, Processes, ToolAnswer, ToolCall } from "./hands.js";
11
11
  export { HANDOFF_DEFAULT_S, HANDOFF_MAX_S, HANDOFF_MIN_S, HANDOFF_TOOL } from "./handoff.js";
12
12
  export type { Handoff } from "./hands.js";
13
13
  export { BACKGROUND_DIR, BACKGROUND_INPUT_BYTES, BACKGROUND_LIST_MAX, BACKGROUND_LOG_BYTES, BACKGROUND_MAX, BACKGROUND_PAGE_BYTES, BACKGROUND_SIGNALS, BACKGROUND_TOOLS, BASH_DEFAULT_TIMEOUT_S, READ_IMAGE_MAX_BYTES, SHELL_TOOLS, } from "./limits.js";
@@ -1,14 +1,14 @@
1
1
  // The kernel's one tool registry: the host tools' definitions, words and limits, the gate's shell family, the run's
2
- // hands, and the executors the kernel owns; a computer kind's own tools (desktop, phone) and the narrow ports they run
3
- // on. A browser computer's tools are the host's browser tool's (serve's tools.ts, pi-browser's durable extension), not
2
+ // hands, and the executors the kernel owns; a desktop's or a phone's computer tools and the one port they run on. A
3
+ // browser computer's tools are the host's browser tool's (serve's tools.ts, pi-browser's durable extension), not
4
4
  // the registry's.
5
5
  export { BACKGROUND_EXECUTORS, endBackground, NEEDS_A_COMPUTER, NO_SHELL } from "./background.js";
6
6
  export { boundedBash } from "./bash.js";
7
- export { bashTimeoutWords, CAPABILITY_KINDS, capabilityToolDefs, hostToolDefs } from "./defs.js";
8
- export { DESKTOP_ACTIONS, DESKTOP_EXECUTORS, DESKTOP_KEYS, DESKTOP_MAX_ACTIONS, DESKTOP_TOOL_DEFS } from "./desktop.js";
7
+ export { bashTimeoutWords, CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
8
+ export { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor, SCRIPT_TOOL } from "./computer.js";
9
+ export { COMPUTER_TOOL_SCHEMAS, computerDeclarations } from "./computer-surface.generated.js";
9
10
  export { defaultPermissionOf, SHELL_FAMILY, SHELL_WATCHERS, shellPermission, shellToolOf } from "./gate.js";
10
11
  export { answerOf, exitCodeOf, needsKindWords } from "./hands.js";
11
- export { PHONE_EXECUTORS, PHONE_KEYS, PHONE_SWIPE_MAX_MS, PHONE_SWIPE_MS, PHONE_TOOL_DEFS, PHONE_TREE_MAX_CHARS, PHONE_TREE_MAX_NODES } from "./phone.js";
12
12
  export { HANDOFF_DEFAULT_S, HANDOFF_MAX_S, HANDOFF_MIN_S, HANDOFF_TOOL } from "./handoff.js";
13
13
  export { BACKGROUND_DIR, BACKGROUND_INPUT_BYTES, BACKGROUND_LIST_MAX, BACKGROUND_LOG_BYTES, BACKGROUND_MAX, BACKGROUND_PAGE_BYTES, BACKGROUND_SIGNALS, BACKGROUND_TOOLS, BASH_DEFAULT_TIMEOUT_S, READ_IMAGE_MAX_BYTES, SHELL_TOOLS, } from "./limits.js";
14
14
  export { createRegistry } from "./registry.js";
@@ -6,14 +6,13 @@ export type Registry = {
6
6
  get(name: string): ToolDef | undefined;
7
7
  invoke(name: string, args: unknown, call: ToolCall): Promise<ToolAnswer>;
8
8
  };
9
- /** The registry of a run's host tools, by where its shell is, and its computer's kinds (`kinds`: none, or the desktop
10
- * or the phone it is), with the host's executors. `without`: a kind's
11
- * tools this run's computer does not offer (its capabilities lack them: an iPhone's tree and keys, say), not listed to
12
- * the model; one called anyway runs, and its port's absence answers in words. */
9
+ /** The registry of a run's host tools, by where its shell is, and its computer's kind (`kinds`: none, or the desktop or
10
+ * the phone it is) with its groups (`groups`, its hello's: which of the computer's tools it offers), with the host's
11
+ * executors. */
13
12
  export declare function createRegistry(options: {
14
13
  maxExecS: number;
15
14
  shell: ShellPlace;
16
15
  kinds?: readonly CapabilityKind[];
16
+ groups?: readonly string[];
17
17
  executors: HostExecutors;
18
- without?: readonly string[];
19
18
  }): Registry;
@@ -1,24 +1,23 @@
1
1
  // The kernel's one registry of host tools: the model-visible definitions (defs.ts), each with its executor. read,
2
2
  // write and edit, and bash's own run, are the host's (pi's tools over the run's hands, until their semantics move
3
- // here); the kernel bounds bash and runs the five background tools itself. A computer of a kind past files and a shell
4
- // adds its kind's tools after them, in a fixed order: the desktop's and the phone's run on the kernel's own executors
5
- // over the hands' narrow ports. A registry missing an executor for a tool the model sees is refused when it is built,
6
- // so a run never offers a tool it cannot run. A kind's tool the run does not have is refused in words that name the
7
- // setting.
3
+ // here); the kernel bounds bash and runs the five background tools itself. A desktop or a phone adds the computer's
4
+ // tools after them (computer.ts), as its groups offer them, under its kind's settings key: observe, act and screenshot
5
+ // run on the kernel's own executors over the hands' computer port; computer_script runs in the host's isolate, listed
6
+ // only where the host gives it. A registry missing an executor for a tool the model sees is refused when it is built,
7
+ // so a run never offers a tool it cannot run. A computer's tool the run does not have is refused in words that name
8
+ // the setting.
8
9
  import { BACKGROUND_EXECUTORS, NEEDS_A_COMPUTER, NO_SHELL } from "./background.js";
9
10
  import { boundedBash } from "./bash.js";
10
- import { CAPABILITY_KINDS, capabilityToolDefs, hostToolDefs } from "./defs.js";
11
- import { DESKTOP_EXECUTORS, DESKTOP_TOOL_DEFS } from "./desktop.js";
11
+ import { COMPUTER_TOOLS, computerExecutors, computerToolDefs, computerToolsFor, SCRIPT_TOOL } from "./computer.js";
12
+ import { CAPABILITY_KINDS, hostToolDefs } from "./defs.js";
12
13
  import { answerOf, needsKindWords } from "./hands.js";
13
14
  import { BACKGROUND_TOOLS } from "./limits.js";
14
- import { PHONE_EXECUTORS, PHONE_TOOL_DEFS } from "./phone.js";
15
- /** The registry of a run's host tools, by where its shell is, and its computer's kinds (`kinds`: none, or the desktop
16
- * or the phone it is), with the host's executors. `without`: a kind's
17
- * tools this run's computer does not offer (its capabilities lack them: an iPhone's tree and keys, say), not listed to
18
- * the model; one called anyway runs, and its port's absence answers in words. */
15
+ /** The registry of a run's host tools, by where its shell is, and its computer's kind (`kinds`: none, or the desktop or
16
+ * the phone it is) with its groups (`groups`, its hello's: which of the computer's tools it offers), with the host's
17
+ * executors. */
19
18
  export function createRegistry(options) {
20
19
  const { shell, executors } = options;
21
- const kinds = CAPABILITY_KINDS.filter((kind) => options.kinds?.includes(kind));
20
+ const kind = CAPABILITY_KINDS.find((each) => options.kinds?.includes(each));
22
21
  const own = hostToolDefs({ maxExecS: options.maxExecS, shell });
23
22
  const runs = new Map();
24
23
  for (const def of own) {
@@ -30,24 +29,21 @@ export function createRegistry(options) {
30
29
  runs.set(def.name, run);
31
30
  }
32
31
  const defs = [...own];
33
- for (const kind of kinds) {
34
- for (const def of capabilityToolDefs(kind)) {
35
- // A tool two kinds share (the hand-over) is listed once, as the first kind gives it.
36
- if (runs.has(def.name))
32
+ if (kind) {
33
+ const groups = options.groups ?? [];
34
+ const offered = computerToolsFor(groups);
35
+ const kernel = computerExecutors(kind);
36
+ for (const def of computerToolDefs(kind, groups)) {
37
+ if (!offered.includes(def.name))
37
38
  continue;
38
- const run = kind === "desktop" ? DESKTOP_EXECUTORS[def.name] : PHONE_EXECUTORS[def.name];
39
+ const run = def.name === SCRIPT_TOOL ? executors.computer_script : kernel[def.name];
40
+ // The script is the host's to run (its isolate): a host that gives none lists none.
39
41
  if (!run)
40
- throw new Error(`the registry has no executor for ${def.name}, a ${kind} tool the model sees`);
42
+ continue;
41
43
  runs.set(def.name, run);
42
- if (!options.without?.includes(def.name))
43
- defs.push(def);
44
+ defs.push(def);
44
45
  }
45
46
  }
46
- // Every kind's tool by name, the run's or not, for the refusal of one it does not have.
47
- const kindOf = new Map([
48
- ...DESKTOP_TOOL_DEFS.map((def) => [def.name, "desktop"]),
49
- ...PHONE_TOOL_DEFS.map((def) => [def.name, "phone"]),
50
- ]);
51
47
  const byName = new Map(defs.map((def) => [def.name, def]));
52
48
  return {
53
49
  list: () => defs,
@@ -63,9 +59,11 @@ export function createRegistry(options) {
63
59
  // No shell at all: bash is not listed, and a call of it anyway says how to get one.
64
60
  if (shell === "none" && name === "bash")
65
61
  throw new Error(NO_SHELL);
66
- const kind = kindOf.get(name);
67
- if (kind)
68
- throw new Error(needsKindWords(name, kind));
62
+ if (COMPUTER_TOOLS.includes(name)) {
63
+ if (kind)
64
+ throw new Error(`${name} is not offered by this ${kind} computer: it offers ${(options.groups ?? []).join(", ") || "no groups"}`);
65
+ throw new Error(`${needsKindWords(name, "desktop")} A phone computer has it too.`);
66
+ }
69
67
  throw new Error(`there is no host tool ${name} in this run`);
70
68
  });
71
69
  },
@@ -108,10 +108,11 @@ export interface HostTool<Input = any> {
108
108
  * from the record answers with the same images. */
109
109
  execute(input: Input, io: HostToolIO): Promise<string | ToolOutput>;
110
110
  }
111
- /** A tool's answer with images. */
111
+ /** A tool's answer with images, and its value where it gives one (a computer tool's: what a script reads). */
112
112
  export interface ToolOutput {
113
113
  text: string;
114
114
  images?: readonly ToolImage[];
115
+ structured?: JsonValue;
115
116
  }
116
117
  export interface HostToolIO {
117
118
  signal: AbortSignal;
@@ -1,5 +1,5 @@
1
1
  import { type DriveContext, type EffectError, type HostTool, type ReplayPolicy } from '../kernel/index.js';
2
- import { type GateAnswer, type GateCall, type ToolImage } from '../types.js';
2
+ import { type GateAnswer, type GateCall, type JsonValue, type ToolImage } from '../types.js';
3
3
  /**
4
4
  * The tool bridge: `HostTool`s as a harness-neutral list a port maps onto its
5
5
  * harness's own tool mechanism (Codex's dynamic tools).
@@ -26,6 +26,8 @@ export interface ToolResult {
26
26
  interrupted?: boolean;
27
27
  /** Images the tool answered with, as it gave them (recorded with its outcome). */
28
28
  images?: readonly ToolImage[];
29
+ /** The answer as a value, where the tool gives one (recorded with its outcome): what a script's nested call reads. */
30
+ structured?: JsonValue;
29
31
  /** The text was cut at HOST_RESULT_MAX_CHARS (`boundResult`); it says so in its last line. */
30
32
  truncated?: true;
31
33
  }
@@ -75,12 +75,12 @@ export function toolBridge(ctx, tools, options = {}) {
75
75
  replay: tool.replay,
76
76
  input: args,
77
77
  }, (io) => tool.execute(args, hostToolIO(io, ctx, { signal: signal ? AbortSignal.any([signal, io.signal]) : io.signal })));
78
- // A tool answers in text, or in text with images (a screenshot): the images come back as recorded.
79
- const { text: whole, images } = typeof output === 'object' && output !== null ? output : { text: String(output), images: undefined };
78
+ // A tool answers in text, or in text with images (a screenshot) and its value: they come back as recorded.
79
+ const { text: whole, images, structured } = typeof output === 'object' && output !== null ? output : { text: String(output), images: undefined, structured: undefined };
80
80
  const { text, truncated } = boundResult(String(whole));
81
81
  if (fresh && live)
82
82
  ctx.live({ type: 'tool_end', name: tool.name, callId, result: text, ...(truncated ? { truncated } : {}) });
83
- return { text, isError: false, ...(images?.length ? { images } : {}), ...(truncated ? { truncated } : {}) };
83
+ return { text, isError: false, ...(images?.length ? { images } : {}), ...(structured !== undefined ? { structured } : {}), ...(truncated ? { truncated } : {}) };
84
84
  }
85
85
  catch (err) {
86
86
  if (err instanceof EffectFailure) {
@@ -4,6 +4,9 @@ import type { SessionRecord } from "./types.js";
4
4
  export interface RunSessions {
5
5
  keys(): readonly string[] | Promise<readonly string[]>;
6
6
  read(key: string): Promise<SessionRecord[]>;
7
+ /** What was spent on the run's behalf outside its sessions, as the host reads it at each admission (what ended at its
8
+ * cost, what is under way at the most it may take); null: unknown. None: nothing is. */
9
+ beside?(): Promise<number | null>;
7
10
  }
8
11
  /** `before`, asked about each request with the run's totals over `sessions` in place of its session's own. */
9
12
  export declare function runBooking(sessions: RunSessions, before: BeforeRequest): BeforeRequest;
@@ -6,6 +6,9 @@
6
6
  // answered (in flight in any session, in this process or a lost one) counts there at what its host held for it;
7
7
  // - `inFlight`: the requests this booking admitted that no record shows yet, each at what it was held at, so two
8
8
  // sessions asking at once never both take room only one of them fits.
9
+ // - what was spent beside its sessions on the run's behalf (`sessions.beside`: a run's learning, graded by runs of its
10
+ // own), read with them at each admission and added to `spentUsd`, so a run and what it pays for elsewhere never pass
11
+ // its cap together. Unknown (null) makes the run's spend unknown.
9
12
  // Admissions run one at a time across the run's sessions. The priced tools' charges and holds are not here: they are the
10
13
  // host's spentUsd and heldUsd hooks (beforeRequestOf), read beside these.
11
14
  import { sessionSpendOf } from "./runner.js";
@@ -29,6 +32,10 @@ export function runBooking(sessions, before) {
29
32
  if (!spend.openHolds.has(turn))
30
33
  recorded.add(idOf(key, turn));
31
34
  }
35
+ if (sessions.beside) {
36
+ const beside = await sessions.beside();
37
+ spentUsd = spentUsd === null || beside === null || !Number.isFinite(beside) ? null : spentUsd + beside;
38
+ }
32
39
  for (const id of [...admitted.keys()])
33
40
  if (recorded.has(id))
34
41
  admitted.delete(id);
package/dist/runner.d.ts CHANGED
@@ -41,6 +41,9 @@ export type BeforeRequest = (request: {
41
41
  /** The most input tokens this request can be billed for (the body's UTF-8 bytes plus what the provider adds, as the
42
42
  * model proxy measured it), undefined when it was not measured: a budget treats that input as unknown, never 0. */
43
43
  inputTokensMax?: number;
44
+ /** Its `inputTokensMax` is its provider's count, not its bytes (ModelRequest.inputTokensCounted): two bounds are compared
45
+ * (the cap's expected pricing) only when both were measured the same way. */
46
+ inputTokensCounted?: true;
44
47
  /** The most output tokens it asks for, and the least it may be limited to and still answer, as the model proxy read
45
48
  * them from its body (ModelRequest). */
46
49
  outputTokensAsked?: number;
@@ -64,6 +67,9 @@ export type BeforeRequest = (request: {
64
67
  };
65
68
  /** A side call (ModelRequest.side): on another model than the session's (a client's), or the host's own (`host`). */
66
69
  side?: true;
70
+ /** A request on the session's model that is no answer of the turn (ModelRequest.unanswered: a title, a summary, a
71
+ * child's): still the run's own to book, but never the session's previous request, read or replaced. */
72
+ unanswered?: true;
67
73
  /** It carries the host's note (ModelRequest.noted), and its input measured without it (`inputTokensMaxWithoutNote`):
68
74
  * a host that admits it only without the note says so (Admission.withoutNote). */
69
75
  noted?: true;
@@ -108,6 +114,7 @@ export type InFlight = {
108
114
  inputTokensMax?: number;
109
115
  outputTokensMax?: number;
110
116
  holdUsd?: number;
117
+ boundUsd?: number;
111
118
  host?: string;
112
119
  route?: string;
113
120
  };
@@ -202,6 +209,8 @@ export interface ModelPortOptions {
202
209
  draining?: () => Error | undefined;
203
210
  /** A note the host has for the session's next main-model request, taken by the call (ModelPort.note). */
204
211
  note?: (model: string, request?: NoteRequest) => string | undefined;
212
+ /** A note given back by the request that took it, before its admission (ModelPort.noteReturned). */
213
+ noteReturned?: (note: string) => void;
205
214
  /** The port ends the attempt (the turn limit, the host's refusal, a cost it could not record): the error it throws. */
206
215
  stop?: (reason: "turn_limit" | "refused" | "model_error", message?: string, code?: string) => Error;
207
216
  }
@@ -213,6 +222,14 @@ export interface ModelBooking {
213
222
  turns(): number;
214
223
  /** What they cost; null once one's price is unknown. */
215
224
  spentUsd(): number | null;
225
+ /** What its host holds for the requests still in flight (admitted, not yet answered): `spentUsd` counts answered ones
226
+ * only, so a reader of the run's spend that a request overlapping another must see adds this. Null while one in flight
227
+ * has no hold (never under a cap, which refuses a request it cannot price): unknown, never read as 0. */
228
+ heldUsd(): number | null;
229
+ /** What a cache miss adds to those holds (Admission.boundUsd), the requests still in flight together: the run's tools stop
230
+ * on the holds plus this (the cap counts a request that never answers at both), so a reader that must warn no later
231
+ * adds it too. 0 with none in flight or none with a bound. */
232
+ boundUsd(): number;
216
233
  /** Books, once, each paid request a crash cut off (a `runner.request` with no `runner.turn`): a `runner.turn` marked
217
234
  * `cut` at an unknown price, and a `request_end` saying so. A booked request is answered, so this is idempotent.
218
235
  * Call it before the session sends anything. */
@@ -241,8 +258,10 @@ export declare function sessionSpendOf(records: readonly SessionRecord[]): {
241
258
  }[];
242
259
  cost: SessionCost;
243
260
  };
244
- /** Whether a request answered with no price of its own (bookRequest's rule `answered`, or `answered:<label>`, its usd
245
- * null): booked at its hold for the cap, its cost unknown. */
261
+ /** Whether a request ended with no price of its own (usd null) under a rule that books it at a figure of the host's: an
262
+ * answer with no cost (bookRequest's rule `answered`, or `answered:<label>`: its hold), or one our side stopped
263
+ * (`stopped`: what streamed, an estimate) or lost (`lost`, `lost:<label>`: its hold). Each counts for the cap at that
264
+ * figure, but as a cost it is unknown, never an estimate stored as a known charge (the provider may bill otherwise). */
246
265
  export declare const answeredUnpriced: (data: {
247
266
  usd?: unknown;
248
267
  rule?: unknown;