toolCalls property

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

Get tool calls from the response

Implementation

@override
List<ToolCall>? get toolCalls {
  final candidates = _rawResponse['candidates'] as List?;
  if (candidates == null || candidates.isEmpty) return null;

  final content = candidates.first['content'] as Map<String, dynamic>?;
  if (content == null) return null;

  final parts = content['parts'] as List?;
  if (parts == null || parts.isEmpty) return null;

  final functionCalls = <ToolCall>[];

  for (final part in parts) {
    final functionCall = part['functionCall'] as Map<String, dynamic>?;
    if (functionCall != null) {
      final name = functionCall['name'] as String;
      final args = functionCall['args'] as Map<String, dynamic>? ?? {};

      functionCalls.add(
        ToolCall(
          id: 'call_$name',
          callType: 'function',
          function: FunctionCall(name: name, arguments: jsonEncode(args)),
        ),
      );
    }
  }

  return functionCalls.isEmpty ? null : functionCalls;
}