UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 3.37 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{o as sessionChanges}from"./session-row-changes-21DJT-m_.mjs";import{randomUUID}from"node:crypto";import{AsyncLocalStorage}from"node:async_hooks"; //!src/infra/agent-event-execution-context.ts function getAgentEventExecutionContext(){return resolveGlobalSingleton(Symbol.for(`openclaw.agentEvents.executionContext`),()=>new AsyncLocalStorage)}function recordAgentEventRouting(e,t,n){const r=getAgentEventExecutionContext().getStore();if(!r||r.lifecycleGeneration!==t.lifecycleGeneration)return;const i=r.routingByRun??=/* @__PURE__ */ new Map;const a=i.get(e);const o=a?.owner.deref();const s=a&&o===t?a.routing:{};s.agentId=t.agentId;s.sessionKey=t.sessionKey;s.sessionId=t.sessionId;s.completionSource=t.completionSource;s.isControlUiVisible=t.isControlUiVisible;s.projectSessionLifecycle=t.projectSessionLifecycle;s.projectSessionMessages=t.projectSessionMessages;s.mainSessionRestartRecovery=t.mainSessionRestartRecovery;s.lifecycleStartedAt=t.lifecycleStartedAt;s.lifecycleGeneration=t.lifecycleGeneration;s.verboseLevel=t.verboseLevel;s.registeredAt=t.registeredAt;s.eventState=t.eventState;if(a&&(o===t||n!==void 0&&o===n&&a.routing.lifecycleGeneration!==t.lifecycleGeneration)){if(o!==t)a.owner=new WeakRef(t);a.routing=s}else i.set(e,{owner:new WeakRef(t),routing:s})} //!src/infra/agent-run-registry-state.ts const registryState=resolveGlobalSingleton(Symbol.for(`openclaw.agentRunRegistry.state`),()=>({contexts:/* @__PURE__ */ new Map,owners:/* @__PURE__ */ new Map,lifecycleGeneration:randomUUID(),version:0}));function getAgentRunRegistryState(){return registryState}function storeRunContext(e,t,n){t.capacityWaits=void 0;t.executionActivity=void 0;t.localModelWait=void 0;t.eventState={seq:0};t.modelCallSequence={value:0};t.acceptedUserReply=void 0;t.registeredAt??=Date.now();registryState.contexts.set(e,t);recordAgentEventRouting(e,t,n)}function readAgentRunIndexVersion(){return registryState.version}function getAgentRunLifecycleGeneration(){return registryState.lifecycleGeneration}function listAgentRunsForSession(e){const t=getAgentRunRegistryState();const n=[];for(const[r,i]of t.contexts)if(i.sessionKey===e.sessionKey&&(e.anySessionId||!i.sessionId||i.sessionId===e.sessionId)&&i.lifecycleGeneration===t.lifecycleGeneration)n.push({runId:r,lifecycleGeneration:i.lifecycleGeneration});return n.toSorted((e,t)=>e.runId.localeCompare(t.runId))}function getAgentRunContextOwnerStatus(e,t,n){const r=getAgentRunRegistryState();const i=r.owners.get(e);if(n!==r.lifecycleGeneration||i?.lifecycleGeneration!==n||!i.claimIds.has(t))return;return i.clearRequested?`clear-requested`:`active`}function bumpAgentRunIndexVersion(e,t){getAgentRunRegistryState().version+=1;for(const n of t&&(t.sessionKey!==e?.sessionKey||!t.sessionKey&&t.sessionId!==e?.sessionId||t.agentId!==e?.agentId)?[t,e]:[e]){const{sessionKey:e,sessionId:t,agentId:r}=n??{};sessionChanges.emit(e?{sessionKey:e,agentId:r,scope:`runtime`}:{all:true,scope:`agent-runs`,...t?{runtimeSession:{sessionId:t,agentId:r}}:{}})}}export{listAgentRunsForSession as a,getAgentEventExecutionContext as c,getAgentRunRegistryState as i,recordAgentEventRouting as l,getAgentRunContextOwnerStatus as n,readAgentRunIndexVersion as o,getAgentRunLifecycleGeneration as r,storeRunContext as s,bumpAgentRunIndexVersion as t};