feat: Add commands and functionality to save and load chat sessions.
This commit is contained in:
42
Program.cs
42
Program.cs
@@ -81,13 +81,13 @@ infoTable.AddRow("[grey]CWD[/]", $"[green]{Markup.Escape(Environment.CurrentDire
|
||||
|
||||
if (modelInfo?.Pricing != null)
|
||||
|
||||
if (modelInfo?.Pricing != null)
|
||||
{
|
||||
var inM = tokenTracker.InputPrice * 1_000_000m;
|
||||
var outM = tokenTracker.OutputPrice * 1_000_000m;
|
||||
infoTable.AddRow("[grey]Pricing[/]",
|
||||
$"[yellow]${inM:F2}[/][dim]/M in[/] [yellow]${outM:F2}[/][dim]/M out[/]");
|
||||
}
|
||||
if (modelInfo?.Pricing != null)
|
||||
{
|
||||
var inM = tokenTracker.InputPrice * 1_000_000m;
|
||||
var outM = tokenTracker.OutputPrice * 1_000_000m;
|
||||
infoTable.AddRow("[grey]Pricing[/]",
|
||||
$"[yellow]${inM:F2}[/][dim]/M in[/] [yellow]${outM:F2}[/][dim]/M out[/]");
|
||||
}
|
||||
if (modelInfo != null)
|
||||
{
|
||||
infoTable.AddRow("[grey]Context[/]",
|
||||
@@ -110,7 +110,7 @@ var openAiClient = new OpenAIClient(new ApiKeyCredential(apiKey), new OpenAIClie
|
||||
IChatClient innerClient = openAiClient.GetChatClient(model).AsIChatClient();
|
||||
|
||||
// ── Tool call logging via Spectre ───────────────────────────────────────
|
||||
object consoleLock = new object();
|
||||
object consoleLock = new();
|
||||
|
||||
void ToolLog(string message)
|
||||
{
|
||||
@@ -142,12 +142,38 @@ commandRegistry.Register(new StatusCommand(model, endpoint));
|
||||
commandRegistry.Register(new CompactCommand(session.Compactor, session.History));
|
||||
commandRegistry.Register(new SetupCommand());
|
||||
commandRegistry.Register(new ResetCommand(session, tokenTracker));
|
||||
commandRegistry.Register(new SaveCommand(session));
|
||||
commandRegistry.Register(new LoadCommand(session));
|
||||
|
||||
|
||||
var commandDispatcher = new CommandDispatcher(commandRegistry);
|
||||
|
||||
// ── Run Repl ────────────────────────────────────────────────────────────
|
||||
|
||||
// Auto-load session if it exists
|
||||
const string sessionPath = ".anchor/session.json";
|
||||
if (File.Exists(sessionPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
await session.LoadAsync(sessionPath, default);
|
||||
AnsiConsole.MarkupLine($"[dim grey]Auto-loaded previous session.[/]");
|
||||
}
|
||||
catch { /* Ignore load errors on startup */ }
|
||||
}
|
||||
|
||||
var repl = new ReplLoop(session, tokenTracker, commandDispatcher);
|
||||
await repl.RunAsync();
|
||||
|
||||
// Auto-save session on clean exit
|
||||
try
|
||||
{
|
||||
var directory = Path.GetDirectoryName(sessionPath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
await session.SaveAsync(sessionPath, default);
|
||||
}
|
||||
catch { /* Ignore save errors on exit */ }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user