model-list.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import type { FC } from 'react'
  2. import { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import type {
  5. CustomConfigurationModelFixedFields,
  6. ModelItem,
  7. ModelProvider,
  8. } from '../declarations'
  9. import {
  10. ConfigurationMethodEnum,
  11. } from '../declarations'
  12. // import Tab from './tab'
  13. import AddModelButton from './add-model-button'
  14. import ModelListItem from './model-list-item'
  15. import { ChevronDownDouble } from '@/app/components/base/icons/src/vender/line/arrows'
  16. import { useModalContextSelector } from '@/context/modal-context'
  17. import { useAppContext } from '@/context/app-context'
  18. type ModelListProps = {
  19. provider: ModelProvider
  20. models: ModelItem[]
  21. onCollapse: () => void
  22. onConfig: (currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields) => void
  23. onChange?: (provider: string) => void
  24. }
  25. const ModelList: FC<ModelListProps> = ({
  26. provider,
  27. models,
  28. onCollapse,
  29. onConfig,
  30. onChange,
  31. }) => {
  32. const { t } = useTranslation()
  33. const configurativeMethods = provider.configurate_methods.filter(method => method !== ConfigurationMethodEnum.fetchFromRemote)
  34. const { isCurrentWorkspaceManager } = useAppContext()
  35. const isConfigurable = configurativeMethods.includes(ConfigurationMethodEnum.customizableModel)
  36. const setShowModelLoadBalancingModal = useModalContextSelector(state => state.setShowModelLoadBalancingModal)
  37. const onModifyLoadBalancing = useCallback((model: ModelItem) => {
  38. setShowModelLoadBalancingModal({
  39. provider,
  40. model: model!,
  41. open: !!model,
  42. onClose: () => setShowModelLoadBalancingModal(null),
  43. onSave: onChange,
  44. })
  45. }, [onChange, provider, setShowModelLoadBalancingModal])
  46. return (
  47. <div className='px-2 pb-2 rounded-b-xl'>
  48. <div className='py-1 bg-white rounded-lg'>
  49. <div className='flex items-center pl-1 pr-[3px]'>
  50. <span className='group shrink-0 flex items-center mr-2'>
  51. <span className='group-hover:hidden pl-1 pr-1.5 h-6 leading-6 text-xs font-medium text-gray-500'>
  52. {t('common.modelProvider.modelsNum', { num: models.length })}
  53. </span>
  54. <span
  55. className='hidden group-hover:inline-flex items-center pl-1 pr-1.5 h-6 text-xs font-medium text-gray-500 bg-gray-50 cursor-pointer rounded-lg'
  56. onClick={() => onCollapse()}
  57. >
  58. <ChevronDownDouble className='mr-0.5 w-3 h-3 rotate-180' />
  59. {t('common.modelProvider.collapse')}
  60. </span>
  61. </span>
  62. {/* {
  63. isConfigurable && canSystemConfig && (
  64. <span className='flex items-center'>
  65. <Tab active='all' onSelect={() => {}} />
  66. </span>
  67. )
  68. } */}
  69. {
  70. isConfigurable && isCurrentWorkspaceManager && (
  71. <div className='grow flex justify-end'>
  72. <AddModelButton onClick={() => onConfig()} />
  73. </div>
  74. )
  75. }
  76. </div>
  77. {
  78. models.map(model => (
  79. <ModelListItem
  80. key={model.model}
  81. {...{
  82. model,
  83. provider,
  84. isConfigurable,
  85. onConfig,
  86. onModifyLoadBalancing,
  87. }}
  88. />
  89. ))
  90. }
  91. </div>
  92. </div>
  93. )
  94. }
  95. export default ModelList