V18 / 8d896b8a86e5
AddonToolDraft.cs
Lilith/Core/Addons/AddonToolDraft.cs
Changed in this versionCS
using System.Text.Json;
namespace Lilith.Core.Addons;
internal sealed record AddonToolDraft(string Description,JsonElement Parameters,string Body)
{
internal static JsonElement Schema=>JsonSerializer.SerializeToElement(new{type="object",additionalProperties=false,required=new[]{"description","parameters","body"},properties=new{description=new{type="string"},parameters=new{type="object"},body=new{type="string"}}});
internal static string Render(string json,string name)
{
var p=JsonSerializer.Deserialize<AddonToolDraft>(json,new JsonSerializerOptions{PropertyNameCaseInsensitive=true})??throw new InvalidDataException("Missing C# tool draft.");
if(p.Body.Length is <10 or >60000||p.Description.Length>1000||p.Parameters.ValueKind!=JsonValueKind.Object||!p.Parameters.TryGetProperty("type",out var type)||type.GetString()!="object")throw new InvalidDataException("Invalid tool body or parameter schema.");
return "using System;\nusing System.IO;\nusing System.Text.Json;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Lilith.Core.Tools;\nusing Lilith.Core.Addons;\n"+
"public sealed class CraftedTool : ILilithTool, IAddonContextAware {\nprivate AddonContext? _context;\npublic void Configure(AddonContext context) { _context=context; }\n"+
"public string Name => "+JsonSerializer.Serialize(name)+";\npublic string Description => "+JsonSerializer.Serialize(p.Description)+";\npublic JsonElement Parameters => JsonDocument.Parse("+JsonSerializer.Serialize(p.Parameters.GetRawText())+").RootElement.Clone();\n"+
"public async Task<string> ExecuteAsync(JsonElement arguments, CancellationToken cancellationToken=default) {\nvar context=_context??throw new InvalidOperationException(\"Addon context unavailable.\");\ncancellationToken.ThrowIfCancellationRequested();\n"+p.Body+"\n}\n}\n";
}
}