You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-tools.ts 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { del, get, post, put } from './base'
  2. import type {
  3. Collection,
  4. MCPServerDetail,
  5. Tool,
  6. } from '@/app/components/tools/types'
  7. import type { RAGRecommendedPlugins, ToolWithProvider } from '@/app/components/workflow/types'
  8. import type { AppIconType } from '@/types/app'
  9. import { useInvalid } from './use-base'
  10. import {
  11. useMutation,
  12. useQuery,
  13. useQueryClient,
  14. } from '@tanstack/react-query'
  15. const NAME_SPACE = 'tools'
  16. const useAllToolProvidersKey = [NAME_SPACE, 'allToolProviders']
  17. export const useAllToolProviders = (enabled = true) => {
  18. return useQuery<Collection[]>({
  19. queryKey: useAllToolProvidersKey,
  20. queryFn: () => get<Collection[]>('/workspaces/current/tool-providers'),
  21. enabled,
  22. })
  23. }
  24. export const useInvalidateAllToolProviders = () => {
  25. return useInvalid(useAllToolProvidersKey)
  26. }
  27. const useAllBuiltInToolsKey = [NAME_SPACE, 'builtIn']
  28. export const useAllBuiltInTools = () => {
  29. return useQuery<ToolWithProvider[]>({
  30. queryKey: useAllBuiltInToolsKey,
  31. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/builtin'),
  32. })
  33. }
  34. export const useInvalidateAllBuiltInTools = () => {
  35. return useInvalid(useAllBuiltInToolsKey)
  36. }
  37. const useAllCustomToolsKey = [NAME_SPACE, 'customTools']
  38. export const useAllCustomTools = () => {
  39. return useQuery<ToolWithProvider[]>({
  40. queryKey: useAllCustomToolsKey,
  41. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/api'),
  42. })
  43. }
  44. export const useInvalidateAllCustomTools = () => {
  45. return useInvalid(useAllCustomToolsKey)
  46. }
  47. const useAllWorkflowToolsKey = [NAME_SPACE, 'workflowTools']
  48. export const useAllWorkflowTools = () => {
  49. return useQuery<ToolWithProvider[]>({
  50. queryKey: useAllWorkflowToolsKey,
  51. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/workflow'),
  52. })
  53. }
  54. export const useInvalidateAllWorkflowTools = () => {
  55. return useInvalid(useAllWorkflowToolsKey)
  56. }
  57. const useAllMCPToolsKey = [NAME_SPACE, 'MCPTools']
  58. export const useAllMCPTools = () => {
  59. return useQuery<ToolWithProvider[]>({
  60. queryKey: useAllMCPToolsKey,
  61. queryFn: () => get<ToolWithProvider[]>('/workspaces/current/tools/mcp'),
  62. })
  63. }
  64. export const useInvalidateAllMCPTools = () => {
  65. return useInvalid(useAllMCPToolsKey)
  66. }
  67. export const useCreateMCP = () => {
  68. return useMutation({
  69. mutationKey: [NAME_SPACE, 'create-mcp'],
  70. mutationFn: (payload: {
  71. name: string
  72. server_url: string
  73. icon_type: AppIconType
  74. icon: string
  75. icon_background?: string | null
  76. timeout?: number
  77. sse_read_timeout?: number
  78. headers?: Record<string, string>
  79. }) => {
  80. return post<ToolWithProvider>('workspaces/current/tool-provider/mcp', {
  81. body: {
  82. ...payload,
  83. },
  84. })
  85. },
  86. })
  87. }
  88. export const useUpdateMCP = ({
  89. onSuccess,
  90. }: {
  91. onSuccess?: () => void
  92. }) => {
  93. return useMutation({
  94. mutationKey: [NAME_SPACE, 'update-mcp'],
  95. mutationFn: (payload: {
  96. name: string
  97. server_url: string
  98. icon_type: AppIconType
  99. icon: string
  100. icon_background?: string | null
  101. provider_id: string
  102. timeout?: number
  103. sse_read_timeout?: number
  104. headers?: Record<string, string>
  105. }) => {
  106. return put('workspaces/current/tool-provider/mcp', {
  107. body: {
  108. ...payload,
  109. },
  110. })
  111. },
  112. onSuccess,
  113. })
  114. }
  115. export const useDeleteMCP = ({
  116. onSuccess,
  117. }: {
  118. onSuccess?: () => void
  119. }) => {
  120. return useMutation({
  121. mutationKey: [NAME_SPACE, 'delete-mcp'],
  122. mutationFn: (id: string) => {
  123. return del('/workspaces/current/tool-provider/mcp', {
  124. body: {
  125. provider_id: id,
  126. },
  127. })
  128. },
  129. onSuccess,
  130. })
  131. }
  132. export const useAuthorizeMCP = () => {
  133. return useMutation({
  134. mutationKey: [NAME_SPACE, 'authorize-mcp'],
  135. mutationFn: (payload: { provider_id: string; }) => {
  136. return post<{ result?: string; authorization_url?: string }>('/workspaces/current/tool-provider/mcp/auth', {
  137. body: payload,
  138. })
  139. },
  140. })
  141. }
  142. export const useUpdateMCPAuthorizationToken = () => {
  143. return useMutation({
  144. mutationKey: [NAME_SPACE, 'refresh-mcp-server-code'],
  145. mutationFn: (payload: { provider_id: string; authorization_code: string }) => {
  146. return get<MCPServerDetail>('/workspaces/current/tool-provider/mcp/token', {
  147. params: {
  148. ...payload,
  149. },
  150. })
  151. },
  152. })
  153. }
  154. export const useMCPTools = (providerID: string) => {
  155. return useQuery({
  156. enabled: !!providerID,
  157. queryKey: [NAME_SPACE, 'get-MCP-provider-tool', providerID],
  158. queryFn: () => get<{ tools: Tool[] }>(`/workspaces/current/tool-provider/mcp/tools/${providerID}`),
  159. })
  160. }
  161. export const useInvalidateMCPTools = () => {
  162. const queryClient = useQueryClient()
  163. return (providerID: string) => {
  164. queryClient.invalidateQueries(
  165. {
  166. queryKey: [NAME_SPACE, 'get-MCP-provider-tool', providerID],
  167. })
  168. }
  169. }
  170. export const useUpdateMCPTools = () => {
  171. return useMutation({
  172. mutationFn: (providerID: string) => get<{ tools: Tool[] }>(`/workspaces/current/tool-provider/mcp/update/${providerID}`),
  173. })
  174. }
  175. export const useMCPServerDetail = (appID: string) => {
  176. return useQuery<MCPServerDetail>({
  177. queryKey: [NAME_SPACE, 'MCPServerDetail', appID],
  178. queryFn: () => get<MCPServerDetail>(`/apps/${appID}/server`),
  179. })
  180. }
  181. export const useInvalidateMCPServerDetail = () => {
  182. const queryClient = useQueryClient()
  183. return (appID: string) => {
  184. queryClient.invalidateQueries(
  185. {
  186. queryKey: [NAME_SPACE, 'MCPServerDetail', appID],
  187. })
  188. }
  189. }
  190. export const useCreateMCPServer = () => {
  191. return useMutation({
  192. mutationKey: [NAME_SPACE, 'create-mcp-server'],
  193. mutationFn: (payload: {
  194. appID: string
  195. description?: string
  196. parameters?: Record<string, string>
  197. }) => {
  198. const { appID, ...rest } = payload
  199. return post(`apps/${appID}/server`, {
  200. body: {
  201. ...rest,
  202. },
  203. })
  204. },
  205. })
  206. }
  207. export const useUpdateMCPServer = () => {
  208. return useMutation({
  209. mutationKey: [NAME_SPACE, 'update-mcp-server'],
  210. mutationFn: (payload: {
  211. appID: string
  212. id: string
  213. description?: string
  214. status?: string
  215. parameters?: Record<string, string>
  216. }) => {
  217. const { appID, ...rest } = payload
  218. return put(`apps/${appID}/server`, {
  219. body: {
  220. ...rest,
  221. },
  222. })
  223. },
  224. })
  225. }
  226. export const useRefreshMCPServerCode = () => {
  227. return useMutation({
  228. mutationKey: [NAME_SPACE, 'refresh-mcp-server-code'],
  229. mutationFn: (appID: string) => {
  230. return get<MCPServerDetail>(`apps/${appID}/server/refresh`)
  231. },
  232. })
  233. }
  234. export const useBuiltinProviderInfo = (providerName: string) => {
  235. return useQuery({
  236. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  237. queryFn: () => get<Collection>(`/workspaces/current/tool-provider/builtin/${providerName}/info`),
  238. })
  239. }
  240. export const useInvalidateBuiltinProviderInfo = () => {
  241. const queryClient = useQueryClient()
  242. return (providerName: string) => {
  243. queryClient.invalidateQueries(
  244. {
  245. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  246. })
  247. }
  248. }
  249. export const useBuiltinTools = (providerName: string) => {
  250. return useQuery({
  251. queryKey: [NAME_SPACE, 'builtin-provider-tools', providerName],
  252. queryFn: () => get<Tool[]>(`/workspaces/current/tool-provider/builtin/${providerName}/tools`),
  253. })
  254. }
  255. export const useUpdateProviderCredentials = ({
  256. onSuccess,
  257. }: {
  258. onSuccess?: () => void
  259. }) => {
  260. return useMutation({
  261. mutationKey: [NAME_SPACE, 'update-provider-credentials'],
  262. mutationFn: (payload: { providerName: string, credentials: Record<string, any> }) => {
  263. const { providerName, credentials } = payload
  264. return post(`/workspaces/current/tool-provider/builtin/${providerName}/update`, {
  265. body: {
  266. credentials,
  267. },
  268. })
  269. },
  270. onSuccess,
  271. })
  272. }
  273. export const useRemoveProviderCredentials = ({
  274. onSuccess,
  275. }: {
  276. onSuccess?: () => void
  277. }) => {
  278. return useMutation({
  279. mutationKey: [NAME_SPACE, 'remove-provider-credentials'],
  280. mutationFn: (providerName: string) => {
  281. return post(`/workspaces/current/tool-provider/builtin/${providerName}/delete`, {
  282. body: {},
  283. })
  284. },
  285. onSuccess,
  286. })
  287. }
  288. const useRAGRecommendedPluginListKey = [NAME_SPACE, 'rag-recommended-plugins']
  289. export const useRAGRecommendedPlugins = () => {
  290. return useQuery<RAGRecommendedPlugins>({
  291. queryKey: useRAGRecommendedPluginListKey,
  292. queryFn: () => get<RAGRecommendedPlugins>('/rag/pipelines/recommended-plugins'),
  293. })
  294. }
  295. export const useInvalidateRAGRecommendedPlugins = () => {
  296. return useInvalid(useRAGRecommendedPluginListKey)
  297. }