From 8c1ba3a36b9a871b2ae7c69cb590784515413ac9 Mon Sep 17 00:00:00 2001 From: krut_ni Date: Mon, 2 Mar 2026 23:29:02 +0100 Subject: [PATCH] added helper functions like simple structured logging and date time utilities --- .../Plugins/assistants/README.md | 34 +++++++- .../Assistants/PluginAssistants.cs | 81 ++++++++++++++++++- 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/app/MindWork AI Studio/Plugins/assistants/README.md b/app/MindWork AI Studio/Plugins/assistants/README.md index 27ef2120..d605d889 100644 --- a/app/MindWork AI Studio/Plugins/assistants/README.md +++ b/app/MindWork AI Studio/Plugins/assistants/README.md @@ -70,7 +70,6 @@ input = { ... }, profile = { - Id = "", Name = "", NeedToKnow = "", Actions = "", @@ -129,6 +128,39 @@ ASSISTANT.BuildPrompt = function(input) end ``` +### Logging helpers (assistant plugins only) +The assistant runtime exposes basic logging helpers to Lua. Use them to debug custom prompt building. + +- `LogDebug(message)` +- `LogInfo(message)` +- `LogWarn(message)` +- `LogError(message)` + +Example: +```lua +ASSISTANT.BuildPrompt = function(input) + LogInfo("BuildPrompt called") + return input.fields.Text or "" +end +``` + +### Date/time helpers (assistant plugins only) +Use these when you need timestamps inside Lua. + +- `DateTime(format)` returns a table with date/time parts plus a formatted string. + - `format` is optional; default is `yyyy-MM-dd HH:mm:ss` (ISO 8601-like). + - `formatted` contains the date in your desired format (e.g. `dd.MM.yyyy HH:mm`) or the default. + - Members: `year`, `month`, `day`, `hour`, `minute`, `second`, `millisecond`, `formatted`. +- `Timestamp()` returns a UTC timestamp in ISO-8601 format (`O` / round-trip), e.g. `2026-03-02T21:15:30.1234567Z`. + +Example: +```lua +local dt = DateTime("yyyy-MM-dd HH:mm:ss") +LogInfo(dt.formatted) +LogInfo(Timestamp()) +LogInfo(dt.day .. "." .. dt.month .. "." .. dt.year) +``` + #### Example: simple custom prompt ```lua ASSISTANT.BuildPrompt = function(input) diff --git a/app/MindWork AI Studio/Tools/PluginSystem/Assistants/PluginAssistants.cs b/app/MindWork AI Studio/Tools/PluginSystem/Assistants/PluginAssistants.cs index acd89094..5dca9a09 100644 --- a/app/MindWork AI Studio/Tools/PluginSystem/Assistants/PluginAssistants.cs +++ b/app/MindWork AI Studio/Tools/PluginSystem/Assistants/PluginAssistants.cs @@ -1,6 +1,7 @@ -using System.Xml.XPath; +using System.Xml.XPath; using AIStudio.Tools.PluginSystem.Assistants.DataModel; using Lua; +using System.Globalization; namespace AIStudio.Tools.PluginSystem.Assistants; @@ -30,9 +31,9 @@ public sealed class PluginAssistants(bool isInternal, LuaState state, PluginType /// /// Tries to parse the assistant table into our internal assistant render tree data model. It follows this process: /// - /// ASSISTANT → Title/Description → UI - /// UI: Root element → required Children → Components - /// Components: Type → Props → Children (recursively) + /// ASSISTANT ? Title/Description ? UI + /// UI: Root element ? required Children ? Components + /// Components: Type ? Props ? Children (recursively) /// /// /// The error message, when parameters from the table could not be read. @@ -42,6 +43,8 @@ public sealed class PluginAssistants(bool isInternal, LuaState state, PluginType message = string.Empty; this.HasEmbeddedProfileSelection = false; this.buildPromptFunction = null; + + this.RegisterLuaHelpers(); // Ensure that the main ASSISTANT table exists and is a valid Lua table: if (!this.state.Environment["ASSISTANT"].TryRead(out var assistantTable)) @@ -407,4 +410,74 @@ public sealed class PluginAssistants(bool isInternal, LuaState state, PluginType return true; } + + private void RegisterLuaHelpers() + { + + this.state.Environment["LogInfo"] = new LuaFunction((context, buffer, ct) => + { + if (context.ArgumentCount == 0) return new(0); + + var message = context.GetArgument(0); + LOGGER.LogInformation($"[Lua] [Assistants] [{this.Name}]: {message}"); + return new (1); + }); + + this.state.Environment["LogDebug"] = new LuaFunction((context, buffer, ct) => + { + if (context.ArgumentCount == 0) return new(0); + + var message = context.GetArgument(0); + LOGGER.LogDebug($"[Lua] [Assistants] [{this.Name}]: {message}"); + return new (1); + }); + + this.state.Environment["LogWarning"] = new LuaFunction((context, buffer, ct) => + { + if (context.ArgumentCount == 0) return new(0); + + var message = context.GetArgument(0); + LOGGER.LogWarning($"[Lua] [Assistants] [{this.Name}]: {message}"); + return new (1); + }); + + this.state.Environment["LogError"] = new LuaFunction((context, buffer, ct) => + { + if (context.ArgumentCount == 0) return new(0); + + var message = context.GetArgument(0); + LOGGER.LogError($"[Lua] [Assistants] [{this.Name}]: {message}"); + return new (1); + }); + + this.state.Environment["DateTime"] = new LuaFunction((context, buffer, ct) => + { + var format = context.ArgumentCount > 0 ? context.GetArgument(0) : "yyyy-MM-dd HH:mm:ss"; + var now = DateTime.Now; + var formattedDate = now.ToString(format); + + var table = new LuaTable + { + ["year"] = now.Year, + ["month"] = now.Month, + ["day"] = now.Day, + ["hour"] = now.Hour, + ["minute"] = now.Minute, + ["second"] = now.Second, + ["millisecond"] = now.Millisecond, + ["formatted"] = formattedDate, + }; + buffer.Span[0] = table; + + return new(1); + }); + + this.state.Environment["Timestamp"] = new LuaFunction((context, buffer, ct) => + { + var timestamp = DateTime.UtcNow.ToString("o"); + buffer.Span[0] = timestamp; + + return new(1); + }); + } }