Skip to content

Component Speech Input

Reese Hodge edited this page Sep 26, 2026 · 1 revision

Component Speech Input

Speech input code exists as a browser Web Speech hook, microphone availability helpers, a hold-to-talk button, and a disclosure dialog. As of the code read on 2026-09-26, Component-Frontend-App renders only TextInput; MicButton, SpeechDisclosure, and useSpeech are not wired into App.tsx.

Files

  • packages/frontend/src/hooks/useSpeech.ts wraps the browser SpeechRecognition/webkitSpeechRecognition API.
  • packages/frontend/src/services/micDetection.ts probes microphone support and subscribes to device changes.
  • packages/frontend/src/components/MicButton.tsx renders a pointer-held mic button.
  • packages/frontend/src/components/SpeechDisclosure.tsx renders a provider disclosure dialog.
  • packages/frontend/src/components/TextInput.tsx renders the currently active text entry field.
  • packages/frontend/src/App.tsx imports and renders TextInput only.

Exported functions and props

  • interface UseSpeechReturn { isListening: boolean; transcript: string; error: string | null; start: () => void; stop: () => string; }
  • getSpeechProvider(): string
  • useSpeech(): UseSpeechReturn
  • probeMic(): Promise<boolean>
  • isMicAvailable(): boolean
  • onMicChange(callback: () => void): () => void
  • MicButton({ onStart, onStop, disabled }: MicButtonProps)
  • SpeechDisclosure({ provider, visible, onDismiss }: SpeechDisclosureProps)
  • interface TextInputProps { onSubmit: (text: string) => void; disabled?: boolean; maxLength?: number; }
  • TextInput = forwardRef<HTMLInputElement, TextInputProps>(...)

Current behavior

useSpeech.start():

  1. Locates window.SpeechRecognition or window.webkitSpeechRecognition.
  2. Sets continuous = true, interimResults = true, and lang = 'en-US'.
  3. Clears transcript and error state.
  4. Appends only final transcript fragments in onresult.
  5. Sets error from onerror and clears isListening.
  6. Clears isListening on onend.

useSpeech.stop() calls recognition.stop(), clears isListening, and returns the hook's current transcript state.

getSpeechProvider() reports Google for user agents containing Chrome, Apple for Safari user agents without Chrome, otherwise your browser.

micDetection:

  • probeMic() calls navigator.mediaDevices.getUserMedia({ audio: true }), immediately stops tracks, and returns false for unsupported or failed probes.
  • isMicAvailable() is a synchronous capability check.
  • onMicChange() attaches a devicechange listener when navigator.mediaDevices exists.

MicButton starts on pointer down, stops on pointer up or pointer leave, and renders an ON AIR indicator while held.

TextInput submits trimmed text on Enter, clears after submit, defaults maxLength to 2000, and shows Talk to Max... unless disabled.

Neighbour connections

Text input connects to Component-WebSocket-Transport through App.handleSend(), which sends a user_message with inputMethod: 'text'. The speech hook's output would need a caller to send inputMethod: 'voice'; that caller is not present in App.tsx today. Voice UI styling lives in the shared app CSS described by Component-CRT-Presentation.

Gotchas

  • Speech recognition is implemented but not composed into the visible app shell.
  • stop() returns React state captured by the current render; if final recognition results arrive very near stop time, a caller may need to account for state update timing.
  • probeMic() requests real microphone permission as part of availability probing, then stops tracks immediately.

Clone this wiki locally