quota-panel.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import type { FC } from 'react'
  2. import { useSWRConfig } from 'swr'
  3. import { useTranslation } from 'react-i18next'
  4. import type { ModelProvider } from '../declarations'
  5. import {
  6. CustomConfigurationStatusEnum,
  7. PreferredProviderTypeEnum,
  8. QuotaUnitEnum,
  9. } from '../declarations'
  10. import {
  11. useAnthropicBuyQuota,
  12. useFreeQuota,
  13. } from '../hooks'
  14. import PriorityUseTip from './priority-use-tip'
  15. import { InfoCircle } from '@/app/components/base/icons/src/vender/line/general'
  16. import Button from '@/app/components/base/button'
  17. import TooltipPlus from '@/app/components/base/tooltip-plus'
  18. type QuotaPanelProps = {
  19. provider: ModelProvider
  20. }
  21. const QuotaPanel: FC<QuotaPanelProps> = ({
  22. provider,
  23. }) => {
  24. const { t } = useTranslation()
  25. const { mutate } = useSWRConfig()
  26. const handlePay = useAnthropicBuyQuota()
  27. const handleFreeQuotaSuccess = () => {
  28. mutate('/workspaces/current/model-providers')
  29. }
  30. const handleFreeQuota = useFreeQuota(handleFreeQuotaSuccess)
  31. const customConfig = provider.custom_configuration
  32. const priorityUseType = provider.preferred_provider_type
  33. const systemConfig = provider.system_configuration
  34. const currentQuota = systemConfig.enabled && systemConfig.quota_configurations.find(item => item.quota_type === systemConfig.current_quota_type)
  35. const openaiOrAnthropic = ['openai', 'anthropic'].includes(provider.provider)
  36. return (
  37. <div className='group relative shrink-0 min-w-[112px] px-3 py-2 rounded-lg bg-white/[0.3] border-[0.5px] border-black/5'>
  38. <div className='flex items-center mb-2 h-4 text-xs font-medium text-gray-500'>
  39. {t('common.modelProvider.quota')}
  40. <TooltipPlus popupContent={
  41. openaiOrAnthropic
  42. ? t('common.modelProvider.card.tip')
  43. : t('common.modelProvider.quotaTip')
  44. }>
  45. <InfoCircle className='ml-0.5 w-3 h-3 text-gray-400' />
  46. </TooltipPlus>
  47. </div>
  48. {
  49. currentQuota && (
  50. <div className='flex items-center h-4 text-xs text-gray-500'>
  51. <span className='mr-0.5 text-sm font-semibold text-gray-700'>{(currentQuota?.quota_limit || 0) - (currentQuota?.quota_used || 0)}</span>
  52. {
  53. currentQuota?.quota_unit === QuotaUnitEnum.tokens && 'Tokens'
  54. }
  55. {
  56. currentQuota?.quota_unit === QuotaUnitEnum.times && t('common.modelProvider.callTimes')
  57. }
  58. </div>
  59. )
  60. }
  61. {
  62. !currentQuota && provider.provider === 'anthropic' && (
  63. <Button
  64. className='h-6 bg-white text-xs font-medium rounded-md'
  65. onClick={handlePay}
  66. >
  67. {t('common.modelProvider.buyQuota')}
  68. </Button>
  69. )
  70. }
  71. {
  72. !currentQuota && ['minimax', 'spark', 'zhipuai'].includes(provider.provider) && (
  73. <Button
  74. className='h-6 bg-white text-xs font-medium rounded-md'
  75. onClick={() => handleFreeQuota(provider.provider)}
  76. >
  77. {t('common.modelProvider.getFreeTokens')}
  78. </Button>
  79. )
  80. }
  81. {
  82. provider.provider === 'anthropic' && systemConfig.enabled && (
  83. <div
  84. className={`
  85. absolute left-0 bottom-0 hidden group-hover:flex items-center justify-center
  86. w-full h-[30px] backdrop-blur-[2px] bg-gradient-to-r from-[rgba(238,244,255,0.80)] to-[rgba(237,237,240,0.70)]
  87. text-xs font-medium text-primary-600 cursor-pointer rounded-b-lg
  88. `}
  89. onClick={handlePay}
  90. >
  91. {t('common.modelProvider.buyQuota')}
  92. </div>
  93. )
  94. }
  95. {
  96. priorityUseType === PreferredProviderTypeEnum.system && customConfig.status === CustomConfigurationStatusEnum.active && (
  97. <PriorityUseTip />
  98. )
  99. }
  100. </div>
  101. )
  102. }
  103. export default QuotaPanel