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.
4 lines • 5.12 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{J as t,X as n,et as r,rt as i,ut as a}from"./sdk-io.js";import"./command.js";import{t as o}from"./scoped-provider.js";import{f as s,u as c}from"./host-file.js";import l from"node:path";function u(e,t){return t===`user-installed`?e.filter(e=>!e.bundleId.startsWith(`com.apple.`)):e}function d(t,n){return{openBundle:async(e,n,r)=>{let{resolveListedRunningBundle:i}=await import(`./launch-services-registration.js`),a=await i(t,e);await t(`open`,[...f(r),...a?m(a,n):p(e,n)])},openTarget:async(e,n)=>{await t(`open`,[...f(n),e])},readClipboard:async()=>r(await t(`pbpaste`,[],{allowFailure:!0}),`Failed to read macOS clipboard`).stdout.replaceAll(`\r
`,`
`).replace(/\n$/,``),writeClipboard:async e=>{r(await t(`pbcopy`,[],{allowFailure:!0,stdin:e}),`Failed to write macOS clipboard`)},readDarkMode:async()=>{let n=r(await t(`osascript`,[`-e`,`tell application "System Events" to tell appearance preferences to get dark mode`],{allowFailure:!0}),`Failed to read macOS appearance`),i=n.stdout.trim().toLowerCase();if(i===`true`)return!0;if(i===`false`)return!1;throw new e(`COMMAND_FAILED`,`Unable to determine current macOS appearance from osascript output: ${n.stdout.trim()}`)},setDarkMode:async e=>{r(await t(`osascript`,[`-e`,`tell application "System Events" to tell appearance preferences to set dark mode to ${e?`true`:`false`}`],{allowFailure:!0}),`Failed to set macOS appearance`)},listApps:async e=>await h(t,n,e)}}function f(e){return e?.background?[`-g`]:[]}function p(e,t){let n=[`-b`,e];return t&&n.push(t),n}function m(e,t){return t?[`-a`,e,t]:[e]}async function h(e,t,n){let r=[`/Applications`,`/System/Applications`,l.join(s(),`Applications`)],i=new Set;for(let t of r){if(!(await c(t).catch(()=>null))?.isDirectory())continue;let n=await e(`find`,[t,`-maxdepth`,`4`,`-type`,`d`,`-name`,`*.app`],{allowFailure:!0});if(n.exitCode===0)for(let e of n.stdout.split(`
`)){let t=e.trim();t&&i.add(t)}}return u((await Promise.all(Array.from(i).map(async e=>{let n=await g(e,t).catch(()=>({})),r=n.bundleId;return r?{bundleId:r,name:n.appName??l.basename(e,`.app`)}:null}))).filter(e=>e!==null).sort((e,t)=>e.name.localeCompare(t.name)),n)}async function g(e,t){for(let n of[l.join(e,`Contents`,`Info.plist`),l.join(e,`Info.plist`)]){let e=await t(n),r=_(e,`CFBundleIdentifier`),i=_(e,`CFBundleDisplayName`),a=_(e,`CFBundleName`);if(r||i||a)return{bundleId:r,appName:i??a}}return{}}function _(e,t){let n=e?.[t];return typeof n==`string`&&n.trim()?n.trim():void 0}function v(e){return Object.freeze([`simctl`,...e])}async function y(e,t,n){return await e(`xcrun`,[...v(t)],n)}const b=/^An error was encountered processing the command \(domain=([^,]+), code=(-?\d+)\):[\t ]*(?:\r?\n|$)/m,x=`simulator-screenshot-timeout`;function S(e){let t=e[0]===`--set`?e.slice(2):e;return t[0]===`io`&&t[2]===`screenshot`}async function C(e,t){let n=[...v(e)];if(t?.allowFailure||!S(e))return await i(`xcrun`,n,t);let a=await i(`xcrun`,n,{...t,allowFailure:!0}),o=b.exec(a.stderr),s=o?.[1]===`NSPOSIXErrorDomain`&&Number(o?.[2])===60;return r(a,`xcrun exited with code ${a.exitCode}`,{cmd:`xcrun`,args:n,...s?{reason:x}:{}})}function w(e){return Object.freeze(e.slice(1))}const T=15e3,E=O(),D=o(E,O);function O(e={}){let t=F(e.runCommand??i),r={run:e.simctl?I(e.simctl.run):e.runCommand?async(e,n)=>await y(t,e,n):C},o=e.plist??{readJson:async(e,n)=>await L(t,e,n)},s={simctl:r,runCommand:t};return{...e,simctl:r,runCommand:t,...e.macosHelper?{macosHelper:{run:I(e.macosHelper.run)}}:{},simulatorFiles:e.simulatorFiles??{copyToHost:async e=>await(await import(`./local-simulator-files.js`)).copySimulatorFileToHost(r,e),openContainer:async e=>await(await import(`./local-simulator-files.js`)).openSimulatorContainer(r,e),stageFile:async(e,t)=>(t?.throwIfAborted(),{path:e,[Symbol.asyncDispose]:async()=>{}})},simulatorApps:e.simulatorApps??{list:async(e,t)=>await(await import(`./local-simulator-apps.js`)).readSimulatorAppMetadata(s,e,t),readInfoPlist:async e=>await(await import(`./local-simulator-apps.js`)).readSimulatorAppInfoPlist(s,e)},devicectl:{run:e.devicectl?I(e.devicectl.run):async(e,n)=>await t(`xcrun`,[`devicectl`,...e],n)},plist:o,macosHost:e.macosHost??d(t,async e=>await o.readJson(e)),whichCommand:e.whichCommand??(async e=>await a(e,n().env))}}function k(e){return D.resolve(e)}async function A(e,t){return await D.run(e,t)}function j(){return D.hasScope()}async function M(e,t,n){return await k().runCommand(e,t,n)}async function N(e,t){let n=k();if(e[0]===`simctl`)return await n.simctl.run(w(e),t);let[r,...i]=e;return r===`devicectl`?await n.devicectl.run(i,t):await M(`xcrun`,[...e],t)}async function P(e,t){return await k().plist?.readJson(e,t)??null}function F(e){return async(n,r,i)=>t(await e(n,r,i))}function I(e){return async(n,r)=>t(await e(n,r))}async function L(e,t,n){try{let r=await e(`plutil`,[`-convert`,`json`,`-o`,`-`,t],{allowFailure:!0,timeoutMs:T,signal:n});return r.exitCode!==0||!r.stdout.trim()?null:JSON.parse(r.stdout)}catch{return n?.throwIfAborted(),null}}export{P as a,N as c,j as i,A as l,x as n,k as o,O as r,M as s,T as t,u};