@@ -44,6 +44,7 @@ import {
44
44
} from "../utils/fluxNode" ;
45
45
import { useLocalStorage } from "../utils/lstore" ;
46
46
import { mod } from "../utils/mod" ;
47
+ import { getAvailableChatModels } from "../utils/models" ;
47
48
import { generateNodeId , generateStreamId } from "../utils/nodeId" ;
48
49
import { messagesFromLineage , promptFromLineage } from "../utils/prompt" ;
49
50
import { getQueryParam , resetURL } from "../utils/qparams" ;
@@ -864,11 +865,57 @@ function App() {
864
865
865
866
const [ apiKey , setApiKey ] = useLocalStorage < string > ( API_KEY_LOCAL_STORAGE_KEY ) ;
866
867
867
- const isAnythingLoading = isSavingReactFlow || isSavingSettings ;
868
+ const [ availableModels , setAvailableModels ] = useState < string [ ] | null > ( null ) ;
869
+
870
+ // modelsLoadCounter lets us discard the results of the requests if a concurrent newer one was made.
871
+ const modelsLoadCounter = useRef ( 0 ) ;
872
+ useEffect ( ( ) => {
873
+ if ( isValidAPIKey ( apiKey ) ) {
874
+ const modelsLoadIndex = modelsLoadCounter . current + 1 ;
875
+ modelsLoadCounter . current = modelsLoadIndex ;
876
+
877
+ setAvailableModels ( null ) ;
878
+
879
+ ( async ( ) => {
880
+ let modelList : string [ ] = [ ] ;
881
+ try {
882
+ modelList = await getAvailableChatModels ( apiKey ! ) ;
883
+ } catch ( e ) {
884
+ toast ( {
885
+ title : "Failed to load model list!" ,
886
+ status : "error" ,
887
+ ...TOAST_CONFIG ,
888
+ } ) ;
889
+ }
890
+ if ( modelsLoadIndex !== modelsLoadCounter . current ) return ;
891
+
892
+ if ( modelList . length === 0 ) modelList . push ( settings . model ) ;
893
+
894
+ setAvailableModels ( modelList ) ;
895
+
896
+ if ( ! modelList . includes ( settings . model ) ) {
897
+ const oldModel = settings . model ;
898
+ const newModel = modelList . includes ( DEFAULT_SETTINGS . model ) ? DEFAULT_SETTINGS . model : modelList [ 0 ] ;
899
+
900
+ setSettings ( ( settings ) => ( { ...settings , model : newModel } ) ) ;
901
+
902
+ toast ( {
903
+ title : `Model "${ oldModel } " no longer available!` ,
904
+ description : `Switched to "${ newModel } "` ,
905
+ status : "warning" ,
906
+ ...TOAST_CONFIG ,
907
+ } ) ;
908
+ }
909
+ } ) ( ) ;
910
+ }
911
+ } , [ apiKey ] ) ;
912
+
913
+ const isAnythingSaving = isSavingReactFlow || isSavingSettings ;
914
+ const isAnythingLoading = isAnythingSaving || ( availableModels === null ) ;
868
915
869
916
useBeforeunload ( ( event : BeforeUnloadEvent ) => {
870
917
// Prevent leaving the page before saving.
871
- if ( isAnythingLoading ) event . preventDefault ( ) ;
918
+ if ( isAnythingSaving ) event . preventDefault ( ) ;
872
919
} ) ;
873
920
874
921
/*//////////////////////////////////////////////////////////////
@@ -1000,6 +1047,7 @@ function App() {
1000
1047
onClose = { onCloseSettingsModal }
1001
1048
apiKey = { apiKey }
1002
1049
setApiKey = { setApiKey }
1050
+ availableModels = { availableModels }
1003
1051
/>
1004
1052
< Column
1005
1053
mainAxisAlignment = "center"
0 commit comments