agent-device 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -4
- package/dist/src/bin.js +26 -21
- package/dist/src/daemon.js +9 -8
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj +2 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +15 -0
- package/package.json +3 -2
- package/skills/agent-device/SKILL.md +22 -6
- package/skills/agent-device/references/session-management.md +9 -0
- package/skills/agent-device/references/snapshot-refs.md +18 -5
- package/skills/agent-device/references/video-recording.md +2 -2
- package/src/cli.ts +6 -0
- package/src/core/__tests__/capabilities.test.ts +67 -0
- package/src/core/capabilities.ts +49 -0
- package/src/core/dispatch.ts +29 -118
- package/src/daemon/__tests__/is-predicates.test.ts +68 -0
- package/src/daemon/__tests__/selectors.test.ts +128 -0
- package/src/daemon/__tests__/session-routing.test.ts +108 -0
- package/src/daemon/__tests__/session-selector.test.ts +64 -0
- package/src/daemon/__tests__/session-store.test.ts +95 -0
- package/src/daemon/__tests__/snapshot-processing.test.ts +47 -0
- package/src/daemon/action-utils.ts +29 -0
- package/src/daemon/app-state.ts +66 -0
- package/src/daemon/context.ts +36 -0
- package/src/daemon/device-ready.ts +13 -0
- package/src/daemon/handlers/__tests__/find.test.ts +99 -0
- package/src/daemon/handlers/__tests__/replay-heal.test.ts +364 -0
- package/src/daemon/handlers/__tests__/snapshot.test.ts +128 -0
- package/src/daemon/handlers/find.ts +304 -0
- package/src/daemon/handlers/interaction.ts +510 -0
- package/src/daemon/handlers/parse-utils.ts +8 -0
- package/src/daemon/handlers/record-trace.ts +154 -0
- package/src/daemon/handlers/session.ts +732 -0
- package/src/daemon/handlers/snapshot.ts +396 -0
- package/src/daemon/is-predicates.ts +46 -0
- package/src/daemon/selectors.ts +423 -0
- package/src/daemon/session-routing.ts +22 -0
- package/src/daemon/session-selector.ts +39 -0
- package/src/daemon/session-store.ts +275 -0
- package/src/daemon/snapshot-processing.ts +127 -0
- package/src/daemon/types.ts +55 -0
- package/src/daemon.ts +66 -1592
- package/src/platforms/ios/index.ts +0 -62
- package/src/platforms/ios/runner-client.ts +2 -0
- package/src/utils/args.ts +19 -10
- package/src/utils/interactors.ts +102 -16
- package/src/utils/snapshot.ts +1 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { dispatchCommand, resolveTargetDevice } from '../../core/dispatch.ts';
|
|
2
|
+
import { findNodeByLocator, type FindLocator } from '../../utils/finders.ts';
|
|
3
|
+
import { attachRefs, centerOfRect, type RawSnapshotNode, type SnapshotState } from '../../utils/snapshot.ts';
|
|
4
|
+
import { AppError } from '../../utils/errors.ts';
|
|
5
|
+
import type { DaemonRequest, DaemonResponse } from '../types.ts';
|
|
6
|
+
import { SessionStore } from '../session-store.ts';
|
|
7
|
+
import { contextFromFlags } from '../context.ts';
|
|
8
|
+
import { ensureDeviceReady } from '../device-ready.ts';
|
|
9
|
+
import {
|
|
10
|
+
extractNodeText,
|
|
11
|
+
findNearestHittableAncestor,
|
|
12
|
+
pruneGroupNodes,
|
|
13
|
+
} from '../snapshot-processing.ts';
|
|
14
|
+
import { parseTimeout } from './parse-utils.ts';
|
|
15
|
+
|
|
16
|
+
export async function handleFindCommands(params: {
|
|
17
|
+
req: DaemonRequest;
|
|
18
|
+
sessionName: string;
|
|
19
|
+
logPath: string;
|
|
20
|
+
sessionStore: SessionStore;
|
|
21
|
+
invoke: (req: DaemonRequest) => Promise<DaemonResponse>;
|
|
22
|
+
}): Promise<DaemonResponse | null> {
|
|
23
|
+
const { req, sessionName, logPath, sessionStore, invoke } = params;
|
|
24
|
+
const command = req.command;
|
|
25
|
+
if (command !== 'find') return null;
|
|
26
|
+
|
|
27
|
+
const args = req.positionals ?? [];
|
|
28
|
+
if (args.length === 0) {
|
|
29
|
+
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find requires a locator or text' } };
|
|
30
|
+
}
|
|
31
|
+
const { locator, query, action, value, timeoutMs } = parseFindArgs(args);
|
|
32
|
+
if (!query) {
|
|
33
|
+
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find requires a value' } };
|
|
34
|
+
}
|
|
35
|
+
const session = sessionStore.get(sessionName);
|
|
36
|
+
const isReadOnly = action === 'exists' || action === 'wait' || action === 'get_text' || action === 'get_attrs';
|
|
37
|
+
if (!session && !isReadOnly) {
|
|
38
|
+
return {
|
|
39
|
+
ok: false,
|
|
40
|
+
error: { code: 'SESSION_NOT_FOUND', message: 'No active session. Run open first.' },
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const device = session?.device ?? (await resolveTargetDevice(req.flags ?? {}));
|
|
44
|
+
if (!session) {
|
|
45
|
+
await ensureDeviceReady(device);
|
|
46
|
+
}
|
|
47
|
+
const appBundleId = session?.appBundleId;
|
|
48
|
+
const scope = shouldScopeFind(locator) ? query : undefined;
|
|
49
|
+
const requiresRect = action === 'click' || action === 'focus' || action === 'fill' || action === 'type';
|
|
50
|
+
const interactiveOnly = requiresRect;
|
|
51
|
+
let lastSnapshotAt = 0;
|
|
52
|
+
let lastNodes: SnapshotState['nodes'] | null = null;
|
|
53
|
+
const fetchNodes = async (): Promise<{
|
|
54
|
+
nodes: SnapshotState['nodes'];
|
|
55
|
+
truncated?: boolean;
|
|
56
|
+
backend?: SnapshotState['backend'];
|
|
57
|
+
}> => {
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
if (lastNodes && now - lastSnapshotAt < 750) {
|
|
60
|
+
return { nodes: lastNodes };
|
|
61
|
+
}
|
|
62
|
+
const data = (await dispatchCommand(device, 'snapshot', [], req.flags?.out, {
|
|
63
|
+
...contextFromFlags(
|
|
64
|
+
logPath,
|
|
65
|
+
{
|
|
66
|
+
...req.flags,
|
|
67
|
+
snapshotScope: scope,
|
|
68
|
+
snapshotInteractiveOnly: interactiveOnly,
|
|
69
|
+
snapshotCompact: interactiveOnly,
|
|
70
|
+
},
|
|
71
|
+
appBundleId,
|
|
72
|
+
session?.trace?.outPath,
|
|
73
|
+
),
|
|
74
|
+
})) as {
|
|
75
|
+
nodes?: RawSnapshotNode[];
|
|
76
|
+
truncated?: boolean;
|
|
77
|
+
backend?: 'ax' | 'xctest' | 'android';
|
|
78
|
+
};
|
|
79
|
+
const rawNodes = data?.nodes ?? [];
|
|
80
|
+
const nodes = attachRefs(req.flags?.snapshotRaw ? rawNodes : pruneGroupNodes(rawNodes));
|
|
81
|
+
lastSnapshotAt = now;
|
|
82
|
+
lastNodes = nodes;
|
|
83
|
+
if (session) {
|
|
84
|
+
const snapshot: SnapshotState = {
|
|
85
|
+
nodes,
|
|
86
|
+
truncated: data?.truncated,
|
|
87
|
+
createdAt: Date.now(),
|
|
88
|
+
backend: data?.backend,
|
|
89
|
+
};
|
|
90
|
+
session.snapshot = snapshot;
|
|
91
|
+
sessionStore.set(sessionName, session);
|
|
92
|
+
}
|
|
93
|
+
return { nodes, truncated: data?.truncated, backend: data?.backend };
|
|
94
|
+
};
|
|
95
|
+
if (action === 'wait') {
|
|
96
|
+
const timeout = timeoutMs ?? 10000;
|
|
97
|
+
const start = Date.now();
|
|
98
|
+
while (Date.now() - start < timeout) {
|
|
99
|
+
const { nodes } = await fetchNodes();
|
|
100
|
+
const match = findNodeByLocator(nodes, locator, query, { requireRect: false });
|
|
101
|
+
if (match) {
|
|
102
|
+
if (session) {
|
|
103
|
+
sessionStore.recordAction(session, {
|
|
104
|
+
command,
|
|
105
|
+
positionals: req.positionals ?? [],
|
|
106
|
+
flags: req.flags ?? {},
|
|
107
|
+
result: { found: true, waitedMs: Date.now() - start },
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return { ok: true, data: { found: true, waitedMs: Date.now() - start } };
|
|
111
|
+
}
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
113
|
+
}
|
|
114
|
+
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'find wait timed out' } };
|
|
115
|
+
}
|
|
116
|
+
const { nodes } = await fetchNodes();
|
|
117
|
+
const node = findNodeByLocator(nodes, locator, query, { requireRect: requiresRect });
|
|
118
|
+
if (!node) {
|
|
119
|
+
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'find did not match any element' } };
|
|
120
|
+
}
|
|
121
|
+
const resolvedNode =
|
|
122
|
+
action === 'click' || action === 'focus' || action === 'fill' || action === 'type'
|
|
123
|
+
? findNearestHittableAncestor(nodes, node) ?? node
|
|
124
|
+
: node;
|
|
125
|
+
const ref = `@${resolvedNode.ref}`;
|
|
126
|
+
const actionFlags = { ...(req.flags ?? {}), noRecord: true };
|
|
127
|
+
if (action === 'exists') {
|
|
128
|
+
if (session) {
|
|
129
|
+
sessionStore.recordAction(session, {
|
|
130
|
+
command,
|
|
131
|
+
positionals: req.positionals ?? [],
|
|
132
|
+
flags: req.flags ?? {},
|
|
133
|
+
result: { found: true },
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return { ok: true, data: { found: true } };
|
|
137
|
+
}
|
|
138
|
+
if (action === 'get_text') {
|
|
139
|
+
const text = extractNodeText(node);
|
|
140
|
+
if (session) {
|
|
141
|
+
sessionStore.recordAction(session, {
|
|
142
|
+
command,
|
|
143
|
+
positionals: req.positionals ?? [],
|
|
144
|
+
flags: req.flags ?? {},
|
|
145
|
+
result: { ref, action: 'get text', text },
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return { ok: true, data: { ref, text, node } };
|
|
149
|
+
}
|
|
150
|
+
if (action === 'get_attrs') {
|
|
151
|
+
if (session) {
|
|
152
|
+
sessionStore.recordAction(session, {
|
|
153
|
+
command,
|
|
154
|
+
positionals: req.positionals ?? [],
|
|
155
|
+
flags: req.flags ?? {},
|
|
156
|
+
result: { ref, action: 'get attrs' },
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return { ok: true, data: { ref, node } };
|
|
160
|
+
}
|
|
161
|
+
if (action === 'click') {
|
|
162
|
+
const response = await invoke({
|
|
163
|
+
token: req.token,
|
|
164
|
+
session: sessionName,
|
|
165
|
+
command: 'click',
|
|
166
|
+
positionals: [ref],
|
|
167
|
+
flags: actionFlags,
|
|
168
|
+
});
|
|
169
|
+
if (!response.ok) return response;
|
|
170
|
+
if (session) {
|
|
171
|
+
sessionStore.recordAction(session, {
|
|
172
|
+
command,
|
|
173
|
+
positionals: req.positionals ?? [],
|
|
174
|
+
flags: req.flags ?? {},
|
|
175
|
+
result: { ref, action: 'click' },
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return response;
|
|
179
|
+
}
|
|
180
|
+
if (action === 'fill') {
|
|
181
|
+
if (!value) {
|
|
182
|
+
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find fill requires text' } };
|
|
183
|
+
}
|
|
184
|
+
const response = await invoke({
|
|
185
|
+
token: req.token,
|
|
186
|
+
session: sessionName,
|
|
187
|
+
command: 'fill',
|
|
188
|
+
positionals: [ref, value],
|
|
189
|
+
flags: actionFlags,
|
|
190
|
+
});
|
|
191
|
+
if (!response.ok) return response;
|
|
192
|
+
if (session) {
|
|
193
|
+
sessionStore.recordAction(session, {
|
|
194
|
+
command,
|
|
195
|
+
positionals: req.positionals ?? [],
|
|
196
|
+
flags: req.flags ?? {},
|
|
197
|
+
result: { ref, action: 'fill' },
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return response;
|
|
201
|
+
}
|
|
202
|
+
if (action === 'focus') {
|
|
203
|
+
const coords = node.rect ? centerOfRect(node.rect) : null;
|
|
204
|
+
if (!coords) {
|
|
205
|
+
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'matched element has no bounds' } };
|
|
206
|
+
}
|
|
207
|
+
const response = await dispatchCommand(device, 'focus', [String(coords.x), String(coords.y)], req.flags?.out, {
|
|
208
|
+
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
209
|
+
});
|
|
210
|
+
if (session) {
|
|
211
|
+
sessionStore.recordAction(session, {
|
|
212
|
+
command,
|
|
213
|
+
positionals: req.positionals ?? [],
|
|
214
|
+
flags: req.flags ?? {},
|
|
215
|
+
result: { ref, action: 'focus' },
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return { ok: true, data: response ?? { ref } };
|
|
219
|
+
}
|
|
220
|
+
if (action === 'type') {
|
|
221
|
+
if (!value) {
|
|
222
|
+
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find type requires text' } };
|
|
223
|
+
}
|
|
224
|
+
const coords = node.rect ? centerOfRect(node.rect) : null;
|
|
225
|
+
if (!coords) {
|
|
226
|
+
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'matched element has no bounds' } };
|
|
227
|
+
}
|
|
228
|
+
await dispatchCommand(device, 'focus', [String(coords.x), String(coords.y)], req.flags?.out, {
|
|
229
|
+
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
230
|
+
});
|
|
231
|
+
const response = await dispatchCommand(device, 'type', [value], req.flags?.out, {
|
|
232
|
+
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
233
|
+
});
|
|
234
|
+
if (session) {
|
|
235
|
+
sessionStore.recordAction(session, {
|
|
236
|
+
command,
|
|
237
|
+
positionals: req.positionals ?? [],
|
|
238
|
+
flags: req.flags ?? {},
|
|
239
|
+
result: { ref, action: 'type' },
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return { ok: true, data: response ?? { ref } };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
type FindAction =
|
|
249
|
+
| { kind: 'click' }
|
|
250
|
+
| { kind: 'focus' }
|
|
251
|
+
| { kind: 'fill'; value: string }
|
|
252
|
+
| { kind: 'type'; value: string }
|
|
253
|
+
| { kind: 'get_text' }
|
|
254
|
+
| { kind: 'get_attrs' }
|
|
255
|
+
| { kind: 'exists' }
|
|
256
|
+
| { kind: 'wait'; timeoutMs?: number };
|
|
257
|
+
|
|
258
|
+
export function parseFindArgs(args: string[]): {
|
|
259
|
+
locator: FindLocator;
|
|
260
|
+
query: string;
|
|
261
|
+
action: FindAction['kind'];
|
|
262
|
+
value?: string;
|
|
263
|
+
timeoutMs?: number;
|
|
264
|
+
} {
|
|
265
|
+
const locatorTokens: FindLocator[] = ['text', 'label', 'value', 'role', 'id'];
|
|
266
|
+
let locator: FindLocator = 'any';
|
|
267
|
+
let queryIndex = 0;
|
|
268
|
+
if (locatorTokens.includes(args[0] as FindLocator)) {
|
|
269
|
+
locator = args[0] as FindLocator;
|
|
270
|
+
queryIndex = 1;
|
|
271
|
+
}
|
|
272
|
+
const query = args[queryIndex] ?? '';
|
|
273
|
+
const actionTokens = args.slice(queryIndex + 1);
|
|
274
|
+
if (actionTokens.length === 0) {
|
|
275
|
+
return { locator, query, action: 'click' };
|
|
276
|
+
}
|
|
277
|
+
const action = actionTokens[0].toLowerCase();
|
|
278
|
+
if (action === 'get') {
|
|
279
|
+
const sub = actionTokens[1]?.toLowerCase();
|
|
280
|
+
if (sub === 'text') return { locator, query, action: 'get_text' };
|
|
281
|
+
if (sub === 'attrs') return { locator, query, action: 'get_attrs' };
|
|
282
|
+
throw new AppError('INVALID_ARGS', 'find get only supports text or attrs');
|
|
283
|
+
}
|
|
284
|
+
if (action === 'wait') {
|
|
285
|
+
const timeoutMs = parseTimeout(actionTokens[1]);
|
|
286
|
+
return { locator, query, action: 'wait', timeoutMs: timeoutMs ?? undefined };
|
|
287
|
+
}
|
|
288
|
+
if (action === 'exists') return { locator, query, action: 'exists' };
|
|
289
|
+
if (action === 'click') return { locator, query, action: 'click' };
|
|
290
|
+
if (action === 'focus') return { locator, query, action: 'focus' };
|
|
291
|
+
if (action === 'fill') {
|
|
292
|
+
const value = actionTokens.slice(1).join(' ');
|
|
293
|
+
return { locator, query, action: 'fill', value };
|
|
294
|
+
}
|
|
295
|
+
if (action === 'type') {
|
|
296
|
+
const value = actionTokens.slice(1).join(' ');
|
|
297
|
+
return { locator, query, action: 'type', value };
|
|
298
|
+
}
|
|
299
|
+
throw new AppError('INVALID_ARGS', `Unsupported find action: ${actionTokens[0]}`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function shouldScopeFind(locator: FindLocator): boolean {
|
|
303
|
+
return locator !== 'role';
|
|
304
|
+
}
|