| 98 | // Helper function to fix tool call inputs for Bedrock API |
| 99 | // Bedrock requires toolUse.input to be a JSON object, not a string |
| 100 | function fixToolCallInputs(messages: any[]): any[] { |
| 101 | return messages.map((msg) => { |
| 102 | if (msg.role !== "assistant" || !Array.isArray(msg.content)) { |
| 103 | return msg |
| 104 | } |
| 105 | const fixedContent = msg.content.map((part: any) => { |
| 106 | if (part.type === "tool-call") { |
| 107 | if (typeof part.input === "string") { |
| 108 | try { |
| 109 | const parsed = JSON.parse(part.input) |
| 110 | return { ...part, input: parsed } |
| 111 | } catch { |
| 112 | // If parsing fails, wrap the string in an object |
| 113 | return { ...part, input: { rawInput: part.input } } |
| 114 | } |
| 115 | } |
| 116 | // Input is already an object, but verify it's not null/undefined |
| 117 | if (part.input === null || part.input === undefined) { |
| 118 | return { ...part, input: {} } |
| 119 | } |
| 120 | } |
| 121 | return part |
| 122 | }) |
| 123 | return { ...msg, content: fixedContent } |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | // Helper function to create cached stream response |
| 128 | function createCachedStreamResponse(xml: string): Response { |