"use client"; import axios from "axios"; import { redirect } from "next/navigation"; import { useEffect, useState } from "react"; import { MdMic, MdMicOff, MdSettings } from "react-icons/md"; import Button from "../components/ui/Button"; import Card from "../components/ui/Card"; import Modal from "../components/ui/Modal"; import PageHeading from "../components/ui/PageHeading"; import { useSupabase } from "../supabase-provider"; import ChatMessages from "./ChatMessages"; export default function ChatPage() { const [question, setQuestion] = useState(""); const [history, setHistory] = useState>([]); const [model, setModel] = useState("gpt-3.5-turbo"); const [temperature, setTemperature] = useState(0); const [maxTokens, setMaxTokens] = useState(500); const [isPending, setIsPending] = useState(false); const [isListening, setIsListening] = useState(false); const { session } = useSupabase(); if (session === null) { redirect("/login"); } useEffect(() => { if (typeof window !== "undefined") { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const mic = new SpeechRecognition(); mic.continuous = true; mic.interimResults = false; mic.lang = "en-US"; mic.onstart = () => { console.log("Mics on"); }; mic.onend = () => { console.log("Mics off"); }; mic.onerror = (event: SpeechRecognitionErrorEvent) => { console.log(event.error); setIsListening(false); }; mic.onresult = (event: SpeechRecognitionEvent) => { const interimTranscript = event.results[event.results.length - 1][0].transcript; setQuestion((prevQuestion) => prevQuestion + interimTranscript); }; if (isListening) { mic.start(); } return () => { if (mic) { mic.stop(); } }; } }, [isListening]); const askQuestion = async () => { setHistory((hist) => [...hist, ["user", question]]); setIsPending(true); setIsListening(false); const response = await axios.post( `${process.env.NEXT_PUBLIC_BACKEND_URL}/chat/`, { model, question, history, temperature, max_tokens: maxTokens, }, { headers: { Authorization: `Bearer ${session.access_token}`, }, } ); setHistory(response.data.history); setQuestion(""); setIsPending(false); }; const handleListen = () => { setIsListening((prevIsListening) => !prevIsListening); }; return (
{/* Chat */}
{ e.preventDefault(); if (!isPending) askQuestion(); }} className="w-full flex items-center justify-center gap-2" > setQuestion(e.target.value)} className="w-full p-2 border border-gray-300 dark:border-gray-500 outline-none rounded dark:bg-gray-800" placeholder="Begin conversation here..." /> {/* Mic Button */} {/* Settings Button */} } title="Settings" desc="Modify your brain" >
setTemperature(+e.target.value)} />
setMaxTokens(+e.target.value)} />
); }