openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.25 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{a as throwIfSettledRejected}from"./error-graph-internal-Bt2pFJBM.mjs";import{l as COMPUTER_EXECUTION_ID_PATTERN}from"./computer-use-contract-CaYMDN7U.mjs";import{randomUUID}from"node:crypto";
//!src/plugins/computer-use-registration.ts
function registerComputerUseProvider(e,t){let n;let r;let i;const hasActiveWork=()=>n!==void 0||r!==void 0||i!==void 0;const executionEnvelopeFromParams=e=>{let t;try{t=JSON.parse(e??`{}`)}catch{throw new Error(`COMPUTER_INVALID_REQUEST: params must be valid JSON`)}const n=isRecord(t)?t.executionId:void 0;if(n===void 0)return{executionId:void 0,value:t};if(typeof n!==`string`||!new RegExp(`^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`,`u`).test(n))throw new Error(`COMPUTER_INVALID_REQUEST: executionId is required`);return{executionId:n,value:t}};const getExecution=async(e,i)=>{for(let e=r;e!==void 0;e=r)await e;if(n&&n.id!==e)throw new Error(`COMPUTER_HOST_BUSY: another provider execution owns this computer`);if(!n){const r=t.openExecution(i?.sessionKey?{executionId:e,sessionKey:i.sessionKey}:{executionId:e});r.catch(()=>{if(n?.promise===r)n=void 0});n={id:e,promise:r}}return n.promise};const closeCurrentExecution=(e,t)=>{const a=n;if(!a||e!==void 0&&a.id!==e)return Promise.resolve();if(i)return i;const o=a.promise.then(async e=>await e.close(t));i=o;r=o;o.then(()=>{if(n===a)n=void 0;i=void 0;r=void 0},()=>{i=void 0;if(n!==a)r=void 0});return o};const closeExecution=(e,t)=>{if(!i)return closeCurrentExecution(e,t);const r=(async()=>{for(let t=i;t!==void 0;t=i){const r=e===void 0||n?.id===e;try{await t}catch(e){if(r)throw e;return}}await closeCurrentExecution(e,t)})();r.catch(()=>{});return r};e.registerNodeHostCommand({command:`screen.snapshot`,cap:`screen`,dangerous:false,prepare:e=>t.prepare?.(e),isAvailable:()=>t.isAvailable(),hasActiveWork,watchAvailability:(e,n)=>{const r=t.watchAvailability?.(e,n);let i;return async()=>{i??=Promise.resolve().then(()=>r?.()).catch(e=>{i=void 0;throw e});const e=await Promise.allSettled([i,closeExecution(void 0,`node-host-stop`)]);throwIfSettledRejected(e,`computer provider shutdown failed`)}},onDisconnect:async()=>await closeExecution(void 0,`gateway-disconnect`),handle:async(e,n,r)=>{const i=executionEnvelopeFromParams(e);if(i.executionId)return await(await getExecution(i.executionId,r)).snapshot(e,r?.signal);const a=randomUUID();const o=await t.openExecution(r?.sessionKey?{executionId:a,sessionKey:r.sessionKey}:{executionId:a});try{return await o.snapshot(e,r?.signal)}finally{await o.close(`snapshot-complete`)}}});e.registerNodeHostCommand({command:`computer.act`,cap:`computer`,dangerous:true,computerUse:()=>t.capabilities(),isAvailable:()=>t.isAvailable(),hasActiveWork,handle:async(e,t,n)=>{const r=executionEnvelopeFromParams(e);if(!r.executionId)throw new Error(`COMPUTER_INVALID_REQUEST: executionId is required`);if(isRecord(r.value)&&r.value.action===`__close_execution`){const e=r.value.reason;await closeExecution(r.executionId,typeof e===`string`&&e.trim()?e.slice(0,64):`completion`);return JSON.stringify({ok:true})}return await(await getExecution(r.executionId,n)).act(e,n?.signal)}})}export{registerComputerUseProvider as t};