import { useEffect, useRef } from 'react'; import { GetServerSideProps } from 'next'; import Head from 'next/head'; import { DEFAULT_SYSTEM_PROMPT, DEFAULT_TEMPERATURE, saveConversation, saveConversations, updateConversation, useCreateReducer } from '@/utils'; import { Chat } from '@/components/Chat/Chat'; import HomeContext from './home.context'; import { HomeInitialState, initialState } from './home.state'; import { v4 as uuidv4 } from 'uuid'; interface Props { serverSideApiKeyIsSet: boolean; serverSidePluginKeysSet: boolean; defaultModelId: any; } const Home = ({ defaultModelId, }: Props) => { const contextValue = useCreateReducer({ initialState, }); const { state: { lightMode, conversations, selectedConversation }, dispatch, } = contextValue; const stopConversationRef = useRef(false); // FETCH MODELS ---------------------------------------------- const handleSelectConversation = (conversation: any) => { dispatch({ field: 'selectedConversation', value: conversation, }); saveConversation(conversation); }; // CONVERSATION OPERATIONS -------------------------------------------- const handleNewConversation = () => { const lastConversation = conversations[conversations.length - 1]; const newConversation: any = { id: uuidv4(), name: 'New Conversation', messages: [], model: lastConversation?.model || { id: "OpenAIModels[defaultModelId].id", name: "OpenAIModels[defaultModelId].name", maxLength: "OpenAIModels[defaultModelId].maxLength", tokenLimit: "OpenAIModels[defaultModelId].tokenLimit", }, prompt: DEFAULT_SYSTEM_PROMPT, temperature: lastConversation?.temperature ?? DEFAULT_TEMPERATURE, folderId: null, }; const updatedConversations = [...conversations, newConversation]; dispatch({ field: 'selectedConversation', value: newConversation }); dispatch({ field: 'conversations', value: updatedConversations }); saveConversation(newConversation); saveConversations(updatedConversations); dispatch({ field: 'loading', value: false }); }; const handleUpdateConversation = ( conversation: any, data: any, ) => { const updatedConversation = { ...conversation, [data.key]: data.value, }; const { single, all } = updateConversation( updatedConversation, conversations, ); dispatch({ field: 'selectedConversation', value: single }); dispatch({ field: 'conversations', value: all }); }; // EFFECTS -------------------------------------------- useEffect(() => { if (window.innerWidth < 640) { dispatch({ field: 'showChatbar', value: false }); } }, [selectedConversation]); // ON LOAD -------------------------------------------- return ( Web LLM Embed
); }; export default Home; export const getServerSideProps: GetServerSideProps = async ({ locale }) => { const defaultModelId = "fallbackModelID" return { props: { defaultModelId }, }; };