-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(audio): add opt-in browser dictation #3039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
berdebotond
wants to merge
1
commit into
Chainlit:main
Choose a base branch
from
berdebotond:feat/browser-dictation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
frontend/src/components/chat/MessageComposer/DictationButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { Mic, Square } from 'lucide-react'; | ||
|
|
||
| import { useTranslation } from '@/components/i18n/Translator'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { | ||
| Tooltip, | ||
| TooltipContent, | ||
| TooltipProvider, | ||
| TooltipTrigger | ||
| } from '@/components/ui/tooltip'; | ||
|
|
||
| export default function DictationButton({ | ||
| disabled, | ||
| supported, | ||
| listening, | ||
| onClick | ||
| }: { | ||
| disabled: boolean; | ||
| supported: boolean; | ||
| listening: boolean; | ||
| onClick: () => void; | ||
| }) { | ||
| const { t } = useTranslation(); | ||
| const label = t( | ||
| !supported | ||
| ? 'chat.speech.unsupported' | ||
| : listening | ||
| ? 'chat.speech.stop' | ||
| : 'chat.speech.dictate' | ||
| ); | ||
| return ( | ||
| <TooltipProvider> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <span> | ||
| <Button | ||
| type="button" | ||
| disabled={disabled || !supported} | ||
| variant="ghost" | ||
| size="icon" | ||
| aria-label={label} | ||
| aria-pressed={listening} | ||
| onClick={onClick} | ||
| > | ||
| {listening ? ( | ||
| <Square className="!size-5" /> | ||
| ) : ( | ||
| <Mic className="!size-5" /> | ||
| )} | ||
| </Button> | ||
| </span> | ||
| </TooltipTrigger> | ||
| <TooltipContent>{label}</TooltipContent> | ||
| </Tooltip> | ||
| </TooltipProvider> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
|
||
| export interface SpeechResultEvent { | ||
| resultIndex: number; | ||
| results: ArrayLike<{ | ||
| isFinal: boolean; | ||
| [index: number]: { transcript: string }; | ||
| }>; | ||
| } | ||
|
|
||
| export interface BrowserSpeechRecognition { | ||
| lang: string; | ||
| continuous: boolean; | ||
| interimResults: boolean; | ||
| onresult: ((event: SpeechResultEvent) => void) | null; | ||
| onerror: ((event: { error: string }) => void) | null; | ||
| onend: (() => void) | null; | ||
| start: () => void; | ||
| stop: () => void; | ||
| abort: () => void; | ||
| } | ||
|
|
||
| type SpeechWindow = Window & { | ||
| SpeechRecognition?: new () => BrowserSpeechRecognition; | ||
| webkitSpeechRecognition?: new () => BrowserSpeechRecognition; | ||
| }; | ||
|
|
||
| export function useBrowserSpeechRecognition({ | ||
| enabled, | ||
| language, | ||
| onTranscript, | ||
| onError | ||
| }: { | ||
| enabled: boolean; | ||
| language: string; | ||
| onTranscript: (text: string) => void; | ||
| onError: (error: string) => void; | ||
| }) { | ||
| const [listening, setListening] = useState(false); | ||
| const recognitionRef = useRef<BrowserSpeechRecognition | null>(null); | ||
| const callbacks = useRef({ onTranscript, onError }); | ||
| callbacks.current = { onTranscript, onError }; | ||
| const speechWindow = window as SpeechWindow; | ||
| const Recognition = | ||
| speechWindow.SpeechRecognition || speechWindow.webkitSpeechRecognition; | ||
|
|
||
| const dispose = useCallback(() => { | ||
| const recognition = recognitionRef.current; | ||
| recognitionRef.current = null; | ||
| if (recognition) { | ||
| recognition.onresult = null; | ||
| recognition.onerror = null; | ||
| recognition.onend = null; | ||
| recognition.abort(); | ||
| } | ||
| }, []); | ||
|
|
||
| const cancel = useCallback(() => { | ||
| dispose(); | ||
| setListening(false); | ||
| }, [dispose]); | ||
|
|
||
| useEffect(() => { | ||
| if (!enabled) cancel(); | ||
| return dispose; | ||
| }, [enabled, cancel, dispose]); | ||
|
|
||
| const toggle = useCallback(() => { | ||
| if (!enabled || !Recognition) return; | ||
| if (recognitionRef.current) { | ||
| recognitionRef.current.stop(); | ||
| return; | ||
| } | ||
|
|
||
| const recognition = new Recognition(); | ||
| recognitionRef.current = recognition; | ||
| recognition.lang = language; | ||
| recognition.continuous = true; | ||
| recognition.interimResults = false; | ||
| const delivered = new Set<number>(); | ||
| recognition.onresult = (event) => { | ||
| const transcripts: string[] = []; | ||
| for (let i = event.resultIndex; i < event.results.length; i++) { | ||
| const result = event.results[i]; | ||
| if (result.isFinal && !delivered.has(i)) { | ||
| delivered.add(i); | ||
| transcripts.push(result[0].transcript.trim()); | ||
| } | ||
| } | ||
| const transcript = transcripts.filter(Boolean).join(' '); | ||
| if (transcript) callbacks.current.onTranscript(transcript); | ||
| }; | ||
| recognition.onend = () => { | ||
| recognitionRef.current = null; | ||
| recognition.onresult = null; | ||
| recognition.onerror = null; | ||
| recognition.onend = null; | ||
| setListening(false); | ||
| }; | ||
| recognition.onerror = (event) => { | ||
| cancel(); | ||
| if (event.error !== 'aborted' && event.error !== 'no-speech') { | ||
| callbacks.current.onError(event.error); | ||
| } | ||
| }; | ||
| try { | ||
| setListening(true); | ||
| recognition.start(); | ||
| } catch (error) { | ||
| cancel(); | ||
| callbacks.current.onError( | ||
| error instanceof Error ? error.message : 'unknown' | ||
| ); | ||
| } | ||
| }, [enabled, Recognition, language, cancel]); | ||
|
|
||
| return { supported: !!Recognition, listening, toggle, cancel }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The onTranscript handler appends dictated text to the React
valuestate:setValueExtern([value, text].join(' ')). If two final recognition results arrive before React re-renders, both use the same stalevalue, so the second overwrites the first instead of appending to it and dictated text is lost. Read the live input text rather than thevaluestate (e.g., frominputRef.currentor an accumulating ref) so consecutive results always build on the latest content.Prompt for AI agents