openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.42 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{p as runWithSessionSourceScope,s as composeSessionSourceAssertion}from"./session-source-authority-Cun3s-rH.mjs";import{n as captureGatewayToolReceiptAssertion,o as getGatewayToolCallerIdentity}from"./gateway-caller-context-Dw9IijqV.mjs";import{o as copyAgentToolMetadata}from"./agent-tool-metadata-D7ctyGhj.mjs";import{AsyncLocalStorage}from"node:async_hooks";
//!src/agents/agent-tool-source-execution-guard.ts
const executionBudgetContext=resolveGlobalSingleton(Symbol.for(`openclaw.agentToolExecutionBudgetContext`),()=>new AsyncLocalStorage);function createAgentToolExecutionBudget(e){let t=0;let n=true;const assertCurrent=()=>{e.signal.throwIfAborted();try{if(!n||e.isCurrent?.()===false)throw new Error(`Agent tool execution scope is no longer active`)}catch(t){if(t instanceof Error)e.abort(t);throw t}};const admit=()=>{assertCurrent();if(e.maxToolCalls!==void 0&&t>=e.maxToolCalls){const t=/* @__PURE__ */ new Error(`Agent tool-call budget exhausted`);e.abort(t);throw t}t+=1};return{get toolCalls(){return t},async run(e){try{return await executionBudgetContext.run({admit,assertCurrent},e)}finally{n=false}}}}function captureAgentToolExecutionBudget(){return executionBudgetContext.getStore()?.admit}function captureAgentToolSourceExecutionGuard(e){const t=getGatewayToolCallerIdentity()?.receiptAuthority;const n=executionBudgetContext.getStore()?.assertCurrent;const r=t?captureGatewayToolReceiptAssertion(t,`tool invocation authority is no longer active`):void 0;return composeSessionSourceAssertion([r],t=>{e?.throwIfAborted();n?.();t()})}const pluginToolInvocationSources=resolveGlobalSingleton(Symbol.for(`openclaw.pluginToolInvocationSource`),()=>new AsyncLocalStorage);function runWithPluginToolInvocationSource(e){const t=captureAgentToolSourceExecutionGuard();return runWithAgentToolSourceExecutionScope(t,()=>pluginToolInvocationSources.run(t,e))}function assertPluginToolInvocationSourceCurrent(){pluginToolInvocationSources.getStore()?.()}function runWithAgentToolSourceExecutionScope(e,t){return runWithSessionSourceScope(e,t,{checkAfter:false})}const SOURCE_EXECUTION_GUARD=Symbol.for(`openclaw.agentToolSourceExecutionGuard`);function appendSourceExecutionGuard(e,t){const n=e[SOURCE_EXECUTION_GUARD];if(n===t)return;Object.defineProperty(e,SOURCE_EXECUTION_GUARD,{value:n?composeSessionSourceAssertion([n,t]):t,configurable:true,enumerable:false})}function bindAgentToolSourceExecutionGuard(e,t){const n=copyAgentToolMetadata(e,{...e});copyAgentToolSourceExecutionGuard(e,n);appendSourceExecutionGuard(n,t);return n}function copyAgentToolSourceExecutionGuard(e,t){const n=e[SOURCE_EXECUTION_GUARD];if(n)appendSourceExecutionGuard(t,n)}function runAgentToolSourceExecutionGuard(e){e[SOURCE_EXECUTION_GUARD]?.()}function runWithBoundAgentToolSourceExecutionScope(e,t){return runWithAgentToolSourceExecutionScope(e[SOURCE_EXECUTION_GUARD],async()=>{runAgentToolSourceExecutionGuard(e);return t()})}export{copyAgentToolSourceExecutionGuard as a,runWithAgentToolSourceExecutionScope as c,captureAgentToolSourceExecutionGuard as i,runWithBoundAgentToolSourceExecutionScope as l,bindAgentToolSourceExecutionGuard as n,createAgentToolExecutionBudget as o,captureAgentToolExecutionBudget as r,runAgentToolSourceExecutionGuard as s,assertPluginToolInvocationSourceCurrent as t,runWithPluginToolInvocationSource as u};