makeToolCalls method

  1. @override
Future<List<ToolResultMessage>> makeToolCalls({
  1. required List<Tool> tools,
  2. required List toolCalls,
})
override

Implementation

@override
Future<List<ToolResultMessage>> makeToolCalls({required List<Tool> tools, required List toolCalls}) async {
  // Log the tool calls
  logRequest({'tool_calls': toolCalls});

  final List<ToolResultMessage> toolCallResults = [];
  toolCalls = _removeDuplicityTools(toolCalls);

  for (final toolCall in toolCalls) {
    final function = toolCall['function'];
    final arguments = function['arguments'] is String ? jsonDecode(function['arguments']) : function['arguments'];
    final tool = tools.firstWhere((tool) => tool.name == function['name']);

    try {
      final value = await tool.call(arguments);
      toolCallResults.add(ToolResultMessage(id: toolCall['id'], content: value));

      // Log successful tool result
      logResponse({'id': toolCall['id'], 'result': value});
    } catch (e) {
      // Log tool error
      logError(e);

      // Still need to add a result even if there's an error
      toolCallResults.add(ToolResultMessage(id: toolCall['id'], content: 'Error: ${e.toString()}'));
    }
  }
  return toolCallResults;
}