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 • 4.29 kB
JavaScript
import{F as e}from"./sdk-contracts.js";import{Q as t}from"./sdk-io.js";import{o as n}from"./file.js";import"./command.js";import{a as r,i,r as a}from"./host3.js";import{createHash as o}from"node:crypto";import s from"node:path";const c=3e4;async function l(e,t){let n=await d(e,`xcodebuild`,[`-version`],t),i=await d(e,`sw_vers`,[`-productVersion`],t),a=await d(e,`sw_vers`,[`-buildVersion`],t),o=await u(e,t);if(o!==`arm64`&&o!==`x86_64`)throw r(`unsupported`,`simulator-architecture-unsupported`,{architecture:o});return{xcode:n,macosProductVersion:i,macosBuild:a,architecture:o}}async function u(e,t){let n=a(t,`host-arch-deadline`),i=t.signal,o,s,c=new Promise((e,t)=>{o=setTimeout(()=>t(r(`timeout`,`host-arch-deadline`)),n),o.unref?.(),s=()=>t(r(`cancelled`,`abort-signal`)),i?.addEventListener(`abort`,s,{once:!0})});try{return await Promise.race([e.cpuArch(),c])}finally{clearTimeout(o),s&&i?.removeEventListener(`abort`,s)}}async function d(e,t,n,i){let a=await f(e,t,n,i);if(a.exitCode!==0)throw r(`unsupported`,`toolchain-probe-failed`,{command:t,exitCode:a.exitCode,stderr:a.stderr.slice(0,1024)});let o=(a.stdout||a.stderr).trim();if(!o)throw r(`unsupported`,`toolchain-probe-empty`,{command:t});return o}async function f(e,t,n,r){let i=[],o;for(let s=1;;s+=1){let l;try{l=Math.min(c,a(r,`toolchain-probe-deadline`))}catch(e){throw o??e}i.push(l);try{return await h(e,t,n,r,l)}catch(e){if(o=p(e,t,r,i),s>=2)throw o}}}function p(n,i,a,o){if(e(n))throw r(`cancelled`,`abort-signal`,{},n);if(!t(n))throw n;if(a.signal?.aborted)throw r(`cancelled`,`abort-signal`);return m(i,o,n)}function m(e,t,n){let i=t.length;return r(`timeout`,`toolchain-probe-stalled`,{command:e,attemptTimeoutsMs:[...t],hint:`${e} did not answer within ${t[i-1]}ms on ${i} attempt(s). A toolchain probe that cannot answer is a host condition rather than a toolchain defect: run \`${e}\` by hand until it answers, then retry.`},n)}function h(e,t,n,r,i){return e.run(t,n,{allowFailure:!0,signal:r.signal,timeoutMs:i})}const g=`manifest.json`;async function _(e){let{host:t,deadline:r,cacheRoot:i,binaryFilename:c}=e,l=o(`sha256`).update(JSON.stringify(e.keyInputs)).digest(`hex`).slice(0,32),u=s.join(i,l);return await n({acquire:()=>t.acquireLock(s.join(i,`${l}.lock`),{deadline:r,description:e.lockDescription}),task:async()=>{let n=await v(t,u,c,l,r);if(n)return{path:n,cacheKey:l};a(r,`native-build-deadline`),t.exists(u)&&await t.remove(u),a(r,`native-build-deadline`),await t.ensureDirectory(i);let o=s.join(i,`.${l}.${t.processId()}.tmp`);a(r,`native-build-deadline`),await t.remove(o);try{a(r,`native-build-deadline`),await t.ensureDirectory(o);let n=s.join(o,c);await e.build(n),a(r,`native-build-deadline`),await t.chmod(n,493);let i=await b(t,n),d={...e.keyInputs,cacheKey:l,binarySha256:i};return await t.writeText(s.join(o,g),`${JSON.stringify(d,null,2)}\n`),a(r,`native-build-deadline`),await t.rename(o,u),{path:s.join(u,c),cacheKey:l}}catch(e){throw await t.remove(o),e}}})}async function v(e,t,n,r,i){let o=s.join(t,n),c=s.join(t,g);if(!(!e.exists(o)||!e.exists(c)))try{let t=JSON.parse(await e.readText(c));return t.cacheKey!==r||typeof t.binarySha256!=`string`?void 0:(a(i,`native-cache-hash-deadline`),await b(e,o)===t.binarySha256?o:void 0)}catch(e){if(y(e))throw e;return}}function y(e){return e instanceof i&&(e.buildFailureKind===`cancelled`||e.buildFailureKind===`timeout`)}async function b(e,t){return o(`sha256`).update(await e.readBinary(t)).digest(`hex`)}async function x(e,t,n,i){let c=o(`sha256`);for(let o of n){let n=s.join(t,o);if(a(i,`native-source-fingerprint-deadline`),!e.exists(n))throw r(`unsupported`,`native-source-missing`,{filePath:n});c.update(o),c.update(`\0`),c.update(await e.readBinary(n)),c.update(`\0`)}return c.digest(`hex`)}async function S(e){let n=Math.min(e.budgetMs,a(e.deadline,`native-build-deadline`));try{return await e.host.run(`xcrun`,[...e.argv],{signal:e.deadline.signal,timeoutMs:n,allowFailure:!0})}catch(i){throw t(i)?r(`timeout`,`native-build-stalled`,{timeoutMs:n,hint:`The Simulator SDK toolchain did not answer within ${n}ms, which stopped the ${e.label} build before clang reported anything. Run \`xcrun --sdk iphonesimulator clang --version\` by hand until it answers, then retry.`},i):i}}export{l as a,c as i,S as n,x as r,_ as t};