fix: cache model list and fire change event to avoid startup race

This commit is contained in:
evanp committed 2026-11-06 13:13:46 -05:00
1 parent 76da3ac01e
commit 713a1c1d75
1 file changed
+42 -18
+42 -18
View File
@@ -3,30 +3,34 @@ import * as vscode from 'vscode';
const BASE_URL = 'https://gateway.inference.coop/v1'; const BASE_URL = 'https://gateway.inference.coop/v1';
const PREFIX = 'inference-coop/'; const PREFIX = 'inference-coop/';
type CoopModel = {
id: string; name: string; family: string; version: string;
maxInputTokens: number; maxOutputTokens: number;
capabilities: { toolCalling: boolean; imageInput: boolean };
};
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
const out = vscode.window.createOutputChannel('Inference Coop'); const out = vscode.window.createOutputChannel('Inference Coop');
context.subscriptions.push(out); context.subscriptions.push(out);
out.appendLine('activate() called');
const provider: vscode.LanguageModelChatProvider = { const changed = new vscode.EventEmitter<void>();
async provideLanguageModelChatInformation() { context.subscriptions.push(changed);
out.appendLine('provideLanguageModelChatInformation: called');
const key = vscode.workspace let cached: CoopModel[] = [];
.getConfiguration('inferenceCoop') let lastFetch = 0;
.get<string>('apiKey');
if (!key) {return [];}
async function refresh(force = false): Promise<CoopModel[]> {
if (!force && Date.now() - lastFetch < 30_000 && cached.length) {return cached;}
const key = vscode.workspace.getConfiguration('inferenceCoop').get<string>('apiKey');
if (!key) {return cached;}
try {
const res = await fetch(`${BASE_URL}/models`, { const res = await fetch(`${BASE_URL}/models`, {
headers: { Authorization: `Bearer ${key}` } headers: { Authorization: `Bearer ${key}` }
}); });
out.appendLine(` /models status: ${res.status}`); out.appendLine(`refresh: /models ${res.status}`);
if (!res.ok) {return [];} if (!res.ok) {return cached;}
const body = (await res.json()) as { data: Array<{ id: string; mode?: string }> };
const body = (await res.json()) as { const next: CoopModel[] = body.data
data: Array<{ id: string; mode?: string }>;
};
const models = body.data
.filter(m => !m.mode || m.mode === 'chat') .filter(m => !m.mode || m.mode === 'chat')
.map(m => ({ .map(m => ({
id: `${PREFIX}${m.id}`, id: `${PREFIX}${m.id}`,
@@ -37,8 +41,29 @@ export function activate(context: vscode.ExtensionContext) {
maxOutputTokens: 8192, maxOutputTokens: 8192,
capabilities: { toolCalling: true, imageInput: false } capabilities: { toolCalling: true, imageInput: false }
})); }));
out.appendLine(` returning ${models.length} models`); lastFetch = Date.now();
return models; if (JSON.stringify(next) !== JSON.stringify(cached)) {
cached = next;
changed.fire();
out.appendLine(`refresh: ${cached.length} models, fired change event`);
}
return cached;
} catch (e) {
out.appendLine(`refresh failed: ${e}`);
return cached;
}
}
const provider: vscode.LanguageModelChatProvider = {
onDidChangeLanguageModelChatInformation: changed.event,
async provideLanguageModelChatInformation() {
out.appendLine('provideLanguageModelChatInformation: called');
if (cached.length) {
void refresh(); // refresh in background, answer now
return cached;
}
return await refresh(true); // first call: must wait
}, },
async provideLanguageModelChatResponse(model, messages, options, progress, token) { async provideLanguageModelChatResponse(model, messages, options, progress, token) {
@@ -222,7 +247,6 @@ function toOpenAIMessages(
if (toolCalls.length) {m.tool_calls = toolCalls;} if (toolCalls.length) {m.tool_calls = toolCalls;}
result.push(m); result.push(m);
} }
// OpenAI requires tool results to follow the assistant message that requested them.
result.push(...toolResults); result.push(...toolResults);
} }