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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import React, { useRef, useState } from "react";
import { Mic, MicOff, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast";
import { useAppDispatch, useAppSelector } from "@/redux/hooks";
import { addMessage } from "@/redux/slices/chatSlice";
import { setIsListening } from "@/redux/slices/chatSlice";
import {
  setConnected,
  setThinking,
  setTranscribing,
} from "@/redux/slices/sessionSlice";

interface MessageMetadata {
  session_id: string;
  sequence_id: string;
  transcript: string;
}

const VoiceChat = () => {
  // WebSocket and audio state
  const [socketConnected, setSocketConnected] = useState(false);
  const [audioStream, setAudioStream] = useState<MediaStream | null>(null);
  const [audioContext, setAudioContext] = useState<AudioContext | null>(null);
  const [audioContextState, setAudioContextState] = useState(false);
  const [elapsedTime, setElapsedTime] = useState(0);
  const [latency, setLatency] = useState(0);
  const [generating, setGenerating] = useState(false);
  const [isPlaying, setIsPlaying] = useState(false);

  const { sessionId, transcribing, thinking, connected } = useAppSelector(
    (state) => state.session
  );

  // App state management with Redux
  const { toast } = useToast();
  const dispatch = useAppDispatch();
  const { activeChat } = useAppSelector((state) => state.chats);
  const isListening = useAppSelector((state) => state.chat.isListening);
  const { temperature, activeVoice, maxTokens, threshold, silenceDuration } =
    useAppSelector((state) => state.settings);

  // Refs for audio processing
  const socketRef = useRef<WebSocket | null>(null);
  const sourceRef = useRef<AudioBufferSourceNode | null>(null);
  const audioContextRef = useRef<AudioContext | null>(null);
  const playingRef = useRef<boolean>(false);
  const lastShiftedRef = useRef<string | null>(null);
  const bufferQueueRef = useRef<string[]>([]);
  const isGrpcRef = useRef<boolean | null>(null);

  const formatTime = (milliseconds: number): string => {
    const totalSeconds = Math.floor(milliseconds / 1000);
    const hours = Math.floor(totalSeconds / 3600);
    const minutes = Math.floor((totalSeconds % 3600) / 60);
    const seconds = totalSeconds % 60;

    return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(
      2,
      "0"
    )}:${String(seconds).padStart(2, "0")}`;
  };

  const floatTo16BitPCM = (input: Float32Array): ArrayBuffer => {
    const output = new Int16Array(input.length);
    for (let i = 0; i < input.length; i++) {
      const sample = Math.max(-1, Math.min(1, input[i]));
      output[i] = sample < 0 ? sample * 0x8000 : sample * 0x7fff;
    }
    return output.buffer;
  };

  const handlePlay = async (timestamp: number): Promise<void> => {
    try {
      if (!isGrpcRef.current) {
        return;
      }

      playingRef.current = true;
      setIsPlaying(true);

      const base64Data = bufferQueueRef.current.shift();
      if (!base64Data) {
        playingRef.current = false;
        setIsPlaying(false);
        return;
      }

      lastShiftedRef.current = base64Data;

      const bytes = new Uint8Array(
        atob(base64Data)
          .split("")
          .map((char) => char.charCodeAt(0))
      );

      let arrayBuffer = bytes.buffer;

      const metadataEndIndex = bytes.indexOf(0);
      const metadataStr = new TextDecoder().decode(
        bytes.slice(0, metadataEndIndex)
      );
      const metadata = JSON.parse(metadataStr) as MessageMetadata;

      const { session_id, sequence_id, transcript } = metadata;

      if (sequence_id !== "-2") {
        if (socketRef.current && (socketRef.current as any).interval) {
          clearInterval((socketRef.current as any).interval);
          (socketRef.current as any).interval = null;
          setLatency((prev) => prev + 150);
          dispatch(setThinking(false));
        }

        arrayBuffer = arrayBuffer.slice(metadataEndIndex + 1);

        try {
          if (audioContextRef.current?.state === "suspended") {
            await audioContextRef.current.resume();
          }

          if (!audioContextRef.current) {
            return;
          }

          const audioBuffer = await audioContextRef.current.decodeAudioData(
            arrayBuffer
          );

          if (sourceRef.current) {
            sourceRef.current.disconnect();
          }

          const source = audioContextRef.current.createBufferSource();
          source.buffer = audioBuffer;
          source.connect(audioContextRef.current.destination);
          source.start(0);

          sourceRef.current = source;
          (sourceRef.current as any).session_id = session_id;
          (sourceRef.current as any).sequence_id = sequence_id;
          (sourceRef.current as any).transcript = transcript;

          sourceRef.current.onended = () => {
            lastShiftedRef.current = null;

            if (
              socketRef.current?.readyState === WebSocket.OPEN &&
              (sourceRef.current as any)?.sequence_id
            ) {
              socketRef.current.send(
                JSON.stringify({
                  type: "status",
                  msg: {
                    session_id: (sourceRef.current as any)?.session_id,
                    sequence_id: (sourceRef.current as any)?.sequence_id,
                    transcript: (sourceRef.current as any)?.transcript,
                  },
                })
              );
            }

            if (bufferQueueRef.current.length > 0) {
              playingRef.current = true;
              setIsPlaying(true);
              const currentTimestamp = Date.now();
              handlePlay(currentTimestamp);
            } else {
              playingRef.current = false;
              setIsPlaying(false);
            }
          };
        } catch (error) {
          console.error("Error decoding audio data:", error);
        }
      } else {
        const startTime = Date.now();
        const interval = setInterval(() => {
          setLatency(Date.now() - startTime);
        }, 10);

        (socketRef.current as any).interval = interval;

        if (bufferQueueRef.current.length > 0) {
          playingRef.current = true;
          setIsPlaying(true);
          const currentTimestamp = Date.now();
          handlePlay(currentTimestamp);
        } else {
          playingRef.current = false;
          setIsPlaying(false);
        }
      }
    } catch (error) {
      console.error("Error in handlePlay: ", error);
    }
  };

  const connectToRealtimeTTS = async (): Promise<void> => {
    return new Promise<void>((resolve, reject) => {
      try {
        const newAudioContext = new (window.AudioContext ||
          (window as any).webkitAudioContext)();
        audioContextRef.current = newAudioContext;
        setAudioContextState(true);

        let websocketURL = import.meta.env.VITE_WEBSOCKET_URL;

        websocketURL = import.meta.env.VITE_WEBSOCKET_URL.includes("localhost")
          ? `ws://${import.meta.env.VITE_WEBSOCKET_URL}/v2v`
          : `wss://${import.meta.env.VITE_WEBSOCKET_URL}/v2v`;

        const ws = new WebSocket(websocketURL);

        ws.onopen = () => {
          // Initial connection established
        };

        ws.onmessage = async (event) => {
          try {
            const data = JSON.parse(event.data);
            const { type, msg } = data;

            switch (type) {
              case "initial":
                socketRef.current = ws;
                setSocketConnected(true);
                resolve();
                break;
              case "media":
                const timestamp = Date.now();
                bufferQueueRef.current.push(msg);

                if (!playingRef.current && bufferQueueRef.current.length > 0) {
                  handlePlay(timestamp);
                }
                break;
              case "info":
                toast({
                  variant: "destructive",
                  title: "Error",
                  description: msg,
                });
                break;
              case "thinking":
                dispatch(setThinking(true));
                break;
              case "transcribing":
                dispatch(setTranscribing(true));
                break;
              case "stop_transcribing":
                dispatch(setTranscribing(false));
                break;
              case "connected":
                dispatch(setConnected(true));
                break;
              case "ready":
                isGrpcRef.current = true;
                startAudioStream();
                break;
              case "pause":
                if (sourceRef.current) {
                  sourceRef.current.onended = null;
                  sourceRef.current.stop();
                  sourceRef.current.disconnect();
                  sourceRef.current = null;
                }
                playingRef.current = false;
                setGenerating(true);
                setIsPlaying(false);
                break;
              case "continue":
                if (lastShiftedRef.current) {
                  bufferQueueRef.current.unshift(lastShiftedRef.current);
                  lastShiftedRef.current = null;
                }
                setGenerating(false);
                const currentTimestamp = Date.now();
                handlePlay(currentTimestamp);
                if ((socketRef.current as any).interval) {
                  clearInterval((socketRef.current as any).interval);
                  (socketRef.current as any).interval = null;
                }
                break;
              case "clear":
                bufferQueueRef.current = [];
                playingRef.current = false;
                setGenerating(false);
                setIsPlaying(false);

                if (sourceRef.current) {
                  sourceRef.current.onended = null;
                  sourceRef.current.stop();
                  sourceRef.current.disconnect();
                  sourceRef.current = null;
                }
                break;
              case "end":
                try {
                  if (audioContext) {
                    audioContext
                      .close()
                      .then(() => {
                        setAudioContextState(false);
                        if (isListening) {
                          toast({
                            variant: "destructive",
                            title: "Connection closed",
                            description: "Please restart the conversation.",
                          });
                        }
                      })
                      .catch(() => {
                        if (isListening) {
                          toast({
                            variant: "destructive",
                            title: "Error",
                            description: "Please restart the conversation.",
                          });
                        }
                      });
                  }
                  stopAudioStream();
                  dispatch(setIsListening(false));
                } catch (error) {
                  console.error("Error in closing audioContext.");
                }
                break;
              case "chat":
                if (msg && activeChat) {
                  if (msg.role && msg.content) {
                    const messageType =
                      msg.role.toLowerCase() === "user" ? "user" : "assistant";
                    dispatch(
                      addMessage({
                        role: messageType,
                        content: msg.content,
                      })
                    );
                  }
                  // dispatch(updateChatTimestamp(activeChat));
                }
                break;
              case "chathistory":
                console.info("Chathistory");
                break;
              default:
                break;
            }
          } catch (error) {
            console.error("Error in websocket message handling:", error);
          }
        };

        ws.onclose = async () => {
          try {
            if (audioStream) {
              audioStream.getTracks().forEach((track) => track.stop());
              setAudioStream(null);
            }
            setElapsedTime(0);
          } catch (err) {
            console.error("Error in closing audio stream:", err);
          }
        };

        ws.onerror = (err) => {
          console.error("WebSocket Error:", err);
          reject(err);
        };
      } catch (err) {
        console.error("Error in making WebSocket connection:", err);
        reject(err);
      }
    });
  };

  const startAudioStream = async (): Promise<void> => {
    try {
      const startTime = Date.now();
      setLatency(0);
      const interval = setInterval(() => {
        setElapsedTime(Date.now() - startTime);
      }, 1000);
      dispatch(setThinking(false));

      if (!socketRef.current) {
        toast({
          variant: "destructive",
          title: "Connection Error",
          description: "Please try again. Socket not connected.",
        });
        return;
      }

      audioContextRef.current = new (window.AudioContext ||
        (window as any).webkitAudioContext)();

      const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
      setAudioStream(stream);

      const newAudioContext = new AudioContext({
        sampleRate: 16000,
      });

      setAudioContext(newAudioContext);
      const audioInput = newAudioContext.createMediaStreamSource(stream);
      const bufferSize = 512;
      const scriptProcessorNode = newAudioContext.createScriptProcessor(
        bufferSize,
        1,
        1
      );

      scriptProcessorNode.onaudioprocess = async (e) => {
        const inputData = e.inputBuffer.getChannelData(0);
        // const l16Data = inputData;
        const l16Data = floatTo16BitPCM(inputData);
        try {
          if (!isGrpcRef.current) {
            try {
              if (audioContextState && newAudioContext) {
                newAudioContext
                  .close()
                  .then(() => {
                    setAudioContextState(false);
                    toast({
                      variant: "destructive",
                      title: "Connection Error",
                      description: "Please restart the conversation.",
                    });
                  })
                  .catch(() => {
                    toast({
                      variant: "destructive",
                      title: "Connection Error",
                      description: "Please restart the conversation.",
                    });
                  });
                await stopAudioStream();
                dispatch(setIsListening(false));
              }
            } catch (error) {
              console.error("Error in closing audioContext:", error);
            }
          }

          if (
            isGrpcRef.current &&
            socketRef.current &&
            socketRef.current.readyState === WebSocket.OPEN
          ) {
            socketRef.current.send(l16Data);
          }
        } catch (err) {
          console.error("Error in sending buffer:", err);
        }
      };

      audioInput.connect(scriptProcessorNode);
      scriptProcessorNode.connect(newAudioContext.destination);
    } catch (error) {
      console.error("Error accessing microphone:", error);
      toast({
        variant: "destructive",
        title: "Microphone Error",
        description:
          "Could not access your microphone. Please check your permissions.",
      });
    }
  };

  const stopAudioStream = async (): Promise<void> => {
    setGenerating(false);
    setLatency(0);
    dispatch(setThinking(false));
    dispatch(setConnected(false));
    dispatch(setIsListening(false));
    setElapsedTime(0);
    bufferQueueRef.current = [];

    if (socketRef.current && socketRef.current.readyState === WebSocket.OPEN) {
      if ((socketRef.current as any).interval) {
        clearInterval((socketRef.current as any).interval);
        (socketRef.current as any).interval = null;
      }

      if (audioStream) {
        try {
          audioStream.getTracks().forEach((track) => track.stop());
          setAudioStream(null);
        } catch (err) {
          console.error("Error stopping audio stream:", err);
        }
      }

      try {
        isGrpcRef.current = false;
        socketRef.current.send(JSON.stringify({ type: "stop", msg: "stop" }));
        socketRef.current.close();
        socketRef.current = null;
      } catch (err) {
        console.error("Error closing WebSocket:", err);
      }

      try {
        if (audioContext) {
          await audioContext.close();
          setAudioContext(null);
        }
      } catch (err) {
        console.error("Error closing AudioContext:", err);
      }
    }
  };

  const handleVoiceChatToggle = async (): Promise<void> => {
    if (!activeChat) {
      toast({
        variant: "destructive",
        title: "Error",
        description: "Please select a chat or create one",
      });
      return;
    }
    if (!isListening) {
      setLatency(0);
      dispatch(setIsListening(true));
      try {
        await connectToRealtimeTTS();
        if (socketRef.current) {
          socketRef.current.send(
            JSON.stringify({
              type: "start",
              msg: JSON.stringify({
                temperature: temperature,
                silenceDuration: silenceDuration,
                activeVoice: activeVoice,
                threshold: threshold,
                sessionId: activeChat,
                maxTokens: maxTokens,
              }),
            })
          );
        }
      } catch (error) {
        console.error("Failed to start voice chat:", error);
        dispatch(setIsListening(false));
        toast({
          variant: "destructive",
          title: "Connection Error",
          description: "Failed to start voice chat. Please try again.",
        });
      }
    } else {
      socketRef.current.close();
      await stopAudioStream();
      socketRef.current = null;
    }
  };

  return (
    <div className="flex flex-col items-center w-full max-w-md mx-auto">
      <div className="w-full flex justify-between items-center mb-4">
        {/* <div className={`text-sm text-white ${!isListening && "m-auto"}`}>
          {isListening ? `Active: ${formatTime(elapsedTime)}` : "Ready"}
        </div> */}
        {isListening && (
          <div className="text-sm text-white/60">Latency: {latency}ms</div>
        )}

        {/* <div className="relative mb-4">{isListening && <VoiceVisualizer />}</div> */}

        {/* <LottieMicAnimation isListening={isListening} /> */}

        {transcribing && (
          <div className="flex gap-2 text-white">
            <div>
              <Loader2 className="animate-spin" />
            </div>
            <div>Transcribing</div>
          </div>
        )}
        {thinking && (
          <div className="flex gap-2 text-white">
            <div>
              <Loader2 className="animate-spin" />
            </div>
            <div>Thinking</div>
          </div>
        )}
      </div>

      <Button
        onClick={handleVoiceChatToggle}
        className={`w-16 h-16 rounded-full transition-all duration-300 ${
          isListening
            ? "bg-red-500 hover:bg-red-600"
            : "bg-emerald-500 hover:bg-emerald-600"
        }`}
        // disabled={generating || isPlaying}
      >
        {isListening ? (
          !connected ? (
            <Loader2 className="animate-spin" />
          ) : (
            <MicOff className="h-6 w-6" />
          )
        ) : generating ? (
          <Loader2 className="h-6 w-6 animate-spin" />
        ) : (
          <Mic className="h-6 w-6" />
        )}
      </Button>

      <p className="text-white/70 mt-3 text-sm">
        {isListening ? "Click to stop listening" : "Click to start voice chat"}
      </p>
    </div>
  );
};

export default VoiceChat;