Changesets · Branches · Tags · Compare
| Changeset | 5db5405f58c07b9e517768c34eaf464c778731df32c2bf20bb2e21a4403a69d8 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-01 18:12 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-07-01 18:12 -07:00 |
| Parents | 693d0df27206 |
2 files changed
modified core/llm/llms/Bedrock.test.ts +248 −80
| @@ -1,12 +1,10 @@ | ||
| 1 | -import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime"; | |
| 2 | -import { fromNodeProviderChain } from "@aws-sdk/credential-providers"; | |
| 3 | -import Bedrock from "./Bedrock.js"; | |
| 4 | 1 | import { |
| 5 | 2 | ChatMessage, |
| 6 | 3 | Chunk, |
| 7 | 4 | CompletionOptions, |
| 8 | 5 | UserChatMessage, |
| 9 | 6 | } from "../../index.js"; |
| 7 | +import Bedrock from "./Bedrock.js"; | |
| 10 | 8 | |
| 11 | 9 | // Mock AWS SDK |
| 12 | 10 | jest.mock("@aws-sdk/client-bedrock-runtime", () => ({ |
| @@ -317,96 +315,266 @@ | ||
| 317 | 315 | }); |
| 318 | 316 | }); |
| 319 | 317 | |
| 320 | - it("should convert text messages correctly", () => { | |
| 321 | - const message: ChatMessage = { | |
| 322 | - role: "user", | |
| 323 | - content: "Hello", | |
| 324 | - } as UserChatMessage; | |
| 325 | - const converted = bedrock["_convertMessage"](message); | |
| 326 | - expect(converted).toEqual({ | |
| 327 | - role: "user", | |
| 328 | - content: [{ text: "Hello" }], | |
| 329 | - }); | |
| 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 | + ]); | |
| 330 | 331 | }); |
| 331 | 332 | |
| 332 | - it("should convert tool responses correctly", () => { | |
| 333 | - const message: ChatMessage = { | |
| 334 | - role: "tool", | |
| 335 | - content: "Tool result", | |
| 336 | - toolCallId: "tool-1", | |
| 337 | - }; | |
| 338 | - const converted = bedrock["_convertMessage"](message); | |
| 339 | - expect(converted).toEqual({ | |
| 340 | - role: "user", | |
| 341 | - content: [ | |
| 342 | - { | |
| 343 | - toolResult: { | |
| 344 | - toolUseId: "tool-1", | |
| 345 | - content: [{ text: "Tool result" }], | |
| 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 | + }, | |
| 346 | 347 | }, |
| 347 | - }, | |
| 348 | - ], | |
| 349 | - }); | |
| 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 | + ]); | |
| 350 | 386 | }); |
| 351 | 387 | |
| 352 | - it("should convert tool calls correctly", () => { | |
| 353 | - const message: ChatMessage = { | |
| 354 | - role: "assistant", | |
| 355 | - content: "", | |
| 356 | - toolCalls: [ | |
| 357 | - { | |
| 358 | - id: "tool-1", | |
| 359 | - type: "function" as const, | |
| 360 | - function: { | |
| 361 | - name: "test_tool", | |
| 362 | - arguments: '{"arg":"value"}', | |
| 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 | + }, | |
| 363 | 402 | }, |
| 364 | - }, | |
| 365 | - ], | |
| 366 | - }; | |
| 367 | - const converted = bedrock["_convertMessage"](message); | |
| 368 | - expect(converted).toEqual({ | |
| 369 | - role: "assistant", | |
| 370 | - content: [ | |
| 371 | - { | |
| 372 | - toolUse: { | |
| 373 | - toolUseId: "tool-1", | |
| 374 | - name: "test_tool", | |
| 375 | - input: { arg: "value" }, | |
| 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:"), | |
| 376 | 418 | }, |
| 377 | - }, | |
| 378 | - ], | |
| 379 | - }); | |
| 419 | + ], | |
| 420 | + }, | |
| 421 | + ]); | |
| 380 | 422 | }); |
| 381 | 423 | |
| 382 | - it("should handle multimodal content correctly", () => { | |
| 383 | - const message: ChatMessage = { | |
| 384 | - role: "user", | |
| 385 | - content: [ | |
| 386 | - { type: "text", text: "Look at this image:" }, | |
| 387 | - { | |
| 388 | - type: "imageUrl", | |
| 389 | - imageUrl: { | |
| 390 | - url: "data:image/jpeg;base64,/9j/4AAQSkZJRg==", | |
| 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 | + }, | |
| 391 | 446 | }, |
| 392 | - }, | |
| 393 | - ], | |
| 394 | - } as UserChatMessage; | |
| 395 | - const converted = bedrock["_convertMessage"](message); | |
| 396 | - expect(converted).toEqual({ | |
| 397 | - role: "user", | |
| 398 | - content: [ | |
| 399 | - { text: "Look at this image:" }, | |
| 400 | - { | |
| 401 | - image: { | |
| 402 | - format: "jpeg", | |
| 403 | - source: { | |
| 404 | - bytes: Buffer.from("/9j/4AAQSkZJRg==", "base64"), | |
| 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), | |
| 405 | 475 | }, |
| 406 | 476 | }, |
| 407 | - }, | |
| 408 | - ], | |
| 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 for prompt caching when needed", () => { | |
| 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", | |
| 409 | 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.+/); | |
| 410 | 578 | }); |
| 411 | 579 | }); |
| 412 | 580 | }); |
modified core/llm/llms/Bedrock.ts +61 −28
| @@ -392,6 +392,7 @@ | ||
| 392 | 392 | } |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | + const hasAddedToolCallIds = new Set<string>(); | |
| 395 | 396 | let currentRole: "user" | "assistant" = "user"; |
| 396 | 397 | let currentBlocks: ContentBlock[] = []; |
| 397 | 398 | let addCachePoint = false; |
| @@ -419,12 +420,19 @@ | ||
| 419 | 420 | |
| 420 | 421 | for (let idx = 0; idx < nonSystemMessages.length; idx++) { |
| 421 | 422 | const message = nonSystemMessages[idx]; |
| 423 | + | |
| 422 | 424 | if (message.role === "user" || message.role === "tool") { |
| 423 | - pushCurrentMessage(); | |
| 424 | - currentRole = ConversationRole.USER; | |
| 425 | + if (currentRole !== ConversationRole.USER) { | |
| 426 | + pushCurrentMessage(); | |
| 427 | + currentRole = ConversationRole.USER; | |
| 428 | + } | |
| 425 | 429 | |
| 426 | 430 | if (message.role === "user") { |
| 427 | - if (message.content) { | |
| 431 | + const trimmedContent = | |
| 432 | + typeof message.content === "string" | |
| 433 | + ? message.content.trim() | |
| 434 | + : message.content; | |
| 435 | + if (trimmedContent) { | |
| 428 | 436 | // Add cache_control parameter to the last two user messages |
| 429 | 437 | // The second-to-last because it retrieves potentially already cached contents, |
| 430 | 438 | // The last one because we want it cached for later retrieval. |
| @@ -438,47 +446,69 @@ | ||
| 438 | 446 | } |
| 439 | 447 | currentBlocks.push( |
| 440 | 448 | ...this._convertMessageContentToBlocks( |
| 441 | - message.content, | |
| 449 | + trimmedContent, | |
| 442 | 450 | messageIsCached, |
| 443 | 451 | ), |
| 444 | 452 | ); |
| 445 | 453 | } |
| 446 | 454 | } else if (message.role === "tool") { |
| 447 | - currentBlocks.push({ | |
| 448 | - toolResult: { | |
| 449 | - toolUseId: message.toolCallId, | |
| 450 | - content: [ | |
| 451 | - { | |
| 452 | - text: message.content || "", | |
| 453 | - }, | |
| 454 | - ], | |
| 455 | - }, | |
| 456 | - }); | |
| 455 | + const trimmedContent = message.content.trim() || "No tool output"; | |
| 456 | + if (hasAddedToolCallIds.has(message.toolCallId)) { | |
| 457 | + currentBlocks.push({ | |
| 458 | + toolResult: { | |
| 459 | + toolUseId: message.toolCallId, | |
| 460 | + content: [ | |
| 461 | + { | |
| 462 | + text: trimmedContent, | |
| 463 | + }, | |
| 464 | + ], | |
| 465 | + }, | |
| 466 | + }); | |
| 467 | + } else { | |
| 468 | + currentBlocks.push({ | |
| 469 | + text: `Tool call output for Tool Call ID ${message.toolCallId}:\n\n${trimmedContent}`, | |
| 470 | + }); | |
| 471 | + } | |
| 472 | + } | |
| 473 | + } else if (message.role === "assistant" || message.role === "thinking") { | |
| 474 | + if (currentRole !== ConversationRole.ASSISTANT) { | |
| 475 | + pushCurrentMessage(); | |
| 476 | + currentRole = ConversationRole.ASSISTANT; | |
| 457 | 477 | } |
| 458 | - } else { | |
| 459 | - pushCurrentMessage(); | |
| 460 | - currentRole = ConversationRole.ASSISTANT; | |
| 461 | 478 | |
| 462 | 479 | if (message.role === "assistant") { |
| 463 | - if (message.content) { | |
| 480 | + const trimmedContent = | |
| 481 | + typeof message.content === "string" | |
| 482 | + ? message.content.trim() | |
| 483 | + : message.content; | |
| 484 | + if (trimmedContent) { | |
| 464 | 485 | currentBlocks.push( |
| 465 | - ...this._convertMessageContentToBlocks(message.content, false), | |
| 486 | + ...this._convertMessageContentToBlocks(trimmedContent, false), | |
| 466 | 487 | ); |
| 467 | 488 | } |
| 468 | 489 | if (message.toolCalls) { |
| 469 | 490 | for (const toolCall of message.toolCalls) { |
| 470 | - if (availableTools.has(toolCall.function?.name || "")) { | |
| 471 | - currentBlocks.push({ | |
| 472 | - toolUse: { | |
| 473 | - toolUseId: toolCall.id, | |
| 474 | - name: toolCall.function?.name, | |
| 475 | - input: safeParseToolCallArgs(toolCall), | |
| 476 | - }, | |
| 477 | - }); | |
| 491 | + if (toolCall.id && toolCall.function?.name) { | |
| 492 | + if (availableTools.has(toolCall.function.name)) { | |
| 493 | + hasAddedToolCallIds.add(toolCall.id); | |
| 494 | + currentBlocks.push({ | |
| 495 | + toolUse: { | |
| 496 | + toolUseId: toolCall.id, | |
| 497 | + name: toolCall.function.name, | |
| 498 | + input: safeParseToolCallArgs(toolCall), | |
| 499 | + }, | |
| 500 | + }); | |
| 501 | + } else { | |
| 502 | + const toolCallText = `Assistant tool call:\nTool name: ${toolCall.function.name}\nTool Call ID: ${toolCall.id}\nArguments: ${toolCall.function?.arguments ?? "{}"}`; | |
| 503 | + currentBlocks.push({ | |
| 504 | + text: toolCallText, | |
| 505 | + }); | |
| 506 | + } | |
| 478 | 507 | } else { |
| 479 | 508 | console.warn( |
| 480 | - `Tool ${toolCall.function?.name} is not available for this model.`, | |
| 509 | + `Bedrock: tool call missing id or name, skipping tool call: ${JSON.stringify(toolCall)}`, | |
| 481 | 510 | ); |
| 511 | + continue; | |
| 482 | 512 | } |
| 483 | 513 | } |
| 484 | 514 | } |
| @@ -506,6 +536,9 @@ | ||
| 506 | 536 | } |
| 507 | 537 | } |
| 508 | 538 | } |
| 539 | + if (currentBlocks.length > 0) { | |
| 540 | + pushCurrentMessage(); | |
| 541 | + } | |
| 509 | 542 | |
| 510 | 543 | return converted; |
| 511 | 544 | } |