2025-05-07 14:29:01 +08:00
|
|
|
import type { MutationOptions } from '@tanstack/react-query'
|
2025-06-04 11:39:04 +08:00
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
2025-05-07 18:09:38 +08:00
|
|
|
import { del, get, patch, post } from './base'
|
2025-05-07 11:30:13 +08:00
|
|
|
import type {
|
2025-05-16 15:14:50 +08:00
|
|
|
DeleteTemplateResponse,
|
|
|
|
ExportTemplateDSLResponse,
|
2025-05-14 14:49:01 +08:00
|
|
|
ImportPipelineDSLConfirmResponse,
|
2025-05-07 18:09:38 +08:00
|
|
|
ImportPipelineDSLRequest,
|
|
|
|
ImportPipelineDSLResponse,
|
2025-05-14 14:49:01 +08:00
|
|
|
PipelineCheckDependenciesResponse,
|
2025-05-20 11:42:22 +08:00
|
|
|
PipelineDatasourceNodeRunRequest,
|
2025-05-21 10:53:18 +08:00
|
|
|
PipelineDatasourceNodeRunResponse,
|
2025-05-20 11:42:22 +08:00
|
|
|
PipelineProcessingParamsRequest,
|
2025-05-08 18:29:49 +08:00
|
|
|
PipelineProcessingParamsResponse,
|
2025-05-07 11:30:13 +08:00
|
|
|
PipelineTemplateByIdResponse,
|
|
|
|
PipelineTemplateListParams,
|
|
|
|
PipelineTemplateListResponse,
|
2025-05-21 16:37:02 +08:00
|
|
|
PublishedPipelineInfoResponse,
|
2025-06-03 10:14:48 +08:00
|
|
|
PublishedPipelineRunPreviewResponse,
|
2025-05-27 11:01:38 +08:00
|
|
|
PublishedPipelineRunRequest,
|
2025-06-03 10:14:48 +08:00
|
|
|
PublishedPipelineRunResponse,
|
2025-05-16 15:14:50 +08:00
|
|
|
UpdateTemplateInfoRequest,
|
|
|
|
UpdateTemplateInfoResponse,
|
2025-05-07 11:30:13 +08:00
|
|
|
} from '@/models/pipeline'
|
2025-05-26 15:57:34 +08:00
|
|
|
import type { DataSourceItem } from '@/app/components/workflow/block-selector/types'
|
2025-06-04 11:39:04 +08:00
|
|
|
import type { ToolCredential } from '@/app/components/tools/types'
|
2025-05-07 11:30:13 +08:00
|
|
|
|
|
|
|
const NAME_SPACE = 'pipeline'
|
|
|
|
|
|
|
|
export const usePipelineTemplateList = (params: PipelineTemplateListParams) => {
|
|
|
|
return useQuery<PipelineTemplateListResponse>({
|
|
|
|
queryKey: [NAME_SPACE, 'template', 'list'],
|
|
|
|
queryFn: () => {
|
2025-05-16 15:14:50 +08:00
|
|
|
return get<PipelineTemplateListResponse>('/rag/pipeline/templates', { params })
|
2025-05-07 11:30:13 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-05-07 18:09:38 +08:00
|
|
|
export const usePipelineTemplateById = (templateId: string, enabled: boolean) => {
|
2025-05-07 11:30:13 +08:00
|
|
|
return useQuery<PipelineTemplateByIdResponse>({
|
|
|
|
queryKey: [NAME_SPACE, 'template', templateId],
|
|
|
|
queryFn: () => {
|
2025-05-16 15:14:50 +08:00
|
|
|
return get<PipelineTemplateByIdResponse>(`/rag/pipeline/templates/${templateId}`)
|
2025-05-07 11:30:13 +08:00
|
|
|
},
|
2025-05-07 18:09:38 +08:00
|
|
|
enabled,
|
2025-05-07 11:30:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:14:50 +08:00
|
|
|
export const useUpdateTemplateInfo = (
|
|
|
|
mutationOptions: MutationOptions<UpdateTemplateInfoResponse, Error, UpdateTemplateInfoRequest> = {},
|
2025-05-07 14:29:01 +08:00
|
|
|
) => {
|
2025-05-07 11:30:13 +08:00
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'template', 'update'],
|
2025-05-16 15:14:50 +08:00
|
|
|
mutationFn: (request: UpdateTemplateInfoRequest) => {
|
|
|
|
const { template_id, ...rest } = request
|
|
|
|
return patch<UpdateTemplateInfoResponse>(`/rag/customized/templates/${template_id}`, {
|
2025-05-07 11:30:13 +08:00
|
|
|
body: rest,
|
|
|
|
})
|
|
|
|
},
|
2025-05-07 14:29:01 +08:00
|
|
|
...mutationOptions,
|
2025-05-07 11:30:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:14:50 +08:00
|
|
|
export const useDeleteTemplate = (
|
|
|
|
mutationOptions: MutationOptions<DeleteTemplateResponse, Error, string> = {},
|
2025-05-07 14:29:01 +08:00
|
|
|
) => {
|
2025-05-07 11:30:13 +08:00
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'template', 'delete'],
|
2025-05-16 15:14:50 +08:00
|
|
|
mutationFn: (templateId: string) => {
|
|
|
|
return del<DeleteTemplateResponse>(`/rag/customized/templates/${templateId}`)
|
2025-05-07 11:30:13 +08:00
|
|
|
},
|
2025-05-07 14:29:01 +08:00
|
|
|
...mutationOptions,
|
2025-05-07 11:30:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-05-16 15:14:50 +08:00
|
|
|
export const useExportTemplateDSL = (
|
2025-05-16 16:32:25 +08:00
|
|
|
mutationOptions: MutationOptions<ExportTemplateDSLResponse, Error, string> = {},
|
2025-05-07 14:29:01 +08:00
|
|
|
) => {
|
|
|
|
return useMutation({
|
2025-05-14 14:49:01 +08:00
|
|
|
mutationKey: [NAME_SPACE, 'dsl-export'],
|
2025-05-16 15:14:50 +08:00
|
|
|
mutationFn: (templateId: string) => {
|
|
|
|
return get<ExportTemplateDSLResponse>(`/rag/customized/templates/${templateId}`)
|
2025-05-07 11:30:13 +08:00
|
|
|
},
|
2025-05-07 14:29:01 +08:00
|
|
|
...mutationOptions,
|
2025-05-07 11:30:13 +08:00
|
|
|
})
|
|
|
|
}
|
2025-05-07 18:09:38 +08:00
|
|
|
|
|
|
|
export const useImportPipelineDSL = (
|
|
|
|
mutationOptions: MutationOptions<ImportPipelineDSLResponse, Error, ImportPipelineDSLRequest> = {},
|
|
|
|
) => {
|
|
|
|
return useMutation({
|
2025-05-14 14:49:01 +08:00
|
|
|
mutationKey: [NAME_SPACE, 'dsl-import'],
|
2025-05-07 18:09:38 +08:00
|
|
|
mutationFn: (request: ImportPipelineDSLRequest) => {
|
2025-05-16 16:32:25 +08:00
|
|
|
return post<ImportPipelineDSLResponse>('/rag/pipelines/imports', { body: request })
|
2025-05-14 14:49:01 +08:00
|
|
|
},
|
|
|
|
...mutationOptions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useImportPipelineDSLConfirm = (
|
2025-05-14 16:27:59 +08:00
|
|
|
mutationOptions: MutationOptions<ImportPipelineDSLConfirmResponse, Error, string> = {},
|
2025-05-14 14:49:01 +08:00
|
|
|
) => {
|
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'dsl-import-confirm'],
|
2025-05-14 16:27:59 +08:00
|
|
|
mutationFn: (importId: string) => {
|
2025-05-20 11:42:22 +08:00
|
|
|
return post<ImportPipelineDSLConfirmResponse>(`/rag/pipelines/imports/${importId}/confirm`)
|
2025-05-14 14:49:01 +08:00
|
|
|
},
|
|
|
|
...mutationOptions,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useCheckPipelineDependencies = (
|
|
|
|
mutationOptions: MutationOptions<PipelineCheckDependenciesResponse, Error, string> = {},
|
|
|
|
) => {
|
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'check-dependencies'],
|
|
|
|
mutationFn: (pipelineId: string) => {
|
|
|
|
return post<PipelineCheckDependenciesResponse>(`/rag/pipelines/imports/${pipelineId}/check-dependencies`)
|
2025-05-07 18:09:38 +08:00
|
|
|
},
|
|
|
|
...mutationOptions,
|
|
|
|
})
|
|
|
|
}
|
2025-05-08 18:29:49 +08:00
|
|
|
|
2025-05-21 10:53:18 +08:00
|
|
|
export const useDatasourceNodeRun = (
|
|
|
|
mutationOptions: MutationOptions<PipelineDatasourceNodeRunResponse, Error, PipelineDatasourceNodeRunRequest> = {},
|
|
|
|
) => {
|
2025-05-20 11:42:22 +08:00
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'datasource-node-run'],
|
|
|
|
mutationFn: (request: PipelineDatasourceNodeRunRequest) => {
|
|
|
|
const { pipeline_id, node_id, ...rest } = request
|
2025-05-21 10:53:18 +08:00
|
|
|
return post<PipelineDatasourceNodeRunResponse>(`/rag/pipelines/${pipeline_id}/workflows/published/nodes/${node_id}/run`, {
|
2025-05-20 11:42:22 +08:00
|
|
|
body: rest,
|
|
|
|
})
|
|
|
|
},
|
2025-05-21 10:53:18 +08:00
|
|
|
...mutationOptions,
|
2025-05-20 11:42:22 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-05-22 17:39:39 +08:00
|
|
|
export const useDraftPipelineProcessingParams = (params: PipelineProcessingParamsRequest) => {
|
2025-05-20 11:42:22 +08:00
|
|
|
const { pipeline_id, node_id } = params
|
2025-05-08 18:29:49 +08:00
|
|
|
return useQuery<PipelineProcessingParamsResponse>({
|
2025-05-20 11:42:22 +08:00
|
|
|
queryKey: [NAME_SPACE, 'pipeline-processing-params', pipeline_id],
|
2025-05-08 18:29:49 +08:00
|
|
|
queryFn: () => {
|
2025-05-22 17:39:39 +08:00
|
|
|
return get<PipelineProcessingParamsResponse>(`/rag/pipelines/${pipeline_id}/workflows/draft/processing/parameters`, {
|
|
|
|
params: {
|
|
|
|
node_id,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2025-05-23 15:10:20 +08:00
|
|
|
staleTime: 0,
|
2025-05-22 17:39:39 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const usePublishedPipelineProcessingParams = (params: PipelineProcessingParamsRequest) => {
|
|
|
|
const { pipeline_id, node_id } = params
|
|
|
|
return useQuery<PipelineProcessingParamsResponse>({
|
|
|
|
queryKey: [NAME_SPACE, 'pipeline-processing-params', pipeline_id],
|
|
|
|
queryFn: () => {
|
|
|
|
return get<PipelineProcessingParamsResponse>(`/rag/pipelines/${pipeline_id}/workflows/published/processing/parameters`, {
|
2025-05-20 11:42:22 +08:00
|
|
|
params: {
|
|
|
|
node_id,
|
|
|
|
},
|
|
|
|
})
|
2025-05-08 18:29:49 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2025-05-16 14:53:39 +08:00
|
|
|
|
2025-05-29 15:18:27 +08:00
|
|
|
export const useDataSourceList = (enabled: boolean, onSuccess?: (v: DataSourceItem[]) => void) => {
|
2025-05-26 15:57:34 +08:00
|
|
|
return useQuery<DataSourceItem[]>({
|
2025-05-16 14:53:39 +08:00
|
|
|
enabled,
|
2025-05-26 10:50:39 +08:00
|
|
|
queryKey: [NAME_SPACE, 'datasource'],
|
2025-05-28 15:35:32 +08:00
|
|
|
staleTime: 0,
|
2025-05-23 14:25:38 +08:00
|
|
|
queryFn: async () => {
|
2025-05-26 15:57:34 +08:00
|
|
|
const data = await get<DataSourceItem[]>('/rag/pipelines/datasource-plugins')
|
2025-05-29 15:18:27 +08:00
|
|
|
onSuccess?.(data)
|
2025-05-23 14:25:38 +08:00
|
|
|
return data
|
2025-05-16 14:53:39 +08:00
|
|
|
},
|
|
|
|
retry: false,
|
|
|
|
})
|
|
|
|
}
|
2025-05-21 16:37:02 +08:00
|
|
|
|
2025-06-04 15:16:02 +08:00
|
|
|
export const publishedPipelineInfoQueryKeyPrefix = [NAME_SPACE, 'published-pipeline']
|
|
|
|
|
2025-05-21 16:37:02 +08:00
|
|
|
export const usePublishedPipelineInfo = (pipelineId: string) => {
|
|
|
|
return useQuery<PublishedPipelineInfoResponse>({
|
2025-06-04 15:16:02 +08:00
|
|
|
queryKey: [...publishedPipelineInfoQueryKeyPrefix, pipelineId],
|
2025-05-21 16:37:02 +08:00
|
|
|
queryFn: () => {
|
|
|
|
return get<PublishedPipelineInfoResponse>(`/rag/pipelines/${pipelineId}/workflows/publish`)
|
|
|
|
},
|
2025-05-22 14:49:40 +08:00
|
|
|
enabled: !!pipelineId,
|
2025-05-21 16:37:02 +08:00
|
|
|
})
|
|
|
|
}
|
2025-05-27 11:01:38 +08:00
|
|
|
|
|
|
|
export const useRunPublishedPipeline = (
|
2025-06-03 10:14:48 +08:00
|
|
|
mutationOptions: MutationOptions<PublishedPipelineRunPreviewResponse | PublishedPipelineRunResponse, Error, PublishedPipelineRunRequest> = {},
|
2025-05-27 11:01:38 +08:00
|
|
|
) => {
|
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'run-published-pipeline'],
|
|
|
|
mutationFn: (request: PublishedPipelineRunRequest) => {
|
2025-06-03 10:14:48 +08:00
|
|
|
const { pipeline_id: pipelineId, is_preview, ...rest } = request
|
2025-06-03 17:42:40 +08:00
|
|
|
return post<PublishedPipelineRunPreviewResponse | PublishedPipelineRunResponse>(`/rag/pipelines/${pipelineId}/workflows/published/run`, {
|
2025-05-27 11:01:38 +08:00
|
|
|
body: {
|
|
|
|
...rest,
|
2025-06-03 10:14:48 +08:00
|
|
|
is_preview,
|
2025-05-27 11:01:38 +08:00
|
|
|
response_mode: 'blocking',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
...mutationOptions,
|
|
|
|
})
|
|
|
|
}
|
2025-06-04 11:39:04 +08:00
|
|
|
|
|
|
|
export const useDataSourceCredentials = (provider: string, pluginId: string, onSuccess: (value: ToolCredential[]) => void) => {
|
|
|
|
return useQuery<ToolCredential[]>({
|
|
|
|
queryKey: [NAME_SPACE, 'datasource-credentials', provider, pluginId],
|
|
|
|
queryFn: async () => {
|
|
|
|
const result = await get<ToolCredential[]>(`/auth/datasource/provider/${provider}/plugin/${pluginId}`)
|
|
|
|
onSuccess(result)
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
enabled: !!provider && !!pluginId,
|
|
|
|
retry: 2,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useUpdateDataSourceCredentials = (
|
|
|
|
) => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
|
|
mutationKey: [NAME_SPACE, 'update-datasource-credentials'],
|
|
|
|
mutationFn: ({
|
|
|
|
provider,
|
|
|
|
pluginId,
|
|
|
|
credentials,
|
|
|
|
}: { provider: string; pluginId: string; credentials: Record<string, any>; }) => {
|
|
|
|
return post(`/auth/datasource/provider/${provider}/plugin/${pluginId}`, {
|
|
|
|
body: {
|
|
|
|
credentials,
|
|
|
|
},
|
|
|
|
}).then(() => {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: [NAME_SPACE, 'datasource'],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|