From 713a1c1d7529774e580a9b9482f0230edaccea74 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 6 Nov 2026 13:13:46 -0500 Subject: [PATCH] fix: cache model list and fire change event to avoid startup race --- src/extension.ts | 60 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index c9e5e45..a7eca98 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,30 +3,34 @@ import * as vscode from 'vscode'; const BASE_URL = 'https://gateway.inference.coop/v1'; 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) { const out = vscode.window.createOutputChannel('Inference Coop'); context.subscriptions.push(out); - out.appendLine('activate() called'); - const provider: vscode.LanguageModelChatProvider = { - async provideLanguageModelChatInformation() { - out.appendLine('provideLanguageModelChatInformation: called'); + const changed = new vscode.EventEmitter(); + context.subscriptions.push(changed); - const key = vscode.workspace - .getConfiguration('inferenceCoop') - .get('apiKey'); - if (!key) {return [];} + let cached: CoopModel[] = []; + let lastFetch = 0; + async function refresh(force = false): Promise { + if (!force && Date.now() - lastFetch < 30_000 && cached.length) {return cached;} + const key = vscode.workspace.getConfiguration('inferenceCoop').get('apiKey'); + if (!key) {return cached;} + try { const res = await fetch(`${BASE_URL}/models`, { headers: { Authorization: `Bearer ${key}` } }); - out.appendLine(` /models status: ${res.status}`); - if (!res.ok) {return [];} - - const body = (await res.json()) as { - data: Array<{ id: string; mode?: string }>; - }; - const models = body.data + out.appendLine(`refresh: /models ${res.status}`); + if (!res.ok) {return cached;} + const body = (await res.json()) as { data: Array<{ id: string; mode?: string }> }; + const next: CoopModel[] = body.data .filter(m => !m.mode || m.mode === 'chat') .map(m => ({ id: `${PREFIX}${m.id}`, @@ -37,8 +41,29 @@ export function activate(context: vscode.ExtensionContext) { maxOutputTokens: 8192, capabilities: { toolCalling: true, imageInput: false } })); - out.appendLine(` returning ${models.length} models`); - return models; + lastFetch = Date.now(); + 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) { @@ -222,7 +247,6 @@ function toOpenAIMessages( if (toolCalls.length) {m.tool_calls = toolCalls;} result.push(m); } - // OpenAI requires tool results to follow the assistant message that requested them. result.push(...toolResults); }