| 1 | #nullable enable |
| 2 | |
| 3 | using CascadeIDE.Features.Intercom.Transport; |
| 4 | using CascadeIDE.Models.AgentChat; |
| 5 | |
| 6 | namespace CascadeIDE.Features.Chat; |
| 7 | |
| 8 | public partial class ChatPanelViewModel |
| 9 | { |
| 10 | private IntercomTransportCoordinator? _intercomTransport; |
| 11 | |
| 12 | public void SetIntercomTransportCoordinator(IntercomTransportCoordinator coordinator) |
| 13 | { |
| 14 | _intercomTransport = coordinator; |
| 15 | coordinator.Configure( |
| 16 | () => _getCascadeSettings?.Invoke() ?? new Models.CascadeIdeSettings(), |
| 17 | ResolveAttachWorkspaceRoot, |
| 18 | () => _sessionId, |
| 19 | () => _sessionStore, |
| 20 | ReloadIntercomSessionFromDiskAsync); |
| 21 | } |
| 22 | |
| 23 | private Func<Models.CascadeIdeSettings>? _getCascadeSettings; |
| 24 | |
| 25 | public void SetCascadeSettingsAccessor(Func<Models.CascadeIdeSettings> getSettings) => |
| 26 | _getCascadeSettings = getSettings; |
| 27 | |
| 28 | public void SetIntercomAdminRunner( |
| 29 | Func<string, string?, CancellationToken, Task<ChatSlashIntercomResult>> runAdmin) => |
| 30 | _slashCommandRunner.SetIntercomAdminRunner(runAdmin); |
| 31 | |
| 32 | public async Task StartIntercomTransportAsync(CancellationToken ct = default) |
| 33 | { |
| 34 | if (_intercomTransport is null) |
| 35 | return; |
| 36 | await _intercomTransport.StartAsync(ct).ConfigureAwait(false); |
| 37 | await UpdateTransportStatusOnUiAsync().ConfigureAwait(false); |
| 38 | } |
| 39 | |
| 40 | public async Task<(bool Ok, string Message)> ConnectIntercomTransportAsync(CancellationToken ct = default) |
| 41 | { |
| 42 | if (_intercomTransport is null) |
| 43 | return (false, "Transport не инициализирован."); |
| 44 | var result = await _intercomTransport.ConnectAsync(ct).ConfigureAwait(false); |
| 45 | await UpdateTransportStatusOnUiAsync().ConfigureAwait(false); |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | public async Task DisconnectIntercomTransportAsync(CancellationToken ct = default) |
| 50 | { |
| 51 | if (_intercomTransport is null) |
| 52 | return; |
| 53 | await _intercomTransport.DisconnectAsync(ct).ConfigureAwait(false); |
| 54 | await UpdateTransportStatusOnUiAsync().ConfigureAwait(false); |
| 55 | } |
| 56 | |
| 57 | private void NotifyIntercomTransportAfterPersist(ChatHistoryEvent ev) |
| 58 | { |
| 59 | _intercomTransport?.PublishLocalEventFireAndForget(ev); |
| 60 | _ = UpdateTransportStatusOnUiAsync(); |
| 61 | } |
| 62 | |
| 63 | private async Task TryAutoConnectIntercomTransportAsync() |
| 64 | { |
| 65 | if (_intercomTransport is null) |
| 66 | return; |
| 67 | await _intercomTransport.TryAutoConnectOnSendAsync().ConfigureAwait(false); |
| 68 | await UpdateTransportStatusOnUiAsync().ConfigureAwait(false); |
| 69 | } |
| 70 | |
| 71 | private Task UpdateTransportStatusOnUiAsync() |
| 72 | { |
| 73 | if (_intercomTransport is null) |
| 74 | return Task.CompletedTask; |
| 75 | |
| 76 | var delivery = _intercomTransport.DeliveryStatus; |
| 77 | var connection = _intercomTransport.ConnectionStatus; |
| 78 | var text = string.Join(" · ", new[] { connection, delivery }.Where(s => !string.IsNullOrWhiteSpace(s))); |
| 79 | if (string.IsNullOrWhiteSpace(text)) |
| 80 | return Task.CompletedTask; |
| 81 | |
| 82 | return UiScheduler.Default.InvokeAsync(() => |
| 83 | { |
| 84 | if (!string.IsNullOrWhiteSpace(connection)) |
| 85 | ClarificationStatusText = text; |
| 86 | }); |
| 87 | } |
| 88 | } |
| 89 | |