| const [abortController, setAbortController] = useState<AbortController | null>(null) | const [abortController, setAbortController] = useState<AbortController | null>(null) | ||||
| const [isShowFormattingChangeConfirm, setIsShowFormattingChangeConfirm] = useState(false) | const [isShowFormattingChangeConfirm, setIsShowFormattingChangeConfirm] = useState(false) | ||||
| const [isShowSuggestion, setIsShowSuggestion] = useState(false) | const [isShowSuggestion, setIsShowSuggestion] = useState(false) | ||||
| const [messageTaskId, setMessageTaskId] = useState('') | |||||
| const [hasStopResponded, setHasStopResponded, getHasStopResponded] = useGetState(false) | |||||
| useEffect(() => { | useEffect(() => { | ||||
| if (formattingChanged && chatList.some(item => !item.isAnswer)) | if (formattingChanged && chatList.some(item => !item.isAnswer)) | ||||
| setFormattingChanged(false) | setFormattingChanged(false) | ||||
| }, [formattingChanged]) | }, [formattingChanged]) | ||||
| const clearConversation = () => { | |||||
| const clearConversation = async () => { | |||||
| setConversationId(null) | setConversationId(null) | ||||
| abortController?.abort() | abortController?.abort() | ||||
| setResponsingFalse() | setResponsingFalse() | ||||
| const doShowSuggestion = isShowSuggestion && !isResponsing | const doShowSuggestion = isShowSuggestion && !isResponsing | ||||
| const [suggestQuestions, setSuggestQuestions] = useState<string[]>([]) | const [suggestQuestions, setSuggestQuestions] = useState<string[]>([]) | ||||
| const [messageTaskId, setMessageTaskId] = useState('') | |||||
| const onSend = async (message: string) => { | const onSend = async (message: string) => { | ||||
| if (isResponsing) { | if (isResponsing) { | ||||
| notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) | notify({ type: 'info', message: t('appDebug.errorMessage.waitForResponse') }) | ||||
| let _newConversationId: null | string = null | let _newConversationId: null | string = null | ||||
| setHasStopResponded(false) | |||||
| setResponsingTrue() | setResponsingTrue() | ||||
| setIsShowSuggestion(false) | setIsShowSuggestion(false) | ||||
| sendChatMessage(appId, data, { | sendChatMessage(appId, data, { | ||||
| } | } | ||||
| })) | })) | ||||
| } | } | ||||
| if (suggestedQuestionsAfterAnswerConfig.enabled) { | |||||
| if (suggestedQuestionsAfterAnswerConfig.enabled && !getHasStopResponded()) { | |||||
| const { data }: any = await fetchSuggestedQuestions(appId, responseItem.id) | const { data }: any = await fetchSuggestedQuestions(appId, responseItem.id) | ||||
| setSuggestQuestions(data) | setSuggestQuestions(data) | ||||
| setIsShowSuggestion(true) | setIsShowSuggestion(true) | ||||
| isResponsing={isResponsing} | isResponsing={isResponsing} | ||||
| canStopResponsing={!!messageTaskId} | canStopResponsing={!!messageTaskId} | ||||
| abortResponsing={async () => { | abortResponsing={async () => { | ||||
| abortController?.abort() | |||||
| await stopChatMessageResponding(appId, messageTaskId) | await stopChatMessageResponding(appId, messageTaskId) | ||||
| setHasStopResponded(true) | |||||
| setResponsingFalse() | setResponsingFalse() | ||||
| }} | }} | ||||
| isShowSuggestion={doShowSuggestion} | isShowSuggestion={doShowSuggestion} | ||||
| className="mt-2" | className="mt-2" | ||||
| content={completionRes} | content={completionRes} | ||||
| isLoading={!completionRes && isResponsing} | isLoading={!completionRes && isResponsing} | ||||
| isInstalledApp={false} | |||||
| /> | /> | ||||
| )} | )} | ||||
| </div> | </div> |
| }, [chatList, currConversationId]) | }, [chatList, currConversationId]) | ||||
| // user can not edit inputs if user had send message | // user can not edit inputs if user had send message | ||||
| const canEditInpus = !chatList.some(item => item.isAnswer === false) && isNewConversation | const canEditInpus = !chatList.some(item => item.isAnswer === false) && isNewConversation | ||||
| const createNewChat = () => { | |||||
| const createNewChat = async () => { | |||||
| // if new chat is already exist, do not create new chat | // if new chat is already exist, do not create new chat | ||||
| abortController?.abort() | abortController?.abort() | ||||
| setResponsingFalse() | setResponsingFalse() | ||||
| const doShowSuggestion = isShowSuggestion && !isResponsing | const doShowSuggestion = isShowSuggestion && !isResponsing | ||||
| 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 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') }) | ||||
| let tempNewConversationId = '' | let tempNewConversationId = '' | ||||
| setHasStopResponded(false) | |||||
| setResponsingTrue() | setResponsingTrue() | ||||
| setIsShowSuggestion(false) | setIsShowSuggestion(false) | ||||
| sendChatMessage(data, { | sendChatMessage(data, { | ||||
| resetNewConversationInputs() | resetNewConversationInputs() | ||||
| setChatNotStarted() | setChatNotStarted() | ||||
| setCurrConversationId(tempNewConversationId, appId, true) | setCurrConversationId(tempNewConversationId, appId, true) | ||||
| if (suggestedQuestionsAfterAnswerConfig?.enabled) { | |||||
| if (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) | ||||
| isResponsing={isResponsing} | isResponsing={isResponsing} | ||||
| canStopResponsing={!!messageTaskId} | canStopResponsing={!!messageTaskId} | ||||
| abortResponsing={async () => { | abortResponsing={async () => { | ||||
| abortController?.abort() | |||||
| await stopChatMessageResponding(appId, messageTaskId, isInstalledApp, installedAppInfo?.id) | await stopChatMessageResponding(appId, messageTaskId, isInstalledApp, installedAppInfo?.id) | ||||
| setHasStopResponded(true) | |||||
| setResponsingFalse() | setResponsingFalse() | ||||
| }} | }} | ||||
| checkCanSend={checkCanSend} | checkCanSend={checkCanSend} |