detail.tsx 14 KB

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