You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

add-custom-model.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. memo,
  3. useCallback,
  4. useMemo,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. RiAddCircleFill,
  9. } from '@remixicon/react'
  10. import {
  11. Button,
  12. } from '@/app/components/base/button'
  13. import type {
  14. CustomConfigurationModelFixedFields,
  15. ModelProvider,
  16. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import { ConfigurationMethodEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  18. import Authorized from './authorized'
  19. import {
  20. useAuth,
  21. useCustomModels,
  22. } from './hooks'
  23. import cn from '@/utils/classnames'
  24. import Tooltip from '@/app/components/base/tooltip'
  25. type AddCustomModelProps = {
  26. provider: ModelProvider,
  27. configurationMethod: ConfigurationMethodEnum,
  28. currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
  29. }
  30. const AddCustomModel = ({
  31. provider,
  32. configurationMethod,
  33. currentCustomConfigurationModelFixedFields,
  34. }: AddCustomModelProps) => {
  35. const { t } = useTranslation()
  36. const customModels = useCustomModels(provider)
  37. const noModels = !customModels.length
  38. const {
  39. handleOpenModal,
  40. } = useAuth(provider, configurationMethod, currentCustomConfigurationModelFixedFields, true)
  41. const notAllowCustomCredential = provider.allow_custom_token === false
  42. const handleClick = useCallback(() => {
  43. if (notAllowCustomCredential)
  44. return
  45. handleOpenModal()
  46. }, [handleOpenModal, notAllowCustomCredential])
  47. const ButtonComponent = useMemo(() => {
  48. const Item = (
  49. <Button
  50. variant='ghost-accent'
  51. size='small'
  52. onClick={handleClick}
  53. className={cn(
  54. notAllowCustomCredential && 'cursor-not-allowed opacity-50',
  55. )}
  56. >
  57. <RiAddCircleFill className='mr-1 h-3.5 w-3.5' />
  58. {t('common.modelProvider.addModel')}
  59. </Button>
  60. )
  61. if (notAllowCustomCredential) {
  62. return (
  63. <Tooltip
  64. asChild
  65. popupContent={t('plugin.auth.credentialUnavailable')}
  66. >
  67. {Item}
  68. </Tooltip>
  69. )
  70. }
  71. return Item
  72. }, [handleClick, notAllowCustomCredential, t])
  73. const renderTrigger = useCallback((open?: boolean) => {
  74. const Item = (
  75. <Button
  76. variant='ghost'
  77. size='small'
  78. className={cn(
  79. open && 'bg-components-button-ghost-bg-hover',
  80. )}
  81. >
  82. <RiAddCircleFill className='mr-1 h-3.5 w-3.5' />
  83. {t('common.modelProvider.addModel')}
  84. </Button>
  85. )
  86. return Item
  87. }, [t])
  88. if (noModels)
  89. return ButtonComponent
  90. return (
  91. <Authorized
  92. provider={provider}
  93. configurationMethod={ConfigurationMethodEnum.customizableModel}
  94. items={customModels.map(model => ({
  95. model,
  96. credentials: model.available_model_credentials ?? [],
  97. }))}
  98. renderTrigger={renderTrigger}
  99. isModelCredential
  100. enableAddModelCredential
  101. bottomAddModelCredentialText={t('common.modelProvider.auth.addNewModel')}
  102. />
  103. )
  104. }
  105. export default memo(AddCustomModel)