provider-context.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use client'
  2. import { createContext, useContext } from 'use-context-selector'
  3. import useSWR from 'swr'
  4. import { useEffect, useMemo, useState } from 'react'
  5. import {
  6. fetchModelList,
  7. fetchModelProviders,
  8. fetchSupportRetrievalMethods,
  9. operationUtm,
  10. } from '@/service/common'
  11. import {
  12. ModelFeatureEnum,
  13. ModelStatusEnum,
  14. ModelTypeEnum,
  15. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  16. import type { Model, ModelProvider } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import type { RETRIEVE_METHOD } from '@/types/app'
  18. import { Plan, type UsagePlanInfo } from '@/app/components/billing/type'
  19. import { fetchCurrentPlanInfo } from '@/service/billing'
  20. import { parseCurrentPlan } from '@/app/components/billing/utils'
  21. import { defaultPlan } from '@/app/components/billing/config'
  22. const ProviderContext = createContext<{
  23. modelProviders: ModelProvider[]
  24. textGenerationModelList: Model[]
  25. agentThoughtModelList: Model[]
  26. supportRetrievalMethods: RETRIEVE_METHOD[]
  27. hasSettedApiKey: boolean
  28. plan: {
  29. type: Plan
  30. usage: UsagePlanInfo
  31. total: UsagePlanInfo
  32. }
  33. isFetchedPlan: boolean
  34. enableBilling: boolean
  35. enableReplaceWebAppLogo: boolean
  36. }>({
  37. modelProviders: [],
  38. textGenerationModelList: [],
  39. agentThoughtModelList: [],
  40. supportRetrievalMethods: [],
  41. hasSettedApiKey: true,
  42. plan: {
  43. type: Plan.sandbox,
  44. usage: {
  45. vectorSpace: 32,
  46. buildApps: 12,
  47. teamMembers: 1,
  48. annotatedResponse: 1,
  49. },
  50. total: {
  51. vectorSpace: 200,
  52. buildApps: 50,
  53. teamMembers: 1,
  54. annotatedResponse: 10,
  55. },
  56. },
  57. isFetchedPlan: false,
  58. enableBilling: false,
  59. enableReplaceWebAppLogo: false,
  60. })
  61. export const useProviderContext = () => useContext(ProviderContext)
  62. type ProviderContextProviderProps = {
  63. children: React.ReactNode
  64. }
  65. export const ProviderContextProvider = ({
  66. children,
  67. }: ProviderContextProviderProps) => {
  68. const { data: providersData } = useSWR('/workspaces/current/model-providers', fetchModelProviders)
  69. const fetchModelListUrlPrefix = '/workspaces/current/models/model-types/'
  70. const { data: textGenerationModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelTypeEnum.textGeneration}`, fetchModelList)
  71. const { data: supportRetrievalMethods } = useSWR('/datasets/retrieval-setting', fetchSupportRetrievalMethods)
  72. const agentThoughtModelList = useMemo(() => {
  73. const result: Model[] = []
  74. if (textGenerationModelList?.data) {
  75. textGenerationModelList?.data.forEach((item) => {
  76. const agentThoughtModels = item.models.filter(model => model.features?.includes(ModelFeatureEnum.agentThought))
  77. if (agentThoughtModels.length) {
  78. result.push({
  79. ...item,
  80. models: agentThoughtModels,
  81. })
  82. }
  83. })
  84. return result
  85. }
  86. return []
  87. }, [textGenerationModelList])
  88. const [plan, setPlan] = useState(defaultPlan)
  89. const [isFetchedPlan, setIsFetchedPlan] = useState(false)
  90. const [enableBilling, setEnableBilling] = useState(true)
  91. const [enableReplaceWebAppLogo, setEnableReplaceWebAppLogo] = useState(false)
  92. const handleOperateUtm = () => {
  93. let utm
  94. try {
  95. utm = JSON.parse(localStorage?.getItem('utm') || '{}')
  96. }
  97. catch (e) {
  98. utm = {
  99. utm_source: '',
  100. utm_medium: '',
  101. utm_campaign: '',
  102. utm_content: '',
  103. utm_term: '',
  104. }
  105. }
  106. if (utm.utm_source || utm.utm_medium || utm.utm_campaign || utm.utm_content || utm.utm_term)
  107. operationUtm({ url: '/operation/utm', body: utm })
  108. }
  109. useEffect(() => {
  110. (async () => {
  111. const data = await fetchCurrentPlanInfo()
  112. const enabled = data.billing.enabled
  113. setEnableBilling(enabled)
  114. setEnableReplaceWebAppLogo(data.can_replace_logo)
  115. if (enabled) {
  116. setPlan(parseCurrentPlan(data))
  117. handleOperateUtm()
  118. setIsFetchedPlan(true)
  119. }
  120. })()
  121. }, [])
  122. return (
  123. <ProviderContext.Provider value={{
  124. modelProviders: providersData?.data || [],
  125. textGenerationModelList: textGenerationModelList?.data || [],
  126. agentThoughtModelList,
  127. hasSettedApiKey: !!textGenerationModelList?.data.some(model => model.status === ModelStatusEnum.active),
  128. supportRetrievalMethods: supportRetrievalMethods?.retrieval_method || [],
  129. plan,
  130. isFetchedPlan,
  131. enableBilling,
  132. enableReplaceWebAppLogo,
  133. }}>
  134. {children}
  135. </ProviderContext.Provider>
  136. )
  137. }
  138. export default ProviderContext