Repository navigation
Expand file tree
/
Copy pathalert.ts
More file actions
202 lines (187 loc) · 6.49 KB
/
Copy pathalert.ts
File metadata and controls
202 lines (187 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
type AlertAction,
ALERT_ACTION_RETRY_MS,
ALERT_POLL_INTERVAL_MS,
DEFAULT_ALERT_TIMEOUT_MS,
} from '@agent-device/contracts/alert-contract';
import { AppError } from '@agent-device/kernel/errors';
import { withDiagnosticTimer } from '@agent-device/host-kit/diagnostics';
import { Deadline, pollUntilSettled } from '@agent-device/host-kit/retry';
import { successText } from '@agent-device/kernel/success-text';
import type { DeviceInfo } from '@agent-device/kernel/device';
import type { RawSnapshotNode } from '@agent-device/kernel/snapshot';
import {
chooseAndroidAlertButton,
findAndroidAlertCandidate,
type AndroidAlertCandidate,
type AndroidAlertInfo,
} from './alert-detection.ts';
import { backAndroid, pressAndroid } from './input-actions.ts';
type AndroidAlertOptions = {
timeoutMs?: number;
captureNodes: () => Promise<RawSnapshotNode[]>;
};
export type AndroidAlertResult =
| {
kind: 'alertStatus';
platform: 'android';
action: 'get';
alert: AndroidAlertInfo | null;
message?: string;
}
| {
kind: 'alertWait';
platform: 'android';
action: 'wait';
alert: AndroidAlertInfo;
waitedMs: number;
message?: string;
}
| {
kind: 'alertHandled';
platform: 'android';
action: 'accept' | 'dismiss';
handled: true;
alert: AndroidAlertInfo;
button: string;
coordinates?: { x: number; y: number };
message?: string;
};
export async function handleAndroidAlert(
device: DeviceInfo,
action: AlertAction,
options: AndroidAlertOptions,
): Promise<AndroidAlertResult> {
if (action === 'wait') {
return await waitForAndroidAlert(
options.captureNodes,
options.timeoutMs ?? DEFAULT_ALERT_TIMEOUT_MS,
);
}
if (action === 'get') {
const candidate = await readAndroidAlertCandidate(options.captureNodes);
return buildAndroidAlertStatusResponse(candidate?.alert ?? null);
}
return await handleAndroidAlertAction(device, action, options.captureNodes);
}
async function waitForAndroidAlert(
captureNodes: AndroidAlertOptions['captureNodes'],
timeoutMs: number,
): Promise<AndroidAlertResult> {
const start = Date.now();
const candidate = await pollAndroidAlertCandidate(captureNodes, timeoutMs);
if (!candidate) {
throw new AppError('COMMAND_FAILED', 'alert wait timed out');
}
return {
kind: 'alertWait',
platform: 'android',
action: 'wait',
alert: candidate.alert,
waitedMs: Date.now() - start,
...successText('Alert visible'),
};
}
async function handleAndroidAlertAction(
device: DeviceInfo,
action: 'accept' | 'dismiss',
captureNodes: AndroidAlertOptions['captureNodes'],
): Promise<AndroidAlertResult> {
const candidate = await pollAndroidAlertCandidate(captureNodes, ALERT_ACTION_RETRY_MS);
if (!candidate) {
throw new AppError('COMMAND_FAILED', 'alert not found', {
hint: 'If a sheet is visible in snapshot but alert reports no alert, it is likely app-owned UI. Use snapshot -i and press the visible label/ref.',
});
}
const button = chooseAndroidAlertButton(candidate.buttons, action);
if (button) {
await pressAndroid(device, button.x, button.y);
await confirmAndroidAlertDismissed(candidate.alert, action, captureNodes);
return buildAndroidAlertHandledResponse(action, candidate.alert, button.label, {
x: button.x,
y: button.y,
});
}
if (action === 'dismiss') {
await backAndroid(device);
await confirmAndroidAlertDismissed(candidate.alert, action, captureNodes);
return buildAndroidAlertHandledResponse(action, candidate.alert, 'Back');
}
throw new AppError('COMMAND_FAILED', 'alert accept found an alert but no accept button', {
alert: candidate.alert,
hint: 'Inspect alert get --json for visible buttons, then use press by visible label/ref if needed.',
});
}
/**
* `alert accept|dismiss` means the dialog is gone, not that a button was pressed: the next
* command reads the app, and a capture that lands while the dialog window is still up sees
* only the dialog. A different alert taking its place counts as dismissed. Bounded by the
* same budget the pre-press lookup uses; iOS's runner applies the same re-check.
*/
async function confirmAndroidAlertDismissed(
dismissed: AndroidAlertInfo,
action: 'accept' | 'dismiss',
captureNodes: AndroidAlertOptions['captureNodes'],
): Promise<void> {
const { settled } = await pollUntilSettled({
probe: () => readAndroidAlertCandidate(captureNodes),
settled: (current) => !current || !sameAndroidAlert(current.alert, dismissed),
deadline: Deadline.fromTimeoutMs(ALERT_ACTION_RETRY_MS),
intervalMs: ALERT_POLL_INTERVAL_MS,
});
if (settled) return;
throw new AppError('COMMAND_FAILED', `alert ${action} did not dismiss the visible alert`, {
alert: dismissed,
hint: 'The alert button was pressed but the dialog is still visible. Inspect alert get --json, then press the visible button by label/ref or retry.',
});
}
function sameAndroidAlert(left: AndroidAlertInfo, right: AndroidAlertInfo): boolean {
return left.title === right.title && left.buttons.join('\u0000') === right.buttons.join('\u0000');
}
async function pollAndroidAlertCandidate(
captureNodes: AndroidAlertOptions['captureNodes'],
timeoutMs: number,
): Promise<AndroidAlertCandidate | null> {
const { value } = await pollUntilSettled({
probe: () => readAndroidAlertCandidate(captureNodes),
settled: (candidate) => candidate !== null,
deadline: Deadline.fromTimeoutMs(timeoutMs),
intervalMs: ALERT_POLL_INTERVAL_MS,
});
return value;
}
async function readAndroidAlertCandidate(
captureNodes: AndroidAlertOptions['captureNodes'],
): Promise<AndroidAlertCandidate | null> {
const result = await withDiagnosticTimer('snapshot_capture', captureNodes, {
backend: 'android',
purpose: 'alert',
});
return findAndroidAlertCandidate(result);
}
function buildAndroidAlertStatusResponse(alert: AndroidAlertInfo | null): AndroidAlertResult {
return {
kind: 'alertStatus',
platform: 'android',
action: 'get',
alert,
...(alert ? successText('Alert visible') : successText('No alert visible')),
};
}
function buildAndroidAlertHandledResponse(
action: 'accept' | 'dismiss',
alert: AndroidAlertInfo,
button: string,
coordinates?: { x: number; y: number },
): AndroidAlertResult {
return {
kind: 'alertHandled',
platform: 'android',
action,
handled: true,
alert,
button,
...(coordinates ? { coordinates } : {}),
...successText(`Alert ${action}ed`),
};
}