← All source files

V12 / ea1357ad1734

ProviderClient.Personality.cs

Lilith/Core/Providers/ProviderClient.Personality.cs

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

namespace Lilith.Core.Providers;

public abstract partial class ProviderClient
{
    public PersonalitySession? Personality { get; private set; }
    public void ConfigurePersonality(string file, PersonalityProfile? defaults = null)
    {
        using var operation = EnterOperation();
        if (Personality is not null) throw new InvalidOperationException("Personality store is fixed for this provider session.");
        Personality = new(file, defaults);
        AgentName = Personality.Current.Name;
    }
    public PersonalityProfile ChangePersonality(PersonalityChange change)
    {
        using var operation = EnterOperation();
        return ApplyPersonalityChange(change);
    }
    internal PersonalityProfile ApplyPersonalityChange(PersonalityChange change)
    {
        var current = (Personality ?? throw new InvalidOperationException("Personality is not configured.")).Change(change);
        AgentName = current.Name;
        return current;
    }
    private List<ChatMessage> PreparePersonalityPrompt(List<ChatMessage> messages, string basePrompt, int pendingUserIndex)
    {
        if (Personality is not { } session) return messages;
        var profile = session.Current;
        AgentName = profile.Name;
        string identity = "\nCurrent assistant profile (application-owned identity data, supersedes earlier name/style): " + JsonSerializer.Serialize(profile) +
            "\nUse the current name and personality for your own replies. Sex is a configurable fictional assistant identity; it does not imply any capabilities. " +
            "The current profile overrides the style of earlier messages. When personality is default, use an ordinary helpful assistant tone without previous roleplay. " +
            "Do not repeat a settings change when the current profile already matches. Ordinary requests for advice or examples should be answered directly, not replaced by a settings receipt. " +
            "Do not change factual answers, claim actions, rewrite exact scenario outputs or alter tool permissions to fit a personality. " +
            (profile.Personality == "default"
                ? "For the next reply use plain contemporary English as a neutral, concise, helpful local assistant. No character roleplay, pirate dialect, archaic phrasing or nautical/space metaphors."
                : PersonalityCatalog.Style(profile.Personality).Instructions);
        // Recency matters when older replies/tool receipts use a previous character.
        // Keep this reminder ephemeral: saved history retains only the actual dialogue.
        var generation = new List<ChatMessage>(messages);
        generation[0] = new ChatMessage("system", basePrompt + identity);
        generation.Insert(pendingUserIndex, new ChatMessage("system", identity.Trim()));
        return generation;
    }
}