Context Awareness (Screen and Text Reading)
Relevant source files
The following files were used as context for generating this wiki page:
The Context Awareness feature in VoiceInk significantly improves the accuracy of AI-enhanced transcriptions by providing the AI model with surrounding context from the user's screen. This system captures text from the active window, the user's current text selection, and the clipboard, then injects this information into the AI prompt. By understanding the context, the AI can better interpret ambiguous words, correct speech recognition errors, and format the output appropriately.
This feature is a core part of the AI Enhancement system and is configurable on a per-Power-Mode basis, allowing users to tailor its behavior for different applications and workflows. Proper functionality relies on the user granting Accessibility and Screen Recording permissions to the application.
Architecture Overview
The Context Awareness system is orchestrated by the AIEnhancementService. This service is responsible for gathering contextual data from various sources before making a request to the AI provider. The primary sources of context are the ScreenCaptureService, SelectedTextService, and the system clipboard.
The following diagram illustrates the high-level data flow for context gathering.
This diagram shows how AIEnhancementService aggregates text from multiple services to build a comprehensive context for the AI prompt.
Sources: VoiceInk/Services/AIEnhancementService.swift:142-184
Core Components
The system is composed of several key services that work together to capture and utilize on-screen context.
AIEnhancementService
This service acts as the central coordinator for the context awareness feature. It determines which contextual information to gather based on user settings and Power Mode configurations.
Key responsibilities include:
- Triggering context capture from other services.
- Assembling the final system prompt for the AI by injecting the captured context.
- Managing user settings for enabling/disabling different context sources.
The service maintains several @Published properties to manage the state of context gathering:
| Property | Type | Description | Source File |
|---|---|---|---|
useClipboardContext | Bool | If true, the content of the clipboard is used as context. | VoiceInk/Services/AIEnhancementService.swift:27-31 |
useScreenCaptureContext | Bool | If true, text from the current window is captured and used as context. | VoiceInk/Services/AIEnhancementService.swift:33-37 |
lastCapturedClipboard | String? | Stores the most recently captured clipboard content. | VoiceInk/Services/AIEnhancementService.swift:70, VoiceInk/Services/AIEnhancementService.swift:332-334 |
Sources: VoiceInk/Services/AIEnhancementService.swift
ScreenCaptureService
This service is responsible for capturing an image of the current application window and using VisionKit to perform Optical Character Recognition (OCR) to extract text.
- The
captureAndExtractText()method is the primary function that performs the screen capture and text recognition. - The result is stored in the
lastCapturedTextproperty. - This service requires Screen Recording permission from the user to function.
Sources: VoiceInk/Services/ScreenCaptureService.swift
SelectedTextService
This service retrieves the currently selected text from the frontmost application.
- It relies on the
SelectedTextKitlibrary. - The
fetchSelectedText()static method is called to get the selected text. - This service requires Accessibility permission from the user to function.
Sources: VoiceInk/Services/SelectedTextService.swift, README.md:100-101
Configuration
Users can control the Context Awareness feature through both global settings and specific Power Mode configurations.
Power Mode Configuration
Within the configuration for a "Power Mode," users can enable or disable context awareness for that specific mode using the "Context Awareness" toggle.
// VoiceInk/PowerMode/PowerModeConfigView.swift
Toggle("Context Awareness", isOn: $useScreenCapture)
.frame(maxWidth: .infinity, alignment: .leading)
Sources: VoiceInk/PowerMode/PowerModeConfigView.swift:478-479
This useScreenCapture state is saved as part of the PowerModeConfig struct. When a Power Mode is active, this setting overrides the global default.
| Property | Type | Description | Source File |
|---|---|---|---|
useScreenCapture | Bool | Determines if screen context is captured for a specific PowerModeConfig. | VoiceInk/PowerMode/PowerModeConfigView.swift:101, VoiceInk/PowerMode/PowerModeConfigView.swift:603 |
Global Settings
The AIEnhancementService also manages global settings that can serve as defaults.
| Setting | UserDefaults Key | Description |
|---|---|---|
useClipboardContext | useClipboardContext | Enables or disables clipboard context. |
useScreenCaptureContext | useScreenCaptureContext | Enables or disables screen capture context. |
Sources: VoiceInk/Services/AIEnhancementService.swift:27-37
Data Flow and Prompt Injection
The core of the feature is how the captured context is formatted and injected into the prompt sent to the AI.
Context Gathering Sequence
The following sequence diagram shows how AIEnhancementService gathers context before making an AI request.
This diagram details the step-by-step process from an enhancement request to the final response, highlighting the interactions between services.
Sources: VoiceInk/Services/AIEnhancementService.swift:142-184, VoiceInk/Services/AIEnhancementService.swift:302-313
Prompt Structure
The gathered context is embedded within a system prompt template using specific tags. This allows the AI to distinguish the transcribed text from the contextual information.
The main prompt template is defined in AIPrompts.swift.
// VoiceInk/Models/AIPrompts.swift
static let customPromptTemplate = """
<SYSTEM_INSTRUCTIONS>
Your are a TRANSCRIPTION ENHANCER...
1. Always reference <CLIPBOARD_CONTEXT> and <CURRENT_WINDOW_CONTEXT> for better accuracy...
...
</SYSTEM_INSTRUCTIONS>
"""
Sources: VoiceInk/Models/AIPrompts.swift:2-17
The getSystemMessage function in AIEnhancementService dynamically builds this prompt.
// VoiceInk/Services/AIEnhancementService.swift
private func getSystemMessage(for mode: EnhancementPrompt) async -> String {
let selectedText = await SelectedTextService.fetchSelectedText()
let selectedTextContext = if let selectedText = selectedText, !selectedText.isEmpty {
"\n\n<CURRENTLY_SELECTED_TEXT>\n\(selectedText)\n</CURRENTLY_SELECTED_TEXT>"
} else {
""
}
let clipboardContext = if useClipboardContext,
let clipboardText = lastCapturedClipboard,
!clipboardText.isEmpty {
"\n\n<CLIPBOARD_CONTEXT>\n\(clipboardText)\n</CLIPBOARD_CONTEXT>"
} else {
""
}
let screenCaptureContext = if useScreenCaptureContext,
let capturedText = screenCaptureService.lastCapturedText,
!capturedText.isEmpty {
"\n\n<CURRENT_WINDOW_CONTEXT>\n\(capturedText)\n</CURRENT_WINDOW_CONTEXT>"
} else {
""
}
// ... combines contexts ...
}
Sources: VoiceInk/Services/AIEnhancementService.swift:142-184
The following table describes the context tags used in the prompt:
| Tag | Content Source | Description |
|---|---|---|
<CURRENTLY_SELECTED_TEXT> | SelectedTextService.fetchSelectedText() | The text currently highlighted by the user in the active application. |
<CLIPBOARD_CONTEXT> | NSPasteboard.general | The current text content of the system clipboard. |
<CURRENT_WINDOW_CONTEXT> | ScreenCaptureService.lastCapturedText | Text extracted via OCR from a screenshot of the active window. |
<DICTIONARY_CONTEXT> | DictionaryContextService.getDictionaryContext() | Custom words and phrases defined by the user to aid in correcting technical terms and proper nouns. |
Sources: VoiceInk/Services/AIEnhancementService.swift:142-184, VoiceInk/Models/AIPrompts.swift:4-6
Permissions
For the Context Awareness feature to work fully, the user must grant specific permissions through macOS System Settings. The application provides guidance during setup and checks for these permissions.
| Permission | Purpose | How to Check Status | Onboarding/Setup View |
|---|---|---|---|
| Accessibility | To read the currently selected text (SelectedTextService). | AXIsProcessTrusted() | MetricsSetupView prompts for this permission. |
| Screen Recording | To capture the screen for OCR (ScreenCaptureService). | CGPreflightScreenCaptureAccess() / CGRequestScreenCaptureAccess() | MetricsSetupView prompts for this permission. |
Sources: VoiceInk/Services/SystemInfoService.swift:163-169, VoiceInk/Views/Metrics/MetricsSetupView.swift:51-64
The MetricsSetupView guides the user through enabling these permissions as part of the initial setup process.
Conclusion
The Context Awareness system is a sophisticated feature in VoiceInk that leverages on-screen information to dramatically enhance the quality of AI-powered transcription. By combining text from the active window, user selections, and the clipboard, it provides the AI with the necessary context to resolve ambiguities and produce more accurate and relevant results. Its integration with Power Modes allows for fine-grained control, making it a powerful tool adaptable to various user workflows.