|
| 1 | +import { Type } from "typebox"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import type { AgentTool } from "../../runtime/index.js"; |
| 4 | +import type { ToolDefinition } from "../extensions/types.js"; |
| 5 | +import { |
| 6 | + createToolDefinitionsFromAgentTools, |
| 7 | + snapshotSessionToolDefinitions, |
| 8 | + wrapToolDefinitions, |
| 9 | +} from "./tool-definition-wrapper.js"; |
| 10 | + |
| 11 | +function createUnreadableParametersDefinition(name: string): ToolDefinition { |
| 12 | + const definition = { |
| 13 | + name, |
| 14 | + label: name, |
| 15 | + description: "bad schema", |
| 16 | + execute: async () => ({ |
| 17 | + content: [{ type: "text" as const, text: "bad" }], |
| 18 | + }), |
| 19 | + } as ToolDefinition; |
| 20 | + Object.defineProperty(definition, "parameters", { |
| 21 | + get: () => { |
| 22 | + throw new Error("revoked schema"); |
| 23 | + }, |
| 24 | + }); |
| 25 | + return definition; |
| 26 | +} |
| 27 | + |
| 28 | +describe("session tool definition wrapper", () => { |
| 29 | + it("skips unreadable ToolDefinition schemas while preserving healthy siblings", () => { |
| 30 | + const healthy = { |
| 31 | + name: "healthy_lookup", |
| 32 | + label: "Healthy Lookup", |
| 33 | + description: "survives bad siblings", |
| 34 | + parameters: Type.Object({ query: Type.String() }), |
| 35 | + execute: async () => ({ |
| 36 | + content: [{ type: "text" as const, text: "ok" }], |
| 37 | + }), |
| 38 | + } satisfies ToolDefinition; |
| 39 | + |
| 40 | + const tools = wrapToolDefinitions([ |
| 41 | + createUnreadableParametersDefinition("bad_lookup"), |
| 42 | + healthy, |
| 43 | + ]); |
| 44 | + |
| 45 | + expect(tools.map((tool) => tool.name)).toEqual(["healthy_lookup"]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("snapshots schemas without stripping TypeBox metadata", () => { |
| 49 | + const parameters = Type.Object({ query: Type.String() }); |
| 50 | + const [snapshot] = snapshotSessionToolDefinitions([ |
| 51 | + { |
| 52 | + name: "search", |
| 53 | + label: "Search", |
| 54 | + description: "searches", |
| 55 | + parameters, |
| 56 | + execute: async () => ({ |
| 57 | + content: [{ type: "text" as const, text: "ok" }], |
| 58 | + }), |
| 59 | + }, |
| 60 | + ]); |
| 61 | + if (!snapshot) { |
| 62 | + throw new Error("missing snapshot"); |
| 63 | + } |
| 64 | + (parameters.properties.query as Record<string, unknown>).type = "number"; |
| 65 | + |
| 66 | + expect(snapshot.parameters).not.toBe(parameters); |
| 67 | + expect(snapshot.parameters).toMatchObject({ |
| 68 | + type: "object", |
| 69 | + properties: { query: { type: "string" } }, |
| 70 | + }); |
| 71 | + expect(Object.getOwnPropertyDescriptor(snapshot.parameters, "~kind")).toMatchObject({ |
| 72 | + value: "Object", |
| 73 | + enumerable: false, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("keeps tools that intentionally omit a parameter schema", () => { |
| 78 | + const [tool] = wrapToolDefinitions([ |
| 79 | + { |
| 80 | + name: "no_args", |
| 81 | + label: "No Args", |
| 82 | + description: "accepts no arguments", |
| 83 | + parameters: undefined as never, |
| 84 | + execute: async () => ({ |
| 85 | + content: [{ type: "text" as const, text: "ok" }], |
| 86 | + }), |
| 87 | + }, |
| 88 | + ]); |
| 89 | + |
| 90 | + expect(tool?.name).toBe("no_args"); |
| 91 | + expect(tool?.parameters).toBeUndefined(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("skips unreadable AgentTool schemas while preserving healthy base overrides", () => { |
| 95 | + const badTool = { |
| 96 | + name: "bad_override", |
| 97 | + label: "Bad Override", |
| 98 | + description: "bad schema", |
| 99 | + execute: async () => ({ |
| 100 | + content: [{ type: "text" as const, text: "bad" }], |
| 101 | + }), |
| 102 | + } as AgentTool; |
| 103 | + Object.defineProperty(badTool, "parameters", { |
| 104 | + get: () => { |
| 105 | + throw new Error("revoked schema"); |
| 106 | + }, |
| 107 | + }); |
| 108 | + const healthyTool = { |
| 109 | + name: "healthy_override", |
| 110 | + label: "Healthy Override", |
| 111 | + description: "survives bad overrides", |
| 112 | + parameters: Type.Object({ query: Type.String() }), |
| 113 | + execute: async () => ({ |
| 114 | + content: [{ type: "text" as const, text: "ok" }], |
| 115 | + }), |
| 116 | + } satisfies AgentTool; |
| 117 | + |
| 118 | + const definitions = createToolDefinitionsFromAgentTools({ |
| 119 | + bad_override: badTool, |
| 120 | + healthy_override: healthyTool, |
| 121 | + }); |
| 122 | + |
| 123 | + expect(Object.keys(definitions)).toEqual(["healthy_override"]); |
| 124 | + }); |
| 125 | +}); |
0 commit comments