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.

detail.tsx 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. 'use client'
  2. import React, { useCallback, useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import {
  6. RiCloseLine,
  7. } from '@remixicon/react'
  8. import { AuthHeaderPrefix, AuthType, CollectionType } from '../types'
  9. import { basePath } from '@/utils/var'
  10. import type { Collection, CustomCollectionBackend, Tool, WorkflowToolProviderRequest, WorkflowToolProviderResponse } from '../types'
  11. import ToolItem from './tool-item'
  12. import cn from '@/utils/classnames'
  13. import I18n from '@/context/i18n'
  14. import { getLanguage } from '@/i18n/language'
  15. import Confirm from '@/app/components/base/confirm'
  16. import Button from '@/app/components/base/button'
  17. import Indicator from '@/app/components/header/indicator'
  18. import { LinkExternal02, Settings01 } from '@/app/components/base/icons/src/vender/line/general'
  19. import Icon from '@/app/components/plugins/card/base/card-icon'
  20. import Title from '@/app/components/plugins/card/base/title'
  21. import OrgInfo from '@/app/components/plugins/card/base/org-info'
  22. import Description from '@/app/components/plugins/card/base/description'
  23. import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials'
  24. import EditCustomToolModal from '@/app/components/tools/edit-custom-collection-modal'
  25. import WorkflowToolModal from '@/app/components/tools/workflow-tool'
  26. import Toast from '@/app/components/base/toast'
  27. import Drawer from '@/app/components/base/drawer'
  28. import ActionButton from '@/app/components/base/action-button'
  29. import {
  30. deleteWorkflowTool,
  31. fetchBuiltInToolList,
  32. fetchCustomCollection,
  33. fetchCustomToolList,
  34. fetchModelToolList,
  35. fetchWorkflowToolDetail,
  36. removeBuiltInToolCredential,
  37. removeCustomCollection,
  38. saveWorkflowToolProvider,
  39. updateBuiltInToolCredential,
  40. updateCustomCollection,
  41. } from '@/service/tools'
  42. import { useModalContext } from '@/context/modal-context'
  43. import { useProviderContext } from '@/context/provider-context'
  44. import { ConfigurationMethodEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  45. import Loading from '@/app/components/base/loading'
  46. import { useAppContext } from '@/context/app-context'
  47. import { useInvalidateAllWorkflowTools } from '@/service/use-tools'
  48. type Props = {
  49. collection: Collection
  50. onHide: () => void
  51. onRefreshData: () => void
  52. }
  53. const ProviderDetail = ({
  54. collection,
  55. onHide,
  56. onRefreshData,
  57. }: Props) => {
  58. const { t } = useTranslation()
  59. const { locale } = useContext(I18n)
  60. const language = getLanguage(locale)
  61. const needAuth = collection.allow_delete || collection.type === CollectionType.model
  62. const isAuthed = collection.is_team_authorization
  63. const isBuiltIn = collection.type === CollectionType.builtIn
  64. const isModel = collection.type === CollectionType.model
  65. const { isCurrentWorkspaceManager } = useAppContext()
  66. const invalidateAllWorkflowTools = useInvalidateAllWorkflowTools()
  67. const [isDetailLoading, setIsDetailLoading] = useState(false)
  68. // built in provider
  69. const [showSettingAuth, setShowSettingAuth] = useState(false)
  70. const { setShowModelModal } = useModalContext()
  71. const { modelProviders: providers } = useProviderContext()
  72. const showSettingAuthModal = () => {
  73. if (isModel) {
  74. const provider = providers.find(item => item.provider === collection?.id)
  75. if (provider) {
  76. setShowModelModal({
  77. payload: {
  78. currentProvider: provider,
  79. currentConfigurationMethod: ConfigurationMethodEnum.predefinedModel,
  80. currentCustomConfigurationModelFixedFields: undefined,
  81. },
  82. onSaveCallback: () => {
  83. onRefreshData()
  84. },
  85. })
  86. }
  87. }
  88. else {
  89. setShowSettingAuth(true)
  90. }
  91. }
  92. // custom provider
  93. const [customCollection, setCustomCollection] = useState<CustomCollectionBackend | WorkflowToolProviderResponse | null>(null)
  94. const [isShowEditCollectionToolModal, setIsShowEditCustomCollectionModal] = useState(false)
  95. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  96. const [deleteAction, setDeleteAction] = useState('')
  97. const doUpdateCustomToolCollection = async (data: CustomCollectionBackend) => {
  98. await updateCustomCollection(data)
  99. onRefreshData()
  100. Toast.notify({
  101. type: 'success',
  102. message: t('common.api.actionSuccess'),
  103. })
  104. setIsShowEditCustomCollectionModal(false)
  105. }
  106. const doRemoveCustomToolCollection = async () => {
  107. await removeCustomCollection(collection?.name as string)
  108. onRefreshData()
  109. Toast.notify({
  110. type: 'success',
  111. message: t('common.api.actionSuccess'),
  112. })
  113. setIsShowEditCustomCollectionModal(false)
  114. }
  115. const getCustomProvider = useCallback(async () => {
  116. setIsDetailLoading(true)
  117. const res = await fetchCustomCollection(collection.name)
  118. if (res.credentials.auth_type === AuthType.apiKey && !res.credentials.api_key_header_prefix) {
  119. if (res.credentials.api_key_value)
  120. res.credentials.api_key_header_prefix = AuthHeaderPrefix.custom
  121. }
  122. setCustomCollection({
  123. ...res,
  124. labels: collection.labels,
  125. provider: collection.name,
  126. })
  127. setIsDetailLoading(false)
  128. }, [collection.labels, collection.name])
  129. // workflow provider
  130. const [isShowEditWorkflowToolModal, setIsShowEditWorkflowToolModal] = useState(false)
  131. const getWorkflowToolProvider = useCallback(async () => {
  132. setIsDetailLoading(true)
  133. const res = await fetchWorkflowToolDetail(collection.id)
  134. const payload = {
  135. ...res,
  136. parameters: res.tool?.parameters.map((item) => {
  137. return {
  138. name: item.name,
  139. description: item.llm_description,
  140. form: item.form,
  141. required: item.required,
  142. type: item.type,
  143. }
  144. }) || [],
  145. labels: res.tool?.labels || [],
  146. }
  147. setCustomCollection(payload)
  148. setIsDetailLoading(false)
  149. }, [collection.id])
  150. const removeWorkflowToolProvider = async () => {
  151. await deleteWorkflowTool(collection.id)
  152. onRefreshData()
  153. Toast.notify({
  154. type: 'success',
  155. message: t('common.api.actionSuccess'),
  156. })
  157. setIsShowEditWorkflowToolModal(false)
  158. }
  159. const updateWorkflowToolProvider = async (data: WorkflowToolProviderRequest & Partial<{
  160. workflow_app_id: string
  161. workflow_tool_id: string
  162. }>) => {
  163. await saveWorkflowToolProvider(data)
  164. invalidateAllWorkflowTools()
  165. onRefreshData()
  166. getWorkflowToolProvider()
  167. Toast.notify({
  168. type: 'success',
  169. message: t('common.api.actionSuccess'),
  170. })
  171. setIsShowEditWorkflowToolModal(false)
  172. }
  173. const onClickCustomToolDelete = () => {
  174. setDeleteAction('customTool')
  175. setShowConfirmDelete(true)
  176. }
  177. const onClickWorkflowToolDelete = () => {
  178. setDeleteAction('workflowTool')
  179. setShowConfirmDelete(true)
  180. }
  181. const handleConfirmDelete = () => {
  182. if (deleteAction === 'customTool')
  183. doRemoveCustomToolCollection()
  184. else if (deleteAction === 'workflowTool')
  185. removeWorkflowToolProvider()
  186. setShowConfirmDelete(false)
  187. }
  188. // ToolList
  189. const [toolList, setToolList] = useState<Tool[]>([])
  190. const getProviderToolList = useCallback(async () => {
  191. setIsDetailLoading(true)
  192. try {
  193. if (collection.type === CollectionType.builtIn) {
  194. const list = await fetchBuiltInToolList(collection.name)
  195. setToolList(list)
  196. }
  197. else if (collection.type === CollectionType.model) {
  198. const list = await fetchModelToolList(collection.name)
  199. setToolList(list)
  200. }
  201. else if (collection.type === CollectionType.workflow) {
  202. setToolList([])
  203. }
  204. else {
  205. const list = await fetchCustomToolList(collection.name)
  206. setToolList(list)
  207. }
  208. }
  209. catch { }
  210. setIsDetailLoading(false)
  211. }, [collection.name, collection.type])
  212. useEffect(() => {
  213. if (collection.type === CollectionType.custom)
  214. getCustomProvider()
  215. if (collection.type === CollectionType.workflow)
  216. getWorkflowToolProvider()
  217. getProviderToolList()
  218. }, [collection.name, collection.type, getCustomProvider, getProviderToolList, getWorkflowToolProvider])
  219. return (
  220. <Drawer
  221. isOpen={!!collection}
  222. clickOutsideNotOpen={false}
  223. onClose={onHide}
  224. footer={null}
  225. mask={false}
  226. positionCenter={false}
  227. panelClassName={cn('mb-2 mr-2 mt-[64px] !w-[420px] !max-w-[420px] justify-start rounded-2xl border-[0.5px] border-components-panel-border !bg-components-panel-bg !p-0 shadow-xl')}
  228. >
  229. <div className='flex h-full flex-col p-4'>
  230. <div className="shrink-0">
  231. <div className='mb-3 flex'>
  232. <Icon src={collection.icon} />
  233. <div className="ml-3 w-0 grow">
  234. <div className="flex h-5 items-center">
  235. <Title title={collection.label[language]} />
  236. </div>
  237. <div className='mb-1 flex h-4 items-center justify-between'>
  238. <OrgInfo
  239. className="mt-0.5"
  240. packageNameClassName='w-auto'
  241. orgName={collection.author}
  242. packageName={collection.name}
  243. />
  244. </div>
  245. </div>
  246. <div className='flex gap-1'>
  247. <ActionButton onClick={onHide}>
  248. <RiCloseLine className='h-4 w-4' />
  249. </ActionButton>
  250. </div>
  251. </div>
  252. </div>
  253. {!!collection.description[language] && (
  254. <Description text={collection.description[language]} descriptionLineRows={2}></Description>
  255. )}
  256. <div className='flex gap-1 border-b-[0.5px] border-divider-subtle'>
  257. {collection.type === CollectionType.custom && !isDetailLoading && (
  258. <Button
  259. className={cn('my-3 w-full shrink-0')}
  260. onClick={() => setIsShowEditCustomCollectionModal(true)}
  261. >
  262. <Settings01 className='mr-1 h-4 w-4 text-text-tertiary' />
  263. <div className='system-sm-medium text-text-secondary'>{t('tools.createTool.editAction')}</div>
  264. </Button>
  265. )}
  266. {collection.type === CollectionType.workflow && !isDetailLoading && customCollection && (
  267. <>
  268. <Button
  269. variant='primary'
  270. className={cn('my-3 w-[183px] shrink-0')}
  271. >
  272. <a className='flex items-center' href={`${basePath}/app/${(customCollection as WorkflowToolProviderResponse).workflow_app_id}/workflow`} rel='noreferrer' target='_blank'>
  273. <div className='system-sm-medium'>{t('tools.openInStudio')}</div>
  274. <LinkExternal02 className='ml-1 h-4 w-4' />
  275. </a>
  276. </Button>
  277. <Button
  278. className={cn('my-3 w-[183px] shrink-0')}
  279. onClick={() => setIsShowEditWorkflowToolModal(true)}
  280. disabled={!isCurrentWorkspaceManager}
  281. >
  282. <div className='system-sm-medium text-text-secondary'>{t('tools.createTool.editAction')}</div>
  283. </Button>
  284. </>
  285. )}
  286. </div>
  287. <div className='flex min-h-0 flex-1 flex-col pt-3'>
  288. {isDetailLoading && <div className='flex h-[200px]'><Loading type='app' /></div>}
  289. {!isDetailLoading && (
  290. <>
  291. <div className="shrink-0">
  292. {(collection.type === CollectionType.builtIn || collection.type === CollectionType.model) && isAuthed && (
  293. <div className='system-sm-semibold-uppercase mb-1 flex h-6 items-center justify-between text-text-secondary'>
  294. {t('plugin.detailPanel.actionNum', { num: toolList.length, action: toolList.length > 1 ? 'actions' : 'action' })}
  295. {needAuth && (
  296. <Button
  297. variant='secondary'
  298. size='small'
  299. onClick={() => {
  300. if (collection.type === CollectionType.builtIn || collection.type === CollectionType.model)
  301. showSettingAuthModal()
  302. }}
  303. disabled={!isCurrentWorkspaceManager}
  304. >
  305. <Indicator className='mr-2' color={'green'} />
  306. {t('tools.auth.authorized')}
  307. </Button>
  308. )}
  309. </div>
  310. )}
  311. {(collection.type === CollectionType.builtIn || collection.type === CollectionType.model) && needAuth && !isAuthed && (
  312. <>
  313. <div className='system-sm-semibold-uppercase text-text-secondary'>
  314. <span className=''>{t('tools.includeToolNum', { num: toolList.length, action: toolList.length > 1 ? 'actions' : 'action' }).toLocaleUpperCase()}</span>
  315. <span className='px-1'>·</span>
  316. <span className='text-util-colors-orange-orange-600'>{t('tools.auth.setup').toLocaleUpperCase()}</span>
  317. </div>
  318. <Button
  319. variant='primary'
  320. className={cn('my-3 w-full shrink-0')}
  321. onClick={() => {
  322. if (collection.type === CollectionType.builtIn || collection.type === CollectionType.model)
  323. showSettingAuthModal()
  324. }}
  325. disabled={!isCurrentWorkspaceManager}
  326. >
  327. {t('tools.auth.unauthorized')}
  328. </Button>
  329. </>
  330. )}
  331. {(collection.type === CollectionType.custom) && (
  332. <div className='system-sm-semibold-uppercase text-text-secondary'>
  333. <span className=''>{t('tools.includeToolNum', { num: toolList.length, action: toolList.length > 1 ? 'actions' : 'action' }).toLocaleUpperCase()}</span>
  334. </div>
  335. )}
  336. {(collection.type === CollectionType.workflow) && (
  337. <div className='system-sm-semibold-uppercase text-text-secondary'>
  338. <span className=''>{t('tools.createTool.toolInput.title').toLocaleUpperCase()}</span>
  339. </div>
  340. )}
  341. </div>
  342. <div className='mt-1 flex-1 overflow-y-auto py-2'>
  343. {collection.type !== CollectionType.workflow && toolList.map(tool => (
  344. <ToolItem
  345. key={tool.name}
  346. disabled={false}
  347. collection={collection}
  348. tool={tool}
  349. isBuiltIn={isBuiltIn}
  350. isModel={isModel}
  351. />
  352. ))}
  353. {collection.type === CollectionType.workflow && (customCollection as WorkflowToolProviderResponse)?.tool?.parameters.map(item => (
  354. <div key={item.name} className='mb-1 py-1'>
  355. <div className='mb-1 flex items-center gap-2'>
  356. <span className='code-sm-semibold text-text-secondary'>{item.name}</span>
  357. <span className='system-xs-regular text-text-tertiary'>{item.type}</span>
  358. <span className='system-xs-medium text-text-warning-secondary'>{item.required ? t('tools.createTool.toolInput.required') : ''}</span>
  359. </div>
  360. <div className='system-xs-regular text-text-tertiary'>{item.llm_description}</div>
  361. </div>
  362. ))}
  363. </div>
  364. </>
  365. )}
  366. </div>
  367. {showSettingAuth && (
  368. <ConfigCredential
  369. collection={collection}
  370. onCancel={() => setShowSettingAuth(false)}
  371. onSaved={async (value) => {
  372. await updateBuiltInToolCredential(collection.name, value)
  373. Toast.notify({
  374. type: 'success',
  375. message: t('common.api.actionSuccess'),
  376. })
  377. await onRefreshData()
  378. setShowSettingAuth(false)
  379. }}
  380. onRemove={async () => {
  381. await removeBuiltInToolCredential(collection.name)
  382. Toast.notify({
  383. type: 'success',
  384. message: t('common.api.actionSuccess'),
  385. })
  386. await onRefreshData()
  387. setShowSettingAuth(false)
  388. }}
  389. />
  390. )}
  391. {isShowEditCollectionToolModal && (
  392. <EditCustomToolModal
  393. payload={customCollection}
  394. onHide={() => setIsShowEditCustomCollectionModal(false)}
  395. onEdit={doUpdateCustomToolCollection}
  396. onRemove={onClickCustomToolDelete}
  397. />
  398. )}
  399. {isShowEditWorkflowToolModal && (
  400. <WorkflowToolModal
  401. payload={customCollection}
  402. onHide={() => setIsShowEditWorkflowToolModal(false)}
  403. onRemove={onClickWorkflowToolDelete}
  404. onSave={updateWorkflowToolProvider}
  405. />
  406. )}
  407. {showConfirmDelete && (
  408. <Confirm
  409. title={t('tools.createTool.deleteToolConfirmTitle')}
  410. content={t('tools.createTool.deleteToolConfirmContent')}
  411. isShow={showConfirmDelete}
  412. onConfirm={handleConfirmDelete}
  413. onCancel={() => setShowConfirmDelete(false)}
  414. />
  415. )}
  416. </div>
  417. </Drawer>
  418. )
  419. }
  420. export default ProviderDetail