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 • 5.56 kB
import{B as e,v as t}from"./sdk-contracts.js";import{g as n}from"./sdk-batch-runner.js";import{t as r}from"./plugin-inventory.js";import{n as i,r as a,t as o}from"./manifest.js";import{isProviderId as s}from"./discovery.js";import{t as c}from"./host5.js";function l(t){let n=e(t),r=n.details?.reason;return{code:n.code,message:n.message,...typeof r==`string`?{reason:r}:{},...n.hint?{hint:n.hint}:{}}}function u(e){if(!n(e))throw h(`Plugin factory must return a plugin object`);if(e.apiVersion!==2)throw new t(`INVALID_ARGS`,`Plugin does not declare a compatible provider contract`,{reason:`incompatible_plugin`,apiVersion:2,hint:`Install a plugin release supporting provider plugin apiVersion 2.`});if(!s(e.provider))throw h(`Plugin provider must be a lowercase id such as "example"`);if(o.includes(e.provider))throw new t(`INVALID_ARGS`,`Provider id is reserved for routing: ${e.provider}`,{reason:`reserved_provider`,provider:e.provider});if(d(e.credentials),e.connection!==void 0&&f(e.connection),typeof e.setup!=`function`)throw h(`Plugin must provide setup(host)`);return e}function d(e){if(!n(e)||!Array.isArray(e.variables))throw h(`Plugin credentials must declare variables`);if(i(e.variables),e.byBackend!==void 0){if(!n(e.byBackend))throw h(`Plugin credentials byBackend must map a lease backend to variables`);for(let t of Object.values(e.byBackend)){if(!Array.isArray(t))throw h(`Plugin credentials byBackend must map a lease backend to variables`);i(t)}}}function f(e){if(!n(e)||e.capabilities===void 0||typeof e.resolve!=`function`||typeof e.verify!=`function`)throw h(`Plugin connection must provide capabilities, resolve and verify`);a(e.capabilities)}function p(e,t,n){let r=n?.owner;if(t?.provider!==e||typeof t.shutdown!=`function`||!m(t)||r?.kind!==`provider-runtime`||r.provider!==e||typeof r.instance!=`string`||r.instance.trim().length===0||typeof n?.loadRuntime!=`function`)throw h(`Plugin runtime owner does not match its provider: ${e}`)}function m(e){return e.leaseLifecycle!==null&&typeof e.leaseLifecycle==`object`&&[`ownsDevice`,`getInteractor`,`deviceInventoryProvider`].every(t=>typeof e[t]==`function`)}function h(e){return new t(`INVALID_ARGS`,e,{reason:`invalid_plugin`})}function g(e){return e.map(e=>({source:`builtin`,provider:e,dormant:!0,load:()=>void 0}))}async function _(e){let n=[],r=new Set;for(let i of e){let e={candidate:i,provider:i.provider};if(n.push(e),i.dormant){i.provider!==void 0&&r.add(i.provider);continue}try{let t=await i.load();e.provider=E(t)??i.provider,e.plugin=u(t)}catch(t){e.failure=w(t,`plugin_load_failed`)}e.provider===void 0||o.includes(e.provider)||(r.has(e.provider)?(e.plugin=void 0,e.failure??=new t(`INVALID_ARGS`,`Duplicate provider plugin: ${e.provider}`,{reason:`duplicate_provider`,provider:e.provider})):r.add(e.provider))}return Object.freeze({connections:new Map(n.flatMap(({plugin:e,candidate:t})=>e?.connection?[[e.provider,y(e.connection,t.hostOptions)]]:[])),claimedProviders:[...r],inventory:()=>n.flatMap(T),slots:n})}async function v(e,t,n){for(let r of e.slots)if(!(!r.plugin||r.failure!==void 0))try{await b(r,r.plugin,t,n)}catch(e){r.failure=w(e,`plugin_setup_failed`),r.registration=void 0}let r=e.slots.flatMap(({registration:e})=>e?[e]:[]);return Object.freeze({registrations:r,claimedProviders:e.claimedProviders,inventory:e.inventory(),close:k(async()=>await Promise.all(r.map(D)))})}function y(e,t){return{capabilities:e.capabilities,resolve:n=>e.resolve({...n,host:c(n.env,t)}),verify:n=>e.verify({...n,host:c(n.env,t)})}}async function b(e,t,n,r){let i=c(n,e.candidate.hostOptions),a=await t.setup(i);try{let{runtime:n,platformModule:o}=await x(t,a,i.clientVersion,r),s=a.dispose?.bind(a);e.registration={provider:t.provider,source:e.candidate.source,runtime:n,platformModule:o,...s?{dispose:s}:{}}}catch(e){throw await C(a),e}}async function x(e,t,r,i){if(!n(t))throw h(`Plugin setup must return a runtime: ${e.provider}`);if(S(t)){if(t.webDriver?.provider!==e.provider)throw h(`WebDriver plugin provider does not match its declaration: ${e.provider}`);return await i.adaptWebDriver(t.webDriver,{clientVersion:r})}let{runtime:a,platformModule:o}=t;return p(e.provider,a,o),{runtime:a,platformModule:o}}function S(e){return`webDriver`in e}async function C(e){if(!n(e))return;let t=e.runtime;try{typeof t?.shutdown==`function`&&await t.shutdown()}catch{}try{typeof e.dispose==`function`&&await e.dispose()}catch{}}function w(e,n){return e instanceof t?typeof e.details?.reason==`string`?e:new t(e.code,e.message,{...e.details,reason:n},e):new t(`COMMAND_FAILED`,e instanceof Error?e.message:String(e),{reason:n},e)}function T(e){let{candidate:t,plugin:n}=e;if(t.dormant)return[];let i={...e.provider===void 0?{}:{provider:e.provider},source:t.source,...t.package===void 0?{}:{package:t.package},...t.version===void 0?{}:{version:t.version}};return e.failure!==void 0||!n?[{...i,state:`failed`,error:l(e.failure),credentials:r}]:[{...i,state:`active`,credentials:{variables:[...n.credentials.variables],byBackend:Object.fromEntries(Object.entries(n.credentials.byBackend??{}).map(([e,t])=>[e,[...t]]))}}]}function E(e){return n(e)&&s(e.provider)?e.provider:void 0}async function D({provider:e,source:t,runtime:n,dispose:r}){return{provider:e,source:t,shutdown:await O(async()=>await n.shutdown()),dispose:await O(async()=>await r?.())}}async function O(e){try{return await e(),{status:`ok`}}catch(e){return{status:`failed`,error:l(e)}}}function k(e){let t;return()=>t??=e()}export{g as claimProviderIds,_ as resolveProviderPlugins,v as startProviderRegistry};