provider-context.tsx 6.3 KB

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