| @@ -52,6 +52,10 @@ const ChatWrapper = () => { | |||
| allInputsHidden, | |||
| initUserVariables, | |||
| } = useChatWithHistoryContext() | |||
| // Semantic variable for better code readability | |||
| const isHistoryConversation = !!currentConversationId | |||
| const appConfig = useMemo(() => { | |||
| const config = appParams || {} | |||
| @@ -62,9 +66,9 @@ const ChatWrapper = () => { | |||
| fileUploadConfig: (config as any).system_parameters, | |||
| }, | |||
| supportFeedback: true, | |||
| opening_statement: currentConversationId ? currentConversationItem?.introduction : (config as any).opening_statement, | |||
| opening_statement: isHistoryConversation ? currentConversationItem?.introduction : (config as any).opening_statement, | |||
| } as ChatConfig | |||
| }, [appParams, currentConversationItem?.introduction, currentConversationId]) | |||
| }, [appParams, currentConversationItem?.introduction, isHistoryConversation]) | |||
| const { | |||
| chatList, | |||
| setTargetMessageId, | |||
| @@ -75,7 +79,7 @@ const ChatWrapper = () => { | |||
| } = useChat( | |||
| appConfig, | |||
| { | |||
| inputs: (currentConversationId ? currentConversationInputs : newConversationInputs) as any, | |||
| inputs: (isHistoryConversation ? currentConversationInputs : newConversationInputs) as any, | |||
| inputsForm: inputsForms, | |||
| }, | |||
| appPrevChatTree, | |||
| @@ -83,7 +87,7 @@ const ChatWrapper = () => { | |||
| clearChatList, | |||
| setClearChatList, | |||
| ) | |||
| const inputsFormValue = currentConversationId ? currentConversationInputs : newConversationInputsRef?.current | |||
| const inputsFormValue = isHistoryConversation ? currentConversationInputs : newConversationInputsRef?.current | |||
| const inputDisabled = useMemo(() => { | |||
| if (allInputsHidden) | |||
| return false | |||
| @@ -132,7 +136,7 @@ const ChatWrapper = () => { | |||
| const data: any = { | |||
| query: message, | |||
| files, | |||
| inputs: formatBooleanInputs(inputsForms, currentConversationId ? currentConversationInputs : newConversationInputs), | |||
| inputs: formatBooleanInputs(inputsForms, isHistoryConversation ? currentConversationInputs : newConversationInputs), | |||
| conversation_id: currentConversationId, | |||
| parent_message_id: (isRegenerate ? parentAnswer?.id : getLastAnswer(chatList)?.id) || null, | |||
| } | |||
| @@ -142,11 +146,11 @@ const ChatWrapper = () => { | |||
| data, | |||
| { | |||
| onGetSuggestedQuestions: responseItemId => fetchSuggestedQuestions(responseItemId, isInstalledApp, appId), | |||
| onConversationComplete: currentConversationId ? undefined : handleNewConversationCompleted, | |||
| onConversationComplete: isHistoryConversation ? undefined : handleNewConversationCompleted, | |||
| isPublicAPI: !isInstalledApp, | |||
| }, | |||
| ) | |||
| }, [chatList, handleNewConversationCompleted, handleSend, currentConversationId, currentConversationInputs, newConversationInputs, isInstalledApp, appId]) | |||
| }, [chatList, handleNewConversationCompleted, handleSend, isHistoryConversation, currentConversationInputs, newConversationInputs, isInstalledApp, appId]) | |||
| const doRegenerate = useCallback((chatItem: ChatItemInTree, editedQuestion?: { message: string, files?: FileEntity[] }) => { | |||
| const question = editedQuestion ? chatItem : chatList.find(item => item.id === chatItem.parentMessageId)! | |||
| @@ -159,31 +163,30 @@ const ChatWrapper = () => { | |||
| }, [chatList, doSend]) | |||
| const messageList = useMemo(() => { | |||
| if (currentConversationId) | |||
| return chatList | |||
| // Always filter out opening statement from message list as it's handled separately in welcome component | |||
| return chatList.filter(item => !item.isOpeningStatement) | |||
| }, [chatList, currentConversationId]) | |||
| }, [chatList]) | |||
| const [collapsed, setCollapsed] = useState(!!currentConversationId) | |||
| const [collapsed, setCollapsed] = useState(isHistoryConversation) | |||
| const chatNode = useMemo(() => { | |||
| if (allInputsHidden || !inputsForms.length) | |||
| return null | |||
| if (isMobile) { | |||
| if (!currentConversationId) | |||
| if (!isHistoryConversation) | |||
| return <InputsForm collapsed={collapsed} setCollapsed={setCollapsed} /> | |||
| return null | |||
| } | |||
| else { | |||
| return <InputsForm collapsed={collapsed} setCollapsed={setCollapsed} /> | |||
| } | |||
| }, [inputsForms.length, isMobile, currentConversationId, collapsed, allInputsHidden]) | |||
| }, [inputsForms.length, isMobile, isHistoryConversation, collapsed, allInputsHidden]) | |||
| const welcome = useMemo(() => { | |||
| const welcomeMessage = chatList.find(item => item.isOpeningStatement) | |||
| if (respondingState) | |||
| return null | |||
| if (currentConversationId) | |||
| if (isHistoryConversation) | |||
| return null | |||
| if (!welcomeMessage) | |||
| return null | |||
| @@ -224,7 +227,7 @@ const ChatWrapper = () => { | |||
| </div> | |||
| </div> | |||
| ) | |||
| }, [appData?.site.icon, appData?.site.icon_background, appData?.site.icon_type, appData?.site.icon_url, chatList, collapsed, currentConversationId, inputsForms.length, respondingState, allInputsHidden]) | |||
| }, [appData?.site.icon, appData?.site.icon_background, appData?.site.icon_type, appData?.site.icon_url, chatList, collapsed, isHistoryConversation, inputsForms.length, respondingState, allInputsHidden]) | |||
| const answerIcon = (appData?.site && appData.site.use_icon_as_answer_icon) | |||
| ? <AnswerIcon | |||
| @@ -248,7 +251,7 @@ const ChatWrapper = () => { | |||
| chatFooterClassName='pb-4' | |||
| chatFooterInnerClassName={`mx-auto w-full max-w-[768px] ${isMobile ? 'px-2' : 'px-4'}`} | |||
| onSend={doSend} | |||
| inputs={currentConversationId ? currentConversationInputs as any : newConversationInputs} | |||
| inputs={isHistoryConversation ? currentConversationInputs as any : newConversationInputs} | |||
| inputsForm={inputsForms} | |||
| onRegenerate={doRegenerate} | |||
| onStopResponding={handleStop} | |||