feat(exec-events): bead-4 - wrap tools with exec context

This commit is contained in:
Grace (Clawdbot) 2026-01-27 14:18:20 +01:00
parent a8837394a6
commit 5bde9de32f

View File

@ -19,6 +19,7 @@ import { listChannelAgentTools } from "./channel-tools.js";
import { createMoltbotTools } from "./moltbot-tools.js"; import { createMoltbotTools } from "./moltbot-tools.js";
import type { ModelAuthMode } from "./model-auth.js"; import type { ModelAuthMode } from "./model-auth.js";
import { wrapToolWithAbortSignal } from "./pi-tools.abort.js"; import { wrapToolWithAbortSignal } from "./pi-tools.abort.js";
import { runWithExecEventContext } from "../infra/exec-events-context.js";
import { import {
filterToolsByPolicy, filterToolsByPolicy,
isToolAllowedByPolicies, isToolAllowedByPolicies,
@ -96,6 +97,28 @@ function resolveExecConfig(cfg: MoltbotConfig | undefined) {
}; };
} }
function wrapToolWithExecEventContext(
tool: AnyAgentTool,
context: { runId?: string; sessionKey?: string },
): AnyAgentTool {
const execute = tool.execute;
if (!execute) return tool;
const runId = context.runId?.trim() || undefined;
const sessionKey = context.sessionKey?.trim() || undefined;
return {
...tool,
execute: (toolCallId, params, signal, onUpdate) =>
runWithExecEventContext(
{
runId,
toolCallId,
sessionKey,
},
() => execute(toolCallId, params, signal, onUpdate),
),
};
}
export const __testing = { export const __testing = {
cleanToolSchemaForGemini, cleanToolSchemaForGemini,
normalizeToolParams, normalizeToolParams,
@ -411,9 +434,15 @@ export function createMoltbotCodingTools(options?: {
// Always normalize tool JSON Schemas before handing them to pi-agent/pi-ai. // Always normalize tool JSON Schemas before handing them to pi-agent/pi-ai.
// Without this, some providers (notably OpenAI) will reject root-level union schemas. // Without this, some providers (notably OpenAI) will reject root-level union schemas.
const normalized = subagentFiltered.map(normalizeToolParameters); const normalized = subagentFiltered.map(normalizeToolParameters);
const withExecContext = normalized.map((tool) =>
wrapToolWithExecEventContext(tool, {
runId: options?.runId,
sessionKey: options?.sessionKey,
}),
);
const withAbort = options?.abortSignal const withAbort = options?.abortSignal
? normalized.map((tool) => wrapToolWithAbortSignal(tool, options.abortSignal)) ? withExecContext.map((tool) => wrapToolWithAbortSignal(tool, options.abortSignal))
: normalized; : withExecContext;
// NOTE: Keep canonical (lowercase) tool names here. // NOTE: Keep canonical (lowercase) tool names here.
// pi-ai's Anthropic OAuth transport remaps tool names to Claude Code-style names // pi-ai's Anthropic OAuth transport remaps tool names to Claude Code-style names