33 * Preventing TS checks with files presented in the video for a better presentation.
44 */
55import type { Message } from 'ai' ;
6- import React , { type RefCallback , useCallback , useEffect , useState } from 'react' ;
6+ import React , { type RefCallback , useEffect , useState } from 'react' ;
77import { ClientOnly } from 'remix-utils/client-only' ;
88import { Menu } from '~/components/sidebar/Menu.client' ;
99import { IconButton } from '~/components/ui/IconButton' ;
1010import { Workbench } from '~/components/workbench/Workbench.client' ;
1111import { classNames } from '~/utils/classNames' ;
12- import { MODEL_LIST , PROVIDER_LIST , initializeModelList } from '~/utils/constants' ;
12+ import { PROVIDER_LIST } from '~/utils/constants' ;
1313import { Messages } from './Messages.client' ;
1414import { SendButton } from './SendButton.client' ;
1515import { APIKeyManager , getApiKeysFromCookies } from './APIKeyManager' ;
@@ -25,13 +25,13 @@ import GitCloneButton from './GitCloneButton';
2525import FilePreview from './FilePreview' ;
2626import { ModelSelector } from '~/components/chat/ModelSelector' ;
2727import { SpeechRecognitionButton } from '~/components/chat/SpeechRecognition' ;
28- import type { IProviderSetting , ProviderInfo } from '~/types/model' ;
28+ import type { ProviderInfo } from '~/types/model' ;
2929import { ScreenshotStateManager } from './ScreenshotStateManager' ;
3030import { toast } from 'react-toastify' ;
3131import StarterTemplates from './StarterTemplates' ;
3232import type { ActionAlert } from '~/types/actions' ;
3333import ChatAlert from './ChatAlert' ;
34- import { LLMManager } from '~/lib/modules/llm/manager ' ;
34+ import type { ModelInfo } from '~/lib/modules/llm/types ' ;
3535
3636const TEXTAREA_MIN_HEIGHT = 76 ;
3737
@@ -102,35 +102,13 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
102102 ) => {
103103 const TEXTAREA_MAX_HEIGHT = chatStarted ? 400 : 200 ;
104104 const [ apiKeys , setApiKeys ] = useState < Record < string , string > > ( getApiKeysFromCookies ( ) ) ;
105- const [ modelList , setModelList ] = useState ( MODEL_LIST ) ;
105+ const [ modelList , setModelList ] = useState < ModelInfo [ ] > ( [ ] ) ;
106106 const [ isModelSettingsCollapsed , setIsModelSettingsCollapsed ] = useState ( false ) ;
107107 const [ isListening , setIsListening ] = useState ( false ) ;
108108 const [ recognition , setRecognition ] = useState < SpeechRecognition | null > ( null ) ;
109109 const [ transcript , setTranscript ] = useState ( '' ) ;
110110 const [ isModelLoading , setIsModelLoading ] = useState < string | undefined > ( 'all' ) ;
111111
112- const getProviderSettings = useCallback ( ( ) => {
113- let providerSettings : Record < string , IProviderSetting > | undefined = undefined ;
114-
115- try {
116- const savedProviderSettings = Cookies . get ( 'providers' ) ;
117-
118- if ( savedProviderSettings ) {
119- const parsedProviderSettings = JSON . parse ( savedProviderSettings ) ;
120-
121- if ( typeof parsedProviderSettings === 'object' && parsedProviderSettings !== null ) {
122- providerSettings = parsedProviderSettings ;
123- }
124- }
125- } catch ( error ) {
126- console . error ( 'Error loading Provider Settings from cookies:' , error ) ;
127-
128- // Clear invalid cookie data
129- Cookies . remove ( 'providers' ) ;
130- }
131-
132- return providerSettings ;
133- } , [ ] ) ;
134112 useEffect ( ( ) => {
135113 console . log ( transcript ) ;
136114 } , [ transcript ] ) ;
@@ -169,25 +147,25 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
169147
170148 useEffect ( ( ) => {
171149 if ( typeof window !== 'undefined' ) {
172- const providerSettings = getProviderSettings ( ) ;
173150 let parsedApiKeys : Record < string , string > | undefined = { } ;
174151
175152 try {
176153 parsedApiKeys = getApiKeysFromCookies ( ) ;
177154 setApiKeys ( parsedApiKeys ) ;
178155 } catch ( error ) {
179156 console . error ( 'Error loading API keys from cookies:' , error ) ;
180-
181- // Clear invalid cookie data
182157 Cookies . remove ( 'apiKeys' ) ;
183158 }
159+
184160 setIsModelLoading ( 'all' ) ;
185- initializeModelList ( { apiKeys : parsedApiKeys , providerSettings } )
186- . then ( ( modelList ) => {
187- setModelList ( modelList ) ;
161+ fetch ( '/api/models' )
162+ . then ( ( response ) => response . json ( ) )
163+ . then ( ( data ) => {
164+ const typedData = data as { modelList : ModelInfo [ ] } ;
165+ setModelList ( typedData . modelList ) ;
188166 } )
189167 . catch ( ( error ) => {
190- console . error ( 'Error initializing model list:' , error ) ;
168+ console . error ( 'Error fetching model list:' , error ) ;
191169 } )
192170 . finally ( ( ) => {
193171 setIsModelLoading ( undefined ) ;
@@ -200,29 +178,24 @@ export const BaseChat = React.forwardRef<HTMLDivElement, BaseChatProps>(
200178 setApiKeys ( newApiKeys ) ;
201179 Cookies . set ( 'apiKeys' , JSON . stringify ( newApiKeys ) ) ;
202180
203- const provider = LLMManager . getInstance ( import . meta . env || process . env || { } ) . getProvider ( providerName ) ;
181+ setIsModelLoading ( providerName ) ;
204182
205- if ( provider && provider . getDynamicModels ) {
206- setIsModelLoading ( providerName ) ;
183+ let providerModels : ModelInfo [ ] = [ ] ;
207184
208- try {
209- const providerSettings = getProviderSettings ( ) ;
210- const staticModels = provider . staticModels ;
211- const dynamicModels = await provider . getDynamicModels (
212- newApiKeys ,
213- providerSettings ,
214- import . meta. env || process . env || { } ,
215- ) ;
216-
217- setModelList ( ( preModels ) => {
218- const filteredOutPreModels = preModels . filter ( ( x ) => x . provider !== providerName ) ;
219- return [ ...filteredOutPreModels , ...staticModels , ...dynamicModels ] ;
220- } ) ;
221- } catch ( error ) {
222- console . error ( 'Error loading dynamic models:' , error ) ;
223- }
224- setIsModelLoading ( undefined ) ;
185+ try {
186+ const response = await fetch ( `/api/models/${ encodeURIComponent ( providerName ) } ` ) ;
187+ const data = await response . json ( ) ;
188+ providerModels = ( data as { modelList : ModelInfo [ ] } ) . modelList ;
189+ } catch ( error ) {
190+ console . error ( 'Error loading dynamic models for:' , providerName , error ) ;
225191 }
192+
193+ // Only update models for the specific provider
194+ setModelList ( ( prevModels ) => {
195+ const otherModels = prevModels . filter ( ( model ) => model . provider !== providerName ) ;
196+ return [ ...otherModels , ...providerModels ] ;
197+ } ) ;
198+ setIsModelLoading ( undefined ) ;
226199 } ;
227200
228201 const startListening = ( ) => {
0 commit comments