fix(hooks): deduplicate before_tool_call hook in toToolDefinitions (#15502)

This commit is contained in:
damaozi
2026-02-14 01:44:13 +08:00
committed by Peter Steinberger
parent b4430c126a
commit 534e4213a1
4 changed files with 50 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
import { toClientToolDefinitions } from "./pi-tool-definition-adapter.js";
import { toClientToolDefinitions, toToolDefinitions } from "./pi-tool-definition-adapter.js";
import { wrapToolWithBeforeToolCallHook } from "./pi-tools.before-tool-call.js";
vi.mock("../plugins/hook-runner-global.js");
@@ -108,6 +108,44 @@ describe("before_tool_call hook integration", () => {
});
});
describe("before_tool_call hook deduplication (#15502)", () => {
let hookRunner: {
hasHooks: ReturnType<typeof vi.fn>;
runBeforeToolCall: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
hookRunner = {
hasHooks: vi.fn(() => true),
runBeforeToolCall: vi.fn(async () => undefined),
};
// oxlint-disable-next-line typescript/no-explicit-any
mockGetGlobalHookRunner.mockReturnValue(hookRunner as any);
});
it("fires hook exactly once when tool goes through wrap + toToolDefinitions", async () => {
const execute = vi.fn().mockResolvedValue({ content: [], details: { ok: true } });
// oxlint-disable-next-line typescript/no-explicit-any
const baseTool = { name: "web_fetch", execute, description: "fetch", parameters: {} } as any;
const wrapped = wrapToolWithBeforeToolCallHook(baseTool, {
agentId: "main",
sessionKey: "main",
});
const [def] = toToolDefinitions([wrapped]);
await def.execute(
"call-dedup",
{ url: "https://example.com" },
undefined,
undefined,
undefined,
);
expect(hookRunner.runBeforeToolCall).toHaveBeenCalledTimes(1);
});
});
describe("before_tool_call hook integration for client tools", () => {
let hookRunner: {
hasHooks: ReturnType<typeof vi.fn>;