( toolUseId: string, messages: Message[], messagesForAPI: (UserMessage | AssistantMessage)[], )
| 220 | |
| 221 | // Temp helper to log tool_use/tool_result mismatch errors |
| 222 | function logToolUseToolResultMismatch( |
| 223 | toolUseId: string, |
| 224 | messages: Message[], |
| 225 | messagesForAPI: (UserMessage | AssistantMessage)[], |
| 226 | ): void { |
| 227 | try { |
| 228 | // Find tool_use in normalized messages |
| 229 | let normalizedIndex = -1 |
| 230 | for (let i = 0; i < messagesForAPI.length; i++) { |
| 231 | const msg = messagesForAPI[i] |
| 232 | if (!msg) continue |
| 233 | const content = msg.message.content |
| 234 | if (Array.isArray(content)) { |
| 235 | for (const block of content) { |
| 236 | if ( |
| 237 | block.type === 'tool_use' && |
| 238 | 'id' in block && |
| 239 | block.id === toolUseId |
| 240 | ) { |
| 241 | normalizedIndex = i |
| 242 | break |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | if (normalizedIndex !== -1) break |
| 247 | } |
| 248 | |
| 249 | // Find tool_use in original messages |
| 250 | let originalIndex = -1 |
| 251 | for (let i = 0; i < messages.length; i++) { |
| 252 | const msg = messages[i] |
| 253 | if (!msg) continue |
| 254 | if (msg.type === 'assistant' && 'message' in msg) { |
| 255 | const content = msg.message.content |
| 256 | if (Array.isArray(content)) { |
| 257 | for (const block of content) { |
| 258 | if ( |
| 259 | block.type === 'tool_use' && |
| 260 | 'id' in block && |
| 261 | block.id === toolUseId |
| 262 | ) { |
| 263 | originalIndex = i |
| 264 | break |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | if (originalIndex !== -1) break |
| 270 | } |
| 271 | |
| 272 | // Build normalized sequence |
| 273 | const normalizedSeq: string[] = [] |
| 274 | for (let i = normalizedIndex + 1; i < messagesForAPI.length; i++) { |
| 275 | const msg = messagesForAPI[i] |
| 276 | if (!msg) continue |
| 277 | const content = msg.message.content |
| 278 | if (Array.isArray(content)) { |
| 279 | for (const block of content) { |
no test coverage detected