| 66 | } |
| 67 | |
| 68 | std::vector<ToolResult> ToolExecutor::execute_tools( |
| 69 | const std::vector<ToolCall>& tool_calls, |
| 70 | const ToolSet& tools, |
| 71 | const Messages& messages, |
| 72 | bool parallel, |
| 73 | const GenerateOptions* options) { |
| 74 | std::vector<ToolResult> results; |
| 75 | results.reserve(tool_calls.size()); |
| 76 | |
| 77 | if (!parallel) { |
| 78 | // Execute sequentially |
| 79 | for (const auto& tool_call : tool_calls) { |
| 80 | // Call the on_tool_call_start callback if provided |
| 81 | if (options && options->on_tool_call_start.has_value()) { |
| 82 | options->on_tool_call_start.value()(tool_call); |
| 83 | } |
| 84 | |
| 85 | auto result = execute_tool(tool_call, tools, messages); |
| 86 | |
| 87 | // Call the on_tool_call_finish callback if provided |
| 88 | if (options && options->on_tool_call_finish.has_value()) { |
| 89 | options->on_tool_call_finish.value()(result); |
| 90 | } |
| 91 | |
| 92 | results.push_back(result); |
| 93 | } |
| 94 | } else { |
| 95 | // Execute in parallel using futures |
| 96 | std::vector<std::future<ToolResult>> futures; |
| 97 | futures.reserve(tool_calls.size()); |
| 98 | |
| 99 | for (const auto& tool_call : tool_calls) { |
| 100 | futures.emplace_back( |
| 101 | std::async(std::launch::async, [&tool_call, &tools, &messages]() { |
| 102 | return execute_tool(tool_call, tools, messages); |
| 103 | })); |
| 104 | } |
| 105 | |
| 106 | // Collect results |
| 107 | for (auto& future : futures) { |
| 108 | results.push_back(future.get()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return results; |
| 113 | } |
| 114 | |
| 115 | std::vector<ToolResult> ToolExecutor::execute_tools_with_options( |
| 116 | const std::vector<ToolCall>& tool_calls, |
nothing calls this directly
no outgoing calls
no test coverage detected