@saadnvd1/agent-os 0.3.610 → 0.3.611
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.
|
@@ -50,6 +50,10 @@ export function runningBrief(
|
|
|
50
50
|
lines.push(
|
|
51
51
|
`- \`${browse.map}\` maps every page to its URL: navigate by URL, don't explore.`
|
|
52
52
|
);
|
|
53
|
+
if (browse)
|
|
54
|
+
lines.push(
|
|
55
|
+
"- Check state with the browser's text snapshot, not screenshots. Screenshot only the final changed screen, and just the changed element (its uid) rather than the full page where the tool allows; every image stays in your context."
|
|
56
|
+
);
|
|
53
57
|
const sim = simulatorBrief(config.simulator);
|
|
54
58
|
if (sim) lines.push(sim);
|
|
55
59
|
for (const note of notesOf(config.notes))
|
package/lib/simulator/brief.ts
CHANGED
|
@@ -12,7 +12,8 @@ export function simulatorBrief(
|
|
|
12
12
|
return "- The iOS simulator tools are not available on this machine; skip simulator checks and say so in the PR.";
|
|
13
13
|
return [
|
|
14
14
|
"- iOS simulator (optional; it never gates a PR or a build, and a failure is reported, not a blocker): `aos sim open` makes your own simulator and installs this worktree's build (run it with run_in_background, a cold build takes minutes; `aos sim status` shows where it is).",
|
|
15
|
-
'
|
|
15
|
+
' Drive and check state with `aos sim snapshot -i` (accessibility tree with @refs, text, cheap), `aos sim click @e3`, `aos sim fill @e5 "text"`, `aos sim logs --last 2m`. Refs are only valid from the latest snapshot.',
|
|
16
|
+
" Every image you read stays in your context for every later turn, so screenshot only the final changed screens, and prefer `aos sim screenshot --region @e3` (one element from the latest snapshot) for a single row or banner. It prints two paths: read the `read:` one (reduced to 800px); attach the `full-size` file to the PR.",
|
|
16
17
|
" Screenshots are written outside the worktree: don't put them in commits or the PR body unless they belong there. `aos sim close` when you're done; it is closed for you when the task ends.",
|
|
17
18
|
].join("\n");
|
|
18
19
|
}
|
package/lib/simulator/index.ts
CHANGED
|
@@ -24,6 +24,13 @@ import {
|
|
|
24
24
|
simulatorDir,
|
|
25
25
|
type CallResult,
|
|
26
26
|
} from "./agent-device";
|
|
27
|
+
import {
|
|
28
|
+
RegionError,
|
|
29
|
+
cropToFrame,
|
|
30
|
+
frameOf,
|
|
31
|
+
reducedCopy,
|
|
32
|
+
takeRegion,
|
|
33
|
+
} from "./screenshot";
|
|
27
34
|
import {
|
|
28
35
|
allLeases,
|
|
29
36
|
bootedCount,
|
|
@@ -320,7 +327,7 @@ async function wake(sessionId: string, lease: Lease): Promise<void> {
|
|
|
320
327
|
export async function runSimulator(
|
|
321
328
|
sessionId: string,
|
|
322
329
|
args: string[]
|
|
323
|
-
): Promise<CallResult & { screenshot: string | null }> {
|
|
330
|
+
): Promise<CallResult & { screenshot: string | null; read: string | null }> {
|
|
324
331
|
const { config, cwd } = declared(sessionId);
|
|
325
332
|
let lease = getLease(sessionId);
|
|
326
333
|
if (!lease || lease.state === "failed")
|
|
@@ -333,23 +340,58 @@ export async function runSimulator(
|
|
|
333
340
|
await wake(sessionId, lease);
|
|
334
341
|
lease = getLease(sessionId)!;
|
|
335
342
|
}
|
|
343
|
+
let region: string | null = null;
|
|
344
|
+
if (args[0] === "screenshot") {
|
|
345
|
+
try {
|
|
346
|
+
({ args, region } = takeRegion(args));
|
|
347
|
+
} catch (error) {
|
|
348
|
+
throw new SimulatorError((error as Error).message, 400);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const target = { udid: lease.udid!, sessionId, app: config.app, cwd };
|
|
336
352
|
let argv: string[];
|
|
337
353
|
try {
|
|
338
|
-
argv = buildArgs(args,
|
|
339
|
-
udid: lease.udid!,
|
|
340
|
-
sessionId,
|
|
341
|
-
app: config.app,
|
|
342
|
-
cwd,
|
|
343
|
-
});
|
|
354
|
+
argv = buildArgs(args, target);
|
|
344
355
|
} catch (error) {
|
|
345
356
|
throw new SimulatorError((error as Error).message, 400);
|
|
346
357
|
}
|
|
347
358
|
const shot = screenshotPath(argv);
|
|
348
359
|
if (shot) fs.mkdirSync(path.dirname(shot), { recursive: true });
|
|
349
360
|
touchLease(sessionId);
|
|
361
|
+
// The element's frame comes from a fresh snapshot, before the shot, so a
|
|
362
|
+
// bad ref fails without writing anything.
|
|
363
|
+
let located: ReturnType<typeof frameOf> | null = null;
|
|
364
|
+
if (region) {
|
|
365
|
+
const snap = await callTool(
|
|
366
|
+
buildArgs(["snapshot", "-i", "--json"], target)
|
|
367
|
+
);
|
|
368
|
+
try {
|
|
369
|
+
if (snap.code) throw new RegionError(snap.stderr || "snapshot failed");
|
|
370
|
+
located = frameOf(snap.stdout, region);
|
|
371
|
+
} catch (error) {
|
|
372
|
+
if (error instanceof RegionError)
|
|
373
|
+
throw new SimulatorError(error.message, 400);
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
350
377
|
const result = await callTool(argv);
|
|
378
|
+
const taken = shot && fs.existsSync(shot) ? shot : null;
|
|
379
|
+
let note = "";
|
|
380
|
+
if (taken && located) {
|
|
381
|
+
try {
|
|
382
|
+
await cropToFrame(taken, located.frame, located.screen);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
note = `region crop failed (${(error as Error).message}); kept the full screen\n`;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
const read = taken ? await reducedCopy(taken) : null;
|
|
351
388
|
touchLease(sessionId);
|
|
352
|
-
return {
|
|
389
|
+
return {
|
|
390
|
+
...result,
|
|
391
|
+
stderr: result.stderr + note,
|
|
392
|
+
screenshot: taken,
|
|
393
|
+
read,
|
|
394
|
+
};
|
|
353
395
|
}
|
|
354
396
|
|
|
355
397
|
export async function simulatorLogs(sessionId: string, last: string) {
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cheaper screenshots for the agent: every image it reads costs tokens on
|
|
3
|
+
* every later turn, so it reads a copy capped at READ_SIZE px (or just one
|
|
4
|
+
* element of the screen) while the full-size file stays for the PR.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { execFile } from "child_process";
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { promisify } from "util";
|
|
11
|
+
|
|
12
|
+
const execFileAsync = promisify(execFile);
|
|
13
|
+
|
|
14
|
+
export const READ_SIZE = 800;
|
|
15
|
+
// Points of context kept around a cropped element.
|
|
16
|
+
export const REGION_MARGIN = 8;
|
|
17
|
+
|
|
18
|
+
export type Sips = (args: string[]) => Promise<string>;
|
|
19
|
+
|
|
20
|
+
const realSips: Sips = async (args) =>
|
|
21
|
+
(await execFileAsync("sips", args, { timeout: 30_000 })).stdout;
|
|
22
|
+
let sips: Sips = realSips;
|
|
23
|
+
export const setSips = (fn: Sips | null) => {
|
|
24
|
+
sips = fn ?? realSips;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface Frame {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
}
|
|
33
|
+
export interface Crop {
|
|
34
|
+
x: number;
|
|
35
|
+
y: number;
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class RegionError extends Error {}
|
|
41
|
+
|
|
42
|
+
// Where the agent reads from: a copy under shots/read, so the shots strip and
|
|
43
|
+
// the PR files stay the full-size ones.
|
|
44
|
+
export const readCopyPath = (full: string) =>
|
|
45
|
+
path.join(path.dirname(full), "read", path.basename(full));
|
|
46
|
+
|
|
47
|
+
async function pixelSize(file: string) {
|
|
48
|
+
const out = await sips(["-g", "pixelWidth", "-g", "pixelHeight", file]);
|
|
49
|
+
const w = Number(/pixelWidth:\s*(\d+)/.exec(out)?.[1]);
|
|
50
|
+
const h = Number(/pixelHeight:\s*(\d+)/.exec(out)?.[1]);
|
|
51
|
+
if (!w || !h) throw new Error("cannot read image size");
|
|
52
|
+
return { width: w, height: h };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The reduced copy, or the full file when resizing fails.
|
|
56
|
+
export async function reducedCopy(full: string): Promise<string> {
|
|
57
|
+
const out = readCopyPath(full);
|
|
58
|
+
try {
|
|
59
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
60
|
+
await sips(["-Z", String(READ_SIZE), full, "--out", out]);
|
|
61
|
+
return fs.existsSync(out) ? out : full;
|
|
62
|
+
} catch {
|
|
63
|
+
return full;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// An element's frame (points) to a pixel rectangle of the image, with a
|
|
68
|
+
// margin, clamped to the image. `screen` is the size of the frame's space.
|
|
69
|
+
export function cropRect(
|
|
70
|
+
frame: Frame,
|
|
71
|
+
screen: { width: number; height: number },
|
|
72
|
+
image: { width: number; height: number },
|
|
73
|
+
margin = REGION_MARGIN
|
|
74
|
+
): Crop {
|
|
75
|
+
const sx = image.width / screen.width;
|
|
76
|
+
const sy = image.height / screen.height;
|
|
77
|
+
const x0 = Math.max(0, Math.floor((frame.x - margin) * sx));
|
|
78
|
+
const y0 = Math.max(0, Math.floor((frame.y - margin) * sy));
|
|
79
|
+
const x1 = Math.min(
|
|
80
|
+
image.width,
|
|
81
|
+
Math.ceil((frame.x + frame.width + margin) * sx)
|
|
82
|
+
);
|
|
83
|
+
const y1 = Math.min(
|
|
84
|
+
image.height,
|
|
85
|
+
Math.ceil((frame.y + frame.height + margin) * sy)
|
|
86
|
+
);
|
|
87
|
+
if (x1 <= x0 || y1 <= y0)
|
|
88
|
+
throw new RegionError("that element is off the screen");
|
|
89
|
+
return { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type Node = { ref?: unknown; rect?: Frame };
|
|
93
|
+
|
|
94
|
+
// Every node with a ref and a rect, wherever the JSON nests them.
|
|
95
|
+
function nodesOf(value: unknown, out: Node[] = []): Node[] {
|
|
96
|
+
if (Array.isArray(value)) value.forEach((v) => nodesOf(v, out));
|
|
97
|
+
else if (value && typeof value === "object") {
|
|
98
|
+
const n = value as Node;
|
|
99
|
+
if (typeof n.ref === "string" && n.rect) out.push(n);
|
|
100
|
+
for (const v of Object.values(value)) nodesOf(v, out);
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const isFrame = (r: unknown): r is Frame =>
|
|
106
|
+
!!r &&
|
|
107
|
+
["x", "y", "width", "height"].every(
|
|
108
|
+
(k) => typeof (r as Record<string, unknown>)[k] === "number"
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// The frame of `ref` in a `snapshot -i --json` output, and the screen size
|
|
112
|
+
// (the widest, tallest frame: the app's window).
|
|
113
|
+
export function frameOf(
|
|
114
|
+
json: string,
|
|
115
|
+
ref: string
|
|
116
|
+
): { frame: Frame; screen: { width: number; height: number } } {
|
|
117
|
+
let nodes: Node[];
|
|
118
|
+
try {
|
|
119
|
+
nodes = nodesOf(JSON.parse(json)).filter((n) => isFrame(n.rect));
|
|
120
|
+
} catch {
|
|
121
|
+
throw new RegionError(
|
|
122
|
+
"could not read the snapshot; run `aos sim snapshot -i`"
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const want = ref.replace(/^@/, "");
|
|
126
|
+
const hit = nodes.find((n) => String(n.ref).replace(/^@/, "") === want);
|
|
127
|
+
if (!hit)
|
|
128
|
+
throw new RegionError(
|
|
129
|
+
`unknown or stale ref @${want}: run \`aos sim snapshot -i\` and use a ref from it`
|
|
130
|
+
);
|
|
131
|
+
const screen = nodes.reduce(
|
|
132
|
+
(s, n) => ({
|
|
133
|
+
width: Math.max(s.width, n.rect!.x + n.rect!.width),
|
|
134
|
+
height: Math.max(s.height, n.rect!.y + n.rect!.height),
|
|
135
|
+
}),
|
|
136
|
+
{ width: 0, height: 0 }
|
|
137
|
+
);
|
|
138
|
+
return { frame: hit.rect!, screen };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Crops `file` in place to the rectangle.
|
|
142
|
+
export async function cropInPlace(file: string, c: Crop): Promise<void> {
|
|
143
|
+
await sips([
|
|
144
|
+
"--cropToHeightWidth",
|
|
145
|
+
String(c.height),
|
|
146
|
+
String(c.width),
|
|
147
|
+
"--cropOffset",
|
|
148
|
+
String(c.y),
|
|
149
|
+
String(c.x),
|
|
150
|
+
file,
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export async function cropToFrame(
|
|
155
|
+
file: string,
|
|
156
|
+
frame: Frame,
|
|
157
|
+
screen: { width: number; height: number }
|
|
158
|
+
): Promise<void> {
|
|
159
|
+
await cropInPlace(file, cropRect(frame, screen, await pixelSize(file)));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// `--region <ref>` / `--region=<ref>` out of a screenshot's args.
|
|
163
|
+
export function takeRegion(args: string[]): {
|
|
164
|
+
args: string[];
|
|
165
|
+
region: string | null;
|
|
166
|
+
} {
|
|
167
|
+
const rest: string[] = [];
|
|
168
|
+
let region: string | null = null;
|
|
169
|
+
for (let i = 0; i < args.length; i++) {
|
|
170
|
+
const a = args[i];
|
|
171
|
+
if (a === "--region") {
|
|
172
|
+
region = args[++i] ?? "";
|
|
173
|
+
if (!region) throw new RegionError("--region needs a ref, like @e3");
|
|
174
|
+
} else if (a.startsWith("--region=")) region = a.slice(9);
|
|
175
|
+
else rest.push(a);
|
|
176
|
+
}
|
|
177
|
+
if (region !== null && !/^@?e?\d+$/.test(region) && !/^@\S+$/.test(region))
|
|
178
|
+
throw new RegionError("--region takes a ref from `aos sim snapshot -i`");
|
|
179
|
+
return { args: rest, region };
|
|
180
|
+
}
|