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,4 +1,3 @@
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Configuration;
@@ -6,9 +5,9 @@ namespace Toak.Commands;
public static class ConfigUpdaterCommand
{
public static async Task ExecuteAsync(string key, string val, bool verbose)
public static Task ExecuteAsync(string key, string val, bool verbose)
{
Toak.Core.Logger.Verbose = verbose;
Core.Logger.Verbose = verbose;
var configManager = new ConfigManager();
var config = configManager.LoadConfig();
key = key.ToLowerInvariant();
@@ -23,18 +22,19 @@ public static class ConfigUpdaterCommand
case "backend": config.TypingBackend = val; break;
case "punctuation":
if (bool.TryParse(val, out var p)) { config.ModulePunctuation = p; }
else { AnsiConsole.MarkupLine("[red]Invalid value. Use true or false.[/]"); return; }
else { AnsiConsole.MarkupLine("[red]Invalid value. Use true or false.[/]"); return Task.CompletedTask; }
break;
case "tech":
if (bool.TryParse(val, out var t)) { config.ModuleTechnicalSanitization = t; }
else { AnsiConsole.MarkupLine("[red]Invalid value. Use true or false.[/]"); return; }
else { AnsiConsole.MarkupLine("[red]Invalid value. Use true or false.[/]"); return Task.CompletedTask; }
break;
default:
AnsiConsole.MarkupLine($"[red]Unknown config key: {key}[/]");
return;
return Task.CompletedTask;
}
configManager.SaveConfig(config);
AnsiConsole.MarkupLine($"[green]Successfully[/] set {key} to [blue]{val}[/].");
return Task.CompletedTask;
}
}

View File

@@ -1,6 +1,4 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core;

View File

@@ -1,9 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.CommandLine;
using Spectre.Console;
using Toak.Core;
@@ -54,15 +48,15 @@ public static class HistoryCommand
{
try
{
using var writer = new StreamWriter(export);
writer.WriteLine($"# Toak Transcriptions - {DateTime.Now:yyyy-MM-dd}");
writer.WriteLine();
await using var writer = new StreamWriter(export);
await writer.WriteLineAsync($"# Toak Transcriptions - {DateTime.Now:yyyy-MM-dd}");
await writer.WriteLineAsync();
foreach (var entry in entries)
{
writer.WriteLine($"## {entry.Timestamp.ToLocalTime():HH:mm:ss}");
writer.WriteLine(entry.RefinedText);
writer.WriteLine();
await writer.WriteLineAsync($"## {entry.Timestamp.ToLocalTime():HH:mm:ss}");
await writer.WriteLineAsync(entry.RefinedText);
await writer.WriteLineAsync();
}
AnsiConsole.MarkupLine($"[green]Successfully exported {entries.Count} entries to {export}[/]");

View File

@@ -1,7 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Api;
using Toak.Configuration;
@@ -34,7 +31,7 @@ public static class LatencyTestCommand
RedirectStandardOutput = true
};
var proc = Process.Start(pInfo);
proc?.WaitForExit();
if (proc != null) await proc.WaitForExitAsync();
if (!File.Exists(testWavPath))
{
@@ -51,13 +48,13 @@ public static class LatencyTestCommand
{
ctx.Status("Testing STT (Whisper)...");
var sttWatch = Stopwatch.StartNew();
var transcript = await client.TranscribeAsync(testWavPath, config.WhisperLanguage, config.WhisperModel);
await client.TranscribeAsync(testWavPath, config.WhisperLanguage, config.WhisperModel);
sttWatch.Stop();
ctx.Status("Testing LLM (Llama)...");
var systemPrompt = PromptBuilder.BuildPrompt(config);
var llmWatch = Stopwatch.StartNew();
var refinedText = await client.RefineTextAsync("Hello world, this is a latency test.", systemPrompt, config.LlmModel);
await client.RefineTextAsync("Hello world, this is a latency test.", systemPrompt, config.LlmModel);
llmWatch.Stop();
var total = sttWatch.ElapsedMilliseconds + llmWatch.ElapsedMilliseconds;
@@ -73,14 +70,9 @@ public static class LatencyTestCommand
AnsiConsole.Write(table);
if (total < 1500)
{
AnsiConsole.MarkupLine($"[green]Status: OK (under 1.5s target). Total time: {(total / 1000.0):0.0}s.[/]");
}
else
{
AnsiConsole.MarkupLine($"[yellow]Status: SLOW (over 1.5s target). Total time: {(total / 1000.0):0.0}s.[/]");
}
AnsiConsole.MarkupLine(total < 1500
? $"[green]Status: OK (under 1.5s target). Total time: {(total / 1000.0):0.0}s.[/]"
: $"[yellow]Status: SLOW (over 1.5s target). Total time: {(total / 1000.0):0.0}s.[/]");
});
}
catch (Exception ex)

View File

@@ -1,7 +1,4 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Configuration;
using Toak.Core.Skills;
@@ -12,7 +9,7 @@ public static class OnboardCommand
{
public static async Task ExecuteAsync(bool verbose)
{
Toak.Core.Logger.Verbose = verbose;
Core.Logger.Verbose = verbose;
var configManager = new ConfigManager();
var config = configManager.LoadConfig();
@@ -121,7 +118,7 @@ public static class OnboardCommand
}
catch (Exception ex)
{
Toak.Core.Logger.LogDebug($"Failed to restart toak service: {ex.Message}");
Core.Logger.LogDebug($"Failed to restart toak service: {ex.Message}");
}
}
}

View File

@@ -1,4 +1,3 @@
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Configuration;
@@ -8,7 +7,7 @@ public static class ShowCommand
{
public static async Task ExecuteAsync(bool verbose)
{
Toak.Core.Logger.Verbose = verbose;
Core.Logger.Verbose = verbose;
var config = new ConfigManager().LoadConfig();
var table = new Table();

View File

@@ -1,8 +1,5 @@
using System;
using System.CommandLine;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core.Skills;
using Toak.Serialization;
@@ -100,8 +97,8 @@ public static class SkillCommand
};
SkillRegistry.Initialize(); // ensure dir exists
string filename = Path.Combine(SkillRegistry.SkillsDirectory, $"{name.ToLowerInvariant()}.json");
string json = JsonSerializer.Serialize(def, AppJsonSerializerContext.Default.SkillDefinition);
var filename = Path.Combine(SkillRegistry.SkillsDirectory, $"{name.ToLowerInvariant()}.json");
var json = JsonSerializer.Serialize(def, AppJsonSerializerContext.Default.SkillDefinition);
File.WriteAllText(filename, json);
AnsiConsole.MarkupLine($"[bold green]Success![/] Skill '{name}' saved to {filename}");

View File

@@ -1,6 +1,4 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core;

View File

@@ -1,7 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.CommandLine;
using Spectre.Console;
using Toak.Core;
@@ -9,7 +5,7 @@ namespace Toak.Commands;
public static class StatsCommand
{
public static async Task ExecuteAsync(bool verbose)
public static Task ExecuteAsync(bool verbose)
{
Logger.Verbose = verbose;
@@ -17,7 +13,7 @@ public static class StatsCommand
if (entries.Count == 0)
{
AnsiConsole.MarkupLine("[yellow]No history found. Cannot generate statistics.[/]");
return;
return Task.CompletedTask;
}
var totalCount = entries.Count;
@@ -30,7 +26,7 @@ public static class StatsCommand
.FirstOrDefault();
var topWords = entries
.SelectMany(e => e.RefinedText.Split(new[] { ' ', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries))
.SelectMany(e => e.RefinedText.Split([' ', '.', ',', '!', '?'], StringSplitOptions.RemoveEmptyEntries))
.Where(w => w.Length > 3) // Exclude short common words
.GroupBy(w => w.ToLowerInvariant())
.OrderByDescending(g => g.Count())
@@ -52,5 +48,6 @@ public static class StatsCommand
{
AnsiConsole.MarkupLine($"[dim]Top spoken words (>3 chars):[/] {string.Join(", ", topWords)}");
}
return Task.CompletedTask;
}
}

View File

@@ -1,6 +1,4 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core;
@@ -24,7 +22,7 @@ public static class StatusCommand
await socket.SendAsync(msg, SocketFlags.None);
var responseBuffer = new byte[4096];
int received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
var received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
if (received > 0)
{
var text = System.Text.Encoding.UTF8.GetString(responseBuffer, 0, received);
@@ -33,10 +31,7 @@ public static class StatusCommand
}
catch (SocketException)
{
if (json)
Console.WriteLine("{\"state\": \"Offline\"}");
else
Console.WriteLine("Offline");
Console.WriteLine(json ? "{\"state\": \"Offline\"}" : "Offline");
}
catch (Exception ex)
{

View File

@@ -1,6 +1,4 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core;
@@ -25,7 +23,7 @@ public static class StopCommand
var responseBuffer = new byte[4096];
while (true)
{
int received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
var received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
if (received == 0) break;
if (pipeToStdout)
{

View File

@@ -1,6 +1,4 @@
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Core;
@@ -34,7 +32,7 @@ public static class ToggleCommand
var responseBuffer = new byte[4096];
while (true)
{
int received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
var received = await socket.ReceiveAsync(responseBuffer, SocketFlags.None);
if (received == 0) break; // socket closed by daemon
if (pipeToStdout)