agent-device 0.5.0 → 0.5.2
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 +62 -6
- package/dist/src/50.js +1 -0
- package/dist/src/bin.js +33 -32
- package/dist/src/daemon.js +18 -17
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj/project.pbxproj +2 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/AgentDeviceRunnerUITests-Bridging-Header.h +1 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerObjCExceptionCatcher.h +11 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerObjCExceptionCatcher.m +16 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +255 -15
- package/package.json +1 -1
- package/skills/agent-device/SKILL.md +54 -4
- package/skills/agent-device/references/batching.md +79 -0
- package/skills/agent-device/references/permissions.md +4 -0
- package/skills/agent-device/references/snapshot-refs.md +3 -2
- package/dist/src/797.js +0 -1
package/README.md
CHANGED
|
@@ -34,17 +34,64 @@ npx agent-device open SampleApp
|
|
|
34
34
|
## Quick Start
|
|
35
35
|
|
|
36
36
|
Use refs for agent-driven exploration and normal automation flows.
|
|
37
|
+
Use `press` as the canonical tap command; `click` is an equivalent alias.
|
|
37
38
|
|
|
38
39
|
```bash
|
|
39
40
|
agent-device open Contacts --platform ios # creates session on iOS Simulator
|
|
40
41
|
agent-device snapshot
|
|
41
|
-
agent-device
|
|
42
|
+
agent-device press @e5
|
|
42
43
|
agent-device fill @e6 "John"
|
|
43
44
|
agent-device fill @e7 "Doe"
|
|
44
|
-
agent-device
|
|
45
|
+
agent-device press @e3
|
|
45
46
|
agent-device close
|
|
46
47
|
```
|
|
47
48
|
|
|
49
|
+
## Fast batching (JSON steps)
|
|
50
|
+
|
|
51
|
+
Use `batch` to execute multiple commands in a single daemon request.
|
|
52
|
+
|
|
53
|
+
CLI examples:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
agent-device batch \
|
|
57
|
+
--session sim \
|
|
58
|
+
--platform ios \
|
|
59
|
+
--udid 00008150-001849640CF8401C \
|
|
60
|
+
--steps-file /tmp/batch-steps.json \
|
|
61
|
+
--json
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Small inline payloads are also supported:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
agent-device batch --steps '[{"command":"open","positionals":["settings"]},{"command":"wait","positionals":["100"]}]'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Batch payload format:
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
[
|
|
74
|
+
{ "command": "open", "positionals": ["settings"], "flags": {} },
|
|
75
|
+
{ "command": "wait", "positionals": ["label=\"Privacy & Security\"", "3000"], "flags": {} },
|
|
76
|
+
{ "command": "click", "positionals": ["label=\"Privacy & Security\""], "flags": {} },
|
|
77
|
+
{ "command": "get", "positionals": ["text", "label=\"Tracking\""], "flags": {} }
|
|
78
|
+
]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Batch response includes:
|
|
82
|
+
|
|
83
|
+
- `total`, `executed`, `totalDurationMs`
|
|
84
|
+
- per-step `results[]` with `durationMs`
|
|
85
|
+
- failure context with failing `step` and `partialResults`
|
|
86
|
+
|
|
87
|
+
Agent usage guidelines:
|
|
88
|
+
|
|
89
|
+
- Keep each batch to one screen-local workflow.
|
|
90
|
+
- Add sync guards (`wait`, `is exists`) after mutating steps (`open`, `click`, `fill`, `swipe`).
|
|
91
|
+
- Treat refs/snapshot assumptions as stale after UI mutations.
|
|
92
|
+
- Prefer `--steps-file` over inline JSON for reliability.
|
|
93
|
+
- Keep batches moderate (about 5-20 steps) and stop on first error.
|
|
94
|
+
|
|
48
95
|
## CLI Usage
|
|
49
96
|
|
|
50
97
|
```bash
|
|
@@ -56,7 +103,7 @@ Basic flow:
|
|
|
56
103
|
```bash
|
|
57
104
|
agent-device open SampleApp
|
|
58
105
|
agent-device snapshot
|
|
59
|
-
agent-device
|
|
106
|
+
agent-device press @e7
|
|
60
107
|
agent-device fill @e8 "hello"
|
|
61
108
|
agent-device close SampleApp
|
|
62
109
|
```
|
|
@@ -73,19 +120,23 @@ agent-device trace stop ./trace.log
|
|
|
73
120
|
Coordinates:
|
|
74
121
|
- All coordinate-based commands (`press`, `long-press`, `swipe`, `focus`, `fill`) use device coordinates with origin at top-left.
|
|
75
122
|
- X increases to the right, Y increases downward.
|
|
123
|
+
- `press` is the canonical tap command.
|
|
124
|
+
- `click` is an equivalent alias and accepts the same targets (`x y`, `@ref`, selector) and flags.
|
|
76
125
|
|
|
77
126
|
Gesture series examples:
|
|
78
127
|
|
|
79
128
|
```bash
|
|
80
129
|
agent-device press 300 500 --count 12 --interval-ms 45
|
|
81
130
|
agent-device press 300 500 --count 6 --hold-ms 120 --interval-ms 30 --jitter-px 2
|
|
131
|
+
agent-device press @e5 --count 5 --double-tap
|
|
82
132
|
agent-device swipe 540 1500 540 500 120 --count 8 --pause-ms 30 --pattern ping-pong
|
|
83
133
|
```
|
|
84
134
|
|
|
85
135
|
## Command Index
|
|
86
136
|
- `boot`, `open`, `close`, `reinstall`, `home`, `back`, `app-switcher`
|
|
137
|
+
- `batch`
|
|
87
138
|
- `snapshot`, `find`, `get`
|
|
88
|
-
- `click
|
|
139
|
+
- `press` (alias: `click`), `focus`, `type`, `fill`, `long-press`, `swipe`, `scroll`, `scrollintoview`, `pinch`, `is`
|
|
89
140
|
- `alert`, `wait`, `screenshot`
|
|
90
141
|
- `trace start`, `trace stop`
|
|
91
142
|
- `settings wifi|airplane|location on|off`
|
|
@@ -110,10 +161,15 @@ Flags:
|
|
|
110
161
|
- `--interval-ms <ms>` delay between `press` iterations
|
|
111
162
|
- `--hold-ms <ms>` hold duration per `press` iteration
|
|
112
163
|
- `--jitter-px <n>` deterministic coordinate jitter for `press`
|
|
164
|
+
- `--double-tap` use a double-tap gesture per `press`/`click` iteration (cannot be combined with `--hold-ms` or `--jitter-px`)
|
|
113
165
|
- `--pause-ms <ms>` delay between `swipe` iterations
|
|
114
166
|
- `--pattern one-way|ping-pong` repeat pattern for `swipe`
|
|
115
167
|
- `--verbose` for daemon and runner logs
|
|
116
168
|
- `--json` for structured output
|
|
169
|
+
- `--steps <json>` batch: JSON array of steps
|
|
170
|
+
- `--steps-file <path>` batch: read step JSON from file
|
|
171
|
+
- `--on-error stop` batch: stop when a step fails
|
|
172
|
+
- `--max-steps <n>` batch: max allowed steps per request
|
|
117
173
|
|
|
118
174
|
Pinch:
|
|
119
175
|
- `pinch` is supported on iOS simulators.
|
|
@@ -147,7 +203,7 @@ Navigation helpers:
|
|
|
147
203
|
- `boot --platform ios|android` ensures the target is ready without launching an app.
|
|
148
204
|
- Use `boot` mainly when starting a new session and `open` fails because no booted simulator/emulator is available.
|
|
149
205
|
- `open [app|url] [url]` already boots/activates the selected target when needed.
|
|
150
|
-
- `reinstall <app> <path>` uninstalls and installs the app binary in one command (Android + iOS simulator).
|
|
206
|
+
- `reinstall <app> <path>` uninstalls and installs the app binary in one command (Android + iOS simulator/device).
|
|
151
207
|
- `reinstall` accepts package/bundle id style app names and supports `~` in paths.
|
|
152
208
|
|
|
153
209
|
Deep links:
|
|
@@ -242,7 +298,7 @@ Boot diagnostics:
|
|
|
242
298
|
|
|
243
299
|
## iOS notes
|
|
244
300
|
- Core runner commands: `snapshot`, `wait`, `click`, `fill`, `get`, `is`, `find`, `press`, `long-press`, `focus`, `type`, `scroll`, `scrollintoview`, `back`, `home`, `app-switcher`.
|
|
245
|
-
- Simulator-only commands: `alert`, `pinch`, `record`, `
|
|
301
|
+
- Simulator-only commands: `alert`, `pinch`, `record`, `settings`.
|
|
246
302
|
- iOS device runs require valid signing/provisioning (Automatic Signing recommended). Optional overrides: `AGENT_DEVICE_IOS_TEAM_ID`, `AGENT_DEVICE_IOS_SIGNING_IDENTITY`, `AGENT_DEVICE_IOS_PROVISIONING_PROFILE`.
|
|
247
303
|
|
|
248
304
|
## Testing
|
package/dist/src/50.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t,{promises as e}from"node:fs";import r from"node:path";import{fileURLToPath as n,pathToFileURL as o}from"node:url";import{spawn as i,spawnSync as s}from"node:child_process";class a extends Error{code;details;cause;constructor(t,e,r,n){super(e),this.code=t,this.details=r,this.cause=n}}function u(t){return t instanceof a?t:t instanceof Error?new a("UNKNOWN",t.message,void 0,t):new a("UNKNOWN","Unknown error",{err:t})}function d(){try{let e=l();return JSON.parse(t.readFileSync(r.join(e,"package.json"),"utf8")).version??"0.0.0"}catch{return"0.0.0"}}function l(){let e=r.dirname(n(import.meta.url)),o=e;for(let e=0;e<6;e+=1){let e=r.join(o,"package.json");if(t.existsSync(e))return o;o=r.dirname(o)}return e}async function c(t,e,r={}){return new Promise((n,o)=>{let s=i(t,e,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"]}),u="",d=r.binaryStdout?Buffer.alloc(0):void 0,l="",c=!1,f=A(r.timeoutMs),p=f?setTimeout(()=>{c=!0,s.kill("SIGKILL")},f):null;r.binaryStdout||s.stdout.setEncoding("utf8"),s.stderr.setEncoding("utf8"),void 0!==r.stdin&&s.stdin.write(r.stdin),s.stdin.end(),s.stdout.on("data",t=>{r.binaryStdout?d=Buffer.concat([d??Buffer.alloc(0),Buffer.isBuffer(t)?t:Buffer.from(t)]):u+=t}),s.stderr.on("data",t=>{l+=t}),s.on("error",r=>{(p&&clearTimeout(p),"ENOENT"===r.code)?o(new a("TOOL_MISSING",`${t} not found in PATH`,{cmd:t},r)):o(new a("COMMAND_FAILED",`Failed to run ${t}`,{cmd:t,args:e},r))}),s.on("close",i=>{p&&clearTimeout(p);let s=i??1;c&&f?o(new a("COMMAND_FAILED",`${t} timed out after ${f}ms`,{cmd:t,args:e,stdout:u,stderr:l,exitCode:s,timeoutMs:f})):0===s||r.allowFailure?n({stdout:u,stderr:l,exitCode:s,stdoutBuffer:d}):o(new a("COMMAND_FAILED",`${t} exited with code ${s}`,{cmd:t,args:e,stdout:u,stderr:l,exitCode:s}))})})}async function f(t){try{var e;let{shell:r,args:n}=(e=t,"win32"===process.platform?{shell:"cmd.exe",args:["/c","where",e]}:{shell:"bash",args:["-lc",`command -v ${e}`]}),o=await c(r,n,{allowFailure:!0});return 0===o.exitCode&&o.stdout.trim().length>0}catch{return!1}}function p(t,e,r={}){let n=s(t,e,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"],encoding:r.binaryStdout?void 0:"utf8",input:r.stdin,timeout:A(r.timeoutMs)});if(n.error){let o=n.error.code;if("ETIMEDOUT"===o)throw new a("COMMAND_FAILED",`${t} timed out after ${A(r.timeoutMs)}ms`,{cmd:t,args:e,timeoutMs:A(r.timeoutMs)},n.error);if("ENOENT"===o)throw new a("TOOL_MISSING",`${t} not found in PATH`,{cmd:t},n.error);throw new a("COMMAND_FAILED",`Failed to run ${t}`,{cmd:t,args:e},n.error)}let o=r.binaryStdout?Buffer.isBuffer(n.stdout)?n.stdout:Buffer.from(n.stdout??""):void 0,i=r.binaryStdout?"":"string"==typeof n.stdout?n.stdout:(n.stdout??"").toString(),u="string"==typeof n.stderr?n.stderr:(n.stderr??"").toString(),d=n.status??1;if(0!==d&&!r.allowFailure)throw new a("COMMAND_FAILED",`${t} exited with code ${d}`,{cmd:t,args:e,stdout:i,stderr:u,exitCode:d});return{stdout:i,stderr:u,exitCode:d,stdoutBuffer:o}}function m(t,e,r={}){i(t,e,{cwd:r.cwd,env:r.env,stdio:"ignore",detached:!0}).unref()}async function w(t,e,r={}){return new Promise((n,o)=>{let s=i(t,e,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"]}),u="",d="",l=r.binaryStdout?Buffer.alloc(0):void 0;r.binaryStdout||s.stdout.setEncoding("utf8"),s.stderr.setEncoding("utf8"),void 0!==r.stdin&&s.stdin.write(r.stdin),s.stdin.end(),s.stdout.on("data",t=>{if(r.binaryStdout){l=Buffer.concat([l??Buffer.alloc(0),Buffer.isBuffer(t)?t:Buffer.from(t)]);return}let e=String(t);u+=e,r.onStdoutChunk?.(e)}),s.stderr.on("data",t=>{let e=String(t);d+=e,r.onStderrChunk?.(e)}),s.on("error",r=>{"ENOENT"===r.code?o(new a("TOOL_MISSING",`${t} not found in PATH`,{cmd:t},r)):o(new a("COMMAND_FAILED",`Failed to run ${t}`,{cmd:t,args:e},r))}),s.on("close",i=>{let s=i??1;0===s||r.allowFailure?n({stdout:u,stderr:d,exitCode:s,stdoutBuffer:l}):o(new a("COMMAND_FAILED",`${t} exited with code ${s}`,{cmd:t,args:e,stdout:u,stderr:d,exitCode:s}))})})}function h(t,e,r={}){let n=i(t,e,{cwd:r.cwd,env:r.env,stdio:["ignore","pipe","pipe"]}),o="",s="";n.stdout.setEncoding("utf8"),n.stderr.setEncoding("utf8"),n.stdout.on("data",t=>{o+=t}),n.stderr.on("data",t=>{s+=t});let u=new Promise((i,u)=>{n.on("error",r=>{"ENOENT"===r.code?u(new a("TOOL_MISSING",`${t} not found in PATH`,{cmd:t},r)):u(new a("COMMAND_FAILED",`Failed to run ${t}`,{cmd:t,args:e},r))}),n.on("close",n=>{let d=n??1;0===d||r.allowFailure?i({stdout:o,stderr:s,exitCode:d}):u(new a("COMMAND_FAILED",`${t} exited with code ${d}`,{cmd:t,args:e,stdout:o,stderr:s,exitCode:d}))})});return{child:n,wait:u}}function A(t){if(!Number.isFinite(t))return;let e=Math.floor(t);if(!(e<=0))return e}let S=[/(^|[\/\s"'=])dist\/src\/daemon\.js($|[\s"'])/,/(^|[\/\s"'=])src\/daemon\.ts($|[\s"'])/];function I(t){if(!Number.isInteger(t)||t<=0)return!1;try{return process.kill(t,0),!0}catch(t){return"EPERM"===t.code}}function g(t){if(!Number.isInteger(t)||t<=0)return null;try{let e=p("ps",["-p",String(t),"-o","lstart="],{allowFailure:!0,timeoutMs:1e3});if(0!==e.exitCode)return null;let r=e.stdout.trim();return r.length>0?r:null}catch{return null}}function N(t,e){let r;if(!I(t))return!1;if(e){let r=g(t);if(!r||r!==e)return!1}let n=function(t){if(!Number.isInteger(t)||t<=0)return null;try{let e=p("ps",["-p",String(t),"-o","command="],{allowFailure:!0,timeoutMs:1e3});if(0!==e.exitCode)return null;let r=e.stdout.trim();return r.length>0?r:null}catch{return null}}(t);return!!n&&!!(r=n.toLowerCase().replaceAll("\\","/")).includes("agent-device")&&S.some(t=>t.test(r))}function y(t,e){try{return process.kill(t,e),!0}catch(e){let t=e.code;if("ESRCH"===t||"EPERM"===t)return!1;throw e}}async function M(t,e){if(!I(t))return!0;let r=Date.now();for(;Date.now()-r<e;)if(await new Promise(t=>setTimeout(t,50)),!I(t))return!0;return!I(t)}async function E(t,e){!N(t,e.expectedStartTime)||!y(t,"SIGTERM")||await M(t,e.termTimeoutMs)||y(t,"SIGKILL")&&await M(t,e.killTimeoutMs)}let D=100,L=new Set(["batch","replay"]);function $(t){let e;try{e=JSON.parse(t)}catch{throw new a("INVALID_ARGS","Batch steps must be valid JSON.")}if(!Array.isArray(e)||0===e.length)throw new a("INVALID_ARGS","Batch steps must be a non-empty JSON array.");return e}function _(t,e){if(!Array.isArray(t)||0===t.length)throw new a("INVALID_ARGS","batch requires a non-empty batchSteps array.");if(t.length>e)throw new a("INVALID_ARGS",`batch has ${t.length} steps; max allowed is ${e}.`);let r=[];for(let e=0;e<t.length;e+=1){let n=t[e];if(!n||"object"!=typeof n)throw new a("INVALID_ARGS",`Invalid batch step at index ${e}.`);let o="string"==typeof n.command?n.command.trim().toLowerCase():"";if(!o)throw new a("INVALID_ARGS",`Batch step ${e+1} requires command.`);if(L.has(o))throw new a("INVALID_ARGS",`Batch step ${e+1} cannot run ${o}.`);if(void 0!==n.positionals&&!Array.isArray(n.positionals))throw new a("INVALID_ARGS",`Batch step ${e+1} positionals must be an array.`);let i=n.positionals??[];if(i.some(t=>"string"!=typeof t))throw new a("INVALID_ARGS",`Batch step ${e+1} positionals must contain only strings.`);if(void 0!==n.flags&&("object"!=typeof n.flags||Array.isArray(n.flags)||!n.flags))throw new a("INVALID_ARGS",`Batch step ${e+1} flags must be an object.`);r.push({command:o,positionals:i,flags:n.flags??{}})}return r}export{default as node_net}from"node:net";export{default as node_os}from"node:os";export{a as AppError,D as DEFAULT_BATCH_MAX_STEPS,u as asAppError,n as fileURLToPath,l as findProjectRoot,N as isAgentDeviceDaemonProcess,I as isProcessAlive,t as node_fs,r as node_path,$ as parseBatchStepsJson,o as pathToFileURL,e as promises,g as readProcessStartTime,d as readVersion,c as runCmd,h as runCmdBackground,m as runCmdDetached,w as runCmdStreaming,E as stopProcessForTakeover,_ as validateAndNormalizeBatchSteps,f as whichCmd};
|
package/dist/src/bin.js
CHANGED
|
@@ -1,53 +1,54 @@
|
|
|
1
|
-
import{node_path as e,asAppError as t,isAgentDeviceDaemonProcess as s,runCmdDetached as a,node_fs as i,node_os as n,node_net as r,AppError as o,readVersion as l,findProjectRoot as p,stopProcessForTakeover as c,pathToFileURL as u}from"./797.js";let d=["snapshotDepth","snapshotScope","snapshotRaw"],g=[{key:"platform",names:["--platform"],type:"enum",enumValues:["ios","android"],usageLabel:"--platform ios|android",usageDescription:"Platform to target"},{key:"device",names:["--device"],type:"string",usageLabel:"--device <name>",usageDescription:"Device name to target"},{key:"udid",names:["--udid"],type:"string",usageLabel:"--udid <udid>",usageDescription:"iOS device UDID"},{key:"serial",names:["--serial"],type:"string",usageLabel:"--serial <serial>",usageDescription:"Android device serial"},{key:"activity",names:["--activity"],type:"string",usageLabel:"--activity <component>",usageDescription:"Android app launch activity (package/Activity); not for URL opens"},{key:"session",names:["--session"],type:"string",usageLabel:"--session <name>",usageDescription:"Named session"},{key:"count",names:["--count"],type:"int",min:1,max:200,usageLabel:"--count <n>",usageDescription:"Repeat count for press/swipe series"},{key:"intervalMs",names:["--interval-ms"],type:"int",min:0,max:1e4,usageLabel:"--interval-ms <ms>",usageDescription:"Delay between press iterations"},{key:"holdMs",names:["--hold-ms"],type:"int",min:0,max:1e4,usageLabel:"--hold-ms <ms>",usageDescription:"Press hold duration for each iteration"},{key:"jitterPx",names:["--jitter-px"],type:"int",min:0,max:100,usageLabel:"--jitter-px <n>",usageDescription:"Deterministic coordinate jitter radius for press"},{key:"pauseMs",names:["--pause-ms"],type:"int",min:0,max:1e4,usageLabel:"--pause-ms <ms>",usageDescription:"Delay between swipe iterations"},{key:"pattern",names:["--pattern"],type:"enum",enumValues:["one-way","ping-pong"],usageLabel:"--pattern one-way|ping-pong",usageDescription:"Swipe repeat pattern"},{key:"verbose",names:["--verbose","-v"],type:"boolean",usageLabel:"--verbose",usageDescription:"Stream daemon/runner logs"},{key:"json",names:["--json"],type:"boolean",usageLabel:"--json",usageDescription:"JSON output"},{key:"help",names:["--help","-h"],type:"boolean",usageLabel:"--help, -h",usageDescription:"Print help and exit"},{key:"version",names:["--version","-V"],type:"boolean",usageLabel:"--version, -V",usageDescription:"Print version and exit"},{key:"saveScript",names:["--save-script"],type:"booleanOrString",usageLabel:"--save-script [path]",usageDescription:"Save session script (.ad) on close; optional custom output path"},{key:"relaunch",names:["--relaunch"],type:"boolean",usageLabel:"--relaunch",usageDescription:"open: terminate app process before launching it"},{key:"noRecord",names:["--no-record"],type:"boolean",usageLabel:"--no-record",usageDescription:"Do not record this action"},{key:"replayUpdate",names:["--update","-u"],type:"boolean",usageLabel:"--update, -u",usageDescription:"Replay: update selectors and rewrite replay file in place"},{key:"appsFilter",names:["--user-installed"],type:"enum",setValue:"user-installed",usageLabel:"--user-installed",usageDescription:"Apps: list user-installed apps"},{key:"appsFilter",names:["--all"],type:"enum",setValue:"all",usageLabel:"--all",usageDescription:"Apps: list all apps (include system/default apps)"},{key:"snapshotInteractiveOnly",names:["-i"],type:"boolean",usageLabel:"-i",usageDescription:"Snapshot: interactive elements only"},{key:"snapshotCompact",names:["-c"],type:"boolean",usageLabel:"-c",usageDescription:"Snapshot: compact output (drop empty structure)"},{key:"snapshotDepth",names:["--depth","-d"],type:"int",min:0,usageLabel:"--depth, -d <depth>",usageDescription:"Snapshot: limit snapshot depth"},{key:"snapshotScope",names:["--scope","-s"],type:"string",usageLabel:"--scope, -s <scope>",usageDescription:"Snapshot: scope snapshot to label/identifier"},{key:"snapshotRaw",names:["--raw"],type:"boolean",usageLabel:"--raw",usageDescription:"Snapshot: raw node output"},{key:"out",names:["--out"],type:"string",usageLabel:"--out <path>",usageDescription:"Output path"}],m=new Set(["json","help","version","verbose","platform","device","udid","serial","session","noRecord"]),f={boot:{description:"Ensure target device/simulator is booted and ready",positionalArgs:[],allowedFlags:[]},open:{description:"Boot device/simulator; optionally launch app or deep link URL",positionalArgs:["appOrUrl?","url?"],allowedFlags:["activity","saveScript","relaunch"]},close:{description:"Close app or just end session",positionalArgs:["app?"],allowedFlags:["saveScript"]},reinstall:{description:"Uninstall + install app from binary path",positionalArgs:["app","path"],allowedFlags:[]},snapshot:{description:"Capture accessibility tree",positionalArgs:[],allowedFlags:["snapshotInteractiveOnly","snapshotCompact","snapshotDepth","snapshotScope","snapshotRaw"]},devices:{description:"List available devices",positionalArgs:[],allowedFlags:[],skipCapabilityCheck:!0},apps:{description:"List installed apps (includes default/system apps by default)",positionalArgs:[],allowedFlags:["appsFilter"],defaults:{appsFilter:"all"}},appstate:{description:"Show foreground app/activity",positionalArgs:[],allowedFlags:[],skipCapabilityCheck:!0},back:{description:"Navigate back (where supported)",positionalArgs:[],allowedFlags:[]},home:{description:"Go to home screen (where supported)",positionalArgs:[],allowedFlags:[]},"app-switcher":{description:"Open app switcher (where supported)",positionalArgs:[],allowedFlags:[]},wait:{usageOverride:"wait <ms>|text <text>|@ref|<selector> [timeoutMs]",description:"Wait for duration, text, ref, or selector to appear",positionalArgs:["durationOrSelector","timeoutMs?"],allowsExtraPositionals:!0,allowedFlags:[...d]},alert:{usageOverride:"alert [get|accept|dismiss|wait] [timeout]",description:"Inspect or handle alert (iOS simulator)",positionalArgs:["action?","timeout?"],allowedFlags:[]},click:{usageOverride:"click <@ref|selector>",description:"Click element by snapshot ref or selector",positionalArgs:["target"],allowedFlags:[...d]},get:{usageOverride:"get text|attrs <@ref|selector>",description:"Return element text/attributes by ref or selector",positionalArgs:["subcommand","target"],allowedFlags:[...d]},replay:{description:"Replay a recorded session",positionalArgs:["path"],allowedFlags:["replayUpdate"],skipCapabilityCheck:!0},press:{description:"Tap/press at coordinates (supports repeated gesture series)",positionalArgs:["x","y"],allowedFlags:["count","intervalMs","holdMs","jitterPx"]},"long-press":{description:"Long press (where supported)",positionalArgs:["x","y","durationMs?"],allowedFlags:[]},swipe:{description:"Swipe coordinates with optional repeat pattern",positionalArgs:["x1","y1","x2","y2","durationMs?"],allowedFlags:["count","pauseMs","pattern"]},focus:{description:"Focus input at coordinates",positionalArgs:["x","y"],allowedFlags:[]},type:{description:"Type text in focused field",positionalArgs:["text"],allowsExtraPositionals:!0,allowedFlags:[]},fill:{usageOverride:"fill <x> <y> <text> | fill <@ref|selector> <text>",description:"Tap then type",positionalArgs:["targetOrX","yOrText","text?"],allowsExtraPositionals:!0,allowedFlags:[...d]},scroll:{description:"Scroll in direction (0-1 amount)",positionalArgs:["direction","amount?"],allowedFlags:[]},scrollintoview:{description:"Scroll until text appears",positionalArgs:["text"],allowedFlags:[]},pinch:{description:"Pinch/zoom gesture (iOS simulator)",positionalArgs:["scale","x?","y?"],allowedFlags:[]},screenshot:{description:"Capture screenshot",positionalArgs:["path?"],allowedFlags:["out"]},record:{usageOverride:"record start [path] | record stop",description:"Start/stop screen recording",positionalArgs:["start|stop","path?"],allowedFlags:[]},trace:{usageOverride:"trace start [path] | trace stop [path]",description:"Start/stop trace log capture",positionalArgs:["start|stop","path?"],allowedFlags:[],skipCapabilityCheck:!0},find:{usageOverride:"find <locator|text> <action> [value]",description:"Find by text/label/value/role/id and run action",positionalArgs:["query","action","value?"],allowsExtraPositionals:!0,allowedFlags:["snapshotDepth","snapshotRaw"]},is:{description:"Assert UI state (visible|hidden|exists|editable|selected|text)",positionalArgs:["predicate","selector","value?"],allowsExtraPositionals:!0,allowedFlags:[...d]},settings:{description:"Toggle OS settings (simulators)",positionalArgs:["setting","state"],allowedFlags:[]},session:{usageOverride:"session list",description:"List active sessions",positionalArgs:["list?"],allowedFlags:[],skipCapabilityCheck:!0}},h=new Map,y=new Map;for(let e of g){for(let t of e.names)h.set(t,e);let t=y.get(e.key);t?t.push(e):y.set(e.key,[e])}function w(e){if(e)return f[e]}function v(e){let t=e.endsWith("?"),s=t?e.slice(0,-1):e;return t?`[${s}]`:`<${s}>`}function b(e,t){return t.usageOverride?t.usageOverride:[e,...t.positionalArgs.map(v),...t.allowedFlags.flatMap(e=>(y.get(e)??[]).map(e=>e.usageLabel??e.names[0])).map(e=>`[${e}]`)].join(" ")}let x=function(){let e=`agent-device <command> [args] [--json]
|
|
1
|
+
import{node_path as e,parseBatchStepsJson as t,asAppError as s,isAgentDeviceDaemonProcess as a,runCmdDetached as i,node_fs as r,node_os as o,node_net as n,AppError as l,readVersion as p,findProjectRoot as c,stopProcessForTakeover as u,pathToFileURL as d}from"./50.js";let g=["snapshotDepth","snapshotScope","snapshotRaw"],m=[{key:"platform",names:["--platform"],type:"enum",enumValues:["ios","android"],usageLabel:"--platform ios|android",usageDescription:"Platform to target"},{key:"device",names:["--device"],type:"string",usageLabel:"--device <name>",usageDescription:"Device name to target"},{key:"udid",names:["--udid"],type:"string",usageLabel:"--udid <udid>",usageDescription:"iOS device UDID"},{key:"serial",names:["--serial"],type:"string",usageLabel:"--serial <serial>",usageDescription:"Android device serial"},{key:"activity",names:["--activity"],type:"string",usageLabel:"--activity <component>",usageDescription:"Android app launch activity (package/Activity); not for URL opens"},{key:"session",names:["--session"],type:"string",usageLabel:"--session <name>",usageDescription:"Named session"},{key:"count",names:["--count"],type:"int",min:1,max:200,usageLabel:"--count <n>",usageDescription:"Repeat count for press/swipe series"},{key:"intervalMs",names:["--interval-ms"],type:"int",min:0,max:1e4,usageLabel:"--interval-ms <ms>",usageDescription:"Delay between press iterations"},{key:"holdMs",names:["--hold-ms"],type:"int",min:0,max:1e4,usageLabel:"--hold-ms <ms>",usageDescription:"Press hold duration for each iteration"},{key:"jitterPx",names:["--jitter-px"],type:"int",min:0,max:100,usageLabel:"--jitter-px <n>",usageDescription:"Deterministic coordinate jitter radius for press"},{key:"doubleTap",names:["--double-tap"],type:"boolean",usageLabel:"--double-tap",usageDescription:"Use double-tap gesture per press iteration"},{key:"pauseMs",names:["--pause-ms"],type:"int",min:0,max:1e4,usageLabel:"--pause-ms <ms>",usageDescription:"Delay between swipe iterations"},{key:"pattern",names:["--pattern"],type:"enum",enumValues:["one-way","ping-pong"],usageLabel:"--pattern one-way|ping-pong",usageDescription:"Swipe repeat pattern"},{key:"verbose",names:["--verbose","-v"],type:"boolean",usageLabel:"--verbose",usageDescription:"Stream daemon/runner logs"},{key:"json",names:["--json"],type:"boolean",usageLabel:"--json",usageDescription:"JSON output"},{key:"help",names:["--help","-h"],type:"boolean",usageLabel:"--help, -h",usageDescription:"Print help and exit"},{key:"version",names:["--version","-V"],type:"boolean",usageLabel:"--version, -V",usageDescription:"Print version and exit"},{key:"saveScript",names:["--save-script"],type:"booleanOrString",usageLabel:"--save-script [path]",usageDescription:"Save session script (.ad) on close; optional custom output path"},{key:"relaunch",names:["--relaunch"],type:"boolean",usageLabel:"--relaunch",usageDescription:"open: terminate app process before launching it"},{key:"noRecord",names:["--no-record"],type:"boolean",usageLabel:"--no-record",usageDescription:"Do not record this action"},{key:"replayUpdate",names:["--update","-u"],type:"boolean",usageLabel:"--update, -u",usageDescription:"Replay: update selectors and rewrite replay file in place"},{key:"steps",names:["--steps"],type:"string",usageLabel:"--steps <json>",usageDescription:"Batch: JSON array of steps"},{key:"stepsFile",names:["--steps-file"],type:"string",usageLabel:"--steps-file <path>",usageDescription:"Batch: read steps JSON from file"},{key:"batchOnError",names:["--on-error"],type:"enum",enumValues:["stop"],usageLabel:"--on-error stop",usageDescription:"Batch: stop when a step fails"},{key:"batchMaxSteps",names:["--max-steps"],type:"int",min:1,max:1e3,usageLabel:"--max-steps <n>",usageDescription:"Batch: maximum number of allowed steps"},{key:"appsFilter",names:["--user-installed"],type:"enum",setValue:"user-installed",usageLabel:"--user-installed",usageDescription:"Apps: list user-installed apps"},{key:"appsFilter",names:["--all"],type:"enum",setValue:"all",usageLabel:"--all",usageDescription:"Apps: list all apps (include system/default apps)"},{key:"snapshotInteractiveOnly",names:["-i"],type:"boolean",usageLabel:"-i",usageDescription:"Snapshot: interactive elements only"},{key:"snapshotCompact",names:["-c"],type:"boolean",usageLabel:"-c",usageDescription:"Snapshot: compact output (drop empty structure)"},{key:"snapshotDepth",names:["--depth","-d"],type:"int",min:0,usageLabel:"--depth, -d <depth>",usageDescription:"Snapshot: limit snapshot depth"},{key:"snapshotScope",names:["--scope","-s"],type:"string",usageLabel:"--scope, -s <scope>",usageDescription:"Snapshot: scope snapshot to label/identifier"},{key:"snapshotRaw",names:["--raw"],type:"boolean",usageLabel:"--raw",usageDescription:"Snapshot: raw node output"},{key:"out",names:["--out"],type:"string",usageLabel:"--out <path>",usageDescription:"Output path"}],f=new Set(["json","help","version","verbose","platform","device","udid","serial","session","noRecord"]),h={boot:{description:"Ensure target device/simulator is booted and ready",positionalArgs:[],allowedFlags:[]},open:{description:"Boot device/simulator; optionally launch app or deep link URL",positionalArgs:["appOrUrl?","url?"],allowedFlags:["activity","saveScript","relaunch"]},close:{description:"Close app or just end session",positionalArgs:["app?"],allowedFlags:["saveScript"]},reinstall:{description:"Uninstall + install app from binary path",positionalArgs:["app","path"],allowedFlags:[]},snapshot:{description:"Capture accessibility tree",positionalArgs:[],allowedFlags:["snapshotInteractiveOnly","snapshotCompact","snapshotDepth","snapshotScope","snapshotRaw"]},devices:{description:"List available devices",positionalArgs:[],allowedFlags:[],skipCapabilityCheck:!0},apps:{description:"List installed apps (includes default/system apps by default)",positionalArgs:[],allowedFlags:["appsFilter"],defaults:{appsFilter:"all"}},appstate:{description:"Show foreground app/activity",positionalArgs:[],allowedFlags:[],skipCapabilityCheck:!0},back:{description:"Navigate back (where supported)",positionalArgs:[],allowedFlags:[]},home:{description:"Go to home screen (where supported)",positionalArgs:[],allowedFlags:[]},"app-switcher":{description:"Open app switcher (where supported)",positionalArgs:[],allowedFlags:[]},wait:{usageOverride:"wait <ms>|text <text>|@ref|<selector> [timeoutMs]",description:"Wait for duration, text, ref, or selector to appear",positionalArgs:["durationOrSelector","timeoutMs?"],allowsExtraPositionals:!0,allowedFlags:[...g]},alert:{usageOverride:"alert [get|accept|dismiss|wait] [timeout]",description:"Inspect or handle alert (iOS simulator)",positionalArgs:["action?","timeout?"],allowedFlags:[]},click:{usageOverride:"click <x y|@ref|selector>",description:"Tap/click by coordinates, snapshot ref, or selector",positionalArgs:["target"],allowsExtraPositionals:!0,allowedFlags:["count","intervalMs","holdMs","jitterPx","doubleTap",...g]},get:{usageOverride:"get text|attrs <@ref|selector>",description:"Return element text/attributes by ref or selector",positionalArgs:["subcommand","target"],allowedFlags:[...g]},replay:{description:"Replay a recorded session",positionalArgs:["path"],allowedFlags:["replayUpdate"],skipCapabilityCheck:!0},batch:{usageOverride:"batch [--steps <json> | --steps-file <path>]",description:"Execute multiple commands in one daemon request",positionalArgs:[],allowedFlags:["steps","stepsFile","batchOnError","batchMaxSteps","out"],skipCapabilityCheck:!0},press:{usageOverride:"press <x y|@ref|selector>",description:"Tap/press by coordinates, snapshot ref, or selector (supports repeated series)",positionalArgs:["targetOrX","y?"],allowsExtraPositionals:!0,allowedFlags:["count","intervalMs","holdMs","jitterPx","doubleTap",...g]},"long-press":{description:"Long press (where supported)",positionalArgs:["x","y","durationMs?"],allowedFlags:[]},swipe:{description:"Swipe coordinates with optional repeat pattern",positionalArgs:["x1","y1","x2","y2","durationMs?"],allowedFlags:["count","pauseMs","pattern"]},focus:{description:"Focus input at coordinates",positionalArgs:["x","y"],allowedFlags:[]},type:{description:"Type text in focused field",positionalArgs:["text"],allowsExtraPositionals:!0,allowedFlags:[]},fill:{usageOverride:"fill <x> <y> <text> | fill <@ref|selector> <text>",description:"Tap then type",positionalArgs:["targetOrX","yOrText","text?"],allowsExtraPositionals:!0,allowedFlags:[...g]},scroll:{description:"Scroll in direction (0-1 amount)",positionalArgs:["direction","amount?"],allowedFlags:[]},scrollintoview:{description:"Scroll until text appears",positionalArgs:["text"],allowedFlags:[]},pinch:{description:"Pinch/zoom gesture (iOS simulator)",positionalArgs:["scale","x?","y?"],allowedFlags:[]},screenshot:{description:"Capture screenshot",positionalArgs:["path?"],allowedFlags:["out"]},record:{usageOverride:"record start [path] | record stop",description:"Start/stop screen recording",positionalArgs:["start|stop","path?"],allowedFlags:[]},trace:{usageOverride:"trace start [path] | trace stop [path]",description:"Start/stop trace log capture",positionalArgs:["start|stop","path?"],allowedFlags:[],skipCapabilityCheck:!0},find:{usageOverride:"find <locator|text> <action> [value]",description:"Find by text/label/value/role/id and run action",positionalArgs:["query","action","value?"],allowsExtraPositionals:!0,allowedFlags:["snapshotDepth","snapshotRaw"]},is:{description:"Assert UI state (visible|hidden|exists|editable|selected|text)",positionalArgs:["predicate","selector","value?"],allowsExtraPositionals:!0,allowedFlags:[...g]},settings:{description:"Toggle OS settings (simulators)",positionalArgs:["setting","state"],allowedFlags:[]},session:{usageOverride:"session list",description:"List active sessions",positionalArgs:["list?"],allowedFlags:[],skipCapabilityCheck:!0}},y=new Map,w=new Map;for(let e of m){for(let t of e.names)y.set(t,e);let t=w.get(e.key);t?t.push(e):w.set(e.key,[e])}function v(e){if(e)return h[e]}function b(e){let t=e.endsWith("?"),s=t?e.slice(0,-1):e;return t?`[${s}]`:`<${s}>`}function x(e,t){return t.usageOverride?t.usageOverride:[e,...t.positionalArgs.map(b),...t.allowedFlags.flatMap(e=>(w.get(e)??[]).map(e=>e.usageLabel??e.names[0])).map(e=>`[${e}]`)].join(" ")}let $=function(){let e=`agent-device <command> [args] [--json]
|
|
2
2
|
|
|
3
3
|
CLI to control iOS and Android devices for AI agents.
|
|
4
|
-
`,t=Object.keys(
|
|
4
|
+
`,t=Object.keys(h).map(e=>{let t=h[e];if(!t)throw Error(`Missing command schema for ${e}`);return{name:e,schema:t,usage:x(e,t)}}),s=Math.max(...t.map(e=>e.usage.length))+2,a=["Commands:"];for(let e of t)a.push(` ${e.usage.padEnd(s)}${e.schema.description}`);let i=A("Flags:",m.filter(e=>e.usageLabel&&e.usageDescription));return`${e}
|
|
5
5
|
${a.join("\n")}
|
|
6
6
|
|
|
7
7
|
${i}
|
|
8
|
-
`}();function
|
|
9
|
-
(none)`;let s=Math.max(...t.map(e=>(e.usageLabel??"").length))+2,a=[e];for(let e of t)a.push(` ${(e.usageLabel??"").padEnd(s)}${e.usageDescription??""}`);return a.join("\n")}function
|
|
10
|
-
`)}function
|
|
8
|
+
`}();function k(e){return m.filter(t=>e.has(t.key)&&void 0!==t.usageLabel&&void 0!==t.usageDescription)}function A(e,t){if(0===t.length)return`${e}
|
|
9
|
+
(none)`;let s=Math.max(...t.map(e=>(e.usageLabel??"").length))+2,a=[e];for(let e of t)a.push(` ${(e.usageLabel??"").padEnd(s)}${e.usageDescription??""}`);return a.join("\n")}function S(e){let t=e.indexOf("=");return -1===t?[e,void 0]:[e.slice(0,t),e.slice(t+1)]}function D(e){return e.replace(/^-+/,"")}function F(e){if(!e.startsWith("-")||"-"===e)return!1;let[t]=e.startsWith("--")?S(e):[e,void 0];return void 0!==y.get(t)}function L(e){process.stdout.write(`${JSON.stringify(e,null,2)}
|
|
10
|
+
`)}function I(e){let t=e.details?`
|
|
11
11
|
${JSON.stringify(e.details,null,2)}`:"";process.stderr.write(`Error (${e.code}): ${e.message}${t}
|
|
12
|
-
`)}function
|
|
13
|
-
`)}),
|
|
14
|
-
`);
|
|
15
|
-
`),process.exit(0));let
|
|
16
|
-
`),process.exit(0));let t=function(e){let t=
|
|
12
|
+
`)}function N(e,t,s){let a=O(e.type??"Element"),i=function(e,t){var s,a;let i=e.label?.trim(),r=e.value?.trim();if("text-field"===(s=t)||"text-view"===s||"search"===s){if(r)return r;if(i)return i}else if(i)return i;if(r)return r;let o=e.identifier?.trim();return!o||(a=o,/^[\w.]+:id\/[\w.-]+$/i.test(a)&&("group"===t||"image"===t||"list"===t||"collection"===t))?"":o}(e,a),r=" ".repeat(t),o=e.ref?`@${e.ref}`:"",n=[!1===e.enabled?"disabled":null].filter(Boolean).join(", "),l=n?` [${n}]`:"",p=i?` "${i}"`:"";return s?`${r}${o} [${a}]${l}`.trimEnd():`${r}${o} [${a}]${p}${l}`.trimEnd()}function O(e){let t=e.replace(/XCUIElementType/gi,"").toLowerCase(),s=e.includes(".")&&(e.startsWith("android.")||e.startsWith("androidx.")||e.startsWith("com."));switch(t.includes(".")&&(t=t.replace(/^android\.widget\./,"").replace(/^android\.view\./,"").replace(/^android\.webkit\./,"").replace(/^androidx\./,"").replace(/^com\.google\.android\./,"").replace(/^com\.android\./,"")),t){case"application":return"application";case"navigationbar":return"navigation-bar";case"tabbar":return"tab-bar";case"button":case"imagebutton":return"button";case"link":return"link";case"cell":return"cell";case"statictext":case"checkedtextview":return"text";case"textfield":case"edittext":return"text-field";case"textview":return s?"text":"text-view";case"textarea":return"text-view";case"switch":return"switch";case"slider":return"slider";case"image":case"imageview":return"image";case"webview":return"webview";case"framelayout":case"linearlayout":case"relativelayout":case"constraintlayout":case"viewgroup":case"view":case"group":return"group";case"listview":case"recyclerview":return"list";case"collectionview":return"collection";case"searchfield":return"search";case"segmentedcontrol":return"segmented-control";case"window":return"window";case"checkbox":return"checkbox";case"radio":return"radio";case"menuitem":return"menu-item";case"toolbar":return"toolbar";case"scrollarea":case"scrollview":case"nestedscrollview":return"scroll-area";case"table":return"table";default:return t||"element"}}let j=e.join(o.homedir(),".agent-device"),E=e.join(j,"daemon.json"),M=e.join(j,"daemon.lock"),T=function(e=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if(!e)return 9e4;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):9e4}();async function _(e){let t=await C(),s={...e,token:t.token};return await X(t,s)}async function C(){var e;let t=G(),s=p(),i=!!t&&await q(t);if(t&&t.version===s&&i)return t;t&&(t.version!==s||!i)&&(await P(t),J(E)),function(){let e=W();if(!e.hasLock||e.hasInfo)return;let t=U();t&&a(t.pid,t.processStartTime)||J(M)}(),await z();let r=await R(5e3);if(r)return r;if(await V()){await z();let e=await R(5e3);if(e)return e}throw new l("COMMAND_FAILED","Failed to start daemon",{kind:"daemon_startup_failed",infoPath:E,lockPath:M,hint:(e=W()).hasLock&&!e.hasInfo?"Detected ~/.agent-device/daemon.lock without daemon.json. If no agent-device daemon process is running, delete ~/.agent-device/daemon.lock and retry.":e.hasLock&&e.hasInfo?"Daemon metadata may be stale. If no agent-device daemon process is running, delete ~/.agent-device/daemon.json and ~/.agent-device/daemon.lock, then retry.":"Daemon metadata is missing or stale. Delete ~/.agent-device/daemon.json if present and retry."})}async function R(e){let t=Date.now();for(;Date.now()-t<e;){let e=G();if(e&&await q(e))return e;await new Promise(e=>setTimeout(e,100))}return null}async function V(){let e=W();if(!e.hasLock||e.hasInfo)return!1;let t=U();return t&&a(t.pid,t.processStartTime)&&await u(t.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:t.processStartTime}),J(M),!0}async function P(e){await u(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}function G(){let e=B(E);return e&&e.port&&e.token?{...e,pid:Number.isInteger(e.pid)&&e.pid>0?e.pid:0}:null}function U(){let e=B(M);return e&&Number.isInteger(e.pid)&&!(e.pid<=0)?e:null}function W(){return{hasInfo:r.existsSync(E),hasLock:r.existsSync(M)}}function B(e){if(!r.existsSync(e))return null;try{return JSON.parse(r.readFileSync(e,"utf8"))}catch{return null}}function J(e){try{r.existsSync(e)&&r.unlinkSync(e)}catch{}}async function q(e){return new Promise(t=>{let s=n.createConnection({host:"127.0.0.1",port:e.port},()=>{s.destroy(),t(!0)});s.on("error",()=>{t(!1)})})}async function z(){let t=c(),s=e.join(t,"dist","src","daemon.js"),a=e.join(t,"src","daemon.ts"),o=r.existsSync(s),n=r.existsSync(a);if(!o&&!n)throw new l("COMMAND_FAILED","Daemon entry not found",{distPath:s,srcPath:a});let p=(process.execArgv.includes("--experimental-strip-types")?n:!o&&n)?["--experimental-strip-types",a]:[s];i(process.execPath,p)}async function X(e,t){return new Promise((s,a)=>{let i=n.createConnection({host:"127.0.0.1",port:e.port},()=>{i.write(`${JSON.stringify(t)}
|
|
13
|
+
`)}),r=setTimeout(()=>{i.destroy(),a(new l("COMMAND_FAILED","Daemon request timed out",{timeoutMs:T}))},T),o="";i.setEncoding("utf8"),i.on("data",e=>{let t=(o+=e).indexOf("\n");if(-1===t)return;let n=o.slice(0,t).trim();if(n)try{let e=JSON.parse(n);i.end(),clearTimeout(r),s(e)}catch(e){clearTimeout(r),a(e)}}),i.on("error",e=>{clearTimeout(r),a(e)})})}let K={sendToDaemon:_};async function Z(a,i=K){var n,c;let u=function(e,t){let s=(void 0)??function(e){if(!e)return!1;let t=e.trim().toLowerCase();return"1"===t||"true"===t||"yes"===t||"on"===t}(process.env.AGENT_DEVICE_STRICT_FLAGS),a={json:!1,help:!1,version:!1},i=null,r=[],o=[],n=[],p=!0;for(let t=0;t<e.length;t+=1){let s=e[t];if(p&&"--"===s){p=!1;continue}if(!p){i?r.push(s):i=s;continue}let o=s.startsWith("--"),c=s.startsWith("-")&&s.length>1;if(!o&&!c){i?r.push(s):i=s;continue}let[u,d]=o?S(s):[s,void 0],g=y.get(u);if(!g){if(function(e,t,s){var a;if(a=s,!/^-\d+(\.\d+)?$/.test(a)||!e)return!1;let i=v(e);return!i||!!i.allowsExtraPositionals||0!==i.positionalArgs.length&&(t.length<i.positionalArgs.length||i.positionalArgs.some(e=>e.includes("?")))}(i,r,s)){i?r.push(s):i=s;continue}throw new l("INVALID_ARGS",`Unknown flag: ${u}`)}let m=function(e,t,s,a){if(void 0!==e.setValue){if(void 0!==s)throw new l("INVALID_ARGS",`Flag ${t} does not take a value.`);return{value:e.setValue,consumeNext:!1}}if("boolean"===e.type){if(void 0!==s)throw new l("INVALID_ARGS",`Flag ${t} does not take a value.`);return{value:!0,consumeNext:!1}}if("booleanOrString"===e.type){if(void 0!==s){if(0===s.trim().length)throw new l("INVALID_ARGS",`Flag ${t} requires a non-empty value when provided.`);return{value:s,consumeNext:!1}}return void 0===a||F(a)||!function(e){let t=e.trim();return!(!t||/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(t))&&!!(t.startsWith("./")||t.startsWith("../")||t.startsWith("~/")||t.startsWith("/")||t.includes("/")||t.includes("\\"))}(a)?{value:!0,consumeNext:!1}:{value:a,consumeNext:!0}}let i=s??a;if(void 0===i||void 0===s&&F(i))throw new l("INVALID_ARGS",`Flag ${t} requires a value.`);if("string"===e.type)return{value:i,consumeNext:void 0===s};if("enum"===e.type){if(!e.enumValues?.includes(i))throw new l("INVALID_ARGS",`Invalid ${D(t)}: ${i}`);return{value:i,consumeNext:void 0===s}}let r=Number(i);if(!Number.isFinite(r)||"number"==typeof e.min&&r<e.min||"number"==typeof e.max&&r>e.max)throw new l("INVALID_ARGS",`Invalid ${D(t)}: ${i}`);return{value:Math.floor(r),consumeNext:void 0===s}}(g,u,d,e[t+1]);m.consumeNext&&(t+=1),a[g.key]=m.value,n.push({key:g.key,token:u})}let c=v(i),u=new Set([...f,...c?.allowedFlags??[]]),d=n.filter(e=>!u.has(e.key));if(d.length>0){var g,m;let e=(g=i,m=d.map(e=>e.token),g?1===m.length?`Flag ${m[0]} is not supported for command ${g}.`:`Flags ${m.join(", ")} are not supported for command ${g}.`:1===m.length?`Flag ${m[0]} requires a command that supports it.`:`Flags ${m.join(", ")} require a command that supports them.`);if(s)throw new l("INVALID_ARGS",e);for(let t of(o.push(`${e} Enable AGENT_DEVICE_STRICT_FLAGS=1 to fail fast.`),d))delete a[t.key]}if(c?.defaults)for(let[e,t]of Object.entries(c.defaults))void 0===a[e]&&(a[e]=t);if("batch"===i&&1!=+!!a.steps+ +!!a.stepsFile)throw new l("INVALID_ARGS","batch requires exactly one step source: --steps or --steps-file.");return{command:i,positionals:r,flags:a,warnings:o}}(a);for(let e of u.warnings)process.stderr.write(`Warning: ${e}
|
|
14
|
+
`);u.flags.version&&(process.stdout.write(`${p()}
|
|
15
|
+
`),process.exit(0));let d="help"===u.command,g=u.flags.help;if(d||g){d&&u.positionals.length>1&&(I(new l("INVALID_ARGS","help accepts at most one command.")),process.exit(1));let e=d?u.positionals[0]:u.command;e||(process.stdout.write(`${$}
|
|
16
|
+
`),process.exit(0));let t=function(e){let t=v(e);if(!t)return null;let s=x(e,t),a=k(new Set(t.allowedFlags)),i=k(f),r=[];return a.length>0&&r.push(A("Command flags:",a)),r.push(A("Global flags:",i)),`agent-device ${s}
|
|
17
17
|
|
|
18
18
|
${t.description}
|
|
19
19
|
|
|
20
20
|
Usage:
|
|
21
21
|
agent-device ${s}
|
|
22
22
|
|
|
23
|
-
${
|
|
24
|
-
`}(e);t&&(process.stdout.write(t),process.exit(0)),
|
|
25
|
-
`),process.exit(1)}
|
|
26
|
-
`),process.exit(1));let{command:
|
|
27
|
-
`),
|
|
23
|
+
${r.join("\n\n")}
|
|
24
|
+
`}(e);t&&(process.stdout.write(t),process.exit(0)),I(new l("INVALID_ARGS",`Unknown command: ${e}`)),process.stdout.write(`${$}
|
|
25
|
+
`),process.exit(1)}u.command||(process.stdout.write(`${$}
|
|
26
|
+
`),process.exit(1));let{command:m,positionals:h,flags:w}=u,b=function(e){let{json:t,help:s,version:a,...i}=e;return i}(w),j=w.session??process.env.AGENT_DEVICE_SESSION??"default",E=w.verbose&&!w.json?function(){try{let t=e.join(o.homedir(),".agent-device","daemon.log"),s=0,a=!1,i=setInterval(()=>{if(!a&&r.existsSync(t))try{let e=r.statSync(t);if(e.size<s&&(s=0),e.size<=s)return;let a=r.openSync(t,"r");try{let t=Buffer.alloc(e.size-s);r.readSync(a,t,0,t.length,s),s=e.size,t.length>0&&process.stdout.write(t.toString("utf8"))}finally{r.closeSync(a)}}catch{}},200);return()=>{a=!0,clearInterval(i)}}catch{return null}}():null;try{if("batch"===m){let e,s,a;if(h.length>0)throw new l("INVALID_ARGS","batch does not accept positional arguments.");let o=function(e){let s="";if(e.steps)s=e.steps;else if(e.stepsFile)try{s=r.readFileSync(e.stepsFile,"utf8")}catch(s){let t=s instanceof Error?s.message:String(s);throw new l("INVALID_ARGS",`Failed to read --steps-file ${e.stepsFile}: ${t}`)}return t(s)}(w),p={...b,batchSteps:o};delete p.steps,delete p.stepsFile;let c=await i.sendToDaemon({session:j,command:"batch",positionals:h,flags:p});if(!c.ok)throw new l(c.error.code,c.error.message,c.error.details);w.json?L({success:!0,data:c.data??{}}):(n=c.data??{},e="number"==typeof n.total?n.total:0,s="number"==typeof n.executed?n.executed:0,a="number"==typeof n.totalDurationMs?n.totalDurationMs:void 0,process.stdout.write(`Batch completed: ${s}/${e} steps${void 0!==a?` in ${a}ms`:""}
|
|
27
|
+
`)),E&&E();return}if("session"===m){let e=h[0]??"list";if("list"!==e)throw new l("INVALID_ARGS","session only supports list");let t=await i.sendToDaemon({session:j,command:"session_list",positionals:[],flags:b});if(!t.ok)throw new l(t.error.code,t.error.message);w.json?L({success:!0,data:t.data??{}}):process.stdout.write(`${JSON.stringify(t.data??{},null,2)}
|
|
28
|
+
`),E&&E();return}let e=await i.sendToDaemon({session:j,command:m,positionals:h,flags:b});if(e.ok){if(w.json){L({success:!0,data:e.data??{}}),E&&E();return}if("snapshot"===m){process.stdout.write(function(e,t={}){let s=e.nodes,a=Array.isArray(s)?s:[],i=!!e.truncated,r="string"==typeof e.appName?e.appName:void 0,o="string"==typeof e.appBundleId?e.appBundleId:void 0,n=[];r&&n.push(`Page: ${r}`),o&&n.push(`App: ${o}`);let l=`Snapshot: ${a.length} nodes${i?" (truncated)":""}`,p=n.length>0?`${n.join("\n")}
|
|
28
29
|
`:"";if(0===a.length)return`${p}${l}
|
|
29
30
|
`;if(t.raw){let e=a.map(e=>JSON.stringify(e));return`${p}${l}
|
|
30
31
|
${e.join("\n")}
|
|
31
|
-
`}if(t.flatten){let e=a.map(e=>
|
|
32
|
+
`}if(t.flatten){let e=a.map(e=>N(e,0,!1));return`${p}${l}
|
|
32
33
|
${e.join("\n")}
|
|
33
|
-
`}let c=[],u=[];for(let e of a){let t=e.depth??0;for(;c.length>0&&t<=c[c.length-1];)c.pop();let s=e.label?.trim()||e.value?.trim()||e.identifier?.trim()||"",a="group"===
|
|
34
|
+
`}let c=[],u=[];for(let e of a){let t=e.depth??0;for(;c.length>0&&t<=c[c.length-1];)c.pop();let s=e.label?.trim()||e.value?.trim()||e.identifier?.trim()||"",a="group"===O(e.type??"Element")&&!s;a&&c.push(t);let i=a?t:Math.max(0,t-c.length);u.push(N(e,i,a))}return`${p}${l}
|
|
34
35
|
${u.join("\n")}
|
|
35
|
-
`}(e.data??{},{raw:
|
|
36
|
-
`),
|
|
37
|
-
`),
|
|
38
|
-
`),
|
|
39
|
-
`),
|
|
40
|
-
`),
|
|
41
|
-
`),
|
|
42
|
-
`),
|
|
43
|
-
`),
|
|
44
|
-
`),
|
|
45
|
-
`),
|
|
36
|
+
`}(e.data??{},{raw:w.snapshotRaw,flatten:w.snapshotInteractiveOnly})),E&&E();return}if("get"===m){let t=h[0];if("text"===t){let t=e.data?.text??"";process.stdout.write(`${t}
|
|
37
|
+
`),E&&E();return}if("attrs"===t){let t=e.data?.node??{};process.stdout.write(`${JSON.stringify(t,null,2)}
|
|
38
|
+
`),E&&E();return}}if("find"===m){let t=e.data;if("string"==typeof t?.text){process.stdout.write(`${t.text}
|
|
39
|
+
`),E&&E();return}if("boolean"==typeof t?.found){process.stdout.write(`Found: ${t.found}
|
|
40
|
+
`),E&&E();return}if(t?.node){process.stdout.write(`${JSON.stringify(t.node,null,2)}
|
|
41
|
+
`),E&&E();return}}if("is"===m){let t=e.data?.predicate??"assertion";process.stdout.write(`Passed: is ${t}
|
|
42
|
+
`),E&&E();return}if("boot"===m){let t=e.data?.platform??"unknown",s=e.data?.device??e.data?.id??"unknown";process.stdout.write(`Boot ready: ${s} (${t})
|
|
43
|
+
`),E&&E();return}if("click"===m||"press"===m){let t=e.data?.ref??"",s=e.data?.x,a=e.data?.y;t&&"number"==typeof s&&"number"==typeof a&&process.stdout.write(`Tapped @${t} (${s}, ${a})
|
|
44
|
+
`),E&&E();return}if(e.data&&"object"==typeof e.data){let t=e.data;if("devices"===m){let e=(Array.isArray(t.devices)?t.devices:[]).map(e=>{let t=e?.name??e?.id??"unknown",s=e?.platform??"unknown",a=e?.kind?` ${e.kind}`:"",i="boolean"==typeof e?.booted?` booted=${e.booted}`:"";return`${t} (${s}${a})${i}`});process.stdout.write(`${e.join("\n")}
|
|
45
|
+
`),E&&E();return}if("apps"===m){let e=(Array.isArray(t.apps)?t.apps:[]).map(e=>{if("string"==typeof e)return e;if(e&&"object"==typeof e){let t=e.bundleId??e.package,s=e.name??e.label;return s&&t?`${s} (${t})`:t?String(t):JSON.stringify(e)}return String(e)});process.stdout.write(`${e.join("\n")}
|
|
46
|
+
`),E&&E();return}if("appstate"===m){let e=t?.platform,s=t?.appBundleId,a=t?.appName,i=t?.source,r=t?.package,o=t?.activity;if("ios"===e){process.stdout.write(`Foreground app: ${a??s??"unknown"}
|
|
46
47
|
`),s&&process.stdout.write(`Bundle: ${s}
|
|
47
48
|
`),i&&process.stdout.write(`Source: ${i}
|
|
48
|
-
`),
|
|
49
|
-
`),
|
|
50
|
-
`),
|
|
49
|
+
`),E&&E();return}if("android"===e){process.stdout.write(`Foreground app: ${r??"unknown"}
|
|
50
|
+
`),o&&process.stdout.write(`Activity: ${o}
|
|
51
|
+
`),E&&E();return}}}E&&E();return}throw new l(e.error.code,e.error.message,e.error.details)}catch(a){let t=s(a);if("close"===m&&"COMMAND_FAILED"===(c=t).code&&(c.details?.kind==="daemon_startup_failed"||c.message.toLowerCase().includes("failed to start daemon")&&("string"==typeof c.details?.infoPath||"string"==typeof c.details?.lockPath))){w.json&&L({success:!0,data:{closed:"session",source:"no-daemon"}}),E&&E();return}if(w.json)L({success:!1,error:{code:t.code,message:t.message,details:t.details}});else if(I(t),w.verbose)try{let t=e.join(o.homedir(),".agent-device","daemon.log");if(r.existsSync(t)){let e=r.readFileSync(t,"utf8").split("\n"),s=e.slice(Math.max(0,e.length-200)).join("\n");s.trim().length>0&&process.stderr.write(`
|
|
51
52
|
[daemon log]
|
|
52
53
|
${s}
|
|
53
|
-
`)}}catch{}
|
|
54
|
+
`)}}catch{}E&&E(),process.exit(1)}}d(process.argv[1]??"").href===import.meta.url&&Z(process.argv.slice(2)).catch(e=>{I(s(e)),process.exit(1)}),Z(process.argv.slice(2));
|