Added javadoc, and fixed spelling errors.

This commit is contained in:
2025-03-14 14:32:38 +01:00
parent e7e8b445bf
commit 907f2e42e5
30 changed files with 1177 additions and 194 deletions

View File

@@ -1,12 +1,28 @@
package me.zacharias.chat.api;
/**
* API Endpoints
* see <a href="https://server.4zellen.se:3000/Zacharias/chat_thing/wiki/API-Docs">API Docs</a>
*/
public enum APIEndpoints {
// API request endpoints
/**
* Requests a tool to be added to the environment
*/
ADD_TOOL,
/**
* Requests a query to be executed on the environment
*/
QUERY,
// API response endpoints
/**
* Response to a tool request containing the Ollama response
*/
RESPONSE,
/**
* Response to use a tool defined by the API Client
*/
USE_TOOL,
}

View File

@@ -15,11 +15,29 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
/**
* The API server.<br>
* This class is responsible for handling the server socket and the clients for the API.<br>
* It also handles the output redirection and the message printing.<br>
* And the Registration of Tools on the {@link Core} object.
*/
public class APIServer {
/**
* The list of clients connected to the server.
*/
ArrayList<Client> clientsList = new ArrayList<>();
/**
* The server socket.
*/
ServerSocket serverSocket;
/**
* The output stream for the server.
*/
PrintStream dataOut;
/**
* The message handler for the server.
*/
PrintMessageHandler printMessageHandler = new PrintMessageHandler() {
@Override
public void printMessage(String message) {
@@ -40,11 +58,14 @@ public class APIServer {
return false;
}
};
/**
* The core object for the server.
*/
Core core = new Core(printMessageHandler);
/**
* Options foir this is expected to be passed thru the {@link LaunchOptions#instance} object.<br>
* Options for this is expected to be passed through the {@link LaunchOptions#instance} object.<br>
* Used objects:<br>
* - {@link LaunchOptions#autoAccept}<br>
* - {@link LaunchOptions#redirectOutput}<br>
@@ -58,12 +79,15 @@ public class APIServer {
core.setOllamaObject(OllamaObject.builder()
.setModel("llama3-AI")
.keep_alive(10)
.stream(false)
//.stream(false)
.build());
if (!Paths.get(redirectedOutput).toFile().getParentFile().exists()) {
if (redirectedOutput != null && !Paths.get(redirectedOutput).toFile().getParentFile().exists()) {
System.out.println("Failed to be able to open the redirected output file due to missing directory");
}
else {
redirectedOutput = "./out.txt";
}
File f = new File(redirectedOutput);
@@ -121,6 +145,19 @@ public class APIServer {
System.exit(1);
return;
}
System.out.println("Redirecting output to [" + redirectedOutput + "]");
System.out.println("Starting server on port [" + port + "]");
try{
serverSocket = new ServerSocket(port);
}
catch(Exception e){
System.out.println("Failed to start server on port [" + port + "]");
System.exit(1);
return;
}
//serverSocket = new ServerSocket(port);
}

View File

@@ -4,16 +4,28 @@ import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.Socket;
/**
* The client object for the API server.
*/
public class Client {
/**
* The socket for the client.
*/
Socket socket;
/**
* Creates a new client object.
* @param socket the socket for the client.
*/
public Client(Socket socket) {
this.socket = socket;
}
/**
* Returnes true unless if the method failes
* @param message the message to send to the client.
* @return true if the message was sent successfully, false otherwise.
*/
public boolean sendMessage(String message) {
try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {