Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. 'use client'
  2. import React, { useCallback, useEffect } from 'react'
  3. import type { FC } from 'react'
  4. import { useBoolean } from 'ahooks'
  5. import copy from 'copy-to-clipboard'
  6. import { useTranslation } from 'react-i18next'
  7. import { useAppContext } from '@/context/app-context'
  8. import {
  9. RiCloseLine,
  10. RiLoader2Line,
  11. RiLoopLeftLine,
  12. } from '@remixicon/react'
  13. import type { ToolWithProvider } from '../../../workflow/types'
  14. import Icon from '@/app/components/plugins/card/base/card-icon'
  15. import ActionButton from '@/app/components/base/action-button'
  16. import Button from '@/app/components/base/button'
  17. import Confirm from '@/app/components/base/confirm'
  18. import Indicator from '@/app/components/header/indicator'
  19. import Tooltip from '@/app/components/base/tooltip'
  20. import MCPModal from '../modal'
  21. import OperationDropdown from './operation-dropdown'
  22. import ListLoading from './list-loading'
  23. import ToolItem from './tool-item'
  24. import {
  25. useAuthorizeMCP,
  26. useDeleteMCP,
  27. useInvalidateMCPTools,
  28. useMCPTools,
  29. useUpdateMCP,
  30. useUpdateMCPTools,
  31. } from '@/service/use-tools'
  32. import { openOAuthPopup } from '@/hooks/use-oauth'
  33. import cn from '@/utils/classnames'
  34. type Props = {
  35. detail: ToolWithProvider
  36. onUpdate: (isDelete?: boolean) => void
  37. onHide: () => void
  38. isTriggerAuthorize: boolean
  39. onFirstCreate: () => void
  40. }
  41. const MCPDetailContent: FC<Props> = ({
  42. detail,
  43. onUpdate,
  44. onHide,
  45. isTriggerAuthorize,
  46. onFirstCreate,
  47. }) => {
  48. const { t } = useTranslation()
  49. const { isCurrentWorkspaceManager } = useAppContext()
  50. const { data, isFetching: isGettingTools } = useMCPTools(detail.is_team_authorization ? detail.id : '')
  51. const invalidateMCPTools = useInvalidateMCPTools()
  52. const { mutateAsync: updateTools, isPending: isUpdating } = useUpdateMCPTools()
  53. const { mutateAsync: authorizeMcp, isPending: isAuthorizing } = useAuthorizeMCP()
  54. const toolList = data?.tools || []
  55. const [isShowUpdateConfirm, {
  56. setTrue: showUpdateConfirm,
  57. setFalse: hideUpdateConfirm,
  58. }] = useBoolean(false)
  59. const handleUpdateTools = useCallback(async () => {
  60. hideUpdateConfirm()
  61. if (!detail)
  62. return
  63. await updateTools(detail.id)
  64. invalidateMCPTools(detail.id)
  65. onUpdate()
  66. }, [detail, hideUpdateConfirm, invalidateMCPTools, onUpdate, updateTools])
  67. const { mutateAsync: updateMCP } = useUpdateMCP({})
  68. const { mutateAsync: deleteMCP } = useDeleteMCP({})
  69. const [isShowUpdateModal, {
  70. setTrue: showUpdateModal,
  71. setFalse: hideUpdateModal,
  72. }] = useBoolean(false)
  73. const [isShowDeleteConfirm, {
  74. setTrue: showDeleteConfirm,
  75. setFalse: hideDeleteConfirm,
  76. }] = useBoolean(false)
  77. const [deleting, {
  78. setTrue: showDeleting,
  79. setFalse: hideDeleting,
  80. }] = useBoolean(false)
  81. const handleOAuthCallback = useCallback(() => {
  82. if (!isCurrentWorkspaceManager)
  83. return
  84. if (!detail.id)
  85. return
  86. handleUpdateTools()
  87. }, [detail.id, handleUpdateTools, isCurrentWorkspaceManager])
  88. const handleAuthorize = useCallback(async () => {
  89. onFirstCreate()
  90. if (!isCurrentWorkspaceManager)
  91. return
  92. if (!detail)
  93. return
  94. const res = await authorizeMcp({
  95. provider_id: detail.id,
  96. })
  97. if (res.result === 'success')
  98. handleUpdateTools()
  99. else if (res.authorization_url)
  100. openOAuthPopup(res.authorization_url, handleOAuthCallback)
  101. }, [onFirstCreate, isCurrentWorkspaceManager, detail, authorizeMcp, handleUpdateTools, handleOAuthCallback])
  102. const handleUpdate = useCallback(async (data: any) => {
  103. if (!detail)
  104. return
  105. const res = await updateMCP({
  106. ...data,
  107. provider_id: detail.id,
  108. })
  109. if ((res as any)?.result === 'success') {
  110. hideUpdateModal()
  111. onUpdate()
  112. handleAuthorize()
  113. }
  114. }, [detail, updateMCP, hideUpdateModal, onUpdate, handleAuthorize])
  115. const handleDelete = useCallback(async () => {
  116. if (!detail)
  117. return
  118. showDeleting()
  119. const res = await deleteMCP(detail.id)
  120. hideDeleting()
  121. if ((res as any)?.result === 'success') {
  122. hideDeleteConfirm()
  123. onUpdate(true)
  124. }
  125. }, [detail, showDeleting, deleteMCP, hideDeleting, hideDeleteConfirm, onUpdate])
  126. useEffect(() => {
  127. if (isTriggerAuthorize)
  128. handleAuthorize()
  129. // eslint-disable-next-line react-hooks/exhaustive-deps
  130. }, [])
  131. if (!detail)
  132. return null
  133. return (
  134. <>
  135. <div className={cn('shrink-0 border-b border-divider-subtle bg-components-panel-bg p-4 pb-3')}>
  136. <div className='flex'>
  137. <div className='shrink-0 overflow-hidden rounded-xl border border-components-panel-border-subtle'>
  138. <Icon src={detail.icon} />
  139. </div>
  140. <div className='ml-3 w-0 grow'>
  141. <div className='flex h-5 items-center'>
  142. <div className='system-md-semibold truncate text-text-primary' title={detail.name}>{detail.name}</div>
  143. </div>
  144. <div className='mt-0.5 flex items-center gap-1'>
  145. <Tooltip popupContent={t('tools.mcp.identifier')}>
  146. <div className='system-xs-regular shrink-0 cursor-pointer text-text-secondary' onClick={() => copy(detail.server_identifier || '')}>{detail.server_identifier}</div>
  147. </Tooltip>
  148. <div className='system-xs-regular shrink-0 text-text-quaternary'>·</div>
  149. <Tooltip popupContent={t('tools.mcp.modal.serverUrl')}>
  150. <div className='system-xs-regular truncate text-text-secondary'>{detail.server_url}</div>
  151. </Tooltip>
  152. </div>
  153. </div>
  154. <div className='flex gap-1'>
  155. <OperationDropdown
  156. onEdit={showUpdateModal}
  157. onRemove={showDeleteConfirm}
  158. />
  159. <ActionButton onClick={onHide}>
  160. <RiCloseLine className='h-4 w-4' />
  161. </ActionButton>
  162. </div>
  163. </div>
  164. <div className='mt-5'>
  165. {!isAuthorizing && detail.is_team_authorization && (
  166. <Button
  167. variant='secondary'
  168. className='w-full'
  169. onClick={handleAuthorize}
  170. disabled={!isCurrentWorkspaceManager}
  171. >
  172. <Indicator className='mr-2' color={'green'} />
  173. {t('tools.auth.authorized')}
  174. </Button>
  175. )}
  176. {!detail.is_team_authorization && !isAuthorizing && (
  177. <Button
  178. variant='primary'
  179. className='w-full'
  180. onClick={handleAuthorize}
  181. disabled={!isCurrentWorkspaceManager}
  182. >
  183. {t('tools.mcp.authorize')}
  184. </Button>
  185. )}
  186. {isAuthorizing && (
  187. <Button
  188. variant='primary'
  189. className='w-full'
  190. disabled
  191. >
  192. <RiLoader2Line className={cn('mr-1 h-4 w-4 animate-spin')} />
  193. {t('tools.mcp.authorizing')}
  194. </Button>
  195. )}
  196. </div>
  197. </div>
  198. <div className='flex grow flex-col'>
  199. {((detail.is_team_authorization && isGettingTools) || isUpdating) && (
  200. <>
  201. <div className='flex shrink-0 justify-between gap-2 px-4 pb-1 pt-2'>
  202. <div className='flex h-6 items-center'>
  203. {!isUpdating && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.gettingTools')}</div>}
  204. {isUpdating && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.updateTools')}</div>}
  205. </div>
  206. <div></div>
  207. </div>
  208. <div className='flex h-full w-full grow flex-col overflow-hidden px-4 pb-4'>
  209. <ListLoading />
  210. </div>
  211. </>
  212. )}
  213. {!isUpdating && detail.is_team_authorization && !isGettingTools && !toolList.length && (
  214. <div className='flex h-full w-full flex-col items-center justify-center'>
  215. <div className='system-sm-regular mb-3 text-text-tertiary'>{t('tools.mcp.toolsEmpty')}</div>
  216. <Button
  217. variant='primary'
  218. onClick={handleUpdateTools}
  219. >{t('tools.mcp.getTools')}</Button>
  220. </div>
  221. )}
  222. {!isUpdating && !isGettingTools && toolList.length > 0 && (
  223. <>
  224. <div className='flex shrink-0 justify-between gap-2 px-4 pb-1 pt-2'>
  225. <div className='flex h-6 items-center'>
  226. {toolList.length > 1 && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.toolsNum', { count: toolList.length })}</div>}
  227. {toolList.length === 1 && <div className='system-sm-semibold-uppercase text-text-secondary'>{t('tools.mcp.onlyTool')}</div>}
  228. </div>
  229. <div>
  230. <Button size='small' onClick={showUpdateConfirm}>
  231. <RiLoopLeftLine className='mr-1 h-3.5 w-3.5' />
  232. {t('tools.mcp.update')}
  233. </Button>
  234. </div>
  235. </div>
  236. <div className='flex h-0 w-full grow flex-col gap-2 overflow-y-auto px-4 pb-4'>
  237. {toolList.map(tool => (
  238. <ToolItem
  239. key={`${detail.id}${tool.name}`}
  240. tool={tool}
  241. />
  242. ))}
  243. </div>
  244. </>
  245. )}
  246. {!isUpdating && !detail.is_team_authorization && (
  247. <div className='flex h-full w-full flex-col items-center justify-center'>
  248. {!isAuthorizing && <div className='system-md-medium mb-1 text-text-secondary'>{t('tools.mcp.authorizingRequired')}</div>}
  249. {isAuthorizing && <div className='system-md-medium mb-1 text-text-secondary'>{t('tools.mcp.authorizing')}</div>}
  250. <div className='system-sm-regular text-text-tertiary'>{t('tools.mcp.authorizeTip')}</div>
  251. </div>
  252. )}
  253. </div>
  254. {isShowUpdateModal && (
  255. <MCPModal
  256. data={detail}
  257. show={isShowUpdateModal}
  258. onConfirm={handleUpdate}
  259. onHide={hideUpdateModal}
  260. />
  261. )}
  262. {isShowDeleteConfirm && (
  263. <Confirm
  264. isShow
  265. title={t('tools.mcp.delete')}
  266. content={
  267. <div>
  268. {t('tools.mcp.deleteConfirmTitle', { mcp: detail.name })}
  269. </div>
  270. }
  271. onCancel={hideDeleteConfirm}
  272. onConfirm={handleDelete}
  273. isLoading={deleting}
  274. isDisabled={deleting}
  275. />
  276. )}
  277. {isShowUpdateConfirm && (
  278. <Confirm
  279. isShow
  280. title={t('tools.mcp.toolUpdateConfirmTitle')}
  281. content={t('tools.mcp.toolUpdateConfirmContent')}
  282. onCancel={hideUpdateConfirm}
  283. onConfirm={handleUpdateTools}
  284. />
  285. )}
  286. </>
  287. )
  288. }
  289. export default MCPDetailContent