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.

2 lines • 11.3 kB
import{v as e}from"./sdk-contracts.js";import{T as t,V as n,y as r,z as i}from"./sdk-device.js";import{O as a,_ as o,f as s,j as c,y as l}from"./host2.js";import{n as ee}from"./runner-toolchain-identity.js";import u from"node:crypto";import d from"node:fs";import te from"node:os";import f from"node:path";const p=[`SnapshotBridgeRequest.h`,`SnapshotBridgeRequest.m`,`SnapshotBridgeRuntime.h`,`SnapshotBridgeRuntime.m`,`SnapshotBridgeCapture.h`,`SnapshotBridgeCapture.m`,`SnapshotBridgeSurfaceHosts.h`,`SnapshotBridgeSurfaceHosts.m`,`ForegroundApplicationGeometry.h`],m={iOS:{sdkName:{simulator:`iphonesimulator`,device:`iphoneos`},derivedBaseName:{simulator:`ios-simulator`,device:`ios-device`},xctestrunHints:{simulator:{preferred:[`iphonesimulator`],disallowed:[`iphoneos`,`appletvos`,`appletvsimulator`,`macos`]},device:{preferred:[`iphoneos`],disallowed:[`iphonesimulator`,`appletvos`,`appletvsimulator`,`macos`]}}},tvOS:{sdkName:{simulator:`appletvsimulator`,device:`appletvos`},derivedBaseName:{simulator:`tvos-simulator`,device:`tvos-device`},xctestrunHints:{simulator:{preferred:[`appletvsimulator`],disallowed:[`appletvos`,`iphoneos`,`iphonesimulator`,`macos`]},device:{preferred:[`appletvos`],disallowed:[`appletvsimulator`,`iphoneos`,`iphonesimulator`,`macos`]}}},macOS:{sdkName:{simulator:`macosx`,device:`macosx`},derivedBaseName:{simulator:`macos`,device:`macos`},xctestrunHints:{simulator:{preferred:[`macos`],disallowed:[`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`]},device:{preferred:[`macos`],disallowed:[`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`]}}},visionOS:{sdkName:{simulator:`xrsimulator`,device:`xros`},derivedBaseName:{simulator:`visionos-simulator`,device:`visionos-device`},xctestrunHints:{simulator:{preferred:[`xrsimulator`],disallowed:[`xros`,`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`,`macos`]},device:{preferred:[`xros`],disallowed:[`xrsimulator`,`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`,`macos`]}}},watchOS:{sdkName:{simulator:`watchsimulator`,device:`watchos`},derivedBaseName:{simulator:`watchos-simulator`,device:`watchos-device`},xctestrunHints:{simulator:{preferred:[`watchsimulator`],disallowed:[`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`,`macos`]},device:{preferred:[`watchos`],disallowed:[`watchsimulator`,`iphoneos`,`iphonesimulator`,`appletvos`,`appletvsimulator`,`macos`]}}}};function h(e){let t=e.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH?.trim();return t?f.resolve(t):void 0}function g(n){if(!r(n.platform))throw new e(`UNSUPPORTED_PLATFORM`,`Unsupported platform for Apple runner: ${n.platform}`);return t(n)?`macOS`:i(n.target,n.appleOs)}function ne(e){if(!r(e.platform))return{handoff:!1,reason:`non_apple_target`};if(e.kind===`simulator`)return{handoff:!0,lane:`simulator`};if(t(e))return{handoff:!1,reason:`macos_host`};let i=n(e);return i!==`ios`&&i!==`ipados`?{handoff:!1,reason:`physical_non_ios_os`}:e.iosPhysicalDeviceBackend===`xctest`?{handoff:!1,reason:`xctest_backend`}:{handoff:!0,lane:`physical_coredevice`}}function re(e,t){return m[e].sdkName[b(t)]}function _(e){return m[g(e)].derivedBaseName[b(e.kind)]}function v(e){return m[g(e)].xctestrunHints[b(e.kind)]}function y(e){let t=g(e);return t===`macOS`?`platform=macOS,arch=${x()}`:e.kind===`simulator`?`platform=${t} Simulator,id=${e.id}`:`platform=${t},id=${e.id}`}function ie(e){let t=g(e);return t===`macOS`?`platform=macOS,arch=${x()}`:e.kind===`simulator`?`platform=${t} Simulator,id=${e.id}`:`generic/platform=${t}`}function ae(e){let t=g(e);return t===`macOS`?`platform=macOS,arch=${x()}`:e.kind===`simulator`?`generic/platform=${t} Simulator`:`generic/platform=${t}`}function b(e){return e===`simulator`?`simulator`:`device`}function x(){return a()===`arm64`?`arm64`:`x86_64`}const oe=f.join(`apple`,`runner`,`AgentDeviceRunner`),se=f.join(`dist`,`apple`,`runner`,`AgentDeviceRunner`),ce=f.join(`apple`,`snapshot-presentation`),le=f.join(`dist`,`apple`,`snapshot-presentation`),ue=p.map(e=>f.join(`apple`,`snapshot-bridge`,e));function S(e){let t=f.join(e,oe);return d.existsSync(t)?t:f.join(e,se)}function de(e){return f.join(S(e),`AgentDeviceRunner.xcodeproj`)}function fe(e){let t=f.join(e,ce);return d.existsSync(t)?t:f.join(e,le)}const pe=new Set([`.build`,`.swiftpm`,`xcuserdata`]),me=new Set([`.DS_Store`]),he=new Set([`.xcuserstate`]),ge=new Set([`.build`,`.swiftpm`,`SnapshotPresentationConformance`,`Tests`,`xcuserdata`]),C=new Map;function _e(e){let t=[{path:S(e),ignoredDirectoryNames:pe},{path:fe(e),ignoredDirectoryNames:ge}],n=w(t);for(let t of ue){let r=f.join(e,t);d.existsSync(r)&&n.push(r)}n.sort((e,t)=>e.localeCompare(t));let r=ve(e,n),i=JSON.stringify(t.map(({path:e})=>e)),a=C.get(i);if(a?.fileStatsFingerprint===r)return a.sourceFingerprint;let o=u.createHash(`sha256`);for(let t of n){let n=f.relative(e,t);o.update(n),o.update(`\0`),o.update(d.readFileSync(t)),o.update(`\0`)}let s=o.digest(`hex`);return C.set(i,{fileStatsFingerprint:r,sourceFingerprint:s}),s}function ve(e,t){let n=u.createHash(`sha256`);for(let r of t){let t=f.relative(e,r),i=d.statSync(r);n.update(t),n.update(`\0`),n.update(String(i.size)),n.update(`\0`),n.update(String(Math.trunc(i.mtimeMs))),n.update(`\0`)}return n.digest(`hex`)}function w(e){return[...new Set(e.flatMap(({path:e,ignoredDirectoryNames:t})=>T(e,t)))].sort((e,t)=>e.localeCompare(t))}function T(e,t){return d.existsSync(e)?E(e,t,!1):[]}function E(e,t,n){let r=[];for(let i of d.readdirSync(e,{withFileTypes:!0})){let a=f.join(e,i.name);if(i.isDirectory()){if(t.has(i.name))continue;let e=n||O(i.name);r.push(...E(a,t,e))}else if(i.isFile()){if(n?D(i.name):!A(i.name))continue;r.push(a)}}return r}function D(e){return me.has(e)||he.has(f.extname(e))}function O(e){return e.endsWith(`.xcodeproj`)||e.endsWith(`.xcworkspace`)}const k=new Set([`.jpg`,`.json`,`.png`,`.swift`,`.m`,`.h`,`.plist`,`.entitlements`,`.xctestplan`,`.xcconfig`,`.storyboard`,`.xib`,`.xcscheme`,`.xcworkspacedata`]);function A(e){return k.has(f.extname(e))}const j=f.join(te.homedir(),`.agent-device`,`apple-runner`),M=`.agent-device-runner-cache.json`,N=[`-IDEPackageSupportDisableManifestSandbox=1`,`-IDEPackageSupportDisablePluginExecutionSandbox=1`,`ENABLE_USER_SCRIPT_SANDBOXING=NO`],P=`$(inherited) -disable-sandbox -D AGENT_DEVICE_RUNNER_ISOLATION_CANARY`,F=`${P} -D AGENT_DEVICE_RUNNER_UNIT_TESTS`;function I(e){return e?.trim()??``}function L(e=o().env){return I(e.AGENT_DEVICE_IOS_BUNDLE_ID)||I(e.AGENT_DEVICE_IOS_RUNNER_APP_BUNDLE_ID)||`com.callstack.agentdevice.runner`}function R(e=o().env){return I(e.AGENT_DEVICE_IOS_RUNNER_TEST_BUNDLE_ID)||`${L(e)}.uitests`}function ye(e){let t=L(e),n=R(e);return Array.from(new Set([I(e.AGENT_DEVICE_IOS_RUNNER_CONTAINER_BUNDLE_ID),`${n}.xctrunner`,t].filter(e=>e.length>0)))}function be(e,t=s(),n){let r=g(e),{env:i}=o();return{schemaVersion:3,packageVersion:c(t),runnerSourceFingerprint:_e(t),...ee(re(r,e.kind),n?.signal),platformName:r,deviceKind:e.kind,target:e.target??`mobile`,buildDestinationFamily:ae(e),runnerBundleBuildSettings:q(i),runnerSigningBuildSettings:K(i,e.kind===`device`,e),runnerPerformanceBuildSettings:J(),runnerArchBuildSettings:Y(i),runnerSandboxBuildArgs:X(i)}}function xe(e,t){return h(o().env)??z(e,t)}function z(e,t){return f.join(Se(e),B(t))}function Se(e){return f.join(j,`derived`,_(e))}function B(e){return`cache-${u.createHash(`sha256`).update(W(V(e))).digest(`hex`).slice(0,16)}`}function V(e){let{artifacts:t,packageVersion:n,...r}=e;return r}function Ce(e,t){let n=V(e),r=V(t);return[...new Set([...Object.keys(n),...Object.keys(r)])].sort((e,t)=>e.localeCompare(t)).flatMap(e=>{let t=H(n[e]),i=H(r[e]);return t===i?[]:[{key:e,expected:U(t),actual:U(i)}]})}function H(e){return e===void 0?`(absent)`:W(e)}function U(e){return e.length<=300?e:`${e.slice(0,149)}…${e.slice(-149)}`}function W(e){return JSON.stringify(G(e))}function G(e){return Array.isArray(e)?e.map(e=>G(e)):!e||typeof e!=`object`?e:Object.fromEntries(Object.entries(e).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>[e,G(t)]))}function we(e){return t(e)||e.kind===`device`?`-maximum-concurrent-test-device-destinations`:`-maximum-concurrent-test-simulator-destinations`}function K(e=o().env,n=!1,r={platform:`apple`}){if(t(r))return[`CODE_SIGNING_ALLOWED=NO`,`CODE_SIGNING_REQUIRED=NO`,`CODE_SIGN_IDENTITY=`,`DEVELOPMENT_TEAM=`];if(!n)return[];let i=e.AGENT_DEVICE_IOS_TEAM_ID?.trim()||``,a=e.AGENT_DEVICE_IOS_SIGNING_IDENTITY?.trim()||``,s=e.AGENT_DEVICE_IOS_PROVISIONING_PROFILE?.trim()||``,c=[`CODE_SIGN_STYLE=${s?`Manual`:`Automatic`}`];return i&&c.push(`DEVELOPMENT_TEAM=${i}`),a&&c.push(`CODE_SIGN_IDENTITY=${a}`),s&&c.push(`PROVISIONING_PROFILE_SPECIFIER=${s}`),c}function q(e=o().env){let t=L(e),n=R(e);return[`AGENT_DEVICE_IOS_RUNNER_APP_BUNDLE_ID=${t}`,`AGENT_DEVICE_IOS_RUNNER_TEST_BUNDLE_ID=${n}`]}function J(){return[`COMPILER_INDEX_STORE_ENABLE=NO`,`ENABLE_CODE_COVERAGE=NO`,`ONLY_ACTIVE_ARCH=YES`,`ENABLE_PREVIEWS=NO`,`ENABLE_DEBUG_DYLIB=NO`]}function Y(e=o().env){let t=e.AGENT_DEVICE_XCUITEST_ARCHS?.trim();return t?[`ARCHS=${t}`]:[]}function Te(e){let t=f.join(e,`Build`,`Intermediates.noindex`);return[`SYMROOT=${f.join(e,`Build`,`Products`)}`,`OBJROOT=${t}`,`SHARED_PRECOMPS_DIR=${f.join(t,`PrecompiledHeaders`)}`]}function X(e=o().env){let t=`OTHER_SWIFT_FLAGS=${Ee(e)}`;return l(e.AGENT_DEVICE_XCUITEST_INCLUDE_UNIT_TESTS)?[...N,t,`OTHER_CFLAGS=$(inherited) -D AGENT_DEVICE_RUNNER_UNIT_TESTS`]:[...N,t]}function Ee(e){return l(e.AGENT_DEVICE_XCUITEST_INCLUDE_UNIT_TESTS)?F:P}const De=/^\s*Build settings from command line:\s*$/,Oe=/^\s+([A-Z][A-Z0-9_]*)\s*=\s*(.*)$/,Z=/^([A-Z][A-Z0-9_]*)=(.*)$/,Q=/^\s*Command line invocation:\s*$/;function ke(e){let t;try{t=d.readFileSync(e,`utf8`)}catch{return null}let n=t.split(` `),r=new Map,i=!1,a=``;for(let[e,t]of n.entries()){if(Q.test(t)){a=n[e+1]??``;continue}if(De.test(t)){i=!0;continue}if(!i)continue;let o=Oe.exec(t);if(!o)break;r.set(o[1],o[2].trimEnd())}return i?{settings:r,invocationLine:a}:null}function $(e){return[...e.runnerBundleBuildSettings,...e.runnerSigningBuildSettings,...e.runnerPerformanceBuildSettings,...e.runnerArchBuildSettings,...e.runnerSandboxBuildArgs]}function Ae(e){let t={};for(let n of $(e)){let e=Z.exec(n);e&&(t[e[1]]=e[2])}return t}function je(e,t){return $(e).filter(e=>!Z.test(e)).filter(e=>!t.includes(e)).map(e=>({key:`(invocation flag)`,expected:e,actual:`absent from the "Command line invocation:" line`}))}function Me(e,t){let n=ke(t);return n?[...Object.entries(Ae(e)).filter(([e,t])=>{let r=n.settings.get(e);return r===void 0?t!==``:r!==t}).map(([e,t])=>({key:e,expected:t,actual:n.settings.get(e)??`(absent)`})),...je(e,n.invocationLine)]:[{key:`(build log)`,expected:`a "Build settings from command line:" block`,actual:`missing or unreadable log`}]}function Ne(t,n){let r=Me(t,n);if(r.length!==0)throw new e(`COMMAND_FAILED`,`The Apple runner build did not use the settings its cache identity records`,{reason:`runner_build_settings_mismatch`,buildLogPath:n,differences:r,hint:`Align the build invocation with the runner cache identity resolvers, or rebuild without the cache.`})}export{y as C,h as S,v as T,K as _,be as a,p as b,Te as c,ye as d,xe as f,X as g,J as h,Ne as i,q as l,we as m,V as n,L as o,z as p,Ce as r,Y as s,M as t,B as u,W as v,ne as w,ie as x,de as y};