From ff0496eb6114623517f58f1eeba78079225e3436 Mon Sep 17 00:00:00 2001 From: zacharias Date: Mon, 15 Jun 2026 00:20:07 +0200 Subject: [PATCH] Shit has happend, among the "shit" is working on the files stuff and refactoring Core.Source to reflect on the new names for the difrent types of tool sources --- .idea/misc.xml | 39 ++++++++ .../src/main/java/me/neurodock/core/Core.java | 93 +++++++++---------- .../me/neurodock/core/files/FileHandler.java | 2 +- .../core/files/tools/ReadFileTool.java | 2 +- .../me/neurodock/core/memory/CoreMemory.java | 4 + .../neurodock/ollama/OllamaFunctionTools.java | 2 +- .../me/neurodock/ollama/OllamaObject.java | 34 ++++++- .../java/me/neurodock/display/Display.java | 10 +- .../java/me/neurodock/genius/GeniusTools.java | 2 +- .../java/me/neurodock/mal/api/MALAPITool.java | 2 +- .../me/neurodock/wikipedia/WikipediaTool.java | 2 +- 11 files changed, 130 insertions(+), 62 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ba3b46..ac6e8ea 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,6 +7,45 @@ + + + + diff --git a/Core/src/main/java/me/neurodock/core/Core.java b/Core/src/main/java/me/neurodock/core/Core.java index d05961b..dafcc09 100644 --- a/Core/src/main/java/me/neurodock/core/Core.java +++ b/Core/src/main/java/me/neurodock/core/Core.java @@ -74,6 +74,36 @@ public class Core { public static File PLUGIN_DIRECTORY; public static File CACHE_DIRECTORY; + /** + * Creates a new instance of Core with the provided PrintMessageHandler, + * defaulting the Ollama backend to {@code localhost}. + * + * @param printMessageHandler The PrintMessageHandler to use as the default output + */ + public Core(@NotNull PrintAdvanceMessageHandler printMessageHandler) { + this(printMessageHandler, "localhost"); + } + + /** + * Creates a new instance of Core with the provided PrintMessageHandler + * and a specific Ollama backend address. + * + * @param printMessageHandler The PrintMessageHandler to use as the default output + * @param ollamaIP The IP or hostname of the Ollama backend + */ + public Core(@NotNull PrintAdvanceMessageHandler printMessageHandler, @NotNull String ollamaIP) + { + this.printMessageHandler = printMessageHandler; + this.ollamaIP = ollamaIP; + + initDirectories(); + initOllamaUrl(); + confirmOllama(); + initLogWriter(); + initScheduler(); + initShutdownHook(); + } + static { // This should not be enforced like this, instead this should read a data field, or wait for an init to be called.... // Unsure of how to properly do this at this time, however. @@ -407,6 +437,7 @@ public class Core { private void confirmOllama() { try { + URL url = new URL("http://" + ollamaIP + ":" + ollamaPort + "/api/version"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); @@ -427,36 +458,6 @@ public class Core { } } - /** - * Creates a new instance of Core with the provided PrintMessageHandler, - * defaulting the Ollama backend to {@code localhost}. - * - * @param printMessageHandler The PrintMessageHandler to use as the default output - */ - public Core(@NotNull PrintAdvanceMessageHandler printMessageHandler) { - this(printMessageHandler, "localhost"); - } - - /** - * Creates a new instance of Core with the provided PrintMessageHandler - * and a specific Ollama backend address. - * - * @param printMessageHandler The PrintMessageHandler to use as the default output - * @param ollamaIP The IP or hostname of the Ollama backend - */ - public Core(@NotNull PrintAdvanceMessageHandler printMessageHandler, @NotNull String ollamaIP) - { - this.printMessageHandler = printMessageHandler; - this.ollamaIP = ollamaIP; - - initDirectories(); - initOllamaUrl(); - confirmOllama(); - initLogWriter(); - initScheduler(); - initShutdownHook(); - } - /** * Sets the {@link #ollamaObject} object to the provided argument, * Also adds the memory base system. See {@link Core#setOllamaObjectNoMemory} if you don't want to add memory functions @@ -768,31 +769,29 @@ public class Core { * Represents the source of a tool. *

* This is intended for use with {@link Core#addTool(OllamaFunctionTool, String)} - * to indicate the module from which a tool originates. + * to indicate the category from which a tool originates. */ public static class Source { /** - * Represents an external tool that is not derived from the Core. - * Instead, it belongs to an internally defined system or module within the project/program. - */ - public static final String EXTERNAL = "External"; - - /** - * Represents an internally defined tool that is part of the Core system. - * This is meant for tools that are strictly part of the Core and should not be used for definitions outside of it. + * Tools boundeld with the Core runtime (memory, file access, etc.) + * DO NOT USE THIS unless you are poking at core stuff :). */ public static final String CORE = "Core"; - + /** - * Represents a tool defined through an API system. - * These tools are more dynamic, as they originate from fully external sources using the API. + * Compile-Time Plugins: boundeld at build time as part of the application. + * Examples: MALAPITool, GeniusAPI, WikipediaTool. */ - public static final String API = "Api"; - + public static final String CTP = "CTP"; + /** - * Represents an internally defined tool that is derived from Core Components but not the Core itself. - * This is used for tools that are part of the Core Components, such as internal modules, but do not belong directly to the Core. + * Runtime Pre-Compiled Plugins: external plugins loaded at runtime via Plugin-API. */ - public static final String INTERNAL = "Internal"; + public static final String RPCP = "RPCP"; + + /** + * Tools defined via the RESt API interface dynamically. + */ + public static final String API = "API"; } } diff --git a/Core/src/main/java/me/neurodock/core/files/FileHandler.java b/Core/src/main/java/me/neurodock/core/files/FileHandler.java index e8e0566..2b1d2a3 100644 --- a/Core/src/main/java/me/neurodock/core/files/FileHandler.java +++ b/Core/src/main/java/me/neurodock/core/files/FileHandler.java @@ -54,7 +54,7 @@ public class FileHandler { public static ArrayList> getTools() { ArrayList> tools = new ArrayList<>(); - tools.add(new Pair<>(new ReadFileTool(), Core.Source.INTERNAL)); + tools.add(new Pair<>(new ReadFileTool(), Core.Source.CORE)); return tools; } diff --git a/Core/src/main/java/me/neurodock/core/files/tools/ReadFileTool.java b/Core/src/main/java/me/neurodock/core/files/tools/ReadFileTool.java index 9530a05..4cdbdd0 100644 --- a/Core/src/main/java/me/neurodock/core/files/tools/ReadFileTool.java +++ b/Core/src/main/java/me/neurodock/core/files/tools/ReadFileTool.java @@ -26,7 +26,7 @@ public class ReadFileTool extends OllamaFunctionTool { @Override public @NotNull OllamaPerameter parameters() { return OllamaPerameter.builder() - .addProperty("file_path", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The path to the file to be read") + .addProperty("file_path", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "The path to the file to be read", true) .build(); } diff --git a/Core/src/main/java/me/neurodock/core/memory/CoreMemory.java b/Core/src/main/java/me/neurodock/core/memory/CoreMemory.java index e56eec3..ed7a8b5 100644 --- a/Core/src/main/java/me/neurodock/core/memory/CoreMemory.java +++ b/Core/src/main/java/me/neurodock/core/memory/CoreMemory.java @@ -70,6 +70,10 @@ public class CoreMemory { e.printStackTrace(); } } + else { + memory.put(MAPPED_MEMORY, new JSONObject()); + memory.put(ARRAYED_MEMORY, new JSONArray()); + } this.memoryFile = memoryFile; diff --git a/Core/src/main/java/me/neurodock/ollama/OllamaFunctionTools.java b/Core/src/main/java/me/neurodock/ollama/OllamaFunctionTools.java index 83140b9..a68cc8b 100644 --- a/Core/src/main/java/me/neurodock/ollama/OllamaFunctionTools.java +++ b/Core/src/main/java/me/neurodock/ollama/OllamaFunctionTools.java @@ -53,7 +53,7 @@ public class OllamaFunctionTools implements Iterable messages, ArrayList> tools, JSONObject format, Map options, boolean stream, String keep_alive) { this.model = model; this.messages = messages; + + // Reflecting the reflection injection for ALL tools that was added in the OllamaObjectBuilder, etc, etc comment.. its past midnignt and i'm tired. + for(Pair tool : tools) + { + Class clazz = tool.getKey().getClass(); + Field field = null; + while(!clazz.equals(Object.class)) { + try { + field = clazz.getDeclaredField("source"); + if (field != null) { + break; + } + } + catch (NoSuchFieldException ignore){} + clazz = clazz.getSuperclass(); + } + if (field != null) { + field.setAccessible(true); + try { + field.set(tool.getKey(), tool.getValue()); + } catch (IllegalAccessException e) { + Core.writeLog("ERROR: "+e.getMessage()); + } + } + } + this.tools = tools; this.format = format; this.options = options; @@ -139,7 +165,7 @@ public class OllamaObject { */ public void addTool(OllamaTool tool, @MagicConstant(valuesFromClass = Core.Source.class) String source) { // We inject the source into the tool's source field if it exists, This is to not cause issues with duplicate tools - Class clazz = tool.getClass(); + Class clazz = tool.getClass(); Field field = null; while(!clazz.equals(Object.class)) { try { @@ -351,7 +377,7 @@ public class OllamaObject { * @return The {@link OllamaObjectBuilder} */ public OllamaObjectBuilder addTool(OllamaTool tool) { - this.tools.add(new Pair<>(tool, Core.Source.EXTERNAL)); + this.tools.add(new Pair<>(tool, Core.Source.CTP)); return this; } @@ -374,7 +400,7 @@ public class OllamaObject { */ public OllamaObjectBuilder addToolsExternal(ArrayList tools) { for (OllamaTool tool : tools) { - this.tools.add(new Pair<>(tool, Core.Source.EXTERNAL)); + this.tools.add(new Pair<>(tool, Core.Source.CTP)); } return this; } @@ -400,7 +426,7 @@ public class OllamaObject { */ public OllamaObjectBuilder addTools(OllamaTool... tools) { for(OllamaTool tool : tools) { - this.tools.add(new Pair<>(tool, Core.Source.EXTERNAL)); + this.tools.add(new Pair<>(tool, Core.Source.CTP)); } return this; } diff --git a/Display/src/main/java/me/neurodock/display/Display.java b/Display/src/main/java/me/neurodock/display/Display.java index 93819d9..a4e5780 100644 --- a/Display/src/main/java/me/neurodock/display/Display.java +++ b/Display/src/main/java/me/neurodock/display/Display.java @@ -56,12 +56,12 @@ public class Display { core.enablePlugins(Core.PLUGIN_DIRECTORY); - core.addTool(new TimeTool(), Core.Source.INTERNAL); + core.addTool(new TimeTool(), Core.Source.CTP); // TODO: Well Docker failes when luanched.... Fuck - core.addTool(new PythonRunner(core), Core.Source.INTERNAL); - core.addTools(new MALAPITool().getOllamaTools()); - core.addTools(new GeniusTools().getGeniusTools()); - core.addTools(new WikipediaTool().getWikipediaToolsInstance()); + core.addTool(new PythonRunner(core), Core.Source.CTP); + //core.addTools(new MALAPITool().getOllamaTools()); + //core.addTools(new GeniusTools().getGeniusTools()); + //core.addTools(new WikipediaTool().getWikipediaToolsInstance()); //APIApplication.start(); diff --git a/GeniusAPI/src/main/java/me/neurodock/genius/GeniusTools.java b/GeniusAPI/src/main/java/me/neurodock/genius/GeniusTools.java index 14ef89c..eca84dc 100644 --- a/GeniusAPI/src/main/java/me/neurodock/genius/GeniusTools.java +++ b/GeniusAPI/src/main/java/me/neurodock/genius/GeniusTools.java @@ -73,7 +73,7 @@ public class GeniusTools { if (OllamaFunctionTool.class.isAssignableFrom(clazz)) { //System.out.println("Found endpoint: " + clazz.getName() + " With constructor: " + clazz.getDeclaredConstructors().f.getName() + " and arguments: " + Arrays.toString(clazz.getDeclaredConstructor().getParameterTypes())); GeniusEndpointTool tool = (GeniusEndpointTool) clazz.getDeclaredConstructor(GeniusTools.class).newInstance(this); - builder.addTool(tool, Core.Source.INTERNAL); + builder.addTool(tool, Core.Source.CTP); } } } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { diff --git a/MALAPITool/src/main/java/me/neurodock/mal/api/MALAPITool.java b/MALAPITool/src/main/java/me/neurodock/mal/api/MALAPITool.java index 72743d3..0567e64 100644 --- a/MALAPITool/src/main/java/me/neurodock/mal/api/MALAPITool.java +++ b/MALAPITool/src/main/java/me/neurodock/mal/api/MALAPITool.java @@ -46,7 +46,7 @@ public class MALAPITool { if (OllamaFunctionTool.class.isAssignableFrom(clazz)) { //System.out.println("Found endpoint: " + clazz.getName() + " With constructor: " + clazz.getDeclaredConstructors().f.getName() + " and arguments: " + Arrays.toString(clazz.getDeclaredConstructor().getParameterTypes())); MALEndpointTool tool = (MALEndpointTool) clazz.getDeclaredConstructor(MALAPITool.class).newInstance(MALAPITool.this); - builder.addTool(tool, Core.Source.INTERNAL); + builder.addTool(tool, Core.Source.CTP); } } } catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { diff --git a/WikipediaTool/src/main/java/me/neurodock/wikipedia/WikipediaTool.java b/WikipediaTool/src/main/java/me/neurodock/wikipedia/WikipediaTool.java index e9b1285..f9a31e6 100644 --- a/WikipediaTool/src/main/java/me/neurodock/wikipedia/WikipediaTool.java +++ b/WikipediaTool/src/main/java/me/neurodock/wikipedia/WikipediaTool.java @@ -13,7 +13,7 @@ public class WikipediaTool { public WikipediaTool() { this.wikipediaToolsInstance = OllamaFunctionTools.builder() - .addTool(new GetWikiPageText(), Core.Source.INTERNAL) + .addTool(new GetWikiPageText(), Core.Source.CTP) .build(); }