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. import { formatNumber } from '@/utils/format'
  19. type QuotaPanelProps = {
  20. provider: ModelProvider
  21. }
  22. const QuotaPanel: FC<QuotaPanelProps> = ({
  23. provider,
  24. }) => {
  25. const { t } = useTranslation()
  26. const { mutate } = useSWRConfig()
  27. const handlePay = useAnthropicBuyQuota()
  28. const handleFreeQuotaSuccess = () => {
  29. mutate('/workspaces/current/model-providers')
  30. }
  31. const handleFreeQuota = useFreeQuota(handleFreeQuotaSuccess)
  32. const customConfig = provider.custom_configuration
  33. const priorityUseType = provider.preferred_provider_type
  34. const systemConfig = provider.system_configuration
  35. const currentQuota = systemConfig.enabled && systemConfig.quota_configurations.find(item => item.quota_type === systemConfig.current_quota_type)
  36. const openaiOrAnthropic = ['openai', 'anthropic'].includes(provider.provider)
  37. return (
  38. <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'>
  39. <div className='flex items-center mb-2 h-4 text-xs font-medium text-gray-500'>
  40. {t('common.modelProvider.quota')}
  41. <TooltipPlus popupContent={
  42. openaiOrAnthropic
  43. ? t('common.modelProvider.card.tip')
  44. : t('common.modelProvider.quotaTip')
  45. }>
  46. <InfoCircle className='ml-0.5 w-3 h-3 text-gray-400' />
  47. </TooltipPlus>
  48. </div>
  49. {
  50. currentQuota && (
  51. <div className='flex items-center h-4 text-xs text-gray-500'>
  52. <span className='mr-0.5 text-sm font-semibold text-gray-700'>{formatNumber((currentQuota?.quota_limit || 0) - (currentQuota?.quota_used || 0))}</span>
  53. {
  54. currentQuota?.quota_unit === QuotaUnitEnum.tokens && 'Tokens'
  55. }
  56. {
  57. currentQuota?.quota_unit === QuotaUnitEnum.times && t('common.modelProvider.callTimes')
  58. }
  59. </div>
  60. )
  61. }
  62. {
  63. !currentQuota && provider.provider === 'anthropic' && (
  64. <Button
  65. className='h-6 bg-white text-xs font-medium rounded-md'
  66. onClick={handlePay}
  67. >
  68. {t('common.modelProvider.buyQuota')}
  69. </Button>
  70. )
  71. }
  72. {
  73. !currentQuota && ['minimax', 'spark', 'zhipuai'].includes(provider.provider) && (
  74. <Button
  75. className='h-6 bg-white text-xs font-medium rounded-md'
  76. onClick={() => handleFreeQuota(provider.provider)}
  77. >
  78. {t('common.modelProvider.getFreeTokens')}
  79. </Button>
  80. )
  81. }
  82. {
  83. provider.provider === 'anthropic' && systemConfig.enabled && (
  84. <div
  85. className={`
  86. absolute left-0 bottom-0 hidden group-hover:flex items-center justify-center
  87. 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)]
  88. text-xs font-medium text-primary-600 cursor-pointer rounded-b-lg
  89. `}
  90. onClick={handlePay}
  91. >
  92. {t('common.modelProvider.buyQuota')}
  93. </div>
  94. )
  95. }
  96. {
  97. priorityUseType === PreferredProviderTypeEnum.system && customConfig.status === CustomConfigurationStatusEnum.active && (
  98. <PriorityUseTip />
  99. )
  100. }
  101. </div>
  102. )
  103. }
  104. export default QuotaPanel