"use client"; import styles from './page.module.css'; import { useEffect, useRef, useState } from 'react'; import { useChat } from 'ai/react'; import { Message } from 'ai'; import ReactMarkdown from "react-markdown"; import { functionCallHandler } from './hooks/useFunctions'; import Input from './input'; const Page: React.FC = () => { // Ref for the messages container const messagesRef = useRef(null); const [keyboardHeight, setKeyboardHeight] = useState(0); const [isKeyboardVisible, setIsKeyboardVisible] = useState(false); const { messages, input, setInput, handleSubmit, isLoading } = useChat({ experimental_onFunctionCall: functionCallHandler, onError: (error: any) => { console.log(error); }, }); const inputRef = useRef(null); useEffect(() => { if (messagesRef.current) { messagesRef.current.scrollTop = messagesRef.current.scrollHeight; } }, [messages]); const [isExpanded, setIsExpanded] = useState(false); const toggleExpand = () => { setIsExpanded(!isExpanded); }; const roleUIConfig: { [key: string]: { bgColor: string; avatarColor: string; // eslint-disable-next-line no-unused-vars dialogComponent: (message: Message) => JSX.Element; }; } = { user: { bgColor: "bg-white", avatarColor: "bg-black", dialogComponent: (message: Message) => (
( ), }} > {message.content}
), }, assistant: { bgColor: "bg-gray-100", avatarColor: "bg-green-500", dialogComponent: (message: Message) => (
( ), }} > {message.content}
), }, function: { bgColor: "bg-gray-200", avatarColor: "bg-blue-500", dialogComponent: (message: Message) => { return (
{isExpanded && (
{message.content}
)}
); }, } }; return (
{messages.length > 0 ? ( messages.map((message, i) => { const messageClass = `${styles.message} ${message.role === 'user' ? styles['message-user'] : ''}`; return (
{message.content === "" && message.function_call != undefined ? ( typeof message.function_call === "object" ? (
Using{" "} {message.function_call.name} {" "} ...
{message.function_call.arguments}
) : (
{message.function_call}
) ) : (
{roleUIConfig[message.role].dialogComponent(message)}
)}
); }) ) : null}
); } export default Page;