mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-11-23 13:50:20 +00:00
Implemented the .NET side of Tauri event stream handling
This commit is contained in:
parent
398fd613ec
commit
77bd3b389c
@ -133,6 +133,11 @@ internal sealed class Program
|
||||
builder.Services.AddHostedService<UpdateService>();
|
||||
builder.Services.AddHostedService<TemporaryChatService>();
|
||||
builder.Services.AddHostedService<EnterpriseEnvironmentService>();
|
||||
|
||||
// ReSharper disable AccessToDisposedClosure
|
||||
builder.Services.AddHostedService<RustService>(_ => rust);
|
||||
// ReSharper restore AccessToDisposedClosure
|
||||
|
||||
builder.Services.AddRazorComponents()
|
||||
.AddInteractiveServerComponents()
|
||||
.AddHubOptions(options =>
|
||||
|
||||
@ -14,6 +14,7 @@ public enum Event
|
||||
SHOW_ERROR,
|
||||
SHOW_WARNING,
|
||||
SHOW_SUCCESS,
|
||||
TAURI_EVENT_RECEIVED,
|
||||
|
||||
// Update events:
|
||||
USER_SEARCH_FOR_UPDATE,
|
||||
|
||||
3
app/MindWork AI Studio/Tools/Rust/TauriEvent.cs
Normal file
3
app/MindWork AI Studio/Tools/Rust/TauriEvent.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
public readonly record struct TauriEvent(TauriEventType Type, List<string> Payload);
|
||||
11
app/MindWork AI Studio/Tools/Rust/TauriEventType.cs
Normal file
11
app/MindWork AI Studio/Tools/Rust/TauriEventType.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
public enum TauriEventType
|
||||
{
|
||||
WINDOW_FOCUSED,
|
||||
WINDOW_NOT_FOCUSED,
|
||||
|
||||
FILE_DROP_HOVERED,
|
||||
FILE_DROP_DROPPED,
|
||||
FILE_DROP_CANCELED,
|
||||
}
|
||||
53
app/MindWork AI Studio/Tools/Services/RustService.Events.cs
Normal file
53
app/MindWork AI Studio/Tools/Services/RustService.Events.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System.Text.Json;
|
||||
|
||||
using AIStudio.Tools.Rust;
|
||||
|
||||
namespace AIStudio.Tools.Services;
|
||||
|
||||
public partial class RustService
|
||||
{
|
||||
private async Task StartStreamTauriEvents(CancellationToken stopToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stopToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var stream = await this.http.GetStreamAsync("/events", stopToken);
|
||||
using var reader = new StreamReader(stream);
|
||||
while(!reader.EndOfStream)
|
||||
{
|
||||
var line = await reader.ReadLineAsync(stopToken);
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
var tauriEvent = JsonSerializer.Deserialize<TauriEvent>(line, this.jsonRustSerializerOptions);
|
||||
if (tauriEvent != default)
|
||||
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.");
|
||||
}
|
||||
}
|
||||
@ -8,7 +8,7 @@ namespace AIStudio.Tools.Services;
|
||||
/// <summary>
|
||||
/// Calling Rust functions.
|
||||
/// </summary>
|
||||
public sealed partial class RustService : IDisposable
|
||||
public sealed partial class RustService : BackgroundService
|
||||
{
|
||||
private readonly HttpClient http;
|
||||
|
||||
@ -59,11 +59,18 @@ public sealed partial class RustService : IDisposable
|
||||
this.encryptor = encryptionService;
|
||||
}
|
||||
|
||||
#region IDisposable
|
||||
#region Overrides of BackgroundService
|
||||
|
||||
public void Dispose()
|
||||
protected override async Task ExecuteAsync(CancellationToken stopToken)
|
||||
{
|
||||
this.logger?.LogInformation("The Rust service was initialized.");
|
||||
await this.StartStreamTauriEvents(stopToken);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
this.http.Dispose();
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Loading…
Reference in New Issue
Block a user