toolCalls property

  1. @override
List<ToolCall>? get toolCalls
override

Get tool calls from the response

Implementation

@override
List<ToolCall>? get toolCalls {
  // First try the Responses API format
  final output = _rawResponse['output'] as List?;
  if (output != null) {
    final toolCalls = <ToolCall>[];

    // Look for function_call items in the output array
    for (final item in output) {
      if (item is Map<String, dynamic> && item['type'] == 'function_call') {
        try {
          // Convert Responses API function call format to ToolCall
          final toolCall = ToolCall(
            id: item['call_id'] as String? ?? item['id'] as String? ?? '',
            callType: 'function',
            function: FunctionCall(
              name: item['name'] as String? ?? '',
              arguments: item['arguments'] as String? ?? '{}',
            ),
          );
          toolCalls.add(toolCall);
        } catch (e) {
          // Skip malformed tool calls silently
          // Logging should be handled at a higher level
        }
      }
    }

    if (toolCalls.isNotEmpty) return toolCalls;
  }

  // Fallback to legacy format
  final toolCalls = _rawResponse['tool_calls'] as List?;
  if (toolCalls == null) return null;

  return toolCalls
      .map((tc) => ToolCall.fromJson(tc as Map<String, dynamic>))
      .toList();
}