← All source files

V15 / 313404a015e8

CalendarOperations.cs

Lilith/Core/Calendar/CalendarOperations.cs

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

namespace Lilith.Core.Calendar;

internal static class CalendarOperations
{
    internal static readonly string[] Names = ["calendar_create_operation", "calendar_update_operation", "calendar_delete_operation", "calendar_read_operation", "calendar_search_operation", "calendar_agenda_operation", "calendar_offer_operation"];
    internal static bool Mutates(string name) => name is "calendar_create_operation" or "calendar_update_operation" or "calendar_delete_operation";
    internal static string Execute(ProviderClient owner, string name, IReadOnlyDictionary<string, string> inputs)
    {
        string Get(string key) => inputs.GetValueOrDefault(key) ?? "";
        if (name == "calendar_offer_operation")
        {
            string answer = Get("answer");
            return owner.Calendar is not null && Regex.IsMatch(Get("question"), @"\b(when|release|date|launch)\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)
                ? answer + "\n\nWould you like me to add this to your calendar? If so, what reminder date and time would you like, or would you prefer no reminder?" : answer;
        }
        var calendar = owner.Calendar ?? throw new InvalidOperationException("Calendar is not configured.");
        if (!Names.Contains(name)) throw new ArgumentException("Unknown calendar operation.");
        string operation = name[9..^10];
        return JsonSerializer.Serialize(calendar.Execute(new(operation, Get("id"), Get("title"), Get("date"), Get("time"), Get("timezone"), Get("reminder"), Get("notes"), Get("source"), Get("query"))));
    }
}