detail.tsx 13 KB

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