Commit 7e29ddff authored by Joel's avatar Joel

Merge branch 'feat/universal-chat-fe' into deploy/dev

parents 1bb7ab72 b2160c71
import { useState } from 'react' import { useState } from 'react'
import produce from 'immer' import produce from 'immer'
import { useGetState } from 'ahooks'
import type { ConversationItem } from '@/models/share' import type { ConversationItem } from '@/models/share'
const storageConversationIdKey = 'conversationIdInfo' const storageConversationIdKey = 'conversationIdInfo'
...@@ -8,7 +9,7 @@ type ConversationInfoType = Omit<ConversationItem, 'inputs' | 'id'> ...@@ -8,7 +9,7 @@ type ConversationInfoType = Omit<ConversationItem, 'inputs' | 'id'>
function useConversation() { function useConversation() {
const [conversationList, setConversationList] = useState<ConversationItem[]>([]) const [conversationList, setConversationList] = useState<ConversationItem[]>([])
const [pinnedConversationList, setPinnedConversationList] = useState<ConversationItem[]>([]) const [pinnedConversationList, setPinnedConversationList] = useState<ConversationItem[]>([])
const [currConversationId, doSetCurrConversationId] = useState<string>('-1') const [currConversationId, doSetCurrConversationId, getCurrConversationId] = useGetState<string>('-1')
// when set conversation id, we do not have set appId // when set conversation id, we do not have set appId
const setCurrConversationId = (id: string, appId: string, isSetToLocalStroge = true, newConversationName = '') => { const setCurrConversationId = (id: string, appId: string, isSetToLocalStroge = true, newConversationName = '') => {
doSetCurrConversationId(id) doSetCurrConversationId(id)
...@@ -53,6 +54,7 @@ function useConversation() { ...@@ -53,6 +54,7 @@ function useConversation() {
pinnedConversationList, pinnedConversationList,
setPinnedConversationList, setPinnedConversationList,
currConversationId, currConversationId,
getCurrConversationId,
setCurrConversationId, setCurrConversationId,
getConversationIdFromStorage, getConversationIdFromStorage,
isNewConversation, isNewConversation,
......
...@@ -73,7 +73,6 @@ const Main: FC<IMainProps> = () => { ...@@ -73,7 +73,6 @@ const Main: FC<IMainProps> = () => {
const [inited, setInited] = useState<boolean>(false) const [inited, setInited] = useState<boolean>(false)
// in mobile, show sidebar by click button // in mobile, show sidebar by click button
const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false) const [isShowSidebar, { setTrue: showSidebar, setFalse: hideSidebar }] = useBoolean(false)
/* /*
* conversation info * conversation info
*/ */
...@@ -86,6 +85,7 @@ const Main: FC<IMainProps> = () => { ...@@ -86,6 +85,7 @@ const Main: FC<IMainProps> = () => {
pinnedConversationList, pinnedConversationList,
setPinnedConversationList, setPinnedConversationList,
currConversationId, currConversationId,
getCurrConversationId,
setCurrConversationId, setCurrConversationId,
getConversationIdFromStorage, getConversationIdFromStorage,
isNewConversation, isNewConversation,
...@@ -219,7 +219,7 @@ const Main: FC<IMainProps> = () => { ...@@ -219,7 +219,7 @@ const Main: FC<IMainProps> = () => {
} }
// update chat list of current conversation // update chat list of current conversation
if (!isNewConversation && !conversationIdChangeBecauseOfNew && !isResponsing) { if (!isNewConversation && !conversationIdChangeBecauseOfNew) {
fetchChatList(currConversationId).then((res: any) => { fetchChatList(currConversationId).then((res: any) => {
const { data } = res const { data } = res
const newChatList: IChatItem[] = generateNewChatListWithOpenstatement(notSyncToStateIntroduction, notSyncToStateInputs) const newChatList: IChatItem[] = generateNewChatListWithOpenstatement(notSyncToStateIntroduction, notSyncToStateInputs)
...@@ -413,6 +413,7 @@ const Main: FC<IMainProps> = () => { ...@@ -413,6 +413,7 @@ const Main: FC<IMainProps> = () => {
const [messageTaskId, setMessageTaskId] = useState('') const [messageTaskId, setMessageTaskId] = useState('')
const [hasStopResponded, setHasStopResponded, getHasStopResponded] = useGetState(false) const [hasStopResponded, setHasStopResponded, getHasStopResponded] = useGetState(false)
const [errorHappened, setErrorHappened] = useState(false) const [errorHappened, setErrorHappened] = useState(false)
const [isResponsingConIsCurrCon, setIsResponsingConCurrCon, getIsResponsingConIsCurrCon] = useGetState(true)
const handleSend = async (message: string) => { const handleSend = async (message: string) => {
if (isResponsing) { if (isResponsing) {
notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') })
...@@ -471,6 +472,7 @@ const Main: FC<IMainProps> = () => { ...@@ -471,6 +472,7 @@ const Main: FC<IMainProps> = () => {
setResponsingTrue() setResponsingTrue()
setErrorHappened(false) setErrorHappened(false)
setIsShowSuggestion(false) setIsShowSuggestion(false)
setIsResponsingConCurrCon(true)
sendChatMessage(data, { sendChatMessage(data, {
getAbortController: (abortController) => { getAbortController: (abortController) => {
...@@ -483,6 +485,13 @@ const Main: FC<IMainProps> = () => { ...@@ -483,6 +485,13 @@ const Main: FC<IMainProps> = () => {
tempNewConversationId = newConversationId tempNewConversationId = newConversationId
setMessageTaskId(taskId) setMessageTaskId(taskId)
// has switched to other conversation
if (tempNewConversationId !== getCurrConversationId()) {
setIsResponsingConCurrCon(false)
return
}
// closesure new list is outdated. // closesure new list is outdated.
const newListWithAnswer = produce( const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
...@@ -492,6 +501,7 @@ const Main: FC<IMainProps> = () => { ...@@ -492,6 +501,7 @@ const Main: FC<IMainProps> = () => {
draft.push({ ...responseItem }) draft.push({ ...responseItem })
}) })
setChatList(newListWithAnswer) setChatList(newListWithAnswer)
}, },
async onCompleted(hasError?: boolean) { async onCompleted(hasError?: boolean) {
...@@ -508,7 +518,7 @@ const Main: FC<IMainProps> = () => { ...@@ -508,7 +518,7 @@ const Main: FC<IMainProps> = () => {
setConversationIdChangeBecauseOfNew(false) setConversationIdChangeBecauseOfNew(false)
resetNewConversationInputs() resetNewConversationInputs()
setCurrConversationId(tempNewConversationId, APP_ID, true) setCurrConversationId(tempNewConversationId, APP_ID, true)
if (suggestedQuestionsAfterAnswerConfig?.enabled && !getHasStopResponded()) { if (getIsResponsingConIsCurrCon() && suggestedQuestionsAfterAnswerConfig?.enabled && !getHasStopResponded()) {
const { data }: any = await fetchSuggestedQuestions(responseItem.id) const { data }: any = await fetchSuggestedQuestions(responseItem.id)
setSuggestQuestions(data) setSuggestQuestions(data)
setIsShowSuggestion(true) setIsShowSuggestion(true)
...@@ -519,6 +529,11 @@ const Main: FC<IMainProps> = () => { ...@@ -519,6 +529,11 @@ const Main: FC<IMainProps> = () => {
// thought finished then start to return message. Warning: use push agent_thoughts.push would caused problem when the thought is more then 2 // thought finished then start to return message. Warning: use push agent_thoughts.push would caused problem when the thought is more then 2
responseItem.id = thought.message_id; responseItem.id = thought.message_id;
(responseItem as any).agent_thoughts = [...(responseItem as any).agent_thoughts, thought] // .push(thought) (responseItem as any).agent_thoughts = [...(responseItem as any).agent_thoughts, thought] // .push(thought)
// has switched to other conversation
if (tempNewConversationId !== getCurrConversationId()) {
setIsResponsingConCurrCon(false)
return
}
const newListWithAnswer = produce( const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
(draft) => { (draft) => {
...@@ -675,7 +690,7 @@ const Main: FC<IMainProps> = () => { ...@@ -675,7 +690,7 @@ const Main: FC<IMainProps> = () => {
isHideFeedbackEdit isHideFeedbackEdit
onFeedback={handleFeedback} onFeedback={handleFeedback}
isResponsing={isResponsing} isResponsing={isResponsing}
canStopResponsing={!!messageTaskId} canStopResponsing={!!messageTaskId && isResponsingConIsCurrCon}
abortResponsing={async () => { abortResponsing={async () => {
await stopChatMessageResponding(messageTaskId) await stopChatMessageResponding(messageTaskId)
setHasStopResponded(true) setHasStopResponded(true)
......
import { useState } from 'react' import { useState } from 'react'
import produce from 'immer' import produce from 'immer'
import { useGetState } from 'ahooks'
import type { ConversationItem } from '@/models/share' import type { ConversationItem } from '@/models/share'
const storageConversationIdKey = 'conversationIdInfo' const storageConversationIdKey = 'conversationIdInfo'
...@@ -8,7 +9,7 @@ type ConversationInfoType = Omit<ConversationItem, 'inputs' | 'id'> ...@@ -8,7 +9,7 @@ type ConversationInfoType = Omit<ConversationItem, 'inputs' | 'id'>
function useConversation() { function useConversation() {
const [conversationList, setConversationList] = useState<ConversationItem[]>([]) const [conversationList, setConversationList] = useState<ConversationItem[]>([])
const [pinnedConversationList, setPinnedConversationList] = useState<ConversationItem[]>([]) const [pinnedConversationList, setPinnedConversationList] = useState<ConversationItem[]>([])
const [currConversationId, doSetCurrConversationId] = useState<string>('-1') const [currConversationId, doSetCurrConversationId, getCurrConversationId] = useGetState<string>('-1')
// when set conversation id, we do not have set appId // when set conversation id, we do not have set appId
const setCurrConversationId = (id: string, appId: string, isSetToLocalStroge = true, newConversationName = '') => { const setCurrConversationId = (id: string, appId: string, isSetToLocalStroge = true, newConversationName = '') => {
doSetCurrConversationId(id) doSetCurrConversationId(id)
...@@ -53,6 +54,7 @@ function useConversation() { ...@@ -53,6 +54,7 @@ function useConversation() {
pinnedConversationList, pinnedConversationList,
setPinnedConversationList, setPinnedConversationList,
currConversationId, currConversationId,
getCurrConversationId,
setCurrConversationId, setCurrConversationId,
getConversationIdFromStorage, getConversationIdFromStorage,
isNewConversation, isNewConversation,
......
...@@ -90,6 +90,7 @@ const Main: FC<IMainProps> = ({ ...@@ -90,6 +90,7 @@ const Main: FC<IMainProps> = ({
pinnedConversationList, pinnedConversationList,
setPinnedConversationList, setPinnedConversationList,
currConversationId, currConversationId,
getCurrConversationId,
setCurrConversationId, setCurrConversationId,
getConversationIdFromStorage, getConversationIdFromStorage,
isNewConversation, isNewConversation,
...@@ -214,7 +215,7 @@ const Main: FC<IMainProps> = ({ ...@@ -214,7 +215,7 @@ const Main: FC<IMainProps> = ({
} }
// update chat list of current conversation // update chat list of current conversation
if (!isNewConversation && !conversationIdChangeBecauseOfNew && !isResponsing) { if (!isNewConversation && !conversationIdChangeBecauseOfNew) {
fetchChatList(currConversationId, isInstalledApp, installedAppInfo?.id).then((res: any) => { fetchChatList(currConversationId, isInstalledApp, installedAppInfo?.id).then((res: any) => {
const { data } = res const { data } = res
const newChatList: IChatItem[] = generateNewChatListWithOpenstatement(notSyncToStateIntroduction, notSyncToStateInputs) const newChatList: IChatItem[] = generateNewChatListWithOpenstatement(notSyncToStateIntroduction, notSyncToStateInputs)
...@@ -423,6 +424,7 @@ const Main: FC<IMainProps> = ({ ...@@ -423,6 +424,7 @@ const Main: FC<IMainProps> = ({
const [suggestQuestions, setSuggestQuestions] = useState<string[]>([]) const [suggestQuestions, setSuggestQuestions] = useState<string[]>([])
const [messageTaskId, setMessageTaskId] = useState('') const [messageTaskId, setMessageTaskId] = useState('')
const [hasStopResponded, setHasStopResponded, getHasStopResponded] = useGetState(false) const [hasStopResponded, setHasStopResponded, getHasStopResponded] = useGetState(false)
const [isResponsingConIsCurrCon, setIsResponsingConCurrCon, getIsResponsingConIsCurrCon] = useGetState(true)
const handleSend = async (message: string) => { const handleSend = async (message: string) => {
if (isResponsing) { if (isResponsing) {
...@@ -465,6 +467,7 @@ const Main: FC<IMainProps> = ({ ...@@ -465,6 +467,7 @@ const Main: FC<IMainProps> = ({
setHasStopResponded(false) setHasStopResponded(false)
setResponsingTrue() setResponsingTrue()
setIsShowSuggestion(false) setIsShowSuggestion(false)
setIsResponsingConCurrCon(true)
sendChatMessage(data, { sendChatMessage(data, {
getAbortController: (abortController) => { getAbortController: (abortController) => {
setAbortController(abortController) setAbortController(abortController)
...@@ -476,6 +479,11 @@ const Main: FC<IMainProps> = ({ ...@@ -476,6 +479,11 @@ const Main: FC<IMainProps> = ({
tempNewConversationId = newConversationId tempNewConversationId = newConversationId
setMessageTaskId(taskId) setMessageTaskId(taskId)
// has switched to other conversation
if (tempNewConversationId !== getCurrConversationId()) {
setIsResponsingConCurrCon(false)
return
}
// closesure new list is outdated. // closesure new list is outdated.
const newListWithAnswer = produce( const newListWithAnswer = produce(
getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId), getChatList().filter(item => item.id !== responseItem.id && item.id !== placeholderAnswerId),
...@@ -501,7 +509,7 @@ const Main: FC<IMainProps> = ({ ...@@ -501,7 +509,7 @@ const Main: FC<IMainProps> = ({
resetNewConversationInputs() resetNewConversationInputs()
setChatNotStarted() setChatNotStarted()
setCurrConversationId(tempNewConversationId, appId, true) setCurrConversationId(tempNewConversationId, appId, true)
if (suggestedQuestionsAfterAnswerConfig?.enabled && !getHasStopResponded()) { if (getIsResponsingConIsCurrCon() && suggestedQuestionsAfterAnswerConfig?.enabled && !getHasStopResponded()) {
const { data }: any = await fetchSuggestedQuestions(responseItem.id, isInstalledApp, installedAppInfo?.id) const { data }: any = await fetchSuggestedQuestions(responseItem.id, isInstalledApp, installedAppInfo?.id)
setSuggestQuestions(data) setSuggestQuestions(data)
setIsShowSuggestion(true) setIsShowSuggestion(true)
...@@ -630,7 +638,7 @@ const Main: FC<IMainProps> = ({ ...@@ -630,7 +638,7 @@ const Main: FC<IMainProps> = ({
isHideFeedbackEdit isHideFeedbackEdit
onFeedback={handleFeedback} onFeedback={handleFeedback}
isResponsing={isResponsing} isResponsing={isResponsing}
canStopResponsing={!!messageTaskId} canStopResponsing={!!messageTaskId && isResponsingConIsCurrCon}
abortResponsing={async () => { abortResponsing={async () => {
await stopChatMessageResponding(appId, messageTaskId, isInstalledApp, installedAppInfo?.id) await stopChatMessageResponding(appId, messageTaskId, isInstalledApp, installedAppInfo?.id)
setHasStopResponded(true) setHasStopResponded(true)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment