diff --git a/Core/src/main/java/me/neurodock/core/Core.java b/Core/src/main/java/me/neurodock/core/Core.java index 91a9d93..f2b1f8f 100644 --- a/Core/src/main/java/me/neurodock/core/Core.java +++ b/Core/src/main/java/me/neurodock/core/Core.java @@ -23,6 +23,7 @@ import java.nio.file.Path; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -688,13 +689,17 @@ public class Core { message.getJSONArray("tool_calls") )); + List> futures = new ArrayList<>(); + // Process each tool call for(Object call : message.getJSONArray("tool_calls")) { - processToolCall(call); + futures.add(processToolCall(call)); } - checkIfResponceMessage(response); - qurryOllama().thenAccept(this::handleResponse); + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(result -> { + checkIfResponceMessage(response); + qurryOllama().thenAccept(this::handleResponse); + }); } /** @@ -705,21 +710,23 @@ public class Core { * * @param call a JSONObject representing the tool call */ - private void processToolCall(Object call) { - if(!(call instanceof JSONObject jsonObject)) return; - if(!jsonObject.has("function")) return; + private CompletableFuture processToolCall(Object call) { + return CompletableFuture.runAsync(() -> { + if (!(call instanceof JSONObject jsonObject)) return; + if (!jsonObject.has("function")) return; - JSONObject function = jsonObject.getJSONObject("function"); - OllamaFunctionTool func = findTool(function); + JSONObject function = jsonObject.getJSONObject("function"); + OllamaFunctionTool func = findTool(function); - if(func == null) { - reportToolNotFound(function); - return; - } + if (func == null) { + reportToolNotFound(function); + return; + } - JSONObject arguments = function.getJSONObject("arguments"); - renderToolCalling(func.renderCalling(function), function, arguments); - executeToolCall(func, arguments); + JSONObject arguments = function.getJSONObject("arguments"); + renderToolCalling(func.renderCalling(function), function, arguments); + executeToolCall(func, arguments); + }); } /**