Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions Endpoints/ComponentEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,63 @@ public static void MapComponentEndpoints(this WebApplication app)
httpContext.Response.Headers.CacheControl = "no-cache";
httpContext.Response.Headers.Connection = "keep-alive";

var syncLock = new SemaphoreSlim(1, 1);
var channel = System.Threading.Channels.Channel.CreateUnbounded<double>(new System.Threading.Channels.UnboundedChannelOptions
{
SingleWriter = true,
SingleReader = true
});

var progress = new Progress<double>(percent =>
{
_ = Task.Run(async () =>
channel.Writer.TryWrite(percent);
});

var writerTask = Task.Run(async () =>
{
try
{
await syncLock.WaitAsync();
try
while (await channel.Reader.WaitToReadAsync(httpContext.RequestAborted))
{
var eventData = JsonSerializer.Serialize(new { progress = percent, status = "installing" });
await httpContext.Response.WriteAsync($"data: {eventData}\n\n");
await httpContext.Response.Body.FlushAsync();
while (channel.Reader.TryRead(out var percent))
{
var eventData = JsonSerializer.Serialize(new { progress = percent, status = "installing" });
await httpContext.Response.WriteAsync($"data: {eventData}\n\n", httpContext.RequestAborted);
await httpContext.Response.Body.FlushAsync(httpContext.RequestAborted);
}
}
catch { }
finally
{
syncLock.Release();
}
});
}
catch (OperationCanceledException) { }
catch { }
});

try
{
var result = await componentService.InstallComponentAsync(componentId, progress, httpContext.RequestAborted);
await syncLock.WaitAsync();
try
{
var finalData = JsonSerializer.Serialize(new { progress = 100.0, status = result ? "completed" : "failed", success = result });
await httpContext.Response.WriteAsync($"data: {finalData}\n\n");
await httpContext.Response.Body.FlushAsync();
}
finally
{
syncLock.Release();
}
channel.Writer.TryComplete();
await writerTask;

var finalData = JsonSerializer.Serialize(new { progress = 100.0, status = result ? "completed" : "failed", success = result });
await httpContext.Response.WriteAsync($"data: {finalData}\n\n", httpContext.RequestAborted);
await httpContext.Response.Body.FlushAsync(httpContext.RequestAborted);
}
catch (OperationCanceledException)
{
channel.Writer.TryComplete();
await writerTask;
// Request canceled
}
catch (Exception ex)
{
await syncLock.WaitAsync();
channel.Writer.TryComplete();
await writerTask;

try
{
var errData = JsonSerializer.Serialize(new { progress = 0.0, status = "error", message = ex.Message });
await httpContext.Response.WriteAsync($"data: {errData}\n\n");
await httpContext.Response.Body.FlushAsync();
}
finally
{
syncLock.Release();
await httpContext.Response.WriteAsync($"data: {errData}\n\n", httpContext.RequestAborted);
await httpContext.Response.Body.FlushAsync(httpContext.RequestAborted);
}
catch { }
}

return Results.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public async Task InstallAndUninstallComponent_Succeeds()
var installReq = new ComponentInstallRequest { ComponentId = "audio-tts" };
var installResponse = await client.PostAsJsonAsync("/api/components/install", installReq);
Assert.Equal(HttpStatusCode.OK, installResponse.StatusCode);
var installContent = await installResponse.Content.ReadAsStringAsync();
Assert.Contains("completed", installContent);

// Uninstall request
var uninstallReq = new ComponentInstallRequest { ComponentId = "audio-tts" };
Expand All @@ -69,6 +71,8 @@ public async Task InstallAndUninstallComponent_Succeeds()
var aiInstallReq = new ComponentInstallRequest { ComponentId = "ai-assistant" };
var aiInstallResponse = await client.PostAsJsonAsync("/api/components/install", aiInstallReq);
Assert.Equal(HttpStatusCode.OK, aiInstallResponse.StatusCode);
var aiInstallContent = await aiInstallResponse.Content.ReadAsStringAsync();
Assert.Contains("completed", aiInstallContent);

var aiUninstallReq = new ComponentInstallRequest { ComponentId = "ai-assistant" };
var aiUninstallResponse = await client.PostAsJsonAsync("/api/components/uninstall", aiUninstallReq);
Expand Down
Loading