From caf60eddef61ed7e318db6f8970534311943dd8a Mon Sep 17 00:00:00 2001 From: Zacharias Date: Sun, 28 Jun 2026 17:18:41 +0200 Subject: [PATCH] Made Core#qurryOllama asynchronous instead to make so it doesn't lock up the calling thread, issue discovered in the (Ollama-chat)[https://git.server.4zellen.se/neurodock/Ollama-chat] Proof of Concept --- Core/build.gradle | 2 +- .../src/main/java/me/neurodock/core/Core.java | 95 ++++++++++--------- .../java/me/neurodock/display/Display.java | 2 +- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/Core/build.gradle b/Core/build.gradle index 041f977..23b11dc 100644 --- a/Core/build.gradle +++ b/Core/build.gradle @@ -2,7 +2,7 @@ plugins { id 'java-library' } -version = '1.6.6' +version = '1.7' dependencies { implementation project(":Plugin-API") diff --git a/Core/src/main/java/me/neurodock/core/Core.java b/Core/src/main/java/me/neurodock/core/Core.java index 452523e..91a9d93 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.concurrent.CompletableFuture; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -601,65 +602,65 @@ public class Core { * Sends the OllamaObject to Ollama * @return The response from Ollama */ - public JSONObject qurryOllama() + public CompletableFuture qurryOllama() { - try { - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setDoOutput(true); - connection.setConnectTimeout(80*1000); + return CompletableFuture.supplyAsync(() -> { + try { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + connection.setConnectTimeout(80 * 1000); - String ollamaObjectString = ollamaObject.toString(); + String ollamaObjectString = ollamaObject.toString(); - ollamaObjectString = ollamaObjectString.replace("\n", "\\n"); + ollamaObjectString = ollamaObjectString.replace("\n", "\\n"); - try(DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { - wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8)); - wr.flush(); - } - - int responseCode = connection.getResponseCode(); - - // HTTP_OK or 200 response code generally means that the server ran successfully without any errors - StringBuilder response = new StringBuilder(); - - // Read response content - // connection.getInputStream() purpose is to obtain an input stream for reading the server's response. - try ( - BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()))) { - String line; - while ((line = reader.readLine()) != null) { - response.append(line); // Adds every line to response till the end of file. + try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) { + wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8)); + wr.flush(); } - }catch (Exception ex) - { - // If the server returns an error, we read the error stream instead - try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) { + + int responseCode = connection.getResponseCode(); + + // HTTP_OK or 200 response code generally means that the server ran successfully without any errors + StringBuilder response = new StringBuilder(); + + // Read response content + // connection.getInputStream() purpose is to obtain an input stream for reading the server's response. + try ( + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { - response.append(line); + response.append(line); // Adds every line to response till the end of file. + } + } catch (Exception ex) { + // If the server returns an error, we read the error stream instead + InputStream errorStream = connection.getErrorStream(); + if (errorStream != null) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) { + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + } } - } catch (Exception e) { - System.err.println("Error reading error stream: " + e.getMessage()); } - } - if (responseCode == HttpURLConnection.HTTP_OK) { + if (responseCode == HttpURLConnection.HTTP_OK) { - connection.disconnect(); - return new JSONObject(response.toString()); + connection.disconnect(); + return new JSONObject(response.toString()); + } else { + connection.disconnect(); + printMessageHandler.printErrorMessage(new OllamaMessage(OllamaMessageRole.SYSTEM,"Error: HTTP Response code - " + responseCode + "\n" + response.toString())); + throw new RuntimeException("HTTP Response code - " + responseCode); + } + } catch (IOException e) { + throw new RuntimeException(e); } - else { - connection.disconnect(); - System.err.println("Error: HTTP Response code - " + responseCode + "\n"+response.toString()); - throw new RuntimeException("HTTP Response code - " + responseCode); - } - } catch (IOException e) { -// System.err.println("Error: JSON: "+); - throw new RuntimeException(e); - } + }); } /** @@ -693,7 +694,7 @@ public class Core { } checkIfResponceMessage(response); - handleResponse(qurryOllama()); + qurryOllama().thenAccept(this::handleResponse); } /** diff --git a/Display/src/main/java/me/neurodock/display/Display.java b/Display/src/main/java/me/neurodock/display/Display.java index fcdf9f3..6f9317c 100644 --- a/Display/src/main/java/me/neurodock/display/Display.java +++ b/Display/src/main/java/me/neurodock/display/Display.java @@ -169,7 +169,7 @@ public class Display { writeLog("User: " + message); core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString())); //System.out.println(ollamaObject.toString()); - core.handleResponse(core.qurryOllama()); + core.qurryOllama().thenAccept(core::handleResponse); } } } catch (Exception e) {