Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

use-tools.ts 8.0KB

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