agent-device
Version:
Open source device layer for AI coding agents: a CLI, MCP server, and typed Node.js API to control mobile, web, TV, and desktop apps on devices and simulators, locally or in the cloud.
1 lines • 21 kB
JavaScript
import{r as e}from"./rolldown-runtime.js";import{Ai as t,Ci as n,Di as r,Ni as i,Oi as a,_i as o,hi as s,ji as c,li as l,on as u,xi as d,zn as f}from"./sdk-batch.js";import{j as p,ot as m,s as h,v as g}from"./sdk-contracts.js";import{f as _,h as v,i as y,n as b,p as ee}from"./screenshot.js";import"./capture.js";import{St as x,gt as te}from"./sdk-io.js";import"./diagnostics.js";import{R as S,V as ne,w as re}from"./sdk-device.js";import"./file.js";import{t as ie}from"./session-paths.js";import{t as C}from"./snapshot-state.js";import{n as w}from"./provider-device-admission.js";import{n as T}from"./snapshot-runtime-capture-input.js";import{n as ae}from"./context.js";import{i as oe,t as se}from"./selector-pipeline-policy.js";import{i as ce,o as le,r as ue}from"./png-worker-client.js";import{t as de}from"./react-native-overlay2.js";import{t as fe}from"./command-policy.js";import{t as pe}from"./png-size.js";import{a as E,o as D,s as O,t as me}from"./screenshot-overlay-draw.js";import{f as he,i as k,n as ge}from"./runtime-session.js";import{a as _e,i as ve,n as ye,t as be}from"./session-runtime-admission.js";import{r as xe,t as Se}from"./screenshot-density.js";import{promises as A}from"node:fs";import j from"node:os";import M from"node:path";function Ce(e,t){return{resolveInput:async()=>{throw new g(`UNSUPPORTED_OPERATION`,`screenshot does not resolve input artifacts`)},reserveOutput:async n=>{try{let r,i;return n?.kind===`path`?i=n.path:(r=await A.mkdtemp(M.join(j.tmpdir(),`agent-device-screenshot-`)),i=M.join(r,`screenshot.png`)),await A.mkdir(M.dirname(i),{recursive:!0}),e&&n?.kind===`path`?await we(i,t):{path:i,visibility:`client-visible`,publish:async()=>{e&&t?.throwIfAborted()},...r?{cleanup:async()=>{await A.rm(r,{recursive:!0,force:!0})}}:{}}}catch(e){throw e instanceof g?e:N(e,n?.kind===`path`?n.path:`(temporary screenshot)`)}},createTempFile:async e=>{let t=await A.mkdtemp(M.join(j.tmpdir(),`${e.prefix}-`));return{path:M.join(t,`file${e.ext}`),visibility:`internal`,cleanup:async()=>{await A.rm(t,{recursive:!0,force:!0})}}}}}function N(e,t){return new g(`COMMAND_FAILED`,`Failed to publish screenshot`,{reason:`screenshot_publication_failed`,path:t,...e instanceof Error&&`code`in e?{filesystemCode:e.code}:{},cause:p(e)})}async function we(e,t){let n=await Te(e),r=await A.mkdtemp(M.join(j.tmpdir(),`agent-device-crop-`)),i=M.join(r,`screenshot${M.extname(e)||`.png`}`),a=async()=>{await A.rm(r,{recursive:!0,force:!0}).catch(e=>{x({level:`warn`,phase:`screenshot_crop_cleanup_failed`,data:{tempRoot:r,error:p(e)}})})};return{path:i,publishedPath:e,visibility:`client-visible`,publish:async()=>{try{let e=await A.readFile(i);t?.throwIfAborted(),te({destination:n,contents:e,preserveExistingMode:!0})}catch(n){throw t?.aborted?t.reason:N(n,e)}await a()},cleanup:a}}async function Te(e){let t;try{t=(await A.lstat(e).catch(e=>{if(e.code!==`ENOENT`)throw e}))?.isSymbolicLink()?await A.realpath(e).catch(t=>{throw t.code===`ENOENT`?new g(`INVALID_ARGS`,`Screenshot destination is a dangling symbolic link`,{reason:`screenshot_destination_dangling_symlink`,path:e}):t}):e;let n=await A.stat(t).catch(e=>{if(e.code!==`ENOENT`)throw e});if(n&&!n.isFile())throw new g(`INVALID_ARGS`,`Screenshot destination must be a regular file`,{reason:`screenshot_destination_not_file`,path:e})}catch(t){throw t instanceof g?t:N(t,e)}return t}const P=y.pendingPixelIdentityEvidence,Ee=[{target:`ios-simulator`,status:`accepted`},{target:`android-emulator`,status:`accepted`},{target:`android-device`,status:`rejected`,rejectionReason:P},{target:`provider-android-tv`,status:`accepted`},{target:`macos-app-window`,status:`rejected`,rejectionReason:P},{target:`ios-physical`,status:`accepted`},{target:`ios-physical-xctest`,status:`rejected`,rejectionReason:P},{target:`ipados-physical`,status:`rejected`,rejectionReason:P},{target:`macos-helper`,status:`rejected`,rejectionReason:P},{target:`web`,status:`rejected`,rejectionReason:P},{target:`linux`,status:`rejected`,rejectionReason:P},{target:`tvos`,status:`rejected`,rejectionReason:P},{target:`harmonyos`,status:`rejected`,rejectionReason:P},{target:`vega`,status:`rejected`,rejectionReason:P}];function F(e,t){switch(e.platform){case`apple`:return Oe(e,t);case`android`:return De(e);case`harmonyos`:return`harmonyos`;case`vega`:return`vega`;case`linux`:return`linux`;case`web`:return`web`}}function De(e){return e.target===`tv`&&e.kind===`device`&&w(e)?`provider-android-tv`:e.kind===`device`?`android-device`:`android-emulator`}function Oe(e,t){let n=ne(e);switch(n){case`ios`:return e.kind===`device`?e.iosPhysicalDeviceBackend===`xctest`?`ios-physical-xctest`:`ios-physical`:`ios-simulator`;case`ipados`:return e.kind===`device`?`ipados-physical`:`ios-simulator`;case`tvos`:return`tvos`;case`macos`:return ke(t);case`watchos`:case`visionos`:throw I(n)}}function ke(e){return(e??`app`)===`app`?`macos-app-window`:`macos-helper`}function I(e,t){return new g(`UNSUPPORTED_OPERATION`,`screenshot --crop-on is not accepted on ${e} targets`,{reason:y.targetNotAccepted,...t?{rejectionReason:t}:{}})}function Ae(e){if(e.overlayRefs||e.fullscreen)throw new g(`INVALID_ARGS`,`--crop-on cannot be combined with --overlay-refs or --fullscreen: both move the captured frame away from the snapshot viewport the crop is measured against`,{reason:y.frameMismatch});try{f(e.cropOn)}catch{throw new g(`INVALID_ARGS`,`screenshot --crop-on requires a valid selector expression`,{reason:y.selectorInvalid})}let t=F(e.device,e.surface),n=Ee.find(e=>e.target===t);if(!n||n.status===`rejected`)throw I(t,n?.status===`rejected`?n.rejectionReason:void 0)}function je(e,t){if(F(e,void 0)===`ios-physical`&&t.iosPhysicalCaptureBackend!==`coredevice`)throw I(`ios-physical-non-coredevice-capture`,P)}function Me(e,t){if(F(e,void 0)===`provider-android-tv`){if(t.producer!==`cdp-dom`)throw I(`provider-android-tv-non-cdp-snapshot`,P);if(t.captureScale===void 0||t.viewport===void 0)throw new g(`COMMAND_FAILED`,`the TV page published no capture scale or viewport, so --crop-on cannot place its rects in the capture: its browser has no visualViewport, or the visual viewport is panned`,{reason:y.captureUnreadable})}}async function Ne(e,t){if(!Pe(t))throw new g(`INVALID_ARGS`,`Screenshot crop box must be positive integer pixel offsets`);let n=await ue(await A.readFile(e),t,`screenshot`);n!==null&&await A.writeFile(e,n)}function Pe(e){return Number.isInteger(e.x)&&e.x>=0&&Number.isInteger(e.y)&&e.y>=0&&Number.isInteger(e.width)&&e.width>0&&Number.isInteger(e.height)&&e.height>0}function Fe(e){switch(e){case`android`:return`device-pixels`;case`xctest`:case`macos-helper`:return`viewport-points`;default:throw new g(`UNSUPPORTED_OPERATION`,`screenshot --crop-on does not accept snapshot backend "${e??`unknown`}"`,{reason:y.targetNotAccepted})}}function L(e,t,n,r,i,a){if(e===`device-pixels`){let e=a??1;return{x:Math.round(n.x*e),y:Math.round(n.y*e),width:Math.round(n.width*e),height:Math.round(n.height*e)}}if(t===null)return ze(n);let o=r/t.width,s=i/t.height;return{x:Math.round((n.x-t.x)*o),y:Math.round((n.y-t.y)*s),width:Math.round(n.width*o),height:Math.round(n.height*s)}}function R(e,t){return t.captureScale===void 0||t.viewport===void 0?{width:e.width,height:e.height}:{width:Math.min(e.width,Math.round(t.viewport.width*t.captureScale)),height:Math.min(e.height,Math.round(t.viewport.height*t.captureScale))}}function Ie(e,t,n){return d(e,{x:0,y:0,width:t,height:n})??null}function z(e){let t=null;for(let i of e)!l(i)||!n(i.rect)||(!t||r(i.rect)>r(t))&&(t=i.rect);return t||Le(e.filter(e=>n(e.rect)&&!Re(e)))}function Le(e){let t=1/0,r=1/0,i=-1/0,a=-1/0;for(let o of e)n(o.rect)&&(t=Math.min(t,o.rect.x),r=Math.min(r,o.rect.y),i=Math.max(i,o.rect.x+o.rect.width),a=Math.max(a,o.rect.y+o.rect.height));return!Number.isFinite(t)||!Number.isFinite(r)||i<=t||a<=r?null:{x:t,y:r,width:i-t,height:a-r}}function Re(e){return o(e.type??``)===`image`&&!s(e.label)}function ze(e){return{x:Math.round(e.x),y:Math.round(e.y),width:Math.round(e.width),height:Math.round(e.height)}}const Be=`${y.partialIntersection}: the selector frame extends past the captured image; the crop was clipped to the image frame`;function Ve(e){return e?.partialIntersection?[Be]:[]}async function He(e){let{device:t,session:n,surface:r,cropOn:i,screenshotPath:a,logPath:o,dispatchContext:s}=e,c={snapshotInteractiveOnly:!1},l=await k({device:t,session:n,flags:c,logPath:o,snapshotScope:void 0,captureData:async()=>await e.captureSnapshot({options:{appBundleId:s.appBundleId,interactiveOnly:!1,includeRects:!0,surface:r},execution:T(s)})}),u=C(l,c);if(Me(t,l),u.snapshotQuality?.state===`sparse`)throw new g(`COMMAND_FAILED`,`the snapshot taken for --crop-on was sparse and cannot be read as the screen`,{reason:y.captureUnreadable});let d=await Ue(u.nodes,i,u.truncated,t),f=await pe(a),p=L(Fe(u.backend),z(u.nodes),d.rect,f.width,f.height,l.captureScale),m=R(f,l),h=Ie(p,m.width,m.height);if(h===null)throw new g(`COMMAND_FAILED`,`the frame resolved by --crop-on occupies no pixel of the capture`,{reason:y.emptyIntersection});let _=h.x!==p.x||h.y!==p.y||h.width<p.width||h.height<p.height;return await Ne(a,h),{partialIntersection:_}}async function Ue(e,t,n,r){let i=await oe(se.cropTarget,e,t,{platform:S(r)});if(i.kind===`ambiguous`)throw new g(`COMMAND_FAILED`,`--crop-on matched ${i.matchedNodes.length} nodes and must resolve to one`,{reason:y.targetAmbiguous,candidates:i.matchedNodes.map(e=>e.label??e.ref),matches:i.matchedNodes.length});if(i.kind===`none`)throw n?new g(`COMMAND_FAILED`,`the snapshot taken for --crop-on was truncated, so a missing match is not proven`,{reason:y.captureIncomplete}):new g(`COMMAND_FAILED`,`--crop-on matched no node: ${t}`,{reason:y.targetNotFound,hint:`find ${t} list`});return i.node}const We=[`scroll`,`list`,`recyclerview`,`edittext`,`textfield`];function Ge(e,t,n){if(e.backend!==`android`||!n.hittable||!D(n.rect)||l(n))return!1;let r=o(n.type??``);return!(We.some(e=>r.includes(e))||t&&O(n.rect)>O(t)*.25)}function Ke(e,t,n,r){return!e.rect||e.hittable!==!0||n(e)||r(e)?null:qe(e,t,r)}function qe(e,t,n){let r=e.rect,i=Je(e,t,n);if(!i)return null;let a=i.y-r.y,o=r.y+r.height-(i.y+i.height);if(a<0||o<0||Math.abs(o-a)<16)return null;let s=Math.min(a,o),c=Math.round(i.y-s),l=Math.round(i.height+s*2);return l<=0||l>=r.height?null:{x:r.x,y:c,width:r.width,height:l}}function Je(e,n,r){let i=e.rect,o=new Map(n.map(e=>[e.index,e])),s=n.filter(t=>t.ref!==e.ref&&Xe(t,e,o)&&Ye(t,r)&&D(t.rect)&&a(i,t.rect)).map(e=>e.rect);return s.length<2?null:t(s)}function Ye(e,t){let n=o(e.type??``);return n.includes(`text`)||n.includes(`image`)&&t(e)}function Xe(e,t,n){let r=e;for(;r.parentIndex!==void 0;){let e=n.get(r.parentIndex);if(!e)return!1;if(e.ref===t.ref)return!0;r=e}return!1}const Ze=[`button`,`link`,`menu`,`tab`,`textfield`,`searchfield`,`securetextfield`,`checkbox`,`radio`,`switch`,`cell`];async function B(e){let t=await A.readFile(e.screenshotPath),n=await ce(t,`screenshot`),r=Qe(e.snapshot,n.width,n.height,{maxRefs:e.maxRefs});for(let e of r)me(n,e);return await A.writeFile(e.screenshotPath,await le(n)),r}function Qe(e,t,n,r={}){let i=z(e.nodes),a=c(e.nodes),o=ct(e.nodes,a),s=new Map;for(let r of e.nodes){if(!tt(e,i,r))continue;let c=nt(e.nodes,a,r);if(!c?.ref||!c.rect||!D(c.rect))continue;let l=rt(r,c,o),u=it(r,c,l),d=V(e,i,et(e,c,e.nodes),t,n);if(!D(d))continue;let f=s.get(c.ref);(!f||u>f.score)&&s.set(c.ref,{ref:c.ref,label:l,rect:c.rect,overlayRect:d,score:u})}return $e(e,i,s,t,n),at([...s.values()]).sort(gt).slice(0,r.maxRefs??24).sort(J).map(e=>({ref:e.ref,label:e.label,rect:e.rect,overlayRect:e.overlayRect,center:m(e.overlayRect)}))}function $e(e,t,n,r,i){let a=de(e.nodes).primaryAction;if(!a?.ref||!a.rect||!D(a.rect))return;let o=V(e,t,a.rect,r,i);if(!D(o))return;let s={ref:a.ref,label:a.label,rect:a.rect,overlayRect:o,score:100},c=n.get(a.ref);n.set(a.ref,c?{...c,score:Math.max(c.score,s.score)}:s)}function et(e,t,n){return e.backend===`android`?Ke(t,n,H,e=>!!K(e))??t.rect:t.rect}function tt(e,t,n){let r=[n.label,n.value].some(W)||G(n.identifier);return Ge(e,t,n)?!0:H(n)?r:r&&ot(n)}function nt(e,t,n){let r=r=>i(e,n,t,e=>r(e)?e:null);return[U(n)?n:null,r(U),n.hittable?n:null,r(e=>e.hittable===!0)].find(st)??null}function rt(e,t,n){let r=K(e);return e.ref!==t.ref&&r?r:(t.ref?n.get(t.ref):void 0)||K(t)}function it(e,t,n){let r=0;return e.ref===t.ref&&(r+=4),t.hittable&&(r+=3),H(t)&&(r+=3),H(e)&&(r+=2),n&&(r+=2),G(t.identifier)&&(r+=1),s(t.value)&&(r+=1),r}function at(e){let t=[];for(let n of e.sort((e,t)=>O(e.overlayRect)-O(t.overlayRect))){let e=t.findIndex(e=>e.label!==void 0&&e.label===n.label&&(a(e.overlayRect,n.overlayRect)||a(n.overlayRect,e.overlayRect)));if(e===-1){t.push(n);continue}O(n.overlayRect)<O(t[e].overlayRect)&&(t[e]=n)}return t}function V(e,t,n,r,i){let a=e.backend===`android`?`device-pixels`:`viewport-points`,o=R({width:r,height:i},e);return ht(L(a,t,n,r,i,e.captureScale),o.width,o.height)}function H(e){let t=[e.type,e.role,e.subrole].map(e=>o(e??``)).join(` `);return Ze.some(e=>t.includes(e))}function U(e){return H(e)&&!l(e)}function ot(e){let t=o(e.type??``);return t.includes(`statictext`)||t.includes(`image`)||t.includes(`text`)||t.includes(`other`)}function st(e){return!!(e?.rect&&D(e.rect)&&!l(e))}function W(e){return s(e)?!ft(e):!1}function G(e){return typeof e!=`string`||!W(e)?!1:!pt(e)}function K(e){let t=[e.label,e.value].find(W);if(t)return t.trim();if(G(e.identifier))return e.identifier.trim()}function ct(e,t){let n=lt(e,t),r=[],i=new Set,a=n.roots.map(e=>({position:e,expanded:!1}));for(;a.length>0;){let t=a.pop();if(t.expanded){r[t.position]=ut(e,n.byParent.get(t.position),r);continue}if(!i.has(t.position)){i.add(t.position),a.push({position:t.position,expanded:!0});for(let e of n.byParent.get(t.position)??[])a.push({position:e,expanded:!1})}}let o=new Map;return e.forEach((e,t)=>{let n=r[t]?.label;e.ref&&n!==void 0&&!o.has(e.ref)&&o.set(e.ref,n)}),o}function lt(e,t){let n=new Map(e.map((e,t)=>[e,t])),r=[],i=new Map;return e.forEach((a,o)=>{let{parentIndex:s}=a,c=s===void 0?void 0:t.get(s)??e[s],l=c&&c!==a?n.get(c):void 0;if(l===void 0){r.push(o);return}let u=i.get(l);u?u.push(o):i.set(l,[o])}),{roots:r,byParent:i}}function ut(e,t,n){let r;for(let i of t??[]){let t=e[i],a=K(t),o=a?{label:a,score:dt(t),position:i}:void 0;r=q(q(r,o),n[i])}return r}function q(e,t){return!e||!t?e??t:t.score===e.score?t.position<e.position?t:e:t.score>e.score?t:e}function dt(e){let t=0;return o(e.type??``).includes(`text`)&&(t+=2),W(e.label)&&(t+=2),W(e.value)&&(t+=1),t}function ft(e){let t=e?.trim().toLowerCase();return t===`toolbar`||t===`window`||t===`application`||t?.startsWith(`vertical scroll bar`)===!0}function pt(e){return/^[a-z0-9_.]+:id\/[a-z0-9_.-]+$/i.test(e.trim())}function mt(e,t){return Number.parseInt(e.replace(/^\D+/,``),10)-Number.parseInt(t.replace(/^\D+/,``),10)}function ht(e,t,n){let r=E(e.x,0,Math.max(0,t-1)),i=E(e.y,0,Math.max(0,n-1)),a=Math.max(1,t-r),o=Math.max(1,n-i);return{x:r,y:i,width:E(e.width,1,a),height:E(e.height,1,o)}}function J(e,t){let n=e.overlayRect.y-t.overlayRect.y;if(n!==0)return n;let r=e.overlayRect.x-t.overlayRect.x;return r===0?mt(e.ref,t.ref):r}function gt(e,t){return t.score===e.score?J(e,t):t.score-e.score}async function Y(e){let t=u({overlayRefs:e.overlayRefs,cropOn:e.cropOn!==void 0}),n=await be({device:e.device,plan:t,inspectFacts:e.inspectFacts});return n.admitted?{ok:!0,runtime:await _t(e.bindDevice,n)}:{ok:!1,response:yt(t.kind,n.operation,n.fact)}}async function _t(e,t){let n=ye(e),{device:r,plan:i}=_e(t);switch(i.kind){case`capture`:{let e=await n(r,i.use);return Object.freeze({captureScreenshot:X(e)})}case`capture-with-overlay-refs`:case`capture-with-crop-on`:{let e=await n(r,i.use);return Object.freeze({captureScreenshot:X(e),captureSnapshot:vt(e)})}}}function X(e){return async t=>await e.operations.captureScreenshot(t)}function vt(e){return async t=>await e.operations.captureSnapshot(t)}function yt(e,t,n){return t===`captureScreenshot`?ve(`screenshot`,n):e===`capture-with-crop-on`?h(`UNSUPPORTED_OPERATION`,`--crop-on crops the capture to a selector frame using a snapshot taken on the same screen, which this target cannot capture.`,{reason:n.available?void 0:n.reason},{hint:(n.available?void 0:n.hint)??`Re-run screenshot without --crop-on.`}):h(`UNSUPPORTED_OPERATION`,`--overlay-refs annotates a capture with the refs of a snapshot taken on the same screen, which this target cannot capture.`,{reason:n.available?void 0:n.reason},{hint:(n.available?void 0:n.hint)??`Re-run screenshot without --overlay-refs.`})}var bt=e({captureScreenshotArtifact:()=>Z,resolveScreenshotGenericExecution:()=>xt,screenshotExecutionFromContext:()=>Q});async function xt(e){let{req:t,session:n}=e,r=_(`screenshot`,t.flags);if(r)throw new g(`INVALID_ARGS`,r);Se(n.device,t.flags?.screenshotPixelDensity),$(t.flags);let i=Et(t,e.hostCwd),a=typeof t.flags?.screenshotCropOn==`string`&&t.flags.screenshotCropOn.length>0?t.flags.screenshotCropOn:void 0;a!==void 0&&Ae({device:n.device,surface:n.surface,cropOn:a,overlayRefs:t.flags?.overlayRefs===!0,fullscreen:t.flags?.screenshotFullscreen===!0});let o=await Y({device:n.device,overlayRefs:t.flags?.overlayRefs===!0,cropOn:a,inspectFacts:e.inspectFacts,platformServices:e.platformServices,bindDevice:e.bindDevice});if(!o.ok)return o;let s=o.runtime,c={warnings:[]},{createCommandSurface:l}=e;return{ok:!0,recorded:i.recorded,execute:async e=>await St({createCommandSurface:l,session:e.session,sessionName:e.sessionName,logPath:e.logPath,dispatchContext:e.dispatchContext,flags:e.request.flags,outPath:i.outPath,runtime:s,cropOn:a,cropRun:c})}}async function Z(e){let{session:t,sessionName:n,outPath:r,dispatchContext:i}=e;return await(await e.createCommandSurface({backend:Dt(e),artifacts:Ce(e.crop!==void 0,i.signal),sessions:ge(n,t),policy:fe()})).capture.screenshot({session:n,requestId:i.requestId,appBundleId:t.appBundleId,...v(i),surface:t.surface,...r?{out:{kind:`path`,path:r}}:{}})}function Q(e){return T(e)}async function St(e){let{session:t,runtime:n,flags:r}=e,i=Ct({cropOn:e.cropOn,captureSnapshot:n.captureSnapshot,cropRun:e.cropRun,logPath:e.logPath}),a=await Z({createCommandSurface:e.createCommandSurface,session:t,sessionName:e.sessionName,outPath:e.outPath,dispatchContext:e.dispatchContext,captureScreenshot:n.captureScreenshot,...i?{crop:i}:{}}),o=[...a.warnings??[],...i?.run?.warnings??[]];return{...a,...o.length>0?{warnings:o}:{},...await wt({session:t,cropOn:e.cropOn,captureSnapshot:n.captureSnapshot,screenshotPath:a.path,logPath:e.logPath,dispatchContext:e.dispatchContext}),...await xe({device:t.device,path:a.path,requestedPixelDensity:r?.screenshotPixelDensity,scale:r?.screenshotScale})}}function Ct(e){let{cropOn:t,captureSnapshot:n,cropRun:r}=e;if(t!==void 0&&n!==void 0&&r!==void 0)return{cropOn:t,captureSnapshot:n,run:r,logPath:e.logPath}}async function wt(e){let{session:t,cropOn:n,captureSnapshot:r}=e;return n!==void 0||r===void 0?{}:{overlayRefs:await Tt({session:t,logPath:e.logPath,screenshotPath:e.screenshotPath,dispatchContext:e.dispatchContext,captureSnapshot:r})}}async function Tt(e){let{session:t,dispatchContext:n}=e,r={snapshotInteractiveOnly:!0},i=await k({device:t.device,session:t,flags:r,logPath:e.logPath,snapshotScope:void 0,captureData:async()=>await e.captureSnapshot({options:{appBundleId:t.appBundleId,interactiveOnly:!0,surface:t.surface},execution:Q(n)})}),a=C(i,r);return he(t,a),await B({screenshotPath:e.screenshotPath,snapshot:a})}function $(e){let t=e?.screenshotCaptureBackend;if(t!==void 0){if(!b.includes(t))throw new g(`INVALID_ARGS`,`--capture-backend takes ${b.join(`, `)}`);if(e?.screenshotPixelDensity!==void 0||e?.screenshotNormalizeStatusBar)throw new g(`INVALID_ARGS`,`--pixel-density and --normalize-status-bar cannot be combined with --capture-backend runner: they belong to the simctl capture`)}}function Et(e,t){let n=e.positionals??[],r=e.flags??{},i=n=>n===void 0?void 0:ie(n,ae(e,{cwd:t})),a=i(n[0]),o=i(r.out);return{outPath:a??o,recorded:{positionals:a?[a,...n.slice(1)]:n,flags:o?{...r,out:o}:r}}}function Dt(e){let{session:t,dispatchContext:n,captureScreenshot:r,crop:i}=e;return{platform:S(t.device),captureScreenshot:async(e,a,o)=>{let s=v({...n,...ee(o)}),c=await r({outPath:a,options:{appBundleId:n.appBundleId,pixelDensity:s.pixelDensity,screenMask:s.screenMask,fullscreen:s.fullscreen,normalizeStatusBar:s.normalizeStatusBar,stabilize:s.stabilize,surface:o?.surface,skipIosSimulatorBootCheck:n.skipIosSimulatorBootCheck??(re(t.device)&&t.device.kind===`simulator`),captureBackend:s.captureBackend,preferredCaptureBackend:n.preferredScreenshotCaptureBackend},execution:Q(n)});if(i){je(t.device,c);let e=await He({device:t.device,session:t,surface:t.surface,cropOn:i.cropOn,screenshotPath:a,logPath:i.logPath,dispatchContext:n,captureSnapshot:i.captureSnapshot});i.run.warnings.push(...Ve(e))}return c.displayRotation?{displayRotation:c.displayRotation}:void 0}}}export{B as a,Y as i,Q as n,bt as r,Z as t};