← All source files

V18 / 8d896b8a86e5

PersonalityOperations.cs

Lilith/Core/Personality/PersonalityOperations.cs

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

namespace Lilith.Core.Personality;

internal static class PersonalityOperations
{
    internal static readonly string[] Names = ["profile_read_operation", "voices_list_operation", "name_change_operation", "voice_change_operation", "sex_change_operation", "personality_change_operation"];
    internal static bool Mutates(string name) => name.EndsWith("_change_operation", StringComparison.Ordinal);
    internal static string Execute(ProviderClient owner, string name, IReadOnlyDictionary<string, string> inputs)
    {
        var session = owner.Personality ?? throw new InvalidOperationException("Personality is not configured.");
        string Get(string key) => inputs.TryGetValue(key, out var value) ? value : throw new ArgumentException("Missing profile input: " + key);
        object result = name switch
        {
            "profile_read_operation" => new { profile = session.Current, defaults = session.Default, personalities = PersonalityCatalog.Styles },
            "voices_list_operation" => new { profile = session.Current, default_voice = session.Default.Voice, voices = PersonalityCatalog.Voices(session.Default.Voice) },
            "name_change_operation" => owner.ApplyPersonalityChange(new(Name: Get("name"))),
            "voice_change_operation" => owner.ApplyPersonalityChange(new(Voice: Get("voice"))),
            "sex_change_operation" => owner.ApplyPersonalityChange(new(Sex: Get("sex"))),
            "personality_change_operation" => owner.ApplyPersonalityChange(new(Personality: Get("personality"))),
            _ => throw new ArgumentException("Unknown profile operation.")
        };
        return Mutates(name) ? "Assistant settings updated: " + JsonSerializer.Serialize(result) : JsonSerializer.Serialize(result);
    }
}