### 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)tags/v0.8.0
| return { data, loading, runFlow: mutateAsync }; | 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 }; | |||||
| }; |
| id="b" | id="b" | ||||
| ></Handle> | ></Handle> | ||||
| <Handle type="source" position={Position.Bottom} id="a" isConnectable /> | <Handle type="source" position={Position.Bottom} id="a" isConnectable /> | ||||
| <Flex vertical align="center" justify={'center'} gap={6}> | |||||
| <Flex | |||||
| vertical | |||||
| align="center" | |||||
| justify={'center'} | |||||
| gap={data.label === Operator.RewriteQuestion ? 0 : 6} | |||||
| > | |||||
| <OperatorIcon | <OperatorIcon | ||||
| name={data.label as Operator} | name={data.label as Operator} | ||||
| fontSize={style['iconFontSize'] ?? 24} | fontSize={style['iconFontSize'] ?? 24} | ||||
| className={styles.type} | className={styles.type} | ||||
| style={{ fontSize: style.fontSize ?? 14 }} | style={{ fontSize: style.fontSize ?? 14 }} | ||||
| > | > | ||||
| {data.label} | |||||
| {data.label === Operator.RewriteQuestion ? 'Rewrite' : data.label} | |||||
| </span> | </span> | ||||
| <NodeDropdown id={id}></NodeDropdown> | <NodeDropdown id={id}></NodeDropdown> | ||||
| </Flex> | </Flex> |
| import { MessageType } from '@/constants/chat'; | import { MessageType } from '@/constants/chat'; | ||||
| import { useFetchFlow } from '@/hooks/flow-hooks'; | |||||
| import { useFetchFlow, useResetFlow } from '@/hooks/flow-hooks'; | |||||
| import { | import { | ||||
| useHandleMessageInputChange, | useHandleMessageInputChange, | ||||
| useScrollToBottom, | useScrollToBottom, | ||||
| ) => { | ) => { | ||||
| const { id: flowId } = useParams(); | const { id: flowId } = useParams(); | ||||
| const { handleInputChange, value, setValue } = useHandleMessageInputChange(); | 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 { send, answer, done } = useSendMessageWithSse(api.runCanvas); | ||||
| const sendMessage = useCallback( | const sendMessage = useCallback( | ||||
| async (message: string, id?: string) => { | |||||
| async (message: string) => { | |||||
| const params: Record<string, unknown> = { | const params: Record<string, unknown> = { | ||||
| id: flowId, | id: flowId, | ||||
| }; | }; | ||||
| [sendMessage], | [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(() => { | useEffect(() => { | ||||
| if (answer.answer) { | if (answer.answer) { | ||||
| addNewestAnswer(answer); | addNewestAnswer(answer); | ||||
| }, [answer, addNewestAnswer]); | }, [answer, addNewestAnswer]); | ||||
| useEffect(() => { | useEffect(() => { | ||||
| // fetch prologue | |||||
| if (messages.length === 0) { | |||||
| sendMessage(''); | |||||
| } | |||||
| }, [sendMessage, messages]); | |||||
| resetFlowBeforeFetchingPrologue(); | |||||
| }, [resetFlowBeforeFetchingPrologue]); | |||||
| const handlePressEnter = useCallback(() => { | const handlePressEnter = useCallback(() => { | ||||
| if (done) { | if (done) { |
| import { | import { | ||||
| BranchesOutlined, | BranchesOutlined, | ||||
| DatabaseOutlined, | DatabaseOutlined, | ||||
| FormOutlined, | |||||
| MergeCellsOutlined, | MergeCellsOutlined, | ||||
| MessageOutlined, | MessageOutlined, | ||||
| QuestionOutlined, | |||||
| RocketOutlined, | RocketOutlined, | ||||
| SendOutlined, | SendOutlined, | ||||
| SlidersOutlined, | SlidersOutlined, | ||||
| [Operator.Categorize]: DatabaseOutlined, | [Operator.Categorize]: DatabaseOutlined, | ||||
| [Operator.Message]: MessageOutlined, | [Operator.Message]: MessageOutlined, | ||||
| [Operator.Relevant]: BranchesOutlined, | [Operator.Relevant]: BranchesOutlined, | ||||
| [Operator.RewriteQuestion]: QuestionOutlined, | |||||
| [Operator.RewriteQuestion]: FormOutlined, | |||||
| }; | }; | ||||
| export const operatorMap = { | export const operatorMap = { | ||||
| }, | }, | ||||
| [Operator.RewriteQuestion]: { | [Operator.RewriteQuestion]: { | ||||
| description: 'RewriteQuestion description', | description: 'RewriteQuestion description', | ||||
| backgroundColor: 'white', | |||||
| backgroundColor: '#f8c7f8', | |||||
| color: 'white', | |||||
| width: 70, | |||||
| height: 70, | |||||
| fontSize: 12, | |||||
| iconFontSize: 16, | |||||
| }, | }, | ||||
| }; | }; | ||||
| </Space> | </Space> | ||||
| <Space size={'large'}> | <Space size={'large'}> | ||||
| <Button onClick={showChatDrawer}> | <Button onClick={showChatDrawer}> | ||||
| <b>Debug</b> | |||||
| <b>Run</b> | |||||
| </Button> | </Button> | ||||
| <Button type="primary" onClick={saveGraph}> | <Button type="primary" onClick={saveGraph}> | ||||
| <b>Save</b> | <b>Save</b> |