mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-23 08:56:42 +08:00
Feat: Agent api (#14157)
### What problem does this PR solve?
1. **List agents**
**Prev API**:
- `/v1/canvas/list GET`
- `/api/v1/agents GET`
**Current API**: `/api/v2/agents GET`
2. **Get canvas template**
**Prev API**: `/v1/canvas/templates GET`
**Current API**: `/api/v2/agents/templates GET`
3. **Delete an agent**
**Prev API**:
- `/v1/canvas/rm POST`
- `/api/v1/agents/<agent_id> DELETE`
**Current API**: `/api/v2/agents/<agent_id> DELETE`
4. **Update an agent**
**Prev API**:
- `/api/v1/agents/<agent_id> PUT`
- `/v1/canvas/setting POST `
**Current API**: `/api/v2/agents/<agent_id> PATCH`
5. **Create an agent**
**Prev API**:
- `/v1/canvas/set POST`
- `/api/v1/agents POST`
**Current API**: `/api/v2/agents POST`
6. **Get an agent**
**Prev API**:
- `/v1/canvas/get/<canvas_id> GET `
**Current API**: `/api/v2/agents/<agent_id> GET`
7. **Reset an agent**
**Prev API**:
- `/v1/canvas/reset POST`
**Current API**: `/api/v2/agents/<agent_id>/reset POST`
8. **Upload a file to an agent**
**Prev API**:
- `/v1/canvas/upload/<canvas_id> POST`
**Current API**: `/api/v2/agents/<agent_id>/upload POST`
9. **Input form**
**Prev API**:
- `/v1/canvas/input_form GET`
**Current API**:
`/api/v2/agents/<agent_id>/components/<component_id>/input-form GET`
10. **Debug an agent**
**Prev API**:
- `/v1/canvas/debug POST`
**Current API**:
`/api/v2/agents/<agent_id>/components/<component_id>/debug POST`
11. **Trace an agent**
**Prev API**:
- `/v1/canvas/trace GET`
**Current API**: `/api/v2/agents/<agent_id>/logs/<message_id> GET`
12. **Get an agent version list**
**Prev API**:
- `/v1/canvas/getlistversion/<canvas_id>`
**Current API**: `/api/v2/agents/<agent_id>/versions GET`
13. **Get a version of agent**
**Prev API**:
- `/v1/canvas/getversion/<version_id>`
**Current API**: `/api/v2/agents/<agent_id>/versions/<version_id> GET`
14. **Test db connection**
**Prev API**:
- `/v1/canvas/test_db_connect POST`
**Current API**: `/api/v2/agents/test_db_connection`
15. **Rerun the agent**
**Prev API**:
- `/v1/canvas/rerun POST`
**Current API**: `/api/v2/agents/rerun POST`
16. **Get prompts**
**Prev API**:
- `/v1/canvas/prompts GET`
**Current API**: `/api/v2/agents/prompts GET`
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: chanx <1243304602@qq.com>
This commit is contained in:
@@ -28,8 +28,9 @@ import agentService, {
|
||||
fetchPipeLineList,
|
||||
fetchTrace,
|
||||
fetchWebhookTrace,
|
||||
updateAgent,
|
||||
uploadAgentFile,
|
||||
} from '@/services/agent-service';
|
||||
import api from '@/utils/api';
|
||||
import { buildMessageListWithUuid } from '@/utils/chat';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useDebounce } from 'ahooks';
|
||||
@@ -51,15 +52,14 @@ export const enum AgentApiAction {
|
||||
ResetAgent = 'resetAgent',
|
||||
SetAgent = 'setAgent',
|
||||
FetchAgentTemplates = 'fetchAgentTemplates',
|
||||
UploadCanvasFile = 'uploadCanvasFile',
|
||||
UploadCanvasFileWithProgress = 'uploadCanvasFileWithProgress',
|
||||
UploadAgentFile = 'uploadAgentFile',
|
||||
UploadAgentFileWithProgress = 'uploadAgentFileWithProgress',
|
||||
Trace = 'trace',
|
||||
TestDbConnect = 'testDbConnect',
|
||||
DebugSingle = 'debugSingle',
|
||||
FetchInputForm = 'fetchInputForm',
|
||||
FetchVersionList = 'fetchVersionList',
|
||||
FetchVersion = 'fetchVersion',
|
||||
FetchAgentAvatar = 'fetchAgentAvatar',
|
||||
FetchExternalAgentInputs = 'fetchExternalAgentInputs',
|
||||
SetAgentSetting = 'setAgentSetting',
|
||||
FetchPrompt = 'fetchPrompt',
|
||||
@@ -72,7 +72,7 @@ export const enum AgentApiAction {
|
||||
DeleteAgentSession = 'deleteAgentSession',
|
||||
FetchSessionByIdManually = 'fetchSessionByIdManually',
|
||||
FetchAgentLog = 'fetchAgentLog',
|
||||
FetchFlowDetailSSE = 'flowDetailSSE',
|
||||
FetchSharedAgent = 'fetchSharedAgent',
|
||||
}
|
||||
|
||||
export const useFetchAgentTemplates = () => {
|
||||
@@ -80,7 +80,7 @@ export const useFetchAgentTemplates = () => {
|
||||
queryKey: [AgentApiAction.FetchAgentTemplates],
|
||||
initialData: [],
|
||||
queryFn: async () => {
|
||||
const { data } = await agentService.listTemplates();
|
||||
const { data } = await agentService.listAgentTemplate();
|
||||
|
||||
return data.data;
|
||||
},
|
||||
@@ -89,6 +89,37 @@ export const useFetchAgentTemplates = () => {
|
||||
return data;
|
||||
};
|
||||
|
||||
const buildAgentListParams = ({
|
||||
page,
|
||||
pageSize,
|
||||
keywords,
|
||||
canvasCategory,
|
||||
ownerIds,
|
||||
}: {
|
||||
page: number;
|
||||
pageSize: number;
|
||||
keywords?: string;
|
||||
canvasCategory?: string;
|
||||
ownerIds?: string[];
|
||||
}) => {
|
||||
const params: Record<string, unknown> = {
|
||||
page,
|
||||
page_size: pageSize,
|
||||
};
|
||||
|
||||
if (keywords) {
|
||||
params.keywords = keywords;
|
||||
}
|
||||
if (canvasCategory) {
|
||||
params.canvas_category = canvasCategory;
|
||||
}
|
||||
if (Array.isArray(ownerIds) && ownerIds.length > 0) {
|
||||
params.owner_ids = ownerIds.join(',');
|
||||
}
|
||||
|
||||
return params;
|
||||
};
|
||||
|
||||
export const useFetchAgentListByPage = () => {
|
||||
const { searchString, handleInputChange } = useHandleSearchChange();
|
||||
const { pagination, setPagination } = useGetPaginationWithRouter();
|
||||
@@ -99,17 +130,13 @@ export const useFetchAgentListByPage = () => {
|
||||
: [];
|
||||
const owner = filterValue.owner;
|
||||
|
||||
const requestParams: Record<string, any> = {
|
||||
keywords: debouncedSearchString,
|
||||
page_size: pagination.pageSize,
|
||||
const requestParams = buildAgentListParams({
|
||||
page: pagination.current,
|
||||
canvas_category:
|
||||
canvasCategory.length === 1 ? canvasCategory[0] : undefined,
|
||||
};
|
||||
|
||||
if (Array.isArray(owner) && owner.length > 0) {
|
||||
requestParams.owner_ids = owner.join(',');
|
||||
}
|
||||
pageSize: pagination.pageSize,
|
||||
keywords: debouncedSearchString,
|
||||
canvasCategory: canvasCategory.length === 1 ? canvasCategory[0] : undefined,
|
||||
ownerIds: Array.isArray(owner) ? owner : undefined,
|
||||
});
|
||||
|
||||
const { data, isFetching: loading } = useQuery<{
|
||||
canvas: IFlow[];
|
||||
@@ -131,7 +158,7 @@ export const useFetchAgentListByPage = () => {
|
||||
},
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
const { data } = await agentService.listCanvas(
|
||||
const { data } = await agentService.listAgents(
|
||||
{
|
||||
params: requestParams,
|
||||
},
|
||||
@@ -166,13 +193,13 @@ export function useFetchAllAgentList() {
|
||||
const { data, isFetching: loading } = useQuery<IFlow[]>({
|
||||
queryKey: [AgentApiAction.FetchAllAgentList],
|
||||
queryFn: async () => {
|
||||
const { data } = await agentService.listCanvas(
|
||||
const { data } = await agentService.listAgents(
|
||||
{
|
||||
params: {
|
||||
params: buildAgentListParams({
|
||||
page: 1,
|
||||
page_size: 100000,
|
||||
canvas_category: AgentCategory.AgentCanvas,
|
||||
},
|
||||
pageSize: 100000,
|
||||
canvasCategory: AgentCategory.AgentCanvas,
|
||||
}),
|
||||
},
|
||||
true,
|
||||
);
|
||||
@@ -194,7 +221,12 @@ export const useUpdateAgentSetting = () => {
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.UpdateAgentSetting],
|
||||
mutationFn: async (params: any) => {
|
||||
const ret = await agentService.settingCanvas(params);
|
||||
const ret = await updateAgent(params.id, {
|
||||
title: params.title,
|
||||
description: params.description,
|
||||
permission: params.permission,
|
||||
avatar: params.avatar,
|
||||
});
|
||||
if (ret?.data?.code === 0) {
|
||||
message.success('success');
|
||||
queryClient.invalidateQueries({
|
||||
@@ -218,14 +250,14 @@ export const useDeleteAgent = () => {
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.DeleteAgent],
|
||||
mutationFn: async (canvasIds: string[]) => {
|
||||
const { data } = await agentService.removeCanvas({ canvasIds });
|
||||
mutationFn: async (agentId: string) => {
|
||||
const { data } = await agentService.deleteAgent(agentId);
|
||||
if (data.code === 0) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [AgentApiAction.FetchAgentListByPage],
|
||||
});
|
||||
}
|
||||
return data?.data ?? [];
|
||||
return data?.data ?? false;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -252,7 +284,7 @@ export const useFetchAgent = (): {
|
||||
refetchOnWindowFocus: false,
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
const { data } = await agentService.fetchCanvas(sharedId || id);
|
||||
const { data } = await agentService.getAgent(sharedId || id);
|
||||
|
||||
const messageList = buildMessageListWithUuid(
|
||||
get(data, 'data.dsl.messages', []),
|
||||
@@ -286,7 +318,7 @@ export const useResetAgent = () => {
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.ResetAgent],
|
||||
mutationFn: async () => {
|
||||
const { data } = await agentService.resetCanvas({ id });
|
||||
const { data } = await agentService.resetAgent(id);
|
||||
return data;
|
||||
},
|
||||
});
|
||||
@@ -295,6 +327,7 @@ export const useResetAgent = () => {
|
||||
};
|
||||
|
||||
export const useSetAgent = (showMessage: boolean = true) => {
|
||||
const { id } = useParams();
|
||||
const queryClient = useQueryClient();
|
||||
const {
|
||||
data,
|
||||
@@ -309,17 +342,34 @@ export const useSetAgent = (showMessage: boolean = true) => {
|
||||
avatar?: string;
|
||||
canvas_category?: string;
|
||||
release?: string;
|
||||
description?: string | null;
|
||||
permission?: string;
|
||||
}) => {
|
||||
const { data = {} } = await agentService.setCanvas(params);
|
||||
const agentId = params.id ?? id;
|
||||
const { data = {} } = agentId
|
||||
? await updateAgent(agentId, {
|
||||
title: params.title,
|
||||
dsl: params.dsl,
|
||||
avatar: params.avatar,
|
||||
description: params.description,
|
||||
permission: params.permission,
|
||||
release: params.release,
|
||||
})
|
||||
: await agentService.createAgent(params);
|
||||
if (data.code === 0) {
|
||||
if (showMessage) {
|
||||
message.success(
|
||||
i18n.t(`message.${params?.id ? 'modified' : 'created'}`),
|
||||
i18n.t(`message.${agentId ? 'modified' : 'created'}`),
|
||||
);
|
||||
}
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [AgentApiAction.FetchAgentListByPage],
|
||||
});
|
||||
if (agentId) {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [AgentApiAction.FetchAgentDetail],
|
||||
});
|
||||
}
|
||||
}
|
||||
return data;
|
||||
},
|
||||
@@ -329,17 +379,17 @@ export const useSetAgent = (showMessage: boolean = true) => {
|
||||
};
|
||||
|
||||
// Only one file can be uploaded at a time
|
||||
export const useUploadCanvasFile = () => {
|
||||
export const useUploadAgentFile = () => {
|
||||
const { id } = useParams();
|
||||
const [searchParams] = useSearchParams();
|
||||
const shared_id = searchParams.get('shared_id');
|
||||
const canvasId = id || shared_id;
|
||||
const agentId = id || shared_id;
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.UploadCanvasFile],
|
||||
mutationKey: [AgentApiAction.UploadAgentFile],
|
||||
mutationFn: async (body: any) => {
|
||||
let nextBody = body;
|
||||
try {
|
||||
@@ -350,10 +400,7 @@ export const useUploadCanvasFile = () => {
|
||||
});
|
||||
}
|
||||
|
||||
const { data } = await agentService.uploadCanvasFile(
|
||||
{ url: api.uploadAgentFile(canvasId as string), data: nextBody },
|
||||
true,
|
||||
);
|
||||
const { data } = await uploadAgentFile(agentId as string, nextBody);
|
||||
if (data?.code === 0) {
|
||||
message.success(i18n.t('message.uploaded'));
|
||||
}
|
||||
@@ -364,10 +411,10 @@ export const useUploadCanvasFile = () => {
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, uploadCanvasFile: mutateAsync };
|
||||
return { data, loading, uploadAgentFile: mutateAsync };
|
||||
};
|
||||
|
||||
export const useUploadCanvasFileWithProgress = (identifier?: string | null) => {
|
||||
export const useUploadAgentFileWithProgress = (identifier?: string | null) => {
|
||||
const { id } = useParams();
|
||||
|
||||
type UploadParameters = Parameters<NonNullable<FileUploadProps['onUpload']>>;
|
||||
@@ -379,7 +426,7 @@ export const useUploadCanvasFileWithProgress = (identifier?: string | null) => {
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.UploadCanvasFileWithProgress],
|
||||
mutationKey: [AgentApiAction.UploadAgentFileWithProgress],
|
||||
mutationFn: async ({
|
||||
files,
|
||||
options: { onError, onSuccess, onProgress },
|
||||
@@ -392,9 +439,9 @@ export const useUploadCanvasFileWithProgress = (identifier?: string | null) => {
|
||||
});
|
||||
}
|
||||
|
||||
const { data } = await agentService.uploadCanvasFile(
|
||||
const { data } = await agentService.uploadAgentFile(
|
||||
{
|
||||
url: api.uploadAgentFile(identifier || id),
|
||||
agentId: identifier || id,
|
||||
data: formData,
|
||||
onUploadProgress: ({ progress }) => {
|
||||
files.forEach((file) => {
|
||||
@@ -420,7 +467,7 @@ export const useUploadCanvasFileWithProgress = (identifier?: string | null) => {
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, uploadCanvasFile: mutateAsync };
|
||||
return { data, loading, uploadAgentFile: mutateAsync };
|
||||
};
|
||||
|
||||
export const useFetchMessageTrace = (canvasId?: string) => {
|
||||
@@ -490,9 +537,18 @@ export const useDebugSingle = () => {
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.FetchInputForm],
|
||||
mutationKey: [AgentApiAction.DebugSingle],
|
||||
mutationFn: async (params: IDebugSingleRequestBody) => {
|
||||
const ret = await agentService.debugSingle({ id, ...params });
|
||||
const ret = await agentService.debugSingle(
|
||||
{
|
||||
agentId: id as string,
|
||||
componentId: params.component_id,
|
||||
data: {
|
||||
params: params.params,
|
||||
},
|
||||
},
|
||||
true,
|
||||
);
|
||||
if (ret?.data?.code !== 0) {
|
||||
message.error(ret?.data?.message);
|
||||
}
|
||||
@@ -512,12 +568,7 @@ export const useFetchInputForm = (componentId?: string) => {
|
||||
enabled: !!id && !!componentId,
|
||||
queryFn: async () => {
|
||||
const { data } = await agentService.inputForm(
|
||||
{
|
||||
params: {
|
||||
id,
|
||||
component_id: componentId,
|
||||
},
|
||||
},
|
||||
{ agentId: id as string, componentId: componentId as string },
|
||||
true,
|
||||
);
|
||||
|
||||
@@ -552,15 +603,19 @@ export const useFetchVersion = (
|
||||
data?: IFlow;
|
||||
loading: boolean;
|
||||
} => {
|
||||
const { id } = useParams();
|
||||
const { data, isFetching: loading } = useQuery({
|
||||
queryKey: [AgentApiAction.FetchVersion, version_id],
|
||||
queryKey: [AgentApiAction.FetchVersion, id, version_id],
|
||||
initialData: undefined,
|
||||
gcTime: 0,
|
||||
enabled: !!version_id, // Only call API when both values are provided
|
||||
enabled: !!id && !!version_id,
|
||||
queryFn: async () => {
|
||||
if (!version_id) return undefined;
|
||||
if (!id || !version_id) return undefined;
|
||||
|
||||
const { data } = await agentService.fetchVersion(version_id);
|
||||
const { data } = await agentService.fetchVersion({
|
||||
agentId: id,
|
||||
versionId: version_id,
|
||||
});
|
||||
|
||||
return data?.data ?? undefined;
|
||||
},
|
||||
@@ -569,35 +624,6 @@ export const useFetchVersion = (
|
||||
return { data, loading };
|
||||
};
|
||||
|
||||
export const useFetchAgentAvatar = (): {
|
||||
data: IFlow;
|
||||
loading: boolean;
|
||||
refetch: () => void;
|
||||
} => {
|
||||
const { sharedId } = useGetSharedChatSearchParams();
|
||||
|
||||
const {
|
||||
data,
|
||||
isFetching: loading,
|
||||
refetch,
|
||||
} = useQuery({
|
||||
queryKey: [AgentApiAction.FetchAgentAvatar],
|
||||
initialData: {} as IFlow,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnMount: false,
|
||||
refetchOnWindowFocus: false,
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
if (!sharedId) return {};
|
||||
const { data } = await agentService.fetchAgentAvatar(sharedId);
|
||||
|
||||
return data?.data ?? {};
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, refetch };
|
||||
};
|
||||
|
||||
export const useFetchAgentLog = (searchParams: IAgentLogsRequest) => {
|
||||
const { id } = useParams();
|
||||
const { data, isFetching: loading } = useQuery<IAgentLogsResponse>({
|
||||
@@ -609,7 +635,7 @@ export const useFetchAgentLog = (searchParams: IAgentLogsRequest) => {
|
||||
...searchParams,
|
||||
});
|
||||
|
||||
return data?.data ?? [];
|
||||
return { total: data?.total ?? 0, sessions: data?.data ?? [] };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -636,7 +662,7 @@ export const useFetchSessionsByCanvasId = () => {
|
||||
exp_user_id: tenantInfo.tenant_id,
|
||||
});
|
||||
|
||||
return data?.data ?? { total: 0, sessions: [] };
|
||||
return { total: data?.total ?? 0, sessions: data?.data ?? [] };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -672,33 +698,6 @@ export const useFetchExternalAgentInputs = () => {
|
||||
return { data, loading, refetch };
|
||||
};
|
||||
|
||||
export const useSetAgentSetting = () => {
|
||||
const { id } = useParams();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const {
|
||||
data,
|
||||
isPending: loading,
|
||||
mutateAsync,
|
||||
} = useMutation({
|
||||
mutationKey: [AgentApiAction.SetAgentSetting],
|
||||
mutationFn: async (params: any) => {
|
||||
const ret = await agentService.settingCanvas({ id, ...params });
|
||||
if (ret?.data?.code === 0) {
|
||||
message.success('success');
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: [AgentApiAction.FetchAgentDetail],
|
||||
});
|
||||
} else {
|
||||
message.error(ret?.data?.data);
|
||||
}
|
||||
return ret?.data?.code;
|
||||
},
|
||||
});
|
||||
|
||||
return { data, loading, setAgentSetting: mutateAsync };
|
||||
};
|
||||
|
||||
export const useFetchPrompt = () => {
|
||||
const {
|
||||
data,
|
||||
@@ -731,7 +730,9 @@ export const useFetchAgentList = ({
|
||||
initialData: { canvas: [], total: 0 },
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
const { data } = await fetchPipeLineList({ canvas_category });
|
||||
const { data } = await fetchPipeLineList({
|
||||
canvas_category,
|
||||
});
|
||||
|
||||
return data?.data ?? [];
|
||||
},
|
||||
@@ -767,7 +768,7 @@ export const useCancelDataflow = () => {
|
||||
// initialData: [],
|
||||
// gcTime: 0, // https://tanstack.com/query/latest/docs/framework/react/guides/caching?from=reactQueryV3
|
||||
// queryFn: async () => {
|
||||
// const { data } = await agentService.listCanvas();
|
||||
// const { data } = await agentService.listAgents();
|
||||
|
||||
// return data?.data ?? [];
|
||||
// },
|
||||
@@ -793,7 +794,7 @@ export function useCancelConversation() {
|
||||
return { data, loading, cancelConversation: mutateAsync };
|
||||
}
|
||||
|
||||
export const useFetchFlowSSE = (): {
|
||||
export const useFetchSharedAgent = (): {
|
||||
data: IFlow;
|
||||
loading: boolean;
|
||||
refetch: () => void;
|
||||
@@ -805,7 +806,7 @@ export const useFetchFlowSSE = (): {
|
||||
isFetching: loading,
|
||||
refetch,
|
||||
} = useQuery({
|
||||
queryKey: [AgentApiAction.FetchFlowDetailSSE],
|
||||
queryKey: [AgentApiAction.FetchSharedAgent, sharedId],
|
||||
initialData: {} as IFlow,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnMount: false,
|
||||
@@ -813,7 +814,7 @@ export const useFetchFlowSSE = (): {
|
||||
gcTime: 0,
|
||||
queryFn: async () => {
|
||||
if (!sharedId) return {};
|
||||
const { data } = await agentService.getCanvasSSE(sharedId);
|
||||
const { data } = await agentService.getAgent(sharedId);
|
||||
|
||||
const messageList = buildMessageListWithUuid(
|
||||
get(data, 'data.dsl.messages', []),
|
||||
|
||||
Reference in New Issue
Block a user