fix: correct types for before_tool_call hook wrapper

- Remove toolCallId from hook context/event (not in type definitions)
- Return proper AgentToolResult structure when blocking
- Import AgentToolResult from pi-agent-core

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
fuushyn 2026-01-27 01:47:40 +05:30
parent 6e7bfa9633
commit 50ba466bbc

View File

@ -7,6 +7,7 @@
* - Block tool execution with a custom reason * - Block tool execution with a custom reason
*/ */
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
import type { HookRunner } from "../plugins/hooks.js"; import type { HookRunner } from "../plugins/hooks.js";
import type { PluginHookToolContext } from "../plugins/hooks.js"; import type { PluginHookToolContext } from "../plugins/hooks.js";
import type { AnyAgentTool } from "./pi-tools.types.js"; import type { AnyAgentTool } from "./pi-tools.types.js";
@ -43,19 +44,17 @@ export function wrapToolWithBeforeCallHook(
execute: async (toolCallId, params, signal, onUpdate) => { execute: async (toolCallId, params, signal, onUpdate) => {
const toolName = tool.name; const toolName = tool.name;
// Build hook context // Build hook context (matches PluginHookToolContext type)
const hookCtx: PluginHookToolContext = { const hookCtx: PluginHookToolContext = {
agentId, agentId,
sessionKey, sessionKey,
toolName, toolName,
toolCallId,
}; };
// Run the before_tool_call hook // Run the before_tool_call hook
const hookResult = await hookRunner.runBeforeToolCall( const hookResult = await hookRunner.runBeforeToolCall(
{ {
toolName, toolName,
toolCallId,
params: params as Record<string, unknown>, params: params as Record<string, unknown>,
}, },
hookCtx, hookCtx,
@ -64,11 +63,12 @@ export function wrapToolWithBeforeCallHook(
// Check if the hook blocked the tool call // Check if the hook blocked the tool call
if (hookResult?.block) { if (hookResult?.block) {
const reason = hookResult.blockReason ?? "Tool call blocked by policy"; const reason = hookResult.blockReason ?? "Tool call blocked by policy";
// Return a structured error that the agent can understand // Return a properly structured AgentToolResult error
return { const blockedResult: AgentToolResult<unknown> = {
type: "text" as const, content: [{ type: "text", text: `Error: ${reason}` }],
text: `Error: ${reason}`, details: { blocked: true, reason },
}; };
return blockedResult;
} }
// Use modified params if provided, otherwise use original // Use modified params if provided, otherwise use original