@@ -9,15 +9,15 @@ import '@/assets/styles/markdown.css';
99import { ThemeProvider } from '@c-x/ui' ;
1010import { getUserProfile } from '@/api/UserManage' ;
1111import { getAdminProfile } from '@/api/Admin' ;
12+ import { getMyModelList } from '@/api/Model' ;
1213import dayjs from 'dayjs' ;
1314import duration from 'dayjs/plugin/duration' ;
1415import relativeTime from 'dayjs/plugin/relativeTime' ;
15- import { AuthContext } from './context' ;
16- import { DomainUser , DomainAdminUser } from './api/types' ;
16+ import { AuthContext , CommonContext } from './context' ;
17+ import { DomainUser , DomainAdminUser , DomainModel } from './api/types' ;
1718import { lightTheme } from './theme' ;
1819import router from './router' ;
1920import { getRedirectUrl } from './utils' ;
20- import { Loading } from '@c-x/ui' ;
2121
2222dayjs . locale ( 'zh-cn' ) ;
2323dayjs . extend ( duration ) ;
@@ -26,12 +26,56 @@ dayjs.extend(relativeTime);
2626const App = ( ) => {
2727 const [ user , setUser ] = useState < DomainUser | DomainAdminUser | null > ( null ) ;
2828 const [ loading , setLoading ] = useState ( true ) ;
29+ const [ coderModel , setCoderModel ] = useState < DomainModel [ ] > ( [ ] ) ;
30+ const [ llmModel , setLlmModel ] = useState < DomainModel [ ] > ( [ ] ) ;
31+ const [ isConfigModel , setIsConfigModel ] = useState ( false ) ;
2932
3033 const onGotoRedirect = ( source : 'user' | 'admin' ) => {
3134 const redirectUrl = getRedirectUrl ( source ) ;
3235 window . location . href = redirectUrl . href ;
3336 } ;
3437
38+ const getModelList = ( ) => {
39+ return Promise . all ( [
40+ getMyModelList ( {
41+ model_type : 'coder' ,
42+ } ) ,
43+ getMyModelList ( {
44+ model_type : 'llm' ,
45+ } ) ,
46+ ] )
47+ . then ( ( res ) => {
48+ setCoderModel ( res [ 0 ] || [ ] ) ;
49+ setLlmModel ( res [ 1 ] || [ ] ) ;
50+ return res ;
51+ } )
52+ . then ( handleModelConfig ) ;
53+ } ;
54+
55+ const handleModelConfig = ( res : [ DomainModel [ ] , DomainModel [ ] ] ) => {
56+ if ( ( res [ 0 ] || [ ] ) ?. length == 0 || ( res [ 1 ] || [ ] ) ?. length == 0 ) {
57+ if ( location . pathname !== '/model' ) {
58+ window . location . href = '/model' ;
59+ }
60+ setIsConfigModel ( false ) ;
61+ return false ;
62+ } else {
63+ const isActive =
64+ res [ 0 ] . every ( ( item ) => item . is_active ) &&
65+ res [ 1 ] . every ( ( item ) => item . is_active ) ;
66+ if ( isActive ) {
67+ setIsConfigModel ( true ) ;
68+ return true ;
69+ } else {
70+ if ( location . pathname !== '/model' ) {
71+ window . location . href = '/model' ;
72+ }
73+ setIsConfigModel ( false ) ;
74+ return false ;
75+ }
76+ }
77+ } ;
78+
3579 const getUser = ( ) => {
3680 setLoading ( true ) ;
3781 if ( location . pathname . startsWith ( '/user' ) ) {
@@ -49,9 +93,13 @@ const App = () => {
4993 return getAdminProfile ( )
5094 . then ( ( res ) => {
5195 setUser ( res ) ;
52- if ( location . pathname . startsWith ( '/login' ) ) {
53- onGotoRedirect ( 'admin' ) ;
54- }
96+ getModelList ( ) . then ( ( res ) => {
97+ if ( res ) {
98+ if ( location . pathname . startsWith ( '/login' ) ) {
99+ onGotoRedirect ( 'admin' ) ;
100+ }
101+ }
102+ } ) ;
55103 } )
56104 . finally ( ( ) => {
57105 setLoading ( false ) ;
@@ -71,18 +119,27 @@ const App = () => {
71119
72120 return (
73121 < ThemeProvider theme = { lightTheme } >
74- < AuthContext . Provider
75- value = { [
76- user ,
77- {
78- loading,
79- setUser,
80- refreshUser : getUser ,
81- } ,
82- ] }
122+ < CommonContext . Provider
123+ value = { {
124+ coderModel,
125+ llmModel,
126+ isConfigModel,
127+ refreshModel : getModelList ,
128+ } }
83129 >
84- < RouterProvider router = { router } />
85- </ AuthContext . Provider >
130+ < AuthContext . Provider
131+ value = { [
132+ user ,
133+ {
134+ loading,
135+ setUser,
136+ refreshUser : getUser ,
137+ } ,
138+ ] }
139+ >
140+ < RouterProvider router = { router } />
141+ </ AuthContext . Provider >
142+ </ CommonContext . Provider >
86143 </ ThemeProvider >
87144 ) ;
88145} ;
0 commit comments