hex/docs/features/voice-action.md
Dax Raad 1577c19b52 feat(linux): portal shortcuts and hex commands replace raw Wayland input
Wayland shortcuts no longer read /dev/input. One state machine in
linux_input now serves every source:

- X11: the existing key grabs, with auto-repeat filtering split out and
  tested, and the inline gesture flags replaced by the shared rules.
- Wayland with the GlobalShortcuts portal: Register("hex"), one session for
  dictation, Paste Last, and Voice Action, and a second session that binds
  Escape (also with the held shortcut's modifiers) only while recording.
  Activated/Deactivated are press/release; refused shortcuts and the
  trigger the desktop actually granted show in Settings; a closed session
  cancels and stops the listener with an error.
- Everywhere: `hex dictation start|stop|toggle|cancel`,
  `hex voice-action start|stop|toggle`, and `hex paste-last` talk to the
  running service over its socket for desktops without the portal. They
  are idempotent, never start the service, and fail clearly when it is
  missing or not listening.

Settings records shortcuts from its own key events on Wayland too, lists
the granted triggers, or, without the portal, hides the shortcut rows and
lists the commands. Wayland paste no longer waits for held modifiers:
wtype's virtual keyboard sends its own, and the Wayland paste smoke now
pastes while another keyboard holds Ctrl and Alt.

Removes linux_wayland_input.rs, the evdev and udev crates, libudev-dev in
CI, and every input-group requirement from the docs. Also gates
AppWindowPreview::pane to macOS, whose only callers broke the Linux
Clippy build.
2026-10-22 01:13:07 -04:00

241 lines
12 KiB
Markdown

# Voice Action
Hold a separate shortcut and speak an instruction, optionally with text selected.
HEX transcribes locally, asks Voice Action's OpenCode model for paste-ready text,
and pastes at the current focus. This is not
[Voice Commands](commands.md) or ordinary [Dictation](dictation.md).
This map covers macOS; [Linux](#linux) differs only in how the instruction is
captured. Linked checks are locators, not passing runs; no tests, previews,
providers, or native actions were run for this split.
## How To Get To It
```ts
Hex > Voice Action // voice-action; off by default
├── Enable Voice Action -> Activate saved shortcut
├── Shortcut -> Option-Command by default // independent of Dictation
└── Processing -> OpenCode model / available Thinking variant
Foreground text target -> Optional selection -> Hold shortcut -> Speak -> Release
```
Requires ordinary setup permissions, a local transcription model, and OpenCode
with an available text model. Commands and its Moonshine model are not required.
`render_voice_action` in [app_window.rs](../../src/app_window.rs) keeps the toggle
reachable when OpenCode is unavailable; the pane offers setup/retry actions.
HEX discovers `opencode2` and its managed service through
[dictation_processor.rs](../../src/dictation_processor.rs), not a separate
HEX-owned provider service.
Discovery uses `opencode2 api get /api/info`; an exact CLI 404 falls back to
`/api/status`, then `/api/health`, for older V2 services. All attempts share the
original deadline and cancellation flag. Authentication, server, and
malformed-response failures do not trigger that fallback, and discovery output
is excluded from errors. `service_discovery_supports_current_and_legacy_endpoints`
and `service_discovery_fallback_shares_deadline_and_observes_cancellation` cover
these routes with executable fixtures. This shared discovery also serves Modes.
**Fixed in 2.1.21:** releases 2.1.18 through 2.1.20 only tried
`/api/status` and `/api/health`, and posted generation to `/api/generate`.
OpenCode `0.0.0-dev-19726` and later serve only `/api/info` and
`/api/experimental/generate`, so those releases report `OpenCode service
discovery failed (exit status: 1)`, and even with discovery repaired, generation
received an empty 404 body and Modes fell back to the corrected transcript
([#98](https://github.com/anomalyco/hex/issues/98)). Generation now posts to
the new route and falls back to `/api/generate` only on HTTP 404;
`generation_falls_back_to_the_legacy_route_only_when_the_new_one_is_missing`
covers both routes and the served-error case through a loopback fixture. On
September 22, 2026 the ignored `live_catalog` test discovered the running
`0.0.0-dev-19937` service through `/api/info` and loaded its catalog, the legacy
names returned exactly `HTTP 404 Not Found` from the CLI, and an explicit-model
request to `/api/experimental/generate` returned paste-ready text.
## Sub-features
### Independent Opt-In
```ts
New settings OR existing settings without enabled=true // voice-action.opt-in
-> Off -> Saved binding neither active nor reserved
Enable -> Check shortcut conflicts -> Persist opt-in
Disable -> Cancel active Voice Action capture
-> Keep accepted jobs, saved shortcut, model, and variant
```
Checks in [app_settings.rs](../../src/app_settings.rs):
`voice_action_requires_explicit_opt_in_for_new_and_existing_settings`,
`voice_action_settings_round_trip`, and
`disabled_voice_action_preserves_its_binding_without_reserving_it`.
Checks in [app_window.rs](../../src/app_window.rs):
`voice_action_opt_in_rejects_active_conflicts_without_rebinding` and
`voice_action_toggle_preserves_configuration_and_can_always_disable`.
These cover settings and conflict logic, not native shortcut registration.
Active-capture cancellation is wired in [recognition.rs](../../src/recognition.rs)
when the runtime edit binding becomes `None`; the settings tests alone do not
prove that runtime transition.
### Hold Or Promote
```ts
Hold saved Voice Action shortcut // voice-action.shortcut
├── Intentional hold -> Capture -> Release -> Submit
└── Short tap -> Discard // no double-tap locking
Default Option dictation already recording + Command joins
-> Promote same capture -> Preserve recorded audio // not a second recording
-> Voice Action owns release, not ordinary Dictation
```
Source: `handle_edit_hotkey_action`, `voice_action_owns_action`, and
`start_pending_voice_action` in [recognition.rs](../../src/recognition.rs).
Checks there: `slow_chord_release_keeps_voice_action_ownership`,
`accepted_voice_action_preserves_promoted_capture`,
`rejected_voice_action_cancels_promoted_capture`, and
`delayed_voice_action_edges_use_their_source_timestamps`.
These cover synthetic edges and capture admission, not physical onset or
sample-for-sample audio continuity through promotion.
`voice_action_uses_separate_binding_after_dictation_is_rebound` in
[suppression.rs](../../src/suppression.rs) checks independent bindings.
`DictationHotkey::new_without_paste` disables locking for this target; ordinary
Dictation's double-tap settings do not add a Voice Action lock. Cold microphone
modifier order remains a [known gap](#cold-modifier-order).
### Optional Selection
```ts
Intentional Voice Action -> Snapshot optional selected text // voice-action.selection
├── Accessibility exposes bounded nonempty text -> Include as prompt context
└── Empty / inaccessible / oversized -> No selection; generate from instruction
Selection capture -> No clipboard read or copy shortcut
Later paste -> Current focus, not a pinned original selection
```
Sources: `voice_action_context_snapshot` in
[recognition.rs](../../src/recognition.rs), `capture_optional` in
[accessibility.rs](../../src/accessibility.rs), and `voice_action_prompt` in
[dictation_processor.rs](../../src/dictation_processor.rs).
Checks in the processor: `voice_action_prompt_keeps_the_instruction_separate_from_selected_text`
and `voice_action_prompt_supports_generation_without_a_selection`.
These construct prompt strings; they do not prove Accessibility support in an
arbitrary application. Selection is limited to 64 KiB; full window text is not
substituted when selection is absent.
### Local Transcript To Model
```ts
Accepted recording -> Selected local transcription model // voice-action.processing
-> Instruction + optional selection + app / browser host
-> Voice Action OpenCode model and deadline
├── Nonempty reply -> Paste-ready output
└── Failure / timeout / empty -> Paste nothing // no raw-transcript fallback
Modes -> Bypassed // no corrections/rewrite/transformations
```
Sources: the `TranscriptionTarget::VoiceAction` processing branch in
[parakeet.rs](../../src/parakeet.rs), `process_voice_action_cancellable` and
`voice_action_failure` in [dictation_processor.rs](../../src/dictation_processor.rs).
Voice Action uses its own model/variant and persisted deadline (60 seconds by
default); when no explicit model is selected, HEX ranks available fast models
from `RECOMMENDED_OPENCODE_MODELS` (starting with `gemini-3.1-flash-lite` on
`minimal` thinking, `claude-haiku-4-5`, and `gpt-5.4-nano` on `none`) and walks
those candidates in order before falling back to OpenCode's default.
`model_catalog_ranks_recommended_models_and_selects_top_available_default_with_variant`
and `ranked_generation_walks_candidates_until_one_succeeds` cover catalog
ranking, preferred variant selection, and candidate fallback.
Audio transcription stays local, but the prompt text goes to the configured
OpenCode provider.
`voice_action_processing_never_loads_modes_and_announces_stage_before_generation`
in [parakeet.rs](../../src/parakeet.rs) checks that this route never acquires
ordinary Modes profiles and announces Processing before invoking Voice Action.
It uses an injected processor, not a provider or native capture pipeline.
`generation_http_wait_observes_cancellation_and_deadline` and
`cancelled_generation_process_is_killed_promptly` in the processor cover local
HTTP/process fixtures, not a live provider or the full Voice Action pipeline.
Mode bypass and no-fallback routing are source-backed here, not a claimed
end-to-end pass. See [Recovery](recovery.md#degrade-only-the-failing-dependency)
for the distinction from ordinary Dictation's best-effort processing fallback.
### Output And Cancellation
```ts
Accepted jobs -> Shared bounded queue -> Submission-order output // voice-action.output
-> Standalone paste at current focus
├── Successful output -> History when retention permits
└── Last Dictation -> Unchanged // Paste Last never recalls this reply
Escape // voice-action.cancel
├── Active capture -> Cancel that capture first
└── Otherwise -> Cancel newest cancellable accepted job
Before output commit -> No paste; after commit -> No undo
```
Sources: [recognition.rs](../../src/recognition.rs), `finish_output` in
[parakeet.rs](../../src/parakeet.rs), and [paste.rs](../../src/paste.rs).
Checks in `parakeet.rs`: `parallel_processing_results_are_released_in_submission_order`,
`repeated_cancellation_walks_back_through_pending_jobs`, and
`output_stays_cancellable_through_preparation_but_not_after_mutation`.
The last explicitly includes Voice Action, its standalone paste mode, History,
and unchanged last-dictation text. These use controlled output/paste boundaries
and temporary History storage, not a foreground application's clipboard
consumption. See [Dictation](dictation.md#cancellation-before-commit) and
[Recovery](recovery.md#cancel-without-losing-other-work) for the shared boundary.
## Gaps And Constraints
### Cold Modifier Order
```ts
Release when idle + default shortcut // known #23, not fixed here
├── Command-first -> Pending Voice Action -> Intentional threshold -> Open mic
└── Option-first -> Already-started Dictation capture -> Promote
Ordinary cold-Dictation checks -> Both Voice Action orders // NOT established
```
Source: `start_pending_voice_action` and intentional-hold handling in
[recognition.rs](../../src/recognition.rs).
[#23](https://github.com/anomalyco/hex/issues/23) still needs both modifier orders
with a controlled opener, then native audio-onset evidence. See
[Recovery](recovery.md#cold-voice-action); a successful HUD fixture does not
establish equivalent audio onset.
- **Preview limits:** [capture-preview.sh](../../scripts/capture-preview.sh)
provides `voice-action`, `--voice-action-enabled`, and `--opencode-unavailable`
fixtures. They render states, not selection-to-provider-to-paste execution.
- **Focus and provider limits:** the prompt asks for paste-ready text; tests do
not guarantee a provider follows it. Moving focus while work is pending changes
the eventual paste target. A failed action does not paste its local transcript.
- **Native proof:** permissions, shortcut ordering, microphone mode, selection
support, provider response, and target paste still need an explicit controlled
end-to-end check. This map does not authorize operating the installed app.
## Linux
```ts
Settings > Voice Action > Enable // voice-action.linux; off by default
├── X11 -> Hold the saved shortcut (Ctrl+Alt+I by default) -> Release -> Run
├── Wayland + GlobalShortcuts portal -> Hold the granted trigger -> Release -> Run
├── Wayland without the portal -> Desktop keybinding runs
│ `hex voice-action start` on press, `hex voice-action stop` on release,
│ or `hex voice-action toggle`
├── Escape or `hex dictation cancel` -> Cancel the instruction
└── Selected text -> Primary selection (X11 clipboard, `wl-paste --primary`)
```
The shortcut and commands feed the same state machine as dictation in
[linux_input.rs](../../src/linux_input.rs), so Voice Action and dictation never
record at once, and a command while Voice Action is off fails with an error
from the service (`commands_reach_only_a_started_listener` in
[linux_app.rs](../../src/linux_app.rs)). `run_voice_action` in
[linux_dictation.rs](../../src/linux_dictation.rs) sends the instruction and
selection through the same OpenCode Voice Action processing and pastes the
reply. `voice_action_records_while_its_shortcut_is_held` and
`voice_action_commands_are_idempotent` cover the gestures; no OpenCode request
or paste has been observed on a Linux desktop.