Services/LilithChatService.cscsharp

Documentation

Lilith.RazorWebGui (Version 2)

ASP.NET Core Razor Pages web UI for Lilith Version 2. Mirrors LilithConsole chat, commands, and logging.

  • Model: LILITH_MODEL (default gemma4)
  • Commands: exit, /new, /history, /history <n>, /voice <name> (when TTS is enabled)
dotnet run --project Lilith.RazorWebGui.csproj

Browse http://localhost:5000 (see Properties/launchSettings.json).

using Agent.Core.Logging;using Agent.Core.TextToSpeech;using LilithAgent = Lilith.Agent;namespace Lilith.RazorWebGui.Services;public sealed class LilithChatService{    private readonly object _lock = new();    private readonly List<ChatLine> _lines = [];    private LilithAgent.Lilith? _lilith;    private KokoroTts? _tts;    public string Model { get; private set; } = "gemma4";    public int VersionNumber { get; } = 2;    public bool IsReady { get; private set; }    public bool TtsEnabled => _tts is not null;    public IReadOnlyList<ChatLine> Lines    {        get { lock (_lock) { return _lines.ToList(); } }    }    public async Task InitializeAsync(CancellationToken cancellationToken = default)    {        if (IsReady)        {            return;        }        Model = Environment.GetEnvironmentVariable("LILITH_MODEL") ?? "gemma4";        var credentials = new LilithAgent.LilithCredentials(Model: Model);        var lilith = new LilithAgent.Lilith(AppendLog, credentials);        try        {            bool waitForTts = Environment.GetEnvironmentVariable("LILITH_WAIT_FOR_TTS") == "1";            _tts = await KokoroTts.CreateAsync(lilith.Logger, new KokoroTtsOptions { WaitForTts = waitForTts })                .ConfigureAwait(false);            lilith.UseTextToSpeech(_tts);        }        catch (Exception ex)        {            lilith.Logger.WriteLine("TTS", $"Speech disabled: {ex.Message}", LogColors.Orange);        }        await lilith.BootAsync(startServerIfNotRunning: true).ConfigureAwait(false);        _lilith = lilith;        IsReady = true;    }    public async Task<bool> SendAsync(string input, CancellationToken cancellationToken = default)    {        if (_lilith is null)        {            throw new InvalidOperationException("Lilith is not initialized.");        }        if (input.StartsWith("/voice ", StringComparison.OrdinalIgnoreCase))        {            string newVoice = input[7..].Trim();            if (_tts is not null)            {                try                {                    _tts.SetVoice(newVoice);                    AppendSystem($"Voice changed to: {newVoice}");                }                catch (Exception ex)                {                    AppendSystem($"Failed to change voice: {ex.Message}");                }            }            else            {                AppendSystem("TTS is not enabled.");            }            return true;        }        return await _lilith.HandleInputAsync(input).ConfigureAwait(false);    }    public void Shutdown() => _lilith?.SaveConversation();    private void AppendLog(LogEntry entry) => ChatLog.Append(_lines, _lock, entry);    private void AppendSystem(string message) =>        AppendLog(new LogEntry("System", message, LogColors.Cyan, DateTime.Now, LogType.WriteLine));}