mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-07-16 17:46:27 +00:00
Some checks are pending
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (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, nsis) (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,updater, appimage) (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,app,updater, dmg) (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, nsis) (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,updater, appimage) (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
69 lines
3.2 KiB
C#
69 lines
3.2 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Text.Json;
|
|
|
|
using AIStudio.Tools.Rust;
|
|
|
|
namespace AIStudio.Tools.Services;
|
|
|
|
public partial class RustService
|
|
{
|
|
/// <summary>Starts a Rust media normalization job.</summary>
|
|
/// <param name="inputPath">Absolute source path.</param>
|
|
/// <param name="outputPath">Absolute operation-owned output path.</param>
|
|
/// <param name="token">Request cancellation token.</param>
|
|
/// <returns>The opaque runtime job identifier.</returns>
|
|
public async Task<string> StartMediaJobAsync(string inputPath, string outputPath, CancellationToken token = default)
|
|
{
|
|
using var response = await this.http.PostAsJsonAsync(
|
|
"/media/jobs",
|
|
new CreateMediaJobRequest(inputPath, outputPath),
|
|
this.jsonRustSerializerOptions,
|
|
token);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
var result = await response.Content.ReadFromJsonAsync<CreateMediaJobResponse>(this.jsonRustSerializerOptions, token);
|
|
return result?.JobId ?? throw new InvalidOperationException("The Rust runtime did not return a media job ID.");
|
|
}
|
|
|
|
/// <summary>Streams replayed and live snapshots until the media job becomes terminal.</summary>
|
|
/// <param name="jobId">Runtime job identifier.</param>
|
|
/// <param name="token">Stream cancellation token.</param>
|
|
/// <returns>Asynchronous media job snapshots.</returns>
|
|
public async IAsyncEnumerable<MediaJobEvent> StreamMediaJobEventsAsync(string jobId, [EnumeratorCancellation] CancellationToken token = default)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, $"/media/jobs/{Uri.EscapeDataString(jobId)}/events");
|
|
using var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(token);
|
|
using var reader = new StreamReader(stream);
|
|
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
var line = await reader.ReadLineAsync(token);
|
|
if (line is null)
|
|
yield break;
|
|
|
|
if (!line.StartsWith("data:", StringComparison.Ordinal))
|
|
continue;
|
|
|
|
var json = line["data:".Length..].Trim();
|
|
var mediaEvent = JsonSerializer.Deserialize<MediaJobEvent>(json, this.jsonRustSerializerOptions);
|
|
if (mediaEvent is not null)
|
|
yield return mediaEvent;
|
|
|
|
if (mediaEvent?.Phase is MediaJobPhase.COMPLETED or MediaJobPhase.FAILED or MediaJobPhase.CANCELLED)
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
/// <summary>Requests cooperative cancellation of a Rust media job.</summary>
|
|
/// <param name="jobId">Runtime job identifier.</param>
|
|
/// <param name="token">Request cancellation token.</param>
|
|
public async Task CancelMediaJobAsync(string jobId, CancellationToken token = default)
|
|
{
|
|
using var response = await this.http.DeleteAsync($"/media/jobs/{Uri.EscapeDataString(jobId)}", token);
|
|
if (response is { IsSuccessStatusCode: false, StatusCode: not System.Net.HttpStatusCode.NotFound })
|
|
response.EnsureSuccessStatusCode();
|
|
}
|
|
} |