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.99 kB
import{r as e}from"./rolldown-runtime.js";import{C as t,F as n,I as r,P as i,b as a,v as o,w as s}from"./sdk-contracts.js";import{n as c}from"./runtime-slots.js";import{AsyncLocalStorage as l}from"node:async_hooks";const u=Symbol(`requestCancel`),d=()=>c(u,()=>({canceledRequestIds:new Set,requestAbortControllers:new Map}),e=>{for(let t of e.requestAbortControllers.values())t.abort()}),f=()=>d().canceledRequestIds,p=()=>d().requestAbortControllers;function m(e,t){if(typeof e==`string`&&e.length>0)return e;let n=(typeof t==`string`?t:typeof t==`number`&&Number.isFinite(t)?String(t):`generated`).trim().replaceAll(/[^a-zA-Z0-9_-]/g,`_`).slice(0,32)||`generated`,r=Math.random().toString(36).slice(2,10);return`req:${n}:${process.pid}:${Date.now()}:${r}`}function h(e){if(e.size<=5e4)return;let t=0;for(let n of e){if(t>=1e4)break;e.delete(n),t++}}function g(e,n,r){if(!e)return;if(p().has(e))throw new o(`INVALID_ARGS`,`Request ID is already in flight: ${e}`,{requestId:e,reason:`duplicate_request_id`});let i=new AbortController,a=n?p().get(n):void 0;return i.parent=a,i.resultDeadlineMs=n?a?.resultDeadlineMs:r?.resultDeadlineMs,i.readOnly=n?a?.readOnly:r?.readOnly,p().set(e,i),f().has(e)&&i.abort(t()),{requestId:e,controller:i}}function _(e){e&&(h(f()),f().add(e),p().get(e)?.abort(t()))}function v(){for(let e of p().keys())_(e)}function y(e,t){e&&(t&&(t.requestId!==e||p().get(e)!==t.controller)||(f().delete(e),p().delete(e)))}function b(e){e&&y(e.requestId,e)}function x(e){return e?f().has(e)||S(e)?.aborted===!0:!1}function S(e){if(e)return p().get(e)?.signal}function C(e){let t=e?p().get(e):void 0;return t?w(t)?.deadlineMs:void 0}function w(e){let t=e.parent?w(e.parent):void 0;for(let n of e.executionDeadlines??[])(!t||n.deadlineMs<t.deadlineMs)&&(t=n);return t}function T(e){let t=e?p().get(e):void 0;t&&(D(t),t.signal.throwIfAborted())}function E(e){for(let t=e;t;t=t.parent)if(t.signal.aborted)return e.signal.aborted||(e.executionDeadlineCancellation=t.executionDeadlineCancellation,e.abort(t.signal.reason)),!0;return!1}function D(e){if(E(e))return;let t=w(e);t&&Date.now()>=t.deadlineMs&&t.expire(),E(e)}function O(e){return e?p().get(e)?.resultDeadlineMs:void 0}function k(e){let t=[C(e),O(e)].filter(e=>e!==void 0);return t.length>0?Math.min(...t):void 0}function A(e){if(!(!e||p().get(e)?.readOnly!==!0))return k(e)}function j(e){if(S(e)?.throwIfAborted(),x(e))throw t()}function M(e,t,i){let{hint:a,...c}=e instanceof o?e.details??{}:{};return s({...c,...typeof c.reason==`string`&&!r(c)?{causeReason:c.reason}:{},timeoutMs:t,requestId:i,...a===void 0||n(e)?{}:{hint:a}},e)}function N(e,t){if(e.ok)return e;let n=e.error.details?.reason,o=s();return{...e,error:{...e.error,...i(e.error.details)?{message:o.message,hint:String(o.details?.hint)}:{},details:{...e.error.details,...typeof n==`string`&&!r(e.error.details)?{causeReason:n}:{},reason:a,timeoutMs:t}}}}async function P(e,n,r){let i=p().has(e)?void 0:g(e),a=p().get(e);i&&(a.deadlineRegistration=i);let o=Date.now(),s=Math.min(o+n,a.resultDeadlineMs??1/0),c={deadlineMs:s,expire:()=>{if(a.signal.aborted)return;let n={timeoutMs:Math.max(0,s-o),requestId:e};a.executionDeadlineCancellation=n,a.abort(t(n))}},l=a.executionDeadlines??=new Set;l.add(c);let u=()=>setTimeout(d,Math.max(0,(w(a)?.deadlineMs??s)-Date.now())),d=()=>{D(a),a.signal.aborted||(f=u())},f=u();try{D(a),a.signal.throwIfAborted();let e=await r();D(a);let t=a.executionDeadlineCancellation;return t?N(e,t.timeoutMs):(a.signal.throwIfAborted(),e)}catch(e){D(a);let t=a.executionDeadlineCancellation;throw t?M(e,t.timeoutMs,t.requestId):(a.signal.throwIfAborted(),e)}finally{clearTimeout(f),l.delete(c),l.size===0&&(b(a.deadlineRegistration),a.deadlineRegistration=void 0)}}const F=new l;async function I(e,t){return await F.run(e,t)}function L(e){F.getStore()?.(e)}var R=e({throwIfRequestCanceled:()=>j});export{T as _,b as a,C as c,S as d,x as f,j as g,m as h,v as i,A as l,g as m,L as n,y as o,_ as p,I as r,k as s,R as t,O as u,P as v};