Skip to content

Commit 7a4cd75

Browse files
committed
refactor(model-cache): inline redundant refreshPromise IIFE in refreshModels
1 parent 055e052 commit 7a4cd75

1 file changed

Lines changed: 33 additions & 41 deletions

File tree

src/api/providers/fetchers/modelCache.ts

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -399,53 +399,45 @@ export const refreshModels = async (options: GetModelsOptions): Promise<ModelRec
399399
// rather than sharing that promise's resolution/rejection wholesale.
400400
const sharedFetch = shouldSkipCache ? fetchModelsFromProvider(options) : dedupedFetch(cacheKey, options)
401401

402-
const refreshPromise = (async (): Promise<ModelRecord> => {
403-
try {
404-
// Force fresh API fetch - skip getModelsFromCache() check
405-
const models = await sharedFetch
406-
const modelCount = Object.keys(models).length
407-
408-
// Get existing cached data for comparison
409-
const existingCache = shouldSkipCache ? undefined : getModelsFromCache(options)
410-
const existingCount = existingCache ? Object.keys(existingCache).length : 0
411-
412-
if (modelCount === 0) {
413-
captureModelCacheEmptyResponseOnce(provider, cacheKey, {
414-
context: "refreshModels",
415-
hasExistingCache: existingCount > 0,
416-
existingCacheSize: existingCount,
417-
})
418-
if (existingCount > 0) {
419-
return existingCache!
420-
} else {
421-
return {}
422-
}
423-
}
402+
try {
403+
// Force fresh API fetch - skip getModelsFromCache() check
404+
const models = await sharedFetch
405+
const modelCount = Object.keys(models).length
424406

425-
reportedEmptyModelResponse.delete(cacheKey)
407+
// Get existing cached data for comparison
408+
const existingCache = shouldSkipCache ? undefined : getModelsFromCache(options)
409+
const existingCount = existingCache ? Object.keys(existingCache).length : 0
426410

427-
if (!shouldSkipCache) {
428-
memoryCache.set(cacheKey, models)
411+
if (modelCount === 0) {
412+
captureModelCacheEmptyResponseOnce(provider, cacheKey, {
413+
context: "refreshModels",
414+
hasExistingCache: existingCount > 0,
415+
existingCacheSize: existingCount,
416+
})
417+
return existingCount > 0 ? existingCache! : {}
418+
}
429419

430-
await writeModels(cacheKey, models).catch((err) =>
431-
console.error(`[refreshModels] Error writing ${cacheKey} models to disk:`, err),
432-
)
433-
}
420+
reportedEmptyModelResponse.delete(cacheKey)
434421

435-
return models
436-
} catch (error) {
437-
// Log the error for debugging, then return existing cache if available (graceful degradation).
438-
// For auth-scoped providers (zoo-gateway) we MUST NOT return cached models from a prior
439-
// session, since they could belong to a different user -- return empty instead.
440-
console.error(`[refreshModels] Failed to refresh ${cacheKey} models:`, error)
441-
if (shouldSkipCache) {
442-
return {}
443-
}
444-
return getModelsFromCache(options) || {}
422+
if (!shouldSkipCache) {
423+
memoryCache.set(cacheKey, models)
424+
425+
await writeModels(cacheKey, models).catch((err) =>
426+
console.error(`[refreshModels] Error writing ${cacheKey} models to disk:`, err),
427+
)
445428
}
446-
})()
447429

448-
return refreshPromise
430+
return models
431+
} catch (error) {
432+
// Log the error for debugging, then return existing cache if available (graceful degradation).
433+
// For auth-scoped providers (zoo-gateway) we MUST NOT return cached models from a prior
434+
// session, since they could belong to a different user -- return empty instead.
435+
console.error(`[refreshModels] Failed to refresh ${cacheKey} models:`, error)
436+
if (shouldSkipCache) {
437+
return {}
438+
}
439+
return getModelsFromCache(options) || {}
440+
}
449441
}
450442

451443
/**

0 commit comments

Comments
 (0)