| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { createContext, useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import useSWR from 'swr' | 
					
						
							| 
									
										
										
										
											2024-01-31 14:06:27 +08:00
										 |  |  | import { useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   fetchModelList, | 
					
						
							|  |  |  |   fetchModelProviders, | 
					
						
							|  |  |  |   fetchSupportRetrievalMethods, | 
					
						
							|  |  |  | } from '@/service/common' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   ModelStatusEnum, | 
					
						
							|  |  |  |   ModelTypeEnum, | 
					
						
							|  |  |  | } from '@/app/components/header/account-setting/model-provider-page/declarations' | 
					
						
							|  |  |  | import type { Model, ModelProvider } from '@/app/components/header/account-setting/model-provider-page/declarations' | 
					
						
							| 
									
										
										
										
											2023-11-18 11:53:35 +08:00
										 |  |  | import type { RETRIEVE_METHOD } from '@/types/app' | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  | import { Plan, type UsagePlanInfo } from '@/app/components/billing/type' | 
					
						
							|  |  |  | import { fetchCurrentPlanInfo } from '@/service/billing' | 
					
						
							|  |  |  | import { parseCurrentPlan } from '@/app/components/billing/utils' | 
					
						
							|  |  |  | import { defaultPlan } from '@/app/components/billing/config' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:13 +08:00
										 |  |  | const ProviderContext = createContext<{ | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |   modelProviders: ModelProvider[] | 
					
						
							|  |  |  |   textGenerationModelList: Model[] | 
					
						
							| 
									
										
										
										
											2023-11-18 11:53:35 +08:00
										 |  |  |   supportRetrievalMethods: RETRIEVE_METHOD[] | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |   hasSettedApiKey: boolean | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   plan: { | 
					
						
							|  |  |  |     type: Plan | 
					
						
							|  |  |  |     usage: UsagePlanInfo | 
					
						
							|  |  |  |     total: UsagePlanInfo | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   isFetchedPlan: boolean | 
					
						
							|  |  |  |   enableBilling: boolean | 
					
						
							| 
									
										
										
										
											2024-01-26 11:16:50 +08:00
										 |  |  |   onPlanInfoChanged: () => void | 
					
						
							| 
									
										
										
										
											2023-12-20 15:37:57 +08:00
										 |  |  |   enableReplaceWebAppLogo: boolean | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:13 +08:00
										 |  |  | }>({ | 
					
						
							| 
									
										
										
										
											2024-01-26 11:16:50 +08:00
										 |  |  |       modelProviders: [], | 
					
						
							|  |  |  |       textGenerationModelList: [], | 
					
						
							|  |  |  |       supportRetrievalMethods: [], | 
					
						
							|  |  |  |       hasSettedApiKey: true, | 
					
						
							|  |  |  |       plan: { | 
					
						
							|  |  |  |         type: Plan.sandbox, | 
					
						
							|  |  |  |         usage: { | 
					
						
							|  |  |  |           vectorSpace: 32, | 
					
						
							|  |  |  |           buildApps: 12, | 
					
						
							|  |  |  |           teamMembers: 1, | 
					
						
							|  |  |  |           annotatedResponse: 1, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         total: { | 
					
						
							|  |  |  |           vectorSpace: 200, | 
					
						
							|  |  |  |           buildApps: 50, | 
					
						
							|  |  |  |           teamMembers: 1, | 
					
						
							|  |  |  |           annotatedResponse: 10, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       isFetchedPlan: false, | 
					
						
							|  |  |  |       enableBilling: false, | 
					
						
							|  |  |  |       onPlanInfoChanged: () => { }, | 
					
						
							|  |  |  |       enableReplaceWebAppLogo: false, | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useProviderContext = () => useContext(ProviderContext) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ProviderContextProviderProps = { | 
					
						
							|  |  |  |   children: React.ReactNode | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export const ProviderContextProvider = ({ | 
					
						
							|  |  |  |   children, | 
					
						
							|  |  |  | }: ProviderContextProviderProps) => { | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |   const { data: providersData } = useSWR('/workspaces/current/model-providers', fetchModelProviders) | 
					
						
							|  |  |  |   const fetchModelListUrlPrefix = '/workspaces/current/models/model-types/' | 
					
						
							|  |  |  |   const { data: textGenerationModelList } = useSWR(`${fetchModelListUrlPrefix}${ModelTypeEnum.textGeneration}`, fetchModelList) | 
					
						
							| 
									
										
										
										
											2023-11-18 11:53:35 +08:00
										 |  |  |   const { data: supportRetrievalMethods } = useSWR('/datasets/retrieval-setting', fetchSupportRetrievalMethods) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   const [plan, setPlan] = useState(defaultPlan) | 
					
						
							|  |  |  |   const [isFetchedPlan, setIsFetchedPlan] = useState(false) | 
					
						
							|  |  |  |   const [enableBilling, setEnableBilling] = useState(true) | 
					
						
							| 
									
										
										
										
											2023-12-20 15:37:57 +08:00
										 |  |  |   const [enableReplaceWebAppLogo, setEnableReplaceWebAppLogo] = useState(false) | 
					
						
							| 
									
										
										
										
											2024-01-29 17:31:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:16:50 +08:00
										 |  |  |   const fetchPlan = async () => { | 
					
						
							|  |  |  |     const data = await fetchCurrentPlanInfo() | 
					
						
							|  |  |  |     const enabled = data.billing.enabled | 
					
						
							|  |  |  |     setEnableBilling(enabled) | 
					
						
							|  |  |  |     setEnableReplaceWebAppLogo(data.can_replace_logo) | 
					
						
							|  |  |  |     if (enabled) { | 
					
						
							|  |  |  |       setPlan(parseCurrentPlan(data)) | 
					
						
							|  |  |  |       setIsFetchedPlan(true) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							| 
									
										
										
										
											2024-01-26 11:16:50 +08:00
										 |  |  |     fetchPlan() | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:13 +08:00
										 |  |  |     <ProviderContext.Provider value={{ | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |       modelProviders: providersData?.data || [], | 
					
						
							|  |  |  |       textGenerationModelList: textGenerationModelList?.data || [], | 
					
						
							|  |  |  |       hasSettedApiKey: !!textGenerationModelList?.data.some(model => model.status === ModelStatusEnum.active), | 
					
						
							| 
									
										
										
										
											2023-11-18 11:53:35 +08:00
										 |  |  |       supportRetrievalMethods: supportRetrievalMethods?.retrieval_method || [], | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |       plan, | 
					
						
							|  |  |  |       isFetchedPlan, | 
					
						
							|  |  |  |       enableBilling, | 
					
						
							| 
									
										
										
										
											2024-01-26 11:16:50 +08:00
										 |  |  |       onPlanInfoChanged: fetchPlan, | 
					
						
							| 
									
										
										
										
											2023-12-20 15:37:57 +08:00
										 |  |  |       enableReplaceWebAppLogo, | 
					
						
							| 
									
										
										
										
											2023-08-12 00:57:13 +08:00
										 |  |  |     }}> | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |       {children} | 
					
						
							|  |  |  |     </ProviderContext.Provider> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default ProviderContext |