use-tools.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { get, post } from './base'
  2. import type {
  3. Collection,
  4. Tool,
  5. } from '@/app/components/tools/types'
  6. import type { ToolWithProvider } from '@/app/components/workflow/types'
  7. import { useInvalid } from './use-base'
  8. import {
  9. useMutation,
  10. useQuery,
  11. useQueryClient,
  12. } from '@tanstack/react-query'
  13. const NAME_SPACE = 'tools'
  14. const useAllToolProvidersKey = [NAME_SPACE, 'allToolProviders']
  15. export const useAllToolProviders = () => {
  16. return useQuery<Collection[]>({
  17. queryKey: useAllToolProvidersKey,
  18. queryFn: () => get<Collection[]>('/workspaces/current/tool-providers'),
  19. })
  20. }
  21. export const useInvalidateAllToolProviders = () => {
  22. return useInvalid(useAllToolProvidersKey)
  23. }
  24. const useAllBuiltInToolsKey = [NAME_SPACE, 'builtIn']
  25. export const useAllBuiltInTools = () => {
  26. return useQuery<ToolWithProvider[]>({
  27. queryKey: useAllBuiltInToolsKey,
  28. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/builtin'),
  29. })
  30. }
  31. export const useInvalidateAllBuiltInTools = () => {
  32. return useInvalid(useAllBuiltInToolsKey)
  33. }
  34. const useAllCustomToolsKey = [NAME_SPACE, 'customTools']
  35. export const useAllCustomTools = () => {
  36. return useQuery<ToolWithProvider[]>({
  37. queryKey: useAllCustomToolsKey,
  38. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/api'),
  39. })
  40. }
  41. export const useInvalidateAllCustomTools = () => {
  42. return useInvalid(useAllCustomToolsKey)
  43. }
  44. const useAllWorkflowToolsKey = [NAME_SPACE, 'workflowTools']
  45. export const useAllWorkflowTools = () => {
  46. return useQuery<ToolWithProvider[]>({
  47. queryKey: useAllWorkflowToolsKey,
  48. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/workflow'),
  49. })
  50. }
  51. export const useInvalidateAllWorkflowTools = () => {
  52. return useInvalid(useAllWorkflowToolsKey)
  53. }
  54. export const useBuiltinProviderInfo = (providerName: string) => {
  55. return useQuery({
  56. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  57. queryFn: () => get<Collection>(`/workspaces/current/tool-provider/builtin/${providerName}/info`),
  58. })
  59. }
  60. export const useInvalidateBuiltinProviderInfo = () => {
  61. const queryClient = useQueryClient()
  62. return (providerName: string) => {
  63. queryClient.invalidateQueries(
  64. {
  65. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  66. })
  67. }
  68. }
  69. export const useBuiltinTools = (providerName: string) => {
  70. return useQuery({
  71. queryKey: [NAME_SPACE, 'builtin-provider-tools', providerName],
  72. queryFn: () => get<Tool[]>(`/workspaces/current/tool-provider/builtin/${providerName}/tools`),
  73. })
  74. }
  75. export const useUpdateProviderCredentials = ({
  76. onSuccess,
  77. }: {
  78. onSuccess?: () => void
  79. }) => {
  80. return useMutation({
  81. mutationKey: [NAME_SPACE, 'update-provider-credentials'],
  82. mutationFn: (payload: { providerName: string, credentials: Record<string, any> }) => {
  83. const { providerName, credentials } = payload
  84. return post(`/workspaces/current/tool-provider/builtin/${providerName}/update`, {
  85. body: {
  86. credentials,
  87. },
  88. })
  89. },
  90. onSuccess,
  91. })
  92. }
  93. export const useRemoveProviderCredentials = ({
  94. onSuccess,
  95. }: {
  96. onSuccess?: () => void
  97. }) => {
  98. return useMutation({
  99. mutationKey: [NAME_SPACE, 'remove-provider-credentials'],
  100. mutationFn: (providerName: string) => {
  101. return post(`/workspaces/current/tool-provider/builtin/${providerName}/delete`, {
  102. body: {},
  103. })
  104. },
  105. onSuccess,
  106. })
  107. }