model-list.tsx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import type { FC } from 'react'
  2. import { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiArrowRightSLine,
  6. } from '@remixicon/react'
  7. import type {
  8. Credential,
  9. ModelItem,
  10. ModelProvider,
  11. } from '../declarations'
  12. import {
  13. ConfigurationMethodEnum,
  14. } from '../declarations'
  15. // import Tab from './tab'
  16. import ModelListItem from './model-list-item'
  17. import { useModalContextSelector } from '@/context/modal-context'
  18. import { useAppContext } from '@/context/app-context'
  19. import { AddCustomModel } from '@/app/components/header/account-setting/model-provider-page/model-auth'
  20. type ModelListProps = {
  21. provider: ModelProvider
  22. models: ModelItem[]
  23. onCollapse: () => void
  24. onChange?: (provider: string) => void
  25. }
  26. const ModelList: FC<ModelListProps> = ({
  27. provider,
  28. models,
  29. onCollapse,
  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, credential?: Credential) => {
  38. setShowModelLoadBalancingModal({
  39. provider,
  40. credential,
  41. configurateMethod: model.fetch_from,
  42. model: model!,
  43. open: !!model,
  44. onClose: () => setShowModelLoadBalancingModal(null),
  45. onSave: onChange,
  46. })
  47. }, [onChange, provider, setShowModelLoadBalancingModal])
  48. return (
  49. <div className='rounded-b-xl px-2 pb-2'>
  50. <div className='rounded-lg bg-components-panel-bg py-1'>
  51. <div className='flex items-center pl-1 pr-[3px]'>
  52. <span className='group mr-2 flex shrink-0 items-center'>
  53. <span className='system-xs-medium inline-flex h-6 items-center pl-1 pr-1.5 text-text-tertiary group-hover:hidden'>
  54. {t('common.modelProvider.modelsNum', { num: models.length })}
  55. <RiArrowRightSLine className='mr-0.5 h-4 w-4 rotate-90' />
  56. </span>
  57. <span
  58. className='system-xs-medium hidden h-6 cursor-pointer items-center rounded-lg bg-state-base-hover pl-1 pr-1.5 text-text-tertiary group-hover:inline-flex'
  59. onClick={() => onCollapse()}
  60. >
  61. {t('common.modelProvider.modelsNum', { num: models.length })}
  62. <RiArrowRightSLine className='mr-0.5 h-4 w-4 rotate-90' />
  63. </span>
  64. </span>
  65. {
  66. isConfigurable && isCurrentWorkspaceManager && (
  67. <div className='flex grow justify-end'>
  68. <AddCustomModel
  69. provider={provider}
  70. configurationMethod={ConfigurationMethodEnum.customizableModel}
  71. currentCustomConfigurationModelFixedFields={undefined}
  72. />
  73. </div>
  74. )
  75. }
  76. </div>
  77. {
  78. models.map(model => (
  79. <ModelListItem
  80. key={`${model.model}-${model.fetch_from}`}
  81. {...{
  82. model,
  83. provider,
  84. isConfigurable,
  85. onModifyLoadBalancing,
  86. }}
  87. />
  88. ))
  89. }
  90. </div>
  91. </div>
  92. )
  93. }
  94. export default ModelList