provider-context.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use client'
  2. import { createContext, useContext, useContextSelector } from 'use-context-selector'
  3. import useSWR from 'swr'
  4. import { useEffect, useState } from 'react'
  5. import dayjs from 'dayjs'
  6. import { useTranslation } from 'react-i18next'
  7. import {
  8. fetchModelList,
  9. fetchModelProviders,
  10. fetchSupportRetrievalMethods,
  11. } from '@/service/common'
  12. import {
  13. CurrentSystemQuotaTypeEnum,
  14. ModelStatusEnum,
  15. ModelTypeEnum,
  16. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import type { Model, ModelProvider } from '@/app/components/header/account-setting/model-provider-page/declarations'
  18. import type { RETRIEVE_METHOD } from '@/types/app'
  19. import { Plan, type UsagePlanInfo } from '@/app/components/billing/type'
  20. import { fetchCurrentPlanInfo } from '@/service/billing'
  21. import { parseCurrentPlan } from '@/app/components/billing/utils'
  22. import { defaultPlan } from '@/app/components/billing/config'
  23. import Toast from '@/app/components/base/toast'
  24. type ProviderContextState = {
  25. modelProviders: ModelProvider[]
  26. textGenerationModelList: Model[]
  27. supportRetrievalMethods: RETRIEVE_METHOD[]
  28. isAPIKeySet: boolean
  29. plan: {
  30. type: Plan
  31. usage: UsagePlanInfo
  32. total: UsagePlanInfo
  33. }
  34. isFetchedPlan: boolean
  35. enableBilling: boolean
  36. onPlanInfoChanged: () => void
  37. enableReplaceWebAppLogo: boolean
  38. modelLoadBalancingEnabled: boolean
  39. datasetOperatorEnabled: boolean
  40. }
  41. const ProviderContext = createContext<ProviderContextState>({
  42. modelProviders: [],
  43. textGenerationModelList: [],
  44. supportRetrievalMethods: [],
  45. isAPIKeySet: true,
  46. plan: {
  47. type: Plan.sandbox,
  48. usage: {
  49. vectorSpace: 32,
  50. buildApps: 12,
  51. teamMembers: 1,
  52. annotatedResponse: 1,
  53. documentsUploadQuota: 50,
  54. },
  55. total: {
  56. vectorSpace: 200,
  57. buildApps: 50,
  58. teamMembers: 1,
  59. annotatedResponse: 10,
  60. documentsUploadQuota: 500,
  61. },
  62. },
  63. isFetchedPlan: false,
  64. enableBilling: false,
  65. onPlanInfoChanged: () => { },
  66. enableReplaceWebAppLogo: false,
  67. modelLoadBalancingEnabled: false,
  68. datasetOperatorEnabled: false,
  69. })
  70. export const useProviderContext = () => useContext(ProviderContext)
  71. // Adding a dangling comma to avoid the generic parsing issue in tsx, see:
  72. // https://github.com/microsoft/TypeScript/issues/15713
  73. // eslint-disable-next-line @typescript-eslint/comma-dangle
  74. export const useProviderContextSelector = <T,>(selector: (state: ProviderContextState) => T): T =>
  75. useContextSelector(ProviderContext, selector)
  76. type ProviderContextProviderProps = {
  77. children: React.ReactNode
  78. }
  79. export const ProviderContextProvider = ({
  80. children,
  81. }: ProviderContextProviderProps) => {
  82. const { data: providersData } = useSWR('/workspaces/current/model-providers', fetchModelProviders)
  83. const fetchModelListUrlPrefix = '/workspaces/current/models/model-types/'
  84. const { data: textGenerationModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelTypeEnum.textGeneration}`, fetchModelList)
  85. const { data: supportRetrievalMethods } = useSWR('/datasets/retrieval-setting', fetchSupportRetrievalMethods)
  86. const [plan, setPlan] = useState(defaultPlan)
  87. const [isFetchedPlan, setIsFetchedPlan] = useState(false)
  88. const [enableBilling, setEnableBilling] = useState(true)
  89. const [enableReplaceWebAppLogo, setEnableReplaceWebAppLogo] = useState(false)
  90. const [modelLoadBalancingEnabled, setModelLoadBalancingEnabled] = useState(false)
  91. const [datasetOperatorEnabled, setDatasetOperatorEnabled] = useState(false)
  92. const fetchPlan = async () => {
  93. const data = await fetchCurrentPlanInfo()
  94. const enabled = data.billing.enabled
  95. setEnableBilling(enabled)
  96. setEnableReplaceWebAppLogo(data.can_replace_logo)
  97. if (enabled) {
  98. setPlan(parseCurrentPlan(data))
  99. setIsFetchedPlan(true)
  100. }
  101. if (data.model_load_balancing_enabled)
  102. setModelLoadBalancingEnabled(true)
  103. if (data.dataset_operator_enabled)
  104. setDatasetOperatorEnabled(true)
  105. }
  106. useEffect(() => {
  107. fetchPlan()
  108. }, [])
  109. const { t } = useTranslation()
  110. useEffect(() => {
  111. if (localStorage.getItem('anthropic_quota_notice') === 'true')
  112. return
  113. if (dayjs().isAfter(dayjs('2025-03-11')))
  114. return
  115. if (providersData?.data && providersData.data.length > 0) {
  116. const anthropic = providersData.data.find(provider => provider.provider === 'anthropic')
  117. if (anthropic && anthropic.system_configuration.current_quota_type === CurrentSystemQuotaTypeEnum.trial) {
  118. const quota = anthropic.system_configuration.quota_configurations.find(item => item.quota_type === anthropic.system_configuration.current_quota_type)
  119. if (quota && quota.is_valid && quota.quota_used < quota.quota_limit) {
  120. Toast.notify({
  121. type: 'info',
  122. message: t('common.provider.anthropicHosted.trialQuotaTip'),
  123. duration: 60000,
  124. onClose: () => {
  125. localStorage.setItem('anthropic_quota_notice', 'true')
  126. },
  127. })
  128. }
  129. }
  130. }
  131. }, [providersData, t])
  132. return (
  133. <ProviderContext.Provider value={{
  134. modelProviders: providersData?.data || [],
  135. textGenerationModelList: textGenerationModelList?.data || [],
  136. isAPIKeySet: !!textGenerationModelList?.data.some(model => model.status === ModelStatusEnum.active),
  137. supportRetrievalMethods: supportRetrievalMethods?.retrieval_method || [],
  138. plan,
  139. isFetchedPlan,
  140. enableBilling,
  141. onPlanInfoChanged: fetchPlan,
  142. enableReplaceWebAppLogo,
  143. modelLoadBalancingEnabled,
  144. datasetOperatorEnabled,
  145. }}>
  146. {children}
  147. </ProviderContext.Provider>
  148. )
  149. }
  150. export default ProviderContext