| 1 | import { |
| 2 | ChatMessage, |
| 3 | Chunk, |
| 4 | CompletionOptions, |
| 5 | UserChatMessage, |
| 6 | } from "../../index.js"; |
| 7 | import Bedrock from "./Bedrock.js"; |
| 8 | |
| 9 | // Mock AWS SDK |
| 10 | jest.mock("@aws-sdk/client-bedrock-runtime", () => ({ |
| 11 | BedrockRuntimeClient: jest.fn().mockImplementation(() => ({ |
| 12 | send: jest.fn(), |
| 13 | middlewareStack: { add: jest.fn() }, |
| 14 | })), |
| 15 | ConverseStreamCommand: jest.fn(), |
| 16 | InvokeModelCommand: jest.fn(), |
| 17 | })); |
| 18 | |
| 19 | // Mock credential provider |
| 20 | jest.mock("@aws-sdk/credential-providers", () => ({ |
| 21 | fromNodeProviderChain: jest.fn().mockImplementation(() => async () => ({ |
| 22 | accessKeyId: "test-access-key", |
| 23 | secretAccessKey: "test-secret-key", |
| 24 | sessionToken: "test-session-token", |
| 25 | })), |
| 26 | })); |
| 27 | |
| 28 | // Extend CompletionOptions for testing |
| 29 | interface TestCompletionOptions extends CompletionOptions { |
| 30 | mockError?: string; |
| 31 | mockToolUse?: boolean; |
| 32 | } |
| 33 | |
| 34 | // Create a test class that overrides the protected methods |
| 35 | class TestBedrock extends Bedrock { |
| 36 | // Make _streamChat public for testing |
| 37 | public async *streamChat( |
| 38 | messages: ChatMessage[], |
| 39 | signal: AbortSignal, |
| 40 | options: TestCompletionOptions, |
| 41 | ): AsyncGenerator<ChatMessage> { |
| 42 | if (options.mockError) { |
| 43 | throw new Error(options.mockError); |
| 44 | } |
| 45 | |
| 46 | if (options.mockToolUse) { |
| 47 | yield { |
| 48 | role: "assistant", |
| 49 | content: "", |
| 50 | toolCalls: [ |
| 51 | { |
| 52 | id: "tool-1", |
| 53 | type: "function", |
| 54 | function: { |
| 55 | name: "test_tool", |
| 56 | arguments: '{"arg1":"value1"}', |
| 57 | }, |
| 58 | }, |
| 59 | ], |
| 60 | }; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | yield { role: "assistant", content: "Hello" }; |
| 65 | yield { role: "assistant", content: " world" }; |
| 66 | } |
| 67 | |
| 68 | // Override embed to avoid making real API calls |
| 69 | async embed(chunks: string[]): Promise<number[][]> { |
| 70 | if (chunks[0] === "error") { |
| 71 | throw new Error("Embedding Error"); |
| 72 | } |
| 73 | return [[0.1, 0.2, 0.3]]; |
| 74 | } |
| 75 | |
| 76 | // Override rerank to avoid making real API calls |
| 77 | async rerank(query: string, chunks: Chunk[]): Promise<number[]> { |
| 78 | if (!query || !chunks.length) { |
| 79 | throw new Error("Query and chunks must not be empty"); |
| 80 | } |
| 81 | |
| 82 | if (query === "error") { |
| 83 | throw new Error("Reranking Error"); |
| 84 | } |
| 85 | |
| 86 | return chunks.map((_, i) => 0.8 - i * 0.3); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | describe("Bedrock", () => { |
| 91 | beforeEach(() => { |
| 92 | jest.clearAllMocks(); |
| 93 | }); |
| 94 | |
| 95 | describe("constructor", () => { |
| 96 | it("should initialize with default options", () => { |
| 97 | const bedrock = new TestBedrock({ |
| 98 | apiKey: "test-key", |
| 99 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 100 | region: "us-east-1", |
| 101 | }); |
| 102 | |
| 103 | expect(bedrock.region).toBe("us-east-1"); |
| 104 | expect(bedrock.profile).toBe("bedrock"); |
| 105 | expect(bedrock.apiBase).toBe( |
| 106 | "https://bedrock-runtime.us-east-1.amazonaws.com", |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | it("should use custom options when provided", () => { |
| 111 | const bedrock = new TestBedrock({ |
| 112 | apiKey: "test-key", |
| 113 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 114 | region: "us-west-2", |
| 115 | profile: "custom-profile", |
| 116 | apiBase: "https://custom-endpoint.amazonaws.com/", |
| 117 | }); |
| 118 | |
| 119 | expect(bedrock.region).toBe("us-west-2"); |
| 120 | expect(bedrock.profile).toBe("custom-profile"); |
| 121 | expect(bedrock.apiBase).toBe("https://custom-endpoint.amazonaws.com/"); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | describe("streamChat", () => { |
| 126 | let bedrock: TestBedrock; |
| 127 | const mockAbortSignal = new AbortController().signal; |
| 128 | const defaultOptions: TestCompletionOptions = { |
| 129 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 130 | }; |
| 131 | |
| 132 | beforeEach(() => { |
| 133 | bedrock = new TestBedrock({ |
| 134 | apiKey: "test-key", |
| 135 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 136 | region: "us-east-1", |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | it("should handle text streaming correctly", async () => { |
| 141 | const messages: ChatMessage[] = [ |
| 142 | { |
| 143 | role: "user", |
| 144 | content: "Hi", |
| 145 | } as UserChatMessage, |
| 146 | ]; |
| 147 | |
| 148 | const results = []; |
| 149 | |
| 150 | for await (const message of bedrock.streamChat( |
| 151 | messages, |
| 152 | mockAbortSignal, |
| 153 | defaultOptions, |
| 154 | )) { |
| 155 | results.push(message); |
| 156 | } |
| 157 | |
| 158 | expect(results).toEqual([ |
| 159 | { role: "assistant", content: "Hello" }, |
| 160 | { role: "assistant", content: " world" }, |
| 161 | ]); |
| 162 | }); |
| 163 | |
| 164 | it("should handle tool use streaming correctly", async () => { |
| 165 | const messages: ChatMessage[] = [ |
| 166 | { |
| 167 | role: "user", |
| 168 | content: "Use a tool", |
| 169 | } as UserChatMessage, |
| 170 | ]; |
| 171 | |
| 172 | const results = []; |
| 173 | const options: TestCompletionOptions = { |
| 174 | ...defaultOptions, |
| 175 | mockToolUse: true, |
| 176 | }; |
| 177 | |
| 178 | for await (const message of bedrock.streamChat( |
| 179 | messages, |
| 180 | mockAbortSignal, |
| 181 | options, |
| 182 | )) { |
| 183 | results.push(message); |
| 184 | } |
| 185 | |
| 186 | expect(results).toEqual([ |
| 187 | { |
| 188 | role: "assistant", |
| 189 | content: "", |
| 190 | toolCalls: [ |
| 191 | { |
| 192 | id: "tool-1", |
| 193 | type: "function", |
| 194 | function: { |
| 195 | name: "test_tool", |
| 196 | arguments: '{"arg1":"value1"}', |
| 197 | }, |
| 198 | }, |
| 199 | ], |
| 200 | }, |
| 201 | ]); |
| 202 | }); |
| 203 | |
| 204 | it("should handle API errors", async () => { |
| 205 | const messages: ChatMessage[] = [ |
| 206 | { |
| 207 | role: "user", |
| 208 | content: "Hi", |
| 209 | } as UserChatMessage, |
| 210 | ]; |
| 211 | |
| 212 | const options: TestCompletionOptions = { |
| 213 | ...defaultOptions, |
| 214 | mockError: "API Error", |
| 215 | }; |
| 216 | |
| 217 | await expect(async () => { |
| 218 | for await (const _ of bedrock.streamChat( |
| 219 | messages, |
| 220 | mockAbortSignal, |
| 221 | options, |
| 222 | )) { |
| 223 | // consume generator |
| 224 | } |
| 225 | }).rejects.toThrow("API Error"); |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | describe("embed", () => { |
| 230 | let bedrock: TestBedrock; |
| 231 | |
| 232 | beforeEach(() => { |
| 233 | bedrock = new TestBedrock({ |
| 234 | apiKey: "test-key", |
| 235 | model: "cohere.embed-english-v3", |
| 236 | region: "us-east-1", |
| 237 | }); |
| 238 | }); |
| 239 | |
| 240 | it("should handle embedding requests correctly", async () => { |
| 241 | const result = await bedrock.embed(["test text"]); |
| 242 | expect(result).toEqual([[0.1, 0.2, 0.3]]); |
| 243 | }); |
| 244 | |
| 245 | it("should handle embedding errors", async () => { |
| 246 | await expect(bedrock.embed(["error"])).rejects.toThrow("Embedding Error"); |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | describe("rerank", () => { |
| 251 | let bedrock: TestBedrock; |
| 252 | |
| 253 | beforeEach(() => { |
| 254 | bedrock = new TestBedrock({ |
| 255 | apiKey: "test-key", |
| 256 | model: "cohere.rerank-english-v2.0", |
| 257 | region: "us-east-1", |
| 258 | }); |
| 259 | }); |
| 260 | |
| 261 | it("should handle reranking requests correctly", async () => { |
| 262 | const chunks: Chunk[] = [ |
| 263 | { |
| 264 | content: "doc1", |
| 265 | digest: "digest1", |
| 266 | filepath: "file1", |
| 267 | index: 0, |
| 268 | startLine: 1, |
| 269 | endLine: 1, |
| 270 | }, |
| 271 | { |
| 272 | content: "doc2", |
| 273 | digest: "digest2", |
| 274 | filepath: "file2", |
| 275 | index: 1, |
| 276 | startLine: 1, |
| 277 | endLine: 1, |
| 278 | }, |
| 279 | ]; |
| 280 | |
| 281 | const result = await bedrock.rerank("query", chunks); |
| 282 | expect(result).toEqual([0.8, 0.5]); |
| 283 | }); |
| 284 | |
| 285 | it("should handle reranking errors", async () => { |
| 286 | const chunk: Chunk = { |
| 287 | content: "doc1", |
| 288 | digest: "digest1", |
| 289 | filepath: "file1", |
| 290 | index: 0, |
| 291 | startLine: 1, |
| 292 | endLine: 1, |
| 293 | }; |
| 294 | |
| 295 | await expect(bedrock.rerank("error", [chunk])).rejects.toThrow( |
| 296 | "Reranking Error", |
| 297 | ); |
| 298 | }); |
| 299 | |
| 300 | it("should throw error for empty input", async () => { |
| 301 | await expect(bedrock.rerank("", [])).rejects.toThrow( |
| 302 | "Query and chunks must not be empty", |
| 303 | ); |
| 304 | }); |
| 305 | }); |
| 306 | |
| 307 | describe("message conversion", () => { |
| 308 | let bedrock: TestBedrock; |
| 309 | |
| 310 | beforeEach(() => { |
| 311 | bedrock = new TestBedrock({ |
| 312 | apiKey: "test-key", |
| 313 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 314 | region: "us-east-1", |
| 315 | }); |
| 316 | }); |
| 317 | |
| 318 | it("should convert simple user and assistant messages correctly", () => { |
| 319 | const messages: ChatMessage[] = [ |
| 320 | { role: "user", content: "Hello" } as UserChatMessage, |
| 321 | { role: "assistant", content: "Hi there" }, |
| 322 | ]; |
| 323 | |
| 324 | const availableTools = new Set<string>(); |
| 325 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 326 | |
| 327 | expect(converted).toEqual([ |
| 328 | { role: "user", content: [{ text: "Hello" }] }, |
| 329 | { role: "assistant", content: [{ text: "Hi there" }] }, |
| 330 | ]); |
| 331 | }); |
| 332 | |
| 333 | it("should handle tool responses correctly", () => { |
| 334 | const messages: ChatMessage[] = [ |
| 335 | { role: "user", content: "Use a tool" } as UserChatMessage, |
| 336 | { |
| 337 | role: "assistant", |
| 338 | content: "", |
| 339 | toolCalls: [ |
| 340 | { |
| 341 | id: "tool-1", |
| 342 | type: "function", |
| 343 | function: { |
| 344 | name: "test_tool", |
| 345 | arguments: '{"arg":"value"}', |
| 346 | }, |
| 347 | }, |
| 348 | ], |
| 349 | }, |
| 350 | { |
| 351 | role: "tool", |
| 352 | content: "Tool result", |
| 353 | toolCallId: "tool-1", |
| 354 | }, |
| 355 | ]; |
| 356 | |
| 357 | const availableTools = new Set(["test_tool"]); |
| 358 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 359 | |
| 360 | expect(converted).toEqual([ |
| 361 | { role: "user", content: [{ text: "Use a tool" }] }, |
| 362 | { |
| 363 | role: "assistant", |
| 364 | content: [ |
| 365 | { |
| 366 | toolUse: { |
| 367 | toolUseId: "tool-1", |
| 368 | name: "test_tool", |
| 369 | input: { arg: "value" }, |
| 370 | }, |
| 371 | }, |
| 372 | ], |
| 373 | }, |
| 374 | { |
| 375 | role: "user", |
| 376 | content: [ |
| 377 | { |
| 378 | toolResult: { |
| 379 | toolUseId: "tool-1", |
| 380 | content: [{ text: "Tool result" }], |
| 381 | }, |
| 382 | }, |
| 383 | ], |
| 384 | }, |
| 385 | ]); |
| 386 | }); |
| 387 | |
| 388 | it("should handle tool calls with unavailable tools as text", () => { |
| 389 | const messages: ChatMessage[] = [ |
| 390 | { role: "user", content: "Use a tool" } as UserChatMessage, |
| 391 | { |
| 392 | role: "assistant", |
| 393 | content: "", |
| 394 | toolCalls: [ |
| 395 | { |
| 396 | id: "tool-1", |
| 397 | type: "function", |
| 398 | function: { |
| 399 | name: "unavailable_tool", |
| 400 | arguments: '{"arg":"value"}', |
| 401 | }, |
| 402 | }, |
| 403 | ], |
| 404 | }, |
| 405 | ]; |
| 406 | |
| 407 | // Empty set means no tools are available |
| 408 | const availableTools = new Set<string>(); |
| 409 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 410 | |
| 411 | expect(converted).toEqual([ |
| 412 | { role: "user", content: [{ text: "Use a tool" }] }, |
| 413 | { |
| 414 | role: "assistant", |
| 415 | content: [ |
| 416 | { |
| 417 | text: expect.stringContaining("Assistant tool call:"), |
| 418 | }, |
| 419 | ], |
| 420 | }, |
| 421 | ]); |
| 422 | }); |
| 423 | |
| 424 | it("should handle thinking messages correctly", () => { |
| 425 | const messages: ChatMessage[] = [ |
| 426 | { role: "user", content: "Think about this" } as UserChatMessage, |
| 427 | { role: "thinking", content: "I am thinking about the solution" }, |
| 428 | { role: "assistant", content: "Here's my answer" }, |
| 429 | ]; |
| 430 | |
| 431 | const availableTools = new Set<string>(); |
| 432 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 433 | |
| 434 | expect(converted).toEqual([ |
| 435 | { role: "user", content: [{ text: "Think about this" }] }, |
| 436 | { |
| 437 | role: "assistant", |
| 438 | content: [ |
| 439 | { |
| 440 | reasoningContent: { |
| 441 | reasoningText: { |
| 442 | text: "I am thinking about the solution", |
| 443 | signature: undefined, |
| 444 | }, |
| 445 | }, |
| 446 | }, |
| 447 | { text: "Here's my answer" }, |
| 448 | ], |
| 449 | }, |
| 450 | ]); |
| 451 | }); |
| 452 | |
| 453 | it("should handle redacted thinking messages", () => { |
| 454 | const messages: ChatMessage[] = [ |
| 455 | { role: "user", content: "Think about this" } as UserChatMessage, |
| 456 | { |
| 457 | role: "thinking", |
| 458 | content: "", |
| 459 | redactedThinking: "redacted-content-bytes", |
| 460 | }, |
| 461 | { role: "assistant", content: "Here's my answer" }, |
| 462 | ]; |
| 463 | |
| 464 | const availableTools = new Set<string>(); |
| 465 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 466 | |
| 467 | expect(converted).toEqual([ |
| 468 | { role: "user", content: [{ text: "Think about this" }] }, |
| 469 | { |
| 470 | role: "assistant", |
| 471 | content: [ |
| 472 | { |
| 473 | reasoningContent: { |
| 474 | redactedContent: expect.any(Uint8Array), |
| 475 | }, |
| 476 | }, |
| 477 | { text: "Here's my answer" }, |
| 478 | ], |
| 479 | }, |
| 480 | ]); |
| 481 | |
| 482 | // Check that the Uint8Array contains the expected bytes |
| 483 | const redactedContent = |
| 484 | converted[1].content?.[0].reasoningContent?.redactedContent; |
| 485 | expect(redactedContent).toBeInstanceOf(Uint8Array); |
| 486 | expect(Buffer.from(redactedContent!).toString()).toBe( |
| 487 | "redacted-content-bytes", |
| 488 | ); |
| 489 | }); |
| 490 | |
| 491 | it("should convert multimodal content correctly", () => { |
| 492 | const messages: ChatMessage[] = [ |
| 493 | { |
| 494 | role: "user", |
| 495 | content: [ |
| 496 | { type: "text", text: "Look at this image:" }, |
| 497 | { |
| 498 | type: "imageUrl", |
| 499 | imageUrl: { |
| 500 | url: "data:image/jpeg;base64,/9j/4AAQSkZJRg==", |
| 501 | }, |
| 502 | }, |
| 503 | ], |
| 504 | } as UserChatMessage, |
| 505 | ]; |
| 506 | |
| 507 | const availableTools = new Set<string>(); |
| 508 | const converted = bedrock["_convertMessages"](messages, availableTools); |
| 509 | |
| 510 | expect(converted).toEqual([ |
| 511 | { |
| 512 | role: "user", |
| 513 | content: [ |
| 514 | { text: "Look at this image:" }, |
| 515 | { |
| 516 | image: { |
| 517 | format: "jpeg", |
| 518 | source: { |
| 519 | bytes: expect.any(Uint8Array), |
| 520 | }, |
| 521 | }, |
| 522 | }, |
| 523 | ], |
| 524 | }, |
| 525 | ]); |
| 526 | |
| 527 | // Check that the image bytes were processed correctly |
| 528 | const imageBytes = converted[0].content?.[1].image?.source?.bytes; |
| 529 | expect(imageBytes).toBeInstanceOf(Uint8Array); |
| 530 | expect(Buffer.from(imageBytes!).toString("base64")).toBe( |
| 531 | "/9j/4AAQSkZJRg==", |
| 532 | ); |
| 533 | }); |
| 534 | |
| 535 | it("should add cache points when cacheBehavior.cacheConversation is enabled", () => { |
| 536 | // Create a test instance with caching behavior enabled |
| 537 | const bedrockWithCaching = new TestBedrock({ |
| 538 | apiKey: "test-key", |
| 539 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 540 | region: "us-east-1", |
| 541 | }); |
| 542 | |
| 543 | // Set caching behavior |
| 544 | bedrockWithCaching.cacheBehavior = { |
| 545 | cacheConversation: true, |
| 546 | cacheSystemMessage: true, |
| 547 | }; |
| 548 | |
| 549 | const messages: ChatMessage[] = [ |
| 550 | { role: "system", content: "You are a helpful assistant" }, |
| 551 | { role: "user", content: "First message" } as UserChatMessage, |
| 552 | { role: "assistant", content: "First response" }, |
| 553 | { role: "user", content: "Second message" } as UserChatMessage, |
| 554 | ]; |
| 555 | |
| 556 | const availableTools = new Set<string>(); |
| 557 | const converted = bedrockWithCaching["_convertMessages"]( |
| 558 | messages, |
| 559 | availableTools, |
| 560 | ); |
| 561 | |
| 562 | // The last two user messages should have cache points |
| 563 | // But we're not testing the system message here since it's handled separately |
| 564 | expect(converted.length).toBe(3); |
| 565 | expect(converted[0].role).toBe("user"); |
| 566 | expect(converted[0].content?.some((block) => block.cachePoint)).toBe( |
| 567 | true, |
| 568 | ); |
| 569 | expect(converted[1].role).toBe("assistant"); |
| 570 | expect(converted[2].role).toBe("user"); |
| 571 | expect(converted[2].content?.some((block) => block.cachePoint)).toBe( |
| 572 | true, |
| 573 | ); |
| 574 | |
| 575 | // Verify that text content includes a cache ID suffix |
| 576 | const userMessageText = converted[0].content?.[0].text; |
| 577 | expect(userMessageText).toMatch(/First message.+/); |
| 578 | }); |
| 579 | |
| 580 | it("should add cache points when completionOptions.promptCaching is enabled", () => { |
| 581 | // Create a test instance with prompt caching enabled via completionOptions |
| 582 | const bedrockWithPromptCaching = new TestBedrock({ |
| 583 | apiKey: "test-key", |
| 584 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 585 | region: "us-east-1", |
| 586 | }); |
| 587 | |
| 588 | // Set completionOptions.promptCaching (without cacheBehavior) |
| 589 | bedrockWithPromptCaching.completionOptions = { |
| 590 | ...bedrockWithPromptCaching.completionOptions, |
| 591 | promptCaching: true, |
| 592 | }; |
| 593 | |
| 594 | const messages: ChatMessage[] = [ |
| 595 | { role: "user", content: "First message" } as UserChatMessage, |
| 596 | { role: "assistant", content: "First response" }, |
| 597 | { role: "user", content: "Second message" } as UserChatMessage, |
| 598 | ]; |
| 599 | |
| 600 | const availableTools = new Set<string>(); |
| 601 | const converted = bedrockWithPromptCaching["_convertMessages"]( |
| 602 | messages, |
| 603 | availableTools, |
| 604 | ); |
| 605 | |
| 606 | // The last two user messages should have cache points |
| 607 | expect(converted.length).toBe(3); |
| 608 | expect(converted[0].role).toBe("user"); |
| 609 | expect(converted[0].content?.some((block) => block.cachePoint)).toBe( |
| 610 | true, |
| 611 | ); |
| 612 | expect(converted[1].role).toBe("assistant"); |
| 613 | expect(converted[2].role).toBe("user"); |
| 614 | expect(converted[2].content?.some((block) => block.cachePoint)).toBe( |
| 615 | true, |
| 616 | ); |
| 617 | |
| 618 | // Verify that text content includes a cache ID suffix |
| 619 | const userMessageText = converted[0].content?.[0].text; |
| 620 | expect(userMessageText).toMatch(/First message.+/); |
| 621 | }); |
| 622 | |
| 623 | it("should not add cache points when both caching options are disabled", () => { |
| 624 | // Create a test instance with no caching enabled |
| 625 | const bedrockNoCaching = new TestBedrock({ |
| 626 | apiKey: "test-key", |
| 627 | model: "anthropic.claude-3-sonnet-20240229-v1:0", |
| 628 | region: "us-east-1", |
| 629 | }); |
| 630 | |
| 631 | // Explicitly set no caching |
| 632 | bedrockNoCaching.cacheBehavior = { |
| 633 | cacheConversation: false, |
| 634 | cacheSystemMessage: false, |
| 635 | }; |
| 636 | bedrockNoCaching.completionOptions = { |
| 637 | ...bedrockNoCaching.completionOptions, |
| 638 | promptCaching: false, |
| 639 | }; |
| 640 | |
| 641 | const messages: ChatMessage[] = [ |
| 642 | { role: "user", content: "First message" } as UserChatMessage, |
| 643 | { role: "assistant", content: "First response" }, |
| 644 | { role: "user", content: "Second message" } as UserChatMessage, |
| 645 | ]; |
| 646 | |
| 647 | const availableTools = new Set<string>(); |
| 648 | const converted = bedrockNoCaching["_convertMessages"]( |
| 649 | messages, |
| 650 | availableTools, |
| 651 | ); |
| 652 | |
| 653 | // No cache points should be added |
| 654 | expect(converted.length).toBe(3); |
| 655 | expect(converted[0].role).toBe("user"); |
| 656 | expect(converted[0].content?.some((block) => block.cachePoint)).toBe( |
| 657 | false, |
| 658 | ); |
| 659 | expect(converted[1].role).toBe("assistant"); |
| 660 | expect(converted[2].role).toBe("user"); |
| 661 | expect(converted[2].content?.some((block) => block.cachePoint)).toBe( |
| 662 | false, |
| 663 | ); |
| 664 | |
| 665 | // Verify that text content doesn't include a cache ID suffix |
| 666 | const userMessageText = converted[0].content?.[0].text; |
| 667 | expect(userMessageText).toBe("First message"); |
| 668 | }); |
| 669 | }); |
| 670 | }); |