### What problem does this PR solve? feat: save graph data before opening the debug drawer #918 ### Type of change - [x] New Feature (non-breaking change which adds functionality)tags/v0.8.0
| @@ -1,5 +1,5 @@ | |||
| import { MessageType } from '@/constants/chat'; | |||
| import { useFetchFlow, useResetFlow } from '@/hooks/flow-hooks'; | |||
| import { useFetchFlow } from '@/hooks/flow-hooks'; | |||
| import { | |||
| useHandleMessageInputChange, | |||
| useScrollToBottom, | |||
| @@ -12,6 +12,7 @@ import { message } from 'antd'; | |||
| import { useCallback, useEffect, useState } from 'react'; | |||
| import { useParams } from 'umi'; | |||
| import { v4 as uuid } from 'uuid'; | |||
| import { receiveMessageError } from '../utils'; | |||
| const antMessage = message; | |||
| @@ -94,7 +95,6 @@ export const useSendMessage = ( | |||
| const { id: flowId } = useParams(); | |||
| const { handleInputChange, value, setValue } = useHandleMessageInputChange(); | |||
| const { refetch } = useFetchFlow(); | |||
| const { resetFlow } = useResetFlow(); | |||
| const { send, answer, done } = useSendMessageWithSse(api.runCanvas); | |||
| @@ -108,7 +108,7 @@ export const useSendMessage = ( | |||
| } | |||
| const res = await send(params); | |||
| if (res && (res?.response.status !== 200 || res?.data?.retcode !== 0)) { | |||
| if (receiveMessageError(res)) { | |||
| antMessage.error(res?.data?.retmsg); | |||
| // cancel loading | |||
| @@ -128,28 +128,12 @@ 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); | |||
| } | |||
| }, [answer, addNewestAnswer]); | |||
| useEffect(() => { | |||
| resetFlowBeforeFetchingPrologue(); | |||
| }, [resetFlowBeforeFetchingPrologue]); | |||
| const handlePressEnter = useCallback(() => { | |||
| if (done) { | |||
| setValue(''); | |||
| @@ -3,7 +3,7 @@ import { Button, Flex, Space } from 'antd'; | |||
| import { useFetchFlow } from '@/hooks/flow-hooks'; | |||
| import { ArrowLeftOutlined } from '@ant-design/icons'; | |||
| import { Link } from 'umi'; | |||
| import { useSaveGraph } from '../hooks'; | |||
| import { useSaveGraph, useSaveGraphBeforeOpeningDebugDrawer } from '../hooks'; | |||
| import styles from './index.less'; | |||
| @@ -13,7 +13,7 @@ interface IProps { | |||
| const FlowHeader = ({ showChatDrawer }: IProps) => { | |||
| const { saveGraph } = useSaveGraph(); | |||
| const handleRun = useSaveGraphBeforeOpeningDebugDrawer(showChatDrawer); | |||
| const { data } = useFetchFlow(); | |||
| return ( | |||
| @@ -31,7 +31,7 @@ const FlowHeader = ({ showChatDrawer }: IProps) => { | |||
| <h3>{data.title}</h3> | |||
| </Space> | |||
| <Space size={'large'}> | |||
| <Button onClick={showChatDrawer}> | |||
| <Button onClick={handleRun}> | |||
| <b>Run</b> | |||
| </Button> | |||
| <Button type="primary" onClick={saveGraph}> | |||
| @@ -1,5 +1,5 @@ | |||
| import { useSetModalState } from '@/hooks/commonHooks'; | |||
| import { useFetchFlow, useSetFlow } from '@/hooks/flow-hooks'; | |||
| import { useFetchFlow, useResetFlow, useSetFlow } from '@/hooks/flow-hooks'; | |||
| import { useFetchLlmList } from '@/hooks/llmHooks'; | |||
| import { IGraph } from '@/interfaces/database/flow'; | |||
| import { useIsFetching } from '@tanstack/react-query'; | |||
| @@ -17,8 +17,9 @@ import { | |||
| ModelVariableType, | |||
| settledModelVariableMap, | |||
| } from '@/constants/knowledge'; | |||
| import { useFetchModelId } from '@/hooks/logicHooks'; | |||
| import { useFetchModelId, useSendMessageWithSse } from '@/hooks/logicHooks'; | |||
| import { Variable } from '@/interfaces/database/chat'; | |||
| import api from '@/utils/api'; | |||
| import { useDebounceEffect } from 'ahooks'; | |||
| import { FormInstance, message } from 'antd'; | |||
| import { humanId } from 'human-id'; | |||
| @@ -37,7 +38,7 @@ import { | |||
| initialRewriteQuestionValues, | |||
| } from './constant'; | |||
| import useGraphStore, { RFState } from './store'; | |||
| import { buildDslComponentsByGraph } from './utils'; | |||
| import { buildDslComponentsByGraph, receiveMessageError } from './utils'; | |||
| const selector = (state: RFState) => ({ | |||
| nodes: state.nodes, | |||
| @@ -193,9 +194,9 @@ export const useSaveGraph = () => { | |||
| const { setFlow } = useSetFlow(); | |||
| const { id } = useParams(); | |||
| const { nodes, edges } = useGraphStore((state) => state); | |||
| const saveGraph = useCallback(() => { | |||
| const saveGraph = useCallback(async () => { | |||
| const dslComponents = buildDslComponentsByGraph(nodes, edges); | |||
| setFlow({ | |||
| return setFlow({ | |||
| id, | |||
| title: data.title, | |||
| dsl: { ...data.dsl, graph: { nodes, edges }, components: dslComponents }, | |||
| @@ -345,3 +346,29 @@ export const useHandleNodeNameChange = (node?: Node) => { | |||
| return { name, handleNameBlur, handleNameChange }; | |||
| }; | |||
| export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => { | |||
| const { id } = useParams(); | |||
| const { saveGraph } = useSaveGraph(); | |||
| const { resetFlow } = useResetFlow(); | |||
| const { send } = useSendMessageWithSse(api.runCanvas); | |||
| const handleRun = useCallback(async () => { | |||
| const saveRet = await saveGraph(); | |||
| if (saveRet?.retcode === 0) { | |||
| // Call the reset api before opening the run drawer each time | |||
| const resetRet = await resetFlow(); | |||
| // After resetting, all previous messages will be cleared. | |||
| if (resetRet?.retcode === 0) { | |||
| // fetch prologue | |||
| const sendRet = await send({ id }); | |||
| if (receiveMessageError(sendRet)) { | |||
| message.error(sendRet?.data?.retmsg); | |||
| } else { | |||
| show(); | |||
| } | |||
| } | |||
| } | |||
| }, [saveGraph, resetFlow, id, send, show]); | |||
| return handleRun; | |||
| }; | |||
| @@ -181,3 +181,6 @@ export const buildDslComponentsByGraph = ( | |||
| return components; | |||
| }; | |||
| export const receiveMessageError = (res: any) => | |||
| res && (res?.response.status !== 200 || res?.data?.retcode !== 0); | |||