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.

1 lines • 3.38 kB
import{X as e,v as t}from"./sdk-contracts.js";import{r as n,t as r}from"./png.js";import{n as i,r as a,t as o}from"./png-worker-contract.js";import s from"node:fs";import c from"node:path";import{fileURLToPath as l}from"node:url";import{Worker as u}from"node:worker_threads";function d(e,t){try{let n=l(e),r=c.extname(n)||`.js`;return[c.join(c.dirname(n),`${t}${r}`),c.join(c.dirname(n),`internal`,`${t}${r}`)].find(e=>s.existsSync(e))??null}catch{return null}}var f=class extends Error{};let p=null,m;const h=new Set;let g=0;const _=new Map;function v(e){if(!m){m=e;for(let t of h)t(e)}}function y(n){let r=_.get(n.id);r&&(_.delete(n.id),S(),n.ok?r.resolve(n.result):r.reject(new t(e(n.error.code),n.error.message,n.error.details)))}function b(e,t){p===e&&(v(t),p=null,e.terminate().catch(()=>{}),x(new f(`PNG worker failed: ${t.kind}`)))}function x(e){let t=[..._.values()];_.clear();for(let n of t)n.reject(e)}function S(){p&&(_.size>0?p.ref():p.unref())}function C(){if(m)return null;if(p)return p;let e=d(import.meta.url,`png-worker`);if(!e)return v({kind:`entry-not-found`}),null;try{let t=new u(e,{env:{},execArgv:e.endsWith(`.ts`)?[`--experimental-strip-types`]:[]});return t.on(`message`,y),t.on(`error`,e=>{b(t,{kind:`worker-error`,detail:e.message})}),t.on(`exit`,e=>{b(t,{kind:`worker-exited`,detail:`exit code ${e}`})}),t.unref(),p=t,t}catch(e){return v({kind:`spawn-failed`,detail:e instanceof Error?e.message:String(e)}),null}}function w(e){let t=C();if(!t)return Promise.reject(new f(`PNG worker is unavailable`));g+=1;let n=g;return new Promise((r,i)=>{_.set(n,{resolve:r,reject:i}),S();try{t.postMessage({...e,id:n})}catch(e){_.delete(n),S(),i(new f(`failed to post job to PNG worker: ${e instanceof Error?e.message:String(e)}`))}})}async function T(e,t){try{return await w(e)}catch(e){if(e instanceof f)return await t();throw e}}let E=0;function D(e){h.add(e),m?e(m):C(),E+=1;let t=!1;return async()=>{t||(t=!0,h.delete(e),--E,E===0&&await O())}}async function O(){let e=p;p=null,x(new f(`PNG worker terminated`)),e&&await e.terminate()}async function k(e,t){return j(await T({kind:`decode`,png:e,label:t},()=>{let n=r(e,t);return{kind:`decode`,width:n.width,height:n.height,data:n.data}}))}async function A(e,t,n){return j(await T({kind:`decode-image`,image:e,label:t,maxPixels:n},async()=>{let{decodeScreenshotImage:r}=await import(`./screenshot-image.js`),i=r(e,t,n);return{kind:`decode-image`,width:i.width,height:i.height,data:i.data}}))}function j(e){let t=new n.PNG({width:e.width,height:e.height});return t.data=o(e.data),t}async function M(e){let t=await T({kind:`encode`,width:e.width,height:e.height,data:e.data},async()=>{let{encodePngPixels:t}=await import(`./png-encode.js`).then(e=>e.n);return{kind:`encode`,png:t(e.data,e.width,e.height,4)}});return o(t.png)}async function N(e,t,n){let r=await T({kind:`crop`,png:e,label:n,box:t},async()=>{let{cropPngBytes:r}=await import(`./png-crop-bytes.js`).then(e=>e.n);return{kind:`crop`,png:r(e,t,n)}});return r.png===null?null:o(r.png)}async function P(e,t,n){let{kind:a,...o}=await T({kind:`rgb-difference`,firstPng:e,secondPng:t,label:n},()=>({kind:`rgb-difference`,...i(r(e,n),r(t,n))}));return o}async function F(e){let{kind:t,...n}=await T({kind:`diff-pixels`,...e},()=>({kind:`diff-pixels`,...a(e)}));return{...n,diffData:o(n.diffData)}}export{A as a,k as i,F as n,M as o,N as r,D as s,P as t};