UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.43 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{i as createAbortError}from"./src-De06gkOi.mjs";import"./abort-signal-BKkRe6pu.mjs";import{t as AsyncContext}from"./async-context-D4i8WbBE.mjs";import{l as isAgentRunDirectAbortReason,u as isAgentRunRestartAbortReason}from"./run-termination-vr_m9wT0.mjs"; //!src/infra/exec-request-context.ts const state=resolveGlobalSingleton(Symbol.for(`openclaw.execRequestContext`),()=>({context:new AsyncContext(Symbol.for(`openclaw.execRequestContext.turn`)),turns:/* @__PURE__ */ new Set}));const EVENT_OWNERS=Symbol.for(`openclaw.execRequestEventOwners`);function createExecRequestOwner(e){const t=new AbortController;return{identity:Object.freeze({...e}),turnRunIds:new Set(e.runId?[e.runId]:[]),controller:t,signal:t.signal,pendingProcesses:/* @__PURE__ */ new Set,cleanupUncertain:false}}function cancelExecRequestOwners(e){for(const t of e){const e=createAbortError(`The request that owns this command was stopped`);t.controller.abort(e)}}function execRequestMatches(e,t){return(t.runId===void 0||e.turnRunIds.has(t.runId))&&(t.sessionKey===void 0||e.identity.sessionKey===t.sessionKey)&&(t.sessionId===void 0||e.identity.sessionId===t.sessionId)&&(t.agentId===void 0||e.identity.agentId===t.agentId)}function captureExecRequestOwners(e){const t=state.context.getStore();return t&&t.identity.runId===e.runId&&t.identity.sessionId===e.sessionId?t.owners:void 0}function captureExecRequestCustody(e){const t=captureExecRequestOwners(e);return t?.length?Object.freeze({[EVENT_OWNERS]:Object.freeze([...t])}):void 0}function captureExecRequestCustodySelection(e,t){const n=new Set(e);return e=>e?.[EVENT_OWNERS]?.some(e=>n.has(e)||Boolean(t&&execRequestMatches(e,t.target)&&t.acceptsRequest(e.identity)))===true}function activeExecRequestOwners(e,t){return Array.from(new Set([...state.turns].flatMap(({identity:n,owners:r})=>{const i=(e.runId===void 0||n.runId===e.runId)&&(e.sessionKey===void 0||n.sessionKey===e.sessionKey)&&(e.sessionId===void 0||n.sessionId===e.sessionId)&&(e.agentId===void 0||n.agentId===e.agentId);const a=Boolean(n.ownerConnId?.trim()||n.ownerDeviceId?.trim());return r.filter(r=>{const o=a?n:{...r.identity,...n};return i&&t(o)||execRequestMatches(r,e)&&t(r.identity)})})))}function adoptExecRequestSession(e){const t=state.context.getStore();if(t?.identity.runId!==e.runId||t.identity.sessionId!==e.previousSessionId)return;for(const n of state.turns)if(n.owners===t.owners&&n.identity.runId===e.runId&&n.identity.sessionId===e.previousSessionId)n.identity={...n.identity,sessionId:e.sessionId};for(const n of t.owners)if(n.identity.sessionId===e.previousSessionId)n.identity=Object.freeze({...n.identity,sessionId:e.sessionId})}function retainExecRequestProcess(e,t){if(!e?.length)return;for(const n of e)n.pendingProcesses.add(t);const release=()=>{for(const n of e)n.pendingProcesses.delete(t)};t.then(release,release)}async function withExecRequestTurn(e,t){const n=state.context.getStore();const r=n?.identity.runId===e.identity.runId&&n?.identity.sessionId===e.identity.sessionId?n:void 0;const i=r?.identity??e.identity;const a=e.owners??r?.owners;const o=a?.length?a:[createExecRequestOwner(i)];for(const t of o){t.signal.throwIfAborted();if(e.identity.runId)t.turnRunIds.add(e.identity.runId)}const s={identity:i,owners:o};const stop=()=>{if(isAgentRunDirectAbortReason(e.abortSignal?.reason)||isAgentRunRestartAbortReason(e.abortSignal?.reason))cancelExecRequestOwners(o)};state.turns.add(s);e.abortSignal?.addEventListener(`abort`,stop,{once:true});if(e.abortSignal?.aborted)stop();try{return await state.context.run(s,t)}finally{try{await Promise.all(o.flatMap(e=>e.signal.aborted?Array.from(e.pendingProcesses):[]))}finally{state.turns.delete(s);e.abortSignal?.removeEventListener(`abort`,stop)}}}function withExecRequestOwners(e,t){return t?.length?{...e,[EVENT_OWNERS]:t}:e}function readExecRequestOwners(e){return e[EVENT_OWNERS]}function execRequestAbortSignal(e,t){if(!e?.length)return t;const n=e.map(e=>e.signal);return AbortSignal.any(t?[t,...n]:n)}export{captureExecRequestCustodySelection as a,execRequestMatches as c,withExecRequestOwners as d,withExecRequestTurn as f,captureExecRequestCustody as i,readExecRequestOwners as l,adoptExecRequestSession as n,captureExecRequestOwners as o,cancelExecRequestOwners as r,execRequestAbortSignal as s,activeExecRequestOwners as t,retainExecRequestProcess as u};