UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

243 lines (242 loc) • 8.48 kB
import { l as isRecord, t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs"; import { t as warnPluginSdkDeprecation } from "./sdk-deprecation-D5N9acnu.mjs"; import { t as classifyAgentToolSource } from "./tool-metadata-rqUpUHlb.mjs"; import { n as consumeToolExecutionNotStarted } from "./tool-effect-receipt-BbbEwqBS.mjs"; import { t as prepareConversationCapabilityProfile } from "./conversation-capability-profile-Be5DqCww.mjs"; import { i as consumeAdjustedParamsForToolCall, o as consumePreExecutionBlockedToolCall } from "./agent-tools.before-tool-call.state-C5F7vnXY.mjs"; import "./agent-tool-result-middleware-C6qDSxiL.mjs"; import "./gateway-tool-policy-Bz1T20RP.mjs"; import "./async-work-resources-BF09U4aO.mjs"; import { c as resolveToolResultFailureKind } from "./tool-result-error-B2GHe83l.mjs"; import { n as runWithToolExecutionValidation, t as coerceToolExecutionArguments } from "./agent-tools.execution-validation-COq3xpqN.mjs"; import "./accepted-session-spawn-DXCz6IHR.mjs"; import { k as isLocalModelLeanEnabled } from "./tool-search-DcQ9xra6.mjs"; import "./embedded-agent-messaging-extraction-C5OCu-VM.mjs"; import "./embedded-agent-tool-results-DbtxSr9V.mjs"; import "./embedded-agent-tool-media-Bzv7p-IE.mjs"; import { t as withLegacySessionCapabilityRead } from "./subagent-session-store-legacy-DGY34vTb.mjs"; import { t as createAgentHarnessToolSurfaceRuntimeCore } from "./tool-surface-bridge-BDxYoRUW.mjs"; import { a as resolveAgentHarnessToolResultPresentation } from "./tool-result-facts-C9Xta0St.mjs"; //#region src/agents/harness/tool-execution.ts /** Retains the exact execution promise, including failure, for each native call tuple. */ 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 }; } }; } /** Records the tool boundary independently of backend result presentation. */ 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); } }; } }; } //#endregion //#region src/agents/harness/tool-invocation.ts /** Owns host execution and presentation; adapters retain their native terminal owner. */ 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(); } } //#endregion //#region src/plugin-sdk/agent-harness-tool-runtime.ts /** @deprecated Await createAgentHarnessToolSurfaceRuntimeAsync. Final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */ 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 }; } }; } //#endregion export { createAgentHarnessToolExecutionRegistry as a, createAgentHarnessToolExecutionBoundaryRegistry as i, createAgentHarnessToolSurfaceRuntimeAsync as n, runAgentHarnessToolInvocation as r, createAgentHarnessToolSurfaceRuntime as t };