mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
### What problem does this PR solve? Fixes: Added session variable types and modified configuration - Added more types of session variables - Modified the embedding model switching logic in the knowledge base configuration ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import {
|
|
useFetchAgent,
|
|
useResetAgent,
|
|
useSetAgent,
|
|
} from '@/hooks/use-agent-request';
|
|
import { GlobalVariableType } from '@/interfaces/database/agent';
|
|
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
|
import { formatDate } from '@/utils/date';
|
|
import { useDebounceEffect } from 'ahooks';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { useParams } from 'umi';
|
|
import useGraphStore from '../store';
|
|
import { useBuildDslData } from './use-build-dsl';
|
|
|
|
export const useSaveGraph = (showMessage: boolean = true) => {
|
|
const { data } = useFetchAgent();
|
|
const { setAgent, loading } = useSetAgent(showMessage);
|
|
const { id } = useParams();
|
|
const { buildDslData } = useBuildDslData();
|
|
|
|
const saveGraph = useCallback(
|
|
async (
|
|
currentNodes?: RAGFlowNodeType[],
|
|
otherParam?: { globalVariables: Record<string, GlobalVariableType> },
|
|
) => {
|
|
return setAgent({
|
|
id,
|
|
title: data.title,
|
|
dsl: buildDslData(currentNodes, otherParam),
|
|
});
|
|
},
|
|
[setAgent, data, id, buildDslData],
|
|
);
|
|
|
|
return { saveGraph, loading };
|
|
};
|
|
|
|
export const useSaveGraphBeforeOpeningDebugDrawer = (show: () => void) => {
|
|
const { saveGraph, loading } = useSaveGraph();
|
|
const { resetAgent } = useResetAgent();
|
|
|
|
const handleRun = useCallback(
|
|
async (nextNodes?: RAGFlowNodeType[]) => {
|
|
const saveRet = await saveGraph(nextNodes);
|
|
if (saveRet?.code === 0) {
|
|
// Call the reset api before opening the run drawer each time
|
|
const resetRet = await resetAgent();
|
|
// After resetting, all previous messages will be cleared.
|
|
if (resetRet?.code === 0) {
|
|
show();
|
|
}
|
|
}
|
|
},
|
|
[saveGraph, resetAgent, show],
|
|
);
|
|
|
|
return { handleRun, loading };
|
|
};
|
|
|
|
export const useWatchAgentChange = (chatDrawerVisible: boolean) => {
|
|
const [time, setTime] = useState<string>();
|
|
const nodes = useGraphStore((state) => state.nodes);
|
|
const edges = useGraphStore((state) => state.edges);
|
|
const { saveGraph } = useSaveGraph(false);
|
|
const { data: flowDetail } = useFetchAgent();
|
|
|
|
const setSaveTime = useCallback((updateTime: number) => {
|
|
setTime(formatDate(updateTime));
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
setSaveTime(flowDetail?.update_time);
|
|
}, [flowDetail, setSaveTime]);
|
|
|
|
const saveAgent = useCallback(async () => {
|
|
if (!chatDrawerVisible) {
|
|
const ret = await saveGraph();
|
|
setSaveTime(ret.data.update_time);
|
|
}
|
|
}, [chatDrawerVisible, saveGraph, setSaveTime]);
|
|
|
|
useDebounceEffect(
|
|
() => {
|
|
saveAgent();
|
|
},
|
|
[nodes, edges],
|
|
{
|
|
wait: 1000 * 20,
|
|
},
|
|
);
|
|
|
|
return time;
|
|
};
|