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.35 kB
JavaScript
import{j as e}from"./sdk-contracts.js";import{t}from"./lifecycle-budget.js";const n=2e4;var r=class{settled=new Map;host;constructor(e){this.host=e}async start(r,a){let s=r.capture;if(s?.phase===`starting`){await t(s.opening,n);return}let c=this.host.harDir;if(!c){r.capture={phase:`unavailable`,reason:`capture-dir-unset`};return}let l=this.host.liveSessionOf(r);if(!l)return;let u,d=new Promise(e=>{u=e});r.capture={phase:`starting`,opening:d};let f=o(a),p=e=>{let t=r.capture;t!==void 0&&t.phase===`starting`&&t.opening===d&&(r.capture=e),u(e)};i(l.client,c,r.lease.leaseId,f).then(p,t=>p({phase:`unavailable`,reason:`tunnel-start-failed`,message:e(t)})),await d}async end(e){let r=e.lease.leaseId,i=e.capture;if(i===void 0)return this.settled.get(r);if(e.capture=void 0,i.phase!==`starting`)return await a(r,i,this.record);let o=await t(i.opening,n);return o.ok?await a(r,o.value,this.record):(i.opening.then(async e=>{await a(r,e,this.record)}).catch(()=>void 0),this.settled.get(r))}async list(e,t){if(t===void 0)return this.settled.get(e);if(t.phase!==`starting`){let{writeCaptureRecord:e}=await import(`./network-har.js`);return await e(t)}return this.settled.get(e)}record=(e,t)=>(this.settled.delete(e),this.settled.set(e,t),this.forgetRecordsOverLimit(),t);forgetRecordsOverLimit(){let e=this.heldCaptures();for(;this.settled.size+e.size>32;){let t=[...this.settled.keys()].find(t=>!e.has(t));if(t===void 0)return;this.settled.delete(t)}}heldCaptures(){let e=new Set;for(let t of this.host.heldOwners())t.capture!==void 0&&e.add(t.lease.leaseId);return e}};async function i(e,t,n,r){let{limrunHarPath:i,startLimrunHarCapture:a}=await import(`./network-har.js`),o=await a(e,r.length>0?r:void 0);return o.ok?{phase:`capturing`,harPath:i(t,n),capture:o.capture}:{phase:`unavailable`,reason:o.reason,message:o.message}}async function a(t,n,r){let{writeCaptureRecord:i}=await import(`./network-har.js`),a=await i(n).then(e=>({ok:!0,value:e}),e=>({ok:!1,error:e})),o;if(n.phase===`capturing`)try{n.capture.close()}catch(t){o=e(t)}if(!a.ok)throw o===void 0?a.error:Error(`${e(a.error)}; Capture close failed: ${o}`,{cause:a.error});let s=a.value;return r(t,o===void 0?s:{...s,closeFailure:o})}function o(e){return typeof e==`string`?e.split(`,`).map(e=>e.trim()).filter(Boolean):[]}export{n as CAPTURE_OPEN_BUDGET_MS,r as LimrunCaptureLifecycle};