|
| 1 | +@using PolyPilot.Models |
| 2 | +@using PolyPilot.Services |
| 3 | +@using PolyPilot.Services.AI |
| 4 | +@inject DirectLocalChatService LocalChat |
| 5 | +@inject IJSRuntime JS |
| 6 | + |
| 7 | +@if (IsVisible) |
| 8 | +{ |
| 9 | + <div class="appchat-popover @(isExpanded ? "expanded" : "collapsed")"> |
| 10 | + <div class="appchat-header" @onclick="ToggleExpand"> |
| 11 | + <span class="appchat-title">✨ App Chat</span> |
| 12 | + <div class="appchat-header-actions"> |
| 13 | + @if (messages.Count > 0) |
| 14 | + { |
| 15 | + <button class="appchat-clear-btn" title="Clear chat" @onclick="ClearChat" @onclick:stopPropagation="true">🗑</button> |
| 16 | + } |
| 17 | + <button class="appchat-close-btn" title="Close" @onclick="Close" @onclick:stopPropagation="true">✕</button> |
| 18 | + </div> |
| 19 | + </div> |
| 20 | + |
| 21 | + @if (isExpanded) |
| 22 | + { |
| 23 | + <div class="appchat-body"> |
| 24 | + <ChatMessageList Messages="@messages" |
| 25 | + StreamingContent="@streamingContent" |
| 26 | + IsProcessing="@isProcessing" |
| 27 | + Compact="true" |
| 28 | + Layout="ChatLayout.BothLeft" |
| 29 | + Style="ChatStyle.Minimal" /> |
| 30 | + </div> |
| 31 | + |
| 32 | + <div class="appchat-input-area"> |
| 33 | + <input id="appchat-input" |
| 34 | + type="text" |
| 35 | + placeholder="Ask about your sessions..." |
| 36 | + disabled="@isProcessing" |
| 37 | + @onkeydown="HandleKeyDown" /> |
| 38 | + <button class="appchat-send-btn" |
| 39 | + disabled="@isProcessing" |
| 40 | + @onclick="SendMessage"> |
| 41 | + @(isProcessing ? "⏳" : "➤") |
| 42 | + </button> |
| 43 | + </div> |
| 44 | + } |
| 45 | + </div> |
| 46 | +} |
| 47 | + |
| 48 | +@code { |
| 49 | + [Parameter] public bool IsVisible { get; set; } |
| 50 | + [Parameter] public EventCallback OnClose { get; set; } |
| 51 | + |
| 52 | + private List<ChatMessage> messages = new(); |
| 53 | + private string streamingContent = ""; |
| 54 | + private bool isProcessing; |
| 55 | + private bool isExpanded = true; |
| 56 | + private string sessionName = "appchat-" + Guid.NewGuid().ToString("N")[..8]; |
| 57 | + private CancellationTokenSource? _cts; |
| 58 | + |
| 59 | + private void ToggleExpand() => isExpanded = !isExpanded; |
| 60 | + |
| 61 | + private async Task Close() |
| 62 | + { |
| 63 | + _cts?.Cancel(); |
| 64 | + await OnClose.InvokeAsync(); |
| 65 | + } |
| 66 | + |
| 67 | + private void ClearChat() |
| 68 | + { |
| 69 | + _cts?.Cancel(); |
| 70 | + messages.Clear(); |
| 71 | + streamingContent = ""; |
| 72 | + isProcessing = false; |
| 73 | + LocalChat.RemoveSession(sessionName); |
| 74 | + sessionName = "appchat-" + Guid.NewGuid().ToString("N")[..8]; |
| 75 | + } |
| 76 | + |
| 77 | + private async Task HandleKeyDown(KeyboardEventArgs e) |
| 78 | + { |
| 79 | + if (e.Key == "Enter" && !isProcessing) |
| 80 | + await SendMessage(); |
| 81 | + } |
| 82 | + |
| 83 | + private async Task SendMessage() |
| 84 | + { |
| 85 | + var prompt = await JS.InvokeAsync<string>("eval", "document.getElementById('appchat-input')?.value || ''"); |
| 86 | + if (string.IsNullOrWhiteSpace(prompt)) return; |
| 87 | + |
| 88 | + await JS.InvokeVoidAsync("eval", "document.getElementById('appchat-input').value = ''"); |
| 89 | + |
| 90 | + messages.Add(ChatMessage.UserMessage(prompt)); |
| 91 | + isProcessing = true; |
| 92 | + streamingContent = ""; |
| 93 | + StateHasChanged(); |
| 94 | + |
| 95 | + _cts?.Cancel(); |
| 96 | + _cts = new CancellationTokenSource(); |
| 97 | + |
| 98 | + await LocalChat.SendPromptStreamingAsync( |
| 99 | + sessionName, |
| 100 | + prompt, |
| 101 | + onDelta: delta => |
| 102 | + { |
| 103 | + streamingContent += delta; |
| 104 | + InvokeAsync(StateHasChanged); |
| 105 | + }, |
| 106 | + onComplete: full => |
| 107 | + { |
| 108 | + messages.Add(ChatMessage.AssistantMessage(full)); |
| 109 | + streamingContent = ""; |
| 110 | + isProcessing = false; |
| 111 | + InvokeAsync(StateHasChanged); |
| 112 | + }, |
| 113 | + onError: error => |
| 114 | + { |
| 115 | + messages.Add(ChatMessage.ErrorMessage(error)); |
| 116 | + streamingContent = ""; |
| 117 | + isProcessing = false; |
| 118 | + InvokeAsync(StateHasChanged); |
| 119 | + }, |
| 120 | + cancellationToken: _cts.Token); |
| 121 | + } |
| 122 | + |
| 123 | + public void Dispose() |
| 124 | + { |
| 125 | + _cts?.Cancel(); |
| 126 | + _cts?.Dispose(); |
| 127 | + LocalChat.RemoveSession(sessionName); |
| 128 | + } |
| 129 | +} |
0 commit comments