@hollis-labs/kit-voice
v0.1.0
Published
Voice kit — speech input, microphone and voice selection, transcript and audio playback. Props down, messages up: no transport, no STT/TTS, no opinion about where audio goes.
Maintainers
Readme
@hollis-labs/kit-voice
Voice input and output components on Base UI and the token contract. Private
0.0.0, unpublished.
Props down, messages up. No transport, no speech-to-text or text-to-speech service, no
store, no fetching: a host hands the components audio and text and receives events
and Blobs back.
Components
Vendored as source from AI Elements
(Vercel, Apache-2.0), pinned at 6a9d5b1, with a provenance header on each file that
states what diverged (and docs/upstream-versions.md). Each lands in its own change.
| Component | Needs | Status |
|---|---|---|
| SpeechInput | browser APIs only | available |
| MicSelector (+ MicSelector* parts) and useAudioDevices | browser APIs only | available |
| VoiceSelector (+ its parts), Transcription | browser APIs only | available |
| AudioPlayer (+ its controls) | media-chrome (optional peer), behind @hollis-labs/kit-voice/audio-player | available. Imports ButtonGroup: needs the next design-components release (unreleased) |
Not taken: AI Elements' Persona. It needs Rive and .riv files hotlinked from
Vercel's storage, and no licence for those assets is granted by the repository.
Compatibility. kit-voice needs the next design-components release (unreleased). It
imports useControllableState (used by MicSelector, VoiceSelector and Transcription), which the workspace design-components exports but the
published 0.3.0 does not, and AudioPlayer imports ButtonGroup and ButtonGroupText,
also unreleased. Workspace linking hides this; a consumer on registry 0.3.0 would fail to
resolve it. The ^0.3.0 range in package.json is the sibling convention and is raised
together with that release by whoever cuts it; nothing here publishes.
Secure context: the microphone needs HTTPS or localhost
Browsers withhold the microphone, and so navigator.mediaDevices, from any page that is
not a secure context: https://, http://localhost or http://127.0.0.1. A page
served over plain http:// on a LAN address or hostname gets neither Web Speech nor
getUserMedia, and nothing in the page can change that.
What the components do about it, instead of failing silently:
SpeechInputis disabled, withdata-unavailable="insecure-context"on the button so a host can show a reason. It readswindow.isSecureContext.useAudioDevicesreturnserrorexplaining it, adds nodevicechangelistener, and does not throw.MicSelectorthen lists nothing.
SpeechInput
A round mic button that toggles listening. Two modes, chosen once from what the browser has:
| Mode | When | What happens |
|---|---|---|
| Web Speech | SpeechRecognition / webkitSpeechRecognition exists (Chromium, Safari) | The browser recognises speech and onTranscriptionChange receives each final chunk |
| MediaRecorder fallback | No Web Speech, but MediaRecorder and getUserMedia | Records until clicked again, then hands the host a Blob through onAudioRecorded; the text it resolves goes to onTranscriptionChange |
import { SpeechInput } from '@hollis-labs/kit-voice'
<SpeechInput
lang="en-US"
onTranscriptionChange={(text) => setDraft((draft) => draft + text)} // append: each call is one chunk
onAudioRecorded={async (blob) => (await transcribe(blob)).text} // the host's own STT
onError={({ code, message }) => toast(message)}
/>onAudioRecordedis the host's transcription step and is required for the fallback. Without it, in a browser that has no Web Speech, the button is disabled withdata-unavailable="media-recorder-needs-onAudioRecorded". kit-voice never sends audio anywhere. TheBlob'stypeis the recorder's ownmimeType, which differs by browser (audio/webm;codecs=opusin Chromium and Firefox,audio/mp4in Safari); send it with that type rather than assuming webm.- Errors reach
onErroras{ code, message, cause }. Codes:permission-denied,no-audio-input,recognition-error,recording-error,transcription-failed. Web Speech's ownabortedis not an error. A rejectedonAudioRecordedistranscription-failedand the button is usable again. - Why it is disabled is
data-unavailable:insecure-context,unsupported(neither Web Speech nor MediaRecorder) ormedia-recorder-needs-onAudioRecorded.data-availabilityalways names the mode in use or the reason. - Accessibility: the button is named
Voice input(override witharia-label, e.g. to localise) and is a toggle,aria-pressedwhile listening,aria-busywhile the host transcribes. onClickruns first; callevent.preventDefault()to cancel the toggle.- Leaving the page or unmounting releases the microphone. A recording still running at unmount is discarded, not transcribed.
langis read each time recognition starts; changing it mid-session applies to the next one.
MicSelector and useAudioDevices
A Popover + Command combobox over the user's microphones. The parts are upstream's:
<MicSelector value={deviceId} onValueChange={setDeviceId}>
<MicSelectorTrigger><MicSelectorValue /></MicSelectorTrigger>
<MicSelectorContent>
<MicSelectorInput />
<MicSelectorList>
{(devices) =>
devices.length === 0 ? <MicSelectorEmpty /> : devices.map((device) => (
<MicSelectorItem key={device.deviceId} value={device.deviceId} keywords={[device.label]}>
<MicSelectorLabel device={device} />
</MicSelectorItem>
))
}
</MicSelectorList>
</MicSelectorContent>
</MicSelector>- Pass
keywords={[device.label]}. The item'svalueis the device id, and the search matchesvalueandkeywords, not the visible text; without it, typing a device name finds nothing. - Device labels are empty until the user grants microphone permission. The selector asks once each time it opens; closing and reopening is the retry. (Upstream asked again on every state change, so a denied permission became an endless prompt loop.)
value/openare controlled when passed and uncontrolled otherwise (defaultValue,defaultOpen).undefinedcounts as "not passed", so to control an empty selection pass'': it matches no device and the trigger shows "Select microphone...".useAudioDevices()is exported for hosts that draw their own picker. It returns{ devices, error, hasPermission, loadDevices, loading };loadDevicestriggers the permission prompt and is safe to call repeatedly (one request in flight at a time).- Picking a microphone does not bind it to
SpeechInput: Web Speech cannot be pointed at a device, and the fallback uses the browser's default. The id is yours to use in your owngetUserMediacall.
VoiceSelector
A dialog of voices: VoiceSelector (root, owns value and open), VoiceSelectorTrigger,
VoiceSelectorContent, then Input, List, Empty, Group, Item, Separator, Shortcut
and the row parts Name, Gender, Accent, Age, Description, Attributes, Bullet and
Preview. VoiceSelectorDialog is the same thing as one CommandDialog if you manage the
trigger yourself.
function Rows() {
const { setOpen, setValue } = useVoiceSelector() // inside <VoiceSelector>
return voices.map((voice) => (
<VoiceSelectorItem key={voice.id} value={voice.id} keywords={[voice.name]}
onSelect={() => { setValue(voice.id); setOpen(false) }}>
<VoiceSelectorName>{voice.name}</VoiceSelectorName>
<VoiceSelectorAttributes>
<VoiceSelectorGender value={voice.gender} /><VoiceSelectorBullet />
<VoiceSelectorAccent value={voice.accent} />
</VoiceSelectorAttributes>
<VoiceSelectorPreview playing={playing === voice.id} onPlay={() => preview(voice.id)} />
</VoiceSelectorItem>
))
}- An item does not choose by itself. Upstream's
VoiceSelectorItemis a plainCommandItem: wireonSelectthroughuseVoiceSelector()as above (and passkeywords, as forMicSelector: search matchesvalueandkeywords, not the visible text). onOpenChangeis(open: boolean) => void, called for every change including the context'ssetOpen, so a controlledopencloses when the host acts on it. (Base UI's own(open, eventDetails)form is not passed through.)VoiceSelectorContent'stitleis the dialog's accessible name (visually hidden).VoiceSelectorPreviewonly reportsonPlay; what a preview plays is the host's. It stops its click from reaching the row, so previewing does not choose the voice.VoiceSelectorGenderandVoiceSelectorAccentare announced by their value (role="img"andaria-label) when they show the default icon or flag; pass children to show your own.- Accents render as flag emoji, so what the user sees is up to their OS: Windows draws the two-letter region code instead of a flag, and a machine with no emoji font draws empty boxes (that is what this repo's headless-Chromium screenshots show). Pass children for an icon of your own. An accent not in the table renders nothing.
- Known limitation, from upstream: a
VoiceSelectorPreviewbutton sits inside arole="option"row, nested interactive content that the ARIA listbox pattern does not allow. It works with mouse, touch and Tab; screen-reader behaviour inside the listbox is not verified.
Transcription
A timed transcript that follows a clock and seeks on click. The host owns the clock.
<Transcription segments={segments} currentTime={playerTime} onSeek={(t) => player.seek(t)}>
{(segment, index) => <TranscriptionSegment key={index} segment={segment} index={index} />}
</Transcription>segmentsare{ text, startSecond, endSecond }: the fields upstream reads from the AI SDK's transcription result, so those segments pass straight in withoutaibeing a dependency. Blank segments are skipped.- Feed
currentTimefrom your player's time updates and the active segment (data-active,text-primary) follows it; earlier segments are muted. A segment is active from its start up to, not including, its end. onSeek(startSecond)is called on every click on a segment, including the one already active. WithoutonSeek(and without a segmentonClick) segments are plain<span>s, not tab stops. Left uncontrolled,currentTimestarts at 0 and follows the last segment clicked.
AudioPlayer
Controls over an <audio> element, built on media-chrome.
It lives behind its own subpath so a host that plays no audio never loads it:
npm install media-chrome # optional peer, ^4.17.2import {
AudioPlayer, AudioPlayerElement, AudioPlayerControlBar, AudioPlayerPlayButton,
AudioPlayerSeekBackwardButton, AudioPlayerSeekForwardButton, AudioPlayerTimeDisplay,
AudioPlayerTimeRange, AudioPlayerDurationDisplay, AudioPlayerMuteButton, AudioPlayerVolumeRange,
} from '@hollis-labs/kit-voice/audio-player'
<AudioPlayer>
<AudioPlayerElement src={url} /> {/* or blob={recording} */}
<AudioPlayerControlBar>
<AudioPlayerPlayButton />
<AudioPlayerSeekBackwardButton seekOffset={10} />
<AudioPlayerSeekForwardButton seekOffset={10} />
<AudioPlayerTimeDisplay />
<AudioPlayerTimeRange />
<AudioPlayerDurationDisplay />
<AudioPlayerMuteButton />
<AudioPlayerVolumeRange />
</AudioPlayerControlBar>
</AudioPlayer>media-chromeis a peer, and it must be the host's single copy. It registers custom elements in the global registry, so a second bundled copy either throws "already defined" or quietly uses the wrong class. Without it installed, importing/audio-playerfails to resolve; the main entry (@hollis-labs/kit-voice) never imports it.The audio is the host's.
AudioPlayerElementtakessrc(a URL) orblob(aBlob: a recording, a fetched speech response). Ablobis played through an object URL the element creates and revokes when theBlobchanges or it unmounts. There is no AI SDK type here and no fetching. Captions, if there are any, are<track>children ofAudioPlayerElement.Pairing with
Transcription: read the audio element'stimeupdateintocurrentTimeand seek it fromonSeek(refis forwarded; the demo'sAudioSectiondoes exactly this):const audio = useRef<HTMLAudioElement>(null) const [time, setTime] = useState(0) <AudioPlayerElement ref={audio} src={url} onTimeUpdate={(e) => setTime(e.currentTarget.currentTime)} /> <Transcription segments={segments} currentTime={time} onSeek={(t) => { if (audio.current) audio.current.currentTime = t }}>…</Transcription>The play and seek buttons carry the outline icon-button classes directly (no Base UI
Buttonwrapper, which would add a role, tabindex and key handler to elements that already have them); the time and range parts areButtonGroupText. The--media-*custom properties onAudioPlayermap to the contract's variables and can be overridden withstyle.The time range is
min-w-40by default; media-chrome's own minimum, less the group's padding, leaves a seek bar about 50px wide. PassclassName="min-w-64"for more.Not covered by the jsdom tests: media-chrome's elements are not registered under jsdom, so playback, seeking, volume and mute are checked in real Chromium (
docs/evidence/verify-browser.mjs).
Why its own package
kit-chat is a published core package versioned in lockstep with five others; voice is
a different concern with its own release cadence. The main entry carries no heavy
dependency. media-chrome is an optional peer reached only through the
/audio-player subpath, the same shape as kit-chat/markdown: it registers
custom elements in the global registry, so it must be the host's single copy, and a host
that does not play audio pays nothing.
Install
npm install @hollis-labs/kit-voice @hollis-labs/design-components @hollis-labs/design-tokens
npm install @base-ui/react react react-dom lucide-react # peers@import "tailwindcss";
@import "@hollis-labs/design-tokens/design-tokens.css";
@import "@hollis-labs/design-components/source.css";
@import "@hollis-labs/kit-voice/source.css"; /* required, or styling silently fails */Working on it
npm run build && npm run typecheck && npm run lint && npm run test:run
npm run demo -w @hollis-labs/kit-voice # fixtures; ?theme=sysop-p4-white&mode=light- Tests run in jsdom against fakes (
src/__tests__/fakes.ts) formediaDevices,MediaRecorder,SpeechRecognitionandisSecureContext. The MicSelector tests renderCommandwhere an app rendersMicSelectorContent, because an open Base UI Popover popup hangs jsdom (bisected with raw@base-ui/react/popover, no kit code). The real popover is covered in a real browser instead. docs/evidence/verify-browser.mjsdrives headless Chromium with fake microphones (--use-fake-device-for-media-stream) over the built demo: the MediaRecorder fallback, the real popover, the no-re-prompt-loop, the insecure-context path, the voice dialog, and the AudioPlayer playing a local Blob (play, pause, seek buttons, time range, volume, mute, and the paired transcription following and seeking it). Its output and light/dark screenshots for two themes are committed beside it. Not verified anywhere: Web Speech recognition itself (no speech service in headless Chromium), Safari and Firefox.
