@parcha/agentrun-kernel 0.1.0-beta.35 → 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.
- package/CHANGELOG.md +45 -0
- package/README.md +0 -7
- package/dist/host/box.d.ts +0 -14
- package/dist/host/index.d.ts +1 -1
- package/dist/host/proxy.d.ts +20 -4
- package/dist/host/proxy.js +67 -5
- package/dist/index.d.ts +1 -3
- package/dist/index.js +0 -1
- package/dist/kernel/cap/index.d.ts +3 -3
- package/dist/kernel/cap/index.js +3 -3
- package/dist/kernel/cap/port.js +6 -3
- package/dist/kernel/cap/price.d.ts +6 -0
- package/dist/kernel/cap/price.js +14 -0
- package/dist/kernel/cap/rule.d.ts +8 -0
- package/dist/kernel/cap/rule.js +9 -0
- package/dist/kernel/cap/types.d.ts +2 -0
- package/dist/kernel/cap/words.d.ts +3 -0
- package/dist/kernel/cap/words.js +4 -1
- package/dist/kernel/index.d.ts +0 -2
- package/dist/kernel/index.js +0 -1
- package/dist/kernel/registry/computer-surface.generated.d.ts +9 -0
- package/dist/kernel/registry/computer-surface.generated.js +611 -0
- package/dist/kernel/registry/computer.d.ts +14 -0
- package/dist/kernel/registry/computer.js +67 -0
- package/dist/kernel/registry/defs.d.ts +1 -4
- package/dist/kernel/registry/defs.js +1 -13
- package/dist/kernel/registry/handoff.js +4 -4
- package/dist/kernel/registry/hands.d.ts +22 -115
- package/dist/kernel/registry/hands.js +0 -2
- package/dist/kernel/registry/index.d.ts +5 -5
- package/dist/kernel/registry/index.js +6 -6
- package/dist/kernel/registry/limits.d.ts +0 -1
- package/dist/kernel/registry/limits.js +0 -1
- package/dist/kernel/registry/registry.d.ts +4 -5
- package/dist/kernel/registry/registry.js +27 -29
- package/dist/kernel/types.d.ts +2 -1
- package/dist/process/records.d.ts +0 -16
- package/dist/process/records.js +0 -41
- package/dist/process/settle.d.ts +1 -1
- package/dist/process/settle.js +2 -2
- package/dist/process/tools.d.ts +3 -1
- package/dist/process/tools.js +3 -3
- package/dist/process/zod.d.ts +0 -2
- package/dist/process/zod.js +0 -4
- package/dist/run-booking.d.ts +3 -0
- package/dist/run-booking.js +7 -0
- package/dist/runner.d.ts +21 -2
- package/dist/runner.js +27 -10
- package/dist/testing.d.ts +9 -4
- package/dist/testing.js +22 -16
- package/dist/types.d.ts +18 -4
- package/package.json +2 -2
- package/dist/delegate.d.ts +0 -87
- package/dist/delegate.js +0 -143
- package/dist/kernel/follow-up.d.ts +0 -67
- package/dist/kernel/follow-up.js +0 -146
- package/dist/kernel/registry/desktop.d.ts +0 -11
- package/dist/kernel/registry/desktop.js +0 -90
- package/dist/kernel/registry/phone.d.ts +0 -15
- package/dist/kernel/registry/phone.js +0 -135
package/dist/delegate.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
// A lead hands one task to a named sub-agent, which runs as a child node on the same kernel. Written for AgentRun.
|
|
2
|
-
//
|
|
3
|
-
// `delegateTool` is an ordinary host tool, `delegate({ agent, task, context? })`, for a lead on any harness. Each call runs
|
|
4
|
-
// one child: an `agent` node, through `createHarnessRunner`, with the agent's own system prompt (as the host gave it) and
|
|
5
|
-
// its own subset of the lead's tools, answering in a fixed shape (`DELEGATE_RESULT_SCHEMA`). The child is durable under its
|
|
6
|
-
// own key, `step:<the lead's key>/delegate:<call id>`, so the call is replay-safe: a delegate that finished answers again
|
|
7
|
-
// from its recorded result, and one a crash cut off resumes from its own store (its finished paid requests are never sent
|
|
8
|
-
// again). Its paid requests are booked under that key and go through the lead's `beforeRequest`, so one budget covers the
|
|
9
|
-
// lead and every child; its deadline is the lead's, or the agent's share of what is left. Children run one at a time, and
|
|
10
|
-
// a child has no delegate tool (depth 1).
|
|
11
|
-
import { Mutex } from "./process/tools.js";
|
|
12
|
-
import { createHarnessRunner, RUNNER_RECORDS, SUBMIT_TOOL } from "./runner.js";
|
|
13
|
-
/** The tool's name, as the lead calls it. */
|
|
14
|
-
export const DELEGATE_TOOL = "delegate";
|
|
15
|
-
/** What every delegate answers: its finding, the sources it rests on, and what it could not settle. */
|
|
16
|
-
export const DELEGATE_RESULT_SCHEMA = Object.freeze({
|
|
17
|
-
type: "object",
|
|
18
|
-
properties: {
|
|
19
|
-
answer: { type: "string" },
|
|
20
|
-
citations: {
|
|
21
|
-
type: "array",
|
|
22
|
-
items: { type: "object", properties: { url: { type: "string" }, title: { type: "string" }, quote: { type: "string" } }, required: ["url"], additionalProperties: false },
|
|
23
|
-
},
|
|
24
|
-
gaps: { type: "array", items: { type: "string" } },
|
|
25
|
-
},
|
|
26
|
-
required: ["answer", "citations"],
|
|
27
|
-
additionalProperties: false,
|
|
28
|
-
});
|
|
29
|
-
const AGENT_NAME = /^[a-z][a-z0-9-]{0,63}$/;
|
|
30
|
-
/** What is wrong with the delegate's definition, or nothing: checked when the tool is made. */
|
|
31
|
-
export function delegateProblems(options) {
|
|
32
|
-
const problems = [];
|
|
33
|
-
const lead = new Set(options.tools.map((t) => t.name));
|
|
34
|
-
const names = Object.keys(options.agents);
|
|
35
|
-
if (names.length === 0)
|
|
36
|
-
problems.push("no agents to delegate to");
|
|
37
|
-
if (lead.has(DELEGATE_TOOL))
|
|
38
|
-
problems.push(`the lead already has a tool named ${DELEGATE_TOOL}`);
|
|
39
|
-
for (const name of names) {
|
|
40
|
-
const agent = options.agents[name];
|
|
41
|
-
if (!AGENT_NAME.test(name))
|
|
42
|
-
problems.push(`the agent name ${JSON.stringify(name.slice(0, 80))} is not lowercase letters, digits and dashes`);
|
|
43
|
-
if (typeof agent.system !== "string" || agent.system.length === 0)
|
|
44
|
-
problems.push(`the agent ${name} has no system prompt`);
|
|
45
|
-
const seen = new Set();
|
|
46
|
-
for (const tool of agent.tools) {
|
|
47
|
-
if (seen.has(tool))
|
|
48
|
-
problems.push(`the agent ${name} names the tool ${tool} twice`);
|
|
49
|
-
seen.add(tool);
|
|
50
|
-
if (tool === DELEGATE_TOOL)
|
|
51
|
-
problems.push(`the agent ${name} may not delegate further (depth 1)`);
|
|
52
|
-
else if (tool === SUBMIT_TOOL)
|
|
53
|
-
problems.push(`the agent ${name} names ${SUBMIT_TOOL}, which the runner gives every child itself`);
|
|
54
|
-
else if (!lead.has(tool))
|
|
55
|
-
problems.push(`the agent ${name} names the tool ${tool}, which the lead does not have`);
|
|
56
|
-
}
|
|
57
|
-
const share = agent.deadlineShare;
|
|
58
|
-
if (share !== undefined && !(share > 0 && share <= 1))
|
|
59
|
-
problems.push(`the agent ${name}'s deadline share ${share} is not in (0, 1]`);
|
|
60
|
-
}
|
|
61
|
-
return problems;
|
|
62
|
-
}
|
|
63
|
-
/** The `delegate` host tool: refuses a bad definition when made. */
|
|
64
|
-
export function delegateTool(options) {
|
|
65
|
-
const problems = delegateProblems(options);
|
|
66
|
-
if (problems.length)
|
|
67
|
-
throw new Error(`agentrun-kernel: delegate: ${problems.join("; ")}`);
|
|
68
|
-
const byName = new Map(options.tools.map((t) => [t.name, t]));
|
|
69
|
-
const names = Object.keys(options.agents);
|
|
70
|
-
// One child at a time, whatever the harness's own tool calls do.
|
|
71
|
-
const serial = new Mutex();
|
|
72
|
-
return {
|
|
73
|
-
name: DELEGATE_TOOL,
|
|
74
|
-
description: `Hand one task to a sub-agent, which works on it with its own tools and answers with its finding, its sources and its gaps. One at a time. The agents:\n${names.map((n) => `- ${n}: ${options.agents[n].description}`).join("\n")}`,
|
|
75
|
-
inputSchema: {
|
|
76
|
-
type: "object",
|
|
77
|
-
properties: {
|
|
78
|
-
agent: { type: "string", enum: names },
|
|
79
|
-
task: { type: "string", description: "What the sub-agent is to find out or do, complete in itself." },
|
|
80
|
-
context: { type: "string", description: "What it should know from your work so far." },
|
|
81
|
-
},
|
|
82
|
-
required: ["agent", "task"],
|
|
83
|
-
additionalProperties: false,
|
|
84
|
-
},
|
|
85
|
-
// A child is durable under its own key: run again after a crash, it answers from its record or resumes its session.
|
|
86
|
-
replay: "safe",
|
|
87
|
-
call: (args, { callId, signal, sessionDir }) => serial.run(async () => {
|
|
88
|
-
const { agent: name, task, context } = (args ?? {});
|
|
89
|
-
// An own entry only: a name like `constructor` is not an agent.
|
|
90
|
-
const agent = typeof name === "string" && Object.hasOwn(options.agents, name) ? options.agents[name] : undefined;
|
|
91
|
-
if (!agent)
|
|
92
|
-
return { text: `Not delegated: ${JSON.stringify(String(name).slice(0, 80))} is not one of the agents (${names.join(", ")}).`, isError: true };
|
|
93
|
-
if (typeof task !== "string" || task.trim() === "")
|
|
94
|
-
return { text: "Not delegated: give the task as text.", isError: true };
|
|
95
|
-
const tag = { agent: name, callId, parentKey: options.parentKey };
|
|
96
|
-
// The child's session: its step's id, so its records are the runner's under `step:<id>`.
|
|
97
|
-
const sessionId = `${options.parentKey}/delegate:${callId}`;
|
|
98
|
-
const deadlineAt = options.deadlineAt?.();
|
|
99
|
-
const left = deadlineAt === undefined ? undefined : deadlineAt - Date.now();
|
|
100
|
-
if (left !== undefined && left <= 0) {
|
|
101
|
-
// A child that delivered before a crash answers from its record, deadline or not; only new work is refused. Read,
|
|
102
|
-
// not opened: opening would fence the session for nothing.
|
|
103
|
-
const saved = (await options.store.read(`step:${sessionId}`)).findLast((r) => r.kind === RUNNER_RECORDS.accepted);
|
|
104
|
-
if (saved)
|
|
105
|
-
return { text: JSON.stringify(saved.data.value) };
|
|
106
|
-
return { text: "Not delegated: the lead has no time left.", isError: true };
|
|
107
|
-
}
|
|
108
|
-
const timeoutMs = left === undefined ? options.timeoutMs : Math.max(1, Math.floor(left * (agent.deadlineShare ?? 1)));
|
|
109
|
-
const runNode = createHarnessRunner(typeof options.adapter === "function" ? options.adapter(name) : options.adapter, {
|
|
110
|
-
store: options.store,
|
|
111
|
-
tools: agent.tools.map((t) => byName.get(t)),
|
|
112
|
-
signal,
|
|
113
|
-
// The lead's runner's private directories, if it has them: the child's is its own, by its own key.
|
|
114
|
-
...(sessionDir ? { sessionDir } : {}),
|
|
115
|
-
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
|
116
|
-
...(options.maxTurns === undefined ? {} : { maxTurns: options.maxTurns }),
|
|
117
|
-
...(options.beforeRequest ? { beforeRequest: options.beforeRequest } : {}),
|
|
118
|
-
...(options.onEvent ? { onEvent: (event) => options.onEvent({ ...event, delegate: tag }) } : {}),
|
|
119
|
-
});
|
|
120
|
-
try {
|
|
121
|
-
const answer = await runNode({
|
|
122
|
-
kind: "agent",
|
|
123
|
-
label: `delegate:${name}`,
|
|
124
|
-
system: [agent.system],
|
|
125
|
-
user: typeof context === "string" && context.trim() ? `${task}\n\nContext from the lead:\n${context}` : task,
|
|
126
|
-
schema: DELEGATE_RESULT_SCHEMA,
|
|
127
|
-
tools: [...agent.tools],
|
|
128
|
-
...(agent.tier === undefined ? {} : { tier: agent.tier }),
|
|
129
|
-
step: { sessionId, attempt: 0, earlierSessionIds: [] },
|
|
130
|
-
});
|
|
131
|
-
return { text: JSON.stringify(answer) };
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
// Stopped (the lead paused, the process is draining): not an answer. Rethrown, the lead's harness records the call
|
|
135
|
-
// as interrupted and runs the same call id again on resume, which resumes this child. A child that failed on its
|
|
136
|
-
// own is an answer the lead reads.
|
|
137
|
-
if (signal?.aborted)
|
|
138
|
-
throw signal.reason ?? error;
|
|
139
|
-
return { text: `The delegate ${name} stopped without an answer: ${String(error?.message ?? error).slice(0, 400)}`, isError: true };
|
|
140
|
-
}
|
|
141
|
-
}),
|
|
142
|
-
};
|
|
143
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { SessionState as AdapterSessionState, SessionStore, TurnStatus } from "../types.js";
|
|
2
|
-
import { foldSession } from "./drive.js";
|
|
3
|
-
/** The effect's key in the follow-up's own session. */
|
|
4
|
-
export declare const FOLLOW_UP_EFFECT = "follow-up";
|
|
5
|
-
/** What a request the reserve refuses says, first. */
|
|
6
|
-
export declare const FOLLOW_UP_BUDGET_REACHED = "follow_up_budget_reached";
|
|
7
|
-
/** What a request asked for after the follow-up was cancelled says, first. */
|
|
8
|
-
export declare const FOLLOW_UP_CANCELLED = "follow_up_cancelled";
|
|
9
|
-
/** The follow-up's own session key, beside the session it follows up. */
|
|
10
|
-
export declare const followUpKey: (session: string, name: string) => string;
|
|
11
|
-
/** The session as a follow-up drives it: an adapter's session, and how a turn a crash cut off is closed unsent. */
|
|
12
|
-
export interface FollowUpTarget {
|
|
13
|
-
readonly state: AdapterSessionState;
|
|
14
|
-
/** Send `text` as a new turn, its submission under `id`. */
|
|
15
|
-
send(text: string, options?: {
|
|
16
|
-
id?: string;
|
|
17
|
-
}): Promise<void>;
|
|
18
|
-
turnEnd(): Promise<{
|
|
19
|
-
status: TurnStatus;
|
|
20
|
-
error?: string;
|
|
21
|
-
}>;
|
|
22
|
-
/** Record the open turn as ended, interrupted, and send nothing. It withdraws only the submission it is named
|
|
23
|
-
* (`opening`), while that is unplaced and the oldest pending. */
|
|
24
|
-
settleOpenTurn(options?: {
|
|
25
|
-
opening?: string;
|
|
26
|
-
}): Promise<void>;
|
|
27
|
-
/** Stop the turn in flight. */
|
|
28
|
-
interrupt(): Promise<void>;
|
|
29
|
-
}
|
|
30
|
-
export interface FollowUpOptions {
|
|
31
|
-
store: SessionStore;
|
|
32
|
-
/** The key of the session it follows up. */
|
|
33
|
-
session: string;
|
|
34
|
-
/** The follow-up's name: one follow-up per name per session. */
|
|
35
|
-
name: string;
|
|
36
|
-
prompt: string;
|
|
37
|
-
/** The most its paid requests may cost, in dollars, counted from its first request. */
|
|
38
|
-
reserveUsd: number;
|
|
39
|
-
/** A request's worst case, held for each request in flight and the one asked for. Required: a host with no worst case
|
|
40
|
-
* passes 0, and the reserve may then be passed by the one request admitted last. */
|
|
41
|
-
worstUsd: number;
|
|
42
|
-
}
|
|
43
|
-
export type FollowUpResult = {
|
|
44
|
-
status: "done";
|
|
45
|
-
} | {
|
|
46
|
-
status: "interrupted";
|
|
47
|
-
} | {
|
|
48
|
-
status: "failed";
|
|
49
|
-
error: string;
|
|
50
|
-
};
|
|
51
|
-
export interface FollowUp {
|
|
52
|
-
readonly key: string;
|
|
53
|
-
/** Ahead of the host's own check on each paid request: undefined outside the follow-up's turn. */
|
|
54
|
-
beforeRequest(request: {
|
|
55
|
-
spentUsd: number | null;
|
|
56
|
-
inFlight: readonly unknown[];
|
|
57
|
-
}): undefined | {
|
|
58
|
-
refuse: string;
|
|
59
|
-
};
|
|
60
|
-
/** Sends the follow-up once, or answers from its record. */
|
|
61
|
-
run(target: FollowUpTarget, signal: AbortSignal): Promise<FollowUpResult>;
|
|
62
|
-
}
|
|
63
|
-
export type FollowUpState = "none" | "running" | "done" | "interrupted" | "failed";
|
|
64
|
-
/** The follow-up's state in its records, as written: `running` is one a process that died left without an outcome. A
|
|
65
|
-
* host reads it before it resumes an open turn: a turn of a `running` or `interrupted` follow-up is settled instead. */
|
|
66
|
-
export declare function followUpState(records: Parameters<typeof foldSession>[1]): FollowUpState;
|
|
67
|
-
export declare function followUpOnce(options: FollowUpOptions): FollowUp;
|
package/dist/kernel/follow-up.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { foldSession, openDrive } from "./drive.js";
|
|
2
|
-
import { EffectFailure, EffectInterrupted } from "./errors.js";
|
|
3
|
-
/** The effect's key in the follow-up's own session. */
|
|
4
|
-
export const FOLLOW_UP_EFFECT = "follow-up";
|
|
5
|
-
/** What a request the reserve refuses says, first. */
|
|
6
|
-
export const FOLLOW_UP_BUDGET_REACHED = "follow_up_budget_reached";
|
|
7
|
-
/** What a request asked for after the follow-up was cancelled says, first. */
|
|
8
|
-
export const FOLLOW_UP_CANCELLED = "follow_up_cancelled";
|
|
9
|
-
/** The effect that records the follow-up's own cut turn settled: no later run settles another turn. */
|
|
10
|
-
const SETTLED_EFFECT = "follow-up.settled";
|
|
11
|
-
/** The run of each follow-up key in this process: a second run waits for it, never fences it. */
|
|
12
|
-
const runs = new WeakMap();
|
|
13
|
-
/** The follow-up's own session key, beside the session it follows up. */
|
|
14
|
-
export const followUpKey = (session, name) => `${session}:follow-up:${name}`;
|
|
15
|
-
/** The follow-up's state in its records, as written: `running` is one a process that died left without an outcome. A
|
|
16
|
-
* host reads it before it resumes an open turn: a turn of a `running` or `interrupted` follow-up is settled instead. */
|
|
17
|
-
export function followUpState(records) {
|
|
18
|
-
const effect = foldSession("", records).effects.get(FOLLOW_UP_EFFECT);
|
|
19
|
-
if (!effect)
|
|
20
|
-
return "none";
|
|
21
|
-
if (effect.status === "completed")
|
|
22
|
-
return "done";
|
|
23
|
-
if (effect.status === "failed")
|
|
24
|
-
return effect.error?.interrupted ? "interrupted" : "failed";
|
|
25
|
-
return "running";
|
|
26
|
-
}
|
|
27
|
-
export function followUpOnce(options) {
|
|
28
|
-
const { store, prompt, reserveUsd } = options;
|
|
29
|
-
const { worstUsd } = options;
|
|
30
|
-
if (typeof reserveUsd !== "number" || !Number.isFinite(reserveUsd) || reserveUsd <= 0)
|
|
31
|
-
throw new Error("reserveUsd must be a number of dollars above 0");
|
|
32
|
-
if (typeof worstUsd !== "number" || !Number.isFinite(worstUsd) || worstUsd < 0)
|
|
33
|
-
throw new Error("worstUsd must be given, a number of dollars, 0 or more");
|
|
34
|
-
const key = followUpKey(options.session, options.name);
|
|
35
|
-
// The id of the prompt's submission in the session: the follow-up's own, so a later run names it to the settle and a
|
|
36
|
-
// settle never withdraws input that is not the follow-up's.
|
|
37
|
-
const submission = `follow-up:${key}`;
|
|
38
|
-
let active = false;
|
|
39
|
-
let signalNow;
|
|
40
|
-
// The session's spentUsd when the follow-up's first request asks: the reserve counts from there. Whatever the session
|
|
41
|
-
// books after that counts against the reserve too: a task request whose cost was booked late (REQUEST_COST_LATE), and
|
|
42
|
-
// a harness's own background requests during the follow-up's turn (a CLI's small-model calls). Both refuse the
|
|
43
|
-
// follow-up sooner, never let it spend more.
|
|
44
|
-
let start;
|
|
45
|
-
const refuse = (why) => ({ refuse: `${FOLLOW_UP_BUDGET_REACHED}: ${why}` });
|
|
46
|
-
return {
|
|
47
|
-
key,
|
|
48
|
-
beforeRequest({ spentUsd, inFlight }) {
|
|
49
|
-
if (!active)
|
|
50
|
-
return undefined;
|
|
51
|
-
if (signalNow?.aborted)
|
|
52
|
-
return { refuse: `${FOLLOW_UP_CANCELLED}: the follow-up was cancelled; the request was not sent` };
|
|
53
|
-
if (spentUsd === null)
|
|
54
|
-
return refuse("what the session has spent is unknown, so the follow-up's reserve cannot be kept; the request was not sent");
|
|
55
|
-
start ??= spentUsd;
|
|
56
|
-
const used = spentUsd - start;
|
|
57
|
-
if (used + worstUsd * (inFlight.length + 1) > reserveUsd)
|
|
58
|
-
return refuse(`$${used.toFixed(6)} spent of a $${reserveUsd} reserve, and the next request could cost up to $${worstUsd}; the request was not sent`);
|
|
59
|
-
return undefined;
|
|
60
|
-
},
|
|
61
|
-
run(target, signal) {
|
|
62
|
-
const byKey = runs.get(store) ?? new Map();
|
|
63
|
-
runs.set(store, byKey);
|
|
64
|
-
const prior = byKey.get(key) ?? Promise.resolve();
|
|
65
|
-
// Waiting its turn, a run still hears its own signal: cancelled, it stops waiting and leaves the run before it alone.
|
|
66
|
-
const turn = new Promise((resolve, reject) => {
|
|
67
|
-
if (signal.aborted)
|
|
68
|
-
return reject(signal.reason);
|
|
69
|
-
const onAbort = () => reject(signal.reason);
|
|
70
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
71
|
-
void prior.then(() => {
|
|
72
|
-
signal.removeEventListener("abort", onAbort);
|
|
73
|
-
resolve();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
const run = turn.then(() => runOnce(target, signal));
|
|
77
|
-
// The next run of the key waits for the one before this, whether this one ran or gave up waiting.
|
|
78
|
-
const settled = prior.then(() => run.catch(() => undefined));
|
|
79
|
-
byKey.set(key, settled);
|
|
80
|
-
void settled.then(() => byKey.get(key) === settled && byKey.delete(key));
|
|
81
|
-
return run;
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
async function runOnce(target, signal) {
|
|
85
|
-
const drive = await openDrive({ writer: await store.open(key), signal });
|
|
86
|
-
const known = drive.state().effects.get(FOLLOW_UP_EFFECT);
|
|
87
|
-
if (known?.status === "completed")
|
|
88
|
-
return { status: "done" };
|
|
89
|
-
if (known?.status === "failed") {
|
|
90
|
-
if (!known.error?.interrupted)
|
|
91
|
-
return { status: "failed", error: known.error?.message ?? "failed" };
|
|
92
|
-
// Its own turn may still be open in the session: closed unsent, never resumed, and once. An open turn after that
|
|
93
|
-
// is another's, and is left alone.
|
|
94
|
-
if (drive.state().effects.get(SETTLED_EFFECT)?.status !== "completed") {
|
|
95
|
-
if (target.state === "open_turn")
|
|
96
|
-
await target.settleOpenTurn({ opening: submission });
|
|
97
|
-
await drive.effect(SETTLED_EFFECT, { name: SETTLED_EFFECT, replay: "safe", input: null }, async () => null);
|
|
98
|
-
}
|
|
99
|
-
return { status: "interrupted" };
|
|
100
|
-
}
|
|
101
|
-
if (target.state === "open_turn")
|
|
102
|
-
throw new Error("the session has an open turn that is not this follow-up's: recover it before the follow-up");
|
|
103
|
-
try {
|
|
104
|
-
await drive.effect(FOLLOW_UP_EFFECT, { name: options.name, replay: "never", input: { prompt } }, async () => {
|
|
105
|
-
if (signal.aborted)
|
|
106
|
-
throw signal.reason;
|
|
107
|
-
active = true;
|
|
108
|
-
signalNow = signal;
|
|
109
|
-
// A cancel stops the turn and ends the run at once: no outcome is written, so the next process reads the
|
|
110
|
-
// follow-up interrupted, and a turn that ends after it never answers done.
|
|
111
|
-
let onAbort = () => { };
|
|
112
|
-
const aborted = new Promise((_, reject) => {
|
|
113
|
-
onAbort = () => {
|
|
114
|
-
void target.interrupt().catch(() => undefined);
|
|
115
|
-
reject(signal.reason);
|
|
116
|
-
};
|
|
117
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
118
|
-
});
|
|
119
|
-
aborted.catch(() => undefined);
|
|
120
|
-
try {
|
|
121
|
-
// A send the session refuses because it holds the id already cannot strand the follow-up: the effect is
|
|
122
|
-
// `never`, so a crash after the submission landed is recovered interrupted, and the prompt is never sent again.
|
|
123
|
-
await Promise.race([target.send(prompt, { id: submission }), aborted]);
|
|
124
|
-
const end = await Promise.race([target.turnEnd(), aborted]);
|
|
125
|
-
if (signal.aborted)
|
|
126
|
-
throw signal.reason;
|
|
127
|
-
if (end.status === "interrupted")
|
|
128
|
-
throw new EffectInterrupted(end.error ?? "the follow-up's turn was interrupted");
|
|
129
|
-
if (end.status !== "completed")
|
|
130
|
-
throw new Error(end.error ?? "the follow-up's turn failed");
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
finally {
|
|
134
|
-
active = false;
|
|
135
|
-
signal.removeEventListener("abort", onAbort);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
return { status: "done" };
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
if (error instanceof EffectFailure)
|
|
142
|
-
return error.error.interrupted ? { status: "interrupted" } : { status: "failed", error: error.error.message };
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
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>>;
|
|
@@ -1,90 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ToolDef } from "./defs.js";
|
|
2
|
-
import { type Executor, type PhoneKey } from "./hands.js";
|
|
3
|
-
/** How long a swipe takes when a call names no duration, and the longest one may. */
|
|
4
|
-
export declare const PHONE_SWIPE_MS = 300;
|
|
5
|
-
export declare const PHONE_SWIPE_MAX_MS = 10000;
|
|
6
|
-
/** The most nodes phone_tree lists, and the most characters its answer takes: past either, one line counts the rest. A
|
|
7
|
-
* screen's structure can hold thousands of nodes; the answer stays a screenful, never a flood in the model's context or
|
|
8
|
-
* the run's journal. */
|
|
9
|
-
export declare const PHONE_TREE_MAX_NODES = 150;
|
|
10
|
-
export declare const PHONE_TREE_MAX_CHARS = 12000;
|
|
11
|
-
/** The keys phone_key presses. */
|
|
12
|
-
export declare const PHONE_KEYS: readonly PhoneKey[];
|
|
13
|
-
export declare const PHONE_TOOL_DEFS: readonly ToolDef[];
|
|
14
|
-
/** The phone's tools' runs, on the hands' phone. */
|
|
15
|
-
export declare const PHONE_EXECUTORS: Readonly<Record<string, Executor>>;
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { answerOf, needsKindWords, shotAnswer } from "./hands.js";
|
|
2
|
-
import { HANDOFF_TOOL, handoffDef, handoffExecutor } from "./handoff.js";
|
|
3
|
-
/** How long a swipe takes when a call names no duration, and the longest one may. */
|
|
4
|
-
export const PHONE_SWIPE_MS = 300;
|
|
5
|
-
export const PHONE_SWIPE_MAX_MS = 10_000;
|
|
6
|
-
/** The most nodes phone_tree lists, and the most characters its answer takes: past either, one line counts the rest. A
|
|
7
|
-
* screen's structure can hold thousands of nodes; the answer stays a screenful, never a flood in the model's context or
|
|
8
|
-
* the run's journal. */
|
|
9
|
-
export const PHONE_TREE_MAX_NODES = 150;
|
|
10
|
-
export const PHONE_TREE_MAX_CHARS = 12_000;
|
|
11
|
-
/** The most characters of a node's text or label phone_tree shows. */
|
|
12
|
-
const PHONE_TREE_TEXT_CHARS = 80;
|
|
13
|
-
/** The keys phone_key presses. */
|
|
14
|
-
export const PHONE_KEYS = Object.freeze(["back", "home", "enter"]);
|
|
15
|
-
const def = (name, description, parameters, replaySafe) => ({
|
|
16
|
-
name, source: "host", gateName: `host:phone/${name}`, settingsKey: "phone", description, parameters, replaySafe,
|
|
17
|
-
});
|
|
18
|
-
const object = (properties, required) => ({ type: "object", additionalProperties: false, properties, required });
|
|
19
|
-
const COORDINATE = { type: "integer", minimum: 0 };
|
|
20
|
-
const POINT = { ...object({ x: COORDINATE, y: COORDINATE }, ["x", "y"]), description: "A point in screenshot pixels, origin top left." };
|
|
21
|
-
export const PHONE_TOOL_DEFS = Object.freeze([
|
|
22
|
-
def("phone_screen", "Take a screenshot of the phone's screen, as an image. Its pixels are the coordinates phone_tap and phone_swipe take, origin top left.", object({}, []), true),
|
|
23
|
-
def("phone_tree", `List what on the phone's screen can be tapped or typed into, from the screen's structure: each node's class, its text or label, its id, and its center and bounds in a phone_screen screenshot's pixels (tap the center). At most ${PHONE_TREE_MAX_NODES} nodes; a last line counts any more.`, object({}, []), true),
|
|
24
|
-
def("phone_tap", "Tap the phone's screen once at x, y, in the pixels of a phone_screen screenshot.", object({ x: COORDINATE, y: COORDINATE }, ["x", "y"]), false),
|
|
25
|
-
def("phone_swipe", `Swipe on the phone's screen from one point to another, in screenshot pixels, over duration_ms (${PHONE_SWIPE_MS} ms when absent). A swipe up scrolls a list down.`, object({ from: POINT, to: POINT, duration_ms: { type: "integer", minimum: 1, maximum: PHONE_SWIPE_MAX_MS } }, ["from", "to"]), false),
|
|
26
|
-
def("phone_type", "Type text into the field that has focus on the phone, as given. Tap the field first.", object({ text: { type: "string", minLength: 1 } }, ["text"]), false),
|
|
27
|
-
def("phone_key", "Press one of the phone's keys: back (the system back: leaves a page, closes a dialog or the keyboard), home (the home screen) or enter (submits the field in focus).", object({ key: { type: "string", enum: [...PHONE_KEYS] } }, ["key"]), false),
|
|
28
|
-
def("phone_apps", "List the phone's installed apps, each by its id (and its name where the phone gives one), and which app is in front.", object({}, []), true),
|
|
29
|
-
def("phone_open", "Open an app on the phone by its id (phone_apps lists them), bringing it to the front.", object({ id: { type: "string", minLength: 1 } }, ["id"]), false),
|
|
30
|
-
// The agent's hand-over of the phone to the person (handoff.ts), under the phone's settings key.
|
|
31
|
-
handoffDef("phone"),
|
|
32
|
-
]);
|
|
33
|
-
const POINT_WORDS = "x and y are whole numbers from 0, in the screenshot's pixels";
|
|
34
|
-
const pointOf = (value, what = POINT_WORDS) => {
|
|
35
|
-
const point = value;
|
|
36
|
-
if (typeof point !== "object" || point === null || !Number.isSafeInteger(point.x) || !Number.isSafeInteger(point.y) || point.x < 0 || point.y < 0)
|
|
37
|
-
throw new Error(what);
|
|
38
|
-
return { x: point.x, y: point.y };
|
|
39
|
-
};
|
|
40
|
-
const said = (point) => `(${point.x}, ${point.y})`;
|
|
41
|
-
const phoneOf = (hands, tool) => {
|
|
42
|
-
if (!hands.phone)
|
|
43
|
-
throw new Error(needsKindWords(tool, "phone"));
|
|
44
|
-
return hands.phone;
|
|
45
|
-
};
|
|
46
|
-
const run = (tool, body) => (args, call) => answerOf(call, async () => body(phoneOf(call.hands, tool), (typeof args === "object" && args !== null ? args : {})));
|
|
47
|
-
/** A node's class as a model reads it: the platform's, its package or iOS prefix dropped ("Button", "EditText"). */
|
|
48
|
-
const classOf = (role) => role.replace(/^XCUIElementType/, "").split(".").pop() || role;
|
|
49
|
-
const clip = (text) => (text.length > PHONE_TREE_TEXT_CHARS ? `${text.slice(0, PHONE_TREE_TEXT_CHARS - 1)}…` : text);
|
|
50
|
-
/** One node, on one line: its class, its text or label (else its first inner text, marked so), its id, whether it is a
|
|
51
|
-
* field (and in focus), its center to tap and its bounds. */
|
|
52
|
-
function nodeLine(node) {
|
|
53
|
-
const own = node.text || node.label;
|
|
54
|
-
const name = own ? JSON.stringify(clip(own)) : node.inner ? `${JSON.stringify(clip(node.inner))} (inner text)` : "";
|
|
55
|
-
const { x, y, w, h } = node.bounds;
|
|
56
|
-
const state = node.editable ? `a field${node.focused ? ", focused" : ""}; ` : node.focused ? "focused; " : "";
|
|
57
|
-
const center = `(${Math.round(x + w / 2)}, ${Math.round(y + h / 2)})`;
|
|
58
|
-
return `- ${clip(classOf(node.role))}${name ? ` ${name}` : ""}${node.id ? ` [${clip(node.id)}]` : ""}: ${state}center ${center}, bounds ${x},${y} ${w}x${h}`;
|
|
59
|
-
}
|
|
60
|
-
/** phone_tree's answer: a head line, then a line a node, at most PHONE_TREE_MAX_NODES of them and PHONE_TREE_MAX_CHARS
|
|
61
|
-
* characters in all; one last line counts the nodes not shown. */
|
|
62
|
-
function treeAnswer(tree) {
|
|
63
|
-
const nodes = tree.nodes.filter((node) => node.clickable || node.editable);
|
|
64
|
-
const head = `${nodes.length} node${nodes.length === 1 ? "" : "s"} you can tap or type into, of ${tree.total} on the screen. Coordinates are a phone_screen screenshot's pixels.${tree.truncated ? " The phone gave only part of the screen's structure: take a phone_screen screenshot for the rest." : ""}`;
|
|
65
|
-
const lines = [head];
|
|
66
|
-
// Room kept for the last line, whatever it counts.
|
|
67
|
-
let room = PHONE_TREE_MAX_CHARS - head.length - `\n${nodes.length} more nodes not shown.`.length;
|
|
68
|
-
let shown = 0;
|
|
69
|
-
for (const node of nodes) {
|
|
70
|
-
if (shown >= PHONE_TREE_MAX_NODES)
|
|
71
|
-
break;
|
|
72
|
-
const line = nodeLine(node);
|
|
73
|
-
if (line.length + 1 > room)
|
|
74
|
-
break;
|
|
75
|
-
lines.push(line);
|
|
76
|
-
room -= line.length + 1;
|
|
77
|
-
shown += 1;
|
|
78
|
-
}
|
|
79
|
-
if (shown < nodes.length)
|
|
80
|
-
lines.push(`${nodes.length - shown} more node${nodes.length - shown === 1 ? "" : "s"} not shown.`);
|
|
81
|
-
return lines.join("\n");
|
|
82
|
-
}
|
|
83
|
-
/** The phone's tools' runs, on the hands' phone. */
|
|
84
|
-
export const PHONE_EXECUTORS = Object.freeze({
|
|
85
|
-
phone_screen: run("phone_screen", async (phone) => {
|
|
86
|
-
const shot = await phone.shot();
|
|
87
|
-
return shotAnswer(`A screenshot of the phone's screen, ${shot.width}x${shot.height} pixels.`, shot);
|
|
88
|
-
}),
|
|
89
|
-
phone_tree: run("phone_tree", async (phone) => {
|
|
90
|
-
if (!phone.tree)
|
|
91
|
-
throw new Error("this phone does not offer its UI tree");
|
|
92
|
-
return { text: treeAnswer(await phone.tree()) };
|
|
93
|
-
}),
|
|
94
|
-
phone_key: run("phone_key", async (phone, args) => {
|
|
95
|
-
if (!phone.key)
|
|
96
|
-
throw new Error("this phone does not offer its keys");
|
|
97
|
-
const key = args.key;
|
|
98
|
-
if (typeof key !== "string" || !PHONE_KEYS.includes(key))
|
|
99
|
-
throw new Error(`key is ${PHONE_KEYS.slice(0, -1).join(", ")} or ${PHONE_KEYS.at(-1)}`);
|
|
100
|
-
await phone.key(key);
|
|
101
|
-
return { text: `Pressed ${key}.` };
|
|
102
|
-
}),
|
|
103
|
-
phone_tap: run("phone_tap", async (phone, args) => {
|
|
104
|
-
const at = pointOf(args);
|
|
105
|
-
await phone.tap(at);
|
|
106
|
-
return { text: `Tapped at ${said(at)}.` };
|
|
107
|
-
}),
|
|
108
|
-
phone_swipe: run("phone_swipe", async (phone, args) => {
|
|
109
|
-
const from = pointOf(args.from, `from: ${POINT_WORDS}`);
|
|
110
|
-
const to = pointOf(args.to, `to: ${POINT_WORDS}`);
|
|
111
|
-
const durationMs = args.duration_ms ?? PHONE_SWIPE_MS;
|
|
112
|
-
if (!Number.isSafeInteger(durationMs) || durationMs < 1 || durationMs > PHONE_SWIPE_MAX_MS)
|
|
113
|
-
throw new Error(`duration_ms is a whole number of milliseconds from 1 to ${PHONE_SWIPE_MAX_MS}`);
|
|
114
|
-
await phone.swipe({ from, to, durationMs: durationMs });
|
|
115
|
-
return { text: `Swiped from ${said(from)} to ${said(to)} over ${durationMs} ms.` };
|
|
116
|
-
}),
|
|
117
|
-
phone_type: run("phone_type", async (phone, args) => {
|
|
118
|
-
if (typeof args.text !== "string" || args.text === "")
|
|
119
|
-
throw new Error("text is the text to type");
|
|
120
|
-
await phone.type(args.text);
|
|
121
|
-
return { text: `Typed ${[...args.text].length} characters.` };
|
|
122
|
-
}),
|
|
123
|
-
phone_apps: run("phone_apps", async (phone) => {
|
|
124
|
-
const { apps, foreground } = await phone.apps.list();
|
|
125
|
-
const head = `${apps.length} app${apps.length === 1 ? "" : "s"}; ${foreground === null ? "none is in front" : `${foreground} is in front`}.`;
|
|
126
|
-
return { text: [head, ...apps.map((app) => (app.name ? `${app.id} (${app.name})` : app.id))].join("\n") };
|
|
127
|
-
}),
|
|
128
|
-
phone_open: run("phone_open", async (phone, args) => {
|
|
129
|
-
if (typeof args.id !== "string" || args.id === "")
|
|
130
|
-
throw new Error("id is an app's id, as phone_apps lists it");
|
|
131
|
-
await phone.apps.open(args.id);
|
|
132
|
-
return { text: `Opened ${args.id}.` };
|
|
133
|
-
}),
|
|
134
|
-
[HANDOFF_TOOL]: handoffExecutor("phone"),
|
|
135
|
-
});
|