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 • 5.15 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{n as t}from"./app-inventory.js";import"./device.js";import{t as n}from"./ios-client.js";import{o as r,r as i}from"./android.js";import{t as a}from"./request-cancellation.js";import o from"node:fs";import s from"node:path";import{setTimeout as c}from"node:timers/promises";async function l(e,t){let r=await n({apiUrl:e.apiUrl,token:e.token,logLevel:`warn`});try{let n=await t.ios.createRunner({...e,client:r});return{...e,platform:`ios`,client:r,runner:n,dependencies:t}}catch(e){throw r.disconnect(),e}}async function u(e,t,n,r,i){r?.throwIfAborted();let{LimrunSyncCache:s}=await import(`./sync-cache.js`),c=e.syncCache??=new s,l=((await o.promises.stat(t)).isDirectory()?await e.dependencies.ios.readBundleAppName(t):void 0)??h(t),u=(await f(e,async()=>{r?.throwIfAborted();let o=await a(i,c.run(t,`ios`,r=>e.client.syncApp(t,{basisCacheDir:r,install:!0,watch:!1,launchMode:n?.relaunch?`RelaunchIfRunning`:`None`}),r));return r?.throwIfAborted(),o.installedBundleId},n,r,i)).appId;return{...u?{bundleId:u,launchTarget:u}:{},...l?{appName:l}:{}}}async function d(e,t,n,r,i){return await f(e,async()=>(await a(i,e.client.installApp(t,{launchMode:n?.relaunch?`RelaunchIfRunning`:void 0}),r)).bundleId,n,r,i)}async function f(t,n,i,o,s){o?.throwIfAborted();let l=await a(s,t.client.listApps(),o).catch(e=>{if(o?.aborted)throw e}),u=r(await n()),d=r(i?.appIdentifierHint),f=[];for(let e of g){e>0&&await c(e,void 0,{signal:o}),f=await a(s,t.client.listApps(),o);let n=_({resultBundleId:u,requestedBundleId:d,beforeInstallApps:l,afterInstallApps:f});if(n)return{appId:n}}throw new e(`COMMAND_FAILED`,`Limrun iOS app installation could not be verified.`,{resultBundleId:u,requestedBundleId:d,installedUserApps:f.filter(y).map(e=>e.bundleId).sort()})}function p(e,t){let n={...t,get appBundleId(){return t?.appBundleId??e.appBundleId}},r=e.runner.createInteractor(n),i=new m(e,r,n);return{...r,open:i.open.bind(i),openDevice:i.openDevice.bind(i),close:i.close.bind(i),setSetting:i.setSetting.bind(i)}}var m=class{session;signal;context;shared;constructor(e,t,n){this.session=e,this.shared=t,this.context=n,this.signal=n.signal}async open(e,t){let n=t?.appBundleId??(await this.session.runner.applicationTools.resolveOpenTarget(this.session.device,{target:e,currentAppBundleId:this.context.appBundleId,surface:`app`})).appBundleId;await this.shared.open(e,{...t,appBundleId:n}),this.session.appBundleId=n}async openDevice(){}async close(e){let{appBundleId:t}=await this.session.runner.applicationTools.resolveOpenTarget(this.session.device,{target:e,surface:`app`});await this.shared.close(t??e),t!==void 0&&this.session.appBundleId===t&&(await this.session.runner.resetTarget(this.signal),this.session.appBundleId=void 0)}async setSetting(t,n,r,i){if(t.toLowerCase()===`reset-keychain`)throw new e(`UNSUPPORTED_OPERATION`,`Limrun does not expose keychain reset.`);return await this.shared.setSetting(t,n,r,i)}};function h(e){return s.basename(e).replace(/\.(?:app|ipa|apk|aab|zip)$/i,``)||void 0}const g=[0,250];function _(e){let t=new Set(e.afterInstallApps.map(e=>e.bundleId));return(e.resultBundleId&&t.has(e.resultBundleId)?e.resultBundleId:void 0)??(e.requestedBundleId&&t.has(e.requestedBundleId)?e.requestedBundleId:void 0)??v(e.beforeInstallApps,e.afterInstallApps)}function v(e,t){if(!e)return;let n=new Set(e.map(e=>e.bundleId)),r=t.filter(e=>y(e)&&!n.has(e.bundleId));return r.length===1?r[0]?.bundleId:void 0}function y(e){return!e.bundleId.startsWith(`com.apple.`)&&!e.installType.toLowerCase().includes(`system`)}function b(e){return e.platform===`android`?x(e):S(e)}function x(e){let n=e.adbProvider.exec;return{platform:`android`,device:e.device,interactor:i(e),adb:e.adbProvider,listApps:async r=>await e.dependencies.android.listApps(n,t(r)),getForegroundApp:async t=>await e.dependencies.android.getForegroundApp(e.device,n,t),pressKey:async(t,n)=>{await e.client.pressKey(t,n)},getKeyboardState:async()=>await e.dependencies.android.getKeyboardState(n),dismissKeyboard:async()=>await e.dependencies.android.dismissKeyboard(n),readLogs:async t=>await e.dependencies.android.readLogs(n,t),...C(e.client,e.token,e.dependencies.host),installRemoteApp:async t=>{await e.client.sendAsset(t)}}}function S(e){return{platform:`ios`,device:e.device,interactor:p(e),viewport:{width:e.client.deviceInfo.screenWidth,height:e.client.deviceInfo.screenHeight},listApps:async n=>(await e.client.listApps()).filter(e=>t(n)===`all`||y(e)).map(e=>({id:e.bundleId,name:e.name,installType:e.installType})).sort((e,t)=>e.id.localeCompare(t.id)),pressKey:async(t,n)=>{await e.client.pressKey(t,n)},readLogs:async(t,n)=>await e.client.appLogTail(t,n),...C(e.client,e.token,e.dependencies.host),installRemoteApp:async(t,n)=>await d(e,t,n),runSimctl:t=>e.client.simctl(t)}}function C(e,t,n){return{startRecording:async t=>{await e.startRecordingSession(t)},stopRecording:async t=>({downloadUrl:await e.stopRecordingSession(t)}),downloadRecording:async e=>{await n.downloadFile({url:e.downloadUrl,headers:{Authorization:`Bearer ${t}`},destinationPath:e.outPath,timeoutMs:6e4})}}}export{u as i,p as n,l as r,b as t};