feat(api): major refactor from TCP socket to HTTP REST via Spring Boot

This commit introduces a large-scale refactor that replaces the existing TCP-based API with a Spring Boot-powered HTTP REST architecture. Due to the size and scope of this change, only essential structural notes are included below.

High-level changes:
- Replaced TCP-based communication with RESTful endpoints
- Introduced Spring Boot for API handling and configuration
- Refactored internal core logic to support REST architecture

New/Updated API components:
- `APIApplication.java`: Main Spring Boot entry point
- `MessageController.java`: Handles LLM-related queries
- `ToolController.java`: Handles adding/removing tools
- `NewToolRequest.java` / `NewToolResponse.java`: Data models for tool addition
- `NewQueryResponseHook.java`: Webhook handler for LLM query results
- `WebhookError.java`: Model for reporting webhook errors
- `EnableIfNotDisplay.java`: Conditional configuration for TTY context
- Other supporting classes (e.g., `ToolArgument`, `ToolRequest`)

Core changes:
- `Core.java`: Removed deprecated `addFunctionTool`, added `removeTool`
- `LaunchOptions.java`: Added `notDisplay` flag for headless operation
- `OllamaObject.java`: Implements tool removal logic

Launcher/display changes:
- `Launcher.java`: Starts `APIApplication` if not in TTY mode
- `Display.java`: Integrates REST API contextually with TTY display

NOTE: Several classes are included but not yet fully utilized; these are placeholders for upcoming features (e.g., `MessageResponse`, `ToolRequest`).

BREAKING CHANGE: This refactors removes all TCP-based API code and replaces it with HTTP REST using Spring Boot. Any clients or modules depending on the old TCP interface will need to be updated.
This commit is contained in:
2025-05-24 18:11:58 +02:00
parent e7cedb7f08
commit 8e44c11385
31 changed files with 771 additions and 452 deletions

View File

@@ -273,15 +273,17 @@ public class Core {
public OllamaObject getOllamaObject() {
return ollamaObject;
}
/**
* Adds a new tool to the System
* @deprecated Use {@link Core#addTool(OllamaFunctionTool, String)} instead
* @param funtionTool The tool to add
*/
@Deprecated
public void addFuntionTool(OllamaFunctionTool funtionTool) {
funtionTools.add(new Pair<>(funtionTool, "External"));
public void removeTool(String name) {
Pair<OllamaFunctionTool, String> funtionTool = funtionTools.stream().filter(tool -> tool.getKey().name().equalsIgnoreCase(name)).findFirst().orElse(null);
funtionTools.remove(funtionTool);
if(funtionTool.getKey() == null) {
// This should never happens... So if it does, Shit hit the fan
Exception e = new IllegalArgumentException("Function tool with name '"+name+"' does not exist");
e.printStackTrace();
System.exit(1);
}
ollamaObject.removeTool(funtionTool.getKey());
}
/**
@@ -306,8 +308,10 @@ public class Core {
connection.setDoOutput(true);
connection.setConnectTimeout(80*1000);
String ollamaObjectString = ollamaObject.toString();
try(DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.writeBytes(ollamaObject.toString());
wr.writeBytes(ollamaObjectString);
wr.flush();
}

View File

@@ -0,0 +1,29 @@
package me.zacharias.chat.core;
import java.util.HashMap;
import java.util.Map;
public class GlobalObjects {
private static final Map<String, Object> objects = new HashMap<>();
public static void addObject(String name, Object object) {
if (name == null || object == null) {
throw new IllegalArgumentException("Name and object cannot be null");
}
objects.put(name, object);
}
public static Object getObject(String name) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
return objects.get(name);
}
public static boolean removeObject(String name) {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null");
}
return objects.remove(name) != null;
}
}

View File

@@ -19,6 +19,7 @@ public class LaunchOptions {
private boolean autoAccept;
private boolean serverMode;
private boolean serverCredentialsEnabled;
private boolean notDisplay = true;
private int port = 39075;
private String redirectOutput;
private String serverCredentials;
@@ -143,4 +144,20 @@ public class LaunchOptions {
public void setServerCredentials(String serverCredentials) {
this.serverCredentials = serverCredentials;
}
/**
* Gets if the program is running in display mode.
* @return a boolean indicating if the program is running in display mode.
*/
public boolean isNotDisplay() {
return notDisplay;
}
/**
* Sets if the program is running in display mode.
* @param notDisplay a boolean indicating if the program is running in display mode.
*/
public void setNotDisplay(boolean notDisplay) {
this.notDisplay = notDisplay;
}
}

View File

@@ -134,6 +134,10 @@ public class OllamaObject {
public void addTool(OllamaTool tool) {
tools.add(tool);
}
public void removeTool(OllamaTool tool) {
tools.remove(tool);
}
/**
* Gets the format of the Ollama Object.