using System.Collections.ObjectModel; namespace MeetingAssistant.Workflow; public sealed class WorkflowRulesEditorChatViewModel { private readonly IWorkflowRulesEditorChatPipeline pipeline; public WorkflowRulesEditorChatViewModel(IWorkflowRulesEditorChatPipeline pipeline) { this.pipeline = pipeline; } public ObservableCollection Messages { get; } = []; public string Draft { get; set; } = ""; public bool IsThinking { get; private set; } public event EventHandler? Changed; public async Task SendAsync(CancellationToken cancellationToken = default) { var prompt = Draft.Trim(); if (string.IsNullOrWhiteSpace(prompt) || IsThinking) { return; } Draft = ""; var priorConversation = Messages.ToList(); Messages.Add(new WorkflowRulesEditorChatMessage(WorkflowRulesEditorChatRole.User, prompt)); IsThinking = true; OnChanged(); try { var result = await pipeline.SendAsync( priorConversation, prompt, cancellationToken); Messages.Clear(); foreach (var message in result.Conversation) { Messages.Add(message); } } catch (Exception exception) when (exception is not OperationCanceledException) { Messages.Add(new WorkflowRulesEditorChatMessage( WorkflowRulesEditorChatRole.Agent, $"Rules editor failed: {exception.Message}")); } finally { IsThinking = false; OnChanged(); } } private void OnChanged() { Changed?.Invoke(this, EventArgs.Empty); } }