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 • 3.26 kB
import{r as e}from"./rolldown-runtime.js";import{v as t}from"./sdk-contracts.js";import"./command.js";import{r as n}from"./hdc.js";import{t as r}from"./snapshot3.js";var i=e({closeHarmonyApp:()=>g,listHarmonyApps:()=>l,openHarmonyApp:()=>h,parseHarmonyBundleList:()=>a,parseHarmonyIsSystemApp:()=>s,parseHarmonyLaunchTarget:()=>o});function a(e){return e.split(` `).map(e=>e.trim()).filter(e=>/^([a-zA-Z0-9_]+\.)+[a-zA-Z0-9_]+$/.test(e))}function o(e){let t=c(e);if(!t)return;let n=t.hapModuleInfos?.find(e=>e.mainElementName);if(n?.mainElementName)return{ability:n.mainElementName,module:n.moduleName}}function s(e){let t=c(e),n=t?.applicationInfo?.isSystemApp??t?.appInfo?.isSystemApp;return typeof n==`boolean`?n:void 0}function c(e){let t=e.indexOf(`{`);if(t!==-1)try{return JSON.parse(e.slice(t))}catch{return}}async function l(e,r,i={}){if(r===`all`)return u(a((await n(e,[`bm`,`dump`,`-a`],{timeoutMs:15e3,signal:i.signal})).stdout));let o=new AbortController,s=setTimeout(()=>o.abort(),6e4),c=f(i.signal,o.signal);try{return u(await d(e,a((await n(e,[`bm`,`dump`,`-a`],{timeoutMs:15e3,signal:c})).stdout),c))}catch(e){throw o.signal.aborted?new t(`COMMAND_FAILED`,`Timed out while classifying HarmonyOS application inventory`,{hint:`Use apps --all to list bundles without per-bundle classification, then retry after the device is responsive.`}):e}finally{clearTimeout(s)}}function u(e){return e.map(e=>({package:e,name:e.split(`.`).at(-1)??e}))}async function d(e,r,i){let a=Array(r.length),o=new AbortController,c=f(i,o.signal),l=0,u,d=Math.min(4,r.length),p=Array.from({length:d},async()=>{try{for(;;){c?.throwIfAborted();let i=l++;if(i>=r.length)return;let o=r[i];if(!o)return;let u=s((await n(e,[`bm`,`dump`,`-n`,o],{timeoutMs:15e3,signal:c})).stdout);if(u===void 0)throw new t(`COMMAND_FAILED`,`Could not determine whether ${o} is a system application`,{hint:`Use apps --all to list all bundles when Bundle Manager does not expose applicationInfo.isSystemApp.`});a[i]=u}}catch(e){throw!c?.aborted&&u===void 0&&(u=e),o.abort(),e}}),m=await Promise.allSettled(p);if(u!==void 0)throw u;let h=m.find(e=>e.status===`rejected`);if(h)throw h.reason;return r.filter((e,t)=>!a[t])}function f(...e){let t=e.filter(e=>!!e);if(t.length!==0)return t.length===1?t[0]:AbortSignal.any(t)}const p=/^[A-Za-z][A-Za-z0-9_.]*$/;function m(e){if(!p.test(e))throw new t(`INVALID_ARGS`,`Invalid HarmonyOS ability name: ${e}`,{reason:`invalid-harmony-ability-name`,ability:e,hint:`Pass --activity as the ability name from module.json5, such as EntryAbility.`})}async function h(e,i,a){a?.activity!==void 0&&m(a.activity);let s=a?.activity?{ability:a.activity}:o((await n(e,[`bm`,`dump`,`-n`,i],{signal:a?.signal,timeoutMs:15e3})).stdout);if(!s)throw new t(`COMMAND_FAILED`,`Could not determine a launchable ability for ${i}`,{hint:`Pass --activity with the HarmonyOS ability name, or check that the bundle is installed.`});let c=[`aa`,`start`,`-a`,s.ability,`-b`,i];s.module&&c.push(`-m`,s.module);let l=await n(e,c,{signal:a?.signal});if(!l.stdout.includes(`start ability successfully`))throw new t(`COMMAND_FAILED`,`Failed to start ${i}`,{details:{output:l.stdout.trim()}});r(e)}async function g(e,t){await n(e,[`aa`,`force-stop`,t]),r(e)}export{a as n,o as r,i as t};