From 1d3e4844a584059d28c665eabb52a8daacec9d5f Mon Sep 17 00:00:00 2001 From: balibabu Date: Thu, 4 Jul 2024 15:10:45 +0800 Subject: [PATCH] feat: call the reset api before opening the run drawer each time #918 (#1370) ### What problem does this PR solve? feat: call the reset api before opening the run drawer each time #918 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- web/src/hooks/flow-hooks.ts | 17 +++++++++++++++ web/src/pages/flow/canvas/node/index.tsx | 9 ++++++-- web/src/pages/flow/chat/hooks.ts | 27 ++++++++++++++++-------- web/src/pages/flow/constant.tsx | 11 +++++++--- web/src/pages/flow/header/index.tsx | 2 +- 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/web/src/hooks/flow-hooks.ts b/web/src/hooks/flow-hooks.ts index c56ccf75f..d26567e4c 100644 --- a/web/src/hooks/flow-hooks.ts +++ b/web/src/hooks/flow-hooks.ts @@ -164,3 +164,20 @@ export const useRunFlow = () => { return { data, loading, runFlow: mutateAsync }; }; + +export const useResetFlow = () => { + const { id } = useParams(); + const { + data, + isPending: loading, + mutateAsync, + } = useMutation({ + mutationKey: ['resetFlow'], + mutationFn: async () => { + const { data } = await flowService.resetCanvas({ id }); + return data; + }, + }); + + return { data, loading, resetFlow: mutateAsync }; +}; diff --git a/web/src/pages/flow/canvas/node/index.tsx b/web/src/pages/flow/canvas/node/index.tsx index 94bf1d731..0d905008d 100644 --- a/web/src/pages/flow/canvas/node/index.tsx +++ b/web/src/pages/flow/canvas/node/index.tsx @@ -39,7 +39,12 @@ export function RagNode({ id="b" > - + - {data.label} + {data.label === Operator.RewriteQuestion ? 'Rewrite' : data.label} diff --git a/web/src/pages/flow/chat/hooks.ts b/web/src/pages/flow/chat/hooks.ts index 0916eb6ac..0059c0797 100644 --- a/web/src/pages/flow/chat/hooks.ts +++ b/web/src/pages/flow/chat/hooks.ts @@ -1,5 +1,5 @@ import { MessageType } from '@/constants/chat'; -import { useFetchFlow } from '@/hooks/flow-hooks'; +import { useFetchFlow, useResetFlow } from '@/hooks/flow-hooks'; import { useHandleMessageInputChange, useScrollToBottom, @@ -93,13 +93,13 @@ export const useSendMessage = ( ) => { const { id: flowId } = useParams(); const { handleInputChange, value, setValue } = useHandleMessageInputChange(); - const { data: flowDetail, refetch } = useFetchFlow(); - const messages = flowDetail.dsl.messages; + const { refetch } = useFetchFlow(); + const { resetFlow } = useResetFlow(); const { send, answer, done } = useSendMessageWithSse(api.runCanvas); const sendMessage = useCallback( - async (message: string, id?: string) => { + async (message: string) => { const params: Record = { id: flowId, }; @@ -128,6 +128,18 @@ export const useSendMessage = ( [sendMessage], ); + /** + * Call the reset api before opening the run drawer each time + */ + const resetFlowBeforeFetchingPrologue = useCallback(async () => { + // After resetting, all previous messages will be cleared. + const ret = await resetFlow(); + if (ret.retcode === 0) { + // fetch prologue + sendMessage(''); + } + }, [resetFlow, sendMessage]); + useEffect(() => { if (answer.answer) { addNewestAnswer(answer); @@ -135,11 +147,8 @@ export const useSendMessage = ( }, [answer, addNewestAnswer]); useEffect(() => { - // fetch prologue - if (messages.length === 0) { - sendMessage(''); - } - }, [sendMessage, messages]); + resetFlowBeforeFetchingPrologue(); + }, [resetFlowBeforeFetchingPrologue]); const handlePressEnter = useCallback(() => { if (done) { diff --git a/web/src/pages/flow/constant.tsx b/web/src/pages/flow/constant.tsx index 53a32dcee..8a596e6a0 100644 --- a/web/src/pages/flow/constant.tsx +++ b/web/src/pages/flow/constant.tsx @@ -1,9 +1,9 @@ import { BranchesOutlined, DatabaseOutlined, + FormOutlined, MergeCellsOutlined, MessageOutlined, - QuestionOutlined, RocketOutlined, SendOutlined, SlidersOutlined, @@ -28,7 +28,7 @@ export const operatorIconMap = { [Operator.Categorize]: DatabaseOutlined, [Operator.Message]: MessageOutlined, [Operator.Relevant]: BranchesOutlined, - [Operator.RewriteQuestion]: QuestionOutlined, + [Operator.RewriteQuestion]: FormOutlined, }; export const operatorMap = { @@ -76,7 +76,12 @@ export const operatorMap = { }, [Operator.RewriteQuestion]: { description: 'RewriteQuestion description', - backgroundColor: 'white', + backgroundColor: '#f8c7f8', + color: 'white', + width: 70, + height: 70, + fontSize: 12, + iconFontSize: 16, }, }; diff --git a/web/src/pages/flow/header/index.tsx b/web/src/pages/flow/header/index.tsx index 40b55b213..ef5c13960 100644 --- a/web/src/pages/flow/header/index.tsx +++ b/web/src/pages/flow/header/index.tsx @@ -32,7 +32,7 @@ const FlowHeader = ({ showChatDrawer }: IProps) => {