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.
Files changed (91) hide show
  1. package/README.md +2 -9
  2. package/dist/src/797.js +1 -1
  3. package/dist/src/bin.js +5 -5
  4. package/dist/src/daemon.js +16 -20
  5. package/package.json +7 -9
  6. package/skills/agent-device/SKILL.md +3 -6
  7. package/skills/agent-device/references/permissions.md +3 -15
  8. package/skills/agent-device/references/snapshot-refs.md +1 -4
  9. package/dist/bin/axsnapshot +0 -0
  10. package/ios-runner/AXSnapshot/Package.swift +0 -18
  11. package/ios-runner/AXSnapshot/Sources/AXSnapshot/main.swift +0 -444
  12. package/src/__tests__/cli-close.test.ts +0 -155
  13. package/src/__tests__/cli-help.test.ts +0 -102
  14. package/src/bin.ts +0 -3
  15. package/src/cli.ts +0 -305
  16. package/src/core/__tests__/capabilities.test.ts +0 -75
  17. package/src/core/__tests__/dispatch-open.test.ts +0 -25
  18. package/src/core/__tests__/open-target.test.ts +0 -55
  19. package/src/core/capabilities.ts +0 -57
  20. package/src/core/dispatch.ts +0 -382
  21. package/src/core/open-target.ts +0 -27
  22. package/src/daemon/__tests__/device-ready.test.ts +0 -52
  23. package/src/daemon/__tests__/is-predicates.test.ts +0 -68
  24. package/src/daemon/__tests__/selectors.test.ts +0 -261
  25. package/src/daemon/__tests__/session-routing.test.ts +0 -108
  26. package/src/daemon/__tests__/session-selector.test.ts +0 -64
  27. package/src/daemon/__tests__/session-store.test.ts +0 -142
  28. package/src/daemon/__tests__/snapshot-processing.test.ts +0 -47
  29. package/src/daemon/action-utils.ts +0 -29
  30. package/src/daemon/context.ts +0 -48
  31. package/src/daemon/device-ready.ts +0 -155
  32. package/src/daemon/handlers/__tests__/find.test.ts +0 -99
  33. package/src/daemon/handlers/__tests__/interaction.test.ts +0 -22
  34. package/src/daemon/handlers/__tests__/replay-heal.test.ts +0 -509
  35. package/src/daemon/handlers/__tests__/session-reinstall.test.ts +0 -219
  36. package/src/daemon/handlers/__tests__/session.test.ts +0 -820
  37. package/src/daemon/handlers/__tests__/snapshot-handler.test.ts +0 -92
  38. package/src/daemon/handlers/__tests__/snapshot.test.ts +0 -128
  39. package/src/daemon/handlers/find.ts +0 -324
  40. package/src/daemon/handlers/interaction.ts +0 -550
  41. package/src/daemon/handlers/parse-utils.ts +0 -8
  42. package/src/daemon/handlers/record-trace.ts +0 -154
  43. package/src/daemon/handlers/session.ts +0 -1137
  44. package/src/daemon/handlers/snapshot.ts +0 -439
  45. package/src/daemon/is-predicates.ts +0 -46
  46. package/src/daemon/selectors.ts +0 -540
  47. package/src/daemon/session-routing.ts +0 -22
  48. package/src/daemon/session-selector.ts +0 -39
  49. package/src/daemon/session-store.ts +0 -296
  50. package/src/daemon/snapshot-processing.ts +0 -131
  51. package/src/daemon/types.ts +0 -56
  52. package/src/daemon-client.ts +0 -272
  53. package/src/daemon.ts +0 -295
  54. package/src/platforms/__tests__/boot-diagnostics.test.ts +0 -59
  55. package/src/platforms/android/__tests__/index.test.ts +0 -274
  56. package/src/platforms/android/devices.ts +0 -196
  57. package/src/platforms/android/index.ts +0 -784
  58. package/src/platforms/android/ui-hierarchy.ts +0 -312
  59. package/src/platforms/boot-diagnostics.ts +0 -128
  60. package/src/platforms/ios/__tests__/index.test.ts +0 -312
  61. package/src/platforms/ios/__tests__/runner-client.test.ts +0 -155
  62. package/src/platforms/ios/apps.ts +0 -358
  63. package/src/platforms/ios/ax-snapshot.ts +0 -207
  64. package/src/platforms/ios/config.ts +0 -28
  65. package/src/platforms/ios/devicectl.ts +0 -134
  66. package/src/platforms/ios/devices.ts +0 -100
  67. package/src/platforms/ios/index.ts +0 -20
  68. package/src/platforms/ios/runner-client.ts +0 -994
  69. package/src/platforms/ios/simulator.ts +0 -164
  70. package/src/utils/__tests__/args.test.ts +0 -239
  71. package/src/utils/__tests__/daemon-client.test.ts +0 -95
  72. package/src/utils/__tests__/exec.test.ts +0 -16
  73. package/src/utils/__tests__/finders.test.ts +0 -34
  74. package/src/utils/__tests__/keyed-lock.test.ts +0 -55
  75. package/src/utils/__tests__/process-identity.test.ts +0 -33
  76. package/src/utils/__tests__/retry.test.ts +0 -44
  77. package/src/utils/args.ts +0 -239
  78. package/src/utils/command-schema.ts +0 -622
  79. package/src/utils/device.ts +0 -84
  80. package/src/utils/errors.ts +0 -35
  81. package/src/utils/exec.ts +0 -339
  82. package/src/utils/finders.ts +0 -101
  83. package/src/utils/interactive.ts +0 -4
  84. package/src/utils/interactors.ts +0 -173
  85. package/src/utils/keyed-lock.ts +0 -14
  86. package/src/utils/output.ts +0 -204
  87. package/src/utils/process-identity.ts +0 -100
  88. package/src/utils/retry.ts +0 -180
  89. package/src/utils/snapshot.ts +0 -64
  90. package/src/utils/timeouts.ts +0 -9
  91. package/src/utils/version.ts +0 -26
@@ -1,622 +0,0 @@
1
- export type CliFlags = {
2
- json: boolean;
3
- platform?: 'ios' | 'android';
4
- device?: string;
5
- udid?: string;
6
- serial?: string;
7
- out?: string;
8
- session?: string;
9
- verbose?: boolean;
10
- snapshotInteractiveOnly?: boolean;
11
- snapshotCompact?: boolean;
12
- snapshotDepth?: number;
13
- snapshotScope?: string;
14
- snapshotRaw?: boolean;
15
- snapshotBackend?: 'ax' | 'xctest';
16
- appsFilter?: 'user-installed' | 'all';
17
- count?: number;
18
- intervalMs?: number;
19
- holdMs?: number;
20
- jitterPx?: number;
21
- pauseMs?: number;
22
- pattern?: 'one-way' | 'ping-pong';
23
- activity?: string;
24
- saveScript?: boolean | string;
25
- relaunch?: boolean;
26
- noRecord?: boolean;
27
- replayUpdate?: boolean;
28
- help: boolean;
29
- version: boolean;
30
- };
31
-
32
- export type FlagKey = keyof CliFlags;
33
- export type FlagType = 'boolean' | 'int' | 'enum' | 'string' | 'booleanOrString';
34
-
35
- export type FlagDefinition = {
36
- key: FlagKey;
37
- names: readonly string[];
38
- type: FlagType;
39
- enumValues?: readonly string[];
40
- min?: number;
41
- max?: number;
42
- setValue?: CliFlags[FlagKey];
43
- usageLabel?: string;
44
- usageDescription?: string;
45
- };
46
-
47
- export type CommandSchema = {
48
- description: string;
49
- positionalArgs: readonly string[];
50
- allowsExtraPositionals?: boolean;
51
- allowedFlags: readonly FlagKey[];
52
- defaults?: Partial<CliFlags>;
53
- skipCapabilityCheck?: boolean;
54
- usageOverride?: string;
55
- };
56
-
57
- const SNAPSHOT_FLAGS = [
58
- 'snapshotInteractiveOnly',
59
- 'snapshotCompact',
60
- 'snapshotDepth',
61
- 'snapshotScope',
62
- 'snapshotRaw',
63
- 'snapshotBackend',
64
- ] as const satisfies readonly FlagKey[];
65
-
66
- const SELECTOR_SNAPSHOT_FLAGS = [
67
- 'snapshotDepth',
68
- 'snapshotScope',
69
- 'snapshotRaw',
70
- 'snapshotBackend',
71
- ] as const satisfies readonly FlagKey[];
72
-
73
- const FIND_SNAPSHOT_FLAGS = ['snapshotDepth', 'snapshotRaw', 'snapshotBackend'] as const satisfies readonly FlagKey[];
74
-
75
- export const FLAG_DEFINITIONS: readonly FlagDefinition[] = [
76
- {
77
- key: 'platform',
78
- names: ['--platform'],
79
- type: 'enum',
80
- enumValues: ['ios', 'android'],
81
- usageLabel: '--platform ios|android',
82
- usageDescription: 'Platform to target',
83
- },
84
- {
85
- key: 'device',
86
- names: ['--device'],
87
- type: 'string',
88
- usageLabel: '--device <name>',
89
- usageDescription: 'Device name to target',
90
- },
91
- {
92
- key: 'udid',
93
- names: ['--udid'],
94
- type: 'string',
95
- usageLabel: '--udid <udid>',
96
- usageDescription: 'iOS device UDID',
97
- },
98
- {
99
- key: 'serial',
100
- names: ['--serial'],
101
- type: 'string',
102
- usageLabel: '--serial <serial>',
103
- usageDescription: 'Android device serial',
104
- },
105
- {
106
- key: 'activity',
107
- names: ['--activity'],
108
- type: 'string',
109
- usageLabel: '--activity <component>',
110
- usageDescription: 'Android app launch activity (package/Activity); not for URL opens',
111
- },
112
- {
113
- key: 'session',
114
- names: ['--session'],
115
- type: 'string',
116
- usageLabel: '--session <name>',
117
- usageDescription: 'Named session',
118
- },
119
- {
120
- key: 'count',
121
- names: ['--count'],
122
- type: 'int',
123
- min: 1,
124
- max: 200,
125
- usageLabel: '--count <n>',
126
- usageDescription: 'Repeat count for press/swipe series',
127
- },
128
- {
129
- key: 'intervalMs',
130
- names: ['--interval-ms'],
131
- type: 'int',
132
- min: 0,
133
- max: 10_000,
134
- usageLabel: '--interval-ms <ms>',
135
- usageDescription: 'Delay between press iterations',
136
- },
137
- {
138
- key: 'holdMs',
139
- names: ['--hold-ms'],
140
- type: 'int',
141
- min: 0,
142
- max: 10_000,
143
- usageLabel: '--hold-ms <ms>',
144
- usageDescription: 'Press hold duration for each iteration',
145
- },
146
- {
147
- key: 'jitterPx',
148
- names: ['--jitter-px'],
149
- type: 'int',
150
- min: 0,
151
- max: 100,
152
- usageLabel: '--jitter-px <n>',
153
- usageDescription: 'Deterministic coordinate jitter radius for press',
154
- },
155
- {
156
- key: 'pauseMs',
157
- names: ['--pause-ms'],
158
- type: 'int',
159
- min: 0,
160
- max: 10_000,
161
- usageLabel: '--pause-ms <ms>',
162
- usageDescription: 'Delay between swipe iterations',
163
- },
164
- {
165
- key: 'pattern',
166
- names: ['--pattern'],
167
- type: 'enum',
168
- enumValues: ['one-way', 'ping-pong'],
169
- usageLabel: '--pattern one-way|ping-pong',
170
- usageDescription: 'Swipe repeat pattern',
171
- },
172
- {
173
- key: 'verbose',
174
- names: ['--verbose', '-v'],
175
- type: 'boolean',
176
- usageLabel: '--verbose',
177
- usageDescription: 'Stream daemon/runner logs',
178
- },
179
- {
180
- key: 'json',
181
- names: ['--json'],
182
- type: 'boolean',
183
- usageLabel: '--json',
184
- usageDescription: 'JSON output',
185
- },
186
- {
187
- key: 'help',
188
- names: ['--help', '-h'],
189
- type: 'boolean',
190
- usageLabel: '--help, -h',
191
- usageDescription: 'Print help and exit',
192
- },
193
- {
194
- key: 'version',
195
- names: ['--version', '-V'],
196
- type: 'boolean',
197
- usageLabel: '--version, -V',
198
- usageDescription: 'Print version and exit',
199
- },
200
- {
201
- key: 'saveScript',
202
- names: ['--save-script'],
203
- type: 'booleanOrString',
204
- usageLabel: '--save-script [path]',
205
- usageDescription: 'Save session script (.ad) on close; optional custom output path',
206
- },
207
- {
208
- key: 'relaunch',
209
- names: ['--relaunch'],
210
- type: 'boolean',
211
- usageLabel: '--relaunch',
212
- usageDescription: 'open: terminate app process before launching it',
213
- },
214
- {
215
- key: 'noRecord',
216
- names: ['--no-record'],
217
- type: 'boolean',
218
- usageLabel: '--no-record',
219
- usageDescription: 'Do not record this action',
220
- },
221
- {
222
- key: 'replayUpdate',
223
- names: ['--update', '-u'],
224
- type: 'boolean',
225
- usageLabel: '--update, -u',
226
- usageDescription: 'Replay: update selectors and rewrite replay file in place',
227
- },
228
- {
229
- key: 'appsFilter',
230
- names: ['--user-installed'],
231
- type: 'enum',
232
- setValue: 'user-installed',
233
- usageLabel: '--user-installed',
234
- usageDescription: 'Apps: list user-installed apps',
235
- },
236
- {
237
- key: 'appsFilter',
238
- names: ['--all'],
239
- type: 'enum',
240
- setValue: 'all',
241
- usageLabel: '--all',
242
- usageDescription: 'Apps: list all apps (include system/default apps)',
243
- },
244
- {
245
- key: 'snapshotInteractiveOnly',
246
- names: ['-i'],
247
- type: 'boolean',
248
- usageLabel: '-i',
249
- usageDescription: 'Snapshot: interactive elements only',
250
- },
251
- {
252
- key: 'snapshotCompact',
253
- names: ['-c'],
254
- type: 'boolean',
255
- usageLabel: '-c',
256
- usageDescription: 'Snapshot: compact output (drop empty structure)',
257
- },
258
- {
259
- key: 'snapshotDepth',
260
- names: ['--depth', '-d'],
261
- type: 'int',
262
- min: 0,
263
- usageLabel: '--depth, -d <depth>',
264
- usageDescription: 'Snapshot: limit snapshot depth',
265
- },
266
- {
267
- key: 'snapshotScope',
268
- names: ['--scope', '-s'],
269
- type: 'string',
270
- usageLabel: '--scope, -s <scope>',
271
- usageDescription: 'Snapshot: scope snapshot to label/identifier',
272
- },
273
- {
274
- key: 'snapshotRaw',
275
- names: ['--raw'],
276
- type: 'boolean',
277
- usageLabel: '--raw',
278
- usageDescription: 'Snapshot: raw node output',
279
- },
280
- {
281
- key: 'snapshotBackend',
282
- names: ['--backend'],
283
- type: 'enum',
284
- enumValues: ['ax', 'xctest'],
285
- usageLabel: '--backend ax|xctest',
286
- usageDescription: 'Snapshot backend (iOS): ax or xctest',
287
- },
288
- {
289
- key: 'out',
290
- names: ['--out'],
291
- type: 'string',
292
- usageLabel: '--out <path>',
293
- usageDescription: 'Output path',
294
- },
295
- ];
296
-
297
- export const GLOBAL_FLAG_KEYS = new Set<FlagKey>([
298
- 'json',
299
- 'help',
300
- 'version',
301
- 'verbose',
302
- 'platform',
303
- 'device',
304
- 'udid',
305
- 'serial',
306
- 'session',
307
- 'noRecord',
308
- ]);
309
-
310
- export const COMMAND_SCHEMAS: Record<string, CommandSchema> = {
311
- boot: {
312
- description: 'Ensure target device/simulator is booted and ready',
313
- positionalArgs: [],
314
- allowedFlags: [],
315
- },
316
- open: {
317
- description: 'Boot device/simulator; optionally launch app or deep link URL',
318
- positionalArgs: ['appOrUrl?', 'url?'],
319
- allowedFlags: ['activity', 'saveScript', 'relaunch'],
320
- },
321
- close: {
322
- description: 'Close app or just end session',
323
- positionalArgs: ['app?'],
324
- allowedFlags: ['saveScript'],
325
- },
326
- reinstall: {
327
- description: 'Uninstall + install app from binary path',
328
- positionalArgs: ['app', 'path'],
329
- allowedFlags: [],
330
- },
331
- snapshot: {
332
- description: 'Capture accessibility tree',
333
- positionalArgs: [],
334
- allowedFlags: [...SNAPSHOT_FLAGS],
335
- },
336
- devices: {
337
- description: 'List available devices',
338
- positionalArgs: [],
339
- allowedFlags: [],
340
- skipCapabilityCheck: true,
341
- },
342
- apps: {
343
- description: 'List installed apps (includes default/system apps by default)',
344
- positionalArgs: [],
345
- allowedFlags: ['appsFilter'],
346
- defaults: { appsFilter: 'all' },
347
- },
348
- appstate: {
349
- description: 'Show foreground app/activity',
350
- positionalArgs: [],
351
- allowedFlags: [],
352
- skipCapabilityCheck: true,
353
- },
354
- back: {
355
- description: 'Navigate back (where supported)',
356
- positionalArgs: [],
357
- allowedFlags: [],
358
- },
359
- home: {
360
- description: 'Go to home screen (where supported)',
361
- positionalArgs: [],
362
- allowedFlags: [],
363
- },
364
- 'app-switcher': {
365
- description: 'Open app switcher (where supported)',
366
- positionalArgs: [],
367
- allowedFlags: [],
368
- },
369
- wait: {
370
- usageOverride: 'wait <ms>|text <text>|@ref|<selector> [timeoutMs]',
371
- description: 'Wait for duration, text, ref, or selector to appear',
372
- positionalArgs: ['durationOrSelector', 'timeoutMs?'],
373
- allowsExtraPositionals: true,
374
- allowedFlags: [...SELECTOR_SNAPSHOT_FLAGS],
375
- },
376
- alert: {
377
- usageOverride: 'alert [get|accept|dismiss|wait] [timeout]',
378
- description: 'Inspect or handle alert (iOS simulator)',
379
- positionalArgs: ['action?', 'timeout?'],
380
- allowedFlags: [],
381
- },
382
- click: {
383
- usageOverride: 'click <@ref|selector>',
384
- description: 'Click element by snapshot ref or selector',
385
- positionalArgs: ['target'],
386
- allowedFlags: [...SELECTOR_SNAPSHOT_FLAGS],
387
- },
388
- get: {
389
- usageOverride: 'get text|attrs <@ref|selector>',
390
- description: 'Return element text/attributes by ref or selector',
391
- positionalArgs: ['subcommand', 'target'],
392
- allowedFlags: [...SELECTOR_SNAPSHOT_FLAGS],
393
- },
394
- replay: {
395
- description: 'Replay a recorded session',
396
- positionalArgs: ['path'],
397
- allowedFlags: ['replayUpdate'],
398
- skipCapabilityCheck: true,
399
- },
400
- press: {
401
- description: 'Tap/press at coordinates (supports repeated gesture series)',
402
- positionalArgs: ['x', 'y'],
403
- allowedFlags: ['count', 'intervalMs', 'holdMs', 'jitterPx'],
404
- },
405
- 'long-press': {
406
- description: 'Long press (where supported)',
407
- positionalArgs: ['x', 'y', 'durationMs?'],
408
- allowedFlags: [],
409
- },
410
- swipe: {
411
- description: 'Swipe coordinates with optional repeat pattern',
412
- positionalArgs: ['x1', 'y1', 'x2', 'y2', 'durationMs?'],
413
- allowedFlags: ['count', 'pauseMs', 'pattern'],
414
- },
415
- focus: {
416
- description: 'Focus input at coordinates',
417
- positionalArgs: ['x', 'y'],
418
- allowedFlags: [],
419
- },
420
- type: {
421
- description: 'Type text in focused field',
422
- positionalArgs: ['text'],
423
- allowsExtraPositionals: true,
424
- allowedFlags: [],
425
- },
426
- fill: {
427
- usageOverride: 'fill <x> <y> <text> | fill <@ref|selector> <text>',
428
- description: 'Tap then type',
429
- positionalArgs: ['targetOrX', 'yOrText', 'text?'],
430
- allowsExtraPositionals: true,
431
- allowedFlags: [...SELECTOR_SNAPSHOT_FLAGS],
432
- },
433
- scroll: {
434
- description: 'Scroll in direction (0-1 amount)',
435
- positionalArgs: ['direction', 'amount?'],
436
- allowedFlags: [],
437
- },
438
- scrollintoview: {
439
- description: 'Scroll until text appears',
440
- positionalArgs: ['text'],
441
- allowedFlags: [],
442
- },
443
- pinch: {
444
- description: 'Pinch/zoom gesture (iOS simulator)',
445
- positionalArgs: ['scale', 'x?', 'y?'],
446
- allowedFlags: [],
447
- },
448
- screenshot: {
449
- description: 'Capture screenshot',
450
- positionalArgs: ['path?'],
451
- allowedFlags: ['out'],
452
- },
453
- record: {
454
- usageOverride: 'record start [path] | record stop',
455
- description: 'Start/stop screen recording',
456
- positionalArgs: ['start|stop', 'path?'],
457
- allowedFlags: [],
458
- },
459
- trace: {
460
- usageOverride: 'trace start [path] | trace stop [path]',
461
- description: 'Start/stop trace log capture',
462
- positionalArgs: ['start|stop', 'path?'],
463
- allowedFlags: [],
464
- skipCapabilityCheck: true,
465
- },
466
- find: {
467
- usageOverride: 'find <locator|text> <action> [value]',
468
- description: 'Find by text/label/value/role/id and run action',
469
- positionalArgs: ['query', 'action', 'value?'],
470
- allowsExtraPositionals: true,
471
- allowedFlags: [...FIND_SNAPSHOT_FLAGS],
472
- },
473
- is: {
474
- description: 'Assert UI state (visible|hidden|exists|editable|selected|text)',
475
- positionalArgs: ['predicate', 'selector', 'value?'],
476
- allowsExtraPositionals: true,
477
- allowedFlags: [...SELECTOR_SNAPSHOT_FLAGS],
478
- },
479
- settings: {
480
- description: 'Toggle OS settings (simulators)',
481
- positionalArgs: ['setting', 'state'],
482
- allowedFlags: [],
483
- },
484
- session: {
485
- usageOverride: 'session list',
486
- description: 'List active sessions',
487
- positionalArgs: ['list?'],
488
- allowedFlags: [],
489
- skipCapabilityCheck: true,
490
- },
491
- };
492
-
493
- const flagDefinitionByName = new Map<string, FlagDefinition>();
494
- const flagDefinitionsByKey = new Map<FlagKey, FlagDefinition[]>();
495
- for (const definition of FLAG_DEFINITIONS) {
496
- for (const name of definition.names) {
497
- flagDefinitionByName.set(name, definition);
498
- }
499
- const list = flagDefinitionsByKey.get(definition.key);
500
- if (list) list.push(definition);
501
- else flagDefinitionsByKey.set(definition.key, [definition]);
502
- }
503
-
504
- export function getFlagDefinition(token: string): FlagDefinition | undefined {
505
- return flagDefinitionByName.get(token);
506
- }
507
-
508
- export function getCommandSchema(command: string | null): CommandSchema | undefined {
509
- if (!command) return undefined;
510
- return COMMAND_SCHEMAS[command];
511
- }
512
-
513
- export function getCliCommandNames(): string[] {
514
- return Object.keys(COMMAND_SCHEMAS);
515
- }
516
-
517
- export function getSchemaCapabilityKeys(): string[] {
518
- return Object.entries(COMMAND_SCHEMAS)
519
- .filter(([, schema]) => !schema.skipCapabilityCheck)
520
- .map(([name]) => name)
521
- .sort();
522
- }
523
-
524
- export function isStrictFlagModeEnabled(value: string | undefined): boolean {
525
- if (!value) return false;
526
- const normalized = value.trim().toLowerCase();
527
- return normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on';
528
- }
529
-
530
- function formatPositionalArg(arg: string): string {
531
- const optional = arg.endsWith('?');
532
- const name = optional ? arg.slice(0, -1) : arg;
533
- return optional ? `[${name}]` : `<${name}>`;
534
- }
535
-
536
- function buildCommandUsage(commandName: string, schema: CommandSchema): string {
537
- if (schema.usageOverride) return schema.usageOverride;
538
- const positionals = schema.positionalArgs.map(formatPositionalArg);
539
- const flagLabels = schema.allowedFlags.flatMap((key) =>
540
- (flagDefinitionsByKey.get(key) ?? []).map((definition) => definition.usageLabel ?? definition.names[0]),
541
- );
542
- const optionalFlags = flagLabels.map((label) => `[${label}]`);
543
- return [commandName, ...positionals, ...optionalFlags].join(' ');
544
- }
545
-
546
- function renderUsageText(): string {
547
- const header = `agent-device <command> [args] [--json]
548
-
549
- CLI to control iOS and Android devices for AI agents.
550
- `;
551
-
552
- const commands = getCliCommandNames().map((name) => {
553
- const schema = COMMAND_SCHEMAS[name];
554
- if (!schema) throw new Error(`Missing command schema for ${name}`);
555
- return { name, schema, usage: buildCommandUsage(name, schema) };
556
- });
557
- const maxUsage = Math.max(...commands.map((command) => command.usage.length)) + 2;
558
- const commandLines: string[] = ['Commands:'];
559
- for (const command of commands) {
560
- commandLines.push(` ${command.usage.padEnd(maxUsage)}${command.schema.description}`);
561
- }
562
-
563
- const helpFlags = FLAG_DEFINITIONS
564
- .filter((definition) => definition.usageLabel && definition.usageDescription);
565
- const flagsSection = renderFlagSection('Flags:', helpFlags);
566
-
567
- return `${header}
568
- ${commandLines.join('\n')}
569
-
570
- ${flagsSection}
571
- `;
572
- }
573
-
574
- const USAGE_TEXT = renderUsageText();
575
-
576
- export function buildUsageText(): string {
577
- return USAGE_TEXT;
578
- }
579
-
580
- function listHelpFlags(keys: ReadonlySet<FlagKey>): FlagDefinition[] {
581
- return FLAG_DEFINITIONS.filter(
582
- (definition) =>
583
- keys.has(definition.key) &&
584
- definition.usageLabel !== undefined &&
585
- definition.usageDescription !== undefined,
586
- );
587
- }
588
-
589
- function renderFlagSection(title: string, definitions: FlagDefinition[]): string {
590
- if (definitions.length === 0) {
591
- return `${title}\n (none)`;
592
- }
593
- const maxFlagLabel = Math.max(...definitions.map((flag) => (flag.usageLabel ?? '').length)) + 2;
594
- const lines = [title];
595
- for (const flag of definitions) {
596
- lines.push(` ${(flag.usageLabel ?? '').padEnd(maxFlagLabel)}${flag.usageDescription ?? ''}`);
597
- }
598
- return lines.join('\n');
599
- }
600
-
601
- export function buildCommandUsageText(commandName: string): string | null {
602
- const schema = getCommandSchema(commandName);
603
- if (!schema) return null;
604
- const usage = buildCommandUsage(commandName, schema);
605
- const commandFlags = listHelpFlags(new Set<FlagKey>(schema.allowedFlags));
606
- const globalFlags = listHelpFlags(GLOBAL_FLAG_KEYS);
607
- const sections: string[] = [];
608
- if (commandFlags.length > 0) {
609
- sections.push(renderFlagSection('Command flags:', commandFlags));
610
- }
611
- sections.push(renderFlagSection('Global flags:', globalFlags));
612
-
613
- return `agent-device ${usage}
614
-
615
- ${schema.description}
616
-
617
- Usage:
618
- agent-device ${usage}
619
-
620
- ${sections.join('\n\n')}
621
- `;
622
- }
@@ -1,84 +0,0 @@
1
- import { AppError } from './errors.ts';
2
- import { isInteractive } from './interactive.ts';
3
- import { isCancel, select } from '@clack/prompts';
4
-
5
- export type Platform = 'ios' | 'android';
6
- export type DeviceKind = 'simulator' | 'emulator' | 'device';
7
-
8
- export type DeviceInfo = {
9
- platform: Platform;
10
- id: string;
11
- name: string;
12
- kind: DeviceKind;
13
- booted?: boolean;
14
- };
15
-
16
- export type DeviceSelector = {
17
- platform?: Platform;
18
- deviceName?: string;
19
- udid?: string;
20
- serial?: string;
21
- };
22
-
23
- export async function selectDevice(
24
- devices: DeviceInfo[],
25
- selector: DeviceSelector,
26
- ): Promise<DeviceInfo> {
27
- let candidates = devices;
28
- const normalize = (value: string): string =>
29
- value.toLowerCase().replace(/_/g, ' ').replace(/\s+/g, ' ').trim();
30
-
31
- if (selector.platform) {
32
- candidates = candidates.filter((d) => d.platform === selector.platform);
33
- }
34
-
35
- if (selector.udid) {
36
- const match = candidates.find((d) => d.id === selector.udid && d.platform === 'ios');
37
- if (!match) throw new AppError('DEVICE_NOT_FOUND', `No iOS device with UDID ${selector.udid}`);
38
- return match;
39
- }
40
-
41
- if (selector.serial) {
42
- const match = candidates.find((d) => d.id === selector.serial && d.platform === 'android');
43
- if (!match)
44
- throw new AppError('DEVICE_NOT_FOUND', `No Android device with serial ${selector.serial}`);
45
- return match;
46
- }
47
-
48
- if (selector.deviceName) {
49
- const target = normalize(selector.deviceName);
50
- const match = candidates.find((d) => normalize(d.name) === target);
51
- if (!match) {
52
- throw new AppError('DEVICE_NOT_FOUND', `No device named ${selector.deviceName}`);
53
- }
54
- return match;
55
- }
56
-
57
- if (candidates.length === 1) return candidates[0];
58
-
59
- if (candidates.length === 0) {
60
- throw new AppError('DEVICE_NOT_FOUND', 'No devices found', { selector });
61
- }
62
-
63
- const booted = candidates.filter((d) => d.booted);
64
- if (booted.length === 1) return booted[0];
65
-
66
- if (isInteractive()) {
67
- const choice = await select({
68
- message: 'Multiple devices available. Choose a device to continue:',
69
- options: (booted.length > 0 ? booted : candidates).map((device) => ({
70
- label: `${device.name} (${device.platform}${device.kind ? `, ${device.kind}` : ''}${device.booted ? ', booted' : ''})`,
71
- value: device.id,
72
- })),
73
- });
74
- if (isCancel(choice)) {
75
- throw new AppError('INVALID_ARGS', 'Device selection cancelled');
76
- }
77
- if (choice) {
78
- const match = candidates.find((d) => d.id === choice);
79
- if (match) return match;
80
- }
81
- }
82
-
83
- return booted[0] ?? candidates[0];
84
- }