agent-device 0.4.2 → 0.5.0
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 +2 -9
- package/dist/src/797.js +1 -1
- package/dist/src/bin.js +5 -5
- package/dist/src/daemon.js +16 -20
- package/package.json +7 -9
- package/skills/agent-device/SKILL.md +3 -6
- package/skills/agent-device/references/permissions.md +3 -15
- package/skills/agent-device/references/snapshot-refs.md +1 -4
- package/dist/bin/axsnapshot +0 -0
- package/ios-runner/AXSnapshot/Package.swift +0 -18
- package/ios-runner/AXSnapshot/Sources/AXSnapshot/main.swift +0 -444
- package/src/__tests__/cli-close.test.ts +0 -155
- package/src/__tests__/cli-help.test.ts +0 -102
- package/src/bin.ts +0 -3
- package/src/cli.ts +0 -305
- package/src/core/__tests__/capabilities.test.ts +0 -75
- package/src/core/__tests__/dispatch-open.test.ts +0 -25
- package/src/core/__tests__/open-target.test.ts +0 -55
- package/src/core/capabilities.ts +0 -57
- package/src/core/dispatch.ts +0 -382
- package/src/core/open-target.ts +0 -27
- package/src/daemon/__tests__/device-ready.test.ts +0 -52
- package/src/daemon/__tests__/is-predicates.test.ts +0 -68
- package/src/daemon/__tests__/selectors.test.ts +0 -261
- package/src/daemon/__tests__/session-routing.test.ts +0 -108
- package/src/daemon/__tests__/session-selector.test.ts +0 -64
- package/src/daemon/__tests__/session-store.test.ts +0 -142
- package/src/daemon/__tests__/snapshot-processing.test.ts +0 -47
- package/src/daemon/action-utils.ts +0 -29
- package/src/daemon/context.ts +0 -48
- package/src/daemon/device-ready.ts +0 -155
- package/src/daemon/handlers/__tests__/find.test.ts +0 -99
- package/src/daemon/handlers/__tests__/interaction.test.ts +0 -22
- package/src/daemon/handlers/__tests__/replay-heal.test.ts +0 -509
- package/src/daemon/handlers/__tests__/session-reinstall.test.ts +0 -219
- package/src/daemon/handlers/__tests__/session.test.ts +0 -820
- package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +0 -92
- package/src/daemon/handlers/__tests__/snapshot.test.ts +0 -128
- package/src/daemon/handlers/find.ts +0 -324
- package/src/daemon/handlers/interaction.ts +0 -550
- package/src/daemon/handlers/parse-utils.ts +0 -8
- package/src/daemon/handlers/record-trace.ts +0 -154
- package/src/daemon/handlers/session.ts +0 -1137
- package/src/daemon/handlers/snapshot.ts +0 -439
- package/src/daemon/is-predicates.ts +0 -46
- package/src/daemon/selectors.ts +0 -540
- package/src/daemon/session-routing.ts +0 -22
- package/src/daemon/session-selector.ts +0 -39
- package/src/daemon/session-store.ts +0 -296
- package/src/daemon/snapshot-processing.ts +0 -131
- package/src/daemon/types.ts +0 -56
- package/src/daemon-client.ts +0 -272
- package/src/daemon.ts +0 -295
- package/src/platforms/__tests__/boot-diagnostics.test.ts +0 -59
- package/src/platforms/android/__tests__/index.test.ts +0 -274
- package/src/platforms/android/devices.ts +0 -196
- package/src/platforms/android/index.ts +0 -784
- package/src/platforms/android/ui-hierarchy.ts +0 -312
- package/src/platforms/boot-diagnostics.ts +0 -128
- package/src/platforms/ios/__tests__/index.test.ts +0 -312
- package/src/platforms/ios/__tests__/runner-client.test.ts +0 -155
- package/src/platforms/ios/apps.ts +0 -358
- package/src/platforms/ios/ax-snapshot.ts +0 -207
- package/src/platforms/ios/config.ts +0 -28
- package/src/platforms/ios/devicectl.ts +0 -134
- package/src/platforms/ios/devices.ts +0 -100
- package/src/platforms/ios/index.ts +0 -20
- package/src/platforms/ios/runner-client.ts +0 -994
- package/src/platforms/ios/simulator.ts +0 -164
- package/src/utils/__tests__/args.test.ts +0 -239
- package/src/utils/__tests__/daemon-client.test.ts +0 -95
- package/src/utils/__tests__/exec.test.ts +0 -16
- package/src/utils/__tests__/finders.test.ts +0 -34
- package/src/utils/__tests__/keyed-lock.test.ts +0 -55
- package/src/utils/__tests__/process-identity.test.ts +0 -33
- package/src/utils/__tests__/retry.test.ts +0 -44
- package/src/utils/args.ts +0 -239
- package/src/utils/command-schema.ts +0 -622
- package/src/utils/device.ts +0 -84
- package/src/utils/errors.ts +0 -35
- package/src/utils/exec.ts +0 -339
- package/src/utils/finders.ts +0 -101
- package/src/utils/interactive.ts +0 -4
- package/src/utils/interactors.ts +0 -173
- package/src/utils/keyed-lock.ts +0 -14
- package/src/utils/output.ts +0 -204
- package/src/utils/process-identity.ts +0 -100
- package/src/utils/retry.ts +0 -180
- package/src/utils/snapshot.ts +0 -64
- package/src/utils/timeouts.ts +0 -9
- package/src/utils/version.ts +0 -26
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { handleSnapshotCommands } from '../snapshot.ts';
|
|
7
|
-
import { SessionStore } from '../../session-store.ts';
|
|
8
|
-
import type { SessionState } from '../../types.ts';
|
|
9
|
-
|
|
10
|
-
function makeSessionStore(): SessionStore {
|
|
11
|
-
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-snapshot-handler-'));
|
|
12
|
-
return new SessionStore(path.join(root, 'sessions'));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function makeSession(name: string, device: SessionState['device']): SessionState {
|
|
16
|
-
return {
|
|
17
|
-
name,
|
|
18
|
-
device,
|
|
19
|
-
createdAt: Date.now(),
|
|
20
|
-
actions: [],
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test('snapshot rejects AX backend on iOS physical devices', async () => {
|
|
25
|
-
const sessionStore = makeSessionStore();
|
|
26
|
-
const sessionName = 'ios-device';
|
|
27
|
-
sessionStore.set(
|
|
28
|
-
sessionName,
|
|
29
|
-
makeSession(sessionName, {
|
|
30
|
-
platform: 'ios',
|
|
31
|
-
id: 'ios-device-1',
|
|
32
|
-
name: 'My iPhone',
|
|
33
|
-
kind: 'device',
|
|
34
|
-
booted: true,
|
|
35
|
-
}),
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const response = await handleSnapshotCommands({
|
|
39
|
-
req: {
|
|
40
|
-
token: 't',
|
|
41
|
-
session: sessionName,
|
|
42
|
-
command: 'snapshot',
|
|
43
|
-
positionals: [],
|
|
44
|
-
flags: { snapshotBackend: 'ax' },
|
|
45
|
-
},
|
|
46
|
-
sessionName,
|
|
47
|
-
logPath: '/tmp/daemon.log',
|
|
48
|
-
sessionStore,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
assert.ok(response);
|
|
52
|
-
assert.equal(response?.ok, false);
|
|
53
|
-
if (response && !response.ok) {
|
|
54
|
-
assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
|
|
55
|
-
assert.match(response.error.message, /AX snapshot backend is not supported/i);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('settings rejects unsupported iOS physical devices', async () => {
|
|
60
|
-
const sessionStore = makeSessionStore();
|
|
61
|
-
const sessionName = 'ios-device';
|
|
62
|
-
sessionStore.set(
|
|
63
|
-
sessionName,
|
|
64
|
-
makeSession(sessionName, {
|
|
65
|
-
platform: 'ios',
|
|
66
|
-
id: 'ios-device-1',
|
|
67
|
-
name: 'My iPhone',
|
|
68
|
-
kind: 'device',
|
|
69
|
-
booted: true,
|
|
70
|
-
}),
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const response = await handleSnapshotCommands({
|
|
74
|
-
req: {
|
|
75
|
-
token: 't',
|
|
76
|
-
session: sessionName,
|
|
77
|
-
command: 'settings',
|
|
78
|
-
positionals: ['wifi', 'on'],
|
|
79
|
-
flags: {},
|
|
80
|
-
},
|
|
81
|
-
sessionName,
|
|
82
|
-
logPath: '/tmp/daemon.log',
|
|
83
|
-
sessionStore,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
assert.ok(response);
|
|
87
|
-
assert.equal(response?.ok, false);
|
|
88
|
-
if (response && !response.ok) {
|
|
89
|
-
assert.equal(response.error.code, 'UNSUPPORTED_OPERATION');
|
|
90
|
-
assert.match(response.error.message, /settings is not supported/i);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import { parseWaitArgs } from '../snapshot.ts';
|
|
4
|
-
import { parseTimeout } from '../parse-utils.ts';
|
|
5
|
-
|
|
6
|
-
// --- parseTimeout ---
|
|
7
|
-
|
|
8
|
-
test('parseTimeout parses integer string', () => {
|
|
9
|
-
assert.equal(parseTimeout('500'), 500);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test('parseTimeout parses zero', () => {
|
|
13
|
-
assert.equal(parseTimeout('0'), 0);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('parseTimeout returns null for non-numeric string', () => {
|
|
17
|
-
assert.equal(parseTimeout('abc'), null);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('parseTimeout returns null for Infinity', () => {
|
|
21
|
-
assert.equal(parseTimeout('Infinity'), null);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// --- parseWaitArgs ---
|
|
25
|
-
|
|
26
|
-
test('parseWaitArgs returns null for empty args', () => {
|
|
27
|
-
assert.equal(parseWaitArgs([]), null);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('parseWaitArgs returns sleep for numeric first arg', () => {
|
|
31
|
-
const result = parseWaitArgs(['500']);
|
|
32
|
-
assert.deepEqual(result, { kind: 'sleep', durationMs: 500 });
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('parseWaitArgs returns sleep for zero', () => {
|
|
36
|
-
const result = parseWaitArgs(['0']);
|
|
37
|
-
assert.deepEqual(result, { kind: 'sleep', durationMs: 0 });
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('parseWaitArgs parses text keyword with label', () => {
|
|
41
|
-
const result = parseWaitArgs(['text', 'Loading']);
|
|
42
|
-
assert.deepEqual(result, { kind: 'text', text: 'Loading', timeoutMs: null });
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
test('parseWaitArgs parses text keyword with timeout', () => {
|
|
46
|
-
const result = parseWaitArgs(['text', 'Loading', '5000']);
|
|
47
|
-
assert.deepEqual(result, { kind: 'text', text: 'Loading', timeoutMs: 5000 });
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('parseWaitArgs parses text keyword with multi-word and timeout', () => {
|
|
51
|
-
const result = parseWaitArgs(['text', 'Sign', 'In', '3000']);
|
|
52
|
-
assert.deepEqual(result, { kind: 'text', text: 'Sign In', timeoutMs: 3000 });
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('parseWaitArgs parses text keyword with multi-word and no timeout', () => {
|
|
56
|
-
const result = parseWaitArgs(['text', 'Sign', 'In']);
|
|
57
|
-
assert.deepEqual(result, { kind: 'text', text: 'Sign In', timeoutMs: null });
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test('parseWaitArgs text keyword alone yields empty text', () => {
|
|
61
|
-
const result = parseWaitArgs(['text']);
|
|
62
|
-
assert.deepEqual(result, { kind: 'text', text: '', timeoutMs: null });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
test('parseWaitArgs parses ref', () => {
|
|
66
|
-
const result = parseWaitArgs(['@e3']);
|
|
67
|
-
assert.deepEqual(result, { kind: 'ref', rawRef: '@e3', timeoutMs: null });
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test('parseWaitArgs parses ref with timeout', () => {
|
|
71
|
-
const result = parseWaitArgs(['@e3', '5000']);
|
|
72
|
-
assert.deepEqual(result, { kind: 'ref', rawRef: '@e3', timeoutMs: 5000 });
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('parseWaitArgs parses ref with non-numeric trailing arg as no timeout', () => {
|
|
76
|
-
const result = parseWaitArgs(['@e3', 'abc']);
|
|
77
|
-
assert.deepEqual(result, { kind: 'ref', rawRef: '@e3', timeoutMs: null });
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test('parseWaitArgs parses bare text', () => {
|
|
81
|
-
const result = parseWaitArgs(['Hello']);
|
|
82
|
-
assert.deepEqual(result, { kind: 'text', text: 'Hello', timeoutMs: null });
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('parseWaitArgs parses bare text with timeout', () => {
|
|
86
|
-
const result = parseWaitArgs(['Hello', '5000']);
|
|
87
|
-
assert.deepEqual(result, { kind: 'text', text: 'Hello', timeoutMs: 5000 });
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test('parseWaitArgs parses selector expression', () => {
|
|
91
|
-
const result = parseWaitArgs(['id=login_email']);
|
|
92
|
-
assert.ok(result);
|
|
93
|
-
assert.equal(result.kind, 'selector');
|
|
94
|
-
if (result.kind === 'selector') {
|
|
95
|
-
assert.equal(result.selectorExpression, 'id=login_email');
|
|
96
|
-
assert.equal(result.timeoutMs, null);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('parseWaitArgs parses selector expression with timeout', () => {
|
|
101
|
-
const result = parseWaitArgs(['id=login_email', '5000']);
|
|
102
|
-
assert.ok(result);
|
|
103
|
-
assert.equal(result.kind, 'selector');
|
|
104
|
-
if (result.kind === 'selector') {
|
|
105
|
-
assert.equal(result.selectorExpression, 'id=login_email');
|
|
106
|
-
assert.equal(result.timeoutMs, 5000);
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('parseWaitArgs falls back to text when selector-like token is invalid', () => {
|
|
111
|
-
const result = parseWaitArgs(['foo=bar', '5000']);
|
|
112
|
-
assert.deepEqual(result, { kind: 'text', text: 'foo=bar', timeoutMs: 5000 });
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('parseWaitArgs parses bare multi-word text', () => {
|
|
116
|
-
const result = parseWaitArgs(['Sign', 'In']);
|
|
117
|
-
assert.deepEqual(result, { kind: 'text', text: 'Sign In', timeoutMs: null });
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test('parseWaitArgs parses bare multi-word text with timeout', () => {
|
|
121
|
-
const result = parseWaitArgs(['Sign', 'In', '3000']);
|
|
122
|
-
assert.deepEqual(result, { kind: 'text', text: 'Sign In', timeoutMs: 3000 });
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('parseWaitArgs text keyword with non-numeric trailing leaves timeoutMs null', () => {
|
|
126
|
-
const result = parseWaitArgs(['text', 'Loading', 'abc']);
|
|
127
|
-
assert.deepEqual(result, { kind: 'text', text: 'Loading abc', timeoutMs: null });
|
|
128
|
-
});
|
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
import { dispatchCommand, resolveTargetDevice } from '../../core/dispatch.ts';
|
|
2
|
-
import { findBestMatchesByLocator, 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 = findBestMatchesByLocator(nodes, locator, query, { requireRect: false }).matches[0];
|
|
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 bestMatches = findBestMatchesByLocator(nodes, locator, query, { requireRect: requiresRect });
|
|
118
|
-
if (requiresRect && bestMatches.matches.length > 1) {
|
|
119
|
-
const candidates = bestMatches.matches.slice(0, 8).map((candidate) => {
|
|
120
|
-
const label = extractNodeText(candidate) || candidate.label || candidate.identifier || candidate.type || '';
|
|
121
|
-
return `@${candidate.ref}${label ? `(${label})` : ''}`;
|
|
122
|
-
});
|
|
123
|
-
return {
|
|
124
|
-
ok: false,
|
|
125
|
-
error: {
|
|
126
|
-
code: 'AMBIGUOUS_MATCH',
|
|
127
|
-
message: `find matched ${bestMatches.matches.length} elements for ${locator} "${query}". Use a more specific locator or selector.`,
|
|
128
|
-
details: {
|
|
129
|
-
locator,
|
|
130
|
-
query,
|
|
131
|
-
matches: bestMatches.matches.length,
|
|
132
|
-
candidates,
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
const node = bestMatches.matches[0] ?? null;
|
|
138
|
-
if (!node) {
|
|
139
|
-
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'find did not match any element' } };
|
|
140
|
-
}
|
|
141
|
-
const resolvedNode =
|
|
142
|
-
action === 'click' || action === 'focus' || action === 'fill' || action === 'type'
|
|
143
|
-
? findNearestHittableAncestor(nodes, node) ?? node
|
|
144
|
-
: node;
|
|
145
|
-
const ref = `@${resolvedNode.ref}`;
|
|
146
|
-
const actionFlags = { ...(req.flags ?? {}), noRecord: true };
|
|
147
|
-
if (action === 'exists') {
|
|
148
|
-
if (session) {
|
|
149
|
-
sessionStore.recordAction(session, {
|
|
150
|
-
command,
|
|
151
|
-
positionals: req.positionals ?? [],
|
|
152
|
-
flags: req.flags ?? {},
|
|
153
|
-
result: { found: true },
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
return { ok: true, data: { found: true } };
|
|
157
|
-
}
|
|
158
|
-
if (action === 'get_text') {
|
|
159
|
-
const text = extractNodeText(node);
|
|
160
|
-
if (session) {
|
|
161
|
-
sessionStore.recordAction(session, {
|
|
162
|
-
command,
|
|
163
|
-
positionals: req.positionals ?? [],
|
|
164
|
-
flags: req.flags ?? {},
|
|
165
|
-
result: { ref, action: 'get text', text },
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
return { ok: true, data: { ref, text, node } };
|
|
169
|
-
}
|
|
170
|
-
if (action === 'get_attrs') {
|
|
171
|
-
if (session) {
|
|
172
|
-
sessionStore.recordAction(session, {
|
|
173
|
-
command,
|
|
174
|
-
positionals: req.positionals ?? [],
|
|
175
|
-
flags: req.flags ?? {},
|
|
176
|
-
result: { ref, action: 'get attrs' },
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
return { ok: true, data: { ref, node } };
|
|
180
|
-
}
|
|
181
|
-
if (action === 'click') {
|
|
182
|
-
const response = await invoke({
|
|
183
|
-
token: req.token,
|
|
184
|
-
session: sessionName,
|
|
185
|
-
command: 'click',
|
|
186
|
-
positionals: [ref],
|
|
187
|
-
flags: actionFlags,
|
|
188
|
-
});
|
|
189
|
-
if (!response.ok) return response;
|
|
190
|
-
if (session) {
|
|
191
|
-
sessionStore.recordAction(session, {
|
|
192
|
-
command,
|
|
193
|
-
positionals: req.positionals ?? [],
|
|
194
|
-
flags: req.flags ?? {},
|
|
195
|
-
result: { ref, action: 'click' },
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
return response;
|
|
199
|
-
}
|
|
200
|
-
if (action === 'fill') {
|
|
201
|
-
if (!value) {
|
|
202
|
-
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find fill requires text' } };
|
|
203
|
-
}
|
|
204
|
-
const response = await invoke({
|
|
205
|
-
token: req.token,
|
|
206
|
-
session: sessionName,
|
|
207
|
-
command: 'fill',
|
|
208
|
-
positionals: [ref, value],
|
|
209
|
-
flags: actionFlags,
|
|
210
|
-
});
|
|
211
|
-
if (!response.ok) return response;
|
|
212
|
-
if (session) {
|
|
213
|
-
sessionStore.recordAction(session, {
|
|
214
|
-
command,
|
|
215
|
-
positionals: req.positionals ?? [],
|
|
216
|
-
flags: req.flags ?? {},
|
|
217
|
-
result: { ref, action: 'fill' },
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
return response;
|
|
221
|
-
}
|
|
222
|
-
if (action === 'focus') {
|
|
223
|
-
const coords = node.rect ? centerOfRect(node.rect) : null;
|
|
224
|
-
if (!coords) {
|
|
225
|
-
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'matched element has no bounds' } };
|
|
226
|
-
}
|
|
227
|
-
const response = await dispatchCommand(device, 'focus', [String(coords.x), String(coords.y)], req.flags?.out, {
|
|
228
|
-
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
229
|
-
});
|
|
230
|
-
if (session) {
|
|
231
|
-
sessionStore.recordAction(session, {
|
|
232
|
-
command,
|
|
233
|
-
positionals: req.positionals ?? [],
|
|
234
|
-
flags: req.flags ?? {},
|
|
235
|
-
result: { ref, action: 'focus' },
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
return { ok: true, data: response ?? { ref } };
|
|
239
|
-
}
|
|
240
|
-
if (action === 'type') {
|
|
241
|
-
if (!value) {
|
|
242
|
-
return { ok: false, error: { code: 'INVALID_ARGS', message: 'find type requires text' } };
|
|
243
|
-
}
|
|
244
|
-
const coords = node.rect ? centerOfRect(node.rect) : null;
|
|
245
|
-
if (!coords) {
|
|
246
|
-
return { ok: false, error: { code: 'COMMAND_FAILED', message: 'matched element has no bounds' } };
|
|
247
|
-
}
|
|
248
|
-
await dispatchCommand(device, 'focus', [String(coords.x), String(coords.y)], req.flags?.out, {
|
|
249
|
-
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
250
|
-
});
|
|
251
|
-
const response = await dispatchCommand(device, 'type', [value], req.flags?.out, {
|
|
252
|
-
...contextFromFlags(logPath, req.flags, session?.appBundleId, session?.trace?.outPath),
|
|
253
|
-
});
|
|
254
|
-
if (session) {
|
|
255
|
-
sessionStore.recordAction(session, {
|
|
256
|
-
command,
|
|
257
|
-
positionals: req.positionals ?? [],
|
|
258
|
-
flags: req.flags ?? {},
|
|
259
|
-
result: { ref, action: 'type' },
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
return { ok: true, data: response ?? { ref } };
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
type FindAction =
|
|
269
|
-
| { kind: 'click' }
|
|
270
|
-
| { kind: 'focus' }
|
|
271
|
-
| { kind: 'fill'; value: string }
|
|
272
|
-
| { kind: 'type'; value: string }
|
|
273
|
-
| { kind: 'get_text' }
|
|
274
|
-
| { kind: 'get_attrs' }
|
|
275
|
-
| { kind: 'exists' }
|
|
276
|
-
| { kind: 'wait'; timeoutMs?: number };
|
|
277
|
-
|
|
278
|
-
export function parseFindArgs(args: string[]): {
|
|
279
|
-
locator: FindLocator;
|
|
280
|
-
query: string;
|
|
281
|
-
action: FindAction['kind'];
|
|
282
|
-
value?: string;
|
|
283
|
-
timeoutMs?: number;
|
|
284
|
-
} {
|
|
285
|
-
const locatorTokens: FindLocator[] = ['text', 'label', 'value', 'role', 'id'];
|
|
286
|
-
let locator: FindLocator = 'any';
|
|
287
|
-
let queryIndex = 0;
|
|
288
|
-
if (locatorTokens.includes(args[0] as FindLocator)) {
|
|
289
|
-
locator = args[0] as FindLocator;
|
|
290
|
-
queryIndex = 1;
|
|
291
|
-
}
|
|
292
|
-
const query = args[queryIndex] ?? '';
|
|
293
|
-
const actionTokens = args.slice(queryIndex + 1);
|
|
294
|
-
if (actionTokens.length === 0) {
|
|
295
|
-
return { locator, query, action: 'click' };
|
|
296
|
-
}
|
|
297
|
-
const action = actionTokens[0].toLowerCase();
|
|
298
|
-
if (action === 'get') {
|
|
299
|
-
const sub = actionTokens[1]?.toLowerCase();
|
|
300
|
-
if (sub === 'text') return { locator, query, action: 'get_text' };
|
|
301
|
-
if (sub === 'attrs') return { locator, query, action: 'get_attrs' };
|
|
302
|
-
throw new AppError('INVALID_ARGS', 'find get only supports text or attrs');
|
|
303
|
-
}
|
|
304
|
-
if (action === 'wait') {
|
|
305
|
-
const timeoutMs = parseTimeout(actionTokens[1]);
|
|
306
|
-
return { locator, query, action: 'wait', timeoutMs: timeoutMs ?? undefined };
|
|
307
|
-
}
|
|
308
|
-
if (action === 'exists') return { locator, query, action: 'exists' };
|
|
309
|
-
if (action === 'click') return { locator, query, action: 'click' };
|
|
310
|
-
if (action === 'focus') return { locator, query, action: 'focus' };
|
|
311
|
-
if (action === 'fill') {
|
|
312
|
-
const value = actionTokens.slice(1).join(' ');
|
|
313
|
-
return { locator, query, action: 'fill', value };
|
|
314
|
-
}
|
|
315
|
-
if (action === 'type') {
|
|
316
|
-
const value = actionTokens.slice(1).join(' ');
|
|
317
|
-
return { locator, query, action: 'type', value };
|
|
318
|
-
}
|
|
319
|
-
throw new AppError('INVALID_ARGS', `Unsupported find action: ${actionTokens[0]}`);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function shouldScopeFind(locator: FindLocator): boolean {
|
|
323
|
-
return locator !== 'role';
|
|
324
|
-
}
|