Service Layer Design
Relevant source files
The following files were used as context for generating this wiki page:
- VoiceInk/Services/AIEnhancementService.swift
- VoiceInk/Services/AIService.swift
- VoiceInk/Services/AudioFileTranscriptionManager.swift
- VoiceInk/Services/ImportExportService.swift
- VoiceInk/Services/ParakeetTranscriptionService.swift
- VoiceInk/Services/PowerMode/ActiveWindowService.swift
- VoiceInk/Services/PowerMode/PowerModeSessionManager.swift
- VoiceInk/Services/SystemInfoService.swift
- VoiceInk/Services/VoiceInkCSVExportService.swift
- VoiceInk/VoiceInk.swift
The service layer in VoiceInk is a collection of classes responsible for encapsulating the application's core business logic, data processing, and interactions with external systems. These services are designed to be modular and decoupled from the UI, promoting a clean architecture. They handle tasks such as audio transcription, AI-powered text enhancement, state management for features like Power Mode, and various utility functions.
Services are typically instantiated at the application's entry point (VoiceInkApp) and injected into the SwiftUI view hierarchy as StateObject or EnvironmentObject. This ensures a single source of truth for different parts of the application and facilitates communication between components. Many services are implemented as singletons to provide global access to their functionalities.
Core Services
The application's service layer is composed of several key services, each with a distinct responsibility.
| Service | Primary Responsibility | Source File |
|---|---|---|
AIEnhancementService | Enhances transcribed text using AI, manages prompts, and gathers context. | AIEnhancementService.swift |
AIService | Manages AI provider configurations, API keys, and available models. | AIService.swift |
AudioTranscriptionManager | Orchestrates the transcription of audio files, selecting the appropriate service. | AudioFileTranscriptionManager.swift |
WhisperState | Manages the state of the transcription engine, including model loading and recording. | VoiceInk.swift |
PowerModeSessionManager | Manages application state transitions when a Power Mode is activated or deactivated. | PowerModeSessionManager.swift |
ActiveWindowService | Monitors the active application to trigger context-aware Power Modes. | ActiveWindowService.swift |
SystemInfoService | Gathers and formats system, hardware, and application data for debugging. | SystemInfoService.swift |
ImportExportService | Handles the import and export of application settings. | ImportExportService.swift |
Sources: VoiceInk.swift:20-108, AIEnhancementService.swift, AudioFileTranscriptionManager.swift
Service Initialization and Dependency Injection
Services are initialized in the VoiceInkApp struct's init() method. Key services like AIService and AIEnhancementService are created and then injected into other dependent services and the view hierarchy. This pattern ensures that complex dependencies are resolved at the root of the application.
The diagram below illustrates the initialization and dependency flow between the main app, WhisperState, and other major services.
This diagram shows the dependency graph of core services during application startup.
Sources: VoiceInk.swift:40-108, PowerModeSessionManager.swift:20-24
Transcription Services
Transcription is a core feature, supported by a multi-faceted service architecture that handles both local and cloud-based processing.
AudioFileTranscriptionManager
This singleton class acts as the central orchestrator for transcribing pre-existing audio files. It manages the entire lifecycle from audio processing to final enhancement and database persistence.
The transcription flow is as follows:
- The manager receives a URL for an audio file.
- It uses
AudioProcessorto convert the file into audio samples. - A permanent copy of the audio file is saved in the app's support directory.
- Based on the currently selected
TranscriptionModel's provider (local,parakeet,nativeApple, or cloud), it delegates the transcription task to the appropriate service. - The resulting text is filtered and formatted.
- If AI enhancement is enabled, it calls
AIEnhancementService. - A
Transcriptionobject is created with all relevant metadata and saved to the SwiftData model context.
This flowchart details the process managed by AudioFileTranscriptionManager for transcribing an audio file.
Sources: AudioFileTranscriptionManager.swift:60-192
Transcription Service Implementations
VoiceInk uses several concrete transcription service implementations.
ParakeetTranscriptionService: Implements theTranscriptionServiceprotocol to handle models from the Parakeet family. It uses theFluidAudioframework, managingAsrManagerandVadManagerfor voice activity detection and transcription.
Sources:ParakeetTranscriptionService.swift:10-85- Other Services: The
AudioFileTranscriptionManagerreferencesLocalTranscriptionService,CloudTranscriptionService, andNativeAppleTranscriptionService, indicating a strategy pattern where the specific transcription implementation is chosen at runtime based on the selected model.
Sources:AudioFileTranscriptionManager.swift:29-32,AudioFileTranscriptionManager.swift:103-114
AI Enhancement Service
The AIEnhancementService is responsible for post-processing transcribed text using external AI providers. It constructs a detailed prompt including contextual information and sends it to the configured AI model.
Key Features
- Context Gathering: It can augment the AI prompt with context from the user's clipboard (
useClipboardContext) or the content of the active screen (useScreenCaptureContext). It also includes a custom dictionary. - Prompt Management: Manages a list of
CustomPromptobjects, allowing users to switch between different enhancement styles (e.g., "Email", "Chat", "Rewrite"). Default prompts are loaded fromPromptTemplates. - API Interaction: It uses
AIServiceto handle the actual HTTP requests to AI providers like OpenAI, Anthropic, or a local Ollama instance. It includes logic for rate limiting and request retries.
Sources: AIEnhancementService.swift:10-390, Models/PromptTemplates.swift:5-110
AI Enhancement Flow
The sequence diagram below shows the interactions when enhancing a piece of text.
This diagram shows the sequence of events for an AI enhancement request.
Sources: AIEnhancementService.swift:299-311, AIEnhancementService.swift:150-297
Power Mode Services
Power Mode allows the application to automatically change its configuration based on the active application or website. This is managed by a combination of services that detect context and manage state transitions.
ActiveWindowService
This singleton service is responsible for identifying the currently active application and its frontmost window's URL (if applicable). It uses accessibility APIs to get this information and notifies PowerModeManager to apply the matching PowerModeConfig. It is configured with AIEnhancementService and WhisperState to interact with them when a power mode changes.
Sources: VoiceInk/Services/PowerMode/ActiveWindowService.swift
PowerModeSessionManager
This manager handles the lifecycle of a Power Mode session. When a Power Mode is triggered, it saves the application's current state and applies the new configuration. When the context changes and the Power Mode is no longer active, it restores the original state.
The ApplicationState struct defines the settings that are saved and restored.
| Property | Description |
|---|---|
isEnhancementEnabled | The global on/off state for AI enhancement. |
useScreenCaptureContext | Whether screen context is used for enhancement. |
selectedPromptId | The UUID of the active enhancement prompt. |
selectedAIProvider | The name of the AI provider (e.g., "OpenAI"). |
selectedAIModel | The specific model name for the provider. |
selectedLanguage | The language selected for transcription. |
transcriptionModelName | The name of the active transcription model. |
Sources: PowerModeSessionManager.swift:5-13
The following state diagram illustrates the session lifecycle.
This diagram shows the state transitions managed by PowerModeSessionManager.
Sources: PowerModeSessionManager.swift:26-157
Utility Services
SystemInfoService
A singleton service that aggregates a wide range of system and application information into a formatted string for debugging and support requests. This includes:
- App version and build number
- Hardware details (Model, CPU, Memory)
- Audio device settings
- Hotkey configurations
- Transcription and AI settings
- System permissions status (Accessibility, Microphone, etc.)
The getSystemInfoString() method compiles this data, and copySystemInfoToClipboard() provides an easy way for users to share it.
Sources: SystemInfoService.swift:7-53, SystemInfoService.swift:55-59
ImportExportService
This service provides functionality to back up and restore user settings to a JSON file. The VoiceInkExportedSettings struct defines the data schema for the export file.
Exported Data:
- Custom (non-predefined) enhancement prompts
- All Power Mode configurations
- Custom dictionary items and word replacements
- General settings (hotkeys, launch at login, recorder style, etc.)
- Custom cloud model definitions
API keys are explicitly excluded from the export.
Sources: ImportExportService.swift:8-30, ImportExportService.swift:60-128
VoiceInkCSVExportService
This service allows users to export their transcription history to a CSV file. It generates a CSV string with a detailed header and escapes content to ensure proper formatting.
CSV Columns:
Original Transcript, Enhanced Transcript, Enhancement Model, Prompt Name, Transcription Model, Power Mode, Enhancement Time, Transcription Time, Timestamp, Duration
Sources: VoiceInkCSVExportService.swift:7-40
Conclusion
The service layer of VoiceInk is a well-structured and modular component of the application that effectively separates concerns. By encapsulating logic for transcription, AI interaction, state management, and utilities, it keeps the UI layer clean and focused on presentation. The use of dependency injection, singletons, and clear protocols or service classes allows for maintainability and scalability. This design is crucial for managing the complexity of features like multi-provider AI enhancement and context-aware Power Modes.