refactor: modernize code, improve performance, and clean up various components.
This commit is contained in:
@@ -1,55 +1,39 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using Toak.Core.Interfaces;
|
||||
using Toak.Configuration;
|
||||
|
||||
namespace Toak.Core;
|
||||
|
||||
public class TranscriptionOrchestrator : ITranscriptionOrchestrator
|
||||
public class TranscriptionOrchestrator(
|
||||
ISpeechClient speechClient,
|
||||
ILlmClient llmClient,
|
||||
IConfigProvider configProvider,
|
||||
IAudioRecorder audioRecorder,
|
||||
INotifications notifications,
|
||||
ITextInjector textInjector,
|
||||
IHistoryManager historyManager,
|
||||
IClipboardManager clipboardManager,
|
||||
IRecordingStateTracker stateTracker) : ITranscriptionOrchestrator
|
||||
{
|
||||
private readonly ISpeechClient _speechClient;
|
||||
private readonly ILlmClient _llmClient;
|
||||
private readonly IConfigProvider _configProvider;
|
||||
private readonly IAudioRecorder _audioRecorder;
|
||||
private readonly INotifications _notifications;
|
||||
private readonly ITextInjector _textInjector;
|
||||
private readonly IHistoryManager _historyManager;
|
||||
private readonly IClipboardManager _clipboardManager;
|
||||
private readonly IRecordingStateTracker _stateTracker;
|
||||
private readonly ISpeechClient _speechClient = speechClient;
|
||||
private readonly ILlmClient _llmClient = llmClient;
|
||||
private readonly IConfigProvider _configProvider = configProvider;
|
||||
private readonly IAudioRecorder _audioRecorder = audioRecorder;
|
||||
private readonly INotifications _notifications = notifications;
|
||||
private readonly ITextInjector _textInjector = textInjector;
|
||||
private readonly IHistoryManager _historyManager = historyManager;
|
||||
private readonly IClipboardManager _clipboardManager = clipboardManager;
|
||||
private readonly IRecordingStateTracker _stateTracker = stateTracker;
|
||||
|
||||
public TranscriptionOrchestrator(
|
||||
ISpeechClient speechClient,
|
||||
ILlmClient llmClient,
|
||||
IConfigProvider configProvider,
|
||||
IAudioRecorder audioRecorder,
|
||||
INotifications notifications,
|
||||
ITextInjector textInjector,
|
||||
IHistoryManager historyManager,
|
||||
IClipboardManager clipboardManager,
|
||||
IRecordingStateTracker stateTracker)
|
||||
public Task ProcessStartRecordingAsync()
|
||||
{
|
||||
_speechClient = speechClient;
|
||||
_llmClient = llmClient;
|
||||
_configProvider = configProvider;
|
||||
_audioRecorder = audioRecorder;
|
||||
_notifications = notifications;
|
||||
_textInjector = textInjector;
|
||||
_historyManager = historyManager;
|
||||
_clipboardManager = clipboardManager;
|
||||
_stateTracker = stateTracker;
|
||||
}
|
||||
|
||||
public async Task ProcessStartRecordingAsync()
|
||||
{
|
||||
if (_stateTracker.IsRecording()) return;
|
||||
if (_stateTracker.IsRecording()) return Task.CompletedTask;
|
||||
|
||||
Logger.LogDebug("Received START command");
|
||||
var config = _configProvider.LoadConfig();
|
||||
_notifications.PlaySound(config.StartSoundPath);
|
||||
_audioRecorder.StartRecording();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task ProcessStopRecordingAsync(Socket client, bool pipeToStdout, bool copyToClipboard)
|
||||
@@ -96,9 +80,9 @@ public class TranscriptionOrchestrator : ITranscriptionOrchestrator
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
var detectedSkill = Skills.SkillRegistry.DetectSkill(transcript, config.ActiveSkills);
|
||||
var systemPrompt = detectedSkill != null ? detectedSkill.GetSystemPrompt(transcript) : PromptBuilder.BuildPrompt(config);
|
||||
var isExecutionSkill = detectedSkill != null && detectedSkill.HandlesExecution;
|
||||
|
||||
if (isExecutionSkill)
|
||||
{
|
||||
@@ -118,7 +102,7 @@ public class TranscriptionOrchestrator : ITranscriptionOrchestrator
|
||||
|
||||
if (pipeToStdout || copyToClipboard)
|
||||
{
|
||||
string fullText = "";
|
||||
var fullText = "";
|
||||
await foreach (var token in tokenStream)
|
||||
{
|
||||
fullText += token;
|
||||
@@ -137,7 +121,7 @@ public class TranscriptionOrchestrator : ITranscriptionOrchestrator
|
||||
}
|
||||
else
|
||||
{
|
||||
string fullText = await _textInjector.InjectStreamAsync(tokenStream, config.TypingBackend);
|
||||
var 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");
|
||||
|
||||
Reference in New Issue
Block a user