From 1b7f87752a5a31e6cdc086c590be182c5c44fff9 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Thu, 7 Sep 2023 17:06:17 +0800 Subject: [PATCH] feat: retry send --- client/public/locales/en/common.json | 9 ++- client/public/locales/zh/common.json | 9 ++- .../components/ChatBox/WholeResponseModal.tsx | 2 +- client/src/components/ChatBox/index.tsx | 60 ++++++++++++++----- .../src/components/Icon/icons/light/retry.svg | 8 +++ client/src/components/Icon/index.tsx | 3 +- .../account/components/UpdatePswModal.tsx | 2 +- client/src/service/models/chatItem.ts | 19 +----- client/src/utils/sse.ts | 2 +- 9 files changed, 67 insertions(+), 47 deletions(-) create mode 100644 client/src/components/Icon/icons/light/retry.svg diff --git a/client/public/locales/en/common.json b/client/public/locales/en/common.json index 09f9a5ff8..3b7369b61 100644 --- a/client/public/locales/en/common.json +++ b/client/public/locales/en/common.json @@ -63,10 +63,8 @@ "online": "Online Chat", "share": "Share", "test": "Test Chat " - } - }, - "commom": { - "Password inconsistency": "Password inconsistency" + }, + "retry": "Retry" }, "common": { "Add": "Add", @@ -80,7 +78,8 @@ "Filed is repeated": "", "Input": "Input", "Output": "Output", - "export": "" + "export": "", + "Password inconsistency": "Password inconsistency" }, "dataset": { "Confirm to delete the data": "Confirm to delete the data?", diff --git a/client/public/locales/zh/common.json b/client/public/locales/zh/common.json index 54c848082..867bdb861 100644 --- a/client/public/locales/zh/common.json +++ b/client/public/locales/zh/common.json @@ -63,10 +63,8 @@ "online": "在线使用", "share": "外部链接调用", "test": "测试" - } - }, - "commom": { - "Password inconsistency": "两次密码不一致" + }, + "retry": "重新生成" }, "common": { "Add": "添加", @@ -80,7 +78,8 @@ "Filed is repeated": "字段重复了", "Input": "输入", "Output": "输出", - "export": "" + "export": "", + "Password inconsistency": "两次密码不一致" }, "dataset": { "Confirm to delete the data": "确认删除该数据?", diff --git a/client/src/components/ChatBox/WholeResponseModal.tsx b/client/src/components/ChatBox/WholeResponseModal.tsx index 7c5ee447f..e16e0aadd 100644 --- a/client/src/components/ChatBox/WholeResponseModal.tsx +++ b/client/src/components/ChatBox/WholeResponseModal.tsx @@ -1,5 +1,5 @@ import React, { useMemo } from 'react'; -import { Box, ModalBody, useTheme, ModalHeader, Flex } from '@chakra-ui/react'; +import { Box, ModalBody, useTheme, Flex } from '@chakra-ui/react'; import type { ChatHistoryItemResType } from '@/types/chat'; import { useTranslation } from 'react-i18next'; diff --git a/client/src/components/ChatBox/index.tsx b/client/src/components/ChatBox/index.tsx index d7ecff366..86b8255b7 100644 --- a/client/src/components/ChatBox/index.tsx +++ b/client/src/components/ChatBox/index.tsx @@ -293,7 +293,7 @@ const ChatBox = ( * user confirm send prompt */ const sendPrompt = useCallback( - async (variables: Record = {}, inputVal = '') => { + async (variables: Record = {}, inputVal = '', history = chatHistory) => { if (!onStartChat) return; if (isChatting) { toast({ @@ -314,7 +314,7 @@ const ChatBox = ( } const newChatList: ChatSiteItemType[] = [ - ...chatHistory, + ...history, { dataId: nanoid(), obj: 'Human', @@ -407,6 +407,22 @@ const ChatBox = ( ] ); + // retry input + const retryInput = useCallback( + async (index: number) => { + if (!onDelMessage) return; + const delHistory = chatHistory.slice(index); + setChatHistory((state) => (index === 0 ? [] : state.slice(0, index))); + + await Promise.all( + delHistory.map((item, i) => onDelMessage({ contentId: item.dataId, index: index + i })) + ); + + sendPrompt(variables, delHistory[0].value, chatHistory.slice(0, index)); + }, + [chatHistory, onDelMessage, sendPrompt, variables] + ); + // output data useImperativeHandle(ref, () => ({ getChatHistory: () => chatHistory, @@ -470,7 +486,7 @@ const ChatBox = ( ); const statusBoxData = useMemo(() => { const colorMap = { - loading: '#67c13b', + loading: 'myGray.700', running: '#67c13b', finish: 'myBlue.600' }; @@ -484,6 +500,7 @@ const ChatBox = ( }; }, [chatHistory, isChatting, t]); + // page change and abort request useEffect(() => { return () => { controller.current?.abort('leave'); @@ -492,16 +509,7 @@ const ChatBox = ( }; }, [router.query]); - useEffect(() => { - event.on('guideClick', ({ text }: { text: string }) => { - if (!text) return; - handleSubmit((data) => sendPrompt(data, text))(); - }); - - return () => { - event.off('guideClick'); - }; - }, [handleSubmit, sendPrompt]); + // page destroy and abort request useEffect(() => { const listen = () => { cancelBroadcast(); @@ -513,6 +521,18 @@ const ChatBox = ( }; }, []); + // add guide text listener + useEffect(() => { + event.on('guideClick', ({ text }: { text: string }) => { + if (!text) return; + handleSubmit((data) => sendPrompt(data, text))(); + }); + + return () => { + event.off('guideClick'); + }; + }, [handleSubmit, sendPrompt]); + return ( @@ -616,7 +636,7 @@ const ChatBox = ( justifyContent={'flex-end'} mr={3} > - + onclickCopy(item.value)} /> + {!!onDelMessage && ( + + retryInput(index)} + /> + + )} {onDelMessage && ( - + + + + \ No newline at end of file diff --git a/client/src/components/Icon/index.tsx b/client/src/components/Icon/index.tsx index 99bd99751..1f5edea13 100644 --- a/client/src/components/Icon/index.tsx +++ b/client/src/components/Icon/index.tsx @@ -79,7 +79,8 @@ const map = { promotionLight: require('./icons/light/promotion.svg').default, logsLight: require('./icons/light/logs.svg').default, badLight: require('./icons/light/bad.svg').default, - markLight: require('./icons/light/mark.svg').default + markLight: require('./icons/light/mark.svg').default, + retryLight: require('./icons/light/retry.svg').default }; export type IconName = keyof typeof map; diff --git a/client/src/pages/account/components/UpdatePswModal.tsx b/client/src/pages/account/components/UpdatePswModal.tsx index 0c02bea90..ed5e0b912 100644 --- a/client/src/pages/account/components/UpdatePswModal.tsx +++ b/client/src/pages/account/components/UpdatePswModal.tsx @@ -25,7 +25,7 @@ const UpdatePswModal = ({ onClose }: { onClose: () => void }) => { const { mutate: onSubmit, isLoading } = useRequest({ mutationFn: (data: FormType) => { if (data.newPsw !== data.confirmPsw) { - return Promise.reject(t('commom.Password inconsistency')); + return Promise.reject(t('common.Password inconsistency')); } return updatePasswordByOld(data); }, diff --git a/client/src/service/models/chatItem.ts b/client/src/service/models/chatItem.ts index 3aa4a97c8..803882ab9 100644 --- a/client/src/service/models/chatItem.ts +++ b/client/src/service/models/chatItem.ts @@ -48,24 +48,7 @@ const ChatItemSchema = new Schema({ } }, [TaskResponseKeyEnum.responseData]: { - type: [ - { - moduleName: String, - price: String, - model: String, - tokens: Number, - question: String, - answer: String, - temperature: Number, - maxToken: Number, - quoteList: Array, - completeMessages: Array, - similarity: Number, - limit: Number, - cqList: Array, - cqResult: String - } - ], + type: Array, default: [] } }); diff --git a/client/src/utils/sse.ts b/client/src/utils/sse.ts index 75f3b0edd..275fbe457 100644 --- a/client/src/utils/sse.ts +++ b/client/src/utils/sse.ts @@ -44,7 +44,7 @@ export class SSEParseData { data: parseData }; } catch (error) { - if (typeof item.data === 'string' && !item.data.startsWith(": ping")) { + if (typeof item.data === 'string' && !item.data.startsWith(': ping')) { this.storeReadData += item.data; } else { this.storeReadData = '';