import { Conversation, KeyValuePair, Message, OpenAIModel } from "@/types"; import { FC, useEffect, useRef, useState } from "react"; import { ChatInput } from "./ChatInput"; import { ChatLoader } from "./ChatLoader"; import { ChatMessage } from "./ChatMessage"; import { ModelSelect } from "./ModelSelect"; import { Regenerate } from "./Regenerate"; import { SystemPrompt } from "./SystemPrompt"; interface Props { conversation: Conversation; models: OpenAIModel[]; messageIsStreaming: boolean; modelError: boolean; messageError: boolean; loading: boolean; lightMode: "light" | "dark"; onSend: (message: Message, isResend: boolean) => void; onUpdateConversation: (conversation: Conversation, data: KeyValuePair) => void; } export const Chat: FC = ({ conversation, models, messageIsStreaming, modelError, messageError, loading, lightMode, onSend, onUpdateConversation }) => { const [currentMessage, setCurrentMessage] = useState(); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "auto" }); }; useEffect(() => { scrollToBottom(); }, [conversation.messages]); return (
{modelError ? (
Error fetching models.
Make sure your OpenAI API key is set in the bottom left of the sidebar or in a .env.local file and refresh.
If you completed this step, OpenAI may be experiencing issues.
) : ( <>
{conversation.messages.length === 0 ? ( <>
{models.length === 0 ? "Loading..." : "Chatbot UI"}
{models.length > 0 && (
onUpdateConversation(conversation, { key: "model", value: model })} /> onUpdateConversation(conversation, { key: "prompt", value: prompt })} />
)}
) : ( <>
Model: {conversation.model.name}
{conversation.messages.map((message, index) => ( ))} {loading && }
)}
{messageError ? ( { if (currentMessage) { onSend(currentMessage, true); } }} /> ) : ( { setCurrentMessage(message); onSend(message, false); }} model={conversation.model} /> )} )}
); };