|
1 | 1 | import { describe, expect, it } from "bun:test"; |
2 | 2 |
|
3 | 3 | import { cacheSignature } from "../cache"; |
4 | | -import { recursivelyParseJsonStrings } from "../request-helpers"; |
| 4 | +import { normalizeToolCallArgs, recursivelyParseJsonStrings } from "../request-helpers"; |
| 5 | +import { cacheToolSchemas, clearToolSchemaCache } from "../tool-schema-cache"; |
5 | 6 | import { transformClaudeRequest } from "./claude"; |
6 | 7 | import { transformGeminiRequest } from "./gemini"; |
7 | 8 | import type { ModelFamily, RequestPayload, TransformContext } from "./types"; |
@@ -205,6 +206,101 @@ describe("thoughtSignature handling", () => { |
205 | 206 | }); |
206 | 207 | }); |
207 | 208 |
|
| 209 | + describe("normalizeToolCallArgs (Schema-Aware)", () => { |
| 210 | + it("preserves JSON string when schema says string", () => { |
| 211 | + clearToolSchemaCache(); |
| 212 | + cacheToolSchemas([ |
| 213 | + { |
| 214 | + functionDeclarations: [ |
| 215 | + { |
| 216 | + name: "write", |
| 217 | + parameters: { |
| 218 | + type: "object", |
| 219 | + properties: { |
| 220 | + content: { type: "string" }, |
| 221 | + filePath: { type: "string" } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + ] |
| 226 | + } |
| 227 | + ]); |
| 228 | + |
| 229 | + const args = { |
| 230 | + content: '{"name": "test", "version": "1.0.0"}', |
| 231 | + filePath: "test.json" |
| 232 | + }; |
| 233 | + |
| 234 | + const normalized = normalizeToolCallArgs(args, "write") as any; |
| 235 | + expect(typeof normalized.content).toBe("string"); |
| 236 | + expect(normalized.content).toBe('{"name": "test", "version": "1.0.0"}'); |
| 237 | + }); |
| 238 | + |
| 239 | + it("parses JSON string when schema says array", () => { |
| 240 | + clearToolSchemaCache(); |
| 241 | + cacheToolSchemas([ |
| 242 | + { |
| 243 | + functionDeclarations: [ |
| 244 | + { |
| 245 | + name: "read", |
| 246 | + parameters: { |
| 247 | + type: "object", |
| 248 | + properties: { |
| 249 | + files: { type: "array" } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + ] |
| 254 | + } |
| 255 | + ]); |
| 256 | + |
| 257 | + const args = { |
| 258 | + files: '[{"path": "a.txt"}]' |
| 259 | + }; |
| 260 | + |
| 261 | + const normalized = normalizeToolCallArgs(args, "read") as any; |
| 262 | + expect(Array.isArray(normalized.files)).toBe(true); |
| 263 | + expect(normalized.files[0].path).toBe("a.txt"); |
| 264 | + }); |
| 265 | + |
| 266 | + it("handles sanitized tool names correctly", () => { |
| 267 | + clearToolSchemaCache(); |
| 268 | + // Tool name starting with number |
| 269 | + cacheToolSchemas([ |
| 270 | + { |
| 271 | + functionDeclarations: [ |
| 272 | + { |
| 273 | + name: "21st-dev-magic", |
| 274 | + parameters: { |
| 275 | + type: "object", |
| 276 | + properties: { |
| 277 | + code: { type: "string" } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + ] |
| 282 | + } |
| 283 | + ]); |
| 284 | + |
| 285 | + const args = { |
| 286 | + code: '{"foo": "bar"}' |
| 287 | + }; |
| 288 | + |
| 289 | + // Try with sanitized name |
| 290 | + const normalized = normalizeToolCallArgs(args, "t_21st-dev-magic") as any; |
| 291 | + expect(typeof normalized.code).toBe("string"); |
| 292 | + expect(normalized.code).toBe('{"foo": "bar"}'); |
| 293 | + }); |
| 294 | + |
| 295 | + it("unescapes control characters in strings", () => { |
| 296 | + const args = { |
| 297 | + text: "line1\\nline2" |
| 298 | + }; |
| 299 | + const normalized = normalizeToolCallArgs(args, "any") as any; |
| 300 | + expect(normalized.text).toBe("line1\nline2"); |
| 301 | + }); |
| 302 | + }); |
| 303 | + |
208 | 304 | describe("claude transformer", () => { |
209 | 305 | it("keeps thinking blocks with valid signatures (length > 50)", () => { |
210 | 306 | const payload: RequestPayload = { |
|
0 commit comments