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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = (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. }) => {
  77. return post<ToolWithProvider>('workspaces/current/tool-provider/mcp', {
  78. body: {
  79. ...payload,
  80. },
  81. })
  82. },
  83. })
  84. }
  85. export const useUpdateMCP = ({
  86. onSuccess,
  87. }: {
  88. onSuccess?: () => void
  89. }) => {
  90. return useMutation({
  91. mutationKey: [NAME_SPACE, 'update-mcp'],
  92. mutationFn: (payload: {
  93. name: string
  94. server_url: string
  95. icon_type: AppIconType
  96. icon: string
  97. icon_background?: string | null
  98. provider_id: string
  99. }) => {
  100. return put('workspaces/current/tool-provider/mcp', {
  101. body: {
  102. ...payload,
  103. },
  104. })
  105. },
  106. onSuccess,
  107. })
  108. }
  109. export const useDeleteMCP = ({
  110. onSuccess,
  111. }: {
  112. onSuccess?: () => void
  113. }) => {
  114. return useMutation({
  115. mutationKey: [NAME_SPACE, 'delete-mcp'],
  116. mutationFn: (id: string) => {
  117. return del('/workspaces/current/tool-provider/mcp', {
  118. body: {
  119. provider_id: id,
  120. },
  121. })
  122. },
  123. onSuccess,
  124. })
  125. }
  126. export const useAuthorizeMCP = () => {
  127. return useMutation({
  128. mutationKey: [NAME_SPACE, 'authorize-mcp'],
  129. mutationFn: (payload: { provider_id: string; }) => {
  130. return post<{ result?: string; authorization_url?: string }>('/workspaces/current/tool-provider/mcp/auth', {
  131. body: payload,
  132. })
  133. },
  134. })
  135. }
  136. export const useUpdateMCPAuthorizationToken = () => {
  137. return useMutation({
  138. mutationKey: [NAME_SPACE, 'refresh-mcp-server-code'],
  139. mutationFn: (payload: { provider_id: string; authorization_code: string }) => {
  140. return get<MCPServerDetail>('/workspaces/current/tool-provider/mcp/token', {
  141. params: {
  142. ...payload,
  143. },
  144. })
  145. },
  146. })
  147. }
  148. export const useMCPTools = (providerID: string) => {
  149. return useQuery({
  150. enabled: !!providerID,
  151. queryKey: [NAME_SPACE, 'get-MCP-provider-tool', providerID],
  152. queryFn: () => get<{ tools: Tool[] }>(`/workspaces/current/tool-provider/mcp/tools/${providerID}`),
  153. })
  154. }
  155. export const useInvalidateMCPTools = () => {
  156. const queryClient = useQueryClient()
  157. return (providerID: string) => {
  158. queryClient.invalidateQueries(
  159. {
  160. queryKey: [NAME_SPACE, 'get-MCP-provider-tool', providerID],
  161. })
  162. }
  163. }
  164. export const useUpdateMCPTools = () => {
  165. return useMutation({
  166. mutationFn: (providerID: string) => get<{ tools: Tool[] }>(`/workspaces/current/tool-provider/mcp/update/${providerID}`),
  167. })
  168. }
  169. export const useMCPServerDetail = (appID: string) => {
  170. return useQuery<MCPServerDetail>({
  171. queryKey: [NAME_SPACE, 'MCPServerDetail', appID],
  172. queryFn: () => get<MCPServerDetail>(`/apps/${appID}/server`),
  173. })
  174. }
  175. export const useInvalidateMCPServerDetail = () => {
  176. const queryClient = useQueryClient()
  177. return (appID: string) => {
  178. queryClient.invalidateQueries(
  179. {
  180. queryKey: [NAME_SPACE, 'MCPServerDetail', appID],
  181. })
  182. }
  183. }
  184. export const useCreateMCPServer = () => {
  185. return useMutation({
  186. mutationKey: [NAME_SPACE, 'create-mcp-server'],
  187. mutationFn: (payload: {
  188. appID: string
  189. description?: string
  190. parameters?: Record<string, string>
  191. }) => {
  192. const { appID, ...rest } = payload
  193. return post(`apps/${appID}/server`, {
  194. body: {
  195. ...rest,
  196. },
  197. })
  198. },
  199. })
  200. }
  201. export const useUpdateMCPServer = () => {
  202. return useMutation({
  203. mutationKey: [NAME_SPACE, 'update-mcp-server'],
  204. mutationFn: (payload: {
  205. appID: string
  206. id: string
  207. description?: string
  208. status?: string
  209. parameters?: Record<string, string>
  210. }) => {
  211. const { appID, ...rest } = payload
  212. return put(`apps/${appID}/server`, {
  213. body: {
  214. ...rest,
  215. },
  216. })
  217. },
  218. })
  219. }
  220. export const useRefreshMCPServerCode = () => {
  221. return useMutation({
  222. mutationKey: [NAME_SPACE, 'refresh-mcp-server-code'],
  223. mutationFn: (appID: string) => {
  224. return get<MCPServerDetail>(`apps/${appID}/server/refresh`)
  225. },
  226. })
  227. }
  228. export const useBuiltinProviderInfo = (providerName: string) => {
  229. return useQuery({
  230. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  231. queryFn: () => get<Collection>(`/workspaces/current/tool-provider/builtin/${providerName}/info`),
  232. })
  233. }
  234. export const useInvalidateBuiltinProviderInfo = () => {
  235. const queryClient = useQueryClient()
  236. return (providerName: string) => {
  237. queryClient.invalidateQueries(
  238. {
  239. queryKey: [NAME_SPACE, 'builtin-provider-info', providerName],
  240. })
  241. }
  242. }
  243. export const useBuiltinTools = (providerName: string) => {
  244. return useQuery({
  245. queryKey: [NAME_SPACE, 'builtin-provider-tools', providerName],
  246. queryFn: () => get<Tool[]>(`/workspaces/current/tool-provider/builtin/${providerName}/tools`),
  247. })
  248. }
  249. export const useUpdateProviderCredentials = ({
  250. onSuccess,
  251. }: {
  252. onSuccess?: () => void
  253. }) => {
  254. return useMutation({
  255. mutationKey: [NAME_SPACE, 'update-provider-credentials'],
  256. mutationFn: (payload: { providerName: string, credentials: Record<string, any> }) => {
  257. const { providerName, credentials } = payload
  258. return post(`/workspaces/current/tool-provider/builtin/${providerName}/update`, {
  259. body: {
  260. credentials,
  261. },
  262. })
  263. },
  264. onSuccess,
  265. })
  266. }
  267. export const useRemoveProviderCredentials = ({
  268. onSuccess,
  269. }: {
  270. onSuccess?: () => void
  271. }) => {
  272. return useMutation({
  273. mutationKey: [NAME_SPACE, 'remove-provider-credentials'],
  274. mutationFn: (providerName: string) => {
  275. return post(`/workspaces/current/tool-provider/builtin/${providerName}/delete`, {
  276. body: {},
  277. })
  278. },
  279. onSuccess,
  280. })
  281. }