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

This commit is contained in:
2026-06-28 17:18:41 +02:00
parent 3c6a75a587
commit caf60eddef
3 changed files with 50 additions and 49 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ plugins {
id 'java-library' id 'java-library'
} }
version = '1.6.6' version = '1.7'
dependencies { dependencies {
implementation project(":Plugin-API") implementation project(":Plugin-API")
+48 -47
View File
@@ -23,6 +23,7 @@ import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -601,65 +602,65 @@ public class Core {
* Sends the OllamaObject to Ollama * Sends the OllamaObject to Ollama
* @return The response from Ollama * @return The response from Ollama
*/ */
public JSONObject qurryOllama() public CompletableFuture<JSONObject> qurryOllama()
{ {
try { return CompletableFuture.supplyAsync(() -> {
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try {
connection.setRequestMethod("POST"); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(80*1000); 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())) { try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8)); wr.write(ollamaObjectString.getBytes(StandardCharsets.UTF_8));
wr.flush(); 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.
} }
}catch (Exception ex)
{ int responseCode = connection.getResponseCode();
// If the server returns an error, we read the error stream instead
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) { // 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; String line;
while ((line = reader.readLine()) != null) { 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(); connection.disconnect();
return new JSONObject(response.toString()); 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); checkIfResponceMessage(response);
handleResponse(qurryOllama()); qurryOllama().thenAccept(this::handleResponse);
} }
/** /**
@@ -169,7 +169,7 @@ public class Display {
writeLog("User: " + message); writeLog("User: " + message);
core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString())); core.getOllamaObject().addMessage(new OllamaMessage(OllamaMessageRole.USER, message.toString()));
//System.out.println(ollamaObject.toString()); //System.out.println(ollamaObject.toString());
core.handleResponse(core.qurryOllama()); core.qurryOllama().thenAccept(core::handleResponse);
} }
} }
} catch (Exception e) { } catch (Exception e) {