@@ -6,7 +6,7 @@ import * as vscode from "vscode"
66import { ClineProvider } from "./ClineProvider"
77import { Language , ProviderSettings , GlobalState , Package } from "../../schemas"
88import { changeLanguage , t } from "../../i18n"
9- import { RouterName , toRouterName } from "../../shared/api"
9+ import { RouterName , toRouterName , ModelRecord } from "../../shared/api"
1010import { supportPrompt } from "../../shared/support-prompt"
1111import { checkoutDiffPayloadSchema , checkoutRestorePayloadSchema , WebviewMessage } from "../../shared/WebviewMessage"
1212import { checkExistKey } from "../../shared/checkExistApiConfig"
@@ -32,6 +32,7 @@ import { TelemetrySetting } from "../../shared/TelemetrySetting"
3232import { getWorkspacePath } from "../../utils/path"
3333import { Mode , defaultModeSlug } from "../../shared/modes"
3434import { getModels , flushModels } from "../../api/providers/fetchers/modelCache"
35+ import { GetModelsOptions } from "../../shared/api"
3536import { generateSystemPrompt } from "./generateSystemPrompt"
3637import { getCommand } from "../../utils/commands"
3738
@@ -170,10 +171,6 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
170171 case "askResponse" :
171172 provider . getCurrentCline ( ) ?. handleWebviewAskResponse ( message . askResponse ! , message . text , message . images )
172173 break
173- case "autoCondenseContextPercent" :
174- await updateGlobalState ( "autoCondenseContextPercent" , message . value )
175- await provider . postStateToWebview ( )
176- break
177174 case "terminalOperation" :
178175 if ( message . terminalOperation ) {
179176 provider . getCurrentCline ( ) ?. handleTerminalOperation ( message . terminalOperation )
@@ -282,29 +279,81 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
282279 await provider . resetState ( )
283280 break
284281 case "flushRouterModels" :
285- const routerName : RouterName = toRouterName ( message . text )
286- await flushModels ( routerName )
282+ const routerNameFlush : RouterName = toRouterName ( message . text )
283+ await flushModels ( routerNameFlush )
287284 break
288285 case "requestRouterModels" :
289286 const { apiConfiguration } = await provider . getState ( )
290287
291- const [ openRouterModels , requestyModels , glamaModels , unboundModels , litellmModels ] = await Promise . all ( [
292- getModels ( "openrouter" , apiConfiguration . openRouterApiKey ) ,
293- getModels ( "requesty" , apiConfiguration . requestyApiKey ) ,
294- getModels ( "glama" , apiConfiguration . glamaApiKey ) ,
295- getModels ( "unbound" , apiConfiguration . unboundApiKey ) ,
296- getModels ( "litellm" , apiConfiguration . litellmApiKey , apiConfiguration . litellmBaseUrl ) ,
297- ] )
288+ const routerModels : Partial < Record < RouterName , ModelRecord > > = {
289+ openrouter : { } ,
290+ requesty : { } ,
291+ glama : { } ,
292+ unbound : { } ,
293+ litellm : { } ,
294+ }
295+
296+ const safeGetModels = async ( options : GetModelsOptions ) : Promise < ModelRecord > => {
297+ try {
298+ return await getModels ( options )
299+ } catch ( error ) {
300+ console . error (
301+ `Failed to fetch models in webviewMessageHandler requestRouterModels for ${ options . provider } :` ,
302+ error ,
303+ )
304+ throw error // Re-throw to be caught by Promise.allSettled
305+ }
306+ }
307+
308+ const modelFetchPromises : Array < { key : RouterName ; options : GetModelsOptions } > = [
309+ { key : "openrouter" , options : { provider : "openrouter" } } ,
310+ { key : "requesty" , options : { provider : "requesty" , apiKey : apiConfiguration . requestyApiKey } } ,
311+ { key : "glama" , options : { provider : "glama" } } ,
312+ { key : "unbound" , options : { provider : "unbound" , apiKey : apiConfiguration . unboundApiKey } } ,
313+ ]
314+
315+ const litellmApiKey = apiConfiguration . litellmApiKey || message ?. values ?. litellmApiKey
316+ const litellmBaseUrl = apiConfiguration . litellmBaseUrl || message ?. values ?. litellmBaseUrl
317+ if ( litellmApiKey && litellmBaseUrl ) {
318+ modelFetchPromises . push ( {
319+ key : "litellm" ,
320+ options : { provider : "litellm" , apiKey : litellmApiKey , baseUrl : litellmBaseUrl } ,
321+ } )
322+ }
323+
324+ const results = await Promise . allSettled (
325+ modelFetchPromises . map ( async ( { key, options } ) => {
326+ const models = await safeGetModels ( options )
327+ return { key, models } // key is RouterName here
328+ } ) ,
329+ )
330+
331+ const fetchedRouterModels : Partial < Record < RouterName , ModelRecord > > = { ...routerModels }
332+
333+ results . forEach ( ( result , index ) => {
334+ const routerName = modelFetchPromises [ index ] . key // Get RouterName using index
335+
336+ if ( result . status === "fulfilled" ) {
337+ fetchedRouterModels [ routerName ] = result . value . models
338+ } else {
339+ // Handle rejection: Post a specific error message for this provider
340+ const errorMessage = result . reason instanceof Error ? result . reason . message : String ( result . reason )
341+ console . error ( `Error fetching models for ${ routerName } :` , result . reason )
342+
343+ fetchedRouterModels [ routerName ] = { } // Ensure it's an empty object in the main routerModels message
344+
345+ provider . postMessageToWebview ( {
346+ type : "singleRouterModelFetchResponse" ,
347+ success : false ,
348+ error : errorMessage ,
349+ values : { provider : routerName } ,
350+ } )
351+ }
352+ } )
298353
299354 provider . postMessageToWebview ( {
300355 type : "routerModels" ,
301- routerModels : {
302- openrouter : openRouterModels ,
303- requesty : requestyModels ,
304- glama : glamaModels ,
305- unbound : unboundModels ,
306- litellm : litellmModels ,
307- } ,
356+ routerModels : fetchedRouterModels as Record < RouterName , ModelRecord > ,
308357 } )
309358 break
310359 case "requestOpenAiModels" :
0 commit comments