@@ -17,8 +17,14 @@ export const useModelsQuery = (options?: Partial<UseQueryOptions<Model[]>>) => {
1717 } ) ;
1818} ;
1919
20- export const useModelSelection = ( options ?: { enabled ?: boolean ; chatId ?: string } ) => {
20+ export const useModelSelection = ( options ?: {
21+ enabled ?: boolean ;
22+ chatId ?: string ;
23+ // null = still loading (defer default), undefined = no initial model (use models[0])
24+ initialModelId ?: string | null ;
25+ } ) => {
2126 const chatId = options ?. chatId ?? DEFAULT_MODEL_KEY ;
27+ const initialModelId = options ?. initialModelId ;
2228 const { data : models = [ ] , isLoading } = useModelsQuery ( {
2329 enabled : options ?. enabled ,
2430 } ) ;
@@ -27,10 +33,17 @@ export const useModelSelection = (options?: { enabled?: boolean; chatId?: string
2733 useEffect ( ( ) => {
2834 if ( models . length === 0 ) return ;
2935 const selectedExists = models . some ( ( m ) => m . model_id === selectedModelId ) ;
30- if ( ! selectedExists ) {
31- useModelStore . getState ( ) . selectModel ( chatId , models [ 0 ] . model_id ) ;
32- }
33- } , [ models , selectedModelId , chatId ] ) ;
36+ if ( selectedExists ) return ;
37+
38+ // Still loading initial model from message history — wait before defaulting
39+ if ( initialModelId === null ) return ;
40+
41+ const fallback =
42+ initialModelId && models . some ( ( m ) => m . model_id === initialModelId )
43+ ? initialModelId
44+ : models [ 0 ] . model_id ;
45+ useModelStore . getState ( ) . selectModel ( chatId , fallback ) ;
46+ } , [ models , selectedModelId , chatId , initialModelId ] ) ;
3447
3548 const selectedModel = useMemo (
3649 ( ) => models . find ( ( m ) => m . model_id === selectedModelId ) ?? null ,
0 commit comments