dify/web/service/use-tools.ts

119 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-11-09 12:51:10 +08:00
import { get, post } from './base'
2024-11-07 15:24:02 +08:00
import type {
2024-11-09 12:51:10 +08:00
Collection,
2024-11-07 15:24:02 +08:00
Tool,
} from '@/app/components/tools/types'
import type { ToolWithProvider } from '@/app/components/workflow/types'
import { useInvalid } from './use-base'
2024-11-07 15:24:02 +08:00
import {
2024-11-09 12:51:10 +08:00
useMutation,
2024-11-07 15:24:02 +08:00
useQuery,
2024-11-09 12:51:10 +08:00
useQueryClient,
2024-11-07 15:24:02 +08:00
} from '@tanstack/react-query'
const NAME_SPACE = 'tools'
2024-11-14 14:47:53 +08:00
const useAllToolProvidersKey = [NAME_SPACE, 'allToolProviders']
export const useAllToolProviders = () => {
return useQuery({
queryKey: useAllToolProvidersKey,
queryFn: () => get<Collection[]>('/workspaces/current/tool-providers'),
initialData: [],
})
}
2024-11-13 15:17:22 +08:00
const useAllBuiltInToolsKey = [NAME_SPACE, 'builtIn']
2024-11-07 15:24:02 +08:00
export const useAllBuiltInTools = () => {
return useQuery<ToolWithProvider[]>({
2024-11-13 15:17:22 +08:00
queryKey: useAllBuiltInToolsKey,
2024-11-07 15:24:02 +08:00
queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/builtin'),
})
}
2024-11-13 15:17:22 +08:00
export const useInvalidateAllBuiltInTools = () => {
return useInvalid(useAllBuiltInToolsKey)
2024-11-13 15:17:22 +08:00
}
2024-11-07 15:24:02 +08:00
const useAllCustomToolsKey = [NAME_SPACE, 'customTools']
export const useAllCustomTools = () => {
return useQuery<ToolWithProvider[]>({
queryKey: useAllCustomToolsKey,
queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/api'),
})
}
export const useInvalidateAllCustomTools = () => {
return useInvalid(useAllCustomToolsKey)
2024-11-07 15:24:02 +08:00
}
const useAllWorkflowToolsKey = [NAME_SPACE, 'workflowTools']
2024-11-07 15:24:02 +08:00
export const useAllWorkflowTools = () => {
return useQuery<ToolWithProvider[]>({
queryKey: useAllWorkflowToolsKey,
2024-11-07 15:24:02 +08:00
queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/workflow'),
})
}
export const useInvalidateAllWorkflowTools = () => {
return useInvalid(useAllWorkflowToolsKey)
}
2024-11-09 12:51:10 +08:00
export const useBuiltinProviderInfo = (providerName: string) => {
return useQuery({
queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
queryFn: () => get<Collection>(`/workspaces/current/tool-provider/builtin/${providerName}/info`),
})
}
export const useInvalidateBuiltinProviderInfo = () => {
const queryClient = useQueryClient()
return (providerName: string) => {
queryClient.invalidateQueries(
{
queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
})
}
}
export const useBuiltinTools = (providerName: string) => {
2024-11-07 15:24:02 +08:00
return useQuery({
2024-11-09 12:51:10 +08:00
queryKey: [NAME_SPACE, 'builtin-provider-tools', providerName],
queryFn: () => get<Tool[]>(`/workspaces/current/tool-provider/builtin/${providerName}/tools`),
})
}
export const useUpdateProviderCredentials = ({
onSuccess,
}: {
onSuccess?: () => void
}) => {
return useMutation({
2024-11-09 14:44:48 +08:00
mutationKey: [NAME_SPACE, 'update-provider-credentials'],
2024-11-09 12:51:10 +08:00
mutationFn: (payload: { providerName: string, credentials: Record<string, any> }) => {
const { providerName, credentials } = payload
return post(`/workspaces/current/tool-provider/builtin/${providerName}/update`, {
body: {
credentials,
},
})
},
onSuccess,
})
}
export const useRemoveProviderCredentials = ({
onSuccess,
}: {
onSuccess?: () => void
}) => {
return useMutation({
2024-11-09 14:44:48 +08:00
mutationKey: [NAME_SPACE, 'remove-provider-credentials'],
2024-11-09 12:51:10 +08:00
mutationFn: (providerName: string) => {
return post(`/workspaces/current/tool-provider/builtin/${providerName}/delete`, {
body: {},
})
},
onSuccess,
2024-11-07 15:24:02 +08:00
})
}