( output: OpenAI.Responses.ResponseOutputItem[] )
| 319 | * Extracts tool calls from Responses API output items. |
| 320 | */ |
| 321 | export function extractResponseToolCalls( |
| 322 | output: OpenAI.Responses.ResponseOutputItem[] |
| 323 | ): ResponsesToolCall[] { |
| 324 | if (!Array.isArray(output)) { |
| 325 | return [] |
| 326 | } |
| 327 | |
| 328 | const toolCalls: ResponsesToolCall[] = [] |
| 329 | |
| 330 | for (const item of output) { |
| 331 | if (!isRecord(item)) { |
| 332 | continue |
| 333 | } |
| 334 | |
| 335 | if (item.type === 'function_call') { |
| 336 | const fc = item as OpenAI.Responses.ResponseFunctionToolCall |
| 337 | const callId = fc.call_id ?? (typeof item.id === 'string' ? item.id : undefined) |
| 338 | const name = |
| 339 | fc.name ?? |
| 340 | (isRecord(item.function) && typeof item.function.name === 'string' |
| 341 | ? item.function.name |
| 342 | : undefined) |
| 343 | if (!callId || !name) { |
| 344 | continue |
| 345 | } |
| 346 | |
| 347 | const argumentsValue = |
| 348 | typeof fc.arguments === 'string' ? fc.arguments : JSON.stringify(fc.arguments ?? {}) |
| 349 | |
| 350 | toolCalls.push({ |
| 351 | id: callId, |
| 352 | name, |
| 353 | arguments: argumentsValue, |
| 354 | }) |
| 355 | continue |
| 356 | } |
| 357 | |
| 358 | // Handle Chat Completions-style tool_calls nested under message items |
| 359 | if (item.type === 'message' && Array.isArray(item.tool_calls)) { |
| 360 | for (const toolCall of item.tool_calls) { |
| 361 | const tc = toolCall as Record<string, unknown> |
| 362 | const fn = tc.function as Record<string, unknown> | undefined |
| 363 | const callId = tc.id as string | undefined |
| 364 | const name = (fn?.name ?? tc.name) as string | undefined |
| 365 | if (!callId || !name) { |
| 366 | continue |
| 367 | } |
| 368 | |
| 369 | const argumentsValue = |
| 370 | typeof fn?.arguments === 'string' ? fn.arguments : JSON.stringify(fn?.arguments ?? {}) |
| 371 | |
| 372 | toolCalls.push({ |
| 373 | id: callId, |
| 374 | name, |
| 375 | arguments: argumentsValue, |
| 376 | }) |
| 377 | } |
| 378 | } |
no test coverage detected