1
0

feat: introduce a background daemon service for persistent operation and inter-process communication.

This commit is contained in:
2026-02-28 12:32:03 +01:00
parent 77bc1d4ee5
commit 75a6d20e0d
7 changed files with 322 additions and 150 deletions

View File

@@ -1,9 +1,8 @@
using System.IO;
using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Audio;
using Toak.Core;
using Toak.IO;
namespace Toak.Commands;
@@ -13,23 +12,34 @@ public static class DiscardCommand
{
Logger.Verbose = verbose;
if (StateTracker.IsRecording())
var socketPath = DaemonService.GetSocketPath();
try
{
AudioRecorder.StopRecording();
var wavPath = AudioRecorder.GetWavPath();
if (File.Exists(wavPath)) File.Delete(wavPath);
Notifications.Notify("Toak", "Recording discarded");
if (!pipeToStdout)
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
var endPoint = new UnixDomainSocketEndPoint(socketPath);
await socket.ConnectAsync(endPoint);
// Send ABORT (cmd == 3)
await socket.SendAsync(new byte[] { 3 }, SocketFlags.None);
if (verbose)
{
AnsiConsole.MarkupLine("[yellow]Recording discarded.[/]");
Console.WriteLine("Sent ABORT command to daemon.");
}
}
else
catch (SocketException)
{
if (!pipeToStdout)
if (!pipeToStdout)
{
AnsiConsole.MarkupLine("[grey]No active recording to discard.[/]");
AnsiConsole.MarkupLine("[red]Failed to connect to Toak daemon.[/]");
AnsiConsole.MarkupLine("Please ensure the daemon is running in the background:");
AnsiConsole.MarkupLine(" [dim]toak daemon[/]");
}
}
catch (Exception ex)
{
if (!pipeToStdout) AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message}");
}
}
}

View File

@@ -1,13 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using Spectre.Console;
using Toak.Audio;
using Toak.Configuration;
using Toak.Api;
using Toak.Core;
using Toak.IO;
namespace Toak.Commands;
@@ -17,143 +12,31 @@ public static class ToggleCommand
{
Logger.Verbose = verbose;
if (StateTracker.IsRecording())
var socketPath = DaemonService.GetSocketPath();
try
{
var config = ConfigManager.LoadConfig();
Notifications.PlaySound(config.StopSoundPath);
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
var endPoint = new UnixDomainSocketEndPoint(socketPath);
await socket.ConnectAsync(endPoint);
if (!pipeToStdout) AnsiConsole.MarkupLine("[yellow]Stopping recording and transcribing...[/]");
if (!pipeToStdout) Notifications.Notify("Toak", "Transcribing...");
// Send TOGGLE (cmd == 4)
await socket.SendAsync(new byte[] { 4 }, SocketFlags.None);
AudioRecorder.StopRecording();
Logger.LogDebug($"Loaded configuration: LLM={config.LlmModel}, Whisper={config.WhisperModel}, Typing={config.TypingBackend}");
if (string.IsNullOrWhiteSpace(config.GroqApiKey))
if (verbose)
{
Notifications.Notify("Toak Error", "Groq API Key is not configured. Run 'toak onboard'.");
AnsiConsole.MarkupLine("[red]Groq API Key is not configured.[/] Run 'toak onboard'.");
return;
}
var groq = new GroqApiClient(config.GroqApiKey);
var wavPath = AudioRecorder.GetWavPath();
if (!File.Exists(wavPath) || new FileInfo(wavPath).Length == 0)
{
if (!pipeToStdout) Notifications.Notify("Toak", "No audio recorded.");
return;
}
try
{
var stopWatch = Stopwatch.StartNew();
// 1. STT
Logger.LogDebug($"Starting STT transcription via Whisper for {wavPath}...");
string transcript = string.Empty;
if (!pipeToStdout)
{
await AnsiConsole.Status().StartAsync("Transcribing...", async ctx => {
transcript = await groq.TranscribeAsync(wavPath, config.WhisperLanguage, config.WhisperModel);
});
}
else
{
transcript = await groq.TranscribeAsync(wavPath, config.WhisperLanguage, config.WhisperModel);
}
Logger.LogDebug($"Raw transcript received: '{transcript}'");
if (string.IsNullOrWhiteSpace(transcript))
{
if (!pipeToStdout) Notifications.Notify("Toak", "No speech detected.");
return;
}
// 2. LLM Refinement
var detectedSkill = Toak.Core.Skills.SkillRegistry.DetectSkill(transcript, config.ActiveSkills);
string systemPrompt;
if (detectedSkill != null)
{
Logger.LogDebug($"Skill detected: {detectedSkill.Name}");
if (!pipeToStdout) Notifications.Notify("Toak Skill Detected", detectedSkill.Name);
systemPrompt = detectedSkill.GetSystemPrompt(transcript);
}
else
{
systemPrompt = PromptBuilder.BuildPrompt(config);
}
bool isExecutionSkill = detectedSkill != null && detectedSkill.HandlesExecution;
// 3. Output
if (isExecutionSkill || pipeToStdout || copyToClipboard)
{
Logger.LogDebug("Starting LLM text refinement (synchronous)...");
string finalText = string.Empty;
if (!pipeToStdout) {
await AnsiConsole.Status().StartAsync("Refining text...", async ctx => {
finalText = await groq.RefineTextAsync(transcript, systemPrompt, config.LlmModel);
});
} else {
finalText = await groq.RefineTextAsync(transcript, systemPrompt, config.LlmModel);
}
Logger.LogDebug($"Refined text received: '{finalText}'");
if (string.IsNullOrWhiteSpace(finalText))
{
if (!pipeToStdout) Notifications.Notify("Toak", "Dropped short or empty audio.");
return;
}
if (isExecutionSkill)
{
detectedSkill!.Execute(finalText);
stopWatch.Stop();
Notifications.Notify("Toak", $"Skill executed in {stopWatch.ElapsedMilliseconds}ms");
}
else if (pipeToStdout)
{
Console.WriteLine(finalText);
}
else
{
ClipboardManager.Copy(finalText);
stopWatch.Stop();
Notifications.Notify("Toak", $"Copied to clipboard in {stopWatch.ElapsedMilliseconds}ms");
}
}
else
{
Logger.LogDebug("Starting LLM text refinement (streaming)...");
var tokenStream = groq.RefineTextStreamAsync(transcript, systemPrompt, config.LlmModel);
Logger.LogDebug("Starting to inject text...");
await TextInjector.InjectStreamAsync(tokenStream, config.TypingBackend);
stopWatch.Stop();
Notifications.Notify("Toak", $"Done in {stopWatch.ElapsedMilliseconds}ms");
}
}
catch (Exception ex)
{
if (!pipeToStdout) Notifications.Notify("Toak Error", ex.Message);
if (!pipeToStdout) AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message}");
}
finally
{
if (File.Exists(wavPath)) File.Delete(wavPath);
Console.WriteLine("Sent TOGGLE command to daemon.");
}
}
else
catch (SocketException)
{
// Start recording
if (!pipeToStdout) AnsiConsole.MarkupLine("[green]Starting recording...[/]");
var config = ConfigManager.LoadConfig();
Notifications.PlaySound(config.StartSoundPath);
AudioRecorder.StartRecording();
AnsiConsole.MarkupLine("[red]Failed to connect to Toak daemon.[/]");
AnsiConsole.MarkupLine("Please ensure the daemon is running in the background:");
AnsiConsole.MarkupLine(" [dim]toak daemon[/]");
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message}");
}
}
}