Repository navigation
Expand file tree
/
Copy pathbackend.ts
More file actions
610 lines (557 loc) · 17.7 KB
/
Copy pathbackend.ts
File metadata and controls
610 lines (557 loc) · 17.7 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import type { ScreenshotResultData } from './snapshot-types.ts';
import type { SnapshotCaptureAnnotations } from './snapshot-capture-annotations.ts';
import type { SnapshotDiagnosticsSummary } from './snapshot-diagnostics.ts';
import type { JsonObject } from './json.ts';
import type { AppsFilter } from './app-inventory.ts';
import type { DeviceRotation } from './device-rotation.ts';
import type { AlertAction, AlertInfo } from './alert-contract.ts';
import type { BackMode } from './back-mode.ts';
import type { ClickButton } from './click-button.ts';
import type { GesturePlan } from './gesture-plan-types.ts';
import type { RepeatedInput } from './interaction.ts';
import type { ScrollDirection } from './scroll-gesture.ts';
import type { ResolvedScrollExecutionOptions } from './scroll-command.ts';
import type { TvRemoteButton } from './tv-remote.ts';
import type { RecordingExportQuality } from './recording-export-quality.ts';
import type { SessionSurface } from './session-surface.ts';
import type {
DeviceTarget,
Platform,
PlatformSelector,
PublicPlatform,
} from '@agent-device/kernel/device';
import type {
Point,
Rect,
PresentedSurface,
SnapshotKeyboardBandFact,
SnapshotNode,
SnapshotOptions,
SnapshotState,
SnapshotViewportSize,
} from '@agent-device/kernel/snapshot';
// The backend's public leaf platform (approach b): backends distinguish iOS from
// macOS (e.g. snapshot backend routing, the macOS surface guard), so this carries the
// leaf string, not the internal collapsed `apple`.
export type AgentDeviceBackendPlatform = PublicPlatform;
export type BackendCommandContext = {
session?: string;
requestId?: string;
appId?: string;
appBundleId?: string;
signal?: AbortSignal;
metadata?: Record<string, unknown>;
};
export type BackendSnapshotResult = {
nodes?: SnapshotNode[];
truncated?: boolean;
backend?: string;
snapshot?: SnapshotState;
appName?: string;
appBundleId?: string;
snapshotDiagnostics?: SnapshotDiagnosticsSummary;
/**
* The keyboard band this capture's producer measured (#2660), carried beside the tree it measured.
* A backend that publishes nothing measured nothing, and the tap-path keyboard guard keeps deriving
* the band from `nodes`.
*/
keyboard?: SnapshotKeyboardBandFact;
/**
* iOS: whether this capture read a modal presentation over the app (#3565), classified by the
* producer on its acquired tree. A backend that publishes nothing classified nothing; a consumer
* treats that as `'unknown'`, never as absence.
*/
presentedSurface?: PresentedSurface;
/**
* The box these rects are measured in, as the producer measured it (#3182). Absent means the
* producer measured no box — never a zero — since that absence is the only answer a producer with
* no screen to measure is allowed to give.
*/
viewport?: SnapshotViewportSize;
} & SnapshotCaptureAnnotations;
export type BackendSnapshotOptions = SnapshotOptions & {
includeRects?: boolean;
includeHiddenContentHints?: boolean;
outPath?: string;
};
export type BackendReadTextResult = {
text: string;
};
/**
* A backend's native text-presence reading. `found: true` is authoritative; `false` means
* "not proven by this backend" and the caller still consults the canonical tree.
*/
export type BackendFindTextResult = {
found: boolean;
};
export type BackendScreenshotOptions = {
fullscreen?: boolean;
overlayRefs?: boolean;
pixelDensity?: number;
stabilize?: boolean;
normalizeStatusBar?: boolean;
surface?: SessionSurface;
};
export type BackendScreenshotResult = ScreenshotResultData;
export type BackendActionResult = Record<string, unknown> | void;
export type BackendDeviceOrientation = DeviceRotation;
export type BackendBackOptions = {
mode?: BackMode;
};
export type BackendTvRemoteOptions = {
button: TvRemoteButton;
durationMs?: number;
};
export type BackendKeyboardOptions = {
action: 'status' | 'get' | 'dismiss' | 'enter' | 'return';
};
export type BackendKeyboardResult = {
platform?: Platform;
action?: BackendKeyboardOptions['action'];
visible?: boolean;
inputType?: string | null;
inputMethodPackage?: string | null;
type?: string | null;
wasVisible?: boolean;
dismissed?: boolean;
attempts?: number;
/** iOS only: which mechanism resigned the keyboard (#1598) — 'dismissKey'
* (tapped the keyboard's own Hide/Dismiss/Done key); background-tap dismissal is deliberately unsupported (#1606 review). */
mechanism?: string;
};
export type BackendClipboardTextResult = {
text: string;
};
export type BackendAlertAction = AlertAction;
export type BackendAlertInfo = AlertInfo;
export type BackendAlertResult =
| {
kind: 'alertStatus';
alert: BackendAlertInfo | null;
}
| {
kind: 'alertHandled';
handled: boolean;
alert?: BackendAlertInfo;
button?: string;
}
| {
kind: 'alertWait';
alert: BackendAlertInfo | null;
waitedMs?: number;
timedOut?: boolean;
};
export type BackendTapOptions = RepeatedInput & {
button?: ClickButton;
};
export type BackendRefTarget = {
kind: 'ref';
ref: string;
fallbackLabel?: string;
};
export type BackendFillOptions = {
delayMs?: number;
/** Maestro replay compatibility: permit the runner's non-hittable coordinate path. */
allowNonHittableCoordinateFallback?: boolean;
};
export type BackendLongPressOptions = {
durationMs?: number;
};
export type BackendScrollTarget =
| {
kind: 'viewport';
}
| {
kind: 'point';
point: Point;
};
export type BackendScrollOptions = ResolvedScrollExecutionOptions & {
direction: ScrollDirection;
};
export type BackendOpenTarget = {
/**
* Generic app identifier accepted by the backend. Hosted adapters should
* prefer structured appId, bundleId, or packageName when available.
*/
app?: string;
appId?: string;
bundleId?: string;
packageName?: string;
/**
* URL may be used by itself for a deep link or with an app identifier when
* the backend supports opening a URL in a specific app context.
*/
url?: string;
/**
* Platform-specific activity override, primarily for Android app launches.
*/
activity?: string;
};
export type BackendOpenOptions = {
launchArgs?: string[];
relaunch?: boolean;
};
export type BackendAppListFilter = AppsFilter;
export type BackendAppInfo = {
id: string;
name?: string;
bundleId?: string;
packageName?: string;
activity?: string;
};
export type BackendAppState = {
appId?: string;
bundleId?: string;
packageName?: string;
activity?: string;
state?: 'unknown' | 'notRunning' | 'running' | 'foreground' | 'background';
details?: Record<string, unknown>;
};
export type BackendPushInput =
| {
kind: 'json';
payload: JsonObject;
}
| {
kind: 'file';
path: string;
};
export type BackendAppEvent = {
name: string;
payload?: JsonObject;
};
export type BackendDeviceFilter = {
platform?: PlatformSelector;
target?: DeviceTarget;
kind?: 'simulator' | 'emulator' | 'device' | 'desktop';
};
export type BackendDeviceInfo = {
id: string;
name: string;
platform: AgentDeviceBackendPlatform;
target?: 'mobile' | 'tv' | 'desktop';
kind?: 'simulator' | 'emulator' | 'device' | 'desktop';
booted?: boolean;
details?: Record<string, unknown>;
};
export type BackendDeviceTarget = {
id?: string;
name?: string;
platform?: AgentDeviceBackendPlatform;
target?: 'mobile' | 'tv' | 'desktop';
headless?: boolean;
};
export type BackendInstallSource =
| {
kind: 'path';
path: string;
}
| {
kind: 'uploadedArtifact';
id: string;
}
| {
kind: 'url';
url: string;
};
export type BackendInstallTarget = {
app?: string;
source: BackendInstallSource;
};
export type BackendInstallResult = Record<string, unknown> & {
appId?: string;
appName?: string;
bundleId?: string;
packageName?: string;
launchTarget?: string;
installablePath?: string;
archivePath?: string;
};
export type BackendRecordingOptions = {
outPath?: string;
fps?: number;
quality?: RecordingExportQuality;
showTouches?: boolean;
};
export type BackendRecordingResult = Record<string, unknown> & {
path?: string;
telemetryPath?: string;
warning?: string;
};
export type BackendTraceOptions = {
outPath?: string;
};
export type BackendTraceResult = Record<string, unknown> & {
outPath?: string;
};
import type {
BackendDiagnosticsPageOptions,
BackendDiagnosticsTimeWindow,
BackendDumpNetworkOptions,
BackendDumpNetworkResult,
BackendNetworkEntry,
BackendNetworkIncludeMode,
} from './backend-diagnostics.ts';
export type {
BackendDiagnosticsPageOptions,
BackendDiagnosticsTimeWindow,
BackendDumpNetworkOptions,
BackendDumpNetworkResult,
BackendNetworkEntry,
BackendNetworkIncludeMode,
};
export type BackendLogEntry = {
timestamp?: string;
level?: 'debug' | 'info' | 'warn' | 'error' | string;
message: string;
source?: string;
metadata?: Record<string, unknown>;
};
export type BackendReadLogsOptions = BackendDiagnosticsPageOptions & {
levels?: readonly string[];
search?: string;
source?: string;
};
export type BackendReadLogsResult = {
entries: readonly BackendLogEntry[];
nextCursor?: string;
timeWindow?: BackendDiagnosticsTimeWindow;
backend?: string;
redacted?: boolean;
notes?: readonly string[];
};
export type BackendPerfMetric = {
name: string;
value?: number;
unit?: string;
status?: 'ok' | 'unavailable' | 'error';
message?: string;
metadata?: Record<string, unknown>;
};
export type BackendMeasurePerfOptions = BackendDiagnosticsTimeWindow & {
sampleMs?: number;
metrics?: readonly string[];
};
export type BackendMeasurePerfResult = {
metrics: readonly BackendPerfMetric[];
startedAt?: string;
endedAt?: string;
backend?: string;
redacted?: boolean;
notes?: readonly string[];
};
export type BackendShellResult = {
exitCode: number;
stdout: string;
stderr: string;
};
export type BackendRunnerCommand = {
command: string;
args?: readonly string[];
payload?: Record<string, unknown>;
};
export type BackendEscapeHatches = {
androidShell?(
context: BackendCommandContext,
args: readonly string[],
): Promise<BackendShellResult>;
iosRunnerCommand?(
context: BackendCommandContext,
command: BackendRunnerCommand,
): Promise<BackendActionResult>;
macosDesktopScreenshot?(
context: BackendCommandContext,
outPath: string,
options?: BackendScreenshotOptions,
): Promise<BackendScreenshotResult | void>;
};
export type AgentDeviceBackend = {
platform: AgentDeviceBackendPlatform;
escapeHatches?: BackendEscapeHatches;
captureSnapshot?(
context: BackendCommandContext,
options?: BackendSnapshotOptions,
): Promise<BackendSnapshotResult>;
resolveGestureViewport?(context: BackendCommandContext): Promise<Rect | undefined>;
captureScreenshot?(
context: BackendCommandContext,
outPath: string,
options?: BackendScreenshotOptions,
): Promise<BackendScreenshotResult | void>;
readText?(context: BackendCommandContext, node: SnapshotNode): Promise<BackendReadTextResult>;
/** Present only when the bound runtime advertised the conditional `findText` operation. */
findText?(context: BackendCommandContext, text: string): Promise<BackendFindTextResult>;
/**
* #1542 off-screen refusal double-check: called ONLY at the moment the
* shared off-screen interaction guard is about to REFUSE a click/tap/
* gesture-target resolution, to re-confirm the target directly — bypassing
* whatever bulk accessibility tree the guard's verdict came from (observed
* on iOS: a keyboard-dismiss content-offset correction can leave a
* ScrollView's bulk AX frame squeezed to a stale value, or the whole bulk
* tree pinned at pre-gesture values, while the target is genuinely fine).
*
* Conceptually a boolean ("is this actually visible?"), but returns the
* confirmed LIVE rect rather than a bare `true`/`false`: a rescue must tap
* at the live coordinate, never the stale bulk-tree one the guard was
* about to refuse — a caller that used the original rect after a rescue
* would silently tap the wrong place when the bulk tree is stale, not just
* stale-looking. `rootViewport` is the guard's own already-resolved root
* viewport (Application/Window frame), passed in so an implementation can
* validate the live rect's tap point against it without recomputing it.
*
* Returns `null` when the target cannot be positively confirmed on-screen
* (no stable id/label, not found, ambiguous match, not hittable, outside
* `rootViewport`, or any transport failure) — the guard MUST fail closed
* (refuse) on `null`. This is a rescue path only, never a way to relax a
* genuine refusal. Backends that do not support a direct, tree-independent
* read simply omit this method, which leaves today's refuse-on-off-screen
* behavior byte-for-byte unchanged.
*/
confirmOffscreenTargetVisible?(
context: BackendCommandContext,
node: Pick<SnapshotNode, 'identifier' | 'label'>,
rootViewport: Rect | null,
): Promise<Rect | null>;
tap?(
context: BackendCommandContext,
point: Point,
options?: BackendTapOptions,
): Promise<BackendActionResult>;
tapTarget?(
context: BackendCommandContext,
target: BackendRefTarget,
options?: BackendTapOptions,
): Promise<BackendActionResult>;
fill?(
context: BackendCommandContext,
point: Point,
text: string,
options?: BackendFillOptions,
): Promise<BackendActionResult>;
fillTarget?(
context: BackendCommandContext,
target: BackendRefTarget,
text: string,
options?: BackendFillOptions,
): Promise<BackendActionResult>;
focus?(context: BackendCommandContext, point: Point): Promise<BackendActionResult>;
longPress?(
context: BackendCommandContext,
point: Point,
options?: BackendLongPressOptions,
): Promise<BackendActionResult>;
hover?(context: BackendCommandContext, point: Point): Promise<BackendActionResult>;
hoverTarget?(
context: BackendCommandContext,
target: BackendRefTarget,
): Promise<BackendActionResult>;
scroll?(
context: BackendCommandContext,
target: BackendScrollTarget,
options: BackendScrollOptions,
): Promise<BackendActionResult>;
performGesture?(context: BackendCommandContext, plan: GesturePlan): Promise<BackendActionResult>;
pressKey?(
context: BackendCommandContext,
key: string,
options?: { modifiers?: string[] },
): Promise<BackendActionResult>;
pressBack?(
context: BackendCommandContext,
options?: BackendBackOptions,
): Promise<BackendActionResult>;
pressHome?(context: BackendCommandContext): Promise<BackendActionResult>;
pressTvRemote?(
context: BackendCommandContext,
options: BackendTvRemoteOptions,
): Promise<BackendActionResult>;
setOrientation?(
context: BackendCommandContext,
orientation: BackendDeviceOrientation,
): Promise<BackendActionResult>;
setKeyboard?(
context: BackendCommandContext,
options: BackendKeyboardOptions,
): Promise<BackendKeyboardResult | BackendActionResult>;
getClipboard?(context: BackendCommandContext): Promise<string | BackendClipboardTextResult>;
setClipboard?(context: BackendCommandContext, text: string): Promise<BackendActionResult>;
openSettings?(context: BackendCommandContext, target?: string): Promise<BackendActionResult>;
handleAlert?(
context: BackendCommandContext,
action: BackendAlertAction,
options?: { timeoutMs?: number },
): Promise<BackendAlertResult>;
openAppSwitcher?(context: BackendCommandContext): Promise<BackendActionResult>;
openApp?(
context: BackendCommandContext,
target: BackendOpenTarget,
options?: BackendOpenOptions,
): Promise<BackendActionResult>;
closeApp?(context: BackendCommandContext, app?: string): Promise<BackendActionResult>;
listApps?(
context: BackendCommandContext,
filter: BackendAppListFilter,
): Promise<readonly BackendAppInfo[]>;
getAppState?(context: BackendCommandContext, app: string): Promise<BackendAppState>;
pushFile?(
context: BackendCommandContext,
input: BackendPushInput,
target: string,
): Promise<BackendActionResult>;
triggerAppEvent?(
context: BackendCommandContext,
event: BackendAppEvent,
): Promise<BackendActionResult>;
listDevices?(
context: BackendCommandContext,
filter?: BackendDeviceFilter,
): Promise<readonly BackendDeviceInfo[]>;
bootDevice?(
context: BackendCommandContext,
target?: BackendDeviceTarget,
): Promise<BackendActionResult>;
shutdownDevice?(
context: BackendCommandContext,
target?: BackendDeviceTarget,
): Promise<BackendActionResult>;
resolveInstallSource?(
context: BackendCommandContext,
source: BackendInstallSource,
): Promise<BackendInstallSource>;
installApp?(
context: BackendCommandContext,
target: BackendInstallTarget,
): Promise<BackendInstallResult>;
reinstallApp?(
context: BackendCommandContext,
target: BackendInstallTarget,
): Promise<BackendInstallResult>;
startRecording?(
context: BackendCommandContext,
options?: BackendRecordingOptions,
): Promise<BackendRecordingResult>;
stopRecording?(
context: BackendCommandContext,
options?: BackendRecordingOptions,
): Promise<BackendRecordingResult>;
startTrace?(
context: BackendCommandContext,
options?: BackendTraceOptions,
): Promise<BackendTraceResult>;
stopTrace?(
context: BackendCommandContext,
options?: BackendTraceOptions,
): Promise<BackendTraceResult>;
readLogs?(
context: BackendCommandContext,
options?: BackendReadLogsOptions,
): Promise<BackendReadLogsResult>;
dumpNetwork?(
context: BackendCommandContext,
options?: BackendDumpNetworkOptions,
): Promise<BackendDumpNetworkResult>;
measurePerf?(
context: BackendCommandContext,
options?: BackendMeasurePerfOptions,
): Promise<BackendMeasurePerfResult>;
};