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 • 2.74 kB
JavaScript
import{v as e}from"./sdk-contracts.js";import{g as t}from"./sdk-batch-runner.js";import{a as n,i as r}from"./manifest.js";import i from"node:fs";import a from"node:path";const o=/^[a-z][a-z0-9-]*$/;function s(e){return typeof e==`string`&&o.test(e)}const c=`./agent-device`;function l(e){let t=u(e),i=f(t.exports,c);if(i.status===`resolved`)return{kind:`v2`,file:n(e,i.target)};if(i.status===`blocked`)throw y(e,c);if(t.agentDevicePlugin!==void 0)return{kind:`v1`,manifest:r(e)};let o=f(t.exports,`.`);if(o.status===`blocked`)throw y(e,`.`);let s=o.status===`resolved`?o.target:(t.exports===void 0||t.exports===null)&&typeof t.main==`string`?`./${a.posix.normalize(t.main)}`:void 0;if(s===void 0)throw b(e,c);return{kind:`v2`,file:n(e,s)}}function u(n){try{let e=JSON.parse(i.readFileSync(a.join(n,`package.json`),`utf8`));if(t(e))return e}catch(t){throw new e(`INVALID_ARGS`,`Cannot read plugin manifest: ${n}`,{reason:`plugin_load_failed`},t)}throw new e(`INVALID_ARGS`,`Cannot read plugin manifest: ${n}`,{reason:`plugin_load_failed`})}const d=new Set([`node`,`import`,`module-sync`,`node-addons`,`default`]);function f(e,t){if(e==null)return{status:`absent`};if(!m(e))return t===`.`?p(g(e,void 0)):{status:`absent`};if(Object.hasOwn(e,t)&&!t.includes(`*`))return p(g(e[t],void 0));let n=h(Object.keys(e),t);return n?p(g(e[n.key],n.captured)):{status:`absent`}}function p(e){return typeof e==`string`?{status:`resolved`,target:e}:{status:`blocked`}}function m(e){return t(e)&&Object.keys(e).some(e=>e.startsWith(`.`))}function h(e,t){let n;for(let r of e){let e=r.indexOf(`*`);if(e===-1||e!==r.lastIndexOf(`*`))continue;let i=r.slice(0,e),a=r.slice(e+1);t.startsWith(i)&&t!==i&&t.length>=r.length&&t.endsWith(a)&&(!n||i.length>n.base.length||i.length===n.base.length&&r.length>n.key.length)&&(n={key:r,base:i,trailer:a})}return n?{key:n.key,captured:t.slice(n.base.length,t.length-n.trailer.length)}:void 0}function g(e,n){if(typeof e==`string`)return _(e,n);if(e===null)return null;if(Array.isArray(e))return v(e.map(e=>()=>g(e,n)));if(t(e))return v(Object.entries(e).filter(([e])=>d.has(e)).map(([,e])=>()=>g(e,n)))}function _(e,t){if(e.startsWith(`./`))return t===void 0?e:e.replaceAll(`*`,t)}function v(e){for(let t of e){let e=t();if(e!==void 0)return e}}function y(t,n){return new e(`INVALID_ARGS`,`Package exports ${n} without an import target`,{reason:`incompatible_plugin`,directory:t,hint:`Ask the plugin author for a release whose ${n} export has an import condition.`})}function b(t,n){return new e(`INVALID_ARGS`,`Package does not export a provider plugin`,{reason:`incompatible_plugin`,directory:t,hint:`Install a plugin release that exports ${n}.`})}export{l as discoverPluginEntry,s as isProviderId,u as readPackageJson};