detail.tsx 14 KB

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