| 1 | import { MessageCreateParams, ToolUnion } from "@anthropic-ai/sdk/resources"; |
| 2 | import { |
| 3 | CACHING_STRATEGIES, |
| 4 | CachingStrategyName, |
| 5 | getAvailableStrategies, |
| 6 | getStrategyDescription, |
| 7 | } from "./AnthropicCachingStrategies.js"; |
| 8 | |
| 9 | const body_params = { |
| 10 | model: "claude", |
| 11 | stream: true, |
| 12 | max_tokens: 8192, |
| 13 | }; |
| 14 | |
| 15 | const makeTool = (name: string): ToolUnion => ({ |
| 16 | name, |
| 17 | input_schema: { |
| 18 | type: "object", |
| 19 | }, |
| 20 | }); |
| 21 | |
| 22 | describe("AnthropicCachingStrategies", () => { |
| 23 | describe("noCachingStrategy", () => { |
| 24 | it("should return the body unchanged", () => { |
| 25 | const body: MessageCreateParams = { |
| 26 | system: "test", |
| 27 | messages: [], |
| 28 | ...body_params, |
| 29 | }; |
| 30 | const result = CACHING_STRATEGIES.none(body); |
| 31 | expect(result).toBe(body); |
| 32 | }); |
| 33 | |
| 34 | it("should not modify complex objects", () => { |
| 35 | const body: MessageCreateParams = { |
| 36 | system: [{ type: "text", text: "test" }], |
| 37 | tools: [makeTool("test")], |
| 38 | messages: [{ role: "user", content: "test" }], |
| 39 | ...body_params, |
| 40 | }; |
| 41 | const result = CACHING_STRATEGIES.none(body); |
| 42 | expect(result).toBe(body); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | describe("systemOnlyStrategy", () => { |
| 47 | it("should add cache_control to system messages array", () => { |
| 48 | const body: MessageCreateParams = { |
| 49 | system: [ |
| 50 | { type: "text", text: "system message 1" }, |
| 51 | { type: "text", text: "system message 2" }, |
| 52 | ], |
| 53 | messages: [{ role: "user", content: "user message" }], |
| 54 | ...body_params, |
| 55 | }; |
| 56 | |
| 57 | const result = CACHING_STRATEGIES.systemOnly(body); |
| 58 | |
| 59 | expect(result.system).toEqual([ |
| 60 | { |
| 61 | type: "text", |
| 62 | text: "system message 1", |
| 63 | cache_control: { type: "ephemeral" }, |
| 64 | }, |
| 65 | { |
| 66 | type: "text", |
| 67 | text: "system message 2", |
| 68 | cache_control: { type: "ephemeral" }, |
| 69 | }, |
| 70 | ]); |
| 71 | expect(result.messages).toEqual(body.messages); |
| 72 | }); |
| 73 | |
| 74 | it("should return body unchanged if system is not an array", () => { |
| 75 | const body: MessageCreateParams = { |
| 76 | system: "string system message", |
| 77 | messages: [{ role: "user", content: "user message" }], |
| 78 | ...body_params, |
| 79 | }; |
| 80 | |
| 81 | const result = CACHING_STRATEGIES.systemOnly(body); |
| 82 | expect(result).toEqual(body); |
| 83 | }); |
| 84 | |
| 85 | it("should return body unchanged if system is undefined", () => { |
| 86 | const body: MessageCreateParams = { |
| 87 | messages: [{ role: "user", content: "user message" }], |
| 88 | ...body_params, |
| 89 | }; |
| 90 | |
| 91 | const result = CACHING_STRATEGIES.systemOnly(body); |
| 92 | expect(result).toEqual(body); |
| 93 | }); |
| 94 | |
| 95 | it("should not add more than 5 cached messages", () => { |
| 96 | const body: MessageCreateParams = { |
| 97 | system: [ |
| 98 | { type: "text", text: "system message 1" }, |
| 99 | { type: "text", text: "system message 2" }, |
| 100 | { type: "text", text: "system message 3" }, |
| 101 | { type: "text", text: "system message 4" }, |
| 102 | { type: "text", text: "system message 5" }, |
| 103 | ], |
| 104 | messages: [{ role: "user", content: "user message" }], |
| 105 | ...body_params, |
| 106 | }; |
| 107 | |
| 108 | const result = CACHING_STRATEGIES.systemOnly(body); |
| 109 | |
| 110 | expect(result.system).toEqual([ |
| 111 | { |
| 112 | type: "text", |
| 113 | text: "system message 1", |
| 114 | cache_control: { type: "ephemeral" }, |
| 115 | }, |
| 116 | { |
| 117 | type: "text", |
| 118 | text: "system message 2", |
| 119 | cache_control: { type: "ephemeral" }, |
| 120 | }, |
| 121 | { |
| 122 | type: "text", |
| 123 | text: "system message 3", |
| 124 | cache_control: { type: "ephemeral" }, |
| 125 | }, |
| 126 | { |
| 127 | type: "text", |
| 128 | text: "system message 4", |
| 129 | cache_control: { type: "ephemeral" }, |
| 130 | }, |
| 131 | { type: "text", text: "system message 5" }, |
| 132 | ]); |
| 133 | expect(result.messages).toEqual(body.messages); |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | describe("systemAndToolsStrategy", () => { |
| 138 | it("should add cache_control to both system and tools", () => { |
| 139 | const body: MessageCreateParams = { |
| 140 | system: [{ type: "text", text: "system message" }], |
| 141 | tools: [makeTool("tool1"), makeTool("tool2")], |
| 142 | messages: [{ role: "user", content: "user message" }], |
| 143 | ...body_params, |
| 144 | }; |
| 145 | |
| 146 | const result = CACHING_STRATEGIES.systemAndTools(body); |
| 147 | |
| 148 | expect(result.system).toEqual([ |
| 149 | { |
| 150 | type: "text", |
| 151 | text: "system message", |
| 152 | cache_control: { type: "ephemeral" }, |
| 153 | }, |
| 154 | ]); |
| 155 | expect(result.tools).toEqual([ |
| 156 | { |
| 157 | name: "tool1", |
| 158 | input_schema: { |
| 159 | type: "object", |
| 160 | }, |
| 161 | }, |
| 162 | { |
| 163 | name: "tool2", |
| 164 | input_schema: { |
| 165 | type: "object", |
| 166 | }, |
| 167 | cache_control: { type: "ephemeral" }, |
| 168 | }, |
| 169 | ]); |
| 170 | expect(result.messages).toEqual(body.messages); |
| 171 | }); |
| 172 | |
| 173 | it("should handle missing system messages", () => { |
| 174 | const body: MessageCreateParams = { |
| 175 | tools: [makeTool("tool1")], |
| 176 | messages: [{ role: "user", content: "user message" }], |
| 177 | ...body_params, |
| 178 | }; |
| 179 | |
| 180 | const result = CACHING_STRATEGIES.systemAndTools(body); |
| 181 | |
| 182 | expect(result.tools).toEqual([ |
| 183 | { |
| 184 | name: "tool1", |
| 185 | input_schema: { |
| 186 | type: "object", |
| 187 | }, |
| 188 | cache_control: { type: "ephemeral" }, |
| 189 | }, |
| 190 | ]); |
| 191 | expect(result.messages).toEqual(body.messages); |
| 192 | }); |
| 193 | |
| 194 | it("should handle missing tools", () => { |
| 195 | const body: MessageCreateParams = { |
| 196 | system: [{ type: "text", text: "system message" }], |
| 197 | messages: [{ role: "user", content: "user message" }], |
| 198 | ...body_params, |
| 199 | }; |
| 200 | |
| 201 | const result = CACHING_STRATEGIES.systemAndTools(body); |
| 202 | |
| 203 | expect(result.system).toEqual([ |
| 204 | { |
| 205 | type: "text", |
| 206 | text: "system message", |
| 207 | cache_control: { type: "ephemeral" }, |
| 208 | }, |
| 209 | ]); |
| 210 | expect(result.messages).toEqual(body.messages); |
| 211 | }); |
| 212 | |
| 213 | it("should add only 4 cache controls in total to both system and tools", () => { |
| 214 | const body: MessageCreateParams = { |
| 215 | system: [ |
| 216 | { type: "text", text: "system message 1" }, |
| 217 | { type: "text", text: "system message 2" }, |
| 218 | { type: "text", text: "system message 3" }, |
| 219 | { type: "text", text: "system message 4" }, |
| 220 | ], |
| 221 | tools: [makeTool("tool1"), makeTool("tool2")], |
| 222 | messages: [{ role: "user", content: "user message" }], |
| 223 | ...body_params, |
| 224 | }; |
| 225 | |
| 226 | const result = CACHING_STRATEGIES.systemAndTools(body); |
| 227 | |
| 228 | expect(result.system).toEqual([ |
| 229 | { |
| 230 | type: "text", |
| 231 | text: "system message 1", |
| 232 | cache_control: { type: "ephemeral" }, |
| 233 | }, |
| 234 | { |
| 235 | type: "text", |
| 236 | text: "system message 2", |
| 237 | cache_control: { type: "ephemeral" }, |
| 238 | }, |
| 239 | { |
| 240 | type: "text", |
| 241 | text: "system message 3", |
| 242 | cache_control: { type: "ephemeral" }, |
| 243 | }, |
| 244 | { |
| 245 | type: "text", |
| 246 | text: "system message 4", |
| 247 | cache_control: { type: "ephemeral" }, |
| 248 | }, |
| 249 | ]); |
| 250 | expect(result.tools).toEqual(body.tools); |
| 251 | expect(result.messages).toEqual(body.messages); |
| 252 | }); |
| 253 | }); |
| 254 | |
| 255 | describe("optimizedStrategy", () => { |
| 256 | it("should cache system messages and tools", () => { |
| 257 | const body: MessageCreateParams = { |
| 258 | system: [{ type: "text", text: "system message" }], |
| 259 | tools: [makeTool("tool1")], |
| 260 | messages: [{ role: "user", content: "short message" }], |
| 261 | ...body_params, |
| 262 | }; |
| 263 | |
| 264 | const result = CACHING_STRATEGIES.optimized(body); |
| 265 | |
| 266 | expect(result.system).toEqual([ |
| 267 | { |
| 268 | type: "text", |
| 269 | text: "system message", |
| 270 | cache_control: { type: "ephemeral" }, |
| 271 | }, |
| 272 | ]); |
| 273 | expect(result.tools).toEqual([ |
| 274 | { |
| 275 | name: "tool1", |
| 276 | input_schema: { |
| 277 | type: "object", |
| 278 | }, |
| 279 | cache_control: { type: "ephemeral" }, |
| 280 | }, |
| 281 | ]); |
| 282 | }); |
| 283 | |
| 284 | it("should cache large string messages (>500 tokens)", () => { |
| 285 | const largeContent = "a".repeat(2100); // ~525 tokens |
| 286 | const body: MessageCreateParams = { |
| 287 | messages: [{ role: "user", content: largeContent }], |
| 288 | ...body_params, |
| 289 | }; |
| 290 | |
| 291 | const result = CACHING_STRATEGIES.optimized(body); |
| 292 | |
| 293 | expect(result.messages).toEqual([ |
| 294 | { |
| 295 | role: "user", |
| 296 | content: [ |
| 297 | { |
| 298 | type: "text", |
| 299 | text: largeContent, |
| 300 | cache_control: { type: "ephemeral" }, |
| 301 | }, |
| 302 | ], |
| 303 | }, |
| 304 | ]); |
| 305 | }); |
| 306 | |
| 307 | it("should not cache small string messages (<500 tokens)", () => { |
| 308 | const smallContent = "a".repeat(100); // ~25 tokens |
| 309 | const body: MessageCreateParams = { |
| 310 | messages: [{ role: "user", content: smallContent }], |
| 311 | ...body_params, |
| 312 | }; |
| 313 | |
| 314 | const result = CACHING_STRATEGIES.optimized(body); |
| 315 | |
| 316 | expect(result.messages).toEqual([ |
| 317 | { role: "user", content: smallContent }, |
| 318 | ]); |
| 319 | }); |
| 320 | |
| 321 | it("should cache large text items in array content but only once per message", () => { |
| 322 | const largeText = "a".repeat(2100); // ~525 tokens |
| 323 | const anotherLargeText = "b".repeat(2100); // Also ~525 tokens |
| 324 | const smallText = "a".repeat(100); // ~25 tokens |
| 325 | const body: MessageCreateParams = { |
| 326 | messages: [ |
| 327 | { |
| 328 | role: "user", |
| 329 | content: [ |
| 330 | { type: "text", text: largeText }, |
| 331 | { type: "text", text: anotherLargeText }, |
| 332 | { type: "text", text: smallText }, |
| 333 | { |
| 334 | type: "image", |
| 335 | source: { |
| 336 | data: "image_data", |
| 337 | media_type: "image/jpeg", |
| 338 | type: "base64", |
| 339 | }, |
| 340 | }, |
| 341 | ], |
| 342 | }, |
| 343 | ], |
| 344 | ...body_params, |
| 345 | }; |
| 346 | |
| 347 | const result = CACHING_STRATEGIES.optimized(body); |
| 348 | |
| 349 | // Only the first large text item should get cache_control |
| 350 | expect(result.messages).toEqual([ |
| 351 | { |
| 352 | role: "user", |
| 353 | content: [ |
| 354 | { |
| 355 | type: "text", |
| 356 | text: largeText, |
| 357 | cache_control: { type: "ephemeral" }, |
| 358 | }, |
| 359 | { type: "text", text: anotherLargeText }, |
| 360 | { type: "text", text: smallText }, |
| 361 | { |
| 362 | type: "image", |
| 363 | source: { |
| 364 | data: "image_data", |
| 365 | media_type: "image/jpeg", |
| 366 | type: "base64", |
| 367 | }, |
| 368 | }, |
| 369 | ], |
| 370 | }, |
| 371 | ]); |
| 372 | }); |
| 373 | |
| 374 | it("should add maximum 4 cache control blocks", () => { |
| 375 | const body: MessageCreateParams = { |
| 376 | system: [ |
| 377 | { type: "text", text: "system message 1" }, |
| 378 | { type: "text", text: "system message 2" }, |
| 379 | ], |
| 380 | tools: [makeTool("tool")], |
| 381 | messages: [ |
| 382 | { role: "user", content: "small content" }, |
| 383 | { role: "user", content: "a".repeat(2100) }, |
| 384 | { role: "user", content: "b".repeat(2100) }, |
| 385 | { |
| 386 | role: "user", |
| 387 | content: [ |
| 388 | { type: "text", text: "c".repeat(2100) }, |
| 389 | { |
| 390 | type: "image", |
| 391 | source: { |
| 392 | data: "image_data", |
| 393 | media_type: "image/jpeg", |
| 394 | type: "base64", |
| 395 | }, |
| 396 | }, |
| 397 | ], |
| 398 | }, |
| 399 | ], |
| 400 | ...body_params, |
| 401 | }; |
| 402 | |
| 403 | const result = CACHING_STRATEGIES.optimized(body); |
| 404 | |
| 405 | expect(result.system).toEqual([ |
| 406 | { |
| 407 | type: "text", |
| 408 | text: "system message 1", |
| 409 | cache_control: { type: "ephemeral" }, |
| 410 | }, |
| 411 | { |
| 412 | type: "text", |
| 413 | text: "system message 2", |
| 414 | cache_control: { type: "ephemeral" }, |
| 415 | }, |
| 416 | ]); |
| 417 | expect(result.tools).toEqual([ |
| 418 | { |
| 419 | name: "tool", |
| 420 | input_schema: { |
| 421 | type: "object", |
| 422 | }, |
| 423 | cache_control: { type: "ephemeral" }, |
| 424 | }, |
| 425 | ]); |
| 426 | expect(result.messages[0]).toEqual({ |
| 427 | role: "user", |
| 428 | content: "small content", |
| 429 | }); |
| 430 | |
| 431 | expect(result.messages[1]).toEqual({ |
| 432 | role: "user", |
| 433 | content: [ |
| 434 | { |
| 435 | type: "text", |
| 436 | text: "a".repeat(2100), |
| 437 | cache_control: { type: "ephemeral" }, |
| 438 | }, |
| 439 | ], |
| 440 | }); |
| 441 | expect(result.messages[2].content).toEqual(body.messages[2].content); |
| 442 | expect(result.messages[3].content).toEqual(body.messages[3].content); |
| 443 | }); |
| 444 | |
| 445 | it("should handle complex message structure", () => { |
| 446 | const body: MessageCreateParams = { |
| 447 | system: [{ type: "text", text: "system" }], |
| 448 | tools: [makeTool("tool")], |
| 449 | messages: [ |
| 450 | { role: "user", content: "small content" }, |
| 451 | { role: "user", content: "a".repeat(2100) }, // large content |
| 452 | { |
| 453 | role: "user", |
| 454 | content: [ |
| 455 | { type: "text", text: "a".repeat(2100) }, // large text |
| 456 | { |
| 457 | type: "image", |
| 458 | source: { |
| 459 | data: "image_data", |
| 460 | media_type: "image/jpeg", |
| 461 | type: "base64", |
| 462 | }, |
| 463 | }, |
| 464 | ], |
| 465 | }, |
| 466 | ], |
| 467 | ...body_params, |
| 468 | }; |
| 469 | |
| 470 | const result = CACHING_STRATEGIES.optimized(body); |
| 471 | |
| 472 | expect(result.system).toEqual([ |
| 473 | { type: "text", text: "system", cache_control: { type: "ephemeral" } }, |
| 474 | ]); |
| 475 | expect(result.tools).toEqual([ |
| 476 | { |
| 477 | name: "tool", |
| 478 | input_schema: { |
| 479 | type: "object", |
| 480 | }, |
| 481 | cache_control: { type: "ephemeral" }, |
| 482 | }, |
| 483 | ]); |
| 484 | expect(result.messages[0]).toEqual({ |
| 485 | role: "user", |
| 486 | content: "small content", |
| 487 | }); |
| 488 | expect(result.messages[1]).toEqual({ |
| 489 | role: "user", |
| 490 | content: [ |
| 491 | { |
| 492 | type: "text", |
| 493 | text: "a".repeat(2100), |
| 494 | cache_control: { type: "ephemeral" }, |
| 495 | }, |
| 496 | ], |
| 497 | }); |
| 498 | expect(result.messages[2].content[0]).toEqual({ |
| 499 | type: "text", |
| 500 | text: "a".repeat(2100), |
| 501 | cache_control: { type: "ephemeral" }, |
| 502 | }); |
| 503 | }); |
| 504 | |
| 505 | it("should handle empty or missing messages", () => { |
| 506 | const body: MessageCreateParams = { |
| 507 | system: [{ type: "text", text: "system" }], |
| 508 | messages: [], |
| 509 | ...body_params, |
| 510 | }; |
| 511 | |
| 512 | const result = CACHING_STRATEGIES.optimized(body); |
| 513 | |
| 514 | expect(result.system).toEqual([ |
| 515 | { type: "text", text: "system", cache_control: { type: "ephemeral" } }, |
| 516 | ]); |
| 517 | }); |
| 518 | }); |
| 519 | |
| 520 | describe("estimateTokenCount", () => { |
| 521 | it("should estimate token count correctly", () => { |
| 522 | // Test through the optimized strategy's behavior |
| 523 | const smallContent = "a".repeat(100); // ~25 tokens |
| 524 | const largeContent = "a".repeat(2100); // ~525 tokens |
| 525 | |
| 526 | const bodySmall: MessageCreateParams = { |
| 527 | messages: [{ role: "user", content: smallContent }], |
| 528 | ...body_params, |
| 529 | }; |
| 530 | const bodyLarge: MessageCreateParams = { |
| 531 | messages: [{ role: "user", content: largeContent }], |
| 532 | ...body_params, |
| 533 | }; |
| 534 | |
| 535 | const resultSmall = CACHING_STRATEGIES.optimized(bodySmall); |
| 536 | const resultLarge = CACHING_STRATEGIES.optimized(bodyLarge); |
| 537 | |
| 538 | // Small content should not be cached (remains string) |
| 539 | expect(resultSmall.messages[0].content).toBe(smallContent); |
| 540 | |
| 541 | // Large content should be cached (converted to array) |
| 542 | expect(Array.isArray(resultLarge.messages[0].content)).toBe(true); |
| 543 | }); |
| 544 | }); |
| 545 | |
| 546 | describe("getAvailableStrategies", () => { |
| 547 | it("should return all strategy names", () => { |
| 548 | const strategies = getAvailableStrategies(); |
| 549 | expect(strategies).toEqual([ |
| 550 | "none", |
| 551 | "systemOnly", |
| 552 | "systemAndTools", |
| 553 | "optimized", |
| 554 | ]); |
| 555 | }); |
| 556 | }); |
| 557 | |
| 558 | describe("getStrategyDescription", () => { |
| 559 | it("should return correct descriptions for all strategies", () => { |
| 560 | expect(getStrategyDescription("none")).toBe( |
| 561 | "No caching - baseline for comparison", |
| 562 | ); |
| 563 | expect(getStrategyDescription("systemOnly")).toBe( |
| 564 | "Cache only system messages (current implementation)", |
| 565 | ); |
| 566 | expect(getStrategyDescription("systemAndTools")).toBe( |
| 567 | "Cache system messages and tool definitions (high impact)", |
| 568 | ); |
| 569 | expect(getStrategyDescription("optimized")).toBe( |
| 570 | "Intelligent caching - system, tools, and large content (best performance)", |
| 571 | ); |
| 572 | }); |
| 573 | }); |
| 574 | |
| 575 | describe("CACHING_STRATEGIES", () => { |
| 576 | it("should contain all expected strategies", () => { |
| 577 | expect(Object.keys(CACHING_STRATEGIES)).toEqual([ |
| 578 | "none", |
| 579 | "systemOnly", |
| 580 | "systemAndTools", |
| 581 | "optimized", |
| 582 | ]); |
| 583 | }); |
| 584 | |
| 585 | it("should have all strategies as functions", () => { |
| 586 | Object.values(CACHING_STRATEGIES).forEach((strategy) => { |
| 587 | expect(typeof strategy).toBe("function"); |
| 588 | }); |
| 589 | }); |
| 590 | }); |
| 591 | describe("CachingStrategyName type", () => { |
| 592 | it("should work with valid strategy names", () => { |
| 593 | const strategyNames: CachingStrategyName[] = [ |
| 594 | "none", |
| 595 | "systemOnly", |
| 596 | "systemAndTools", |
| 597 | "optimized", |
| 598 | ]; |
| 599 | |
| 600 | strategyNames.forEach((name) => { |
| 601 | expect(typeof CACHING_STRATEGIES[name]).toBe("function"); |
| 602 | }); |
| 603 | }); |
| 604 | }); |
| 605 | }); |