UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

58 lines (57 loc) • 1.98 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as validateToolArguments } from "./src-DFw9QOgs.mjs"; import "./validation-Dt-LkqDC.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/agents/agent-tools.execution-validation.ts const executionValidators = resolveGlobalSingleton(Symbol.for("openclaw.toolExecutionValidationContext"), () => new AsyncLocalStorage()); function rejectClosedToolExecutionValidation() { throw new Error("Tool execution validation scope is closed"); } /** Keep per-call validation inside the policy wrapper's final execution boundary. */ async function runWithToolExecutionValidation(e, t, n) { const r = { toolCallId: e, validate: t }; try { return await executionValidators.run(r, n); } finally { r.validate = rejectClosedToolExecutionValidation; } } /** Validate hook-adjusted arguments without leaking a validator into concurrent calls. */ async function validateToolExecutionParams(e, t) { const n = executionValidators.getStore(); if (n?.toolCallId === e) await n.validate(t); } function isToolParameters(e) { return isRecord(e); } /** * Coerce model arguments the way the agent loop does for direct calls. * Input the schema still rejects is returned as-is for the caller's validator to report. */ function coerceToolExecutionArguments(e, t, n) { if (!isToolParameters(t) || !isRecord(n)) return n; try { const r = validateToolArguments({ name: e.name, description: e.description ?? "", parameters: t }, { type: "toolCall", id: e.name, name: e.name, arguments: n }); return isRecord(r) ? Object.defineProperties({}, { ...Object.getOwnPropertyDescriptors(n), ...Object.getOwnPropertyDescriptors(r) }) : r; } catch { return n; } } //#endregion export { runWithToolExecutionValidation as n, validateToolExecutionParams as r, coerceToolExecutionArguments as t };