@@ -28,13 +28,7 @@ import { safe } from "ts-safe";
2828import { Alert , AlertDescription , AlertTitle } from "ui/alert" ;
2929import { Button } from "ui/button" ;
3030
31- import {
32- Drawer ,
33- DrawerContent ,
34- DrawerOverlay ,
35- DrawerPortal ,
36- DrawerTitle ,
37- } from "ui/drawer" ;
31+ import { Drawer , DrawerContent , DrawerPortal , DrawerTitle } from "ui/drawer" ;
3832import {
3933 DropdownMenu ,
4034 DropdownMenuContent ,
@@ -61,6 +55,7 @@ import { useTranslations } from "next-intl";
6155import { Dialog , DialogContent , DialogTitle , DialogTrigger } from "ui/dialog" ;
6256import JsonView from "ui/json-view" ;
6357import { useRouter } from "next/navigation" ;
58+ import { isShortcutEvent , Shortcuts } from "lib/keyboard-shortcuts" ;
6459
6560const isNotEmptyUIMessage = ( message : UIMessage ) => {
6661 return message . parts . some ( ( v ) => {
@@ -149,30 +144,30 @@ export function ChatBotVoice() {
149144 start ( ) . then ( ( ) => {
150145 startAudio . current ?. play ( ) . catch ( ( ) => { } ) ;
151146 } ) ;
152- startAudio . current ?. play ( ) . catch ( ( ) => { } ) ;
153147 } , [ start ] ) ;
154148
155149 const endVoiceChat = useCallback ( async ( ) => {
156150 setIsClosing ( true ) ;
157151 await safe ( ( ) => stop ( ) ) ;
158- await safe ( ( ) => {
159- if ( ! currentThreadId ) return ;
152+ await safe ( async ( ) => {
153+ if ( ! currentThreadId || ! voiceChat . autoSaveConversation ) return ;
160154 const saveMessages = messages . filter (
161155 ( v ) => v . completed && isNotEmptyUIMessage ( v ) ,
162156 ) ;
163157 if ( saveMessages . length === 0 ) {
164158 return ;
165159 }
166- return fetch ( `/api/chat/${ currentThreadId } ` , {
160+ await fetch ( `/api/chat/${ currentThreadId } ` , {
167161 method : "POST" ,
168162 body : JSON . stringify ( {
169163 messages : mergeConsecutiveMessages ( saveMessages ) ,
170164 model,
171165 projectId : currentProjectId ,
172166 } ) ,
173167 } ) ;
174- } ) . ifOk ( ( ) => {
175- if ( messages . length && currentThreadId ) {
168+ return true ;
169+ } ) . ifOk ( ( isSaved ) => {
170+ if ( isSaved ) {
176171 nextTick ( ) . then ( ( ) => {
177172 mutate ( "threads" ) ;
178173 router . push ( `/chat/${ currentThreadId } ` ) ;
@@ -189,7 +184,13 @@ export function ChatBotVoice() {
189184 isOpen : false ,
190185 } ,
191186 } ) ;
192- } , [ messages , currentProjectId , model , currentThreadId ] ) ;
187+ } , [
188+ messages ,
189+ currentProjectId ,
190+ model ,
191+ currentThreadId ,
192+ voiceChat . autoSaveConversation ,
193+ ] ) ;
193194
194195 const statusMessage = useMemo ( ( ) => {
195196 if ( isLoading ) {
@@ -261,6 +262,25 @@ export function ChatBotVoice() {
261262 }
262263 } , [ error ] ) ;
263264
265+ useEffect ( ( ) => {
266+ if ( voiceChat . isOpen ) return ;
267+ const handleKeyDown = ( e : KeyboardEvent ) => {
268+ const isVoiceChatEvent = isShortcutEvent ( e , Shortcuts . toggleVoiceChat ) ;
269+ if ( isVoiceChatEvent ) {
270+ e . preventDefault ( ) ;
271+ e . stopPropagation ( ) ;
272+ appStoreMutate ( ( prev ) => ( {
273+ voiceChat : {
274+ ...prev . voiceChat ,
275+ isOpen : true ,
276+ } ,
277+ } ) ) ;
278+ }
279+ } ;
280+ window . addEventListener ( "keydown" , handleKeyDown ) ;
281+ return ( ) => window . removeEventListener ( "keydown" , handleKeyDown ) ;
282+ } , [ voiceChat . isOpen ] ) ;
283+
264284 return (
265285 < Drawer dismissible = { false } open = { voiceChat . isOpen } direction = "top" >
266286 < DrawerPortal >
0 commit comments