@@ -824,6 +824,9 @@ export default function ChatPage() {
824824 // --- Voice Mode Handlers ---
825825 const handleStatusChange = useCallback (
826826 ( status ) => {
827+ console . log (
828+ `[ChatPage] Voice connection status changed to: ${ status } `
829+ )
827830 setConnectionStatus ( status )
828831 if ( status !== "connecting" && ringtoneAudioRef . current ) {
829832 ringtoneAudioRef . current . pause ( )
@@ -834,12 +837,23 @@ export default function ChatPage() {
834837 connectedAudioRef . current . volume = 0.4
835838 connectedAudioRef . current
836839 . play ( )
837- . catch ( ( e ) => console . error ( "Error playing sound:" , e ) )
840+ . catch ( ( e ) =>
841+ console . error (
842+ "[ChatPage] Error playing connected sound:" ,
843+ e
844+ )
845+ )
838846 }
839847 // Add a delay to allow ICE connection to stabilize
848+ console . log (
849+ "[ChatPage] Connection established. Muting mic for 4s to stabilize."
850+ )
840851 setVoiceStatusText ( "Please wait a moment..." )
841852 setMicrophoneEnabled ( false ) // Mute mic during stabilization
842853 setTimeout ( ( ) => {
854+ console . log (
855+ "[ChatPage] Stabilization complete. Unmuting mic."
856+ )
843857 setVoiceStatusText ( "Listening..." )
844858 setMicrophoneEnabled ( true ) // Unmute after delay
845859 } , 4000 )
@@ -854,6 +868,7 @@ export default function ChatPage() {
854868
855869 const handleVoiceEvent = useCallback (
856870 ( event ) => {
871+ console . log ( "[ChatPage] Received voice event:" , event )
857872 if ( event . type === "stt_result" && event . text ) {
858873 setDisplayedMessages ( ( prev ) => [
859874 ...prev ,
@@ -876,6 +891,7 @@ export default function ChatPage() {
876891 }
877892 ] )
878893 } else if ( event . type === "status" ) {
894+ console . log ( `[ChatPage] Voice status update: ${ event . message } ` )
879895 if ( event . message === "thinking" ) {
880896 setVoiceStatusText ( "Thinking..." )
881897 setMicrophoneEnabled ( false )
@@ -890,14 +906,24 @@ export default function ChatPage() {
890906 const textToMeasure = lastSpokenTextRef . current
891907 // Estimate duration: ~18 chars/sec -> ~55ms/char. Add a smaller buffer.
892908 const estimatedDuration = textToMeasure . length * 55 + 250 // ms
909+ console . log (
910+ `[ChatPage] Server is listening. Waiting estimated ${ estimatedDuration } ms for audio buffer to clear before unmuting.`
911+ )
893912
894913 setTimeout ( ( ) => {
895914 if (
896915 webrtcClientRef . current ?. peerConnection
897916 ?. connectionState === "connected"
898917 ) {
918+ console . log (
919+ "[ChatPage] Estimated audio buffer clear time elapsed. Unmuting."
920+ )
899921 setVoiceStatusText ( "Listening..." )
900922 setMicrophoneEnabled ( true )
923+ } else {
924+ console . log (
925+ "[ChatPage] Estimated audio buffer clear time elapsed, but connection is no longer active. Not unmuting."
926+ )
901927 }
902928 } , estimatedDuration )
903929
@@ -936,19 +962,23 @@ export default function ChatPage() {
936962
937963 const handleStartVoice = async ( ) => {
938964 if ( connectionStatus !== "disconnected" ) return
965+ console . log ( "[ChatPage] handleStartVoice triggered." )
939966
940967 setConnectionStatus ( "connecting" )
941968 setVoiceStatusText ( "Connecting..." )
942969 try {
943970 // Step 1: Get the main auth token
971+ console . log ( "[ChatPage] Fetching auth token..." )
944972 const tokenResponse = await fetch ( "/api/auth/token" )
945973 if ( ! tokenResponse . ok ) throw new Error ( "Could not get auth token." )
946974 const { accessToken } = await tokenResponse . json ( )
975+ console . log ( "[ChatPage] Auth token fetched." )
947976
948977 // Step 2: Use the auth token to get a temporary RTC token
949978 const serverUrl =
950979 process . env . NEXT_PUBLIC_APP_SERVER_URL ||
951980 "http://localhost:5000"
981+ console . log ( "[ChatPage] Fetching RTC token..." )
952982 const rtcTokenResponse = await fetch (
953983 `${ serverUrl } /voice/initiate` ,
954984 {
@@ -969,11 +999,18 @@ export default function ChatPage() {
969999 throw error
9701000 }
9711001 const { rtc_token, ice_servers } = await rtcTokenResponse . json ( )
1002+ console . log (
1003+ `[ChatPage] RTC token fetched. ICE servers count: ${ ice_servers ?. iceServers ?. length || 0 } `
1004+ )
9721005
9731006 // Step 3: Create and connect WebRTCClient directly
9741007 if ( webrtcClientRef . current ) {
1008+ console . log (
1009+ "[ChatPage] Disconnecting existing WebRTC client before creating new one."
1010+ )
9751011 webrtcClientRef . current . disconnect ( )
9761012 }
1013+ console . log ( "[ChatPage] Creating new WebRTCClient." )
9771014 const client = new WebRTCClient ( {
9781015 onConnected : ( ) => handleStatusChange ( "connected" ) ,
9791016 onDisconnected : ( ) => handleStatusChange ( "disconnected" ) ,
@@ -983,7 +1020,10 @@ export default function ChatPage() {
9831020 remoteAudioRef . current
9841021 . play ( )
9851022 . catch ( ( e ) =>
986- console . error ( "Error playing remote audio:" , e )
1023+ console . error (
1024+ "[ChatPage] Error playing remote audio:" ,
1025+ e
1026+ )
9871027 )
9881028 }
9891029 } ,
@@ -999,14 +1039,18 @@ export default function ChatPage() {
9991039 ringtoneAudioRef . current . loop = true
10001040 ringtoneAudioRef . current
10011041 . play ( )
1002- . catch ( ( e ) => console . error ( "Error playing ringtone:" , e ) )
1042+ . catch ( ( e ) =>
1043+ console . error ( "[ChatPage] Error playing ringtone:" , e )
1044+ )
10031045 }
1046+ console . log ( "[ChatPage] Calling WebRTCClient.connect()..." )
10041047 await webrtcClientRef . current . connect (
10051048 selectedAudioInputDevice ,
10061049 accessToken ,
10071050 rtc_token
10081051 )
10091052 } catch ( error ) {
1053+ console . error ( "[ChatPage] Error during handleStartVoice:" , error )
10101054 if ( error . status === 429 ) {
10111055 toast . error (
10121056 error . message ||
@@ -1025,8 +1069,12 @@ export default function ChatPage() {
10251069 }
10261070
10271071 const initializeVoiceMode = async ( ) => {
1072+ console . log ( "[ChatPage] Initializing voice mode..." )
10281073 // Check if devices are already loaded to avoid re-prompting
10291074 if ( audioInputDevices . length > 0 ) {
1075+ console . log (
1076+ "[ChatPage] Audio devices already available, skipping permission prompt."
1077+ )
10301078 return true
10311079 }
10321080
@@ -1039,18 +1087,23 @@ export default function ChatPage() {
10391087 return false
10401088 }
10411089 // This is the permission prompt
1090+ console . log ( "[ChatPage] Requesting microphone permissions..." )
10421091 await navigator . mediaDevices . getUserMedia ( {
10431092 audio : {
10441093 noiseSuppression : false ,
10451094 echoCancellation : false
10461095 } ,
10471096 video : false
10481097 } )
1098+ console . log ( "[ChatPage] Microphone permission granted." )
10491099 const devices = await navigator . mediaDevices . enumerateDevices ( )
10501100 const audioInputDevices = devices . filter (
10511101 ( d ) => d . kind === "audioinput"
10521102 )
10531103 if ( audioInputDevices . length > 0 ) {
1104+ console . log (
1105+ `[ChatPage] Found ${ audioInputDevices . length } audio input devices.`
1106+ )
10541107 setAudioInputDevices (
10551108 audioInputDevices . map ( ( d , i ) => ( {
10561109 deviceId : d . deviceId ,
@@ -1064,9 +1117,14 @@ export default function ChatPage() {
10641117 return true
10651118 } else {
10661119 toast . error ( "No audio input devices found." )
1120+ console . warn ( "[ChatPage] No audio input devices found." )
10671121 return false
10681122 }
10691123 } catch ( error ) {
1124+ console . error (
1125+ "[ChatPage] Error during voice initialization:" ,
1126+ error
1127+ )
10701128 toast . error ( "Microphone permission is required for voice mode." )
10711129 return false
10721130 }
@@ -1081,6 +1139,11 @@ export default function ChatPage() {
10811139 audioTracks [ 0 ] . enabled = ! isCurrentlyEnabled
10821140 const newMutedState = ! audioTracks [ 0 ] . enabled
10831141 setIsMuted ( newMutedState )
1142+ console . log (
1143+ `[ChatPage] Toggled mute. Mic is now ${
1144+ newMutedState ? "muted" : "unmuted"
1145+ } .`
1146+ )
10841147 setVoiceStatusText ( newMutedState ? "Muted" : "Listening..." )
10851148 }
10861149 }
@@ -1090,6 +1153,7 @@ export default function ChatPage() {
10901153 if ( connectionStatus === "disconnected" || ! webrtcClientRef . current ) {
10911154 return
10921155 }
1156+ console . log ( "[ChatPage] handleStopVoice triggered." )
10931157
10941158 webrtcClientRef . current ?. disconnect ( )
10951159
@@ -1098,15 +1162,19 @@ export default function ChatPage() {
10981162 const duration_seconds = Math . round (
10991163 ( Date . now ( ) - voiceModeStartTimeRef . current ) / 1000
11001164 )
1165+ console . log (
1166+ `[ChatPage] Voice mode ended. Duration: ${ duration_seconds } seconds.`
1167+ )
11011168 posthog ?. capture ( "voice_mode_used" , { duration_seconds } )
11021169
11031170 // Send usage update to the server
1171+ console . log ( "[ChatPage] Sending voice usage update to server." )
11041172 fetch ( "/api/voice/update-usage" , {
11051173 method : "POST" ,
11061174 headers : { "Content-Type" : "application/json" } ,
11071175 body : JSON . stringify ( { duration_seconds } )
11081176 } ) . catch ( ( err ) =>
1109- console . error ( "Failed to update voice usage:" , err )
1177+ console . error ( "[ChatPage] Failed to update voice usage:" , err )
11101178 )
11111179
11121180 voiceModeStartTimeRef . current = null // Reset after tracking
@@ -1136,17 +1204,26 @@ export default function ChatPage() {
11361204 }
11371205
11381206 if ( isVoiceMode ) {
1207+ console . log ( "[ChatPage] Toggling voice mode OFF." )
11391208 handleStopVoice ( )
11401209 setIsVoiceMode ( false )
11411210 } else {
1211+ console . log ( "[ChatPage] Toggling voice mode ON." )
11421212 // Switching TO voice mode, first get permissions
11431213 const permissionsGranted = await initializeVoiceMode ( )
11441214 if ( permissionsGranted ) {
1215+ console . log (
1216+ "[ChatPage] Permissions granted, activating voice mode."
1217+ )
11451218 // --- ADD POSTHOG EVENT TRACKING ---
11461219 posthog ?. capture ( "voice_mode_activated" )
11471220 voiceModeStartTimeRef . current = Date . now ( ) // Set start time
11481221 // --- END POSTHOG EVENT TRACKING ---
11491222 setIsVoiceMode ( true )
1223+ } else {
1224+ console . warn (
1225+ "[ChatPage] Permissions not granted, voice mode not activated."
1226+ )
11501227 }
11511228 }
11521229 }
0 commit comments