1
0

refactor: modernize code, improve performance, and clean up various components.

This commit is contained in:
2026-03-01 21:05:35 +01:00
parent 15f9647f8a
commit a6c7df0a71
37 changed files with 240 additions and 627 deletions

View File

@@ -1,7 +1,5 @@
using System.Net.Http.Headers;
using System.Text.Json;
using System.Text.Json.Serialization;
using Toak.Api.Models;
using Toak.Serialization;
using Toak.Core;
@@ -22,17 +20,17 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
_reasoningEffort = reasoningEffort == "none" ? null : reasoningEffort;
}
public async Task<string> TranscribeAsync(string filePath, string language = "", string model = Toak.Core.Constants.Defaults.WhisperModel)
public async Task<string> TranscribeAsync(string filePath, string language = "", string model = Constants.Defaults.WhisperModel)
{
// ... (TranscribeAsync content remains same except maybe some internal comments or contexts)
using var content = new MultipartFormDataContent();
using var fileStream = File.OpenRead(filePath);
await using var fileStream = File.OpenRead(filePath);
using var streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("audio/wav"); // or mpeg
content.Add(streamContent, "file", Path.GetFileName(filePath));
string modelToUse = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.WhisperModel : model;
var modelToUse = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.WhisperModel : model;
content.Add(new StringContent(modelToUse), "model");
@@ -57,18 +55,18 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
return result?.Text ?? string.Empty;
}
public async Task<string> RefineTextAsync(string rawTranscript, string systemPrompt, string model = Toak.Core.Constants.Defaults.LlmModel)
public async Task<string> RefineTextAsync(string rawTranscript, string systemPrompt, string model = Constants.Defaults.LlmModel)
{
var requestBody = new OpenAiRequest
{
Model = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.LlmModel : model,
Model = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.LlmModel : model,
Temperature = 0.0,
ReasoningEffort = _reasoningEffort,
Messages = new[]
{
Messages =
[
new OpenAiRequestMessage { Role = "system", Content = systemPrompt },
new OpenAiRequestMessage { Role = "user", Content = $"<transcript>{rawTranscript}</transcript>" }
}
]
};
var jsonContent = new StringContent(JsonSerializer.Serialize(requestBody, AppJsonSerializerContext.Default.OpenAiRequest), System.Text.Encoding.UTF8, "application/json");
@@ -89,19 +87,19 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
return result?.Choices?.FirstOrDefault()?.Message?.Content ?? string.Empty;
}
public async IAsyncEnumerable<string> RefineTextStreamAsync(string rawTranscript, string systemPrompt, string model = Toak.Core.Constants.Defaults.LlmModel)
public async IAsyncEnumerable<string> RefineTextStreamAsync(string rawTranscript, string systemPrompt, string model = Constants.Defaults.LlmModel)
{
var requestBody = new OpenAiRequest
{
Model = string.IsNullOrWhiteSpace(model) ? Toak.Core.Constants.Defaults.LlmModel : model,
Model = string.IsNullOrWhiteSpace(model) ? Constants.Defaults.LlmModel : model,
Temperature = 0.0,
Stream = true,
ReasoningEffort = _reasoningEffort,
Messages = new[]
{
Messages =
[
new OpenAiRequestMessage { Role = "system", Content = systemPrompt },
new OpenAiRequestMessage { Role = "user", Content = $"<transcript>{rawTranscript}</transcript>" }
}
]
};
var jsonContent = new StringContent(JsonSerializer.Serialize(requestBody, AppJsonSerializerContext.Default.OpenAiRequest), System.Text.Encoding.UTF8, "application/json");
@@ -120,7 +118,7 @@ public class OpenAiCompatibleClient : ISpeechClient, ILlmClient
throw new Exception($"OpenAi API Error: {response.StatusCode} - {error}");
}
using var stream = await response.Content.ReadAsStreamAsync();
await using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
string? line;