180 lines
6.4 KiB
C#
180 lines
6.4 KiB
C#
using System.Net.Sockets;
|
|
using Toak.Configuration;
|
|
using Toak.Api;
|
|
using Toak.Core.Interfaces;
|
|
using Toak.Audio;
|
|
using Toak.IO;
|
|
|
|
namespace Toak.Core;
|
|
|
|
public static class DaemonService
|
|
{
|
|
public static string GetSocketPath()
|
|
{
|
|
return Constants.Paths.GetSocketPath();
|
|
}
|
|
|
|
private static FileStream? _lockFile;
|
|
|
|
public static async Task StartAsync(bool verbose)
|
|
{
|
|
var lockPath = Constants.Paths.DaemonLockFile;
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(lockPath)!);
|
|
_lockFile = new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
|
}
|
|
catch (IOException)
|
|
{
|
|
Console.WriteLine("Toak daemon is already running.");
|
|
return;
|
|
}
|
|
|
|
Logger.Verbose = verbose;
|
|
var socketPath = GetSocketPath();
|
|
|
|
if (File.Exists(socketPath))
|
|
{
|
|
try { File.Delete(socketPath); } catch { }
|
|
}
|
|
|
|
var configManager = new ConfigManager();
|
|
var config = configManager.LoadConfig();
|
|
if (config.WhisperProvider == "groq" && string.IsNullOrWhiteSpace(config.GroqApiKey))
|
|
{
|
|
Console.WriteLine("Groq API Key is not configured for Whisper. Run 'toak onboard'.");
|
|
return;
|
|
}
|
|
if (config.WhisperProvider == "fireworks" && string.IsNullOrWhiteSpace(config.FireworksApiKey))
|
|
{
|
|
Console.WriteLine("Fireworks API Key is not configured for Whisper. Run 'toak onboard'.");
|
|
return;
|
|
}
|
|
|
|
var stateTracker = new StateTracker();
|
|
var notifications = new Notifications();
|
|
|
|
ISpeechClient speechClient = config.WhisperProvider == "fireworks"
|
|
? new OpenAiCompatibleClient(config.FireworksApiKey, "https://api.fireworks.ai/inference/v1/")
|
|
: new OpenAiCompatibleClient(config.GroqApiKey);
|
|
|
|
ILlmClient llmClient = config.LlmProvider switch
|
|
{
|
|
"together" => new OpenAiCompatibleClient(config.TogetherApiKey, "https://api.together.xyz/v1/", config.ReasoningEffort),
|
|
"cerebras" => new OpenAiCompatibleClient(config.CerebrasApiKey, "https://api.cerebras.ai/v1/", config.ReasoningEffort),
|
|
"fireworks" => new OpenAiCompatibleClient(config.FireworksApiKey, "https://api.fireworks.ai/inference/v1/", config.ReasoningEffort),
|
|
_ => new OpenAiCompatibleClient(config.GroqApiKey, "https://api.groq.com/openai/v1/", config.ReasoningEffort)
|
|
};
|
|
|
|
IAudioRecorder recorder = config.AudioBackend == "ffmpeg"
|
|
? new FfmpegAudioRecorder(stateTracker, notifications)
|
|
: new PipewireAudioRecorder(stateTracker, notifications);
|
|
|
|
var orchestrator = new TranscriptionOrchestrator(
|
|
speechClient,
|
|
llmClient,
|
|
configManager,
|
|
recorder,
|
|
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);
|
|
|
|
try
|
|
{
|
|
socket.Bind(endPoint);
|
|
socket.Listen(10);
|
|
Logger.LogDebug($"Daemon listening on {socketPath}");
|
|
Console.WriteLine($"Toak daemon started, listening on {socketPath}");
|
|
|
|
while (true)
|
|
{
|
|
var client = await socket.AcceptAsync();
|
|
_ = Task.Run(() => HandleClientAsync(client, orchestrator, stateTracker));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogDebug($"Daemon error: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(socketPath))
|
|
{
|
|
File.Delete(socketPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static async Task HandleClientAsync(Socket client, ITranscriptionOrchestrator orchestrator, IRecordingStateTracker stateTracker)
|
|
{
|
|
try
|
|
{
|
|
var buffer = new byte[3];
|
|
var bytesRead = await client.ReceiveAsync(buffer, SocketFlags.None);
|
|
if (bytesRead > 0)
|
|
{
|
|
var cmd = buffer[0];
|
|
var pipeToStdout = bytesRead > 1 && buffer[1] == 1;
|
|
var copyToClipboard = bytesRead > 2 && buffer[2] == 1;
|
|
|
|
if (cmd == 1) // START
|
|
{
|
|
await orchestrator.ProcessStartRecordingAsync();
|
|
}
|
|
else if (cmd == 2) // STOP
|
|
{
|
|
await orchestrator.ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
|
}
|
|
else if (cmd == 3) // ABORT
|
|
{
|
|
orchestrator.ProcessAbortAsync();
|
|
}
|
|
else if (cmd == 4) // TOGGLE
|
|
{
|
|
if (stateTracker.IsRecording())
|
|
await orchestrator.ProcessStopRecordingAsync(client, pipeToStdout, copyToClipboard);
|
|
else
|
|
await orchestrator.ProcessStartRecordingAsync();
|
|
}
|
|
else if (cmd == 5) // STATUS
|
|
{
|
|
var json = pipeToStdout; // buffer[1] == 1 is json
|
|
var isRecording = stateTracker.IsRecording();
|
|
var stateStr = isRecording ? "Recording" : "Idle";
|
|
|
|
if (json)
|
|
{
|
|
var start = stateTracker.GetRecordingStartTime();
|
|
double durationMs = 0;
|
|
if (isRecording && start.HasValue)
|
|
{
|
|
durationMs = (DateTime.UtcNow - start.Value).TotalMilliseconds;
|
|
}
|
|
var jsonStr = $"{{\"state\": \"{stateStr}\", \"duration\": {Math.Round(durationMs)}}}";
|
|
await client.SendAsync(System.Text.Encoding.UTF8.GetBytes(jsonStr), SocketFlags.None);
|
|
}
|
|
else
|
|
{
|
|
await client.SendAsync(System.Text.Encoding.UTF8.GetBytes(stateStr), SocketFlags.None);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogDebug($"HandleClient error: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
client.Close();
|
|
}
|
|
}
|
|
}
|
|
|