feat: Introduce ITranscriptionOrchestrator and related interfaces, refactoring DaemonService and other components to use dependency injection.
This commit is contained in:
@@ -2,19 +2,17 @@ using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Toak.Audio;
|
||||
using Toak.Configuration;
|
||||
using Toak.Api;
|
||||
using Toak.Core.Interfaces;
|
||||
using Toak.Audio;
|
||||
using Toak.IO;
|
||||
|
||||
namespace Toak.Core;
|
||||
|
||||
public static class DaemonService
|
||||
{
|
||||
private static GroqApiClient? _groqClient;
|
||||
|
||||
public static string GetSocketPath()
|
||||
{
|
||||
var runtimeDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
|
||||
@@ -49,14 +47,29 @@ public static class DaemonService
|
||||
try { File.Delete(socketPath); } catch { }
|
||||
}
|
||||
|
||||
var config = ConfigManager.LoadConfig();
|
||||
var configManager = new ConfigManager();
|
||||
var config = configManager.LoadConfig();
|
||||
if (string.IsNullOrWhiteSpace(config.GroqApiKey))
|
||||
{
|
||||
Console.WriteLine("Groq API Key is not configured. Run 'toak onboard'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_groqClient = new GroqApiClient(config.GroqApiKey);
|
||||
var stateTracker = new StateTracker();
|
||||
var notifications = new Notifications();
|
||||
|
||||
var groqClient = new GroqApiClient(config.GroqApiKey);
|
||||
var orchestrator = new TranscriptionOrchestrator(
|
||||
groqClient,
|
||||
groqClient,
|
||||
configManager,
|
||||
new AudioRecorder(stateTracker, notifications),
|
||||
notifications,
|
||||
new TextInjector(notifications),
|
||||
new HistoryManager(),
|
||||
new ClipboardManager(notifications),
|
||||
stateTracker
|
||||
);
|
||||
|
||||
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
|
||||
var endPoint = new UnixDomainSocketEndPoint(socketPath);
|
||||
@@ -71,7 +84,7 @@ public static class DaemonService
|
||||
while (true)
|
||||
{
|
||||
var client = await socket.AcceptAsync();
|
||||
_ = Task.Run(() => HandleClientAsync(client));
|
||||
_ = Task.Run(() => HandleClientAsync(client, orchestrator, stateTracker));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -87,7 +100,7 @@ public static class DaemonService
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task HandleClientAsync(Socket client)
|
||||
private static async Task HandleClientAsync(Socket client, ITranscriptionOrchestrator orchestrator, IRecordingStateTracker stateTracker)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -101,22 +114,22 @@ public static class DaemonService
|
||||
|
||||
if (cmd == 1) // START
|
||||
{
|
||||
await ProcessStartRecordingAsync();
|
||||
await orchestrator.ProcessStartRecordingAsync();
|
||||
}
|
||||
else if (cmd == 2) // STOP
|
||||
{
|
||||
await ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
||||
await orchestrator.ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
||||
}
|
||||
else if (cmd == 3) // ABORT
|
||||
{
|
||||
ProcessAbortAsync();
|
||||
orchestrator.ProcessAbortAsync();
|
||||
}
|
||||
else if (cmd == 4) // TOGGLE
|
||||
{
|
||||
if (StateTracker.IsRecording())
|
||||
await ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
||||
if (stateTracker.IsRecording())
|
||||
await orchestrator.ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
||||
else
|
||||
await ProcessStartRecordingAsync();
|
||||
await orchestrator.ProcessStartRecordingAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,114 +142,5 @@ public static class DaemonService
|
||||
client.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ProcessStartRecordingAsync()
|
||||
{
|
||||
if (StateTracker.IsRecording()) return;
|
||||
|
||||
Logger.LogDebug("Received START command");
|
||||
var config = ConfigManager.LoadConfig();
|
||||
Notifications.PlaySound(config.StartSoundPath);
|
||||
AudioRecorder.StartRecording();
|
||||
}
|
||||
|
||||
private static async Task ProcessStopRecordingAsync(Socket client, bool pipeToStdout, bool copyToClipboard)
|
||||
{
|
||||
if (!StateTracker.IsRecording()) return;
|
||||
|
||||
Logger.LogDebug("Received STOP command");
|
||||
var config = ConfigManager.LoadConfig();
|
||||
Notifications.PlaySound(config.StopSoundPath);
|
||||
Notifications.Notify("Toak", "Transcribing...");
|
||||
|
||||
AudioRecorder.StopRecording();
|
||||
|
||||
var wavPath = AudioRecorder.GetWavPath();
|
||||
if (!File.Exists(wavPath) || new FileInfo(wavPath).Length == 0)
|
||||
{
|
||||
Notifications.Notify("Toak", "No audio recorded.");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var stopWatch = Stopwatch.StartNew();
|
||||
|
||||
Logger.LogDebug($"Starting STT via Whisper for {wavPath}...");
|
||||
var transcript = await _groqClient!.TranscribeAsync(wavPath, config.WhisperLanguage, config.WhisperModel);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(transcript))
|
||||
{
|
||||
Notifications.Notify("Toak", "No speech detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
// LLM Refinement
|
||||
var detectedSkill = Toak.Core.Skills.SkillRegistry.DetectSkill(transcript, config.ActiveSkills);
|
||||
string systemPrompt = detectedSkill != null ? detectedSkill.GetSystemPrompt(transcript) : PromptBuilder.BuildPrompt(config);
|
||||
bool isExecutionSkill = detectedSkill != null && detectedSkill.HandlesExecution;
|
||||
|
||||
if (isExecutionSkill)
|
||||
{
|
||||
var finalText = await _groqClient.RefineTextAsync(transcript, systemPrompt, config.LlmModel);
|
||||
if (!string.IsNullOrWhiteSpace(finalText))
|
||||
{
|
||||
detectedSkill!.Execute(finalText);
|
||||
stopWatch.Stop();
|
||||
HistoryManager.SaveEntry(transcript, finalText, detectedSkill.Name, stopWatch.ElapsedMilliseconds);
|
||||
Notifications.Notify("Toak", $"Skill executed in {stopWatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogDebug("Starting LLM text refinement (streaming)...");
|
||||
var tokenStream = _groqClient.RefineTextStreamAsync(transcript, systemPrompt, config.LlmModel);
|
||||
|
||||
if (pipeToStdout || copyToClipboard)
|
||||
{
|
||||
string fullText = "";
|
||||
await foreach (var token in tokenStream)
|
||||
{
|
||||
fullText += token;
|
||||
if (pipeToStdout)
|
||||
{
|
||||
await client.SendAsync(System.Text.Encoding.UTF8.GetBytes(token), SocketFlags.None);
|
||||
}
|
||||
}
|
||||
stopWatch.Stop();
|
||||
if (copyToClipboard)
|
||||
{
|
||||
ClipboardManager.Copy(fullText);
|
||||
Notifications.Notify("Toak", $"Copied to clipboard in {stopWatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
HistoryManager.SaveEntry(transcript, fullText, detectedSkill?.Name, stopWatch.ElapsedMilliseconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
string fullText = await TextInjector.InjectStreamAsync(tokenStream, config.TypingBackend);
|
||||
stopWatch.Stop();
|
||||
HistoryManager.SaveEntry(transcript, fullText, detectedSkill?.Name, stopWatch.ElapsedMilliseconds);
|
||||
Notifications.Notify("Toak", $"Done in {stopWatch.ElapsedMilliseconds}ms");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Notifications.Notify("Toak Error", ex.Message);
|
||||
Logger.LogDebug($"Error during processing: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(wavPath)) File.Delete(wavPath);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessAbortAsync()
|
||||
{
|
||||
Logger.LogDebug("Received ABORT command");
|
||||
AudioRecorder.StopRecording();
|
||||
var wavPath = AudioRecorder.GetWavPath();
|
||||
if (File.Exists(wavPath)) File.Delete(wavPath);
|
||||
Notifications.Notify("Toak", "Recording Aborted.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user