2026-10-15 11:18:46 +02:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using AgentWikiUi.Core;
|
|
|
|
|
|
|
|
|
|
namespace AgentWikiUi.Gui;
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
class MessageVm(string role, string text, string author)
|
2026-10-15 11:18:46 +02:00
|
|
|
{
|
|
|
|
|
public string Role { get; } = role;
|
|
|
|
|
public string Text { get; } = text;
|
2026-10-15 11:40:47 +02:00
|
|
|
public string Author { get; } = author;
|
2026-10-15 11:18:46 +02:00
|
|
|
public bool IsUser => Role == "user";
|
|
|
|
|
public bool IsAi => Role == "ai";
|
|
|
|
|
public bool IsErr => Role == "err";
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
class ChatItem(string id, string title, DateTime updated, bool active, string engineName)
|
2026-10-15 11:18:46 +02:00
|
|
|
{
|
|
|
|
|
public string Id { get; } = id;
|
|
|
|
|
public string Title { get; } = title;
|
2026-10-15 11:40:47 +02:00
|
|
|
public string When { get; } = $"{engineName} · {updated:d MMM HH:mm}";
|
2026-10-15 11:18:46 +02:00
|
|
|
public bool IsActive { get; } = active;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
/// <summary>Voce dell'interruttore CLI: un motore non installato è mostrato ma non selezionabile.</summary>
|
|
|
|
|
class EngineChoice(Engine engine)
|
|
|
|
|
{
|
|
|
|
|
public Engine Engine { get; } = engine;
|
|
|
|
|
public bool Installed { get; } = engine.IsInstalled();
|
|
|
|
|
public string Label => Installed ? Engine.Name : $"{Engine.Name} (non installato)";
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:18:46 +02:00
|
|
|
class MainViewModel : ObservableObject
|
|
|
|
|
{
|
|
|
|
|
static readonly ModelOption Default = new("Predefinito", "");
|
|
|
|
|
static readonly ModelOption Custom = new("Altro ID…", "__custom");
|
2026-10-15 11:40:47 +02:00
|
|
|
const string DefaultEffort = "Predefinito";
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
public ObservableCollection<MessageVm> Messages { get; } = [];
|
|
|
|
|
public ObservableCollection<ChatItem> Chats { get; } = [];
|
2026-10-15 11:40:47 +02:00
|
|
|
public EngineChoice[] EngineChoices { get; } = Engines.All.Select(e => new EngineChoice(e)).ToArray();
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
public ICommand NewChatCommand => newChat;
|
|
|
|
|
public ICommand CopyCommand { get; }
|
|
|
|
|
public ICommand SendCommand { get; }
|
|
|
|
|
public ICommand OpenCommand { get; }
|
|
|
|
|
public ICommand DeleteCommand { get; }
|
|
|
|
|
public ICommand ToggleSidebarCommand { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Conferma di eliminazione, fornita dalla finestra.</summary>
|
2026-10-15 11:40:47 +02:00
|
|
|
public Func<Task<bool>> ConfirmDelete { get; set; } = () => Task.FromResult(true);
|
2026-10-15 11:18:46 +02:00
|
|
|
/// <summary>Copia negli appunti, fornita dalla finestra.</summary>
|
|
|
|
|
public Func<string, Task> CopyText { get; set; } = _ => Task.CompletedTask;
|
|
|
|
|
/// <summary>Chiesto alla finestra di rimettere il fuoco sul campo messaggio.</summary>
|
|
|
|
|
public event Action? FocusInput;
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
// Una chat = un motore. chatId è il nome del file; sessionId è l'id del motore:
|
|
|
|
|
// per Claude Code lo sceglie l'app (= chatId), per Codex arriva dall'output dopo il primo messaggio.
|
|
|
|
|
string chatId = Guid.NewGuid().ToString();
|
|
|
|
|
string? sessionId;
|
|
|
|
|
bool started; // true dopo il primo messaggio riuscito: da lì in poi si riprende la sessione
|
2026-10-15 11:18:46 +02:00
|
|
|
List<Msg> history = [];
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
EngineChoice selectedEngine;
|
|
|
|
|
ModelOption[] modelChoices = [];
|
|
|
|
|
string[] effortChoices = [];
|
2026-10-15 11:18:46 +02:00
|
|
|
string input = "";
|
|
|
|
|
bool busy, sidebarOpen = true;
|
|
|
|
|
ModelOption selectedModel = Default;
|
|
|
|
|
string customModel = "";
|
2026-10-15 11:40:47 +02:00
|
|
|
string selectedEffort = DefaultEffort;
|
2026-10-15 11:18:46 +02:00
|
|
|
readonly RelayCommand<object> send, newChat;
|
|
|
|
|
|
|
|
|
|
public MainViewModel()
|
|
|
|
|
{
|
2026-10-15 11:40:47 +02:00
|
|
|
selectedEngine = EngineChoices.FirstOrDefault(c => c.Installed) ?? EngineChoices[0];
|
|
|
|
|
ApplyEngine(selectedEngine);
|
2026-10-15 11:18:46 +02:00
|
|
|
send = new(_ => _ = SendAsync(), () => CanSend);
|
|
|
|
|
SendCommand = send;
|
|
|
|
|
newChat = new(_ => NewChat(), () => !busy);
|
|
|
|
|
CopyCommand = new RelayCommand<string>(t => { if (t is not null) _ = CopyText(t); });
|
|
|
|
|
OpenCommand = new RelayCommand<ChatItem>(c => { if (c is not null) OpenChat(c.Id); });
|
|
|
|
|
DeleteCommand = new RelayCommand<ChatItem>(c => { if (c is not null) _ = DeleteAsync(c); });
|
|
|
|
|
ToggleSidebarCommand = new RelayCommand<object>(_ => SidebarOpen = !SidebarOpen);
|
|
|
|
|
Messages.CollectionChanged += (_, _) => { Raise(nameof(HasMessages)); Raise(nameof(ShowEmpty)); };
|
2026-10-15 11:40:47 +02:00
|
|
|
ResetSession();
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
// all'avvio riapre la chat più recente
|
|
|
|
|
if (ChatStore.List().FirstOrDefault() is { } last) OpenChat(last.Id); else RefreshChats();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
Engine Engine => selectedEngine.Engine;
|
|
|
|
|
|
2026-10-15 11:18:46 +02:00
|
|
|
public bool HasMessages => Messages.Count > 0;
|
|
|
|
|
public bool ShowEmpty => Messages.Count == 0;
|
|
|
|
|
public bool CanSend => !busy && !string.IsNullOrWhiteSpace(input);
|
2026-10-15 11:40:47 +02:00
|
|
|
public string Author => Engine.Author;
|
|
|
|
|
public string InputPlaceholder => $"Scrivi un messaggio a {Engine.Author}…";
|
|
|
|
|
public string BusyText => $"{Engine.Author} sta scrivendo…";
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
public string Input
|
|
|
|
|
{
|
|
|
|
|
get => input;
|
|
|
|
|
set { if (Set(ref input, value)) { Raise(nameof(CanSend)); send.Refresh(); } }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsBusy
|
|
|
|
|
{
|
|
|
|
|
get => busy;
|
2026-10-15 11:40:47 +02:00
|
|
|
private set { if (Set(ref busy, value)) { Raise(nameof(CanSend)); Raise(nameof(CanSwitchEngine)); send.Refresh(); newChat.Refresh(); } }
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SidebarOpen { get => sidebarOpen; set => Set(ref sidebarOpen, value); }
|
2026-10-15 11:40:47 +02:00
|
|
|
|
|
|
|
|
public bool CanSwitchEngine => !busy;
|
|
|
|
|
|
|
|
|
|
/// <summary>Interruttore CLI: cambiare motore apre una nuova chat (il contesto non passa da un motore all'altro).</summary>
|
|
|
|
|
public EngineChoice SelectedEngine
|
|
|
|
|
{
|
|
|
|
|
get => selectedEngine;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value is null || value == selectedEngine || busy || !value.Installed) return;
|
|
|
|
|
ApplyEngine(value);
|
|
|
|
|
NewChat();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ModelOption[] ModelChoices { get => modelChoices; private set => Set(ref modelChoices, value); }
|
|
|
|
|
public string[] EffortChoices { get => effortChoices; private set => Set(ref effortChoices, value); }
|
|
|
|
|
|
2026-10-15 11:18:46 +02:00
|
|
|
public ModelOption SelectedModel
|
|
|
|
|
{
|
|
|
|
|
get => selectedModel;
|
|
|
|
|
set { if (Set(ref selectedModel, value)) Raise(nameof(IsCustomModel)); }
|
|
|
|
|
}
|
|
|
|
|
public bool IsCustomModel => selectedModel == Custom;
|
|
|
|
|
public string CustomModel { get => customModel; set => Set(ref customModel, value); }
|
|
|
|
|
public string SelectedEffort { get => selectedEffort; set => Set(ref selectedEffort, value); }
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
// Adotta un motore: liste di modelli ed effort sue, scelte riportate a "Predefinito".
|
|
|
|
|
void ApplyEngine(EngineChoice choice)
|
|
|
|
|
{
|
|
|
|
|
selectedEngine = choice;
|
|
|
|
|
ModelChoices = [Default, .. choice.Engine.Models, Custom];
|
|
|
|
|
EffortChoices = [DefaultEffort, .. choice.Engine.Efforts];
|
|
|
|
|
SelectedModel = Default; CustomModel = ""; SelectedEffort = DefaultEffort;
|
|
|
|
|
Raise(nameof(SelectedEngine)); Raise(nameof(Author)); Raise(nameof(InputPlaceholder)); Raise(nameof(BusyText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ResetSession()
|
|
|
|
|
{
|
|
|
|
|
chatId = Guid.NewGuid().ToString();
|
|
|
|
|
sessionId = Engine.OwnsSessionId ? null : chatId;
|
|
|
|
|
started = false;
|
|
|
|
|
history = [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:18:46 +02:00
|
|
|
void RefreshChats()
|
|
|
|
|
{
|
|
|
|
|
Chats.Clear();
|
2026-10-15 11:40:47 +02:00
|
|
|
foreach (var c in ChatStore.List())
|
|
|
|
|
Chats.Add(new(c.Id, c.Title, c.Updated, c.Id == chatId, Engines.Find(c.EngineId)?.Name ?? c.EngineId));
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
void Show(List<Msg> msgs, string author)
|
2026-10-15 11:18:46 +02:00
|
|
|
{
|
|
|
|
|
Messages.Clear();
|
2026-10-15 11:40:47 +02:00
|
|
|
foreach (var m in msgs) Messages.Add(new(m.Role, m.Text, author));
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NewChat()
|
|
|
|
|
{
|
2026-10-15 11:40:47 +02:00
|
|
|
ResetSession();
|
|
|
|
|
Show(history, Author); RefreshChats(); FocusInput?.Invoke();
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenChat(string id)
|
|
|
|
|
{
|
|
|
|
|
if (busy) return;
|
|
|
|
|
if (ChatStore.Load(id) is not { } chat) { RefreshChats(); return; }
|
2026-10-15 11:40:47 +02:00
|
|
|
var choice = EngineChoices.FirstOrDefault(c => c.Engine.Id == chat.EngineId);
|
|
|
|
|
if (choice is not null && choice != selectedEngine) ApplyEngine(choice);
|
|
|
|
|
chatId = chat.Id; sessionId = chat.EffectiveSessionId; started = chat.Started; history = chat.Messages;
|
|
|
|
|
Show(history, Author); RefreshChats();
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task DeleteAsync(ChatItem c)
|
|
|
|
|
{
|
|
|
|
|
if (busy || !await ConfirmDelete()) return;
|
|
|
|
|
ChatStore.Delete(c.Id);
|
2026-10-15 11:40:47 +02:00
|
|
|
if (c.Id == chatId) NewChat(); else RefreshChats();
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task SendAsync()
|
|
|
|
|
{
|
|
|
|
|
if (!CanSend) return;
|
|
|
|
|
var text = input.Trim();
|
|
|
|
|
var model = IsCustomModel ? customModel.Trim() : selectedModel.Id;
|
2026-10-15 11:40:47 +02:00
|
|
|
var effort = selectedEffort == DefaultEffort ? "" : selectedEffort;
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
Input = ""; IsBusy = true;
|
|
|
|
|
Add("user", text);
|
|
|
|
|
Persist(); // il messaggio non va perso se l'app si chiude durante l'attesa
|
|
|
|
|
RefreshChats();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-15 11:40:47 +02:00
|
|
|
var r = await EngineRunner.RunCapturedAsync(Engine, text, sessionId, started, model, effort);
|
|
|
|
|
if (r.Error is null)
|
|
|
|
|
{
|
|
|
|
|
started = true;
|
|
|
|
|
if (r.SessionId is not null) sessionId = r.SessionId;
|
|
|
|
|
Add("ai", r.Text);
|
|
|
|
|
}
|
|
|
|
|
else Add("err", r.Error);
|
2026-10-15 11:18:46 +02:00
|
|
|
}
|
2026-10-15 11:40:47 +02:00
|
|
|
catch (System.ComponentModel.Win32Exception) { Add("err", $"'{Engine.Executable}' non trovato nel PATH."); }
|
2026-10-15 11:18:46 +02:00
|
|
|
catch (Exception ex) { Add("err", ex.Message); }
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
IsBusy = false;
|
|
|
|
|
Persist();
|
|
|
|
|
RefreshChats();
|
|
|
|
|
FocusInput?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 11:40:47 +02:00
|
|
|
void Add(string role, string text) { history.Add(new(role, text)); Messages.Add(new(role, text, Author)); }
|
2026-10-15 11:18:46 +02:00
|
|
|
|
|
|
|
|
void Persist()
|
|
|
|
|
{
|
2026-10-15 11:40:47 +02:00
|
|
|
try { ChatStore.Save(chatId, Engine.Id, sessionId, started, history); }
|
2026-10-15 11:18:46 +02:00
|
|
|
catch (IOException) { } // la chat resta a schermo anche se il salvataggio fallisce
|
|
|
|
|
}
|
|
|
|
|
}
|