balibabu
commited on
Commit
·
f72cd8c
1
Parent(s):
7a2a8fe
feat: save graph data before opening the debug drawer #918 (#1387)
Browse files### 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)
web/src/pages/flow/chat/hooks.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import { MessageType } from '@/constants/chat';
|
| 2 |
-
import { useFetchFlow
|
| 3 |
import {
|
| 4 |
useHandleMessageInputChange,
|
| 5 |
useScrollToBottom,
|
|
@@ -12,6 +12,7 @@ import { message } from 'antd';
|
|
| 12 |
import { useCallback, useEffect, useState } from 'react';
|
| 13 |
import { useParams } from 'umi';
|
| 14 |
import { v4 as uuid } from 'uuid';
|
|
|
|
| 15 |
|
| 16 |
const antMessage = message;
|
| 17 |
|
|
@@ -94,7 +95,6 @@ export const useSendMessage = (
|
|
| 94 |
const { id: flowId } = useParams();
|
| 95 |
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
| 96 |
const { refetch } = useFetchFlow();
|
| 97 |
-
const { resetFlow } = useResetFlow();
|
| 98 |
|
| 99 |
const { send, answer, done } = useSendMessageWithSse(api.runCanvas);
|
| 100 |
|
|
@@ -108,7 +108,7 @@ export const useSendMessage = (
|
|
| 108 |
}
|
| 109 |
const res = await send(params);
|
| 110 |
|
| 111 |
-
if (
|
| 112 |
antMessage.error(res?.data?.retmsg);
|
| 113 |
|
| 114 |
// cancel loading
|
|
@@ -128,28 +128,12 @@ export const useSendMessage = (
|
|
| 128 |
[sendMessage],
|
| 129 |
);
|
| 130 |
|
| 131 |
-
/**
|
| 132 |
-
* Call the reset api before opening the run drawer each time
|
| 133 |
-
*/
|
| 134 |
-
const resetFlowBeforeFetchingPrologue = useCallback(async () => {
|
| 135 |
-
// After resetting, all previous messages will be cleared.
|
| 136 |
-
const ret = await resetFlow();
|
| 137 |
-
if (ret.retcode === 0) {
|
| 138 |
-
// fetch prologue
|
| 139 |
-
sendMessage('');
|
| 140 |
-
}
|
| 141 |
-
}, [resetFlow, sendMessage]);
|
| 142 |
-
|
| 143 |
useEffect(() => {
|
| 144 |
if (answer.answer) {
|
| 145 |
addNewestAnswer(answer);
|
| 146 |
}
|
| 147 |
}, [answer, addNewestAnswer]);
|
| 148 |
|
| 149 |
-
useEffect(() => {
|
| 150 |
-
resetFlowBeforeFetchingPrologue();
|
| 151 |
-
}, [resetFlowBeforeFetchingPrologue]);
|
| 152 |
-
|
| 153 |
const handlePressEnter = useCallback(() => {
|
| 154 |
if (done) {
|
| 155 |
setValue('');
|
|
|
|
| 1 |
import { MessageType } from '@/constants/chat';
|
| 2 |
+
import { useFetchFlow } from '@/hooks/flow-hooks';
|
| 3 |
import {
|
| 4 |
useHandleMessageInputChange,
|
| 5 |
useScrollToBottom,
|
|
|
|
| 12 |
import { useCallback, useEffect, useState } from 'react';
|
| 13 |
import { useParams } from 'umi';
|
| 14 |
import { v4 as uuid } from 'uuid';
|
| 15 |
+
import { receiveMessageError } from '../utils';
|
| 16 |
|
| 17 |
const antMessage = message;
|
| 18 |
|
|
|
|
| 95 |
const { id: flowId } = useParams();
|
| 96 |
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
|
| 97 |
const { refetch } = useFetchFlow();
|
|
|
|
| 98 |
|
| 99 |
const { send, answer, done } = useSendMessageWithSse(api.runCanvas);
|
| 100 |
|
|
|
|
| 108 |
}
|
| 109 |
const res = await send(params);
|
| 110 |
|
| 111 |
+
if (receiveMessageError(res)) {
|
| 112 |
antMessage.error(res?.data?.retmsg);
|
| 113 |
|
| 114 |
// cancel loading
|
|
|
|
| 128 |
[sendMessage],
|
| 129 |
);
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
useEffect(() => {
|
| 132 |
if (answer.answer) {
|
| 133 |
addNewestAnswer(answer);
|
| 134 |
}
|
| 135 |
}, [answer, addNewestAnswer]);
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
const handlePressEnter = useCallback(() => {
|
| 138 |
if (done) {
|
| 139 |
setValue('');
|
web/src/pages/flow/header/index.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import { Button, Flex, Space } from 'antd';
|
|
| 3 |
import { useFetchFlow } from '@/hooks/flow-hooks';
|
| 4 |
import { ArrowLeftOutlined } from '@ant-design/icons';
|
| 5 |
import { Link } from 'umi';
|
| 6 |
-
import { useSaveGraph } from '../hooks';
|
| 7 |
|
| 8 |
import styles from './index.less';
|
| 9 |
|
|
@@ -13,7 +13,7 @@ interface IProps {
|
|
| 13 |
|
| 14 |
const FlowHeader = ({ showChatDrawer }: IProps) => {
|
| 15 |
const { saveGraph } = useSaveGraph();
|
| 16 |
-
|
| 17 |
const { data } = useFetchFlow();
|
| 18 |
|
| 19 |
return (
|
|
@@ -31,7 +31,7 @@ const FlowHeader = ({ showChatDrawer }: IProps) => {
|
|
| 31 |
<h3>{data.title}</h3>
|
| 32 |
</Space>
|
| 33 |
<Space size={'large'}>
|
| 34 |
-
<Button onClick={
|
| 35 |
<b>Run</b>
|
| 36 |
</Button>
|
| 37 |
<Button type="primary" onClick={saveGraph}>
|
|
|
|
| 3 |
import { useFetchFlow } from '@/hooks/flow-hooks';
|
| 4 |
import { ArrowLeftOutlined } from '@ant-design/icons';
|
| 5 |
import { Link } from 'umi';
|
| 6 |
+
import { useSaveGraph, useSaveGraphBeforeOpeningDebugDrawer } from '../hooks';
|
| 7 |
|
| 8 |
import styles from './index.less';
|
| 9 |
|
|
|
|
| 13 |
|
| 14 |
const FlowHeader = ({ showChatDrawer }: IProps) => {
|
| 15 |
const { saveGraph } = useSaveGraph();
|
| 16 |
+
const handleRun = useSaveGraphBeforeOpeningDebugDrawer(showChatDrawer);
|
| 17 |
const { data } = useFetchFlow();
|
| 18 |
|
| 19 |
return (
|
|
|
|
| 31 |
<h3>{data.title}</h3>
|
| 32 |
</Space>
|
| 33 |
<Space size={'large'}>
|
| 34 |
+
<Button onClick={handleRun}>
|
| 35 |
<b>Run</b>
|
| 36 |
</Button>
|
| 37 |
<Button type="primary" onClick={saveGraph}>
|
web/src/pages/flow/hooks.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import { useSetModalState } from '@/hooks/commonHooks';
|
| 2 |
-
import { useFetchFlow, useSetFlow } from '@/hooks/flow-hooks';
|
| 3 |
import { useFetchLlmList } from '@/hooks/llmHooks';
|
| 4 |
import { IGraph } from '@/interfaces/database/flow';
|
| 5 |
import { useIsFetching } from '@tanstack/react-query';
|
|
@@ -17,8 +17,9 @@ import {
|
|
| 17 |
ModelVariableType,
|
| 18 |
settledModelVariableMap,
|
| 19 |
} from '@/constants/knowledge';
|
| 20 |
-
import { useFetchModelId } from '@/hooks/logicHooks';
|
| 21 |
import { Variable } from '@/interfaces/database/chat';
|
|
|
|
| 22 |
import { useDebounceEffect } from 'ahooks';
|
| 23 |
import { FormInstance, message } from 'antd';
|
| 24 |
import { humanId } from 'human-id';
|
|
@@ -37,7 +38,7 @@ import {
|
|
| 37 |
initialRewriteQuestionValues,
|
| 38 |
} from './constant';
|
| 39 |
import useGraphStore, { RFState } from './store';
|
| 40 |
-
import { buildDslComponentsByGraph } from './utils';
|
| 41 |
|
| 42 |
const selector = (state: RFState) => ({
|
| 43 |
nodes: state.nodes,
|
|
@@ -193,9 +194,9 @@ export const useSaveGraph = () => {
|
|
| 193 |
const { setFlow } = useSetFlow();
|
| 194 |
const { id } = useParams();
|
| 195 |
const { nodes, edges } = useGraphStore((state) => state);
|
| 196 |
-
const saveGraph = useCallback(() => {
|
| 197 |
const dslComponents = buildDslComponentsByGraph(nodes, edges);
|
| 198 |
-
setFlow({
|
| 199 |
id,
|
| 200 |
title: data.title,
|
| 201 |
dsl: { ...data.dsl, graph: { nodes, edges }, components: dslComponents },
|
|
@@ -345,3 +346,29 @@ export const useHandleNodeNameChange = (node?: Node) => {
|
|
| 345 |
|
| 346 |
return { name, handleNameBlur, handleNameChange };
|
| 347 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import { useSetModalState } from '@/hooks/commonHooks';
|
| 2 |
+
import { useFetchFlow, useResetFlow, useSetFlow } from '@/hooks/flow-hooks';
|
| 3 |
import { useFetchLlmList } from '@/hooks/llmHooks';
|
| 4 |
import { IGraph } from '@/interfaces/database/flow';
|
| 5 |
import { useIsFetching } from '@tanstack/react-query';
|
|
|
|
| 17 |
ModelVariableType,
|
| 18 |
settledModelVariableMap,
|
| 19 |
} from '@/constants/knowledge';
|
| 20 |
+
import { useFetchModelId, useSendMessageWithSse } from '@/hooks/logicHooks';
|
| 21 |
import { Variable } from '@/interfaces/database/chat';
|
| 22 |
+
import api from '@/utils/api';
|
| 23 |
import { useDebounceEffect } from 'ahooks';
|
| 24 |
import { FormInstance, message } from 'antd';
|
| 25 |
import { humanId } from 'human-id';
|
|
|
|
| 38 |
initialRewriteQuestionValues,
|
| 39 |
} from './constant';
|
| 40 |
import useGraphStore, { RFState } from './store';
|
| 41 |
+
import { buildDslComponentsByGraph, receiveMessageError } from './utils';
|
| 42 |
|
| 43 |
const selector = (state: RFState) => ({
|
| 44 |
nodes: state.nodes,
|
|
|
|
| 194 |
const { setFlow } = useSetFlow();
|
| 195 |
const { id } = useParams();
|
| 196 |
const { nodes, edges } = useGraphStore((state) => state);
|
| 197 |
+
const saveGraph = useCallback(async () => {
|
| 198 |
const dslComponents = buildDslComponentsByGraph(nodes, edges);
|
| 199 |
+
return setFlow({
|
| 200 |
id,
|
| 201 |
title: data.title,
|
| 202 |
dsl: { ...data.dsl, graph: { nodes, edges }, components: dslComponents },
|
|
|
|
| 346 |
|
| 347 |
return { name, handleNameBlur, handleNameChange };
|
| 348 |
};
|
| 349 |
+
|
| 350 |
+
export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => {
|
| 351 |
+
const { id } = useParams();
|
| 352 |
+
const { saveGraph } = useSaveGraph();
|
| 353 |
+
const { resetFlow } = useResetFlow();
|
| 354 |
+
const { send } = useSendMessageWithSse(api.runCanvas);
|
| 355 |
+
const handleRun = useCallback(async () => {
|
| 356 |
+
const saveRet = await saveGraph();
|
| 357 |
+
if (saveRet?.retcode === 0) {
|
| 358 |
+
// Call the reset api before opening the run drawer each time
|
| 359 |
+
const resetRet = await resetFlow();
|
| 360 |
+
// After resetting, all previous messages will be cleared.
|
| 361 |
+
if (resetRet?.retcode === 0) {
|
| 362 |
+
// fetch prologue
|
| 363 |
+
const sendRet = await send({ id });
|
| 364 |
+
if (receiveMessageError(sendRet)) {
|
| 365 |
+
message.error(sendRet?.data?.retmsg);
|
| 366 |
+
} else {
|
| 367 |
+
show();
|
| 368 |
+
}
|
| 369 |
+
}
|
| 370 |
+
}
|
| 371 |
+
}, [saveGraph, resetFlow, id, send, show]);
|
| 372 |
+
|
| 373 |
+
return handleRun;
|
| 374 |
+
};
|
web/src/pages/flow/utils.ts
CHANGED
|
@@ -181,3 +181,6 @@ export const buildDslComponentsByGraph = (
|
|
| 181 |
|
| 182 |
return components;
|
| 183 |
};
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
return components;
|
| 183 |
};
|
| 184 |
+
|
| 185 |
+
export const receiveMessageError = (res: any) =>
|
| 186 |
+
res && (res?.response.status !== 200 || res?.data?.retcode !== 0);
|