index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiLoader2Line,
  6. } from '@remixicon/react'
  7. import type {
  8. CustomConfigurationModelFixedFields,
  9. ModelItem,
  10. ModelProvider,
  11. } from '../declarations'
  12. import { ConfigurationMethodEnum } from '../declarations'
  13. import {
  14. DEFAULT_BACKGROUND_COLOR,
  15. MODEL_PROVIDER_QUOTA_GET_PAID,
  16. modelTypeFormat,
  17. } from '../utils'
  18. import ProviderIcon from '../provider-icon'
  19. import ModelBadge from '../model-badge'
  20. import CredentialPanel from './credential-panel'
  21. import QuotaPanel from './quota-panel'
  22. import ModelList from './model-list'
  23. import AddModelButton from './add-model-button'
  24. import { ChevronDownDouble } from '@/app/components/base/icons/src/vender/line/arrows'
  25. import { fetchModelProviderModelList } from '@/service/common'
  26. import { useEventEmitterContextContext } from '@/context/event-emitter'
  27. import { IS_CE_EDITION } from '@/config'
  28. export const UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST = 'UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST'
  29. type ProviderAddedCardProps = {
  30. provider: ModelProvider
  31. onOpenModal: (configurationMethod: ConfigurationMethodEnum, currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields) => void
  32. }
  33. const ProviderAddedCard: FC<ProviderAddedCardProps> = ({
  34. provider,
  35. onOpenModal,
  36. }) => {
  37. const { t } = useTranslation()
  38. const { eventEmitter } = useEventEmitterContextContext()
  39. const [fetched, setFetched] = useState(false)
  40. const [loading, setLoading] = useState(false)
  41. const [collapsed, setCollapsed] = useState(true)
  42. const [modelList, setModelList] = useState<ModelItem[]>([])
  43. const configurationMethods = provider.configurate_methods.filter(method => method !== ConfigurationMethodEnum.fetchFromRemote)
  44. const systemConfig = provider.system_configuration
  45. const hasModelList = fetched && !!modelList.length
  46. const showQuota = systemConfig.enabled && [...MODEL_PROVIDER_QUOTA_GET_PAID].includes(provider.provider) && !IS_CE_EDITION
  47. const getModelList = async (providerName: string) => {
  48. if (loading)
  49. return
  50. try {
  51. setLoading(true)
  52. const modelsData = await fetchModelProviderModelList(`/workspaces/current/model-providers/${providerName}/models`)
  53. setModelList(modelsData.data)
  54. setCollapsed(false)
  55. setFetched(true)
  56. }
  57. finally {
  58. setLoading(false)
  59. }
  60. }
  61. const handleOpenModelList = () => {
  62. if (fetched) {
  63. setCollapsed(false)
  64. return
  65. }
  66. getModelList(provider.provider)
  67. }
  68. eventEmitter?.useSubscription((v: any) => {
  69. if (v?.type === UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST && v.payload === provider.provider)
  70. getModelList(v.payload)
  71. })
  72. return (
  73. <div
  74. className='mb-2 rounded-xl border-[0.5px] border-black/5 shadow-xs'
  75. style={{ background: provider.background || DEFAULT_BACKGROUND_COLOR }}
  76. >
  77. <div className='flex pl-3 py-2 pr-2 rounded-t-xl'>
  78. <div className='grow px-1 pt-1 pb-0.5'>
  79. <ProviderIcon
  80. className='mb-2'
  81. provider={provider}
  82. />
  83. <div className='flex gap-0.5'>
  84. {
  85. provider.supported_model_types.map(modelType => (
  86. <ModelBadge key={modelType}>
  87. {modelTypeFormat(modelType)}
  88. </ModelBadge>
  89. ))
  90. }
  91. </div>
  92. </div>
  93. {
  94. showQuota && (
  95. <QuotaPanel
  96. provider={provider}
  97. />
  98. )
  99. }
  100. {
  101. configurationMethods.includes(ConfigurationMethodEnum.predefinedModel) && (
  102. <CredentialPanel
  103. onSetup={() => onOpenModal(ConfigurationMethodEnum.predefinedModel)}
  104. provider={provider}
  105. />
  106. )
  107. }
  108. </div>
  109. {
  110. collapsed && (
  111. <div className='group flex items-center justify-between pl-2 py-1.5 pr-[11px] border-t border-t-black/5 bg-white/30 text-xs font-medium text-gray-500'>
  112. <div className='group-hover:hidden pl-1 pr-1.5 h-6 leading-6'>
  113. {
  114. hasModelList
  115. ? t('common.modelProvider.modelsNum', { num: modelList.length })
  116. : t('common.modelProvider.showModels')
  117. }
  118. </div>
  119. <div
  120. className='hidden group-hover:flex items-center pl-1 pr-1.5 h-6 rounded-lg hover:bg-white cursor-pointer'
  121. onClick={handleOpenModelList}
  122. >
  123. <ChevronDownDouble className='mr-0.5 w-3 h-3' />
  124. {
  125. hasModelList
  126. ? t('common.modelProvider.showModelsNum', { num: modelList.length })
  127. : t('common.modelProvider.showModels')
  128. }
  129. {
  130. loading && (
  131. <RiLoader2Line className='ml-0.5 animate-spin w-3 h-3' />
  132. )
  133. }
  134. </div>
  135. {
  136. configurationMethods.includes(ConfigurationMethodEnum.customizableModel) && (
  137. <AddModelButton
  138. onClick={() => onOpenModal(ConfigurationMethodEnum.customizableModel)}
  139. className='hidden group-hover:flex group-hover:text-primary-600'
  140. />
  141. )
  142. }
  143. </div>
  144. )
  145. }
  146. {
  147. !collapsed && (
  148. <ModelList
  149. provider={provider}
  150. models={modelList}
  151. onCollapse={() => setCollapsed(true)}
  152. onConfig={currentCustomConfigurationModelFixedFields => onOpenModal(ConfigurationMethodEnum.customizableModel, currentCustomConfigurationModelFixedFields)}
  153. onChange={(provider: string) => getModelList(provider)}
  154. />
  155. )
  156. }
  157. </div>
  158. )
  159. }
  160. export default ProviderAddedCard