|
| 1 | +import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react"; |
| 2 | +import { toast } from "sonner"; |
| 3 | + |
| 4 | +type BrowserSpeechRecognition = { |
| 5 | + continuous: boolean; |
| 6 | + interimResults: boolean; |
| 7 | + lang: string; |
| 8 | + onend: (() => void) | null; |
| 9 | + onerror: ((event: SpeechRecognitionErrorEvent) => void) | null; |
| 10 | + onnomatch: (() => void) | null; |
| 11 | + onresult: ((event: SpeechRecognitionEvent) => void) | null; |
| 12 | + onstart: (() => void) | null; |
| 13 | + abort: () => void; |
| 14 | + start: () => void; |
| 15 | + stop: () => void; |
| 16 | +}; |
| 17 | + |
| 18 | +type SpeechRecognitionConstructor = new () => BrowserSpeechRecognition; |
| 19 | +type DictationPhase = "idle" | "starting" | "listening" | "stopping"; |
| 20 | + |
| 21 | +const subscribeToSpeechRecognition = () => () => {}; |
| 22 | +const getSpeechRecognitionSupport = () => Boolean(getSpeechRecognitionConstructor()); |
| 23 | +const getServerSpeechRecognitionSupport = () => false; |
| 24 | + |
| 25 | +const DICTATION_ERROR_MESSAGES: Partial<Record<SpeechRecognitionErrorCode, string>> = { |
| 26 | + "audio-capture": "No microphone was found.", |
| 27 | + "language-not-supported": "Dictation does not support your browser language.", |
| 28 | + network: "Dictation lost its connection. Try again.", |
| 29 | + "no-speech": "No speech was detected. Try again.", |
| 30 | + "not-allowed": "Allow microphone access in your browser to use dictation.", |
| 31 | + "service-not-allowed": "Dictation is unavailable in this browser.", |
| 32 | +}; |
| 33 | + |
| 34 | +function getSpeechRecognitionConstructor(): SpeechRecognitionConstructor | null { |
| 35 | + if (typeof window === "undefined") { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + const speechWindow = window as Window & { |
| 40 | + SpeechRecognition?: SpeechRecognitionConstructor; |
| 41 | + webkitSpeechRecognition?: SpeechRecognitionConstructor; |
| 42 | + }; |
| 43 | + |
| 44 | + return speechWindow.SpeechRecognition ?? speechWindow.webkitSpeechRecognition ?? null; |
| 45 | +} |
| 46 | + |
| 47 | +function appendTranscript(input: string, transcript: string) { |
| 48 | + const spokenText = transcript.trimStart(); |
| 49 | + if (!spokenText) { |
| 50 | + return input; |
| 51 | + } |
| 52 | + |
| 53 | + const separator = input && !/\s$/.test(input) ? " " : ""; |
| 54 | + return `${input}${separator}${spokenText}`; |
| 55 | +} |
| 56 | + |
| 57 | +function readTranscript(results: SpeechRecognitionResultList) { |
| 58 | + let transcript = ""; |
| 59 | + |
| 60 | + for (let index = 0; index < results.length; index += 1) { |
| 61 | + transcript += results[index]?.[0]?.transcript ?? ""; |
| 62 | + } |
| 63 | + |
| 64 | + return transcript; |
| 65 | +} |
| 66 | + |
| 67 | +export function useAiChatDictation({ |
| 68 | + input, |
| 69 | + setInput, |
| 70 | +}: { |
| 71 | + input: string; |
| 72 | + setInput: (input: string) => void; |
| 73 | +}) { |
| 74 | + const [phase, setPhase] = useState<DictationPhase>("idle"); |
| 75 | + const [isUnavailable, setIsUnavailable] = useState(false); |
| 76 | + const recognitionRef = useRef<BrowserSpeechRecognition | null>(null); |
| 77 | + const browserSupportsSpeechRecognition = useSyncExternalStore( |
| 78 | + subscribeToSpeechRecognition, |
| 79 | + getSpeechRecognitionSupport, |
| 80 | + getServerSpeechRecognitionSupport, |
| 81 | + ); |
| 82 | + const isSupported = browserSupportsSpeechRecognition && !isUnavailable; |
| 83 | + |
| 84 | + useEffect(() => { |
| 85 | + return () => { |
| 86 | + const recognition = recognitionRef.current; |
| 87 | + recognitionRef.current = null; |
| 88 | + recognition?.abort(); |
| 89 | + }; |
| 90 | + }, []); |
| 91 | + |
| 92 | + const cancel = useCallback(() => { |
| 93 | + const recognition = recognitionRef.current; |
| 94 | + recognitionRef.current = null; |
| 95 | + setPhase("idle"); |
| 96 | + recognition?.abort(); |
| 97 | + }, []); |
| 98 | + |
| 99 | + const stop = useCallback(() => { |
| 100 | + const recognition = recognitionRef.current; |
| 101 | + if (!recognition) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (phase !== "listening") { |
| 106 | + cancel(); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + setPhase("stopping"); |
| 111 | + recognition.stop(); |
| 112 | + }, [cancel, phase]); |
| 113 | + |
| 114 | + const start = useCallback(() => { |
| 115 | + const Recognition = getSpeechRecognitionConstructor(); |
| 116 | + if (!Recognition || recognitionRef.current) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const recognition = new Recognition(); |
| 121 | + const inputBeforeDictation = input; |
| 122 | + const finish = () => { |
| 123 | + if (recognitionRef.current !== recognition) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + recognitionRef.current = null; |
| 128 | + setPhase("idle"); |
| 129 | + return true; |
| 130 | + }; |
| 131 | + |
| 132 | + recognition.continuous = true; |
| 133 | + recognition.interimResults = true; |
| 134 | + recognition.lang = navigator.language; |
| 135 | + recognition.onstart = () => { |
| 136 | + if (recognitionRef.current === recognition) { |
| 137 | + setPhase("listening"); |
| 138 | + } |
| 139 | + }; |
| 140 | + recognition.onresult = (event) => { |
| 141 | + if (recognitionRef.current === recognition) { |
| 142 | + setInput(appendTranscript(inputBeforeDictation, readTranscript(event.results))); |
| 143 | + } |
| 144 | + }; |
| 145 | + recognition.onnomatch = () => { |
| 146 | + if (recognitionRef.current === recognition) { |
| 147 | + toast.error("Could not recognize that. Try again."); |
| 148 | + } |
| 149 | + }; |
| 150 | + recognition.onerror = (event) => { |
| 151 | + if (!finish()) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + if (event.error === "language-not-supported" || event.error === "service-not-allowed") { |
| 156 | + setIsUnavailable(true); |
| 157 | + } |
| 158 | + |
| 159 | + const message = DICTATION_ERROR_MESSAGES[event.error]; |
| 160 | + if (message) { |
| 161 | + toast.error(message); |
| 162 | + } |
| 163 | + }; |
| 164 | + recognition.onend = () => { |
| 165 | + finish(); |
| 166 | + }; |
| 167 | + |
| 168 | + recognitionRef.current = recognition; |
| 169 | + setPhase("starting"); |
| 170 | + |
| 171 | + try { |
| 172 | + recognition.start(); |
| 173 | + } catch { |
| 174 | + finish(); |
| 175 | + toast.error("Dictation could not start. Try again."); |
| 176 | + } |
| 177 | + }, [input, setInput]); |
| 178 | + |
| 179 | + const isActive = phase !== "idle"; |
| 180 | + |
| 181 | + return { |
| 182 | + cancel, |
| 183 | + isActive, |
| 184 | + isListening: phase === "listening", |
| 185 | + isSupported, |
| 186 | + toggle: isActive ? stop : start, |
| 187 | + }; |
| 188 | +} |
0 commit comments