Files
opencode-manager/frontend/src/hooks/useVoicePauseSetting.ts
T
Chris Scott 5d8b2c9445 feat(frontend): hands-free voice conversation mode in the composer
The mic opens a voice loop that listens, sends on end of speech, reads each completed reply and reopens the mic when the agent is idle. The phase reducer, pause options and voice-mode instruction move to the shared package. TTS gains per-message completion and pause/resume. The old tap-and-drag recorder, its strip and its CSS are removed.
2026-10-22 20:18:16 -04:00

65 lines
1.8 KiB
TypeScript

import { useSyncExternalStore } from 'react'
import {
DEFAULT_VOICE_PAUSE_MS,
VOICE_PAUSE_STORAGE_KEY,
normalizeVoicePauseMs,
parseVoicePauseMs,
} from '@opencode-manager/shared/opencode'
import { readStoredValue, writeStoredValue } from '@/lib/persistentStorage'
const subscribers = new Set<() => void>()
function readPauseMs(): number {
const raw = readStoredValue(VOICE_PAUSE_STORAGE_KEY)
if (raw === null) {
return DEFAULT_VOICE_PAUSE_MS
}
return parseVoicePauseMs(raw) ?? DEFAULT_VOICE_PAUSE_MS
}
function notifySubscribers(): void {
subscribers.forEach((subscriber) => subscriber())
}
function handleStorageChange(event: StorageEvent): void {
if (event.key === VOICE_PAUSE_STORAGE_KEY) {
notifySubscribers()
}
}
function subscribe(subscriber: () => void): () => void {
subscribers.add(subscriber)
if (subscribers.size === 1) {
window.addEventListener('storage', handleStorageChange)
}
return () => {
subscribers.delete(subscriber)
if (subscribers.size === 0) {
window.removeEventListener('storage', handleStorageChange)
}
}
}
function setPauseMs(ms: number): void {
const normalized = normalizeVoicePauseMs(ms)
if (normalized === undefined) {
return
}
writeStoredValue(VOICE_PAUSE_STORAGE_KEY, normalized)
notifySubscribers()
}
function getServerPauseMs(): number {
return DEFAULT_VOICE_PAUSE_MS
}
/**
* Reads and writes the device-local pause-before-sending duration, keeping every
* mounted consumer in the tab and other tabs in sync. Missing or unusable stored
* values fall back to {@link DEFAULT_VOICE_PAUSE_MS}.
*/
export function useVoicePauseSetting(): { pauseMs: number; setPauseMs: (ms: number) => void } {
const pauseMs = useSyncExternalStore(subscribe, readPauseMs, getServerPauseMs)
return { pauseMs, setPauseMs }
}