fix: cache model list and fire change event to avoid startup race
This commit is contained in:
1 file changed
+42
-18
+42
-18
@@ -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<void>();
|
||||
context.subscriptions.push(changed);
|
||||
|
||||
const key = vscode.workspace
|
||||
.getConfiguration('inferenceCoop')
|
||||
.get<string>('apiKey');
|
||||
if (!key) {return [];}
|
||||
let cached: CoopModel[] = [];
|
||||
let lastFetch = 0;
|
||||
|
||||
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`, {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user