automatically derive openrouter modelinfo based on modelId when calling getModel() (#7568)

This commit is contained in:
celestial-vault authored and GitHub committed 2025-11-19 21:16:48 -08:00
1 parent 7f2d28716f
commit bd1d6159fc
3 files changed
+61 -7

No files matched your search

+5 -2
View File
@@ -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,
}
}
}
@@ -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<Record<string, ModelInfo>> {
const openRouterModelsFilePath = path.join(await ensureCacheDirectoryExists(), GlobalFileNames.openRouterModels)
const models: Record<string, ModelInfo> = {}
let models: Record<string, ModelInfo> = {}
try {
const response = await axios.get("https://openrouter.ai/api/v1/models", getAxiosSettings())
@@ -242,12 +243,17 @@ export async function refreshOpenRouterModels(controller: Controller): Promise<R
// If we failed to fetch models, try to read cached models
const cachedModels = await controller.readOpenRouterModels()
if (cachedModels) {
// Cached models are already in application format (ModelInfo)
return appendClineStealthModels(cachedModels as Record<string, ModelInfo>)
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
}
/**
+46 -1
View File
@@ -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<string, ModelInfo> | null
groqModels: Record<string, ModelInfo> | null
basetenModels: Record<string, ModelInfo> | null
huggingFaceModels: Record<string, ModelInfo> | null
requestyModels: Record<string, ModelInfo> | null
huaweiCloudMaasModels: Record<string, ModelInfo> | null
hicapModels: Record<string, ModelInfo> | null
aihubmixModels: Record<string, ModelInfo> | null
} = {
openRouterModels: null,
groqModels: null,
basetenModels: null,
huggingFaceModels: null,
requestyModels: null,
huaweiCloudMaasModels: null,
hicapModels: null,
aihubmixModels: null,
}
// Debounced persistence state
private pendingGlobalState = new Set<GlobalStateAndSettingsKey>()
private pendingTaskState = new Map<string, Set<SettingsKey>>()
@@ -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<string, ModelInfo>,
): 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.