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'
}
version = '1.6.6'
version = '1.7'
dependencies {
implementation project(":Plugin-API")
+15 -14
View File
@@ -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,20 +602,21 @@ public class Core {
* Sends the OllamaObject to Ollama
* @return The response from Ollama
*/
public JSONObject qurryOllama()
public CompletableFuture<JSONObject> qurryOllama()
{
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);
connection.setConnectTimeout(80 * 1000);
String ollamaObjectString = ollamaObject.toString();
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.flush();
}
@@ -627,21 +629,21 @@ public class Core {
// 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()))) {
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)
{
} catch (Exception ex) {
// If the server returns an error, we read the error stream instead
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
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());
}
}
}
@@ -650,16 +652,15 @@ public class Core {
connection.disconnect();
return new JSONObject(response.toString());
}
else {
} else {
connection.disconnect();
System.err.println("Error: HTTP Response code - " + responseCode + "\n"+response.toString());
printMessageHandler.printErrorMessage(new OllamaMessage(OllamaMessageRole.SYSTEM,"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);
}
/**
@@ -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) {