|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { createAnthropicAdapter } from "../src/adapters/anthropic"; |
| 3 | +import type { OcxParsedRequest, OcxProviderConfig } from "../src/types"; |
| 4 | + |
| 5 | +const provider = { adapter: "anthropic", baseUrl: "https://api.anthropic.com", apiKey: "sk-x", authMode: "apiKey" } as unknown as OcxProviderConfig; |
| 6 | + |
| 7 | +function toolSchema(parameters: unknown): Record<string, unknown> { |
| 8 | + const { body } = createAnthropicAdapter(provider).buildRequest({ |
| 9 | + modelId: "anthropic/claude-opus-4.1", |
| 10 | + stream: false, |
| 11 | + options: {}, |
| 12 | + context: { |
| 13 | + messages: [{ role: "user", content: "use the tool", timestamp: 0 }], |
| 14 | + tools: [{ name: "sample_tool", description: "Sample", parameters }], |
| 15 | + }, |
| 16 | + } as unknown as OcxParsedRequest); |
| 17 | + const parsed = JSON.parse(body as string) as { tools: Array<{ input_schema: Record<string, unknown> }> }; |
| 18 | + return parsed.tools[0].input_schema; |
| 19 | +} |
| 20 | + |
| 21 | +describe("anthropic tool input_schema normalization", () => { |
| 22 | + test("missing root type becomes an object schema with properties", () => { |
| 23 | + expect(toolSchema({ properties: { query: { type: "string" } }, required: ["query"] })).toEqual({ |
| 24 | + type: "object", |
| 25 | + properties: { query: { type: "string" } }, |
| 26 | + required: ["query"], |
| 27 | + }); |
| 28 | + expect(toolSchema({})).toEqual({ type: "object", properties: {} }); |
| 29 | + }); |
| 30 | + |
| 31 | + test("root oneOf and anyOf are flattened without promoting branch required fields", () => { |
| 32 | + const anyOf = toolSchema({ |
| 33 | + anyOf: [ |
| 34 | + { type: "object", properties: { a: { type: "string" } }, required: ["a"] }, |
| 35 | + { type: "object", properties: { b: { type: "number" } }, required: ["b"] }, |
| 36 | + ], |
| 37 | + }); |
| 38 | + expect(anyOf.anyOf).toBeUndefined(); |
| 39 | + expect(anyOf.oneOf).toBeUndefined(); |
| 40 | + expect(anyOf.allOf).toBeUndefined(); |
| 41 | + expect(anyOf).toEqual({ |
| 42 | + type: "object", |
| 43 | + properties: { a: { type: "string" }, b: { type: "number" } }, |
| 44 | + }); |
| 45 | + |
| 46 | + const oneOf = toolSchema({ |
| 47 | + oneOf: [ |
| 48 | + { type: "object", properties: { path: { type: "string" } }, required: ["path"] }, |
| 49 | + ], |
| 50 | + }); |
| 51 | + expect(oneOf).toEqual({ |
| 52 | + type: "object", |
| 53 | + properties: { path: { type: "string" } }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test("root allOf merges required fields and preserves sibling schema metadata", () => { |
| 58 | + expect(toolSchema({ |
| 59 | + title: "Search options", |
| 60 | + additionalProperties: false, |
| 61 | + properties: { existing: { type: "string" } }, |
| 62 | + required: ["existing"], |
| 63 | + allOf: [ |
| 64 | + { type: "object", properties: { limit: { type: "number" } }, required: ["limit"] }, |
| 65 | + { type: "object", properties: { sort: { type: "string" } } }, |
| 66 | + ], |
| 67 | + })).toEqual({ |
| 68 | + title: "Search options", |
| 69 | + additionalProperties: false, |
| 70 | + type: "object", |
| 71 | + properties: { |
| 72 | + existing: { type: "string" }, |
| 73 | + limit: { type: "number" }, |
| 74 | + sort: { type: "string" }, |
| 75 | + }, |
| 76 | + required: ["existing", "limit"], |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + test("nested composition under properties is preserved", () => { |
| 81 | + expect(toolSchema({ |
| 82 | + properties: { |
| 83 | + value: { |
| 84 | + anyOf: [{ type: "string" }, { type: "number" }], |
| 85 | + }, |
| 86 | + }, |
| 87 | + })).toEqual({ |
| 88 | + type: "object", |
| 89 | + properties: { |
| 90 | + value: { |
| 91 | + anyOf: [{ type: "string" }, { type: "number" }], |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments