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 7.9KB

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