openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.07 kB
JavaScript
import{o as racePromiseWithAbortSignal}from"./src-De06gkOi.mjs";import"./abort-signal-BKkRe6pu.mjs";import{r as readWorkerProjectPreparation}from"./preparation-identity.schema-BKTtM6v_.mjs";import{j as NODE_WORKER_WORKSPACE_PREPARE_COMMAND}from"./node-commands-BbC002V7.mjs";import{n as parseNodeWorkerPreparedWorkspaceResult}from"./node-workspace-prepared-protocol-4-YvBlel.mjs";
//!src/gateway/worker-environments/node-worker-prepared-workspace-transport.ts
function createNodeWorkerPreparedWorkspaceTransport(e){const invokePreparedWorkspace=async t=>{const{input:n,signal:r}=t;const assertCurrent=()=>{r?.throwIfAborted();t.assertCurrent()};assertCurrent();const i=e.getNodeTransport();if(!i)throw new Error(`Prepared workspace node transport is unavailable`);const a=await racePromiseWithAbortSignal(i.getCurrentNode(t.deviceId),r);assertCurrent();if(!a||!i.isCurrent(a))throw new Error(`Prepared workspace node protocol is unavailable`);const o=n.action===`register`?605e3:3e4;const s=await i.invoke({node:a,command:NODE_WORKER_WORKSPACE_PREPARE_COMMAND,params:n,signal:r,timeoutMs:o,idempotencyKey:`${n.environmentId}:${n.preparationKey}:${n.action}`,isDispatchAuthorized:()=>{assertCurrent();return i.isCurrent(a)}});assertCurrent();if(!s.ok)throw new Error(`Prepared workspace ${n.action} failed${s.error?.message?`: ${s.error.message}`:``}`);const c=s.payloadJSON?JSON.parse(s.payloadJSON):s.payload;const l=parseNodeWorkerPreparedWorkspaceResult(c);if(!l||l.gatewayNamespace!==n.gatewayNamespace||l.environmentId!==n.environmentId||l.preparationKey!==n.preparationKey||l.cacheKey!==n.cacheKey||n.action===`register`&&(l.workspaceDir!==n.workspaceDir||l.homeDir!==n.homeDir||l.sourceManifestRef!==n.sourceManifestRef||l.preparedManifestRef!==n.preparedManifestRef))throw new Error(`Node did not acknowledge the exact prepared workspace`);return l};return{registerPreparedWorkspace:async({record:t,deviceId:n,workspace:r,assertCurrent:i,signal:a})=>{const o=e.store.get(t.environmentId);const assertRegistrationCurrent=()=>{a?.throwIfAborted();i();const s=e.store.get(t.environmentId);const c=s&&readWorkerProjectPreparation(s.profileSnapshot.project);if(!(s?.state===`provisioning`||s?.state===`ready`&&s.preparation?.consumedAtMs!=null)||s.ownerEpoch!==t.ownerEpoch||s.provisionOperationId!==t.provisionOperationId||!o?.nodeSetupId||s.nodeSetupId!==o.nodeSetupId||s.nodeDeviceId!==n||s.attachedSessionIds.length!==0||s.destroyRequestedAtMs!==null||c?.key!==r.preparationKey||c.cacheKey!==r.cacheKey)throw new Error(`Prepared workspace lost its exact provisioning owner`)};await invokePreparedWorkspace({deviceId:n,assertCurrent:assertRegistrationCurrent,signal:a,input:{action:`register`,gatewayNamespace:e.gatewayNamespace,environmentId:t.environmentId,...r}})},bindPreparedWorkspace:async({assertCurrent:t,signal:n,...r})=>{const i=e.store.get(r.environmentId);const a=await e.placementStore.getAsync(r.sessionId);const assertBindingCurrent=()=>{n?.throwIfAborted();t();const o=e.store.get(r.environmentId);const s=e.placementStore.get(r.sessionId);const c=e.store.getCredential(r.environmentId);const l=o&&readWorkerProjectPreparation(o.profileSnapshot.project);if(!i?.nodeDeviceId||o?.nodeDeviceId!==i.nodeDeviceId||o.state!==`attached`||o.sharedHost!==false||o.ownerEpoch!==r.ownerEpoch||o.attachedSessionIds.length!==1||o.attachedSessionIds[0]!==r.sessionId||o.destroyRequestedAtMs!==null||c?.ownerEpoch!==r.ownerEpoch||c.sessionId!==r.sessionId||l?.key!==r.preparationKey||l.cacheKey!==r.cacheKey||!a||!s||s.generation!==a.generation||s.sessionKey!==r.sessionKey||s.environmentId!==r.environmentId||![`syncing`,`starting`,`active`].includes(s.state)||s.state===`active`&&s.activeOwnerEpoch!==r.ownerEpoch)throw new Error(`Prepared workspace lost its exact attached placement owner`);return i.nodeDeviceId};const o=assertBindingCurrent();return await invokePreparedWorkspace({deviceId:o,assertCurrent:assertBindingCurrent,signal:n,input:{action:`bind`,gatewayNamespace:e.gatewayNamespace,...r}})}}}export{createNodeWorkerPreparedWorkspaceTransport};