added helper functions like simple structured logging and date time utilities

This commit is contained in:
krut_ni 2026-03-02 23:29:02 +01:00
parent cbd74f4f6f
commit 8c1ba3a36b
No known key found for this signature in database
GPG Key ID: A5C0151B4DDB172C
2 changed files with 110 additions and 5 deletions

View File

@ -70,7 +70,6 @@ input = {
...
},
profile = {
Id = "<string guid>",
Name = "<string>",
NeedToKnow = "<string>",
Actions = "<string>",
@ -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)

View File

@ -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
/// <summary>
/// Tries to parse the assistant table into our internal assistant render tree data model. It follows this process:
/// <list type="number">
/// <item><description>ASSISTANT → Title/Description → UI</description></item>
/// <item><description>UI: Root element → required Children → Components</description></item>
/// <item><description>Components: Type → Props → Children (recursively)</description></item>
/// <item><description>ASSISTANT ? Title/Description ? UI</description></item>
/// <item><description>UI: Root element ? required Children ? Components</description></item>
/// <item><description>Components: Type ? Props ? Children (recursively)</description></item>
/// </list>
/// </summary>
/// <param name="message">The error message, when parameters from the table could not be read.</param>
@ -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<LuaTable>(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<string>(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<string>(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<string>(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<string>(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<string>(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);
});
}
}