← All source files

V18 / 8d896b8a86e5

ToolTemplate.cs

Lilith/Core/Tools/ToolTemplate.cs

Unchanged from the preceding version
CS
using System.Text.Json;

namespace Lilith.Core.Tools;

/// <summary>Copy and rename this class to create a tool. Not registered by default.</summary>
public sealed class ToolTemplate : ILilithTool
{
    public string Name => "example_echo";
    public string Description => "Return the supplied text. Replace this description with your tool's purpose.";
    public JsonElement Parameters => JsonSerializer.SerializeToElement(new
    {
        type = "object",
        properties = new { text = new { type = "string", description = "Text to return." } },
        required = new[] { "text" }, additionalProperties = false
    });

    public Task<string> ExecuteAsync(JsonElement arguments, CancellationToken cancellationToken = default)
    {
        cancellationToken.ThrowIfCancellationRequested();
        if (arguments.ValueKind != JsonValueKind.Object ||
            arguments.EnumerateObject().Count() != 1 ||
            !arguments.TryGetProperty("text", out var text) || text.ValueKind != JsonValueKind.String)
            throw new ArgumentException("Expected exactly one string argument named 'text'.");
        // Replace this with your implementation; await asynchronous work and honor cancellation.
        return Task.FromResult(JsonSerializer.Serialize(new { text = text.GetString() }));
    }
}