UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 6.79 kB
import{l as isRecord,t as asNonArrayRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as isPromiseLike}from"./promise-like-OKzBwzLG.mjs";import{t as warnPluginSdkDeprecation}from"./sdk-deprecation-BDUCyBrU.mjs";import{t as classifyAgentToolSource}from"./tool-metadata-Cb5nBZYD.mjs";import{n as consumeToolExecutionNotStarted}from"./tool-effect-receipt-B6D3MzWJ.mjs";import{a as resolveToolResultFailureKind}from"./tool-result-error-DAdEhHzf.mjs";import{t as prepareConversationCapabilityProfile}from"./conversation-capability-profile-DiRRWN6T.mjs";import"./accepted-session-spawn-D4rboLl7.mjs";import{i as consumePreExecutionBlockedToolCall,r as consumeAdjustedParamsForToolCall}from"./agent-tools.before-tool-call.state-BXmY3Lwr.mjs";import"./agent-tool-result-middleware-Zr7nwQA1.mjs";import"./gateway-tool-policy-B3gT1TWd.mjs";import"./async-work-resources-vg9S-hbA.mjs";import{n as runWithToolExecutionValidation,t as coerceToolExecutionArguments}from"./agent-tools.execution-validation-H5xesDQ3.mjs";import{S as isLocalModelLeanEnabled,u as resolveAgentHarnessToolResultPresentation}from"./tool-search-Dzso1_TY.mjs";import"./embedded-agent-tool-results-BPxGp984.mjs";import"./embedded-agent-tool-media-C5KZzCKb.mjs";import"./embedded-agent-messaging-extraction-DtyqGTJA.mjs";import"./tool-result-frame-bound-vdTYafDz.mjs";import{t as withLegacySessionCapabilityRead}from"./subagent-session-store-legacy-BFIRc0IU.mjs";import{t as createAgentHarnessToolSurfaceRuntimeCore}from"./tool-surface-bridge-DAgblpyh.mjs"; //!src/agents/harness/tool-execution.ts function createAgentHarnessToolExecutionRegistry(e){const t=/* @__PURE__ */ new Map;const keyFor=t=>JSON.stringify(e(t));return{get(e){return t.get(keyFor(e))},claim(e,n){const r=t.get(keyFor(e));if(r)return{execution:r};const i=n();t.set(keyFor(e),i);return{execution:i}}}}function createAgentHarnessToolExecutionBoundaryRegistry(){const e=/* @__PURE__ */ new Map;return{consume(t){const n=e.get(t);e.delete(t);if(n)n.consumed=true;return n?.snapshot},begin(t){let n=structuredClone(t.arguments);let r=false;let i=false;let a=false;const o={consumed:false,retainAfterCompletion:t.retainAfterCompletion===true};e.set(t.toolCallId,o);const consumeBlocked=()=>{a=a||consumePreExecutionBlockedToolCall(t.toolCallId,t.runId)};return{get executedArguments(){return n},get didStartExecution(){return i},get executionPrevented(){return a},get executionStarted(){return i&&!a},setArguments(e){n=structuredClone(e)},markDispatched(){r=true},capture(e){a||=e?.noStart===true;i||=r;consumeBlocked();const s=consumeAdjustedParamsForToolCall(t.toolCallId,t.runId);if(isRecord(s))n=s;if(!o.consumed)o.snapshot={executedArguments:n,executionStarted:i&&!a}},consumeBlocked,dispose(){if(e.get(t.toolCallId)===o&&(o.consumed||!o.retainAfterCompletion))e.delete(t.toolCallId);consumeAdjustedParamsForToolCall(t.toolCallId,t.runId)}}}}} //!src/agents/harness/tool-invocation.ts async function runAgentHarnessToolInvocation(e){const t=e.startedAt??Date.now();const n=e.initialArguments??asNonArrayRecord(e.call.arguments);const r=e.boundaries.begin({toolCallId:e.call.toolCallId,runId:e.runId,arguments:n,retainAfterCompletion:e.retainExecutionSnapshot});let i;try{const a=e.tool;if(!a)throw new Error(e.unavailableToolMessage??`OpenClaw tool is unavailable: ${e.call.toolName}`);const o=a.prepareArguments;const s=o?Reflect.apply(o,a,[e.call.arguments]):e.call.arguments;let c=s;if(e.prepareArguments){const t=e.prepareArguments(s,Boolean(o));c=isPromiseLike(t)?await t:t}if(e.validateArguments&&classifyAgentToolSource(a).source===`openclaw`)c=coerceToolExecutionArguments(a,a.parameters,c);const l=isRecord(c)?c:n;if(o||e.prepareArguments||l!==n)r.setArguments(l);const u=e.beforeExecute?.();if(u)await u;e.assertCurrent?.();r.markDispatched();const d=e.shouldValidateArguments?.()??true;const invokeTool=()=>a.execute(e.call.toolCallId,c,e.signal);i=await(e.validateArguments&&d?runWithToolExecutionValidation(e.call.toolCallId,e.validateArguments,invokeTool):invokeTool());r.capture();const f={boundary:r,startedAt:t,executedArguments:r.executedArguments,rawResult:i};const p=resolveToolResultFailureKind(i);const m=p!==void 0;e.beforeSnapshotResult?.({...f,rawIsError:m});const h=e.snapshotResult?e.snapshotResult(i):i;const g={...f,rawResultSnapshot:h,rawIsError:m,rawFailureKind:p};e.onExecutionResult?.(g);const _=await e.applyMiddleware({threadId:e.call.threadId,turnId:e.call.turnId,toolCallId:e.call.toolCallId,toolName:e.call.toolName,args:structuredClone(g.executedArguments),cwd:e.call.cwd,isError:g.rawIsError,result:i});const v=resolveAgentHarnessToolResultPresentation({result:_,executionIsError:g.rawIsError,executionFailureKind:g.rawFailureKind});const y=e.onResult({...g,result:_,observerResult:v.result,isError:v.isError,failureKind:v.failureKind});return isPromiseLike(y)?await y:y}catch(n){r.capture({noStart:consumeToolExecutionNotStarted(n)});const a=e.onError({error:n,boundary:r,startedAt:t,executedArguments:r.executedArguments,rawResult:i});return isPromiseLike(a)?await a:a}finally{r.dispose()}} //!src/plugin-sdk/agent-harness-tool-runtime.ts function createAgentHarnessToolSurfaceRuntime(e){warnPluginSdkDeprecation({family:`agent-harness-tool-surface`,method:`createAgentHarnessToolSurfaceRuntime`,replacement:`createAgentHarnessToolSurfaceRuntimeAsync`});return projectToolSurfaceRuntime(createAgentHarnessToolSurfaceRuntimeCore(e),withLegacySessionCapabilityRead)}async function createAgentHarnessToolSurfaceRuntimeAsync(e){const t=isLocalModelLeanEnabled(e)?await prepareConversationCapabilityProfile({config:e.config,agentId:e.agentId,sessionKey:e.sessionKey,modelProvider:e.modelProvider,modelId:e.modelId,runtimeToolAllowlist:e.runtimeToolAllowlist?[...e.runtimeToolAllowlist]:void 0,scheduledToolPolicy:e.scheduledToolPolicy}):void 0;return projectToolSurfaceRuntime(createAgentHarnessToolSurfaceRuntimeCore({...e,preparedToolOverrideAllowlist:t?.policy.explicitToolOverrideAllowlist??[]}))}function projectToolSurfaceRuntime(e,t=e=>e()){const n=e;return{codeModeControlsEnabled:e.codeModeControlsEnabled,config:e.config,includeToolSearchControls:e.includeToolSearchControls,runtimeToolAllowlist:e.runtimeToolAllowlist,toolSearchCatalogExecutor:n.toolSearchCatalogExecutor,toolSearchCatalogRef:n.toolSearchCatalogRef,toolSearchControlsEnabled:e.toolSearchControlsEnabled,cleanup:e.cleanup,compactTools:(n,{hookContext:r,localModelLeanApplied:i}={})=>{const{tools:a,promptToolPolicy:o}=t(()=>e.compactTools(n,{hookContext:r,localModelLeanApplied:i}));return{tools:a,promptToolPolicy:o}}}}export{createAgentHarnessToolExecutionRegistry as a,createAgentHarnessToolExecutionBoundaryRegistry as i,createAgentHarnessToolSurfaceRuntimeAsync as n,runAgentHarnessToolInvocation as r,createAgentHarnessToolSurfaceRuntime as t};