detail.tsx 17 KB

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