mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-06-02 09:52:37 +08:00

### What problem does this PR solve? feat: monitor changes in the table of relevant operators and synchronize them to the edge #918 feat: fixed the issue of repeated requests when opening the graph page #918 feat: cache node anchor coordinate information #918 feat: monitor changes in the data.form field of the categorize and relevant operators and then synchronize them to the edge #918 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
191 lines
4.4 KiB
TypeScript
191 lines
4.4 KiB
TypeScript
import { ResponseType } from '@/interfaces/database/base';
|
|
import { DSL, IFlow, IFlowTemplate } from '@/interfaces/database/flow';
|
|
import i18n from '@/locales/config';
|
|
import flowService from '@/services/flow-service';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { message } from 'antd';
|
|
import { useParams } from 'umi';
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
export const EmptyDsl = {
|
|
graph: {
|
|
nodes: [
|
|
{
|
|
id: 'begin',
|
|
type: 'beginNode',
|
|
position: {
|
|
x: 50,
|
|
y: 200,
|
|
},
|
|
data: {
|
|
label: 'Begin',
|
|
name: 'begin',
|
|
},
|
|
sourcePosition: 'left',
|
|
targetPosition: 'right',
|
|
},
|
|
],
|
|
edges: [],
|
|
},
|
|
components: {
|
|
begin: {
|
|
obj: {
|
|
component_name: 'Begin',
|
|
params: {},
|
|
},
|
|
downstream: ['Answer:China'], // other edge target is downstream, edge source is current node id
|
|
upstream: [], // edge source is upstream, edge target is current node id
|
|
},
|
|
},
|
|
messages: [],
|
|
reference: [],
|
|
history: [],
|
|
path: [],
|
|
answer: [],
|
|
};
|
|
|
|
export const useFetchFlowTemplates = (): ResponseType<IFlowTemplate[]> => {
|
|
const { data } = useQuery({
|
|
queryKey: ['fetchFlowTemplates'],
|
|
initialData: [],
|
|
queryFn: async () => {
|
|
const { data } = await flowService.listTemplates();
|
|
if (Array.isArray(data?.data)) {
|
|
data.data.unshift({
|
|
id: uuid(),
|
|
title: 'Blank',
|
|
description: 'Create from nothing',
|
|
dsl: EmptyDsl,
|
|
});
|
|
}
|
|
|
|
return data;
|
|
},
|
|
});
|
|
|
|
return data;
|
|
};
|
|
|
|
export const useFetchFlowList = (): { data: IFlow[]; loading: boolean } => {
|
|
const { data, isFetching: loading } = useQuery({
|
|
queryKey: ['fetchFlowList'],
|
|
initialData: [],
|
|
queryFn: async () => {
|
|
const { data } = await flowService.listCanvas();
|
|
|
|
return data?.data ?? [];
|
|
},
|
|
});
|
|
|
|
return { data, loading };
|
|
};
|
|
|
|
export const useFetchFlow = (): {
|
|
data: IFlow;
|
|
loading: boolean;
|
|
refetch: () => void;
|
|
} => {
|
|
const { id } = useParams();
|
|
const {
|
|
data,
|
|
isFetching: loading,
|
|
refetch,
|
|
} = useQuery({
|
|
queryKey: ['flowDetail'],
|
|
initialData: {} as IFlow,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
queryFn: async () => {
|
|
const { data } = await flowService.getCanvas({}, id);
|
|
|
|
return data?.data ?? {};
|
|
},
|
|
});
|
|
|
|
return { data, loading, refetch };
|
|
};
|
|
|
|
export const useSetFlow = () => {
|
|
const queryClient = useQueryClient();
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: ['setFlow'],
|
|
mutationFn: async (params: {
|
|
id?: string;
|
|
title?: string;
|
|
dsl?: DSL;
|
|
avatar?: string;
|
|
}) => {
|
|
const { data } = await flowService.setCanvas(params);
|
|
if (data.retcode === 0) {
|
|
message.success(
|
|
i18n.t(`message.${params?.id ? 'modified' : 'created'}`),
|
|
);
|
|
queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
|
|
}
|
|
return data;
|
|
},
|
|
});
|
|
|
|
return { data, loading, setFlow: mutateAsync };
|
|
};
|
|
|
|
export const useDeleteFlow = () => {
|
|
const queryClient = useQueryClient();
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: ['deleteFlow'],
|
|
mutationFn: async (canvasIds: string[]) => {
|
|
const { data } = await flowService.removeCanvas({ canvasIds });
|
|
if (data.retcode === 0) {
|
|
queryClient.invalidateQueries({ queryKey: ['fetchFlowList'] });
|
|
}
|
|
return data?.data ?? [];
|
|
},
|
|
});
|
|
|
|
return { data, loading, deleteFlow: mutateAsync };
|
|
};
|
|
|
|
export const useRunFlow = () => {
|
|
const {
|
|
data,
|
|
isPending: loading,
|
|
mutateAsync,
|
|
} = useMutation({
|
|
mutationKey: ['runFlow'],
|
|
mutationFn: async (params: { id: string; dsl: DSL }) => {
|
|
const { data } = await flowService.runCanvas(params);
|
|
if (data.retcode === 0) {
|
|
message.success(i18n.t(`message.modified`));
|
|
}
|
|
return data?.data ?? {};
|
|
},
|
|
});
|
|
|
|
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 };
|
|
};
|