agent-device 0.12.3 → 0.12.5
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/dist/src/152.js +1 -1
- package/dist/src/320.js +1 -1
- package/dist/src/57.js +1 -1
- package/dist/src/641.js +38 -0
- package/dist/src/818.js +1 -1
- package/dist/src/974.js +2 -2
- package/dist/src/backend.d.ts +205 -0
- package/dist/src/backend.js +1 -0
- package/dist/src/bin.js +63 -63
- package/dist/src/commands/index.d.ts +908 -0
- package/dist/src/commands/index.js +1 -0
- package/dist/src/contracts.d.ts +1 -1
- package/dist/src/daemon.js +15 -15
- package/dist/src/index.d.ts +898 -3
- package/dist/src/index.js +3 -3
- package/dist/src/io.d.ts +85 -0
- package/dist/src/io.js +1 -0
- package/dist/src/metro-companion.js +1 -1
- package/dist/src/metro.d.ts +10 -0
- package/dist/src/metro.js +1 -1
- package/dist/src/selectors.js +1 -1
- package/dist/src/testing/conformance.d.ts +416 -0
- package/dist/src/testing/conformance.js +1 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandExecution.swift +12 -3
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Models.swift +1 -0
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+ScreenRecorder.swift +24 -5
- package/ios-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift +2 -0
- package/ios-runner/AgentDeviceRunner/RecordingScripts/recording-resize.swift +182 -0
- package/ios-runner/RUNNER_PROTOCOL.md +1 -1
- package/package.json +17 -1
- package/skills/agent-device/references/bootstrap-install.md +13 -0
- package/skills/agent-device/references/remote-tenancy.md +15 -0
- package/skills/agent-device/references/verification.md +1 -0
- package/dist/src/155.js +0 -38
- package/dist/src/940.js +0 -1
package/dist/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import e from"node:net";import t from"node:http";import a from"node:https";import r from"node:fs";import
|
|
2
|
-
`;try{t.logPath&&r.appendFile(t.logPath,n,()=>{}),t.traceLogPath&&r.appendFile(t.traceLogPath,n,()=>{}),t.logPath||t.traceLogPath||process.stderr.write(n)}catch{}}async function S(e,t,a){let r=Date.now();try{let n=await t();return _({level:"info",phase:e,durationMs:Date.now()-r,data:a}),n}catch(t){throw _({level:"error",phase:e,durationMs:Date.now()-r,data:{...a??{},error:t instanceof Error?t.message:String(t)}}),t}}function M(e){let t,a=(t=(e??"").trim())?p(t):n.join(m("~"),".agent-device");return{baseDir:a,infoPath:n.join(a,"daemon.json"),lockPath:n.join(a,"daemon.lock"),logPath:n.join(a,"daemon.log"),sessionsDir:n.join(a,"sessions")}}let P="sha256";async function k(e){let{localPath:o,baseUrl:i,token:s}=e,l=r.statSync(o),c=l.isDirectory(),p=n.basename(o),m=c?"app-bundle":"file",f=i.endsWith("/")?i:`${i}/`,h=c?void 0:await E(o);if(h){let e=await D({normalizedBase:f,token:s,hash:h,filename:p,sizeBytes:l.size,artifactType:m});if(e)return e}let y=new URL("upload",f),w="https:"===y.protocol?a:t,v={"x-artifact-type":m,"x-artifact-filename":p,"transfer-encoding":"chunked"};return h&&(v["x-artifact-hash"]=h,v["x-artifact-hash-algorithm"]=P),s&&(v.authorization=`Bearer ${s}`,v["x-agent-device-token"]=s),new Promise((e,t)=>{let a=w.request({protocol:y.protocol,host:y.hostname,port:y.port,method:"POST",path:y.pathname+y.search,headers:v},a=>{let r="";a.setEncoding("utf8"),a.on("data",e=>{r+=e}),a.on("end",()=>{clearTimeout(i);try{let a=JSON.parse(r);if(!a.ok||!a.uploadId)return void t(new u("COMMAND_FAILED",`Upload failed: ${r}`));e(a.uploadId)}catch{t(new u("COMMAND_FAILED",`Invalid upload response: ${r}`))}})}),i=setTimeout(()=>{a.destroy(),t(new u("COMMAND_FAILED","Artifact upload timed out",{timeoutMs:3e5,hint:"The upload to the remote daemon exceeded the 5-minute timeout."}))},3e5);if(a.on("error",e=>{clearTimeout(i),t(new u("COMMAND_FAILED","Failed to upload artifact to remote daemon",{hint:"Verify the remote daemon is reachable and supports artifact uploads."},e))}),c){let e=d("tar",["cf","-","-C",n.dirname(o),n.basename(o)],{stdio:["ignore","pipe","pipe"]});e.stdout.pipe(a),e.on("error",e=>{a.destroy(),t(new u("COMMAND_FAILED","Failed to create tar archive for app bundle",{},e))}),e.on("close",e=>{0!==e&&(a.destroy(),t(new u("COMMAND_FAILED",`tar failed with exit code ${e}`)))})}else{let e=r.createReadStream(o);e.pipe(a),e.on("error",e=>{a.destroy(),t(new u("COMMAND_FAILED","Failed to read local artifact",{},e))})}})}async function D(e){var t;let a=new URL("upload/preflight",e.normalizedBase),r={"content-type":"application/json"};e.token&&(r.authorization=`Bearer ${e.token}`,r["x-agent-device-token"]=e.token);let n=await fetch(a,{method:"POST",headers:r,signal:AbortSignal.timeout(3e4),body:JSON.stringify({hash:e.hash,hashAlgorithm:P,fileName:e.filename,sizeBytes:e.sizeBytes,artifactType:e.artifactType})}).catch(()=>void 0);if(!n?.ok)return;let o=await n.json().catch(()=>void 0);return(t=o)&&"object"==typeof t&&!0===t.ok&&!0===t.cacheHit&&"string"==typeof t.uploadId?o.uploadId:void 0}async function E(e){let t=l(P);return await new Promise((a,n)=>{r.createReadStream(e).on("data",e=>t.update(e)).on("error",e=>{n(new u("COMMAND_FAILED","Failed to read local artifact",{},e))}).on("end",a)}),t.digest("hex")}let T=/(?:^|[^\w$.])(?:import|export)\s+(?:type\s+)?(?:[^'"`]*?\s+from\s+)?['"]([^'"]+)['"]/gm,N=/import\(\s*['"]([^'"]+)['"]\s*\)/gm,x=[".ts",".tsx",".js",".jsx",".mjs",".cjs"];function U(e,t,a){t.lastIndex=0;let r=null;for(;null!==(r=t.exec(e));){let e=r[1]?.trim();e?.startsWith(".")&&a.add(e)}}function R(e){try{return r.statSync(e).isFile()?e:null}catch{return null}}let O=eh(),C=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS){if(!e)return 15e3;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):15e3}(),L=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_ATTEMPTS){if(!e)return 2;let t=Number(e);return Number.isFinite(t)?Math.min(5,Math.max(1,Math.floor(t))):2}(),F=["xcodebuild .*AgentDeviceRunnerUITests/RunnerTests/testCommand","xcodebuild .*AgentDeviceRunner\\.env\\.session-","xcodebuild build-for-testing .*ios-runner/AgentDeviceRunner/AgentDeviceRunner\\.xcodeproj"],j=new e.BlockList;async function $(t){let a=t.meta?.requestId??A(),r=!!(t.meta?.debug||t.flags?.verbose),n=function(t){let a,r,n,o=t.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR,i=function(e){let t;if(e){try{t=new URL(e)}catch(t){throw new u("INVALID_ARGS","Invalid daemon base URL",{daemonBaseUrl:e},t instanceof Error?t:void 0)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new u("INVALID_ARGS","Daemon base URL must use http or https",{daemonBaseUrl:e});return t.toString().replace(/\/+$/,"")}}(t.flags?.daemonBaseUrl??process.env.AGENT_DEVICE_DAEMON_BASE_URL),s=t.flags?.daemonAuthToken??process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;var l=i,d=s;if(!(!l||"localhost"===(a=new URL(l).hostname.trim().toLowerCase().replace(/^\[(.*)\]$/,"$1"))||(e.isIPv4(a)?j.check(a,"ipv4"):!!e.isIPv6(a)&&j.check(a,"ipv6")))&&("string"!=typeof d||!(d.trim().length>0)))throw new u("INVALID_ARGS","Remote daemon base URL for non-loopback hosts requires daemon authentication",{daemonBaseUrl:l,hint:"Provide --daemon-auth-token or AGENT_DEVICE_DAEMON_AUTH_TOKEN when using a non-loopback remote daemon URL."});let c=t.flags?.daemonTransport??process.env.AGENT_DEVICE_DAEMON_TRANSPORT,p="auto"===(r=(c??"").trim().toLowerCase())?"auto":"socket"===r?"socket":"http"===r?"http":"auto";if(i&&"socket"===p)throw new u("INVALID_ARGS","Remote daemon base URL only supports HTTP transport. Remove --daemon-transport socket.",{daemonBaseUrl:i});let m="http"===(n=(t.flags?.daemonServerMode??process.env.AGENT_DEVICE_DAEMON_SERVER_MODE??("dual"===c?"dual":void 0)??"").trim().toLowerCase())?"http":"dual"===n?"dual":"socket";return{paths:M(o),transportPreference:p,serverMode:m,remoteBaseUrl:i,remoteAuthToken:s}}(t),o=function(e,t=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if("test"!==e)return eh(t)}(t.command),i=await S("daemon_startup",async()=>await V(n),{requestId:a,session:t.session}),s=await B(t,i),l={...t,positionals:s.positionals,flags:s.flags,token:i.token,meta:{...t.meta??{},requestId:a,debug:r,cwd:t.meta?.cwd,tenantId:t.meta?.tenantId??t.flags?.tenant,runId:t.meta?.runId??t.flags?.runId,leaseId:t.meta?.leaseId??t.flags?.leaseId,sessionIsolation:t.meta?.sessionIsolation??t.flags?.sessionIsolation,lockPolicy:t.meta?.lockPolicy,lockPlatform:t.meta?.lockPlatform,...s.uploadedArtifactId?{uploadedArtifactId:s.uploadedArtifactId}:{},...s.clientArtifactPaths?{clientArtifactPaths:s.clientArtifactPaths}:{},...s.installSource?{installSource:s.installSource}:{}}};return _({level:"info",phase:"daemon_request_prepare",data:{requestId:a,command:t.command,session:t.session}}),await S("daemon_request",async()=>await en(i,l,n.transportPreference,o),{requestId:a,command:t.command})}async function B(e,t){let a,o=[...e.positionals??[]],i=e.flags?{...e.flags}:void 0,s=e.meta?.installSource,l={};if(eu(t)){let r=function(e,t){if("screenshot"===e.command){let a=z(e,"path",".png");return t[0]?{field:"path",localPath:a,positionalIndex:0,positionalPath:G("screenshot",".png")}:{field:"path",localPath:a,positionalIndex:0,flagPath:G("screenshot",".png")}}if("record"===e.command&&"start"===(t[0]??"").toLowerCase()){let t=z(e,"outPath",".mp4",1);return{field:"outPath",localPath:t,positionalIndex:1,positionalPath:G("recording",n.extname(t)||".mp4")}}return null}(e,o);r&&(void 0!==r.positionalPath&&(o[r.positionalIndex]=r.positionalPath),void 0!==r.flagPath&&((i??={}).out=r.flagPath),l[r.field]=r.localPath);let d=await q(e,t);d&&(s=d.installSource,a=d.uploadedArtifactId??a)}if(!eu(t)||"install"!==e.command&&"reinstall"!==e.command||o.length<2)return{positionals:o,flags:i,installSource:s,uploadedArtifactId:a,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let d=o[1];if(d.startsWith("remote:"))return o[1]=d.slice(7),{positionals:o,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let c=n.isAbsolute(d)?d:n.resolve(e.meta?.cwd??process.cwd(),d);return r.existsSync(c)?{positionals:o,flags:i,installSource:s,uploadedArtifactId:a=await k({localPath:c,baseUrl:t.baseUrl,token:t.token}),...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}:{positionals:o,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}}async function q(e,t){let a=e.meta?.installSource;if("install_source"!==e.command||!a||"path"!==a.kind)return null;let o=a.path.trim();if(!o)return{installSource:a};if(o.startsWith("remote:"))return{installSource:{...a,path:o.slice(7)}};let i=n.isAbsolute(o)?o:n.resolve(e.meta?.cwd??process.cwd(),o);if(!r.existsSync(i))return{installSource:{...a,path:i}};let s=await k({localPath:i,baseUrl:t.baseUrl,token:t.token});return{installSource:{...a,path:i},uploadedArtifactId:s}}function z(e,t,a,r=0){let o=e.positionals?.[r]??e.flags?.out,i=`${"path"===t?"screenshot":"recording"}-${Date.now()}${a}`,s=o&&o.trim().length>0?o:i;return n.isAbsolute(s)?s:n.resolve(e.meta?.cwd??process.cwd(),s)}function G(e,t){let a=t.startsWith(".")?t:`.${t}`;return n.posix.join("/tmp",`agent-device-${e}-${Date.now()}-${Math.random().toString(36).slice(2,8)}${a}`)}async function V(e){let t;if(e.remoteBaseUrl){let t={transport:"http",token:e.remoteAuthToken??"",pid:0,baseUrl:e.remoteBaseUrl};if(await et(t,"http"))return t;throw new u("COMMAND_FAILED","Remote daemon is unavailable",{daemonBaseUrl:e.remoteBaseUrl,hint:"Verify AGENT_DEVICE_DAEMON_BASE_URL points to a reachable daemon with GET /health and POST /rpc."})}let a=Q(e.paths.infoPath),o=function(){try{let e=I();return JSON.parse(r.readFileSync(n.join(e,"package.json"),"utf8")).version??"0.0.0"}catch{return"0.0.0"}}(),i=function(e,t=I()){try{let a=n.resolve(t),o=[n.resolve(e)],i=new Set,l=[];for(;o.length>0;){let e=o.pop();if(!e||i.has(e))continue;i.add(e);let t=r.statSync(e);if(!t.isFile())continue;let s=n.relative(a,e)||e;l.push(`${s}:${t.size}:${Math.trunc(t.mtimeMs)}`);let d=r.readFileSync(e,"utf8");for(let t of function(e){let t=new Set;return U(e,T,t),U(e,N,t),[...t]}(d)){let a=function(e,t){let a=n.resolve(n.dirname(e),t),r=R(a);if(r)return r;for(let e of x){let t=R(`${a}${e}`);if(t)return t}for(let e of x){let t=R(n.join(a,`index${e}`));if(t)return t}return null}(e,t);a&&o.push(a)}}let d=l.sort().join("|"),c=s.createHash("sha1").update(d).digest("hex");return`graph:${l.length}:${c}`}catch{return"unknown"}}((t=er()).useSrc?t.srcPath:t.distPath,t.root),l=!!a&&await et(a,e.transportPreference);if(a&&a.version===o&&a.codeSignature===i&&l)return a;a&&(a.version!==o||a.codeSignature!==i||!l)&&(await W(a),ee(e.paths.infoPath)),function(e){let t=Y(e);if(!t.hasLock||t.hasInfo)return;let a=X(e.lockPath);if(!a)return ee(e.lockPath);w(a.pid,a.processStartTime)||ee(e.lockPath)}(e.paths);let d=0;for(let t=1;t<=L;t+=1){await ea(e);let a=await K(C,e);if(a)return a;if(await J(e.paths)){d+=1;continue}let r=Y(e.paths);if(!(t<L))break;if(!r.hasInfo&&!r.hasLock){await H(150);continue}}let c=Y(e.paths);throw new u("COMMAND_FAILED","Failed to start daemon",{kind:"daemon_startup_failed",infoPath:e.paths.infoPath,lockPath:e.paths.lockPath,startupTimeoutMs:C,startupAttempts:L,lockRecoveryCount:d,metadataState:c,hint:function(e,t=M(process.env.AGENT_DEVICE_STATE_DIR)){return e.hasLock&&!e.hasInfo?`Detected ${t.lockPath} without ${t.infoPath}. If no agent-device daemon process is running, delete ${t.lockPath} and retry.`:e.hasLock&&e.hasInfo?`Daemon metadata may be stale. If no agent-device daemon process is running, delete ${t.infoPath} and ${t.lockPath}, then retry.`:`Daemon metadata is missing or stale. Delete ${t.infoPath} if present and retry.`}(c,e.paths)})}async function K(e,t){let a=Date.now();for(;Date.now()-a<e;){let e=Q(t.paths.infoPath);if(e&&await et(e,t.transportPreference))return e;await new Promise(e=>setTimeout(e,100))}return null}async function H(e){await new Promise(t=>setTimeout(t,e))}async function J(e){let t=Y(e);if(!t.hasLock||t.hasInfo)return!1;let a=X(e.lockPath);return a&&w(a.pid,a.processStartTime)&&await v(a.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:a.processStartTime}),ee(e.lockPath),!0}async function W(e){await v(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}function Q(e){let t=Z(e);if(!t||"object"!=typeof t)return null;let a="string"==typeof t.token&&t.token.length>0?t.token:null;if(!a)return null;let r=Number.isInteger(t.port)&&Number(t.port)>0,n=Number.isInteger(t.httpPort)&&Number(t.httpPort)>0;if(!r&&!n)return null;let o=t.transport,i="string"==typeof t.version?t.version:void 0,s="string"==typeof t.codeSignature?t.codeSignature:void 0,l="string"==typeof t.processStartTime?t.processStartTime:void 0,d=Number.isInteger(t.pid)&&Number(t.pid)>0;return{token:a,port:r?Number(t.port):void 0,httpPort:n?Number(t.httpPort):void 0,transport:"socket"===o||"http"===o||"dual"===o?o:void 0,pid:d?Number(t.pid):0,version:i,codeSignature:s,processStartTime:l}}function X(e){let t=Z(e);return t&&"object"==typeof t&&Number.isInteger(t.pid)&&Number(t.pid)>0?{pid:Number(t.pid),processStartTime:"string"==typeof t.processStartTime?t.processStartTime:void 0,startedAt:"number"==typeof t.startedAt?t.startedAt:void 0}:null}j.addSubnet("127.0.0.0",8,"ipv4"),j.addAddress("::1","ipv6"),j.addSubnet("::ffff:127.0.0.0",104,"ipv6");function Y(e){return{hasInfo:r.existsSync(e.infoPath),hasLock:r.existsSync(e.lockPath)}}function Z(e){if(!r.existsSync(e))return null;try{return JSON.parse(r.readFileSync(e,"utf8"))}catch{return null}}function ee(e){try{r.existsSync(e)&&r.unlinkSync(e)}catch{}}async function et(r,n){var o;return"http"===eo(r,n)?await function(e){let r=e.baseUrl?ep(e.baseUrl,"health"):e.httpPort?`http://127.0.0.1:${e.httpPort}/health`:null;if(!r)return Promise.resolve(!1);let n=new URL(r),o="https:"===n.protocol?a:t,i=e.baseUrl?3e3:500;return new Promise(e=>{let t=o.request({protocol:n.protocol,host:n.hostname,port:n.port,path:n.pathname+n.search,method:"GET",timeout:i},t=>{t.resume(),e((t.statusCode??500)<500)});t.on("timeout",()=>{t.destroy(),e(!1)}),t.on("error",()=>{e(!1)}),t.end()})}(r):await ((o=r.port)?new Promise(t=>{let a=e.createConnection({host:"127.0.0.1",port:o},()=>{a.destroy(),t(!0)});a.on("error",()=>{t(!1)})}):Promise.resolve(!1))}async function ea(e){let t=er(),a=t.useSrc?["--experimental-strip-types",t.srcPath]:[t.distPath],r={...process.env,AGENT_DEVICE_STATE_DIR:e.paths.baseDir,AGENT_DEVICE_DAEMON_SERVER_MODE:e.serverMode};f(process.execPath,a,{env:r})}function er(){let e=I(),t=n.join(e,"dist","src","daemon.js"),a=n.join(e,"src","daemon.ts"),o=r.existsSync(t),i=r.existsSync(a);if(!o&&!i)throw new u("COMMAND_FAILED","Daemon entry not found",{distPath:t,srcPath:a});return{root:e,distPath:t,srcPath:a,useSrc:process.execArgv.includes("--experimental-strip-types")?i:!o&&i}}async function en(e,t,a,r){return"http"===eo(e,a)?await ec(e,t,r):await ed(e,t,r)}function eo(e,t){if(e.baseUrl){if("socket"===t)throw new u("COMMAND_FAILED","Remote daemon endpoint only supports HTTP transport",{daemonBaseUrl:e.baseUrl});return"http"}if("http"===t||"socket"===t){var a=e,r=t;if(ei(a,r))return r;throw new u("COMMAND_FAILED","http"===r?"Daemon HTTP endpoint is unavailable":"Daemon socket endpoint is unavailable")}let n=("socket"===e.transport||"dual"===e.transport?["socket","http"]:["http","socket"]).find(t=>ei(e,t));if(n)return n;throw new u("COMMAND_FAILED","Daemon metadata has no reachable transport")}function ei(e,t){return"http"===t?!!e.httpPort:!!e.port}function es(e,t,a,r,n,o){let i=n?{terminated:0}:function(){let e=0;try{for(let t of F){let a=h("pkill",["-f",t],{allowFailure:!0});0===a.exitCode&&(e+=1)}return{terminated:e}}catch(t){return{terminated:e,error:t instanceof Error?t.message:String(t)}}}(),s=n?{forcedKill:!1}:function(e,t){let a=!1;try{w(e.pid,e.processStartTime)&&(process.kill(e.pid,"SIGKILL"),a=!0)}catch{v(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}finally{ee(t.infoPath),ee(t.lockPath)}return{forcedKill:a}}(e,t);return _({level:"error",phase:"daemon_request_timeout",data:{timeoutMs:o,requestId:a,command:r,timedOutRunnerPidsTerminated:i.terminated,timedOutRunnerCleanupError:i.error,daemonPidReset:n?void 0:e.pid,daemonPidForceKilled:n?void 0:s.forcedKill,daemonBaseUrl:e.baseUrl}}),new u("COMMAND_FAILED","Daemon request timed out",{timeoutMs:o,requestId:a,hint:n?"Retry with --debug and verify the remote daemon URL, auth token, and remote host logs.":"Retry with --debug and check daemon diagnostics logs. Timed-out iOS runner xcodebuild processes were terminated when detected."})}function el(e,t,a){return _({level:"error",phase:"daemon_request_socket_error",data:{requestId:t,message:e instanceof Error?e.message:String(e)}}),new u("COMMAND_FAILED","Failed to communicate with daemon",{requestId:t,hint:a?"Retry command. If this persists, verify the remote daemon URL, auth token, and remote host reachability.":"Retry command. If this persists, clean stale daemon metadata and start a fresh session."},e instanceof Error?e:void 0)}async function ed(t,a,r){let n=t.port;if(!n)throw new u("COMMAND_FAILED","Daemon socket endpoint is unavailable");return new Promise((o,i)=>{let s=e.createConnection({host:"127.0.0.1",port:n},()=>{s.write(`${JSON.stringify(a)}
|
|
3
|
-
`)}),l=M(a.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),d="number"==typeof r?setTimeout(()=>{s.destroy(),i(es(t,l,a.meta?.requestId,a.command,!1,r))},r):void 0,c="";s.setEncoding("utf8"),s.on("data",e=>{let t=(c+=e).indexOf("\n");if(-1===t)return;let r=c.slice(0,t).trim();if(r)try{let e=JSON.parse(r);s.end(),d&&clearTimeout(d),o(e)}catch(e){d&&clearTimeout(d),i(new u("COMMAND_FAILED","Invalid daemon response",{requestId:a.meta?.requestId,line:r},e instanceof Error?e:void 0))}}),s.on("error",e=>{d&&clearTimeout(d),i(el(e,a.meta?.requestId,!1))})})}async function ec(e,r,n){var o,i,s;let l,d=e.baseUrl?new URL(ep(e.baseUrl,"rpc")):e.httpPort?new URL(`http://127.0.0.1:${e.httpPort}/rpc`):null;if(!d)throw new u("COMMAND_FAILED","Daemon HTTP endpoint is unavailable");let c=JSON.stringify((o=r,i={includeTokenParam:!e.baseUrl},l=o.meta?.requestId??A(),"lease_allocate"!==(s=o.command)&&"lease_heartbeat"!==s&&"lease_release"!==s?{jsonrpc:"2.0",id:l,method:"agent_device.command",params:o}:{jsonrpc:"2.0",id:l,method:function(e){switch(e){case"lease_allocate":return"agent_device.lease.allocate";case"lease_heartbeat":return"agent_device.lease.heartbeat";case"lease_release":return"agent_device.lease.release"}}(o.command),params:function(e,t,a){let r={...a.includeTokenParam?{token:e.token}:{},session:e.session,tenantId:e.meta?.tenantId,runId:e.meta?.runId};switch(t){case"lease_allocate":return{...r,ttlMs:e.meta?.leaseTtlMs,backend:e.meta?.leaseBackend};case"lease_heartbeat":return{...r,leaseId:e.meta?.leaseId,ttlMs:e.meta?.leaseTtlMs};case"lease_release":return{...r,leaseId:e.meta?.leaseId}}}(o,o.command,i)})),p={"content-type":"application/json","content-length":Buffer.byteLength(c)};return e.baseUrl&&e.token&&(p.authorization=`Bearer ${e.token}`,p["x-agent-device-token"]=e.token),await new Promise((o,i)=>{let s=M(r.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),l=("https:"===d.protocol?a:t).request({protocol:d.protocol,host:d.hostname,port:d.port,method:"POST",path:d.pathname+d.search,headers:p},t=>{let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{f&&clearTimeout(f);try{let t=JSON.parse(a);if(t.error){let e=t.error.data??{};i(new u(String(e.code??"COMMAND_FAILED"),String(e.message??t.error.message??"Daemon RPC request failed"),{..."object"==typeof e.details&&e.details?e.details:{},hint:"string"==typeof e.hint?e.hint:void 0,diagnosticId:"string"==typeof e.diagnosticId?e.diagnosticId:void 0,logPath:"string"==typeof e.logPath?e.logPath:void 0,requestId:r.meta?.requestId}));return}if(!t.result||"object"!=typeof t.result)return void i(new u("COMMAND_FAILED","Invalid daemon RPC response",{requestId:r.meta?.requestId}));if(e.baseUrl&&t.result.ok)return void em(e,r,t.result).then(o).catch(i);o(t.result)}catch(e){f&&clearTimeout(f),i(new u("COMMAND_FAILED","Invalid daemon response",{requestId:r.meta?.requestId,line:a},e instanceof Error?e:void 0))}})}),m=eu(e),f="number"==typeof n?setTimeout(()=>{l.destroy(),i(es(e,s,r.meta?.requestId,r.command,m,n))},n):void 0;l.on("error",e=>{f&&clearTimeout(f),i(el(e,r.meta?.requestId,m))}),l.write(c),l.end()})}function eu(e){return"string"==typeof e.baseUrl&&e.baseUrl.length>0}function ep(e,t){return new URL(t,e.endsWith("/")?e:`${e}/`).toString()}async function em(e,t,a){let r=Array.isArray(a.data?.artifacts)?a.data.artifacts:[];if(0===r.length||!e.baseUrl)return a;let o=a.data?{...a.data}:{},i=[];for(let a of r){if(!a||"object"!=typeof a||"string"!=typeof a.artifactId){i.push(a);continue}let r=function(e,t){if(e.localPath&&e.localPath.trim().length>0)return e.localPath;let a=e.fileName?.trim()||`${e.field}-${Date.now()}`;return n.resolve(t.meta?.cwd??process.cwd(),a)}(a,t);await ef({baseUrl:e.baseUrl,token:e.token,artifactId:a.artifactId,destinationPath:r,requestId:t.meta?.requestId}),o[a.field]=r,i.push({...a,localPath:r})}return o.artifacts=i,{ok:!0,data:o}}async function ef(e){var o,i;let s,l=new URL((o=e.baseUrl,i=e.artifactId,s=o.endsWith("/")?o:`${o}/`,new URL(`upload/${encodeURIComponent(i)}`,s).toString())),d="https:"===l.protocol?a:t;await r.promises.mkdir(n.dirname(e.destinationPath),{recursive:!0}),await new Promise((t,a)=>{let n=!1,o=e.timeoutMs??O,i=o=>{if(!n){if(n=!0,clearTimeout(c),o)return void r.promises.rm(e.destinationPath,{force:!0}).finally(()=>a(o));t()}},s=d.request({protocol:l.protocol,host:l.hostname,port:l.port,method:"GET",path:l.pathname+l.search,headers:e.token?{authorization:`Bearer ${e.token}`,"x-agent-device-token":e.token}:void 0},t=>{if((t.statusCode??500)>=400){let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{i(new u("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,statusCode:t.statusCode,requestId:e.requestId,body:a}))});return}let a=r.createWriteStream(e.destinationPath);a.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("aborted",()=>{i(new u("COMMAND_FAILED","Remote artifact download was interrupted",{artifactId:e.artifactId,requestId:e.requestId}))}),a.on("finish",()=>{a.close(()=>i())}),t.pipe(a)}),c=setTimeout(()=>{s.destroy(new u("COMMAND_FAILED","Remote artifact download timed out",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:o}))},o);s.on("error",t=>{t instanceof u?i(t):i(new u("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:o},t instanceof Error?t:void 0))}),s.end()})}function eh(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}let ey="clipboard",ew="wait";function ev(e){let t=e.appId??e.bundleId??e.packageName;return{session:e.session,appId:t,appBundleId:e.bundleId,package:e.packageName}}function eg(e,t,a){return{deviceId:t,deviceName:a,..."android"===e?{serial:t}:"ios"===e?{udid:t}:{}}}function eI(e,t,a,r){let n=a(e[t]);if(void 0===n)throw new u("COMMAND_FAILED",r,{response:e});return n}function eb(e,t){return eI(e,t,ek,`Daemon response is missing "${t}".`)}function eA(e,t){return ek(e[t])}function e_(e,t){var a;let r;return a=ek,null===(r=e[t])?null:a(r)}function eS(e,t){return eI(e,t,eE,`Daemon response has invalid "${t}".`)}function eM(e,t){return function(e){return"tv"===e||"mobile"===e||"desktop"===e?e:void 0}(e[t])??"mobile"}function eP(e,t){let a=e[t];if(!ex(a))return;let r="number"==typeof a.x?a.x:void 0,n="number"==typeof a.y?a.y:void 0,o="number"==typeof a.width?a.width:void 0,i="number"==typeof a.height?a.height:void 0;if(void 0!==r&&void 0!==n&&void 0!==o&&void 0!==i)return{x:r,y:n,width:o,height:i}}function ek(e){return"string"==typeof e&&e.length>0?e:void 0}function eD(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function eE(e){return"ios"===e||"macos"===e||"android"===e?e:void 0}function eT(e){return"simulator"===e||"emulator"===e||"device"===e?e:void 0}function eN(e){if(!ex(e))throw new u("COMMAND_FAILED","Daemon returned an unexpected response shape.",{value:e});return e}function ex(e){return"object"==typeof e&&null!==e}function eU(e){let t={};for(let[a,r]of Object.entries(e))void 0!==r&&(t[a]=r);return t}function eR(e,t){let a=eA(e,"bundleId"),r=eA(e,"package");return{app:eb(e,"app"),appPath:eb(e,"appPath"),platform:eS(e,"platform"),appId:a??r,bundleId:a,package:r,identifiers:ev({session:t,bundleId:a,packageName:r})}}function eO(e){let t=eN(e),a=eS(t,"platform"),r=eb(t,"id"),n=eb(t,"name");return{platform:a,target:eM(t,"target"),kind:eI(t,"kind",eT,'Daemon response has invalid "kind".'),id:r,name:n,booted:"boolean"==typeof t.booted?t.booted:void 0,identifiers:eg(a,r,n),ios:"ios"===a?{udid:r}:void 0,android:"android"===a?{serial:r}:void 0}}function eC(e){let t=eN(e),a=eS(t,"platform"),r=eb(t,"id"),n=eb(t,"name"),o=eM(t,"target"),i=eb(t,"device"),s={session:n,...eg(a,r,i)};return{name:n,createdAt:eI(t,"createdAt",eD,'Daemon response is missing numeric "createdAt".'),device:{platform:a,target:o,id:r,name:i,identifiers:s,ios:"ios"===a?{udid:r,simulatorSetPath:e_(t,"ios_simulator_device_set")}:void 0,android:"android"===a?{serial:r}:void 0},identifiers:s}}function eL(e){return e??"default"}function eF(e={},t={}){var a;let r,n=t.transport??$,o=async(t,a=[],r={})=>{var o,i;let s=(o=e,i=r,{...o,...i}),l=await n({session:eL(s.session),command:t,positionals:a,flags:eU({stateDir:s.stateDir,daemonBaseUrl:s.daemonBaseUrl,daemonAuthToken:s.daemonAuthToken,daemonTransport:s.daemonTransport,daemonServerMode:s.daemonServerMode,tenant:s.tenant,sessionIsolation:s.sessionIsolation,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,platform:s.platform,target:s.target,device:s.device,udid:s.udid,serial:s.serial,iosSimulatorDeviceSet:s.iosSimulatorDeviceSet,androidDeviceAllowlist:s.androidDeviceAllowlist,runtime:s.simulatorRuntimeId,boot:s.boot,reuseExisting:s.reuseExisting,surface:s.surface,activity:s.activity,relaunch:s.relaunch,shutdown:s.shutdown,saveScript:s.saveScript,noRecord:s.noRecord,backMode:s.backMode,metroHost:s.metroHost,metroPort:s.metroPort,bundleUrl:s.bundleUrl,launchUrl:s.launchUrl,snapshotInteractiveOnly:s.interactiveOnly,snapshotCompact:s.compact,snapshotDepth:s.depth,snapshotScope:s.scope,snapshotRaw:s.raw,screenshotFullscreen:s.screenshotFullscreen,overlayRefs:s.overlayRefs,appsFilter:s.appsFilter,out:s.out,count:s.count,fps:s.fps,hideTouches:s.hideTouches,intervalMs:s.intervalMs,delayMs:s.delayMs,holdMs:s.holdMs,jitterPx:s.jitterPx,pixels:s.pixels,doubleTap:s.doubleTap,clickButton:s.clickButton,pauseMs:s.pauseMs,pattern:s.pattern,headless:s.headless,restart:s.restart,replayUpdate:s.replayUpdate,failFast:s.failFast,timeoutMs:s.timeoutMs,retries:s.retries,artifactsDir:s.artifactsDir,reportJunit:s.reportJunit,findFirst:s.findFirst,findLast:s.findLast,networkInclude:s.networkInclude,batchOnError:s.batchOnError,batchMaxSteps:s.batchMaxSteps,batchSteps:s.batchSteps,verbose:s.debug}),runtime:s.runtime,meta:eU({requestId:s.requestId,cwd:s.cwd,debug:s.debug,lockPolicy:s.lockPolicy,lockPlatform:s.lockPlatform,tenantId:s.tenant,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,leaseTtlMs:s.leaseTtlMs,sessionIsolation:s.sessionIsolation,installSource:s.installSource,retainMaterializedPaths:s.retainMaterializedPaths,materializedPathRetentionMs:s.materializedPathRetentionMs,materializationId:s.materializationId})});return l.ok||function(e){throw new u(e.code,e.message,{...e.details??{},hint:e.hint,diagnosticId:e.diagnosticId,logPath:e.logPath})}(l.error),l.data??{}},i=async(e={})=>{let t=await o("session_list",[],e);return(Array.isArray(t.sessions)?t.sessions:[]).map(eC)},s=async(e,t=[],a={})=>await o(e,t,a),l=(t={})=>{var a,r;return eL((a=e,r=t,{...a,...r}).session)};return{command:(a=async e=>await o(e.command,e.positionals,e.options),r=async e=>await a(e),{wait:async e=>await r(function(e){if(1!==[void 0!==e.durationMs?"durationMs":void 0,void 0!==e.text?"text":void 0,void 0!==e.ref?"ref":void 0,void 0!==e.selector?"selector":void 0].filter(Boolean).length)throw new u("INVALID_ARGS","wait command requires exactly one of durationMs, text, ref, or selector.");if(void 0!==e.durationMs)return{command:ew,positionals:[String(e.durationMs)],options:e};let t=void 0!==e.timeoutMs?[String(e.timeoutMs)]:[];if(void 0!==e.text)return{command:ew,positionals:["text",e.text,...t],options:e};if(void 0!==e.ref)return{command:ew,positionals:[e.ref,...t],options:e};let a=e.selector;return function(e){if(!g(e))throw new u("INVALID_ARGS",`Invalid wait selector: ${e}`)}(a),{command:ew,positionals:[a,...t],options:e}}(e)),alert:async(e={})=>{var t;return await r({command:"alert",positionals:[(t=e).action??"get",...void 0!==t.timeoutMs?[String(t.timeoutMs)]:[]],options:t})},appState:async(e={})=>await r({command:"appstate",positionals:[],options:e}),back:async(e={})=>await r({command:"back",positionals:[],options:{...e,backMode:e.mode}}),home:async(e={})=>await r({command:"home",positionals:[],options:e}),rotate:async e=>await r({command:"rotate",positionals:[e.orientation],options:e}),appSwitcher:async(e={})=>await r({command:"app-switcher",positionals:[],options:e}),keyboard:async(e={})=>await r({command:"keyboard",positionals:e.action?[e.action]:[],options:e}),clipboard:async e=>{var t;return await r("read"===(t=e).action?{command:ey,positionals:["read"],options:t}:{command:ey,positionals:["write",t.text],options:t})}}),devices:{list:async(e={})=>{let t=await o("devices",[],e);return(Array.isArray(t.devices)?t.devices:[]).map(eO)},boot:async(e={})=>await s("boot",[],e)},sessions:{list:async(e={})=>await i(e),close:async(e={})=>{let t=l(e),a=(await o("close",[],e)).shutdown;return{session:t,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}}},simulators:{ensure:async e=>{let{runtime:t,...a}=e,r=await o("ensure-simulator",[],{...a,simulatorRuntimeId:t}),n=eb(r,"udid"),i=eb(r,"device");return{udid:n,device:i,runtime:eb(r,"runtime"),created:!0===r.created,booted:!0===r.booted,iosSimulatorDeviceSet:e_(r,"ios_simulator_device_set"),identifiers:{deviceId:n,deviceName:i,udid:n}}}},apps:{install:async e=>eR(await o("install",[e.app,e.appPath],e),l(e)),reinstall:async e=>eR(await o("reinstall",[e.app,e.appPath],e),l(e)),installFromSource:async e=>(function(e,t){let a=eA(e,"bundleId"),r=eA(e,"packageName"),n=a??r??eA(e,"appId"),o=eA(e,"launchTarget")??r??a??n;if(!o)throw new u("COMMAND_FAILED",'Daemon response is missing "launchTarget".',{response:e});return{appName:eA(e,"appName"),appId:n,bundleId:a,packageName:r,launchTarget:o,installablePath:eA(e,"installablePath"),archivePath:eA(e,"archivePath"),materializationId:eA(e,"materializationId"),materializationExpiresAt:eA(e,"materializationExpiresAt"),identifiers:ev({session:t,bundleId:a,packageName:r,appId:n})}})(await o("install_source",[],{...e,installSource:e.source,retainMaterializedPaths:e.retainPaths,materializedPathRetentionMs:e.retentionMs}),l(e)),list:async(e={})=>{let t=await o("apps",[],e);return Array.isArray(t.apps)?t.apps.filter(e=>"string"==typeof e):[]},open:async e=>{let t=l(e),a=e.app?e.url?[e.app,e.url]:[e.app]:[],r=await o("open",a,e),n=function(e){let t=e.platform,a=eA(e,"id"),r=eA(e,"device");if("ios"!==t&&"macos"!==t&&"android"!==t||!a||!r)return;let n=eM(e,"target"),o=eg(t,a,r);return{platform:t,target:n,id:a,name:r,identifiers:o,ios:"ios"===t?{udid:eA(e,"device_udid")??a,simulatorSetPath:e_(e,"ios_simulator_device_set")}:void 0,android:"android"===t?{serial:eA(e,"serial")??a}:void 0}}(r),i=eA(r,"appBundleId");return{session:t,appName:eA(r,"appName"),appBundleId:i,appId:i,startup:function(e){if(ex(e)&&"number"==typeof e.durationMs&&"string"==typeof e.measuredAt&&"string"==typeof e.method)return{durationMs:e.durationMs,measuredAt:e.measuredAt,method:e.method,appTarget:eA(e,"appTarget"),appBundleId:eA(e,"appBundleId")}}(r.startup),runtime:function(e){if(!ex(e))return;let t=e.platform,a=eA(e,"metroHost"),r="number"==typeof e.metroPort?e.metroPort:void 0;return{platform:"ios"===t||"android"===t?t:void 0,metroHost:a,metroPort:r,bundleUrl:eA(e,"bundleUrl"),launchUrl:eA(e,"launchUrl")}}(r.runtime),device:n,identifiers:{session:t,deviceId:n?.id,deviceName:n?.name,udid:n?.ios?.udid,serial:n?.android?.serial,appId:i,appBundleId:i}}},close:async(e={})=>{let t=l(e),a=(await o("close",e.app?[e.app]:[],e)).shutdown;return{session:t,closedApp:e.app,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}},push:async e=>{var t;return await s("push",[e.app,"string"==typeof(t=e.payload)?t:JSON.stringify(t)],e)},triggerEvent:async e=>{var t;return await s("trigger-app-event",[(t=e).event,...t.payload?[JSON.stringify(t.payload)]:[]],e)}},materializations:{release:async e=>{var t;return{released:!0===(t=await o("release_materialized_paths",[],{...e,materializationId:e.materializationId})).released,materializationId:eb(t,"materializationId"),identifiers:{}}}},leases:{allocate:async e=>eq(await o("lease_allocate",[],{...e,leaseId:void 0,leaseTtlMs:e.ttlMs})),heartbeat:async e=>eq(await o("lease_heartbeat",[],{...e,leaseTtlMs:e.ttlMs})),release:async e=>({released:!0===(await o("lease_release",[],e)).released})},metro:{prepare:async t=>await y({projectRoot:t.projectRoot??e.cwd,kind:t.kind,publicBaseUrl:t.publicBaseUrl,proxyBaseUrl:t.proxyBaseUrl,proxyBearerToken:t.bearerToken,launchUrl:t.launchUrl,companionProfileKey:t.companionProfileKey,companionConsumerKey:t.companionConsumerKey,metroPort:t.port,listenHost:t.listenHost,statusHost:t.statusHost,startupTimeoutMs:t.startupTimeoutMs,probeTimeoutMs:t.probeTimeoutMs,reuseExisting:t.reuseExisting,installDependenciesIfNeeded:t.installDependenciesIfNeeded,runtimeFilePath:t.runtimeFilePath,logPath:t.logPath})},capture:{snapshot:async(e={})=>{var t;let a=l(e),r=await o("snapshot",[],e),n=eA(r,"appBundleId"),i="object"==typeof r.visibility&&null!==r.visibility?r.visibility:void 0;return{nodes:Array.isArray(t=r.nodes)?t:[],truncated:!0===r.truncated,appName:eA(r,"appName"),appBundleId:n,...i?{visibility:i}:{},warnings:Array.isArray(r.warnings)?r.warnings.filter(e=>"string"==typeof e):void 0,identifiers:{session:a,appId:n,appBundleId:n}}},screenshot:async(e={})=>{let t=l(e),a=await o("screenshot",e.path?[e.path]:[],{...e,screenshotFullscreen:e.fullscreen});return{path:eb(a,"path"),overlayRefs:function(e){let t=e.overlayRefs;if(!Array.isArray(t))return;let a=[];for(let e of t){if(!ex(e))continue;let t=eA(e,"ref"),r=eP(e,"rect"),n=eP(e,"overlayRect"),o=function(e,t){let a=e[t];if(!ex(a))return;let r="number"==typeof a.x?a.x:void 0,n="number"==typeof a.y?a.y:void 0;if(void 0!==r&&void 0!==n)return{x:r,y:n}}(e,"center");t&&r&&n&&o&&a.push({ref:t,label:eA(e,"label"),rect:r,overlayRect:n,center:o})}return a}(a),identifiers:{session:t}}},diff:async e=>await s("diff",[e.kind],{...e,interactiveOnly:e.interactiveOnly,compact:e.compact,depth:e.depth,scope:e.scope,raw:e.raw})},interactions:{click:async e=>await s("click",ej(e),{...e,clickButton:e.button}),press:async e=>await s("press",ej(e),e),longPress:async e=>await s("longpress",[String(e.x),String(e.y),...eB(e.durationMs)],e),swipe:async e=>await s("swipe",[String(e.from.x),String(e.from.y),String(e.to.x),String(e.to.y),...eB(e.durationMs)],e),focus:async e=>await s("focus",[String(e.x),String(e.y)],e),type:async e=>await s("type",[e.text],e),fill:async e=>await s("fill",[...ej(e),e.text],e),scroll:async e=>await s("scroll",[e.direction,...eB(e.amount)],e),pinch:async e=>await s("pinch",[String(e.scale),...eB(e.x),...eB(e.y)],e),get:async e=>{var t;return await s("get",[e.format,...void 0!==(t=e).ref?[t.ref,...e$(t.label)]:[t.selector]],e)},is:async e=>await s("is",[e.predicate,e.selector,..."text"===e.predicate?[e.value]:[]],e),find:async e=>await s("find",function(e){let t=e.locator&&"any"!==e.locator?[e.locator,e.query]:[e.query];switch(e.action){case void 0:case"click":case"focus":case"exists":return e.action?[...t,e.action]:t;case"getText":return[...t,"get","text"];case"getAttrs":return[...t,"get","attrs"];case"wait":return[...t,"wait",...eB(e.timeoutMs)];case"fill":case"type":return[...t,e.action,e.value]}}(e),{...e,findFirst:e.first,findLast:e.last})},replay:{run:async e=>await s("replay",[e.path],{...e,replayUpdate:e.update}),test:async e=>await s("test",e.paths,{...e,replayUpdate:e.update})},batch:{run:async e=>await s("batch",[],{...e,batchSteps:e.steps,batchOnError:e.onError,batchMaxSteps:e.maxSteps})},observability:{perf:async(e={})=>await s("perf",[],e),logs:async(e={})=>{var t;return await s("logs",[(t=e).action??"path",...e$(t.message)],e)},network:async(e={})=>{var t;return await s("network",[...(t=e).action?[t.action]:[],...eB(t.limit)],{...e,networkInclude:e.include})}},recording:{record:async e=>await s("record",[e.action,...e$(e.path)],e),trace:async e=>await s("trace",[e.action,...e$(e.path)],e)},settings:{update:async e=>await s("settings",[e.setting,e.state,..."permission"in e?[e.permission]:[],..."mode"in e&&e.mode?[e.mode]:[]],e)}}}function ej(e){return void 0!==e.ref?[e.ref,...e$(e.label)]:void 0!==e.selector?[e.selector]:[String(e.x),String(e.y)]}function e$(e){return void 0===e?[]:[e]}function eB(e){return void 0===e?[]:[String(e)]}function eq(e){let t=e.lease;if(!t||"object"!=typeof t||Array.isArray(t))throw Error("Invalid lease response from daemon");return{leaseId:eb(t,"leaseId"),tenantId:eb(t,"tenantId"),runId:eb(t,"runId"),backend:eb(t,"backend"),createdAt:"number"==typeof t.createdAt?t.createdAt:void 0,heartbeatAt:"number"==typeof t.heartbeatAt?t.heartbeatAt:void 0,expiresAt:"number"==typeof t.expiresAt?t.expiresAt:void 0}}export{AppError}from"./152.js";export{centerOfRect}from"./57.js";export{eF as createAgentDeviceClient};
|
|
1
|
+
import e from"node:net";import t from"node:http";import a from"node:https";import r from"node:fs";import o from"node:path";import{fileURLToPath as n}from"node:url";import{AsyncLocalStorage as i}from"node:async_hooks";import s,{createHash as l,randomUUID as c}from"node:crypto";import d from"node:os";import{redactDiagnosticData as u,AppError as p}from"./152.js";import{bindCommands as m}from"./commands/index.js";import{hasBackendEscapeHatch as f,hasBackendCapability as h}from"./backend.js";import{resolveUserPath as y,expandUserHomePath as w}from"./267.js";import{runCmdSync as g,runCmd as v,runCmdDetached as I}from"./818.js";import{prepareMetroRuntime as b,isAgentDeviceDaemonProcess as A,stopProcessForTakeover as _}from"./974.js";import{tryParseSelectorChain as S}from"./selectors.js";function k(e){let t={backend:e.backend,artifacts:e.artifacts,sessions:e.sessions??M(),policy:e.policy??T(),diagnostics:e.diagnostics,clock:e.clock,signal:e.signal};return{...t,...m(t)}}function M(e=[]){let t=new Map(e.map(e=>[e.name,P(e)]));return{get:e=>D(t.get(e)),set:e=>{t.set(e.name,D(e))},delete:e=>{t.delete(e)},list:()=>Array.from(t.values(),P)}}function P(e){return D(e)}function D(e){if(e)return{...e,...e.snapshot?{snapshot:structuredClone(e.snapshot)}:{},...e.metadata?{metadata:function(e){try{return structuredClone(e)}catch{return{...e}}}(e.metadata)}:{}}}function E(e={}){return{allowLocalInputPaths:!0,allowLocalOutputPaths:!0,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}function T(e={}){return{allowLocalInputPaths:!1,allowLocalOutputPaths:!1,maxImagePixels:2e7,allowNamedBackendCapabilities:[],...e}}function N(e,t){if(!h(e.backend,t))throw new p("UNSUPPORTED_OPERATION",`Backend capability ${t} is not supported by this backend`,{capability:t});if(!e.policy.allowNamedBackendCapabilities.includes(t))throw new p("UNSUPPORTED_OPERATION",`Backend capability ${t} is not allowed by command policy`,{capability:t});if(!f(e.backend,t))throw new p("UNSUPPORTED_OPERATION",`Backend capability ${t} does not implement its escape hatch method`,{capability:t})}function x(){let e=o.dirname(n(import.meta.url)),t=e;for(let e=0;e<6;e+=1){let e=o.join(t,"package.json");if(r.existsSync(e))return t;t=o.dirname(t)}return e}let C=new i;function U(){return s.randomBytes(8).toString("hex")}function R(e){let t=C.getStore();if(!t)return;let a={ts:new Date().toISOString(),level:e.level??"info",phase:e.phase,session:t.session,requestId:t.requestId,command:t.command,durationMs:e.durationMs,data:e.data?u(e.data):void 0};if(t.events.push(a),!t.debug)return;let o=`[agent-device][diag] ${JSON.stringify(a)}
|
|
2
|
+
`;try{t.logPath&&r.appendFile(t.logPath,o,()=>{}),t.traceLogPath&&r.appendFile(t.traceLogPath,o,()=>{}),t.logPath||t.traceLogPath||process.stderr.write(o)}catch{}}async function O(e,t,a){let r=Date.now();try{let o=await t();return R({level:"info",phase:e,durationMs:Date.now()-r,data:a}),o}catch(t){throw R({level:"error",phase:e,durationMs:Date.now()-r,data:{...a??{},error:t instanceof Error?t.message:String(t)}}),t}}function L(e){let t,a=(t=(e??"").trim())?y(t):o.join(w("~"),".agent-device");return{baseDir:a,infoPath:o.join(a,"daemon.json"),lockPath:o.join(a,"daemon.lock"),logPath:o.join(a,"daemon.log"),sessionsDir:o.join(a,"sessions")}}let F="sha256";async function B(e){let t=await j(e.localPath,e.platform),a=e.baseUrl.endsWith("/")?e.baseUrl:`${e.baseUrl}/`;try{let r=await H({normalizedBase:a,token:e.token,artifact:t});if(r?.kind==="cache-hit")return r.uploadId;if(r?.kind==="direct-upload")try{return await K(t.payloadPath,r),await V({normalizedBase:a,token:e.token,uploadId:r.uploadId})}catch{}return await z({normalizedBase:a,token:e.token,artifact:t})}finally{t.cleanup()}}async function j(e,t){var a,n,i;let s,l=r.statSync(e),c=o.basename(e),d=l.isDirectory(),u=("ios"===(a=t)||"android"===a?a:void 0)??(n=e,i=l,s=n.toLowerCase(),i.isDirectory()&&s.endsWith(".app")||s.endsWith(".ipa")?"ios":s.endsWith(".apk")||s.endsWith(".aab")?"android":void 0),p=[];try{let t=d?await $(e,p):e,a=r.statSync(t);return{payloadPath:t,fileName:c,artifactType:d?"app-bundle":"file",platform:u,contentType:d?"application/gzip":"application/octet-stream",sha256:await J(t),sizeBytes:a.size,cleanup:()=>q(p)}}catch(e){throw q(p),e}}async function $(e,t){let a=r.mkdtempSync(o.join(d.tmpdir(),`agent-device-upload-${c()}-`));t.push(a);let n=o.join(a,`${o.basename(e)}.tar.gz`);return await v("tar",["czf",n,"-C",o.dirname(e),o.basename(e)]),n}function q(e){for(let t of e)r.rmSync(t,{recursive:!0,force:!0})}async function z(e){let{normalizedBase:t,token:a,artifact:r}=e,o=new URL("upload",t),n={"content-type":r.contentType,"x-artifact-type":r.artifactType,"x-artifact-filename":r.fileName,"x-artifact-hash":r.sha256,"x-artifact-hash-algorithm":F,"transfer-encoding":"chunked"};a&&(n.authorization=`Bearer ${a}`,n["x-agent-device-token"]=a);let i=await G({url:o,method:"POST",headers:n,payloadPath:r.payloadPath,timeoutMessage:"Artifact upload timed out",timeoutHint:"The upload to the remote daemon exceeded the 5-minute timeout.",errorMessage:"Failed to upload artifact to remote daemon",errorHint:"Verify the remote daemon is reachable and supports artifact uploads."});try{let e=JSON.parse(i.body);if(!e.ok||!e.uploadId)throw new p("COMMAND_FAILED",`Upload failed: ${i.body}`);return e.uploadId}catch(e){if(e instanceof p)throw e;throw new p("COMMAND_FAILED",`Invalid upload response: ${i.body}`)}}async function H(e){let t=new URL("upload/preflight",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({sha256:e.artifact.sha256,fileName:e.artifact.fileName,sizeBytes:e.artifact.sizeBytes,artifactType:e.artifact.artifactType,...e.artifact.platform?{platform:e.artifact.platform}:{},contentType:e.artifact.contentType})}).catch(()=>void 0);if(r?.ok)return function(e){var t;if(!e||"object"!=typeof e||!0!==e.ok||"string"!=typeof e.uploadId)return;if(!0===e.cacheHit)return{kind:"cache-hit",uploadId:e.uploadId};let a=e.upload;if(!a||"string"!=typeof a.url)return;let r=a.headers??{};if(!(!(t=r)||"object"!=typeof t||Array.isArray(t))&&Object.values(t).every(e=>"string"==typeof e))return{kind:"direct-upload",uploadId:e.uploadId,url:a.url,headers:r}}(await r.json().catch(()=>void 0))}async function K(e,t){let a=await G({url:new URL(t.url),method:"PUT",headers:t.headers,payloadPath:e,timeoutMessage:"Direct artifact upload timed out",timeoutHint:"The direct upload ticket did not accept the artifact within the timeout.",errorMessage:"Failed to upload artifact with direct upload ticket"});if(a.statusCode<200||a.statusCode>=300)throw new p("COMMAND_FAILED","Direct artifact upload failed",{statusCode:a.statusCode,statusMessage:a.statusMessage})}async function G(e){let o="https:"===e.url.protocol?a:t;return await new Promise((t,a)=>{let n=o.request({protocol:e.url.protocol,host:e.url.hostname,port:e.url.port,method:e.method,path:e.url.pathname+e.url.search,headers:e.headers},e=>{let a="";e.setEncoding("utf8"),e.on("data",e=>{a+=e}),e.on("end",()=>{clearTimeout(i),t({statusCode:e.statusCode??500,statusMessage:e.statusMessage,body:a})})}),i=setTimeout(()=>{n.destroy(),a(new p("COMMAND_FAILED",e.timeoutMessage,{timeoutMs:3e5,...e.timeoutHint?{hint:e.timeoutHint}:{}}))},3e5);n.on("error",t=>{clearTimeout(i),a(new p("COMMAND_FAILED",e.errorMessage,e.errorHint?{hint:e.errorHint}:{},t))}),n.on("close",()=>clearTimeout(i));let s=r.createReadStream(e.payloadPath);s.pipe(n),s.on("error",e=>{n.destroy(),a(new p("COMMAND_FAILED","Failed to read local artifact",{},e))})})}async function V(e){let t=new URL("upload/finalize",e.normalizedBase),a={"content-type":"application/json"};e.token&&(a.authorization=`Bearer ${e.token}`,a["x-agent-device-token"]=e.token);let r=await fetch(t,{method:"POST",headers:a,signal:AbortSignal.timeout(3e4),body:JSON.stringify({uploadId:e.uploadId})}).catch(e=>{throw new p("COMMAND_FAILED","Failed to finalize direct artifact upload",{},e)});if(!r.ok)throw new p("COMMAND_FAILED","Direct artifact upload finalize failed",{status:r.status,statusText:r.statusText});let o=await r.json().catch(()=>void 0);if(!o?.ok||!o.uploadId)throw new p("COMMAND_FAILED","Invalid upload finalize response");return o.uploadId}async function J(e){let t=l(F);return await new Promise((a,o)=>{r.createReadStream(e).on("data",e=>t.update(e)).on("error",e=>{o(new p("COMMAND_FAILED","Failed to read local artifact",{},e))}).on("end",a)}),t.digest("hex")}let W=/(?:^|[^\w$.])(?:import|export)\s+(?:type\s+)?(?:[^'"`]*?\s+from\s+)?['"]([^'"]+)['"]/gm,Q=/import\(\s*['"]([^'"]+)['"]\s*\)/gm,X=[".ts",".tsx",".js",".jsx",".mjs",".cjs"];function Y(e,t,a){t.lastIndex=0;let r=null;for(;null!==(r=t.exec(e));){let e=r[1]?.trim();e?.startsWith(".")&&a.add(e)}}function Z(e){try{return r.statSync(e).isFile()?e:null}catch{return null}}let ee=eU(),et=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS){if(!e)return 15e3;let t=Number(e);return Number.isFinite(t)?Math.max(1e3,Math.floor(t)):15e3}(),ea=function(e=process.env.AGENT_DEVICE_DAEMON_STARTUP_ATTEMPTS){if(!e)return 2;let t=Number(e);return Number.isFinite(t)?Math.min(5,Math.max(1,Math.floor(t))):2}(),er=["xcodebuild .*AgentDeviceRunnerUITests/RunnerTests/testCommand","xcodebuild .*AgentDeviceRunner\\.env\\.session-","xcodebuild build-for-testing .*ios-runner/AgentDeviceRunner/AgentDeviceRunner\\.xcodeproj"],eo=new e.BlockList;async function en(t){let a=t.meta?.requestId??U(),r=!!(t.meta?.debug||t.flags?.verbose),o=function(t){let a,r,o,n=t.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR,i=function(e){let t;if(e){try{t=new URL(e)}catch(t){throw new p("INVALID_ARGS","Invalid daemon base URL",{daemonBaseUrl:e},t instanceof Error?t:void 0)}if("http:"!==t.protocol&&"https:"!==t.protocol)throw new p("INVALID_ARGS","Daemon base URL must use http or https",{daemonBaseUrl:e});return t.toString().replace(/\/+$/,"")}}(t.flags?.daemonBaseUrl??process.env.AGENT_DEVICE_DAEMON_BASE_URL),s=t.flags?.daemonAuthToken??process.env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;var l=i,c=s;if(!(!l||"localhost"===(a=new URL(l).hostname.trim().toLowerCase().replace(/^\[(.*)\]$/,"$1"))||(e.isIPv4(a)?eo.check(a,"ipv4"):!!e.isIPv6(a)&&eo.check(a,"ipv6")))&&("string"!=typeof c||!(c.trim().length>0)))throw new p("INVALID_ARGS","Remote daemon base URL for non-loopback hosts requires daemon authentication",{daemonBaseUrl:l,hint:"Provide --daemon-auth-token or AGENT_DEVICE_DAEMON_AUTH_TOKEN when using a non-loopback remote daemon URL."});let d=t.flags?.daemonTransport??process.env.AGENT_DEVICE_DAEMON_TRANSPORT,u="auto"===(r=(d??"").trim().toLowerCase())?"auto":"socket"===r?"socket":"http"===r?"http":"auto";if(i&&"socket"===u)throw new p("INVALID_ARGS","Remote daemon base URL only supports HTTP transport. Remove --daemon-transport socket.",{daemonBaseUrl:i});let m="http"===(o=(t.flags?.daemonServerMode??process.env.AGENT_DEVICE_DAEMON_SERVER_MODE??("dual"===d?"dual":void 0)??"").trim().toLowerCase())?"http":"dual"===o?"dual":"socket";return{paths:L(n),transportPreference:u,serverMode:m,remoteBaseUrl:i,remoteAuthToken:s}}(t),n=function(e,t=process.env.AGENT_DEVICE_DAEMON_TIMEOUT_MS){if("test"!==e)return eU(t)}(t.command),i=await O("daemon_startup",async()=>await ed(o),{requestId:a,session:t.session}),s=await ei(t,i),l={...t,positionals:s.positionals,flags:s.flags,token:i.token,meta:{...t.meta??{},requestId:a,debug:r,cwd:t.meta?.cwd,tenantId:t.meta?.tenantId??t.flags?.tenant,runId:t.meta?.runId??t.flags?.runId,leaseId:t.meta?.leaseId??t.flags?.leaseId,sessionIsolation:t.meta?.sessionIsolation??t.flags?.sessionIsolation,lockPolicy:t.meta?.lockPolicy,lockPlatform:t.meta?.lockPlatform,...s.uploadedArtifactId?{uploadedArtifactId:s.uploadedArtifactId}:{},...s.clientArtifactPaths?{clientArtifactPaths:s.clientArtifactPaths}:{},...s.installSource?{installSource:s.installSource}:{}}};return R({level:"info",phase:"daemon_request_prepare",data:{requestId:a,command:t.command,session:t.session}}),await O("daemon_request",async()=>await e_(i,l,o.transportPreference,n),{requestId:a,command:t.command})}async function ei(e,t){let a,n=[...e.positionals??[]],i=e.flags?{...e.flags}:void 0,s=e.meta?.installSource,l={};if(eT(t)){let r=function(e,t){if("screenshot"===e.command){let a=el(e,"path",".png");return t[0]?{field:"path",localPath:a,positionalIndex:0,positionalPath:ec("screenshot",".png")}:{field:"path",localPath:a,positionalIndex:0,flagPath:ec("screenshot",".png")}}if("record"===e.command&&"start"===(t[0]??"").toLowerCase()){let t=el(e,"outPath",".mp4",1);return{field:"outPath",localPath:t,positionalIndex:1,positionalPath:ec("recording",o.extname(t)||".mp4")}}return null}(e,n);r&&(void 0!==r.positionalPath&&(n[r.positionalIndex]=r.positionalPath),void 0!==r.flagPath&&((i??={}).out=r.flagPath),l[r.field]=r.localPath);let c=await es(e,t);c&&(s=c.installSource,a=c.uploadedArtifactId??a)}if(!eT(t)||"install"!==e.command&&"reinstall"!==e.command||n.length<2)return{positionals:n,flags:i,installSource:s,uploadedArtifactId:a,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let c=n[1];if(c.startsWith("remote:"))return n[1]=c.slice(7),{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}};let d=o.isAbsolute(c)?c:o.resolve(e.meta?.cwd??process.cwd(),c);return r.existsSync(d)?{positionals:n,flags:i,installSource:s,uploadedArtifactId:a=await B({localPath:d,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform}),...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}:{positionals:n,flags:i,...Object.keys(l).length>0?{clientArtifactPaths:l}:{}}}async function es(e,t){let a=e.meta?.installSource;if("install_source"!==e.command||!a||"path"!==a.kind)return null;let n=a.path.trim();if(!n)return{installSource:a};if(n.startsWith("remote:"))return{installSource:{...a,path:n.slice(7)}};let i=o.isAbsolute(n)?n:o.resolve(e.meta?.cwd??process.cwd(),n);if(!r.existsSync(i))return{installSource:{...a,path:i}};let s=await B({localPath:i,baseUrl:t.baseUrl,token:t.token,platform:e.flags?.platform});return{installSource:{...a,path:i},uploadedArtifactId:s}}function el(e,t,a,r=0){let n=e.positionals?.[r]??e.flags?.out,i=`${"path"===t?"screenshot":"recording"}-${Date.now()}${a}`,s=n&&n.trim().length>0?n:i;return o.isAbsolute(s)?s:o.resolve(e.meta?.cwd??process.cwd(),s)}function ec(e,t){let a=t.startsWith(".")?t:`.${t}`;return o.posix.join("/tmp",`agent-device-${e}-${Date.now()}-${Math.random().toString(36).slice(2,8)}${a}`)}async function ed(e){let t;if(e.remoteBaseUrl){let t={transport:"http",token:e.remoteAuthToken??"",pid:0,baseUrl:e.remoteBaseUrl};if(await eI(t,"http"))return t;throw new p("COMMAND_FAILED","Remote daemon is unavailable",{daemonBaseUrl:e.remoteBaseUrl,hint:"Verify AGENT_DEVICE_DAEMON_BASE_URL points to a reachable daemon with GET /health and POST /rpc."})}let a=eh(e.paths.infoPath),n=function(){try{let e=x();return JSON.parse(r.readFileSync(o.join(e,"package.json"),"utf8")).version??"0.0.0"}catch{return"0.0.0"}}(),i=function(e,t=x()){try{let a=o.resolve(t),n=[o.resolve(e)],i=new Set,l=[];for(;n.length>0;){let e=n.pop();if(!e||i.has(e))continue;i.add(e);let t=r.statSync(e);if(!t.isFile())continue;let s=o.relative(a,e)||e;l.push(`${s}:${t.size}:${Math.trunc(t.mtimeMs)}`);let c=r.readFileSync(e,"utf8");for(let t of function(e){let t=new Set;return Y(e,W,t),Y(e,Q,t),[...t]}(c)){let a=function(e,t){let a=o.resolve(o.dirname(e),t),r=Z(a);if(r)return r;for(let e of X){let t=Z(`${a}${e}`);if(t)return t}for(let e of X){let t=Z(o.join(a,`index${e}`));if(t)return t}return null}(e,t);a&&n.push(a)}}let c=l.sort().join("|"),d=s.createHash("sha1").update(c).digest("hex");return`graph:${l.length}:${d}`}catch{return"unknown"}}((t=eA()).useSrc?t.srcPath:t.distPath,t.root),l=!!a&&await eI(a,e.transportPreference);if(a&&a.version===n&&a.codeSignature===i&&l)return a;a&&(a.version!==n||a.codeSignature!==i||!l)&&(await ef(a),ev(e.paths.infoPath)),function(e){let t=ew(e);if(!t.hasLock||t.hasInfo)return;let a=ey(e.lockPath);if(!a)return ev(e.lockPath);A(a.pid,a.processStartTime)||ev(e.lockPath)}(e.paths);let c=0;for(let t=1;t<=ea;t+=1){await eb(e);let a=await eu(et,e);if(a)return a;if(await em(e.paths)){c+=1;continue}let r=ew(e.paths);if(!(t<ea))break;if(!r.hasInfo&&!r.hasLock){await ep(150);continue}}let d=ew(e.paths);throw new p("COMMAND_FAILED","Failed to start daemon",{kind:"daemon_startup_failed",infoPath:e.paths.infoPath,lockPath:e.paths.lockPath,startupTimeoutMs:et,startupAttempts:ea,lockRecoveryCount:c,metadataState:d,hint:function(e,t=L(process.env.AGENT_DEVICE_STATE_DIR)){return e.hasLock&&!e.hasInfo?`Detected ${t.lockPath} without ${t.infoPath}. If no agent-device daemon process is running, delete ${t.lockPath} and retry.`:e.hasLock&&e.hasInfo?`Daemon metadata may be stale. If no agent-device daemon process is running, delete ${t.infoPath} and ${t.lockPath}, then retry.`:`Daemon metadata is missing or stale. Delete ${t.infoPath} if present and retry.`}(d,e.paths)})}async function eu(e,t){let a=Date.now();for(;Date.now()-a<e;){let e=eh(t.paths.infoPath);if(e&&await eI(e,t.transportPreference))return e;await new Promise(e=>setTimeout(e,100))}return null}async function ep(e){await new Promise(t=>setTimeout(t,e))}async function em(e){let t=ew(e);if(!t.hasLock||t.hasInfo)return!1;let a=ey(e.lockPath);return a&&A(a.pid,a.processStartTime)&&await _(a.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:a.processStartTime}),ev(e.lockPath),!0}async function ef(e){await _(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}function eh(e){let t=eg(e);if(!t||"object"!=typeof t)return null;let a="string"==typeof t.token&&t.token.length>0?t.token:null;if(!a)return null;let r=Number.isInteger(t.port)&&Number(t.port)>0,o=Number.isInteger(t.httpPort)&&Number(t.httpPort)>0;if(!r&&!o)return null;let n=t.transport,i="string"==typeof t.version?t.version:void 0,s="string"==typeof t.codeSignature?t.codeSignature:void 0,l="string"==typeof t.processStartTime?t.processStartTime:void 0,c=Number.isInteger(t.pid)&&Number(t.pid)>0;return{token:a,port:r?Number(t.port):void 0,httpPort:o?Number(t.httpPort):void 0,transport:"socket"===n||"http"===n||"dual"===n?n:void 0,pid:c?Number(t.pid):0,version:i,codeSignature:s,processStartTime:l}}function ey(e){let t=eg(e);return t&&"object"==typeof t&&Number.isInteger(t.pid)&&Number(t.pid)>0?{pid:Number(t.pid),processStartTime:"string"==typeof t.processStartTime?t.processStartTime:void 0,startedAt:"number"==typeof t.startedAt?t.startedAt:void 0}:null}eo.addSubnet("127.0.0.0",8,"ipv4"),eo.addAddress("::1","ipv6"),eo.addSubnet("::ffff:127.0.0.0",104,"ipv6");function ew(e){return{hasInfo:r.existsSync(e.infoPath),hasLock:r.existsSync(e.lockPath)}}function eg(e){if(!r.existsSync(e))return null;try{return JSON.parse(r.readFileSync(e,"utf8"))}catch{return null}}function ev(e){try{r.existsSync(e)&&r.unlinkSync(e)}catch{}}async function eI(r,o){var n;return"http"===eS(r,o)?await function(e){let r=e.baseUrl?eN(e.baseUrl,"health"):e.httpPort?`http://127.0.0.1:${e.httpPort}/health`:null;if(!r)return Promise.resolve(!1);let o=new URL(r),n="https:"===o.protocol?a:t,i=e.baseUrl?3e3:500;return new Promise(e=>{let t=n.request({protocol:o.protocol,host:o.hostname,port:o.port,path:o.pathname+o.search,method:"GET",timeout:i},t=>{t.resume(),e((t.statusCode??500)<500)});t.on("timeout",()=>{t.destroy(),e(!1)}),t.on("error",()=>{e(!1)}),t.end()})}(r):await ((n=r.port)?new Promise(t=>{let a=e.createConnection({host:"127.0.0.1",port:n},()=>{a.destroy(),t(!0)});a.on("error",()=>{t(!1)})}):Promise.resolve(!1))}async function eb(e){let t=eA(),a=t.useSrc?["--experimental-strip-types",t.srcPath]:[t.distPath],r={...process.env,AGENT_DEVICE_STATE_DIR:e.paths.baseDir,AGENT_DEVICE_DAEMON_SERVER_MODE:e.serverMode};I(process.execPath,a,{env:r})}function eA(){let e=x(),t=o.join(e,"dist","src","daemon.js"),a=o.join(e,"src","daemon.ts"),n=r.existsSync(t),i=r.existsSync(a);if(!n&&!i)throw new p("COMMAND_FAILED","Daemon entry not found",{distPath:t,srcPath:a});return{root:e,distPath:t,srcPath:a,useSrc:process.execArgv.includes("--experimental-strip-types")?i:!n&&i}}async function e_(e,t,a,r){return"http"===eS(e,a)?await eE(e,t,r):await eD(e,t,r)}function eS(e,t){if(e.baseUrl){if("socket"===t)throw new p("COMMAND_FAILED","Remote daemon endpoint only supports HTTP transport",{daemonBaseUrl:e.baseUrl});return"http"}if("http"===t||"socket"===t){var a=e,r=t;if(ek(a,r))return r;throw new p("COMMAND_FAILED","http"===r?"Daemon HTTP endpoint is unavailable":"Daemon socket endpoint is unavailable")}let o=("socket"===e.transport||"dual"===e.transport?["socket","http"]:["http","socket"]).find(t=>ek(e,t));if(o)return o;throw new p("COMMAND_FAILED","Daemon metadata has no reachable transport")}function ek(e,t){return"http"===t?!!e.httpPort:!!e.port}function eM(e,t,a,r,o,n){let i=o?{terminated:0}:function(){let e=0;try{for(let t of er){let a=g("pkill",["-f",t],{allowFailure:!0});0===a.exitCode&&(e+=1)}return{terminated:e}}catch(t){return{terminated:e,error:t instanceof Error?t.message:String(t)}}}(),s=o?{forcedKill:!1}:function(e,t){let a=!1;try{A(e.pid,e.processStartTime)&&(process.kill(e.pid,"SIGKILL"),a=!0)}catch{_(e.pid,{termTimeoutMs:3e3,killTimeoutMs:1e3,expectedStartTime:e.processStartTime})}finally{ev(t.infoPath),ev(t.lockPath)}return{forcedKill:a}}(e,t);return R({level:"error",phase:"daemon_request_timeout",data:{timeoutMs:n,requestId:a,command:r,timedOutRunnerPidsTerminated:i.terminated,timedOutRunnerCleanupError:i.error,daemonPidReset:o?void 0:e.pid,daemonPidForceKilled:o?void 0:s.forcedKill,daemonBaseUrl:e.baseUrl}}),new p("COMMAND_FAILED","Daemon request timed out",{timeoutMs:n,requestId:a,hint:o?"Retry with --debug and verify the remote daemon URL, auth token, and remote host logs.":"Retry with --debug and check daemon diagnostics logs. Timed-out iOS runner xcodebuild processes were terminated when detected."})}function eP(e,t,a){return R({level:"error",phase:"daemon_request_socket_error",data:{requestId:t,message:e instanceof Error?e.message:String(e)}}),new p("COMMAND_FAILED","Failed to communicate with daemon",{requestId:t,hint:a?"Retry command. If this persists, verify the remote daemon URL, auth token, and remote host reachability.":"Retry command. If this persists, clean stale daemon metadata and start a fresh session."},e instanceof Error?e:void 0)}async function eD(t,a,r){let o=t.port;if(!o)throw new p("COMMAND_FAILED","Daemon socket endpoint is unavailable");return new Promise((n,i)=>{let s=e.createConnection({host:"127.0.0.1",port:o},()=>{s.write(`${JSON.stringify(a)}
|
|
3
|
+
`)}),l=L(a.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),c="number"==typeof r?setTimeout(()=>{s.destroy(),i(eM(t,l,a.meta?.requestId,a.command,!1,r))},r):void 0,d="";s.setEncoding("utf8"),s.on("data",e=>{let t=(d+=e).indexOf("\n");if(-1===t)return;let r=d.slice(0,t).trim();if(r)try{let e=JSON.parse(r);s.end(),c&&clearTimeout(c),n(e)}catch(e){c&&clearTimeout(c),i(new p("COMMAND_FAILED","Invalid daemon response",{requestId:a.meta?.requestId,line:r},e instanceof Error?e:void 0))}}),s.on("error",e=>{c&&clearTimeout(c),i(eP(e,a.meta?.requestId,!1))})})}async function eE(e,r,o){var n,i,s;let l,c=e.baseUrl?new URL(eN(e.baseUrl,"rpc")):e.httpPort?new URL(`http://127.0.0.1:${e.httpPort}/rpc`):null;if(!c)throw new p("COMMAND_FAILED","Daemon HTTP endpoint is unavailable");let d=JSON.stringify((n=r,i={includeTokenParam:!e.baseUrl},l=n.meta?.requestId??U(),"lease_allocate"!==(s=n.command)&&"lease_heartbeat"!==s&&"lease_release"!==s?{jsonrpc:"2.0",id:l,method:"agent_device.command",params:n}:{jsonrpc:"2.0",id:l,method:function(e){switch(e){case"lease_allocate":return"agent_device.lease.allocate";case"lease_heartbeat":return"agent_device.lease.heartbeat";case"lease_release":return"agent_device.lease.release"}}(n.command),params:function(e,t,a){let r={...a.includeTokenParam?{token:e.token}:{},session:e.session,tenantId:e.meta?.tenantId,runId:e.meta?.runId};switch(t){case"lease_allocate":return{...r,ttlMs:e.meta?.leaseTtlMs,backend:e.meta?.leaseBackend};case"lease_heartbeat":return{...r,leaseId:e.meta?.leaseId,ttlMs:e.meta?.leaseTtlMs};case"lease_release":return{...r,leaseId:e.meta?.leaseId}}}(n,n.command,i)})),u={"content-type":"application/json","content-length":Buffer.byteLength(d)};return e.baseUrl&&e.token&&(u.authorization=`Bearer ${e.token}`,u["x-agent-device-token"]=e.token),await new Promise((n,i)=>{let s=L(r.flags?.stateDir??process.env.AGENT_DEVICE_STATE_DIR),l=("https:"===c.protocol?a:t).request({protocol:c.protocol,host:c.hostname,port:c.port,method:"POST",path:c.pathname+c.search,headers:u},t=>{let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{f&&clearTimeout(f);try{let t=JSON.parse(a);if(t.error){let e=t.error.data??{};i(new p(String(e.code??"COMMAND_FAILED"),String(e.message??t.error.message??"Daemon RPC request failed"),{..."object"==typeof e.details&&e.details?e.details:{},hint:"string"==typeof e.hint?e.hint:void 0,diagnosticId:"string"==typeof e.diagnosticId?e.diagnosticId:void 0,logPath:"string"==typeof e.logPath?e.logPath:void 0,requestId:r.meta?.requestId}));return}if(!t.result||"object"!=typeof t.result)return void i(new p("COMMAND_FAILED","Invalid daemon RPC response",{requestId:r.meta?.requestId}));if(e.baseUrl&&t.result.ok)return void ex(e,r,t.result).then(n).catch(i);n(t.result)}catch(e){f&&clearTimeout(f),i(new p("COMMAND_FAILED","Invalid daemon response",{requestId:r.meta?.requestId,line:a},e instanceof Error?e:void 0))}})}),m=eT(e),f="number"==typeof o?setTimeout(()=>{l.destroy(),i(eM(e,s,r.meta?.requestId,r.command,m,o))},o):void 0;l.on("error",e=>{f&&clearTimeout(f),i(eP(e,r.meta?.requestId,m))}),l.write(d),l.end()})}function eT(e){return"string"==typeof e.baseUrl&&e.baseUrl.length>0}function eN(e,t){return new URL(t,e.endsWith("/")?e:`${e}/`).toString()}async function ex(e,t,a){let r=Array.isArray(a.data?.artifacts)?a.data.artifacts:[];if(0===r.length||!e.baseUrl)return a;let n=a.data?{...a.data}:{},i=[];for(let a of r){if(!a||"object"!=typeof a||"string"!=typeof a.artifactId){i.push(a);continue}let r=function(e,t){if(e.localPath&&e.localPath.trim().length>0)return e.localPath;let a=e.fileName?.trim()||`${e.field}-${Date.now()}`;return o.resolve(t.meta?.cwd??process.cwd(),a)}(a,t);await eC({baseUrl:e.baseUrl,token:e.token,artifactId:a.artifactId,destinationPath:r,requestId:t.meta?.requestId}),n[a.field]=r,i.push({...a,localPath:r})}return n.artifacts=i,{ok:!0,data:n}}async function eC(e){var n,i;let s,l=new URL((n=e.baseUrl,i=e.artifactId,s=n.endsWith("/")?n:`${n}/`,new URL(`upload/${encodeURIComponent(i)}`,s).toString())),c="https:"===l.protocol?a:t;await r.promises.mkdir(o.dirname(e.destinationPath),{recursive:!0}),await new Promise((t,a)=>{let o=!1,n=e.timeoutMs??ee,i=n=>{if(!o){if(o=!0,clearTimeout(d),n)return void r.promises.rm(e.destinationPath,{force:!0}).finally(()=>a(n));t()}},s=c.request({protocol:l.protocol,host:l.hostname,port:l.port,method:"GET",path:l.pathname+l.search,headers:e.token?{authorization:`Bearer ${e.token}`,"x-agent-device-token":e.token}:void 0},t=>{if((t.statusCode??500)>=400){let a="";t.setEncoding("utf8"),t.on("data",e=>{a+=e}),t.on("end",()=>{i(new p("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,statusCode:t.statusCode,requestId:e.requestId,body:a}))});return}let a=r.createWriteStream(e.destinationPath);a.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("error",e=>{i(e instanceof Error?e:Error(String(e)))}),t.on("aborted",()=>{i(new p("COMMAND_FAILED","Remote artifact download was interrupted",{artifactId:e.artifactId,requestId:e.requestId}))}),a.on("finish",()=>{a.close(()=>i())}),t.pipe(a)}),d=setTimeout(()=>{s.destroy(new p("COMMAND_FAILED","Remote artifact download timed out",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n}))},n);s.on("error",t=>{t instanceof p?i(t):i(new p("COMMAND_FAILED","Failed to download remote artifact",{artifactId:e.artifactId,requestId:e.requestId,timeoutMs:n},t instanceof Error?t:void 0))}),s.end()})}function eU(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}let eR="clipboard",eO="wait";function eL(e){let t=e.appId??e.bundleId??e.packageName;return{session:e.session,appId:t,appBundleId:e.bundleId,package:e.packageName}}function eF(e,t,a){return{deviceId:t,deviceName:a,..."android"===e?{serial:t}:"ios"===e?{udid:t}:{}}}function eB(e,t,a,r){let o=a(e[t]);if(void 0===o)throw new p("COMMAND_FAILED",r,{response:e});return o}function ej(e,t){return eB(e,t,eG,`Daemon response is missing "${t}".`)}function e$(e,t){return eG(e[t])}function eq(e,t){var a;let r;return a=eG,null===(r=e[t])?null:a(r)}function ez(e,t){return eB(e,t,eJ,`Daemon response has invalid "${t}".`)}function eH(e,t){return function(e){return"tv"===e||"mobile"===e||"desktop"===e?e:void 0}(e[t])??"mobile"}function eK(e,t){let a=e[t];if(!eX(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0,n="number"==typeof a.width?a.width:void 0,i="number"==typeof a.height?a.height:void 0;if(void 0!==r&&void 0!==o&&void 0!==n&&void 0!==i)return{x:r,y:o,width:n,height:i}}function eG(e){return"string"==typeof e&&e.length>0?e:void 0}function eV(e){return"number"==typeof e&&Number.isFinite(e)?e:void 0}function eJ(e){return"ios"===e||"macos"===e||"android"===e?e:void 0}function eW(e){return"simulator"===e||"emulator"===e||"device"===e?e:void 0}function eQ(e){if(!eX(e))throw new p("COMMAND_FAILED","Daemon returned an unexpected response shape.",{value:e});return e}function eX(e){return"object"==typeof e&&null!==e}function eY(e){let t={};for(let[a,r]of Object.entries(e))void 0!==r&&(t[a]=r);return t}function eZ(e,t){let a=e$(e,"bundleId"),r=e$(e,"package");return{app:ej(e,"app"),appPath:ej(e,"appPath"),platform:ez(e,"platform"),appId:a??r,bundleId:a,package:r,identifiers:eL({session:t,bundleId:a,packageName:r})}}function e0(e){let t=eQ(e),a=ez(t,"platform"),r=ej(t,"id"),o=ej(t,"name");return{platform:a,target:eH(t,"target"),kind:eB(t,"kind",eW,'Daemon response has invalid "kind".'),id:r,name:o,booted:"boolean"==typeof t.booted?t.booted:void 0,identifiers:eF(a,r,o),ios:"ios"===a?{udid:r}:void 0,android:"android"===a?{serial:r}:void 0}}function e1(e){let t=eQ(e),a=ez(t,"platform"),r=ej(t,"id"),o=ej(t,"name"),n=eH(t,"target"),i=ej(t,"device"),s={session:o,...eF(a,r,i)};return{name:o,createdAt:eB(t,"createdAt",eV,'Daemon response is missing numeric "createdAt".'),device:{platform:a,target:n,id:r,name:i,identifiers:s,ios:"ios"===a?{udid:r,simulatorSetPath:eq(t,"ios_simulator_device_set")}:void 0,android:"android"===a?{serial:r}:void 0},identifiers:s}}function e2(e){return e??"default"}function e3(e={},t={}){var a;let r,o=t.transport??en,n=async(t,a=[],r={})=>{var n,i;let s=(n=e,i=r,{...n,...i}),l=await o({session:e2(s.session),command:t,positionals:a,flags:eY({stateDir:s.stateDir,daemonBaseUrl:s.daemonBaseUrl,daemonAuthToken:s.daemonAuthToken,daemonTransport:s.daemonTransport,daemonServerMode:s.daemonServerMode,tenant:s.tenant,sessionIsolation:s.sessionIsolation,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,platform:s.platform,target:s.target,device:s.device,udid:s.udid,serial:s.serial,iosSimulatorDeviceSet:s.iosSimulatorDeviceSet,androidDeviceAllowlist:s.androidDeviceAllowlist,runtime:s.simulatorRuntimeId,boot:s.boot,reuseExisting:s.reuseExisting,surface:s.surface,activity:s.activity,relaunch:s.relaunch,shutdown:s.shutdown,saveScript:s.saveScript,noRecord:s.noRecord,backMode:s.backMode,metroHost:s.metroHost,metroPort:s.metroPort,bundleUrl:s.bundleUrl,launchUrl:s.launchUrl,snapshotInteractiveOnly:s.interactiveOnly,snapshotCompact:s.compact,snapshotDepth:s.depth,snapshotScope:s.scope,snapshotRaw:s.raw,screenshotFullscreen:s.screenshotFullscreen,overlayRefs:s.overlayRefs,appsFilter:s.appsFilter,out:s.out,count:s.count,fps:s.fps,quality:s.quality,hideTouches:s.hideTouches,intervalMs:s.intervalMs,delayMs:s.delayMs,holdMs:s.holdMs,jitterPx:s.jitterPx,pixels:s.pixels,doubleTap:s.doubleTap,clickButton:s.clickButton,pauseMs:s.pauseMs,pattern:s.pattern,headless:s.headless,restart:s.restart,replayUpdate:s.replayUpdate,failFast:s.failFast,timeoutMs:s.timeoutMs,retries:s.retries,artifactsDir:s.artifactsDir,reportJunit:s.reportJunit,findFirst:s.findFirst,findLast:s.findLast,networkInclude:s.networkInclude,batchOnError:s.batchOnError,batchMaxSteps:s.batchMaxSteps,batchSteps:s.batchSteps,verbose:s.debug}),runtime:s.runtime,meta:eY({requestId:s.requestId,cwd:s.cwd,debug:s.debug,lockPolicy:s.lockPolicy,lockPlatform:s.lockPlatform,tenantId:s.tenant,runId:s.runId,leaseId:s.leaseId,leaseBackend:s.leaseBackend,leaseTtlMs:s.leaseTtlMs,sessionIsolation:s.sessionIsolation,installSource:s.installSource,retainMaterializedPaths:s.retainMaterializedPaths,materializedPathRetentionMs:s.materializedPathRetentionMs,materializationId:s.materializationId})});return l.ok||function(e){throw new p(e.code,e.message,{...e.details??{},hint:e.hint,diagnosticId:e.diagnosticId,logPath:e.logPath})}(l.error),l.data??{}},i=async(e={})=>{let t=await n("session_list",[],e);return(Array.isArray(t.sessions)?t.sessions:[]).map(e1)},s=async(e,t=[],a={})=>await n(e,t,a),l=(t={})=>{var a,r;return e2((a=e,r=t,{...a,...r}).session)};return{command:(a=async e=>await n(e.command,e.positionals,e.options),r=async e=>await a(e),{wait:async e=>await r(function(e){if(1!==[void 0!==e.durationMs?"durationMs":void 0,void 0!==e.text?"text":void 0,void 0!==e.ref?"ref":void 0,void 0!==e.selector?"selector":void 0].filter(Boolean).length)throw new p("INVALID_ARGS","wait command requires exactly one of durationMs, text, ref, or selector.");if(void 0!==e.durationMs)return{command:eO,positionals:[String(e.durationMs)],options:e};let t=void 0!==e.timeoutMs?[String(e.timeoutMs)]:[];if(void 0!==e.text)return{command:eO,positionals:["text",e.text,...t],options:e};if(void 0!==e.ref)return{command:eO,positionals:[e.ref,...t],options:e};let a=e.selector;return function(e){if(!S(e))throw new p("INVALID_ARGS",`Invalid wait selector: ${e}`)}(a),{command:eO,positionals:[a,...t],options:e}}(e)),alert:async(e={})=>{var t;return await r({command:"alert",positionals:[(t=e).action??"get",...void 0!==t.timeoutMs?[String(t.timeoutMs)]:[]],options:t})},appState:async(e={})=>await r({command:"appstate",positionals:[],options:e}),back:async(e={})=>await r({command:"back",positionals:[],options:{...e,backMode:e.mode}}),home:async(e={})=>await r({command:"home",positionals:[],options:e}),rotate:async e=>await r({command:"rotate",positionals:[e.orientation],options:e}),appSwitcher:async(e={})=>await r({command:"app-switcher",positionals:[],options:e}),keyboard:async(e={})=>await r({command:"keyboard",positionals:e.action?[e.action]:[],options:e}),clipboard:async e=>{var t;return await r("read"===(t=e).action?{command:eR,positionals:["read"],options:t}:{command:eR,positionals:["write",t.text],options:t})}}),devices:{list:async(e={})=>{let t=await n("devices",[],e);return(Array.isArray(t.devices)?t.devices:[]).map(e0)},boot:async(e={})=>await s("boot",[],e)},sessions:{list:async(e={})=>await i(e),close:async(e={})=>{let t=l(e),a=(await n("close",[],e)).shutdown;return{session:t,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}}},simulators:{ensure:async e=>{let{runtime:t,...a}=e,r=await n("ensure-simulator",[],{...a,simulatorRuntimeId:t}),o=ej(r,"udid"),i=ej(r,"device");return{udid:o,device:i,runtime:ej(r,"runtime"),created:!0===r.created,booted:!0===r.booted,iosSimulatorDeviceSet:eq(r,"ios_simulator_device_set"),identifiers:{deviceId:o,deviceName:i,udid:o}}}},apps:{install:async e=>eZ(await n("install",[e.app,e.appPath],e),l(e)),reinstall:async e=>eZ(await n("reinstall",[e.app,e.appPath],e),l(e)),installFromSource:async e=>(function(e,t){let a=e$(e,"bundleId"),r=e$(e,"packageName"),o=a??r??e$(e,"appId"),n=e$(e,"launchTarget")??r??a??o;if(!n)throw new p("COMMAND_FAILED",'Daemon response is missing "launchTarget".',{response:e});return{appName:e$(e,"appName"),appId:o,bundleId:a,packageName:r,launchTarget:n,installablePath:e$(e,"installablePath"),archivePath:e$(e,"archivePath"),materializationId:e$(e,"materializationId"),materializationExpiresAt:e$(e,"materializationExpiresAt"),identifiers:eL({session:t,bundleId:a,packageName:r,appId:o})}})(await n("install_source",[],{...e,installSource:e.source,retainMaterializedPaths:e.retainPaths,materializedPathRetentionMs:e.retentionMs}),l(e)),list:async(e={})=>{let t=await n("apps",[],e);return Array.isArray(t.apps)?t.apps.filter(e=>"string"==typeof e):[]},open:async e=>{let t=l(e),a=e.app?e.url?[e.app,e.url]:[e.app]:[],r=await n("open",a,e),o=function(e){let t=e.platform,a=e$(e,"id"),r=e$(e,"device");if("ios"!==t&&"macos"!==t&&"android"!==t||!a||!r)return;let o=eH(e,"target"),n=eF(t,a,r);return{platform:t,target:o,id:a,name:r,identifiers:n,ios:"ios"===t?{udid:e$(e,"device_udid")??a,simulatorSetPath:eq(e,"ios_simulator_device_set")}:void 0,android:"android"===t?{serial:e$(e,"serial")??a}:void 0}}(r),i=e$(r,"appBundleId");return{session:t,appName:e$(r,"appName"),appBundleId:i,appId:i,startup:function(e){if(eX(e)&&"number"==typeof e.durationMs&&"string"==typeof e.measuredAt&&"string"==typeof e.method)return{durationMs:e.durationMs,measuredAt:e.measuredAt,method:e.method,appTarget:e$(e,"appTarget"),appBundleId:e$(e,"appBundleId")}}(r.startup),runtime:function(e){if(!eX(e))return;let t=e.platform,a=e$(e,"metroHost"),r="number"==typeof e.metroPort?e.metroPort:void 0;return{platform:"ios"===t||"android"===t?t:void 0,metroHost:a,metroPort:r,bundleUrl:e$(e,"bundleUrl"),launchUrl:e$(e,"launchUrl")}}(r.runtime),device:o,identifiers:{session:t,deviceId:o?.id,deviceName:o?.name,udid:o?.ios?.udid,serial:o?.android?.serial,appId:i,appBundleId:i}}},close:async(e={})=>{let t=l(e),a=(await n("close",e.app?[e.app]:[],e)).shutdown;return{session:t,closedApp:e.app,shutdown:"object"==typeof a&&null!==a?a:void 0,identifiers:{session:t}}},push:async e=>{var t;return await s("push",[e.app,"string"==typeof(t=e.payload)?t:JSON.stringify(t)],e)},triggerEvent:async e=>{var t;return await s("trigger-app-event",[(t=e).event,...t.payload?[JSON.stringify(t.payload)]:[]],e)}},materializations:{release:async e=>{var t;return{released:!0===(t=await n("release_materialized_paths",[],{...e,materializationId:e.materializationId})).released,materializationId:ej(t,"materializationId"),identifiers:{}}}},leases:{allocate:async e=>e7(await n("lease_allocate",[],{...e,leaseId:void 0,leaseTtlMs:e.ttlMs})),heartbeat:async e=>e7(await n("lease_heartbeat",[],{...e,leaseTtlMs:e.ttlMs})),release:async e=>({released:!0===(await n("lease_release",[],e)).released})},metro:{prepare:async t=>await b({projectRoot:t.projectRoot??e.cwd,kind:t.kind,publicBaseUrl:t.publicBaseUrl,proxyBaseUrl:t.proxyBaseUrl,proxyBearerToken:t.bearerToken,bridgeScope:t.bridgeScope,launchUrl:t.launchUrl,companionProfileKey:t.companionProfileKey,companionConsumerKey:t.companionConsumerKey,metroPort:t.port,listenHost:t.listenHost,statusHost:t.statusHost,startupTimeoutMs:t.startupTimeoutMs,probeTimeoutMs:t.probeTimeoutMs,reuseExisting:t.reuseExisting,installDependenciesIfNeeded:t.installDependenciesIfNeeded,runtimeFilePath:t.runtimeFilePath,logPath:t.logPath})},capture:{snapshot:async(e={})=>{var t;let a=l(e),r=await n("snapshot",[],e),o=e$(r,"appBundleId"),i="object"==typeof r.visibility&&null!==r.visibility?r.visibility:void 0;return{nodes:Array.isArray(t=r.nodes)?t:[],truncated:!0===r.truncated,appName:e$(r,"appName"),appBundleId:o,...i?{visibility:i}:{},warnings:Array.isArray(r.warnings)?r.warnings.filter(e=>"string"==typeof e):void 0,identifiers:{session:a,appId:o,appBundleId:o}}},screenshot:async(e={})=>{let t=l(e),a=await n("screenshot",e.path?[e.path]:[],{...e,screenshotFullscreen:e.fullscreen});return{path:ej(a,"path"),overlayRefs:function(e){let t=e.overlayRefs;if(!Array.isArray(t))return;let a=[];for(let e of t){if(!eX(e))continue;let t=e$(e,"ref"),r=eK(e,"rect"),o=eK(e,"overlayRect"),n=function(e,t){let a=e[t];if(!eX(a))return;let r="number"==typeof a.x?a.x:void 0,o="number"==typeof a.y?a.y:void 0;if(void 0!==r&&void 0!==o)return{x:r,y:o}}(e,"center");t&&r&&o&&n&&a.push({ref:t,label:e$(e,"label"),rect:r,overlayRect:o,center:n})}return a}(a),identifiers:{session:t}}},diff:async e=>await s("diff",[e.kind],{...e,interactiveOnly:e.interactiveOnly,compact:e.compact,depth:e.depth,scope:e.scope,raw:e.raw})},interactions:{click:async e=>await s("click",e5(e),{...e,clickButton:e.button}),press:async e=>await s("press",e5(e),e),longPress:async e=>await s("longpress",[String(e.x),String(e.y),...e6(e.durationMs)],e),swipe:async e=>await s("swipe",[String(e.from.x),String(e.from.y),String(e.to.x),String(e.to.y),...e6(e.durationMs)],e),focus:async e=>await s("focus",[String(e.x),String(e.y)],e),type:async e=>await s("type",[e.text],e),fill:async e=>await s("fill",[...e5(e),e.text],e),scroll:async e=>await s("scroll",[e.direction,...e6(e.amount)],e),pinch:async e=>await s("pinch",[String(e.scale),...e6(e.x),...e6(e.y)],e),get:async e=>{var t;return await s("get",[e.format,...void 0!==(t=e).ref?[t.ref,...e8(t.label)]:[t.selector]],e)},is:async e=>await s("is",[e.predicate,e.selector,..."text"===e.predicate?[e.value]:[]],e),find:async e=>await s("find",function(e){let t=e.locator&&"any"!==e.locator?[e.locator,e.query]:[e.query];switch(e.action){case void 0:case"click":case"focus":case"exists":return e.action?[...t,e.action]:t;case"getText":return[...t,"get","text"];case"getAttrs":return[...t,"get","attrs"];case"wait":return[...t,"wait",...e6(e.timeoutMs)];case"fill":case"type":return[...t,e.action,e.value]}}(e),{...e,findFirst:e.first,findLast:e.last})},replay:{run:async e=>await s("replay",[e.path],{...e,replayUpdate:e.update}),test:async e=>await s("test",e.paths,{...e,replayUpdate:e.update})},batch:{run:async e=>await s("batch",[],{...e,batchSteps:e.steps,batchOnError:e.onError,batchMaxSteps:e.maxSteps})},observability:{perf:async(e={})=>await s("perf",[],e),logs:async(e={})=>{var t;return await s("logs",[(t=e).action??"path",...e8(t.message)],e)},network:async(e={})=>{var t;return await s("network",[...(t=e).action?[t.action]:[],...e6(t.limit)],{...e,networkInclude:e.include})}},recording:{record:async e=>await s("record",[e.action,...e8(e.path)],e),trace:async e=>await s("trace",[e.action,...e8(e.path)],e)},settings:{update:async e=>await s("settings",[e.setting,e.state,..."permission"in e?[e.permission]:[],..."mode"in e&&e.mode?[e.mode]:[]],e)}}}function e5(e){return void 0!==e.ref?[e.ref,...e8(e.label)]:void 0!==e.selector?[e.selector]:[String(e.x),String(e.y)]}function e8(e){return void 0===e?[]:[e]}function e6(e){return void 0===e?[]:[String(e)]}function e7(e){let t=e.lease;if(!t||"object"!=typeof t||Array.isArray(t))throw Error("Invalid lease response from daemon");return{leaseId:ej(t,"leaseId"),tenantId:ej(t,"tenantId"),runId:ej(t,"runId"),backend:ej(t,"backend"),createdAt:"number"==typeof t.createdAt?t.createdAt:void 0,heartbeatAt:"number"==typeof t.heartbeatAt?t.heartbeatAt:void 0,expiresAt:"number"==typeof t.expiresAt?t.expiresAt:void 0}}export{AppError,isAgentDeviceError,normalizeAgentDeviceError}from"./152.js";export{centerOfRect}from"./57.js";export{commandCatalog,commands,createCommandRouter,ref,selector}from"./commands/index.js";export{createLocalArtifactAdapter}from"./io.js";export{N as assertBackendCapabilityAllowed,k as createAgentDevice,e3 as createAgentDeviceClient,M as createMemorySessionStore,E as localCommandPolicy,T as restrictedCommandPolicy};
|
package/dist/src/io.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export declare type ArtifactAdapter = {
|
|
2
|
+
resolveInput(ref: FileInputRef, options: ResolveInputOptions): Promise<ResolvedInputFile>;
|
|
3
|
+
reserveOutput(ref: FileOutputRef | undefined, options: ReserveOutputOptions): Promise<ReservedOutputFile>;
|
|
4
|
+
createTempFile(options: CreateTempFileOptions): Promise<TemporaryFile>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export declare type ArtifactDescriptor = {
|
|
8
|
+
kind: 'localPath';
|
|
9
|
+
field: string;
|
|
10
|
+
path: string;
|
|
11
|
+
fileName?: string;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'artifact';
|
|
15
|
+
field: string;
|
|
16
|
+
artifactId: string;
|
|
17
|
+
fileName?: string;
|
|
18
|
+
url?: string;
|
|
19
|
+
clientPath?: string;
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export declare function createLocalArtifactAdapter(options?: LocalArtifactAdapterOptions): ArtifactAdapter;
|
|
24
|
+
|
|
25
|
+
export declare type CreateTempFileOptions = {
|
|
26
|
+
prefix: string;
|
|
27
|
+
ext: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export declare type FileInputRef = {
|
|
31
|
+
kind: 'path';
|
|
32
|
+
path: string;
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'uploadedArtifact';
|
|
35
|
+
id: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export declare type FileOutputRef = {
|
|
39
|
+
kind: 'path';
|
|
40
|
+
path: string;
|
|
41
|
+
} | {
|
|
42
|
+
kind: 'downloadableArtifact';
|
|
43
|
+
clientPath?: string;
|
|
44
|
+
fileName?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export declare type LocalArtifactAdapterOptions = {
|
|
48
|
+
cwd?: string;
|
|
49
|
+
tempDir?: string;
|
|
50
|
+
rootDir?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export declare type OutputVisibility = 'client-visible' | 'internal';
|
|
54
|
+
|
|
55
|
+
export declare type ReservedOutputFile = {
|
|
56
|
+
path: string;
|
|
57
|
+
visibility: OutputVisibility;
|
|
58
|
+
publish: () => Promise<ArtifactDescriptor | undefined>;
|
|
59
|
+
cleanup?: () => Promise<void>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export declare type ReserveOutputOptions = {
|
|
63
|
+
field: string;
|
|
64
|
+
ext: string;
|
|
65
|
+
requestedClientPath?: string;
|
|
66
|
+
visibility?: OutputVisibility;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export declare type ResolvedInputFile = {
|
|
70
|
+
path: string;
|
|
71
|
+
cleanup?: () => Promise<void>;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export declare type ResolveInputOptions = {
|
|
75
|
+
usage: string;
|
|
76
|
+
field?: string;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export declare type TemporaryFile = {
|
|
80
|
+
path: string;
|
|
81
|
+
visibility: 'internal';
|
|
82
|
+
cleanup: () => Promise<void>;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export { }
|
package/dist/src/io.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{promises as t}from"node:fs";import e from"node:os";import i from"node:path";import{AppError as a}from"./152.js";function r(n={}){let l=n.cwd??process.cwd(),c=n.tempDir??e.tmpdir(),p=n.rootDir?o(n.rootDir,l):void 0;return{resolveInput:async t=>{if("uploadedArtifact"===t.kind)throw new a("UNSUPPORTED_OPERATION","Uploaded artifact inputs require a custom artifact adapter");return{path:o(t.path,l,p)}},reserveOutput:async(e,a)=>{let r,n=a.visibility??"client-visible",d=e?.kind==="path"?o(e.path,l,p):i.join(r=await t.mkdtemp(i.join(c,`agent-device-${a.field}-`)),`${a.field}${a.ext}`);return await t.mkdir(i.dirname(d),{recursive:!0}),{path:d,visibility:n,...r?{cleanup:async()=>{await t.rm(r,{recursive:!0,force:!0})}}:{},publish:async()=>e?.kind==="downloadableArtifact"?{kind:"localPath",field:a.field,path:d,fileName:e.fileName??i.basename(e.clientPath??d)}:void 0}},createTempFile:async e=>{let a=await t.mkdtemp(i.join(c,`${e.prefix}-`));return{path:i.join(a,`file${e.ext}`),visibility:"internal",cleanup:async()=>{await t.rm(a,{recursive:!0,force:!0})}}}}}function o(t,e,r){var o,n;let l,c=i.isAbsolute(t)?t:i.resolve(e,t);if(r&&(o=c,n=r,""!==(l=i.relative(n,o))&&(l.startsWith("..")||i.isAbsolute(l))))throw new a("INVALID_ARGS","Local path is outside the artifact adapter root",{path:c,rootDir:r});return c}export{r as createLocalArtifactAdapter};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"node:fs";import{setTimeout as r}from"node:timers/promises";import{normalizeBaseUrl as t,METRO_COMPANION_RUN_ARG as a,
|
|
1
|
+
import e from"node:fs";import{setTimeout as r}from"node:timers/promises";import{normalizeBaseUrl as t,METRO_COMPANION_RUN_ARG as a,ENV_SERVER_BASE_URL as s,ENV_LOCAL_BASE_URL as o,ENV_STATE_PATH as n,ENV_LAUNCH_URL as i,ENV_SCOPE_TENANT_ID as c,ENV_SCOPE_RUN_ID as f,ENV_BEARER_TOKEN as d,ENV_SCOPE_LEASE_ID as m}from"./320.js";async function l(e){var r,a;let s=await fetch(`${t(e.serverBaseUrl)}/api/metro/companion/register`,{method:"POST",headers:(r=e.serverBaseUrl,a=e.bearerToken,{authorization:`Bearer ${a}`,"content-type":"application/json",...r.includes("ngrok")?{"ngrok-skip-browser-warning":"1"}:{}}),body:JSON.stringify({...e.bridgeScope,local_base_url:t(e.localBaseUrl),...e.launchUrl?{launch_url:e.launchUrl}:{}})}),o=await s.json();if(!s.ok||!0!==o.ok||"string"!=typeof o.data?.ws_url)throw Error(`Failed to register Metro companion: ${JSON.stringify(o)}`);return{wsUrl:o.data.ws_url}}async function u(e){return"string"==typeof e?Buffer.from(e,"utf8"):e instanceof ArrayBuffer?Buffer.from(e):ArrayBuffer.isView(e)?Buffer.from(e.buffer,e.byteOffset,e.byteLength):"u">typeof Blob&&e instanceof Blob?Buffer.from(await e.arrayBuffer()):Buffer.from(String(e),"utf8")}async function p(e){return JSON.parse((await u(e.data)).toString("utf8"))}function y(e,r){1===e.readyState&&e.send(JSON.stringify(r))}async function g(e,r){1!==e.readyState&&await new Promise((t,a)=>{let s=()=>{i(),t()},o=()=>{i(),a(Error(`${r} WebSocket failed before opening.`))},n=()=>{i(),a(Error(`${r} WebSocket closed before opening.`))},i=()=>{e.removeEventListener("open",s),e.removeEventListener("error",o),e.removeEventListener("close",n)};e.addEventListener("open",s,{once:!0}),e.addEventListener("error",o,{once:!0}),e.addEventListener("close",n,{once:!0})})}async function w(e){e.readyState>=WebSocket.CLOSING||await new Promise(r=>{let t=()=>{a(),r()},a=()=>{e.removeEventListener("close",t),e.removeEventListener("error",t)};e.addEventListener("close",t,{once:!0}),e.addEventListener("error",t,{once:!0}),e.readyState>=WebSocket.CLOSING&&t()})}function b(e,r,t){try{e.close(1e3===r||r>=3e3&&r<=4999?r:3001,t)}catch{}}function h(r){return!r.statePath||e.existsSync(r.statePath)}async function S(e,r,a,s){var o,n;switch(r.type){case"ping":return void y(e,{type:"pong",timestamp:r.timestamp});case"http-request":try{let s=await fetch(new URL(r.path,`${t(a.localBaseUrl)}/`),{method:r.method,headers:r.headers,...r.bodyBase64?{body:Buffer.from(r.bodyBase64,"base64")}:{}}),o=Buffer.from(await s.arrayBuffer());y(e,{type:"http-response",requestId:r.requestId,status:s.status,headers:Object.fromEntries(s.headers.entries()),...o.length>0?{bodyBase64:o.toString("base64")}:{}})}catch(t){y(e,{type:"http-error",requestId:r.requestId,message:t instanceof Error?t.message:String(t)})}return;case"ws-open":{let n,i=new WebSocket((o=a.localBaseUrl,(n=new URL(r.path,`${t(o)}/`)).protocol="https:"===n.protocol?"wss:":"ws:",n.toString()));i.binaryType="arraybuffer";let c=!1;i.addEventListener("message",t=>{(async()=>{if(!c)return;let a=await u(t.data);y(e,{type:"ws-frame",streamId:r.streamId,dataBase64:a.toString("base64"),binary:"string"!=typeof t.data})})().catch(e=>{console.error(e instanceof Error?e.message:String(e))})}),i.addEventListener("close",t=>{s.delete(r.streamId),c&&y(e,{type:"ws-close",streamId:r.streamId,code:t.code,reason:t.reason})}),i.addEventListener("error",()=>{c&&y(e,{type:"ws-close",streamId:r.streamId,code:1011,reason:"Upstream WebSocket error."})}),s.set(r.streamId,i);try{await g(i,"Upstream"),c=!0,y(e,{type:"ws-open-result",streamId:r.streamId,success:!0,headers:{}})}catch(t){s.delete(r.streamId),b(i,1011,"open failed"),y(e,{type:"ws-open-result",streamId:r.streamId,success:!1,error:t instanceof Error?t.message:String(t)})}return}case"ws-frame":{let e=s.get(r.streamId);if(!e||1!==e.readyState)return;let t=Buffer.from(r.dataBase64,"base64");e.send(r.binary?t:t.toString("utf8"));return}case"ws-close":{let e=s.get(r.streamId);if(!e)return;s.delete(r.streamId),b(e,"number"==typeof(n=r.code)&&Number.isInteger(n)&&(1e3===n||n>=3e3&&n<=4999||n>=1001&&n<=1015&&1004!==n&&1005!==n&&1006!==n)?n:1011,r.reason??"bridge requested close");return}}}async function v(e){let t=new Map,a=setInterval(()=>{h(e)||process.exit(0)},250);for(a.unref();h(e);){try{let r=await l(e),a=new WebSocket(r.wsUrl);a.binaryType="arraybuffer",await g(a,"Bridge"),a.addEventListener("message",r=>{(async()=>{let s=await p(r);await S(a,s,e,t)})().catch(e=>{console.error(e instanceof Error?e.message:String(e))})}),await w(a),t.forEach(e=>b(e,1012,"bridge disconnected")),t.clear()}catch(r){if(!h(e))break;console.error(r instanceof Error?r.message:String(r))}if(!h(e))break;await r(1e3)}clearInterval(a)}(async function(e,r){let t=function(e,r){if(e[0]!==a)return null;let t=r[s]?.trim(),l=r[d]?.trim(),u=r[o]?.trim();if(!t||!l||!u)throw Error("Metro companion worker is missing required environment configuration.");let p=r[c]?.trim(),y=r[f]?.trim(),g=r[m]?.trim();if(!p||!y||!g)throw Error("Metro companion worker is missing required bridge scope configuration.");return{serverBaseUrl:t,bearerToken:l,localBaseUrl:u,bridgeScope:{tenantId:p,runId:y,leaseId:g},launchUrl:r[i]?.trim()||void 0,statePath:r[n]?.trim()||void 0}}(e,r);return!!t&&(await v(t),!0)})(process.argv.slice(2),process.env).catch(e=>{if(e instanceof Error&&e.message.includes("missing required environment")){console.error(e.message),process.exitCode=1;return}console.error(e instanceof Error?e.stack??e.message:String(e)),process.exitCode=1});
|
package/dist/src/metro.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ export declare type EnsureMetroTunnelOptions = {
|
|
|
11
11
|
serverBaseUrl: string;
|
|
12
12
|
bearerToken: string;
|
|
13
13
|
localBaseUrl: string;
|
|
14
|
+
bridgeScope: {
|
|
15
|
+
tenantId: string;
|
|
16
|
+
runId: string;
|
|
17
|
+
leaseId: string;
|
|
18
|
+
};
|
|
14
19
|
launchUrl?: string;
|
|
15
20
|
profileKey?: string;
|
|
16
21
|
consumerKey?: string;
|
|
@@ -155,6 +160,11 @@ export declare type PrepareRemoteMetroOptions = {
|
|
|
155
160
|
publicBaseUrl: string;
|
|
156
161
|
proxyBaseUrl?: string;
|
|
157
162
|
proxyBearerToken?: string;
|
|
163
|
+
bridgeScope?: {
|
|
164
|
+
tenantId: string;
|
|
165
|
+
runId: string;
|
|
166
|
+
leaseId: string;
|
|
167
|
+
};
|
|
158
168
|
launchUrl?: string;
|
|
159
169
|
profileKey?: string;
|
|
160
170
|
consumerKey?: string;
|
package/dist/src/metro.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{URL as t}from"node:url";import"./818.js";import{AppError as e}from"./152.js";import"./164.js";import{buildMetroRuntimeHints as r,ensureMetroCompanion as o,stopMetroCompanion as n,prepareMetroRuntime as i}from"./974.js";function s(t){let e=t?.trim();return e&&e.length>0?e:void 0}function u(t){if(Number.isInteger(t)&&!(t<=0)&&!(t>65535))return t}function l(r){return function(r){if(!r)return;let o=s(r.metroHost),n=u(r.metroPort),i="http",l=s(r.bundleUrl);if(l){var
|
|
1
|
+
import{URL as t}from"node:url";import"./818.js";import{AppError as e}from"./152.js";import"./164.js";import{buildMetroRuntimeHints as r,ensureMetroCompanion as o,stopMetroCompanion as n,prepareMetroRuntime as i}from"./974.js";function s(t){let e=t?.trim();return e&&e.length>0?e:void 0}function u(t){if(Number.isInteger(t)&&!(t<=0)&&!(t>65535))return t}function l(r){return function(r){if(!r)return;let o=s(r.metroHost),n=u(r.metroPort),i="http",l=s(r.bundleUrl);if(l){var p;let r;try{r=new t(l)}catch(t){throw new e("INVALID_ARGS",`Invalid runtime bundle URL: ${l}`,{},t)}("http:"===r.protocol||"https:"===r.protocol)&&(o??=s(r.hostname),n??=u(r.port.length>0?Number(r.port):"https:"===(p=r.protocol)?443:"http:"===p?80:void 0),i="https:"===r.protocol?"https":"http")}if(o&&n)return{host:o,port:n,scheme:i}}(r)}async function p(t){let e=await i({projectRoot:t.projectRoot,kind:t.kind,publicBaseUrl:t.publicBaseUrl,proxyBaseUrl:t.proxyBaseUrl,proxyBearerToken:t.proxyBearerToken,bridgeScope:t.bridgeScope,launchUrl:t.launchUrl,companionProfileKey:t.profileKey,companionConsumerKey:t.consumerKey,metroPort:t.port,listenHost:t.listenHost,statusHost:t.statusHost,startupTimeoutMs:t.startupTimeoutMs,probeTimeoutMs:t.probeTimeoutMs,reuseExisting:t.reuseExisting,installDependenciesIfNeeded:t.installDependenciesIfNeeded,runtimeFilePath:t.runtimeFilePath,logPath:t.logPath,env:t.env});return{iosRuntime:e.iosRuntime,androidRuntime:e.androidRuntime,bridge:e.bridge,started:e.started,reused:e.reused,logPath:e.logPath}}async function a(t){let e=await o(t);return{pid:e.pid,started:e.spawned,logPath:e.logPath}}async function d(t){await n(t)}function m(t){return r(t,"ios")}function c(t){return r(t,"android")}export{buildBundleUrl,normalizeBaseUrl}from"./974.js";export{c as buildAndroidRuntimeHints,m as buildIosRuntimeHints,a as ensureMetroTunnel,p as prepareRemoteMetro,l as resolveRuntimeTransport,d as stopMetroTunnel};
|
package/dist/src/selectors.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{findSelectorChainMatch,formatSelectorFailure,isNodeEditable,isNodeVisible,isSelectorToken,parseSelectorChain,resolveSelectorChain,tryParseSelectorChain}
|
|
1
|
+
import{AppError as e}from"./152.js";import{normalizeText as t}from"./556.js";let r=new Set(["id","role","text","label","value","appname","windowtitle"]),n=new Set(["visible","hidden","editable","selected","enabled","hittable"]),i=new Set([...r,...n]);function l(t){let r=t.trim();if(!r)throw new e("INVALID_ARGS","Selector expression cannot be empty");let n=function(t){let r=[],n="",i=null;for(let l=0;l<t.length;l+=1){let u=t[l];if(('"'===u||"'"===u)&&!f(t,l)){i=c(i,u),n+=u;continue}if(!i&&"|"===u&&"|"===t[l+1]){let i=n.trim();if(!i)throw new e("INVALID_ARGS",`Invalid selector fallback expression: ${t}`);r.push(i),n="",l+=1;continue}n+=u}let l=n.trim();if(!l)throw new e("INVALID_ARGS",`Invalid selector fallback expression: ${t}`);return r.push(l),r}(r);if(0===n.length)throw new e("INVALID_ARGS","Selector expression cannot be empty");return{raw:r,selectors:n.map(t=>(function(t){let r=t.trim();if(!r)throw new e("INVALID_ARGS","Selector segment cannot be empty");let n=function(t){let r=[],n="",i=null;for(let e=0;e<t.length;e+=1){let l=t[e];if(('"'===l||"'"===l)&&!f(t,e)){i=c(i,l),n+=l;continue}if(!i&&/\s/.test(l)){n.trim()&&r.push(n.trim()),n="";continue}n+=l}if(i)throw new e("INVALID_ARGS",`Unclosed quote in selector: ${t}`);return n.trim()&&r.push(n.trim()),r}(r);if(0===n.length)throw new e("INVALID_ARGS",`Invalid selector segment: ${t}`);return{raw:r,terms:n.map(a)}})(t))}}function u(e){try{return l(e)}catch{return null}}function o(e){let t=e.trim();if(!t)return!1;if("||"===t)return!0;let r=t.indexOf("=");if(-1!==r){let e=t.slice(0,r).trim().toLowerCase();return i.has(e)}return i.has(t.toLowerCase())}function a(t){let r=t.trim();if(!r)throw new e("INVALID_ARGS","Empty selector term");let l=r.indexOf("=");if(-1===l){let i=r.toLowerCase();if(!n.has(i))throw new e("INVALID_ARGS",`Invalid selector term "${t}", expected key=value`);return{key:i,value:!0}}let u=r.slice(0,l).trim().toLowerCase(),o=r.slice(l+1).trim();if(!i.has(u))throw new e("INVALID_ARGS",`Unknown selector key: ${u}`);if(!o)throw new e("INVALID_ARGS",`Missing selector value for key: ${u}`);if(n.has(u)){let t,r="true"===(t=s(o).toLowerCase())||"false"!==t&&null;if(null===r)throw new e("INVALID_ARGS",`Invalid boolean value for ${u}: ${o}`);return{key:u,value:r}}return{key:u,value:s(o)}}function c(e,t){return e?e===t?null:e:t}function s(e){let t=e.trim();return t.startsWith('"')&&t.endsWith('"')||t.startsWith("'")&&t.endsWith("'")?t.slice(1,-1).replace(/\\(["'])/g,"$1"):t}function f(e,t){let r=0;for(let n=t-1;n>=0&&"\\"===e[n];n-=1)r+=1;return r%2==1}function d(e,t){let r=t.toLowerCase();return e.find(e=>{let t=(e.label??"").toLowerCase(),n=(e.value??"").toLowerCase(),i=(e.identifier??"").toLowerCase();return t.includes(r)||n.includes(r)||i.includes(r)})??null}function h(e,t){let r=[e.label,e.value,e.identifier].map(e=>"string"==typeof e?e.trim():"").find(e=>e&&e.length>0);return r&&m(r)?r:function(e,t){if(!e.rect)return;let r=e.rect.y+e.rect.height/2,n=null;for(let e of t){if(!e.rect)continue;let t=[e.label,e.value,e.identifier].map(e=>"string"==typeof e?e.trim():"").find(e=>e&&e.length>0);if(!t||!m(t))continue;let i=Math.abs(e.rect.y+e.rect.height/2-r);(!n||i<n.distance)&&(n={label:t,distance:i})}return n?.label}(e,t)??(r&&m(r)?r:void 0)}function m(e){let t=e.trim();return!(!t||/^(true|false)$/i.test(t)||/^\d+$/.test(t))}function w(e){let t=e.trim().replace(/XCUIElementType/gi,"");t.startsWith("AX")&&(t=t.slice(2));let r=Math.max((t=t.toLowerCase()).lastIndexOf("."),t.lastIndexOf("/"));return -1!==r&&(t=t.slice(r+1)),t}function p(e,t){let r=w(e);return!r||("android"===t?r.includes("edittext")||r.includes("autocompletetextview"):r.includes("textfield")||r.includes("securetextfield")||r.includes("searchfield")||r.includes("textview")||r.includes("textarea")||"search"===r)}function b(e,t){if(t.hittable)return t;let r=t,n=new Set;for(;void 0!==r.parentIndex&&!n.has(r.ref);){n.add(r.ref);let t=e[r.parentIndex];if(!t)break;if(t.hittable)return t;r=t}return null}function v(e){return[e.label,e.value,e.identifier].map(e=>"string"==typeof e?e.trim():"").filter(e=>e.length>0)[0]??""}function S(e){return!0===e.hittable||!!e.rect&&e.rect.width>0&&e.rect.height>0}function I(e,t){return p(e.type??"",t)&&!1!==e.enabled}function g(e,t,r){return t.terms.every(t=>(function(e,t,r){switch(t.key){case"id":return A(e.identifier,String(t.value));case"role":return A(w(e.type??""),String(t.value));case"label":return A(e.label,String(t.value));case"value":return A(e.value,String(t.value));case"text":return A(v(e),String(t.value));case"appname":return A(e.appName,String(t.value));case"windowtitle":return A(e.windowTitle,String(t.value));case"visible":return S(e)===!!t.value;case"hidden":return!S(e)==!!t.value;case"editable":return I(e,r)===!!t.value;case"selected":return!0===e.selected==!!t.value;case"enabled":return!1!==e.enabled==!!t.value;case"hittable":return!0===e.hittable==!!t.value;default:return!1}})(e,t,r))}function A(e,r){return t(e??"")===t(r)}function x(e,t,r){let n=r.requireRect??!1,i=r.requireUnique??!0,l=[];for(let u=0;u<t.selectors.length;u+=1){let o=t.selectors[u],a=function(e,t,r,n){let i=0,l=null,u=null,o=!1;for(let a of e){if(n&&!a.rect||!g(a,t,r))continue;if(i+=1,l??=a,!u){u=a;continue}let e=function(e,t){let r=e.depth??0,n=t.depth??0;if(r!==n)return r>n?1:-1;let i=N(e),l=N(t);return i!==l?i<l?1:-1:0}(a,u);e>0?(u=a,o=!1):0===e&&(o=!0)}return{count:i,firstNode:l,disambiguated:o?null:u}}(e,o,r.platform,n);if(l.push({selector:o.raw,matches:a.count}),0!==a.count&&a.firstNode){if(i&&1!==a.count){if(!r.disambiguateAmbiguous||!a.disambiguated)continue;return{node:a.disambiguated,selector:o,selectorIndex:u,matches:a.count,diagnostics:l}}return{node:a.firstNode,selector:o,selectorIndex:u,matches:a.count,diagnostics:l}}}return null}function L(e,t,r){let n=r.requireRect??!1,i=[];for(let l=0;l<t.selectors.length;l+=1){let u=t.selectors[l],o=function(e,t,r,n){let i=0;for(let l of e)(!n||l.rect)&&g(l,t,r)&&(i+=1);return i}(e,u,r.platform,n);if(i.push({selector:u.raw,matches:o}),o>0)return{selectorIndex:l,selector:u,matches:o,diagnostics:i}}return null}function y(e,t,r){if(0===t.length)return`Selector did not match: ${e.raw}`;let n=t.map(e=>`${e.selector} -> ${e.matches}`).join(", ");return r.unique??!0?`Selector did not resolve uniquely (${n})`:`Selector did not match (${n})`}function N(e){return e.rect?e.rect.width*e.rect.height:1/0}export{v as extractNodeText,b as findNearestHittableAncestor,d as findNodeByLabel,L as findSelectorChainMatch,y as formatSelectorFailure,p as isFillableType,I as isNodeEditable,S as isNodeVisible,o as isSelectorToken,w as normalizeType,l as parseSelectorChain,h as resolveRefLabel,x as resolveSelectorChain,u as tryParseSelectorChain};
|