V11 / ca6f28eff240
ScheduleTool.cs
Lilith/Core/Scheduling/ScheduleTool.cs
Unchanged from the preceding versionCS
using System.Globalization;
using System.Text.Json;
using Lilith.Core.Tools;
namespace Lilith.Core.Scheduling;
internal sealed class ScheduleTool(ScheduleSession session, string operation, IReadOnlyDictionary<string, string> inputs,
Action<ScheduleResult> completed) : ITurnResultTool
{
public string Name => "schedule_" + operation + "_operation";
public string Description => "Perform the scenario's schedule operation once using its bound inputs. Call with {}. Never invent success.";
public TimeSpan ExecutionTimeout => TimeSpan.FromSeconds(30);
public JsonElement Parameters => JsonSerializer.SerializeToElement(new { type = "object", properties = new { }, additionalProperties = false });
public async Task<string> ExecuteAsync(JsonElement arguments, CancellationToken cancellationToken = default) =>
(await ExecuteResultAsync(arguments, cancellationToken)).Content;
public Task<ToolExecutionResult> ExecuteResultAsync(JsonElement arguments, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (arguments.ValueKind != JsonValueKind.Object || arguments.EnumerateObject().Any()) throw new ArgumentException("Expected an empty argument object.");
string Get(string name, string fallback = "") => inputs.GetValueOrDefault(name, fallback);
if (!int.TryParse(Get("seconds", "0"), NumberStyles.None, CultureInfo.InvariantCulture, out int seconds)) throw new ArgumentException("Invalid seconds.");
var result = session.Execute(new(operation, Get("name"), Get("kind", "timer"), seconds, Get("cron"), Get("timezone"), Get("message"), Get("scenario"), Get("inputs_json", "{}")));
completed(result);
string json = JsonSerializer.Serialize(result);
return Task.FromResult(new ToolExecutionResult(json, json));
}
internal static readonly string[] Operations = ["create", "update", "cancel", "delete", "list", "peek", "pending"];
internal static string? Operation(string name) => Operations.FirstOrDefault(op => name == "schedule_" + op + "_operation");
}