Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
874 lines (808 loc) · 34.3 KB
/
Copy pathindex.ts
File metadata and controls
874 lines (808 loc) · 34.3 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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
import { FOLD_FLAGS } from '@agent-device/command-registry/flag-groups';
import type { CommandResultMap } from '@agent-device/command-registry/command-result';
import type { ClipboardCommandOptions } from '@agent-device/contracts/client';
import {
type FoldKeyframe,
FOLD_SCREEN_COORDINATE_SPACE,
MAX_FOLD_DURATION_MS,
MAX_FOLD_KEYFRAMES,
parseFoldInput,
parseFoldKeyframesJson,
ORIENTATION_REQUESTS,
FOLD_POSES,
FOLD_POSE_USAGE,
parseOrientationRequest,
TELEPHONY_ACTIONS,
TELEPHONY_CALL_ACTIONS,
TELEPHONY_USAGE,
parseTelephonyEventInput,
parseTelephonyPositionals,
telephonyPositionals,
// The hardware-button vocabulary arrives through the same eager device-vocabulary façade the
// orientation and fold readers use (#3848, eager-closure budgets).
HARDWARE_BUTTON_NAMES,
HARDWARE_BUTTON_NAME_LABELS,
HARDWARE_BUTTON_USAGE,
parseHardwareButtonPositionals,
} from '@agent-device/contracts/device';
import { type BackMode, BACK_MODES } from '@agent-device/contracts/back-mode';
import {
type TvRemoteButton,
TV_REMOTE_BUTTONS,
TV_REMOTE_ANDROID_KEYCODE_PATTERN,
TV_REMOTE_BUTTON_USAGE,
parseTvRemoteButton,
tvRemoteDurationMode,
} from '@agent-device/contracts/tv-remote';
import { APPLE_APPLICATION_STATES } from '@agent-device/kernel/snapshot';
import { SESSION_SURFACES } from '@agent-device/contracts/session';
import { AppError } from '@agent-device/kernel/errors';
import type { CommandSchemaOverride } from '@agent-device/command-registry/command-schema';
import {
commonInputFromFlags,
direct,
optionalString,
request,
requiredDaemonString,
} from '../cli-grammar/common.ts';
import type { CliReader, DaemonWriter } from '../cli-grammar/types.ts';
import type { JsonSchema } from '../command-contract.ts';
import {
customField,
enumField,
integerField,
requiredField,
stringField,
jsonSchemaField,
readFieldInput,
booleanSchema,
constSchema,
enumSchema,
numberSchema,
objectSchema,
stringSchema,
} from '../command-input.ts';
import { compactRecord } from '../input-readers.ts';
import {
defineCommandFacet,
defineCommandFamilyFromFacets,
defineParameterlessCommandFacet,
} from '../family/types.ts';
import { defineFieldCommandMetadata } from '../field-command-contract.ts';
import {
postActionObservationCliFlags,
postActionObservationFields,
} from '../post-action-observation-grammar.ts';
const APPSTATE_COMMAND_NAME = 'appstate';
const BACK_COMMAND_NAME = 'back';
const HOME_COMMAND_NAME = 'home';
const ORIENTATION_COMMAND_NAME = 'orientation';
const FOLD_COMMAND_NAME = 'fold';
const APP_SWITCHER_COMMAND_NAME = 'app-switcher';
const ACTION_BUTTON_COMMAND_NAME = 'action-button';
const HARDWARE_BUTTON_COMMAND_NAME = 'hardware-button';
const KEYBOARD_COMMAND_NAME = 'keyboard';
const CLIPBOARD_COMMAND_NAME = 'clipboard';
const TV_REMOTE_COMMAND_NAME = 'tv-remote';
const TELEPHONY_COMMAND_NAME = 'telephony';
const TV_REMOTE_LONGPRESS_PRESET_MS = 500;
const CLIPBOARD_ACTION_VALUES = ['read', 'write'] as const;
const KEYBOARD_METADATA_ACTION_VALUES = ['status', 'dismiss', 'enter', 'return'] as const;
const tvRemoteButtonSchema: JsonSchema = {
description:
'TV remote button. Android TV also accepts android-keycode:<n> with n from 1 to 2147483647.',
oneOf: [
enumSchema(TV_REMOTE_BUTTONS),
{ type: 'string', pattern: TV_REMOTE_ANDROID_KEYCODE_PATTERN.source },
],
};
/**
* This family's advertised MCP `outputSchema`s, keyed by daemon command name and projected into
* the command map by `command-output-schemas.ts`. Non-strict like every other entry: no
* `additionalProperties: false`, so additive response fields such as `settle`/`cost` keep
* validating. `back`'s settle observation is grafted separately by the trait derivation pass.
*/
export const SYSTEM_COMMAND_OUTPUT_SCHEMAS = {
back: objectSchema(
{
action: constSchema('back'),
mode: enumSchema(BACK_MODES),
message: stringSchema(),
},
['action', 'mode', 'message'],
),
home: objectSchema({ action: constSchema('home'), message: stringSchema() }, [
'action',
'message',
]),
orientation: objectSchema(
{
action: constSchema('orientation'),
orientation: enumSchema(ORIENTATION_REQUESTS),
message: stringSchema(),
confirmed: booleanSchema(),
warning: stringSchema(),
},
['action', 'orientation', 'message'],
),
'app-switcher': objectSchema({ action: constSchema('app-switcher'), message: stringSchema() }, [
'action',
'message',
]),
fold: objectSchema(
{
action: constSchema('fold'),
pose: enumSchema(FOLD_POSES),
hingeAngleDegrees: numberSchema(
'Hinge angle read back after the pose settled: from CoreMotion inside the simulator on iOS, from the emulator hinge sensor on Android.',
),
screen: objectSchema(
{
display: stringSchema('CoreDevice name of the panel the device now lights.'),
coordinateSpace: constSchema(FOLD_SCREEN_COORDINATE_SPACE),
widthPt: numberSchema(
'Panel width in native panel points (pixels divided by point scale), NOT snapshot coordinates; take a fresh snapshot to place a tap.',
),
heightPt: numberSchema(
'Panel height in native panel points (pixels divided by point scale), NOT snapshot coordinates; take a fresh snapshot to place a tap.',
),
},
['display', 'coordinateSpace', 'widthPt', 'heightPt'],
),
message: stringSchema(),
},
['action', 'pose', 'hingeAngleDegrees', 'message'],
),
'action-button': objectSchema({ action: constSchema('action-button'), message: stringSchema() }, [
'action',
'message',
]),
'tv-remote': objectSchema(
{
action: constSchema('tv-remote'),
button: tvRemoteButtonSchema,
durationMs: numberSchema(),
message: stringSchema(),
},
['action', 'button', 'message'],
),
'hardware-button': objectSchema(
{
action: constSchema('hardware-button'),
button: enumSchema(HARDWARE_BUTTON_NAMES),
message: stringSchema(),
},
['action', 'button', 'message'],
),
// packages/contracts/src/telephony-input.ts — discriminated union on `action`.
telephony: {
type: 'object',
oneOf: [
objectSchema(
{
action: enumSchema(TELEPHONY_CALL_ACTIONS),
number: stringSchema(),
message: stringSchema(),
},
['action', 'number', 'message'],
),
objectSchema(
{
action: constSchema('sms'),
number: stringSchema(),
textLength: numberSchema('Length of the delivered message text.'),
message: stringSchema(),
},
['action', 'number', 'textLength', 'message'],
),
],
},
// packages/contracts/src/clipboard.ts — discriminated union on `action`.
clipboard: {
type: 'object',
oneOf: [
objectSchema({ action: constSchema('read'), text: stringSchema() }, ['action', 'text']),
objectSchema(
{ action: constSchema('write'), textLength: numberSchema(), message: stringSchema() },
['action', 'textLength', 'message'],
),
],
},
// packages/contracts/src/app-state.ts — discriminated union on `platform`.
appstate: {
type: 'object',
oneOf: [
objectSchema(
{
platform: enumSchema(['ios', 'macos']),
appName: stringSchema(),
appBundleId: stringSchema(),
source: enumSchema(
['session', 'runner'],
'runner when a live runner read the session app state; session when the record alone answered.',
),
state: enumSchema(
APPLE_APPLICATION_STATES,
'The session app XCUIApplication state as a live runner reads it; absent with source session.',
),
surface: enumSchema(SESSION_SURFACES),
device_udid: stringSchema('iOS only — the session device UDID.'),
ios_simulator_device_set: {
type: ['string', 'null'],
description: 'iOS only — the simulator set path, or null when unknown.',
},
},
['platform', 'appName', 'source', 'surface'],
),
objectSchema(
{
platform: enumSchema(['android', 'harmonyos']),
package: stringSchema(),
activity: stringSchema(),
},
['platform', 'package', 'activity'],
),
],
},
// packages/contracts/src/keyboard.ts — flat closed shape; `platform`/`action` always present.
keyboard: objectSchema(
{
platform: enumSchema(['android', 'ios']),
action: enumSchema(['status', 'dismiss', 'enter']),
visible: booleanSchema(),
wasVisible: booleanSchema(),
dismissed: booleanSchema(),
attempts: numberSchema(),
inputType: stringSchema(),
type: enumSchema(['text', 'number', 'email', 'phone', 'password', 'datetime', 'unknown']),
inputMethodPackage: stringSchema(),
focusedPackage: stringSchema(),
focusedResourceId: stringSchema(),
inputOwner: enumSchema(['app', 'ime', 'unknown']),
message: stringSchema(),
},
['platform', 'action'],
),
} satisfies Pick<
Record<keyof CommandResultMap, JsonSchema>,
| 'back'
| 'home'
| 'orientation'
| 'app-switcher'
| 'fold'
| 'action-button'
| 'tv-remote'
| 'hardware-button'
| 'telephony'
| 'clipboard'
| 'appstate'
| 'keyboard'
>;
const appStateCommandDescription =
'Show foreground app/activity (Android and HarmonyOS; iOS and macOS answer per session)';
const backCommandDescription =
'Navigate back in the app or through system navigation. The in-app mode targets the app navigation stack; system targets platform back behavior. On iOS both modes press the navigation bar system back button (identifier BackButton) only when exactly one hittable one is found in a readable accessibility tree, and succeed only when the number of navigation bars, the number of back buttons, or a back button title then changes (a bar title or a moved back button alone never counts, so a pop that keeps the same back button title and bar count reports back_swipe_unverified); with no such button, two candidates, or an unreadable tree, they swipe from the left edge instead (on an iPad whose app window cannot be resolved, only while the app fills the screen) and succeed only when the app screen changes. Reason back_swipe_no_change: the swipe was sent and the screen did not change; a visible back or close control is an alternative. Reason back_swipe_unverified: the button was pressed but the navigation bar did not change within 1 s, or the swipe may or may not have navigated, because the screen kept changing before it or could not be sampled after it; the resulting screen is unconfirmed. Reason back_swipe_not_dispatched, or a screen that cannot be sampled before the swipe, means nothing was sent. An app with a left-edge drawer may open the drawer instead. A visible back or close control can be targeted directly by its ref.';
const homeCommandDescription =
'Send the selected device to its home screen. This leaves the app session open but moves the foreground away from the app.';
const orientationCommandDescription =
'Set device orientation on iOS and Android. auto hands rotation back to the sensor on ADB-backed Android (local and Limrun); other backends refuse it. In both default and scoped simulator sets, an iPhone Duo classified with more than one integrated panel refuses portrait-upside-down and rotates through the fold helper; it succeeds only when the lit panel shows the requested orientation, otherwise it fails with orientation-unverified, or orientation-panel-table-unavailable when the final guest panel table cannot be read. A simulator classified with one panel, or whose first classification read is unresolved, uses the standard runner rotation.';
const foldGuidance = {
hingeEvent: 'simulator HID hinge event',
permissions: 'Device Hub and host Accessibility permission are not required.',
} as const;
const foldCommandDescription = `Fold or unfold a foldable iPhone simulator (iPhone Duo) or a foldable Android emulator into the closed, half-open, or open pose. iOS sends a ${foldGuidance.hingeEvent}, then reads the hinge angle back inside the simulator to confirm it, and can follow timestamped angle keyframes; Android sets the emulator console posture, then confirms the device state the guest commits and reports the angle read from the hinge sensor, which can differ from the AVD profile's per-posture angles, and dismisses the lock screen a fold to the cover display can raise. On iOS a pose change moves the app to a different panel with a different point size; on Android a fold to or from closed moves the app between the cover and inner displays, and any pose change can reshape the app window, whose size the next snapshot reports. Either way every ref and coordinate from before it is stale. Taps, long presses, and scrolling target the app window on its current panel in closed, half-open, and open poses. Simulator and emulator only; iOS requires the iOS simulator SDK; ${foldGuidance.permissions} An iOS simulator in a scoped simulator set folds the same way as one in the default set.`;
const appSwitcherCommandDescription =
'Open the device app switcher to inspect or change foreground apps. This changes the visible system UI and may move focus away from the current app.';
const keyboardCommandDescription =
'Inspect Android keyboard visibility/type or press/dismiss the device keyboard. The dismiss action hides the keyboard. It taps the keyboard dismiss/hide key when one is exposed, verifies the keyboard closed, and reports UNSUPPORTED_OPERATION when no dismiss key exists \u2014 background taps are never attempted.';
const clipboardCommandDescription =
'Read the current device clipboard text, or replace its contents with the given text. Android runs both through the clipboard service shell command, and a build that implements none (Android 16 does not) refuses with UNSUPPORTED_OPERATION rather than reporting an empty clipboard.';
const actionButtonCommandDescription =
'Press the iPhone or iPad Action Button once. The press is dispatched without activating the session app and nothing is re-observed afterwards, so the app keeps the state the press found. What the system does with the press is not observed by this command: Simulators run no Shortcuts or App Intents, so delivery to an assigned Shortcut is verifiable only on a physical iPhone.';
const tvRemoteCommandDescription =
'Press or long-press a TV remote button on Android TV, tvOS, or Vega OS. Android TV also supports volume, power, media, channel, guide, captions, input, color keys, and android-keycode:N. tvOS and Vega OS support up, down, left, right, select, menu, home, and back. Phones and WebDriver sessions are unsupported. The aliases ok, center, and enter map to select.';
const hardwareButtonCommandDescription =
'Press a phone hardware button once: volume-up, volume-down, power, mute, sleep, wake, media-play-pause, media-next, media-previous, media-rewind, or media-fast-forward. The press is dispatched without activating the session app and nothing is re-observed afterwards, so the OS decides what the key does — which audio stream a volume press moves, which media session a media key reaches, and whether the HUD shows, is device behavior this command does not claim. Power toggles the Android screen; sleep and wake are the explicit Android events that turn it off or on without toggling, and after a press that turns it off, later commands may hit a locked screen. Android (ADB-backed and Appium) presses all eleven on phones and tablets; a physical iPhone or iPad presses volume only through the local runner or Appium, never power, mute, sleep, wake, or media. iOS Simulators, Android TV and tvOS targets refuse this command.';
const telephonyCommandDescription =
'Simulate an incoming phone call or SMS on an Android emulator through its console over adb: call rings from the number, accept answers, cancel ends the call, and sms delivers a text message. The call screen or notification can cover the app and invalidate earlier UI observations.';
const backCommandMetadata = defineFieldCommandMetadata(BACK_COMMAND_NAME, backCommandDescription, {
mode: enumField(BACK_MODES),
...postActionObservationFields(BACK_COMMAND_NAME),
});
const orientationCommandMetadata = defineFieldCommandMetadata(
ORIENTATION_COMMAND_NAME,
orientationCommandDescription,
{
orientation: requiredField(enumField(ORIENTATION_REQUESTS)),
},
);
const foldFields = {
pose: enumField(FOLD_POSES, 'Instant preset; mutually exclusive with keyframes.'),
keyframes: jsonSchemaField<readonly FoldKeyframe[]>({
type: 'array',
minItems: 2,
maxItems: MAX_FOLD_KEYFRAMES,
description:
'Piecewise-linear hinge motion. Start at 0ms; strictly increasing timestamps, up to 60000ms. Repeated angles create holds. Mutually exclusive with pose.',
items: {
type: 'object',
required: ['atMs', 'angle'],
additionalProperties: false,
properties: {
atMs: { type: 'integer', minimum: 0, maximum: MAX_FOLD_DURATION_MS },
angle: { type: 'number', minimum: 0, maximum: 180 },
},
},
}),
};
const foldFieldMetadata = defineFieldCommandMetadata(
FOLD_COMMAND_NAME,
foldCommandDescription,
foldFields,
{
readInput: (input) => {
const fields = readFieldInput(input, foldFields);
const { pose, keyframes, ...common } = fields;
return { ...common, ...parseFoldInput({ pose, keyframes }) };
},
inputExample: { pose: 'half-open' },
},
);
const foldCommandMetadata = {
...foldFieldMetadata,
inputSchema: {
...foldFieldMetadata.inputSchema,
oneOf: [
{ required: ['pose'], not: { required: ['keyframes'] } },
{ required: ['keyframes'], not: { required: ['pose'] } },
],
},
};
const keyboardCommandMetadata = defineFieldCommandMetadata(
KEYBOARD_COMMAND_NAME,
keyboardCommandDescription,
{
action: enumField(KEYBOARD_METADATA_ACTION_VALUES),
},
);
// `clipboard read` prints the clipboard content verbatim, so its warnings belong on stderr.
const clipboardFields = {
action: requiredField(enumField(CLIPBOARD_ACTION_VALUES)),
text: stringField(
'Text for write; required for write ("" clears the clipboard), refused for read.',
{
allowEmpty: true,
},
),
};
const clipboardFieldMetadata = defineFieldCommandMetadata(
CLIPBOARD_COMMAND_NAME,
clipboardCommandDescription,
clipboardFields,
{
parseableOutput: true,
readInput: (input: unknown) => {
const { action, text, ...common } = readFieldInput(input, clipboardFields);
if (action === 'read') {
if (text !== undefined) {
throw new AppError('INVALID_ARGS', 'clipboard read does not accept text.');
}
return { ...common, action };
}
if (text === undefined) {
throw new AppError('INVALID_ARGS', 'clipboard write requires text (use "" to clear).');
}
return { ...common, action, text };
},
},
);
const clipboardCommandMetadata = {
...clipboardFieldMetadata,
inputSchema: {
...clipboardFieldMetadata.inputSchema,
oneOf: [
{ properties: { action: constSchema('write') }, required: ['text'] },
{ properties: { action: constSchema('read') }, not: { required: ['text'] } },
],
},
};
const tvRemoteCommandMetadata = defineFieldCommandMetadata(
TV_REMOTE_COMMAND_NAME,
tvRemoteCommandDescription,
{
button: requiredField(
customField<TvRemoteButton>(tvRemoteButtonSchema, (record, key) =>
parseTvRemoteButton(typeof record[key] === 'string' ? record[key] : undefined),
),
),
durationMs: integerField(
`Press duration in milliseconds. tvOS and Vega OS use the exact hold duration; Android TV maps any positive value to an ADB longpress (${tvRemoteDurationMode('android')}).`,
{
min: 0,
},
),
},
);
const hardwareButtonCommandMetadata = defineFieldCommandMetadata(
HARDWARE_BUTTON_COMMAND_NAME,
hardwareButtonCommandDescription,
{
button: requiredField(
enumField(
HARDWARE_BUTTON_NAMES,
'One press each. volume-up and volume-down move the system volume stream the OS selects; power toggles the screen on Android; sleep and wake are the explicit Android events that turn the screen off or on without toggling; mute, media-play-pause, media-next, media-previous, media-rewind, and media-fast-forward are Android keys the OS routes to its own streams and media sessions. All of these except volume are refused on every Apple leaf.',
),
),
},
);
const telephonyFields = {
action: requiredField(
enumField(
TELEPHONY_ACTIONS,
'call starts an incoming call, accept answers it, cancel ends it, sms delivers a text message.',
),
),
number: requiredField(
stringField('Caller or sender phone number: digits with an optional leading +.'),
),
text: stringField(
'Message text for sms; required for sms and refused for the call actions. The emulator drops characters outside the Basic Multilingual Plane, such as emoji.',
),
};
const telephonyFieldMetadata = defineFieldCommandMetadata(
TELEPHONY_COMMAND_NAME,
telephonyCommandDescription,
telephonyFields,
{
readInput: (input) => {
const { action, number, text, ...common } = readFieldInput(input, telephonyFields);
return { ...common, ...parseTelephonyEventInput({ action, number, text }) };
},
inputExample: { action: 'call', number: '+15555550100' },
},
);
const telephonyCommandMetadata = {
...telephonyFieldMetadata,
inputSchema: {
...telephonyFieldMetadata.inputSchema,
oneOf: [
{ properties: { action: constSchema('sms') }, required: ['text'] },
{ properties: { action: enumSchema(TELEPHONY_CALL_ACTIONS) }, not: { required: ['text'] } },
],
},
};
const backCliSchema = {
usageOverride: 'back [--in-app|--system] [--settle]',
usageFlags: [],
allowedFlags: ['backMode', ...postActionObservationCliFlags(BACK_COMMAND_NAME)],
} as const satisfies CommandSchemaOverride;
const orientationCliSchema = {
usageOverride: `orientation <${ORIENTATION_REQUESTS.join('|')}>`,
positionalArgs: ['orientation'],
} as const satisfies CommandSchemaOverride;
const foldCliSchema = {
usageOverride: `fold [${FOLD_POSE_USAGE}]`,
positionalArgs: ['pose?'],
allowedFlags: FOLD_FLAGS,
} as const satisfies CommandSchemaOverride;
const keyboardCliSchema = {
usageOverride: 'keyboard [status|get|dismiss|enter|return]',
positionalArgs: ['action?'],
} as const satisfies CommandSchemaOverride;
const clipboardCliSchema = {
usageOverride: 'clipboard read | clipboard write <text>',
listUsageOverride: 'clipboard read | clipboard write <text>',
positionalArgs: ['read|write', 'text?'],
allowsExtraPositionals: true,
} as const satisfies CommandSchemaOverride;
const tvRemoteCliSchema = {
usageOverride: `tv-remote [press|longpress] ${TV_REMOTE_BUTTON_USAGE}`,
listUsageOverride: 'tv-remote press|longpress <button> [--duration-ms <ms>]',
positionalArgs: ['press|longpress?', 'button'],
allowedFlags: ['durationMs'],
} as const satisfies CommandSchemaOverride;
const hardwareButtonCliSchema = {
usageOverride: `hardware-button ${HARDWARE_BUTTON_USAGE}`,
positionalArgs: ['button'],
} as const satisfies CommandSchemaOverride;
const telephonyCliSchema = {
usageOverride: TELEPHONY_USAGE,
listUsageOverride: 'telephony call|accept|cancel|sms <number> [text]',
positionalArgs: ['call|accept|cancel|sms', 'number', 'text?'],
allowsExtraPositionals: true,
} as const satisfies CommandSchemaOverride;
export const backCliReader: CliReader = (_positionals, flags) => ({
...commonInputFromFlags(flags),
mode: flags.backMode,
});
export const orientationCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
orientation: parseOrientationRequest(positionals[0]),
});
export const foldCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...parseFoldInput({
pose: positionals[0],
keyframes: flags.keyframes === undefined ? undefined : parseFoldKeyframesJson(flags.keyframes),
}),
});
export const keyboardCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...readKeyboardInput(positionals),
});
export const clipboardCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...readClipboardInput(positionals),
});
export const tvRemoteCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...readTvRemoteInput(positionals, flags.durationMs),
});
export const hardwareButtonCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...readHardwareButtonInput(positionals),
});
export const telephonyCliReader: CliReader = (positionals, flags) => ({
...commonInputFromFlags(flags),
...parseTelephonyPositionals(positionals),
});
export const backDaemonWriter: DaemonWriter = (input) =>
request(BACK_COMMAND_NAME, [], {
...input,
backMode: readBackMode(input.mode),
});
export const orientationDaemonWriter: DaemonWriter = direct(ORIENTATION_COMMAND_NAME, (input) => [
requiredDaemonString(input.orientation, 'orientation requires orientation'),
]);
export const foldDaemonWriter: DaemonWriter = (input) => {
const fold = parseFoldInput(input);
return request(FOLD_COMMAND_NAME, fold.pose ? [fold.pose] : [], {
...input,
keyframes: fold.keyframes ? JSON.stringify(fold.keyframes) : undefined,
});
};
export const keyboardDaemonWriter: DaemonWriter = direct(KEYBOARD_COMMAND_NAME, (input) =>
optionalString(input.action),
);
export const clipboardDaemonWriter: DaemonWriter = direct(CLIPBOARD_COMMAND_NAME, (input) =>
clipboardPositionals(input as ClipboardCommandOptions),
);
export const tvRemoteDaemonWriter: DaemonWriter = direct(TV_REMOTE_COMMAND_NAME, (input) => [
requiredDaemonString(input.button, 'tv-remote requires button'),
]);
export const hardwareButtonDaemonWriter: DaemonWriter = direct(
HARDWARE_BUTTON_COMMAND_NAME,
(input) => [requiredDaemonString(input.button, 'hardware-button requires button')],
);
export const telephonyDaemonWriter: DaemonWriter = direct(TELEPHONY_COMMAND_NAME, (input) =>
telephonyPositionals(parseTelephonyEventInput(input)),
);
const appStateCommandFacet = defineParameterlessCommandFacet({
name: APPSTATE_COMMAND_NAME,
effect: 'write',
description: appStateCommandDescription,
text: {
summary: 'Show the foreground app and activity',
},
run: (client, input) => client.command.appState(input),
});
const backCommandFacet = defineCommandFacet({
name: BACK_COMMAND_NAME,
effect: 'write',
text: {
summary: 'Navigate back in the app or system',
},
metadata: backCommandMetadata,
run: (client, input) => client.command.back(input),
cliSchema: backCliSchema,
cliReader: backCliReader,
daemonWriter: backDaemonWriter,
});
const homeCommandFacet = defineParameterlessCommandFacet({
name: HOME_COMMAND_NAME,
effect: 'write',
description: homeCommandDescription,
text: {
summary: 'Go to the device home screen',
},
run: (client, input) => client.command.home(input),
});
const orientationCommandFacet = defineCommandFacet({
name: ORIENTATION_COMMAND_NAME,
effect: 'write',
text: {
summary: 'Set device orientation',
},
metadata: orientationCommandMetadata,
run: (client, input) => client.command.orientation(input),
cliSchema: orientationCliSchema,
cliReader: orientationCliReader,
daemonWriter: orientationDaemonWriter,
});
const foldCommandFacet = defineCommandFacet({
name: FOLD_COMMAND_NAME,
effect: 'write',
text: {
summary: 'Fold or unfold a foldable iPhone simulator or Android emulator',
cliDetail: `iPhone Duo simulators and foldable Android emulators. iOS sends a ${foldGuidance.hingeEvent} and confirms the hinge angle inside the simulator; Android sets the emulator console posture and confirms the device state. Refs and coordinates do not survive a pose change.`,
},
metadata: foldCommandMetadata,
run: (client, input) => client.command.fold(input),
cliSchema: foldCliSchema,
cliReader: foldCliReader,
daemonWriter: foldDaemonWriter,
});
const appSwitcherCommandFacet = defineParameterlessCommandFacet({
name: APP_SWITCHER_COMMAND_NAME,
effect: 'write',
description: appSwitcherCommandDescription,
text: {
summary: 'Open the device app switcher',
},
run: (client, input) => client.command.appSwitcher(input),
});
const keyboardCommandFacet = defineCommandFacet({
name: KEYBOARD_COMMAND_NAME,
effect: 'destructive',
text: {
summary: 'Inspect, press, or dismiss the device keyboard',
},
metadata: keyboardCommandMetadata,
run: (client, input) => client.command.keyboard(input),
cliSchema: keyboardCliSchema,
cliReader: keyboardCliReader,
daemonWriter: keyboardDaemonWriter,
});
const clipboardCommandFacet = defineCommandFacet({
name: CLIPBOARD_COMMAND_NAME,
effect: 'destructive',
text: {
summary: 'Read or write device clipboard text',
},
metadata: clipboardCommandMetadata,
run: (client, input) => client.command.clipboard(input as ClipboardCommandOptions),
cliSchema: clipboardCliSchema,
cliReader: clipboardCliReader,
daemonWriter: clipboardDaemonWriter,
});
const actionButtonCommandFacet = defineParameterlessCommandFacet({
name: ACTION_BUTTON_COMMAND_NAME,
effect: 'destructive',
description: actionButtonCommandDescription,
text: {
summary: 'Press the iPhone or iPad Action Button',
cliDetail:
'iPhone and iPad only. The runner asks the device for the button and reports unsupported when that model has none.',
},
run: (client, input) => client.command.actionButton(input),
});
const tvRemoteCommandFacet = defineCommandFacet({
name: TV_REMOTE_COMMAND_NAME,
effect: 'destructive',
text: {
summary: 'Press a TV remote navigation, media, or hardware button',
cliDetail:
'longpress selects the 500ms preset; --duration-ms overrides it. Android TV uses the system long-press duration.',
},
metadata: tvRemoteCommandMetadata,
run: (client, input) => client.command.tvRemote(input),
cliSchema: tvRemoteCliSchema,
cliReader: tvRemoteCliReader,
daemonWriter: tvRemoteDaemonWriter,
});
const hardwareButtonCommandFacet = defineCommandFacet({
name: HARDWARE_BUTTON_COMMAND_NAME,
effect: 'write',
text: {
summary: 'Press a phone hardware button once',
cliDetail:
'Single press; no hold. Android presses all eleven named keys on phones and tablets: volume-up, volume-down, power, mute, sleep, wake, and the five media keys. A physical iPhone or iPad presses volume only; the other keys are never supported on Apple targets. iOS Simulators, Android TV, and tvOS refuse — use tv-remote on TV targets.',
},
metadata: hardwareButtonCommandMetadata,
run: (client, input) => client.command.hardwareButton(input),
cliSchema: hardwareButtonCliSchema,
cliReader: hardwareButtonCliReader,
daemonWriter: hardwareButtonDaemonWriter,
});
const telephonyCommandFacet = defineCommandFacet({
name: TELEPHONY_COMMAND_NAME,
effect: 'destructive',
text: {
summary: 'Simulate an incoming call or SMS on an Android emulator over adb',
cliDetail:
'Android emulators through the local adb executor; WebDriver (Appium) and Limrun sessions refuse it. Quote the SMS text, or pass it as the remaining words. Re-snapshot after the event: the call screen or notification can cover the app.',
},
metadata: telephonyCommandMetadata,
run: (client, input) => client.command.telephony(input),
cliSchema: telephonyCliSchema,
cliReader: telephonyCliReader,
daemonWriter: telephonyDaemonWriter,
});
export const systemCommandFamily = defineCommandFamilyFromFacets({
name: 'system',
commands: [
appStateCommandFacet,
backCommandFacet,
homeCommandFacet,
orientationCommandFacet,
foldCommandFacet,
appSwitcherCommandFacet,
actionButtonCommandFacet,
keyboardCommandFacet,
clipboardCommandFacet,
tvRemoteCommandFacet,
hardwareButtonCommandFacet,
telephonyCommandFacet,
],
});
function readBackMode(value: unknown): BackMode | undefined {
return value === 'in-app' || value === 'system' ? value : undefined;
}
function clipboardPositionals(input: ClipboardCommandOptions): string[] {
return input.action === 'read' ? ['read'] : ['write', input.text];
}
function readKeyboardInput(positionals: string[]): Record<string, unknown> {
if (positionals.length > 1) {
throw new AppError('INVALID_ARGS', 'keyboard accepts at most one action argument.');
}
return compactRecord({ action: readKeyboardAction(positionals[0]) });
}
function readClipboardInput(positionals: string[]): Record<string, unknown> {
const action = positionals[0]?.toLowerCase();
if (action !== 'read' && action !== 'write') {
throw new AppError('INVALID_ARGS', 'clipboard requires a subcommand: read or write.');
}
if (action === 'read') {
if (positionals.length !== 1) {
throw new AppError('INVALID_ARGS', 'clipboard read does not accept additional arguments.');
}
return { action };
}
if (positionals.length < 2) {
throw new AppError('INVALID_ARGS', 'clipboard write requires text.');
}
return { action, text: positionals.slice(1).join(' ') };
}
function readTvRemoteInput(
positionals: string[],
durationMs: number | undefined,
): Record<string, unknown> {
const subcommand = positionals[0]?.toLowerCase();
const isNamedAction = subcommand === 'press' || subcommand === 'longpress';
const args = isNamedAction ? positionals.slice(1) : positionals;
if (args.length !== 1) {
throw new AppError(
'INVALID_ARGS',
`tv-remote requires exactly one button: ${TV_REMOTE_BUTTONS.join(', ')}, or android-keycode:<n>.`,
);
}
const effectiveDurationMs =
durationMs ?? (subcommand === 'longpress' ? TV_REMOTE_LONGPRESS_PRESET_MS : undefined);
return compactRecord({
button: parseTvRemoteButton(args[0]),
durationMs: effectiveDurationMs,
});
}
function readHardwareButtonInput(positionals: string[]): Record<string, unknown> {
// One shared helper answers arity and parse for the CLI reader and the daemon resolver alike.
return { button: HARDWARE_BUTTON_NAME_LABELS[parseHardwareButtonPositionals(positionals)] };
}
function readKeyboardAction(
value: string | undefined,
): 'status' | 'dismiss' | 'enter' | 'return' | undefined {
const action = value?.toLowerCase();
if (action === 'get') return 'status';
if (
action === undefined ||
action === 'status' ||
action === 'dismiss' ||
action === 'enter' ||
action === 'return'
) {
return action;
}
throw new AppError(
'INVALID_ARGS',
'keyboard action must be status, get, dismiss, enter, or return.',
);
}