fix: pass LiteLLM credentials when flushing model cache

When refreshing LiteLLM models, the cache flush was not receiving the
required apiKey and baseUrl parameters, causing an 'Invalid URL' error.

This fix:
- Updates flushModels() to accept optional GetModelsOptions parameter
- Passes LiteLLM credentials when flushing cache in both places:
  1. When explicitly refreshing via flushRouterModels message
  2. When requesting models with new credentials

Fixes #9682
This commit is contained in:
Roo Code 2025-11-28 22:51:44 +00:00
parent 127ecf6ddd
commit f3df489999
2 changed files with 15 additions and 3 deletions

View file

@ -273,13 +273,20 @@ export async function initializeModelCacheRefresh(): Promise<void> {
*
* @param router - The router to flush models for.
* @param refresh - If true, immediately fetch fresh data from API
* @param options - Optional provider options (e.g., apiKey, baseUrl) needed for certain providers
*/
export const flushModels = async (router: RouterName, refresh: boolean = false): Promise<void> => {
export const flushModels = async (
router: RouterName,
refresh: boolean = false,
options?: GetModelsOptions,
): Promise<void> => {
if (refresh) {
// Don't delete memory cache - let refreshModels atomically replace it
// This prevents a race condition where getModels() might be called
// before refresh completes, avoiding a gap in cache availability
refreshModels({ provider: router } as GetModelsOptions).catch((error) => {
// Use provided options if available, otherwise fallback to minimal options
const refreshOptions = options || ({ provider: router } as GetModelsOptions)
refreshModels(refreshOptions).catch((error) => {
console.error(`[flushModels] Refresh failed for ${router}:`, error)
})
} else {

View file

@ -876,7 +876,12 @@ export const webviewMessageHandler = async (
// If explicit credentials are provided in message.values (from Refresh Models button),
// flush the cache first to ensure we fetch fresh data with the new credentials
if (message?.values?.litellmApiKey || message?.values?.litellmBaseUrl) {
await flushModels("litellm", true)
const litellmFlushOptions: GetModelsOptions = {
provider: "litellm",
apiKey: litellmApiKey,
baseUrl: litellmBaseUrl,
}
await flushModels("litellm", true, litellmFlushOptions)
}
candidates.push({