← All source files

V10 / 66cebf3528e0

Program.cs

Interfaces/ConsoleApp/LilithConsole/Program.cs

Unchanged from the preceding version
CS
using System;
using System.Text;
using System.Threading.Tasks;
using Lilith.Core.Logging;
using Lilith.Agent;
using LilithAgent = Lilith.Agent;
using LilithConsole.Setup;
using Lilith.Core.Kokoro;

namespace LilithConsole
{
    partial class Program
    {
        static async Task Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            var consoleSink = new TextLogSink(Console.Out, overwriteProgress: !Console.IsOutputRedirected,
                width: () => Math.Max(Console.WindowWidth - 1, 1));
            Action<LogEntry> onLog = consoleSink.Handle;

            var logger = new Logger(onLog);
            try
            {
                string settingsPath = Environment.GetEnvironmentVariable("LILITH_CONSOLE_SETTINGS")
                    ?? System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Lilith", "console-settings.json");
                var credentials = await ProviderSetup.RunAsync(Console.In, Console.Out, logger, settingsPath);
                if (credentials is null) { logger.SystemWriteLine("Setup stopped. Relaunch to continue."); return; }
                if (!Console.IsInputRedirected || Environment.GetEnvironmentVariable("LILITH_TTS") == "1")
                {
                    try
                    {
                        credentials = credentials with { Tts = await KokoroSetup.RunAsync(Console.In, Console.Out,
                            logger, settingsPath + ".kokoro.json") };
                    }
                    catch (Exception ex) { logger.SystemWriteLine($"Speech setup failed: {ex.Message}. Relaunch to retry; continuing with text chat.", LogColors.Orange); }
                }
                Console.Title = $"{credentials.AgentName} — {credentials.Model} Chat";
                await using var lilith = new LilithAgent.Lilith(onLog, credentials);
                string mode = "ask";
                if (!Console.IsInputRedirected)
                {
                    Console.Write("Workspace and scheduling mode for this boot [ask/yolo, default ask]: ");
                    mode = Console.ReadLine()?.Trim().ToLowerInvariant() ?? "ask";
                    if (mode.Length == 0) mode = "ask";
                    if (mode is not ("ask" or "yolo")) throw new ArgumentException("Choose ask or yolo at boot.");
                }
                else if (args.Contains("--workspace-yolo")) mode = "yolo";
                var workspace = Lilith.Core.Workspace.WorkspaceOptions.Default;
                string? workspaceRoot = Environment.GetEnvironmentVariable("LILITH_WORKSPACE_ROOT");
                lilith.WorkspaceOptions = workspace with { RootDirectory = workspaceRoot ?? workspace.RootDirectory,
                    Mode = mode == "yolo" ? Lilith.Core.Workspace.WorkspaceAccessMode.Yolo : Lilith.Core.Workspace.WorkspaceAccessMode.Ask };
                Console.WriteLine($"Workspace: {lilith.WorkspaceOptions.RootDirectory} ({mode}, fixed until restart). Use /workspace, /workspace review <id>, /approve <id>, /reject <id>.");
                await lilith.BootAsync(startServerIfNotRunning: true);

                while (true)
                {
                    Console.Title = $"{lilith.Personality.Name} — {credentials.Model} Chat";
                    string? input;
                    using (consoleSink.BeginInput())
                    {
                        lilith.Logger.Write("Input", "You: ", LogColors.Green);
                        input = Console.ReadLine()?.Trim();
                    }
                    if (input is null) break;
                    if (string.IsNullOrEmpty(input)) continue;
                    if (input.Equals("exit", StringComparison.OrdinalIgnoreCase)) break;
                    try
                    {
                        var command = await lilith.ChatCommands.ExecuteAsync(input);
                        if (command.Output is { Length: > 0 }) Console.WriteLine(command.Output);
                        if (command.ExitRequested) break;
                        if (!command.Handled) await lilith.ChatAsync(input);
                    }
                    catch (Exception ex)
                    {
                        lilith.Logger.SystemWriteLine($"Request failed: {ex.Message}. Please try again.", LogColors.Red);
                    }
                }
                await lilith.StopSchedulingAsync();
                lilith.SaveConversation();
                lilith.Logger.SystemWriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                logger.SystemWriteLine($"Setup/chat failed: {ex.Message}. Relaunch to retry; completed setup steps are retained.", LogColors.Red);
                Environment.ExitCode = 1;
            }
            finally { consoleSink.Flush(); }
        }
    }
}