Changesets · Branches · Tags · Compare
| Changeset | a968bb06ae87621e2086574ff86436ab5c9eb7f9daabcaf24fe2867d2c5c2940 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-01 20:14 -07:00 |
| Committer | Dallin Romney <dallinromney@gmail.com> · 2025-07-01 20:14 -07:00 |
| Parents | 5d2495085c35 |
1 file changed
modified core/llm/llms/Bedrock.ts +60 −49
| @@ -376,26 +376,16 @@ | ||
| 376 | 376 | }; |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | + /* | |
| 380 | + Converts the messages to the format expected by the Bedrock API. | |
| 381 | + | |
| 382 | + */ | |
| 379 | 383 | private _convertMessages( |
| 380 | 384 | messages: ChatMessage[], |
| 381 | 385 | availableTools: Set<string>, |
| 382 | 386 | ): Message[] { |
| 383 | - const nonSystemMessages = messages.filter((m) => m.role !== "system"); | |
| 384 | - | |
| 385 | - const lastTwoUserMsgIndices: number[] = []; | |
| 386 | - for (let i = nonSystemMessages.length - 1; i >= 0; i--) { | |
| 387 | - if (nonSystemMessages[i].role === "user") { | |
| 388 | - lastTwoUserMsgIndices.push(i); | |
| 389 | - if (lastTwoUserMsgIndices.length === 2) { | |
| 390 | - break; | |
| 391 | - } | |
| 392 | - } | |
| 393 | - } | |
| 394 | - | |
| 395 | - const hasAddedToolCallIds = new Set<string>(); | |
| 396 | 387 | let currentRole: "user" | "assistant" = "user"; |
| 397 | 388 | let currentBlocks: ContentBlock[] = []; |
| 398 | - let addCachePoint = false; | |
| 399 | 389 | |
| 400 | 390 | const converted: Message[] = []; |
| 401 | 391 | const pushCurrentMessage = () => { |
| @@ -405,53 +395,46 @@ | ||
| 405 | 395 | ); |
| 406 | 396 | } |
| 407 | 397 | if (currentBlocks.length > 0) { |
| 408 | - if (addCachePoint) { | |
| 409 | - currentBlocks.push({ cachePoint: { type: "default" } }); | |
| 410 | - } | |
| 411 | 398 | converted.push({ |
| 412 | 399 | role: currentRole, |
| 413 | 400 | content: currentBlocks, |
| 414 | 401 | }); |
| 415 | 402 | } |
| 416 | - // Change roles and reset | |
| 417 | 403 | currentBlocks = []; |
| 418 | - addCachePoint = false; | |
| 419 | 404 | }; |
| 420 | 405 | |
| 406 | + const nonSystemMessages = messages.filter((m) => m.role !== "system"); | |
| 407 | + const hasAddedToolCallIds = new Set<string>(); | |
| 408 | + | |
| 421 | 409 | for (let idx = 0; idx < nonSystemMessages.length; idx++) { |
| 422 | 410 | const message = nonSystemMessages[idx]; |
| 423 | 411 | |
| 424 | 412 | if (message.role === "user" || message.role === "tool") { |
| 413 | + // Detect conversational turn change | |
| 425 | 414 | if (currentRole !== ConversationRole.USER) { |
| 426 | 415 | pushCurrentMessage(); |
| 427 | 416 | currentRole = ConversationRole.USER; |
| 428 | 417 | } |
| 429 | 418 | |
| 419 | + // USER messages: | |
| 420 | + // Non-empty user message content is converted to "text" and "image" blocks | |
| 421 | + // If ANY user message part is cached, we add a single cache point block when we push the message | |
| 430 | 422 | if (message.role === "user") { |
| 431 | 423 | const trimmedContent = |
| 432 | 424 | typeof message.content === "string" |
| 433 | 425 | ? message.content.trim() |
| 434 | 426 | : message.content; |
| 435 | 427 | if (trimmedContent) { |
| 436 | - // Add cache_control parameter to the last two user messages | |
| 437 | - // The second-to-last because it retrieves potentially already cached contents, | |
| 438 | - // The last one because we want it cached for later retrieval. | |
| 439 | - // See: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html | |
| 440 | - const messageIsCached = !!( | |
| 441 | - this.cacheBehavior?.cacheConversation && | |
| 442 | - lastTwoUserMsgIndices.includes(idx) | |
| 443 | - ); | |
| 444 | - if (messageIsCached) { | |
| 445 | - addCachePoint = true; | |
| 446 | - } | |
| 447 | 428 | currentBlocks.push( |
| 448 | - ...this._convertMessageContentToBlocks( | |
| 449 | - trimmedContent, | |
| 450 | - messageIsCached, | |
| 451 | - ), | |
| 429 | + ...this._convertMessageContentToBlocks(trimmedContent), | |
| 452 | 430 | ); |
| 453 | 431 | } |
| 454 | - } else if (message.role === "tool") { | |
| 432 | + } | |
| 433 | + // TOOL messages: | |
| 434 | + // Tool messages are represented by "toolResult" blocks | |
| 435 | + // toolResult blocks must follow valid toolUse blocks (which also verifies that the tool name is present in toolConfig) | |
| 436 | + // If it doesn't, we convert it to a text block | |
| 437 | + else if (message.role === "tool") { | |
| 455 | 438 | const trimmedContent = message.content.trim() || "No tool output"; |
| 456 | 439 | if (hasAddedToolCallIds.has(message.toolCallId)) { |
| 457 | 440 | currentBlocks.push({ |
| @@ -471,11 +454,14 @@ | ||
| 471 | 454 | } |
| 472 | 455 | } |
| 473 | 456 | } else if (message.role === "assistant" || message.role === "thinking") { |
| 457 | + // Detect conversational turn change | |
| 474 | 458 | if (currentRole !== ConversationRole.ASSISTANT) { |
| 475 | 459 | pushCurrentMessage(); |
| 476 | 460 | currentRole = ConversationRole.ASSISTANT; |
| 477 | 461 | } |
| 478 | 462 | |
| 463 | + // ASSISTANT messages: | |
| 464 | + // Non-empty assistant message content is converted to "text" and "image" blocks | |
| 479 | 465 | if (message.role === "assistant") { |
| 480 | 466 | const trimmedContent = |
| 481 | 467 | typeof message.content === "string" |
| @@ -483,14 +469,18 @@ | ||
| 483 | 469 | : message.content; |
| 484 | 470 | if (trimmedContent) { |
| 485 | 471 | currentBlocks.push( |
| 486 | - ...this._convertMessageContentToBlocks(trimmedContent, false), | |
| 472 | + ...this._convertMessageContentToBlocks(trimmedContent), | |
| 487 | 473 | ); |
| 488 | 474 | } |
| 475 | + // TOOL CALLS: | |
| 476 | + // Tool calls are represented by "toolUse" blocks | |
| 477 | + // Each tool call must have an id and a function name | |
| 478 | + // The function name must match one of the available tools | |
| 479 | + // Otherwise, we will convert it to a text block (e.g. Chat mode will pass no tools) | |
| 489 | 480 | if (message.toolCalls) { |
| 490 | 481 | for (const toolCall of message.toolCalls) { |
| 491 | 482 | if (toolCall.id && toolCall.function?.name) { |
| 492 | 483 | if (availableTools.has(toolCall.function.name)) { |
| 493 | - hasAddedToolCallIds.add(toolCall.id); | |
| 494 | 484 | currentBlocks.push({ |
| 495 | 485 | toolUse: { |
| 496 | 486 | toolUseId: toolCall.id, |
| @@ -498,6 +488,7 @@ | ||
| 498 | 488 | input: safeParseToolCallArgs(toolCall), |
| 499 | 489 | }, |
| 500 | 490 | }); |
| 491 | + hasAddedToolCallIds.add(toolCall.id); | |
| 501 | 492 | } else { |
| 502 | 493 | const toolCallText = `Assistant tool call:\nTool name: ${toolCall.function.name}\nTool Call ID: ${toolCall.id}\nArguments: ${toolCall.function?.arguments ?? "{}"}`; |
| 503 | 494 | currentBlocks.push({ |
| @@ -513,6 +504,8 @@ | ||
| 513 | 504 | } |
| 514 | 505 | } |
| 515 | 506 | } else if (message.role === "thinking") { |
| 507 | + // THINKING: | |
| 508 | + // Thinking messages are represented by "reasoningContent" blocks which can have redacted content or reasoning content | |
| 516 | 509 | if (message.redactedThinking) { |
| 517 | 510 | const block: ContentBlock.ReasoningContentMember = { |
| 518 | 511 | reasoningContent: { |
| @@ -540,29 +533,47 @@ | ||
| 540 | 533 | pushCurrentMessage(); |
| 541 | 534 | } |
| 542 | 535 | |
| 536 | + // If caching is enabled, we add cache_control parameter to the last two user messages | |
| 537 | + // The second-to-last because it retrieves potentially already cached contents, | |
| 538 | + // The last one because we want it cached for later retrieval. | |
| 539 | + // See: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html | |
| 540 | + if (this.cacheBehavior?.cacheConversation) { | |
| 541 | + this._addCachingToLastTwoUserMessages(converted); | |
| 542 | + } | |
| 543 | + | |
| 543 | 544 | return converted; |
| 544 | 545 | } |
| 545 | 546 | |
| 547 | + private _addCachingToLastTwoUserMessages(converted: Message[]) { | |
| 548 | + let numCached = 0; | |
| 549 | + for (let i = converted.length - 1; i >= 0; i--) { | |
| 550 | + const message = converted[i]; | |
| 551 | + if (message.role === "user") { | |
| 552 | + message.content?.forEach((block) => { | |
| 553 | + if (block.text) { | |
| 554 | + block.text += getSecureID(); | |
| 555 | + } | |
| 556 | + }); | |
| 557 | + message.content?.push({ cachePoint: { type: "default" } }); | |
| 558 | + numCached++; | |
| 559 | + } | |
| 560 | + if (numCached === 2) { | |
| 561 | + break; | |
| 562 | + } | |
| 563 | + } | |
| 564 | + } | |
| 565 | + | |
| 566 | + // Converts Continue message content (string/parts) to Bedrock ContentBlock format. | |
| 567 | + // Unsupported/problematic image formats are skipped with a warning. | |
| 546 | 568 | private _convertMessageContentToBlocks( |
| 547 | 569 | content: MessageContent, |
| 548 | - addCaching: boolean, | |
| 549 | 570 | ): ContentBlock[] { |
| 550 | 571 | const blocks: ContentBlock[] = []; |
| 551 | 572 | if (typeof content === "string") { |
| 552 | - let contentText = content; | |
| 553 | - if (addCaching) { | |
| 554 | - contentText += getSecureID(); | |
| 555 | - } | |
| 556 | - blocks.push({ text: contentText }); | |
| 557 | - if (addCaching) { | |
| 558 | - blocks.push({ cachePoint: { type: "default" } }); | |
| 559 | - } | |
| 573 | + blocks.push({ text: content }); | |
| 560 | 574 | } else { |
| 561 | 575 | for (const part of content) { |
| 562 | 576 | if (part.type === "text") { |
| 563 | - if (addCaching) { | |
| 564 | - part.text += getSecureID(); | |
| 565 | - } | |
| 566 | 577 | blocks.push({ text: part.text }); |
| 567 | 578 | } else if (part.type === "imageUrl" && part.imageUrl) { |
| 568 | 579 | try { |