From 4a8ab90173ba6e7b0a85523e7dbcc980dd78b0ba Mon Sep 17 00:00:00 2001 From: Paul Koudelka Date: Wed, 15 Apr 2026 10:17:14 +0200 Subject: [PATCH] cleanup to ensure uniform style of code --- .../Components/ChatComponent.razor.cs | 2 +- .../Settings/SettingsPanelEmbeddings.razor.cs | 2 +- .../Tools/Services/RustService.Tokenizer.cs | 44 +++++++------------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor.cs b/app/MindWork AI Studio/Components/ChatComponent.razor.cs index 46463562..29676fca 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor.cs +++ b/app/MindWork AI Studio/Components/ChatComponent.razor.cs @@ -995,7 +995,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable return; } - var tokenizerResponse = await this.RustService.EnsureTokenizer(this.Provider); + var tokenizerResponse = await this.RustService.EnsureTokenizer(this.Provider.InstanceName, this.Provider.TokenizerPath); if (tokenizerResponse is null) return; if (!tokenizerResponse.Value.Success) diff --git a/app/MindWork AI Studio/Components/Settings/SettingsPanelEmbeddings.razor.cs b/app/MindWork AI Studio/Components/Settings/SettingsPanelEmbeddings.razor.cs index 8f9ad19c..43c5eaea 100644 --- a/app/MindWork AI Studio/Components/Settings/SettingsPanelEmbeddings.razor.cs +++ b/app/MindWork AI Studio/Components/Settings/SettingsPanelEmbeddings.razor.cs @@ -157,7 +157,7 @@ public partial class SettingsPanelEmbeddings : SettingsPanelProviderBase return; var embeddingProvider = provider.CreateProvider(); - var embeddings = await embeddingProvider.EmbedTextAsync(provider.Model, this.SettingsManager, default, new List { inputText }); + var embeddings = await embeddingProvider.EmbedTextAsync(provider.Model, this.SettingsManager, CancellationToken.None, inputText); if (embeddings.Count == 0) { diff --git a/app/MindWork AI Studio/Tools/Services/RustService.Tokenizer.cs b/app/MindWork AI Studio/Tools/Services/RustService.Tokenizer.cs index 6746c898..02770311 100644 --- a/app/MindWork AI Studio/Tools/Services/RustService.Tokenizer.cs +++ b/app/MindWork AI Studio/Tools/Services/RustService.Tokenizer.cs @@ -31,7 +31,7 @@ public sealed partial class RustService public async Task StoreTokenizer(string modelId, string previousmodelId, string filePath) { - Console.WriteLine($"Storing tokenizer for model '{modelId}' with previous model '{previousmodelId}' from file '{filePath}'"); + this.logger!.LogInformation($"Storing tokenizer for model '{modelId}' with previous model '{previousmodelId}' from file '{filePath}'"); var result = await this.http.PostAsJsonAsync("/tokenizer/store", new { model_id = modelId, previous_model_id = previousmodelId, @@ -53,28 +53,26 @@ public sealed partial class RustService public async Task GetTokenCount(string text) { - try + var result = await this.http.PostAsJsonAsync("/tokenizer/count", new { + text = text, + }, this.jsonRustSerializerOptions); + + if (!result.IsSuccessStatusCode) { - var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); - var payload = new { text }; - var response = await this.http.PostAsJsonAsync("/tokenizer/count", payload, this.jsonRustSerializerOptions, cts.Token); - response.EnsureSuccessStatusCode(); - return await response.Content.ReadFromJsonAsync(this.jsonRustSerializerOptions, cancellationToken: cts.Token); - } - catch (Exception e) - { - if(this.logger is not null) - this.logger.LogError(e, "Error while getting token count from Rust service."); - else - Console.WriteLine($"Error while getting token count from Rust service: '{e}'."); - - return null; + this.logger!.LogError($"Failed to get the token count '{result.StatusCode}'"); + return new TokenizerResponse{ + Success = false, + Message = "Error while getting token count from Rust service: "+result.StatusCode, + TokenCount = 0 + }; } + + return await result.Content.ReadFromJsonAsync(this.jsonRustSerializerOptions); } public async Task SetTokenizer(string providerName, string path) { - Console.WriteLine($"Setting a new tokenizer for '{providerName}'"); + this.logger!.LogInformation($"Setting a new tokenizer for '{providerName}'"); var result = await this.http.PostAsJsonAsync("/tokenizer/set", new { file_path = path, }, this.jsonRustSerializerOptions); @@ -92,17 +90,7 @@ public sealed partial class RustService return await result.Content.ReadFromJsonAsync(this.jsonRustSerializerOptions); } - public Task EnsureTokenizer(Settings.Provider provider) - { - return this.EnsureTokenizer(provider.InstanceName, provider.TokenizerPath); - } - - public Task EnsureTokenizer(IProvider provider) - { - return this.EnsureTokenizer(provider.InstanceName, provider.TokenizerPath); - } - - private async Task EnsureTokenizer(string providerName, string path) + public async Task EnsureTokenizer(string providerName, string path) { await this.tokenizerLock.WaitAsync(); try