fix: preserve message parts order in chat display (#151)

- Fix bug where text after tool calls was merged with initial text
- Group consecutive text/file parts into bubbles while keeping tools in order
- Parts now display as: plan -> tool_result -> additional text
- Remove debug logs from fixToolCallInputs function

Co-authored-by: dayuan.jiang <jiangdy@amazon.co.jp>
This commit is contained in:
Dayuan Jiang
2025-12-07 19:56:31 +09:00
committed by GitHub
parent 86420a42c6
commit 8c431ee6ed
2 changed files with 199 additions and 147 deletions

View File

@@ -67,41 +67,23 @@ function isMinimalDiagram(xml: string): boolean {
// Helper function to fix tool call inputs for Bedrock API
// Bedrock requires toolUse.input to be a JSON object, not a string
function fixToolCallInputs(messages: any[]): any[] {
return messages.map((msg, msgIndex) => {
return messages.map((msg) => {
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
return msg
}
const fixedContent = msg.content.map((part: any, partIndex: number) => {
const fixedContent = msg.content.map((part: any) => {
if (part.type === "tool-call") {
console.log(
`[fixToolCallInputs] msg[${msgIndex}].content[${partIndex}] tool-call:`,
{
toolName: part.toolName,
inputType: typeof part.input,
input: part.input,
},
)
if (typeof part.input === "string") {
try {
const parsed = JSON.parse(part.input)
console.log(
`[fixToolCallInputs] Parsed string input to JSON:`,
parsed,
)
return { ...part, input: parsed }
} catch {
// If parsing fails, wrap the string in an object
console.log(
`[fixToolCallInputs] Failed to parse, wrapping in object`,
)
return { ...part, input: { rawInput: part.input } }
}
}
// Input is already an object, but verify it's not null/undefined
if (part.input === null || part.input === undefined) {
console.log(
`[fixToolCallInputs] Input is null/undefined, using empty object`,
)
return { ...part, input: {} }
}
}