diff --git a/src/core/api/providers/openrouter.ts b/src/core/api/providers/openrouter.ts index 05317bc8..1f192143 100644 --- a/src/core/api/providers/openrouter.ts +++ b/src/core/api/providers/openrouter.ts @@ -1,4 +1,5 @@ import { setTimeout as setTimeoutPromise } from "node:timers/promises" +import { StateManager } from "@core/storage/StateManager" import { ModelInfo, openRouterDefaultModelId, openRouterDefaultModelInfo } from "@shared/api" import { shouldSkipReasoningForModel } from "@utils/model-utils" import axios from "axios" @@ -214,9 +215,11 @@ export class OpenRouterHandler implements ApiHandler { } getModel(): { id: string; info: ModelInfo } { + const modelId = this.options.openRouterModelId || openRouterDefaultModelId + const cachedModelInfo = StateManager.get().getModelInfo("openRouter", modelId) return { - id: this.options.openRouterModelId || openRouterDefaultModelId, - info: this.options.openRouterModelInfo || openRouterDefaultModelInfo, + id: modelId, + info: cachedModelInfo || openRouterDefaultModelInfo, } } } diff --git a/src/core/controller/models/refreshOpenRouterModels.ts b/src/core/controller/models/refreshOpenRouterModels.ts index c80d0691..a961ea54 100644 --- a/src/core/controller/models/refreshOpenRouterModels.ts +++ b/src/core/controller/models/refreshOpenRouterModels.ts @@ -4,6 +4,7 @@ import axios from "axios" import cloneDeep from "clone-deep" import fs from "fs/promises" import path from "path" +import { StateManager } from "@/core/storage/StateManager" import { ANTHROPIC_MAX_THINKING_BUDGET, CLAUDE_SONNET_1M_TIERS, @@ -78,7 +79,7 @@ interface OpenRouterRawModelInfo { export async function refreshOpenRouterModels(controller: Controller): Promise> { const openRouterModelsFilePath = path.join(await ensureCacheDirectoryExists(), GlobalFileNames.openRouterModels) - const models: Record = {} + let models: Record = {} try { const response = await axios.get("https://openrouter.ai/api/v1/models", getAxiosSettings()) @@ -242,12 +243,17 @@ export async function refreshOpenRouterModels(controller: Controller): Promise) + models = cachedModels } } + // Append stealth models if any - return appendClineStealthModels(models) + const finalModels = appendClineStealthModels(models) + + // Store in StateManager's in-memory cache + StateManager.get().setModelsCache("openRouter", finalModels) + + return finalModels } /** diff --git a/src/core/storage/StateManager.ts b/src/core/storage/StateManager.ts index 293410ab..87760230 100644 --- a/src/core/storage/StateManager.ts +++ b/src/core/storage/StateManager.ts @@ -1,4 +1,4 @@ -import { ApiConfiguration } from "@shared/api" +import { ApiConfiguration, ModelInfo } from "@shared/api" import { GlobalState, GlobalStateAndSettings, @@ -44,6 +44,28 @@ export class StateManager { private context: ExtensionContext private isInitialized = false + // In-memory model info cache (not persisted to disk) + // These are for dynamic providers that fetch models from APIs + private modelInfoCache: { + openRouterModels: Record | null + groqModels: Record | null + basetenModels: Record | null + huggingFaceModels: Record | null + requestyModels: Record | null + huaweiCloudMaasModels: Record | null + hicapModels: Record | null + aihubmixModels: Record | null + } = { + openRouterModels: null, + groqModels: null, + basetenModels: null, + huggingFaceModels: null, + requestyModels: null, + huaweiCloudMaasModels: null, + hicapModels: null, + aihubmixModels: null, + } + // Debounced persistence state private pendingGlobalState = new Set() private pendingTaskState = new Map>() @@ -348,6 +370,29 @@ export class StateManager { this.remoteConfigCache = {} as GlobalStateAndSettings } + /** + * Set models cache for a specific provider (in-memory only, not persisted) + */ + setModelsCache( + provider: "openRouter" | "groq" | "baseten" | "huggingFace" | "requesty" | "huaweiCloudMaas" | "hicap" | "aihubmix", + models: Record, + ): void { + const cacheKey = `${provider}Models` as keyof typeof this.modelInfoCache + this.modelInfoCache[cacheKey] = models + } + + /** + * Get model info by provider and model ID (from in-memory cache) + */ + getModelInfo( + provider: "openRouter" | "groq" | "baseten" | "huggingFace" | "requesty" | "huaweiCloudMaas" | "hicap" | "aihubmix", + modelId: string, + ): ModelInfo | undefined { + const cacheKey = `${provider}Models` as keyof typeof this.modelInfoCache + console.log("[OpenRouter] modelInfoCache:", this.modelInfoCache) + return this.modelInfoCache[cacheKey]?.[modelId] + } + /** * Initialize chokidar watcher for the taskHistory.json file * Updates in-memory cache on external changes without writing back to disk.