Generated the JSON contracts of providers, the runtime, and services at compile time (#1150)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-03 07:59:30 +01:00
1 parent e954881fda
commit 24e3c22db3
140 files changed
+1415 -292

No files matched your search

@@ -0,0 +1,24 @@
using System.Text.Json.Serialization;
namespace AIStudio.Tools.Services;
/// <summary>
/// The contracts with which System.Text.Json reads the events of a file extraction from the runtime, built at
/// compile time.
/// </summary>
/// <remarks>
/// A converter reads the metadata of an event as the type its JSON tells, so each of these types needs a
/// contract of its own here. A test fails for one which is missing.
/// </remarks>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(ContentStreamSseEvent))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(ContentStreamTextMetadata))]
[JsonSerializable(typeof(ContentStreamPdfMetadata))]
[JsonSerializable(typeof(ContentStreamSpreadsheetMetadata))]
[JsonSerializable(typeof(ContentStreamPresentationMetadata))]
[JsonSerializable(typeof(ContentStreamImageMetadata))]
[JsonSerializable(typeof(ContentStreamDocumentMetadata))]
[JsonSerializable(typeof(ContentStreamErrorMetadata))]
[JsonSerializable(typeof(ContentStreamPromptInjectionMetadata))]
internal sealed partial class RustContentStreamJsonContext : JsonSerializerContext;
@@ -0,0 +1,89 @@
using System.Text.Json.Serialization;
using AIStudio.Tools.Databases.VectorStore;
using AIStudio.Tools.Rust;
namespace AIStudio.Tools.Services;
/// <summary>
/// The contracts with which System.Text.Json reads and writes nearly every call to the runtime, built at
/// compile time.
/// </summary>
/// <remarks>
/// The calls to the vector store are generic: every query of its client needs the answer type with the type
/// of its result, e.g., DatabaseQueryResponse&lt;bool?&gt;. A test fails for a request or an answer which is
/// missing here.
/// </remarks>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
// The requests:
[JsonSerializable(typeof(SelectSecretRequest))]
[JsonSerializable(typeof(StoreSecretRequest))]
[JsonSerializable(typeof(RustService.ShareFileRequest))]
[JsonSerializable(typeof(PreviousDirectory))]
[JsonSerializable(typeof(SelectFileOptions))]
[JsonSerializable(typeof(SaveFileOptions))]
[JsonSerializable(typeof(OpenPathRequest))]
[JsonSerializable(typeof(OpenDocumentRequest))]
[JsonSerializable(typeof(RegisterShortcutRequest))]
[JsonSerializable(typeof(ValidateShortcutRequest))]
[JsonSerializable(typeof(SetInterfaceSizeRequest))]
[JsonSerializable(typeof(CreateMediaJobRequest))]
[JsonSerializable(typeof(LogEventRequest))]
[JsonSerializable(typeof(ValidateTokenizerRequest))]
[JsonSerializable(typeof(StoreTokenizerRequest))]
[JsonSerializable(typeof(DeleteTokenizerRequest))]
[JsonSerializable(typeof(CountTokensRequest))]
[JsonSerializable(typeof(PrepareImageRequest))]
[JsonSerializable(typeof(PrepareImageForModelRequest))]
// The answers:
[JsonSerializable(typeof(RequestedSecret))]
[JsonSerializable(typeof(StoreSecretResponse))]
[JsonSerializable(typeof(DeleteSecretResponse))]
[JsonSerializable(typeof(ShareFileResponse))]
[JsonSerializable(typeof(ImagePrepareResponse))]
[JsonSerializable(typeof(PreparedModelImage))]
[JsonSerializable(typeof(TauriEvent))]
[JsonSerializable(typeof(UpdateResponse))]
[JsonSerializable(typeof(SetClipboardResponse))]
[JsonSerializable(typeof(DirectorySelectionResponse))]
[JsonSerializable(typeof(FileSelectionResponse))]
[JsonSerializable(typeof(FilesSelectionResponse))]
[JsonSerializable(typeof(FileSaveResponse))]
[JsonSerializable(typeof(OpenPathResponse))]
[JsonSerializable(typeof(OpenDocumentResponse))]
[JsonSerializable(typeof(ShortcutRegistrationResult))]
[JsonSerializable(typeof(ShortcutValidationResponse))]
[JsonSerializable(typeof(ShortcutResponse))]
[JsonSerializable(typeof(RuntimeInfoResponse))]
[JsonSerializable(typeof(AppExitResponse))]
[JsonSerializable(typeof(AppRestartResponse))]
[JsonSerializable(typeof(InterfaceSizeResponse))]
[JsonSerializable(typeof(CreateMediaJobResponse))]
[JsonSerializable(typeof(MediaJobEvent))]
[JsonSerializable(typeof(List<EnterpriseConfig>))]
[JsonSerializable(typeof(GetLogPathsResponse))]
[JsonSerializable(typeof(TokenizerResponse))]
// The vector store, with the result of every query its client sends:
[JsonSerializable(typeof(QdrantEdgeInfo))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.EnsureVectorStoreRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.CheckVectorStoreRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.InsertEmbeddingRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.SearchEmbeddingRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.RetrieveVectorsRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.DeleteEmbeddingByFileRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.RelocateEmbeddingFileRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.OptimizeVectorStoreRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.DeleteVectorStoreRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.PrepareBackupRequest))]
[JsonSerializable(typeof(QdrantEdgeClientImplementation.CopyStoreForBackupRequest))]
[JsonSerializable(typeof(RustService.DatabaseOperationResponse))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<VectorStoreEnsureResult>))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<bool?>))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<List<VectorSearchResult>>))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<List<StoredVector>>))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<VectorStoreBackupPreparation>))]
[JsonSerializable(typeof(RustService.DatabaseQueryResponse<long?>))]
internal sealed partial class RustJsonContext : JsonSerializerContext;
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
using AIStudio.Tools.Rust;
namespace AIStudio.Tools.Services;
/// <summary>
/// The contracts with which System.Text.Json reads and writes the calls to the prompt injection filter of the
/// runtime, built at compile time.
/// </summary>
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(SanitizePromptInjectionsRequest))]
[JsonSerializable(typeof(SanitizePromptInjectionsResponse))]
[JsonSerializable(typeof(SanitizePromptInjectionsBatchRequest))]
[JsonSerializable(typeof(SanitizePromptInjectionsBatchResponse))]
internal sealed partial class RustPromptInjectionJsonContext : JsonSerializerContext;
@@ -63,7 +63,7 @@ public sealed partial class RustService
private async Task<RequestedSecret> GetAPIKeyByKey(string secretKey, bool isTrying)
{
var secretRequest = new SelectSecretRequest(secretKey, Environment.UserName, isTrying);
var result = await this.http.PostAsJsonAsync("/secrets/get", secretRequest, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/get", secretRequest, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
if(!isTrying)
@@ -71,7 +71,7 @@ public sealed partial class RustService
return new RequestedSecret(false, new EncryptedText(string.Empty), TB("Failed to get the API key due to an API issue."));
}
var secret = await result.Content.ReadFromJsonAsync<RequestedSecret>(this.jsonRustSerializerOptions);
var secret = await result.Content.ReadFromJsonAsync<RequestedSecret>(JSON_OPTIONS);
if (secret.Success)
this.logger!.LogDebug($"Successfully retrieved the API key for '{secretKey}'.");
else if (isTrying)
@@ -106,14 +106,14 @@ public sealed partial class RustService
private async Task<StoreSecretResponse> StoreEncryptedAPIKeyByKey(string secretKey, EncryptedText encryptedKey)
{
var request = new StoreSecretRequest(secretKey, Environment.UserName, encryptedKey);
var result = await this.http.PostAsJsonAsync("/secrets/store", request, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/store", request, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
this.logger!.LogError($"Failed to store the API key for '{secretKey}' due to an API issue: '{result.StatusCode}'");
return new StoreSecretResponse(false, TB("Failed to store the API key due to an API issue."));
}
var state = await result.Content.ReadFromJsonAsync<StoreSecretResponse>(this.jsonRustSerializerOptions);
var state = await result.Content.ReadFromJsonAsync<StoreSecretResponse>(JSON_OPTIONS);
if (!state.Success)
this.logger!.LogError($"Failed to store the API key for '{secretKey}': '{state.Issue}'");
else
@@ -149,14 +149,14 @@ public sealed partial class RustService
private async Task<DeleteSecretResponse> DeleteAPIKeyByKey(string secretKey, bool isTrying = false)
{
var request = new SelectSecretRequest(secretKey, Environment.UserName, false);
var result = await this.http.PostAsJsonAsync("/secrets/delete", request, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/delete", request, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
this.logger!.LogError($"Failed to delete the API key for '{secretKey}' due to an API issue: '{result.StatusCode}'");
return new DeleteSecretResponse{Success = false, WasEntryFound = false, Issue = TB("Failed to delete the API key due to an API issue.")};
}
var state = await result.Content.ReadFromJsonAsync<DeleteSecretResponse>(this.jsonRustSerializerOptions);
var state = await result.Content.ReadFromJsonAsync<DeleteSecretResponse>(JSON_OPTIONS);
if (!state.Success && !isTrying)
this.logger!.LogError($"Failed to delete the API key for '{secretKey}': '{state.Issue}'");
@@ -122,7 +122,7 @@ public sealed partial class RustService
public async Task<RuntimeInfoResponse> GetRuntimeInfo()
{
var response = await this.http.GetFromJsonAsync<RuntimeInfoResponse>("/system/runtime/info", this.jsonRustSerializerOptions);
var response = await this.http.GetFromJsonAsync<RuntimeInfoResponse>("/system/runtime/info", JSON_OPTIONS);
return response;
}
@@ -140,7 +140,7 @@ public sealed partial class RustService
return false;
}
var result = await response.Content.ReadFromJsonAsync<AppExitResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<AppExitResponse>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to exit the app: {Error}", result?.ErrorMessage ?? "Unknown error");
@@ -177,7 +177,7 @@ public sealed partial class RustService
return false;
}
var result = await response.Content.ReadFromJsonAsync<AppRestartResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<AppRestartResponse>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to restart the app: {Error}", result?.ErrorMessage ?? "Unknown error");
@@ -203,7 +203,7 @@ public sealed partial class RustService
{
try
{
var response = await this.http.PostAsJsonAsync("/app/interface-size", request, this.jsonRustSerializerOptions);
var response = await this.http.PostAsJsonAsync("/app/interface-size", request, JSON_OPTIONS);
var result = await this.ReadInterfaceSizeResponse(response, "set the interface size");
if (result is not null)
this.logger?.LogInformation("Set the interface sizes of {ScreenCount} screens. AI Studio shows with the size '{Size}' at {CurrentPercent} % and a zoom of {Zoom:0.###} on the screen '{ScreenKey}'.", request.Screens.Count, result.Size, result.CurrentPercent, result.Zoom, result.ScreenKey);
@@ -243,7 +243,7 @@ public sealed partial class RustService
return null;
}
var result = await response.Content.ReadFromJsonAsync<InterfaceSizeResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<InterfaceSizeResponse>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to {Action}: {Error}", action, result?.ErrorMessage ?? "Unknown error");
@@ -28,7 +28,7 @@ public sealed partial class RustService
return;
}
var state = await response.Content.ReadFromJsonAsync<SetClipboardResponse>(this.jsonRustSerializerOptions);
var state = await response.Content.ReadFromJsonAsync<SetClipboardResponse>(JSON_OPTIONS);
if (!state.Success)
{
this.logger!.LogError("Failed to copy the text to the clipboard.");
@@ -24,7 +24,7 @@ public sealed partial class RustService
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(45));
var databaseInfo = await this.http.GetFromJsonAsync<TDatabaseInfo>(infoPath, this.jsonRustSerializerOptions, cts.Token);
var databaseInfo = await this.http.GetFromJsonAsync<TDatabaseInfo>(infoPath, JSON_OPTIONS, cts.Token);
return databaseInfo ?? unavailableFactory("The database information response was empty.");
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
@@ -52,10 +52,10 @@ public sealed partial class RustService
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromMinutes(5));
using var response = await this.http.PostAsJsonAsync(path, request, this.jsonRustSerializerOptions, cts.Token);
using var response = await this.http.PostAsJsonAsync(path, request, JSON_OPTIONS, cts.Token);
response.EnsureSuccessStatusCode();
var operation = await response.Content.ReadFromJsonAsync<DatabaseOperationResponse>(this.jsonRustSerializerOptions, cts.Token);
var operation = await response.Content.ReadFromJsonAsync<DatabaseOperationResponse>(JSON_OPTIONS, cts.Token);
if (operation is not { Success: true })
throw CreateDatabaseException(operation?.Issue, operation?.IssueCode, $"The {databaseName} operation failed.");
}
@@ -65,10 +65,10 @@ public sealed partial class RustService
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromMinutes(5));
using var response = await this.http.PostAsJsonAsync(path, request, this.jsonRustSerializerOptions, cts.Token);
using var response = await this.http.PostAsJsonAsync(path, request, JSON_OPTIONS, cts.Token);
response.EnsureSuccessStatusCode();
var operation = await response.Content.ReadFromJsonAsync<DatabaseQueryResponse<TResult>>(this.jsonRustSerializerOptions, cts.Token);
var operation = await response.Content.ReadFromJsonAsync<DatabaseQueryResponse<TResult>>(JSON_OPTIONS, cts.Token);
if (operation is not { Success: true })
throw CreateDatabaseException(operation?.Issue, operation?.IssueCode, $"The {databaseName} query failed.");
@@ -94,7 +94,10 @@ public sealed partial class RustService
};
}
private sealed record DatabaseOperationResponse(bool Success, string Issue, string IssueCode);
//
// Internal rather than private, so that the JSON context of the runtime can name them:
//
internal sealed record DatabaseOperationResponse(bool Success, string Issue, string IssueCode);
private sealed record DatabaseQueryResponse<TResult>(bool Success, string Issue, string IssueCode, TResult? Data);
internal sealed record DatabaseQueryResponse<TResult>(bool Success, string Issue, string IssueCode, TResult? Data);
}
@@ -39,7 +39,7 @@ public sealed partial class RustService
throw new HttpRequestException($"Failed to query the enterprise configurations: '{result.StatusCode}'");
}
var configs = await result.Content.ReadFromJsonAsync<List<EnterpriseConfig>>(this.jsonRustSerializerOptions);
var configs = await result.Content.ReadFromJsonAsync<List<EnterpriseConfig>>(JSON_OPTIONS);
if (configs is null)
throw new InvalidOperationException("Failed to parse the enterprise configurations from Rust.");
@@ -35,7 +35,7 @@ public partial class RustService
continue;
// Deserialize the Tauri event:
var tauriEvent = JsonSerializer.Deserialize<TauriEvent>(line, this.jsonRustSerializerOptions);
var tauriEvent = JsonSerializer.Deserialize<TauriEvent>(line, JSON_OPTIONS);
// Forward relevant events to the message bus:
if (tauriEvent != default && tauriEvent.EventType
@@ -16,10 +16,10 @@ public sealed partial class RustService
var startDirectory = initialDirectory is null ? null : PathTools.FindNearestExistingPath(initialDirectory);
var result = startDirectory is null
? await this.http.PostAsync($"/select/directory?title={encodedTitle}", null)
: await this.http.PostAsJsonAsync($"/select/directory?title={encodedTitle}", new PreviousDirectory(startDirectory), this.jsonRustSerializerOptions);
: await this.http.PostAsJsonAsync($"/select/directory?title={encodedTitle}", new PreviousDirectory(startDirectory), JSON_OPTIONS);
if (result.IsSuccessStatusCode)
return await result.Content.ReadFromJsonAsync<DirectorySelectionResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<DirectorySelectionResponse>(JSON_OPTIONS);
this.logger!.LogError("Failed to select a directory: '{StatusCode}'", result.StatusCode);
return new DirectorySelectionResponse(true, string.Empty);
@@ -63,9 +63,9 @@ public sealed partial class RustService
Filter = FileTypes.ToFileDialogFilter(filter)
};
var result = await this.http.PostAsJsonAsync("/select/file", payload, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/select/file", payload, JSON_OPTIONS);
if (result.IsSuccessStatusCode)
return await result.Content.ReadFromJsonAsync<FileSelectionResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<FileSelectionResponse>(JSON_OPTIONS);
this.logger!.LogError("Failed to select a file: '{StatusCode}'", result.StatusCode);
return new FileSelectionResponse(true, string.Empty);
@@ -87,9 +87,9 @@ public sealed partial class RustService
Filter = FileTypes.ToFileDialogFilter(filter)
};
var result = await this.http.PostAsJsonAsync("/select/files", payload, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/select/files", payload, JSON_OPTIONS);
if (result.IsSuccessStatusCode)
return await result.Content.ReadFromJsonAsync<FilesSelectionResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<FilesSelectionResponse>(JSON_OPTIONS);
this.logger!.LogError("Failed to select files: '{StatusCode}'", result.StatusCode);
return new FilesSelectionResponse(true, Array.Empty<string>());
@@ -118,9 +118,9 @@ public sealed partial class RustService
Filter = FileTypes.ToFileDialogFilter(filter)
};
var result = await this.http.PostAsJsonAsync("/save/file", payload, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/save/file", payload, JSON_OPTIONS);
if (result.IsSuccessStatusCode)
return await result.Content.ReadFromJsonAsync<FileSaveResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<FileSaveResponse>(JSON_OPTIONS);
this.logger!.LogError("Failed to select a file for writing operation: '{StatusCode}'", result.StatusCode);
return new FileSaveResponse(true, string.Empty);
@@ -133,7 +133,7 @@ public sealed partial class RustService
HttpResponseMessage result;
try
{
result = await this.http.PostAsJsonAsync("/open/path", new OpenPathRequest(path), this.jsonRustSerializerOptions);
result = await this.http.PostAsJsonAsync("/open/path", new OpenPathRequest(path), JSON_OPTIONS);
}
catch (HttpRequestException e)
{
@@ -154,7 +154,7 @@ public sealed partial class RustService
return new OpenPathResponse(false, string.Format(TB("The runtime file manager endpoint returned '{0}'."), result.StatusCode));
}
var response = await result.Content.ReadFromJsonAsync<OpenPathResponse>(this.jsonRustSerializerOptions);
var response = await result.Content.ReadFromJsonAsync<OpenPathResponse>(JSON_OPTIONS);
var normalizedResponse = response.Success
? response
: new OpenPathResponse(false, string.IsNullOrWhiteSpace(response.Issue) ? TB("The runtime file manager endpoint failed without details.") : response.Issue);
@@ -187,7 +187,7 @@ public sealed partial class RustService
HttpResponseMessage result;
try
{
result = await this.http.PostAsJsonAsync("/open/document", new OpenDocumentRequest(path, pageNumber), this.jsonRustSerializerOptions);
result = await this.http.PostAsJsonAsync("/open/document", new OpenDocumentRequest(path, pageNumber), JSON_OPTIONS);
}
catch (HttpRequestException e)
{
@@ -208,7 +208,7 @@ public sealed partial class RustService
return new OpenDocumentResponse(false, false, string.Format(TB("The runtime document endpoint returned '{0}'."), result.StatusCode));
}
var response = await result.Content.ReadFromJsonAsync<OpenDocumentResponse>(this.jsonRustSerializerOptions);
var response = await result.Content.ReadFromJsonAsync<OpenDocumentResponse>(JSON_OPTIONS);
if (response.Success)
{
//
@@ -20,10 +20,10 @@ public sealed partial class RustService
bool optimize,
CancellationToken token = default)
{
using var response = await this.http.PostAsJsonAsync("/image/prepare", new { path, optimize }, this.jsonRustSerializerOptions, token);
using var response = await this.http.PostAsJsonAsync("/image/prepare", new PrepareImageRequest(path, optimize), JSON_OPTIONS, token);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<ImagePrepareResponse>(this.jsonRustSerializerOptions, token)
return await response.Content.ReadFromJsonAsync<ImagePrepareResponse>(JSON_OPTIONS, token)
?? throw new InvalidDataException("The Rust image preparation returned an empty response.");
}
@@ -44,10 +44,10 @@ public sealed partial class RustService
{
try
{
using var response = await this.http.PostAsJsonAsync("/image/prepare-for-model", new { path, maxBytes }, this.jsonRustSerializerOptions, token);
using var response = await this.http.PostAsJsonAsync("/image/prepare-for-model", new PrepareImageForModelRequest(path, maxBytes), JSON_OPTIONS, token);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<PreparedModelImage>(this.jsonRustSerializerOptions, token)
return await response.Content.ReadFromJsonAsync<PreparedModelImage>(JSON_OPTIONS, token)
?? PreparedModelImage.Failed(ModelImageErrorCode.UNKNOWN);
}
catch (Exception e) when (e is not OperationCanceledException)
@@ -10,7 +10,7 @@ public sealed partial class RustService
/// <returns>The paths of the log files.</returns>
public async Task<GetLogPathsResponse> GetLogPaths()
{
return await this.http.GetFromJsonAsync<GetLogPathsResponse>("/log/paths", this.jsonRustSerializerOptions);
return await this.http.GetFromJsonAsync<GetLogPathsResponse>("/log/paths", JSON_OPTIONS);
}
/// <summary>
@@ -33,7 +33,7 @@ public sealed partial class RustService
// feed itself. The unobserved task exception handler in Program.cs remains the safety net here.
//
var request = new LogEventRequest(timestamp, level, category, message, exception, stackTrace);
_ = this.http.PostAsJsonAsync("/log/event", request, this.jsonRustSerializerOptions);
_ = this.http.PostAsJsonAsync("/log/event", request, JSON_OPTIONS);
}
catch
{
@@ -18,11 +18,11 @@ public partial class RustService
using var response = await this.http.PostAsJsonAsync(
"/media/jobs",
new CreateMediaJobRequest(inputPath, outputPath, OpusBitrateBps: opusBitrateBps),
this.jsonRustSerializerOptions,
JSON_OPTIONS,
token);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<CreateMediaJobResponse>(this.jsonRustSerializerOptions, token);
var result = await response.Content.ReadFromJsonAsync<CreateMediaJobResponse>(JSON_OPTIONS, token);
return result?.JobId ?? throw new InvalidOperationException("The Rust runtime did not return a media job ID.");
}
@@ -49,7 +49,7 @@ public partial class RustService
continue;
var json = line["data:".Length..].Trim();
var mediaEvent = JsonSerializer.Deserialize<MediaJobEvent>(json, this.jsonRustSerializerOptions);
var mediaEvent = JsonSerializer.Deserialize<MediaJobEvent>(json, JSON_OPTIONS);
if (mediaEvent is not null)
yield return mediaEvent;
@@ -112,7 +112,7 @@ public sealed partial class RustService
try
{
var sseEvent = JsonSerializer.Deserialize<ContentStreamSseEvent>(jsonContent);
var sseEvent = JsonSerializer.Deserialize<ContentStreamSseEvent>(jsonContent, CONTENT_STREAM_JSON_OPTIONS);
if (sseEvent is null)
continue;
@@ -364,7 +364,7 @@ public sealed partial class RustService
ContentStreamSseEvent? sseEvent = null;
try
{
sseEvent = JsonSerializer.Deserialize<ContentStreamSseEvent>(jsonContent);
sseEvent = JsonSerializer.Deserialize<ContentStreamSseEvent>(jsonContent, CONTENT_STREAM_JSON_OPTIONS);
}
catch (JsonException)
{
@@ -460,7 +460,7 @@ public sealed partial class RustService
{
try
{
var errorMessage = JsonSerializer.Deserialize<string>(jsonContent);
var errorMessage = JsonSerializer.Deserialize<string>(jsonContent, CONTENT_STREAM_JSON_OPTIONS);
if (string.IsNullOrWhiteSpace(errorMessage))
return false;
@@ -65,14 +65,14 @@ public sealed partial class RustService
var secretKey = SecretKey(secretId, storeType);
var encryptedSecret = await this.encryptor!.Encrypt(secretData);
var request = new StoreSecretRequest(secretKey, Environment.UserName, encryptedSecret);
var result = await this.http.PostAsJsonAsync("/secrets/store", request, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/store", request, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
this.logger!.LogError($"Failed to store the secret data for '{secretKey}' due to an API issue: '{result.StatusCode}'");
return new StoreSecretResponse(false, TB("Failed to store the secret data due to an API issue."));
}
var state = await result.Content.ReadFromJsonAsync<StoreSecretResponse>(this.jsonRustSerializerOptions);
var state = await result.Content.ReadFromJsonAsync<StoreSecretResponse>(JSON_OPTIONS);
if (!state.Success)
this.logger!.LogError($"Failed to store the secret data for '{secretKey}': '{state.Issue}'");
@@ -104,7 +104,7 @@ public sealed partial class RustService
private async Task<RequestedSecret> GetSecretByKey(string secretKey, bool isTrying)
{
var secretRequest = new SelectSecretRequest(secretKey, Environment.UserName, isTrying);
var result = await this.http.PostAsJsonAsync("/secrets/get", secretRequest, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/get", secretRequest, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
if(isTrying)
@@ -114,7 +114,7 @@ public sealed partial class RustService
return new RequestedSecret(false, new EncryptedText(string.Empty), TB("Failed to get the secret data due to an API issue."));
}
var state = await result.Content.ReadFromJsonAsync<RequestedSecret>(this.jsonRustSerializerOptions);
var state = await result.Content.ReadFromJsonAsync<RequestedSecret>(JSON_OPTIONS);
if (!state.Success)
{
//
@@ -138,14 +138,14 @@ public sealed partial class RustService
private async Task<DeleteSecretResponse> DeleteSecretByKey(string secretKey)
{
var request = new SelectSecretRequest(secretKey, Environment.UserName, false);
var result = await this.http.PostAsJsonAsync("/secrets/delete", request, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/secrets/delete", request, JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
this.logger!.LogError($"Failed to delete the secret data for '{secretKey}' due to an API issue: '{result.StatusCode}'");
return new DeleteSecretResponse{Success = false, WasEntryFound = false, Issue = TB("Failed to delete the secret data due to an API issue.")};
}
var state = await result.Content.ReadFromJsonAsync<DeleteSecretResponse>(this.jsonRustSerializerOptions);
var state = await result.Content.ReadFromJsonAsync<DeleteSecretResponse>(JSON_OPTIONS);
if (!state.Success)
this.logger!.LogError($"Failed to delete the secret data for '{secretKey}': '{state.Issue}'");
@@ -40,7 +40,8 @@ public sealed partial class RustService
using var response = await this.http.PostAsJsonAsync(
"/security/prompt-injection/sanitize",
new SanitizePromptInjectionsRequest(text),
cancellationToken: timeoutTokenSource.Token);
PROMPT_INJECTION_JSON_OPTIONS,
timeoutTokenSource.Token);
if (!response.IsSuccessStatusCode)
{
@@ -48,7 +49,7 @@ public sealed partial class RustService
return null;
}
return await response.Content.ReadFromJsonAsync<SanitizePromptInjectionsResponse>(timeoutTokenSource.Token);
return await response.Content.ReadFromJsonAsync<SanitizePromptInjectionsResponse>(PROMPT_INJECTION_JSON_OPTIONS, timeoutTokenSource.Token);
}
catch (Exception exception)
{
@@ -82,7 +83,8 @@ public sealed partial class RustService
using var response = await this.http.PostAsJsonAsync(
"/security/prompt-injection/sanitize-batch",
new SanitizePromptInjectionsBatchRequest(texts),
cancellationToken: timeoutTokenSource.Token);
PROMPT_INJECTION_JSON_OPTIONS,
timeoutTokenSource.Token);
if (!response.IsSuccessStatusCode)
{
@@ -90,7 +92,7 @@ public sealed partial class RustService
return null;
}
var batchResponse = await response.Content.ReadFromJsonAsync<SanitizePromptInjectionsBatchResponse>(timeoutTokenSource.Token);
var batchResponse = await response.Content.ReadFromJsonAsync<SanitizePromptInjectionsBatchResponse>(PROMPT_INJECTION_JSON_OPTIONS, timeoutTokenSource.Token);
if (batchResponse.Results is null || batchResponse.Results.Count != texts.Count)
{
this.logger?.LogError("The prompt injection filter answered with {ResultCount} result(s) for {TextCount} text(s).", batchResponse.Results?.Count ?? 0, texts.Count);
@@ -1,4 +1,4 @@
// ReSharper disable NotAccessedPositionalProperty.Local
// ReSharper disable NotAccessedPositionalProperty.Global
using AIStudio.Tools.Rust;
namespace AIStudio.Tools.Services;
@@ -14,14 +14,14 @@ public sealed partial class RustService
{
try
{
using var response = await this.http.PostAsJsonAsync("/share/file", new ShareFileRequest(filePath), this.jsonRustSerializerOptions);
using var response = await this.http.PostAsJsonAsync("/share/file", new ShareFileRequest(filePath), JSON_OPTIONS);
if (!response.IsSuccessStatusCode)
{
this.logger?.LogError($"The Rust runtime rejected the share request: {response.StatusCode}.");
return new(ShareSheetOutcome.FAILED, $"The Rust runtime rejected the share request: {response.StatusCode}.");
}
var result = await response.Content.ReadFromJsonAsync<ShareFileResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<ShareFileResponse>(JSON_OPTIONS);
if (result is null)
{
this.logger?.LogError("The Rust runtime answered the share request without a result.");
@@ -50,5 +50,8 @@ public sealed partial class RustService
}
}
private sealed record ShareFileRequest(string FilePath);
/// <remarks>
/// Internal rather than private, so that the JSON context of the runtime can name it.
/// </remarks>
internal sealed record ShareFileRequest(string FilePath);
}
@@ -18,7 +18,7 @@ public sealed partial class RustService
try
{
var request = new RegisterShortcutRequest(shortcutId, shortcut, description, reconfigure);
var response = await this.http.PostAsJsonAsync("/shortcuts/register", request, this.jsonRustSerializerOptions);
var response = await this.http.PostAsJsonAsync("/shortcuts/register", request, JSON_OPTIONS);
if (!response.IsSuccessStatusCode)
{
@@ -26,7 +26,7 @@ public sealed partial class RustService
return ShortcutRegistrationResult.Failed(TB("The global shortcut could not be registered because the desktop service is unavailable."));
}
var result = await response.Content.ReadFromJsonAsync<ShortcutRegistrationResult>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<ShortcutRegistrationResult>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to register global shortcut '{ShortcutId}': {Error}", shortcutId, result?.ErrorMessage ?? "Unknown error");
@@ -53,7 +53,7 @@ public sealed partial class RustService
try
{
var request = new ValidateShortcutRequest(shortcut);
var response = await this.http.PostAsJsonAsync("/shortcuts/validate", request, this.jsonRustSerializerOptions);
var response = await this.http.PostAsJsonAsync("/shortcuts/validate", request, JSON_OPTIONS);
if (!response.IsSuccessStatusCode)
{
@@ -61,7 +61,7 @@ public sealed partial class RustService
return new ShortcutValidationResult(false, "Network error during validation", false, string.Empty);
}
var result = await response.Content.ReadFromJsonAsync<ShortcutValidationResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<ShortcutValidationResponse>(JSON_OPTIONS);
if (result is null)
return new ShortcutValidationResult(false, "Invalid response from server", false, string.Empty);
@@ -91,7 +91,7 @@ public sealed partial class RustService
return false;
}
var result = await response.Content.ReadFromJsonAsync<ShortcutResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<ShortcutResponse>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to suspend shortcut processing: {Error}", result?.ErrorMessage ?? "Unknown error");
@@ -123,7 +123,7 @@ public sealed partial class RustService
return false;
}
var result = await response.Content.ReadFromJsonAsync<ShortcutResponse>(this.jsonRustSerializerOptions);
var result = await response.Content.ReadFromJsonAsync<ShortcutResponse>(JSON_OPTIONS);
if (result is null || !result.Success)
{
this.logger?.LogError("Failed to resume shortcut processing: {Error}", result?.ErrorMessage ?? "Unknown error");
@@ -15,9 +15,7 @@ public sealed partial class RustService : ITokenizerStorage
public async Task<TokenizerResponse> ValidateTokenizer(string filePath)
{
var result = await this.http.PostAsJsonAsync("/tokenizer/validate", new {
file_path = filePath,
}, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/tokenizer/validate", new ValidateTokenizerRequest(filePath), JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
@@ -25,7 +23,7 @@ public sealed partial class RustService : ITokenizerStorage
return CreateUnavailableTokenizerResponse("An error occured while sending the path to the Rust framework for validation: "+result.StatusCode);
}
var response = await result.Content.ReadFromJsonAsync<TokenizerResponse>(this.jsonRustSerializerOptions);
var response = await result.Content.ReadFromJsonAsync<TokenizerResponse>(JSON_OPTIONS);
return response;
}
@@ -33,10 +31,7 @@ public sealed partial class RustService : ITokenizerStorage
public async Task<TokenizerResponse> StoreTokenizer(string modelId, string filePath)
{
this.logger!.LogInformation($"Storing tokenizer for model '{modelId}' from file '{filePath}'");
var result = await this.http.PostAsJsonAsync("/tokenizer/store", new {
model_id = modelId,
file_path = filePath,
}, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/tokenizer/store", new StoreTokenizerRequest(modelId, filePath), JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
@@ -44,15 +39,13 @@ public sealed partial class RustService : ITokenizerStorage
return CreateUnavailableTokenizerResponse("An error occured while sending the path to the Rust framework for storing: "+result.StatusCode);
}
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(JSON_OPTIONS);
}
public async Task<TokenizerResponse> DeleteTokenizer(string modelId)
{
this.logger!.LogInformation($"Deleting tokenizer for model '{modelId}'");
var result = await this.http.PostAsJsonAsync("/tokenizer/delete", new {
model_id = modelId,
}, this.jsonRustSerializerOptions);
var result = await this.http.PostAsJsonAsync("/tokenizer/delete", new DeleteTokenizerRequest(modelId), JSON_OPTIONS);
if (!result.IsSuccessStatusCode)
{
@@ -60,7 +53,7 @@ public sealed partial class RustService : ITokenizerStorage
return CreateUnavailableTokenizerResponse("An error occured while sending the tokenizer delete request to the Rust framework: "+result.StatusCode);
}
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(this.jsonRustSerializerOptions);
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(JSON_OPTIONS);
}
public Task<TokenizerResponse?> GetTokenCount(AIStudio.Settings.Provider provider, string text, CancellationToken cancellationToken = default) =>
@@ -71,10 +64,7 @@ public sealed partial class RustService : ITokenizerStorage
private async Task<TokenizerResponse?> GetTokenCount(string providerName, string tokenizerPath, string text, CancellationToken cancellationToken)
{
var result = await this.http.PostAsJsonAsync("/tokenizer/count", new {
text,
tokenizer_path = tokenizerPath,
}, this.jsonRustSerializerOptions, cancellationToken);
var result = await this.http.PostAsJsonAsync("/tokenizer/count", new CountTokensRequest(text, tokenizerPath), JSON_OPTIONS, cancellationToken);
if (!result.IsSuccessStatusCode)
{
@@ -82,6 +72,6 @@ public sealed partial class RustService : ITokenizerStorage
return CreateUnavailableTokenizerResponse("Error while getting token count from Rust service: "+result.StatusCode);
}
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(this.jsonRustSerializerOptions, cancellationToken);
return await result.Content.ReadFromJsonAsync<TokenizerResponse>(JSON_OPTIONS, cancellationToken);
}
}
@@ -9,7 +9,7 @@ public sealed partial class RustService
try
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(45));
var response = await this.http.GetFromJsonAsync<UpdateResponse>("/updates/check", this.jsonRustSerializerOptions, cts.Token);
var response = await this.http.GetFromJsonAsync<UpdateResponse>("/updates/check", JSON_OPTIONS, cts.Token);
if (response == default)
{
@@ -34,15 +34,41 @@ public sealed partial class RustService : BackgroundService
private readonly SemaphoreSlim userLanguageLock = new(1, 1);
private readonly SemaphoreSlim userNameLock = new(1, 1);
private readonly JsonSerializerOptions jsonRustSerializerOptions = new()
/// <summary>
/// The JSON options of nearly every call to the runtime, which names its fields in snake case.
/// </summary>
internal static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
Converters =
{
new RustEnumConverter(),
},
};
TypeInfoResolver = RustJsonContext.Default,
}.AsReadOnly();
/// <summary>
/// The JSON options of the events of a file extraction, which name their fields themselves.
/// </summary>
/// <remarks>
/// The same as the defaults of System.Text.Json, with which these events were read before.
/// </remarks>
internal static readonly JsonSerializerOptions CONTENT_STREAM_JSON_OPTIONS = new JsonSerializerOptions
{
TypeInfoResolver = RustContentStreamJsonContext.Default,
}.AsReadOnly();
/// <summary>
/// The JSON options of the prompt injection filter, which names its fields in camel case.
/// </summary>
/// <remarks>
/// The same as the defaults of the JSON calls of HttpClient, with which the filter was called before.
/// </remarks>
internal static readonly JsonSerializerOptions PROMPT_INJECTION_JSON_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
TypeInfoResolver = RustPromptInjectionJsonContext.Default,
}.AsReadOnly();
private ILogger<RustService>? logger;
private Encryption? encryptor;
private string? cachedUserLanguage;