feat: Automatically save agent page data #3301 (#3302)

### What problem does this PR solve?

feat: Automatically save agent page data #3301

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2024-11-08 17:28:11 +08:00
committed by GitHub
parent 464a4d6ead
commit 74d1eeb4d3
10 changed files with 105 additions and 23 deletions

View File

@ -19,7 +19,9 @@ import {
import { useFetchModelId, useSendMessageWithSse } from '@/hooks/logic-hooks';
import { Variable } from '@/interfaces/database/chat';
import api from '@/utils/api';
import { useDebounceEffect } from 'ahooks';
import { FormInstance, message } from 'antd';
import dayjs from 'dayjs';
import { humanId } from 'human-id';
import { lowerFirst } from 'lodash';
import trim from 'lodash/trim';
@ -446,12 +448,21 @@ export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => {
return handleRun;
};
export const useReplaceIdWithText = (output: unknown) => {
export const useReplaceIdWithName = () => {
const getNode = useGraphStore((state) => state.getNode);
const getNameById = (id?: string) => {
return getNode(id)?.data.name;
};
const replaceIdWithName = useCallback(
(id?: string) => {
return getNode(id)?.data.name;
},
[getNode],
);
return replaceIdWithName;
};
export const useReplaceIdWithText = (output: unknown) => {
const getNameById = useReplaceIdWithName();
return {
replacedOutput: replaceIdWithText(output, getNameById),
@ -547,6 +558,7 @@ export const useWatchNodeFormDataChange = () => {
);
useEffect(() => {
console.info('xxx');
nodes.forEach((node) => {
const currentNode = getNode(node.id);
const form = currentNode?.data.form ?? {};
@ -668,3 +680,36 @@ export const useCopyPaste = () => {
};
}, [onPasteCapture]);
};
export const useWatchAgentChange = () => {
const [time, setTime] = useState<string>();
const nodes = useGraphStore((state) => state.nodes);
const edges = useGraphStore((state) => state.edges);
const { saveGraph } = useSaveGraph();
const { data: flowDetail } = useFetchFlow();
const setSaveTime = useCallback((updateTime: number) => {
setTime(dayjs(updateTime).format('YYYY-MM-DD HH:mm:ss'));
}, []);
useEffect(() => {
setSaveTime(flowDetail?.update_time);
}, [flowDetail, setSaveTime]);
const saveAgent = useCallback(async () => {
const ret = await saveGraph();
setSaveTime(ret.data.update_time);
}, [saveGraph, setSaveTime]);
useDebounceEffect(
() => {
saveAgent();
},
[nodes, edges],
{
wait: 1000 * 20,
},
);
return time;
};