Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 7x 7x 7x 7x 7x 7x 7x 627x 7x 167x 167x 167x 167x 3x 1x 248x 248x 9x 239x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 121x 14x 107x 107x 109x 109x 109x 109x 109x 109x 109x 109x 109x 87x 22x 22x 22x 22x 21x 21x 1x 1x 25x 25x 24x 24x 24x 24x 24x 74x 24x 24x 25x | import * as vscode from "vscode";
import {
MODELS_CACHE_KEY_FINGERPRINT_STATE_KEY,
MODELS_CACHE_VERSION,
MODELS_CACHE_VERSION_STATE_KEY,
MODELS_STATE_KEY,
PROVIDER_DISPLAY_NAME,
PROVIDER_VENDOR,
DEFAULT_MAX_OUTPUT_TOKENS,
} from "../shared/constants";
import {
ELITE_MODELS_WHITELIST,
isNormalizedNvidiaModel,
normalizeNvidiaModels,
NormalizedNvidiaModel,
} from "./catalog";
import { fetchModels, fetchModelsOrThrow } from "../api/client";
import { outputLog } from "../shared/logging";
import { getModelAdapter } from "./adapters";
import { getApiKeyFingerprint, NvidiaApiKeyResolver } from "../api/key-resolver";
import {
reportMissingCuratedModels,
runSerializedModelCacheOperation,
writeModelCacheAtomically,
} from "./cache";
export interface ChatRuntimeMetadata {
supportsTools: boolean;
supportsVision: boolean;
contextWindow: number;
runtimeMetadataSource: "catalog" | "api" | "fallback";
}
export interface NvidiaConfigurationProperty {
type: "string" | "number" | "boolean" | "object" | "array";
title?: string;
description?: string;
enum?: readonly string[];
enumItemLabels?: readonly string[];
group?: string;
default?: string | number | boolean;
[key: string]: unknown;
}
export interface NvidiaConfigurationSchema {
properties: Record<string, NvidiaConfigurationProperty>;
[key: string]: unknown;
}
export interface NvidiaLanguageModelChatInformation extends vscode.LanguageModelChatInformation {
readonly id: string;
readonly contextWindow: number;
readonly maxOutputTokens: number;
isUserSelectable: boolean;
configurationSchema?: NvidiaConfigurationSchema;
}
function isCachedCuratedModel(value: unknown): value is NormalizedNvidiaModel {
return (
isNormalizedNvidiaModel(value) &&
Object.prototype.hasOwnProperty.call(ELITE_MODELS_WHITELIST, value.id)
);
}
export class NvidiaModelDiscoveryService {
constructor(
secrets: vscode.SecretStorage,
private readonly userAgent: string,
private readonly globalState?: vscode.Memento,
private readonly keyResolver = new NvidiaApiKeyResolver(secrets),
) {}
private cacheInvalidated = false;
public invalidateCache(): void {
this.cacheInvalidated = true;
}
public markCacheFresh(): void {
this.cacheInvalidated = false;
}
public getNormalizedModels(): NormalizedNvidiaModel[] {
const storedModels = this.globalState?.get<unknown>(MODELS_STATE_KEY);
if (!Array.isArray(storedModels)) {
return [];
}
return storedModels.filter(isCachedCuratedModel);
}
private hasNormalizedModelsCache(): boolean {
const storedModels = this.globalState?.get<unknown>(MODELS_STATE_KEY);
return Array.isArray(storedModels) && storedModels.every(isCachedCuratedModel);
}
public async getAvailableModels(
apiKey?: string,
options: { refreshStaleCache?: boolean } = {},
): Promise<NormalizedNvidiaModel[]> {
const cachedModels = this.getNormalizedModels().filter(isCachedCuratedModel);
const cacheVersion = this.globalState?.get<number>(MODELS_CACHE_VERSION_STATE_KEY);
const cachedKeyFingerprint = this.globalState?.get<string>(
MODELS_CACHE_KEY_FINGERPRINT_STATE_KEY,
);
const currentKeyFingerprint = apiKey ? getApiKeyFingerprint(apiKey) : undefined;
// A cache created before key fingerprints were introduced has no reliable
// ownership information. Treat it as stale whenever a runtime key is
// available instead of silently serving models fetched with another key.
const hasCachedKeyFingerprint =
typeof cachedKeyFingerprint === "string" && cachedKeyFingerprint.length > 0;
const keyChanged =
currentKeyFingerprint !== undefined &&
(!hasCachedKeyFingerprint || currentKeyFingerprint !== cachedKeyFingerprint);
const cacheVersionStale = cacheVersion !== MODELS_CACHE_VERSION;
// The option only controls whether a stale cache version may be used as a
// fallback. Explicit invalidation and key changes always require refresh.
const refreshStaleCache = options.refreshStaleCache !== false;
const shouldRefresh =
this.cacheInvalidated || keyChanged || (cacheVersionStale && refreshStaleCache);
if (this.hasNormalizedModelsCache() && (!apiKey || !shouldRefresh)) {
return cachedModels;
}
const refreshedModels = await this.fetchAvailableModels(apiKey);
// A cache owned by a previous key is not safe to expose after a key
// change, even when the refresh request fails. Same-key refreshes may
// continue using the last known curated list for resilience.
return refreshedModels ?? (keyChanged ? [] : cachedModels);
}
public async fetchAvailableModels(
configuredApiKey?: string,
): Promise<NormalizedNvidiaModel[] | undefined> {
return runSerializedModelCacheOperation(() =>
this.fetchAvailableModelsInternal(configuredApiKey),
);
}
private async fetchAvailableModelsInternal(
configuredApiKey?: string,
): Promise<NormalizedNvidiaModel[] | undefined> {
const resolved = await this.keyResolver.resolveConfiguredOrLegacy(configuredApiKey);
Iif (!resolved) {
return undefined;
}
const apiKey = resolved.value;
try {
// Keep compatibility with test/extension hosts that only expose the
// nullable legacy fetchModels function while production uses the
// structured-error variant.
const fetchModelsRequest =
typeof fetchModelsOrThrow === "function" ? fetchModelsOrThrow : fetchModels;
const rawModels = await fetchModelsRequest(apiKey, undefined, this.userAgent);
if (!Array.isArray(rawModels)) {
return undefined;
}
reportMissingCuratedModels(rawModels);
const normalized = normalizeNvidiaModels(rawModels);
if (this.globalState) {
await writeModelCacheAtomically(
this.globalState,
rawModels,
normalized,
getApiKeyFingerprint(apiKey),
);
}
this.cacheInvalidated = false;
return normalized;
} catch (err) {
outputLog(
"error",
`Failed to fetch models from API: ${err instanceof Error ? err.message : String(err)}`,
);
return undefined;
}
}
public mapToChatInformation(
models: readonly NormalizedNvidiaModel[],
): NvidiaLanguageModelChatInformation[] {
const info: NvidiaLanguageModelChatInformation[] = [];
for (const model of models) {
Iif (!isCachedCuratedModel(model)) {
continue;
}
const adapter = getModelAdapter(model.id);
let configurationSchema: NvidiaConfigurationSchema | undefined;
if (adapter.applyReasoningMode) {
const enumValues = adapter.supportedReasoningModes ?? [
"none",
"on",
"medium",
"high",
"max",
];
const enumItemLabels = enumValues.map((v) =>
v === "none" ? "None" : v.charAt(0).toUpperCase() + v.slice(1),
);
configurationSchema = {
properties: {
reasoningMode: {
type: "string",
title: "Reasoning Mode",
description: "Configure the reasoning effort mode sent to supported models.",
enum: enumValues,
enumItemLabels: enumItemLabels,
group: "navigation",
default: "none",
},
},
};
}
info.push({
id: model.id,
name: model.displayName ?? model.id,
detail: PROVIDER_DISPLAY_NAME,
tooltip: `${PROVIDER_DISPLAY_NAME} ${model.displayName ?? model.id}`,
family: PROVIDER_VENDOR,
version: "1.0.0",
maxInputTokens: Math.max(
1,
model.contextWindow - Math.min(model.maxOutputTokens ?? 65536, DEFAULT_MAX_OUTPUT_TOKENS),
),
maxOutputTokens: model.maxOutputTokens ?? 65536,
contextWindow: model.contextWindow,
isUserSelectable: true,
capabilities: {
toolCalling: model.supportsTools ? 128 : false,
imageInput: model.supportsVision ?? false,
},
...(configurationSchema ? { configurationSchema } : {}),
});
}
return info;
}
}
|