AI-Studio/app/MindWork AI Studio/Tools/Services/RustService.Events.cs
Thorsten Sommer 009bb33d83
Some checks are pending
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Added a preview of the document analysis assistant (#561)
Co-authored-by: Peer Schütt <20603780+peerschuett@users.noreply.github.com>
2025-11-24 12:37:18 +01:00

77 lines
3.1 KiB
C#

using System.Text.Json;
using AIStudio.Tools.Rust;
namespace AIStudio.Tools.Services;
public partial class RustService
{
/// <summary>
/// Consume the Tauri event stream and forward relevant events to the message bus.
/// </summary>
/// <param name="stopToken">Cancellation token to stop the stream.</param>
private async Task StartStreamTauriEvents(CancellationToken stopToken)
{
// Outer try-catch to handle cancellation:
try
{
while (!stopToken.IsCancellationRequested)
{
// Inner try-catch to handle streaming issues:
try
{
// Open the event stream:
await using var stream = await this.http.GetStreamAsync("/events", stopToken);
// Read events line by line:
using var reader = new StreamReader(stream);
// Read until the end of the stream or cancellation:
while(!reader.EndOfStream && !stopToken.IsCancellationRequested)
{
// Read the next line of JSON from the stream:
var line = await reader.ReadLineAsync(stopToken);
// Skip empty lines:
if (string.IsNullOrWhiteSpace(line))
continue;
// Deserialize the Tauri event:
var tauriEvent = JsonSerializer.Deserialize<TauriEvent>(line, this.jsonRustSerializerOptions);
// Forward relevant events to the message bus:
if (tauriEvent != default && tauriEvent.EventType
is not TauriEventType.NONE
and not TauriEventType.UNKNOWN
and not TauriEventType.PING)
{
this.logger!.LogDebug("Received Tauri event {EventType} with {NumPayloadItems} payload items.", tauriEvent.EventType, tauriEvent.Payload.Count);
await MessageBus.INSTANCE.SendMessage(null, Event.TAURI_EVENT_RECEIVED, tauriEvent);
}
}
}
// The cancellation token was triggered, exit the loop:
catch (OperationCanceledException)
{
break;
}
// Some other error occurred, log it and retry after a delay:
catch (Exception e)
{
this.logger!.LogError("Error while streaming Tauri events: {Message}", e.Message);
await Task.Delay(TimeSpan.FromSeconds(3), stopToken);
}
}
}
// The cancellation token was triggered, exit the method:
catch (OperationCanceledException)
{
}
this.logger!.LogWarning("Stopped streaming Tauri events.");
}
}