-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add basic audio support with voice recording and TTS #20
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
base: develop
Are you sure you want to change the base?
Changes from all commits
a73a220
8e5ceee
bb3e189
18bf773
078f4f6
8132e73
de159d0
92814cd
8c74d24
3281b14
4ed954b
6d4ae3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| 'use client'; | ||
|
|
||
| import { useAnonymousAuth } from '@/components/anonymous-auth'; | ||
| import { PENDING_VOICE_MESSAGE_KEY } from '@/components/home/home-input'; | ||
| import { useChatStore } from '@/components/providers/chat-store-provider'; | ||
| import { useTenant } from '@/components/providers/tenant-provider'; | ||
| import type { | ||
|
|
@@ -24,6 +25,7 @@ type Props = { | |
| allParties?: PartyDetails[]; | ||
| proposedQuestions?: ProposedQuestion[]; | ||
| initialQuestion?: string; | ||
| hasPendingVoiceMessage?: boolean; | ||
| }; | ||
|
|
||
| function ChatMessagesView({ | ||
|
|
@@ -34,16 +36,20 @@ function ChatMessagesView({ | |
| allParties, | ||
| proposedQuestions, | ||
| initialQuestion, | ||
| hasPendingVoiceMessage, | ||
| }: Props) { | ||
| const hasFetched = useRef(false); | ||
| const hasProcessedVoiceMessage = useRef(false); | ||
| const storeMessages = useChatStore((state) => state.messages); | ||
| const hydrateChatSession = useChatStore((state) => state.hydrateChatSession); | ||
| const sendVoiceMessage = useChatStore((state) => state.sendVoiceMessage); | ||
| const { user } = useAnonymousAuth(); | ||
| const tenant = useTenant(); | ||
|
|
||
| const hasCurrentStreamingMessages = useChatStore( | ||
| (state) => state.currentStreamingMessages !== undefined, | ||
| ); | ||
| const isSocketConnected = useChatStore((state) => state.socket.connected); | ||
|
|
||
| useEffect(() => { | ||
| if (!user?.uid) return; | ||
|
|
@@ -70,6 +76,31 @@ function ChatMessagesView({ | |
| tenant, | ||
| ]); | ||
|
|
||
| // Handle pending voice message from home page | ||
| useEffect(() => { | ||
| if ( | ||
| !hasPendingVoiceMessage || | ||
| hasProcessedVoiceMessage.current || | ||
| !isSocketConnected | ||
| ) | ||
| return; | ||
|
|
||
| const pendingAudioBase64 = sessionStorage.getItem( | ||
| PENDING_VOICE_MESSAGE_KEY, | ||
| ); | ||
| if (pendingAudioBase64) { | ||
| sessionStorage.removeItem(PENDING_VOICE_MESSAGE_KEY); | ||
| hasProcessedVoiceMessage.current = true; | ||
| // Convert base64 back to Uint8Array | ||
| const binaryString = atob(pendingAudioBase64); | ||
| const audioBytes = new Uint8Array(binaryString.length); | ||
| for (let i = 0; i < binaryString.length; i++) { | ||
| audioBytes[i] = binaryString.charCodeAt(i); | ||
| } | ||
| sendVoiceMessage(audioBytes); | ||
| } | ||
| }, [hasPendingVoiceMessage, sendVoiceMessage, isSocketConnected]); | ||
|
Comment on lines
+80
to
+102
|
||
|
|
||
| const normalizedMessages = useMemo(() => { | ||
| if (messages && !storeMessages.length) { | ||
| return messages; | ||
|
|
||
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.
The base64 encoding/decoding of audio data could fail with large files or non-standard characters, but there's no error handling. If atob() fails (line 95) due to invalid base64, it will throw an uncaught exception. Consider wrapping the conversion in a try-catch block and showing an appropriate error message to the user.