UNPKG

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 • 3.96 kB
import{v as e}from"./sdk-contracts.js";import{Z as t}from"./sdk-io.js";import"./command.js";import{i as n,n as r,r as i,t as a}from"./platform-runtime-screen-recording-process-host.js";import o from"node:fs";const s=(e,t)=>{if(e===t)return!0;let n=/^(?:\S*\/)?xcrun simctl (.+)$/.exec(e)?.[1],r=/^(?:\S*\/)?simctl (.+)$/.exec(t)?.[1];return n!==void 0&&n===r&&/^(?:--set .+ )?io \S+ recordVideo .+$/.test(n)};async function c(e,t,a){let{resolveAppleSimulatorScreenRecordingTransport:c}=await import(`./platform-runtime-screen-recording-apple-transport.js`);a?.throwIfAborted();let p;try{p=await u(c().start({device:e,outputPath:t,signal:a}),a),a?.throwIfAborted()}catch(e){throw p&&await d(p),o.rmSync(t,{force:!0}),a?.throwIfAborted(),e}if(!p)throw Error(`simctl recordVideo acquisition did not return a process`);let m;try{if(m=await l(p.child.pid,p.wait,a),!m)throw Error(`simctl recordVideo did not expose a complete process identity`)}catch(e){throw await d(p),a?.throwIfAborted(),e}try{await h(t,p.wait,a);let e=await r(m.pid)??m,n=await i(e);return f(p,n)}catch(e){throw await n([m],p,s).catch(()=>{}),await p.wait.catch(()=>void 0),o.rmSync(t,{force:!0}),a?.throwIfAborted(),e}}async function l(e,t,n){let i=g(t),a=e===void 0?0:80;for(let t=0;t<=a;t+=1){n?.throwIfAborted();let o=await r(e);if(o)return o;let s=await Promise.race([i,t<a?v(25,n).then(()=>void 0):Promise.resolve(void 0)]);if(s)throw _(s)}}async function u(e,t){let n=Promise.resolve(e);if(!t)return await n;if(t.aborted)throw`child`in e?await d(e):n.then(d).catch(()=>void 0),t.reason;let r=()=>{},i=new Promise((e,n)=>{let i=()=>n(t.reason);t.addEventListener(`abort`,i,{once:!0}),r=()=>t.removeEventListener(`abort`,i),t.aborted&&i()});try{return await Promise.race([n,i])}catch(e){throw t.aborted?(n.then(d).catch(()=>void 0),t.reason):e}finally{r()}}async function d(e){e.child.kill(`SIGINT`);let t=new AbortController;await Promise.race([e.wait.then(()=>!0,()=>!0),v(5e3,t.signal).then(()=>!1)]).finally(()=>t.abort())||(e.child.kill(`SIGKILL`),await e.wait.catch(()=>void 0))}function f(e,t){let r,i=!1,a=()=>n(t,e,s).then(e=>{if(e===`ownership-lost`)throw Error(`simctl recordVideo process ownership changed before cleanup`);i=e===`terminated`}),o=()=>(r??=a().catch(e=>{throw r=void 0,e}),r),c=e.wait.then(async e=>{let t=r;return t&&await t.catch(()=>void 0),i&&e.exitCode!==0?{...e,exitCode:0}:e});return Object.freeze({markers:Object.freeze([...t]),wait:c,terminate:o})}function p(e){return a(e,s)}async function m(e){return await n([e],void 0,s)}async function h(e,t,n){let r=g(t),i;r.then(e=>{i=e}),await Promise.resolve();for(let t=0;t<=60;t+=1){if(n?.throwIfAborted(),i)throw _(i);if(o.existsSync(e)){let e=await Promise.race([r,v(50,n).then(()=>void 0)]);if(e)throw _(e);return}if(t===60)throw Error(`simctl recordVideo did not create its output within 15000ms`);let a=await Promise.race([r,v(250,n).then(()=>void 0)]);if(a)throw _(a)}}function g(e){return e.then(e=>({kind:`exited`,result:e}),e=>({kind:`failed`,error:e}))}function _(n){if(n.kind===`failed`)return n.error instanceof Error?n.error:Error(String(n.error));let{exitCode:r}=n.result;return r===16?new e(`DEVICE_IN_USE`,`CoreSimulator host recording is already in progress`,t({...n.result,exitCode:r},{reason:`apple_simulator_recording_busy`,retriable:!1,hint:`Stop the active recording with record stop in its owning session. If a previous recorder died and no recording is active, ask the host operator to restart the CoreSimulator stream service before retrying.`})):Error(`simctl recordVideo exited with code ${r}`)}function v(e,t){return t?.throwIfAborted(),new Promise((n,r)=>{let i=e=>{clearTimeout(a),t?.removeEventListener(`abort`,o),e===void 0?n():r(e)},a=setTimeout(()=>i(),e);if(!t)return;let o=()=>i(t.reason);t.addEventListener(`abort`,o,{once:!0}),t.aborted&&o()})}export{p as inspectAppleSimulatorRecordingProcess,c as startAppleSimulatorRecording,m as terminateAppleSimulatorRecordingProcess};