mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:41:25 +00:00
fix(hooks): deduplicate before_tool_call hook in toToolDefinitions (#15502)
This commit is contained in:
committed by
Peter Steinberger
parent
b4430c126a
commit
534e4213a1
@@ -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>;
|
||||
|
||||
Reference in New Issue
Block a user