feat: chat message with debug output

This commit is contained in:
evanp committed 2026-11-06 12:16:54 -05:00
1 parent f6c266c96a
commit 52e29529c3
1 file changed
+93 -11
+93 -11
View File
@@ -1,32 +1,41 @@
import * as vscode from 'vscode'; 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/';
export function activate(context: vscode.ExtensionContext) { 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 = { const provider: vscode.LanguageModelChatProvider = {
async provideLanguageModelChatInformation() { async provideLanguageModelChatInformation() {
out.appendLine('provideLanguageModelChatInformation: called');
const key = vscode.workspace const key = vscode.workspace
.getConfiguration('inferenceCoop') .getConfiguration('inferenceCoop')
.get<string>('apiKey'); .get<string>('apiKey');
if (!key) { out.appendLine(` key present: ${Boolean(key)}`);
return []; if (!key) {return [];}
}
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}`);
if (!res.ok) { if (!res.ok) {
vscode.window.showErrorMessage( out.appendLine(` body: ${await res.text()}`);
`Inference Coop: model list failed (HTTP ${res.status})`
);
return []; return [];
} }
const body = (await res.json()) as { data: Array<{ id: string; mode?: string }> }; const body = (await res.json()) as {
return body.data data: Array<{ id: string; mode?: string }>;
};
out.appendLine(` raw models: ${body.data.length}`);
const models = body.data
.filter(m => !m.mode || m.mode === 'chat') .filter(m => !m.mode || m.mode === 'chat')
.map(m => ({ .map(m => ({
id: m.id, id: `${PREFIX}${m.id}`,
name: m.id, name: m.id,
family: 'inference-coop', family: 'inference-coop',
version: '1.0.0', version: '1.0.0',
@@ -34,10 +43,83 @@ 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`);
return models;
}, },
async provideLanguageModelChatResponse() {
throw new Error('Not implemented yet'); async provideLanguageModelChatResponse(model, messages, _options, progress, token) {
out.appendLine(`provideLanguageModelChatResponse: ${model.id}`);
const key = vscode.workspace
.getConfiguration('inferenceCoop')
.get<string>('apiKey');
const body = {
model: model.id.replace(PREFIX, ''),
stream: true,
messages: messages.map(m => ({
role:
m.role === vscode.LanguageModelChatMessageRole.Assistant
? 'assistant'
: 'user',
content: m.content
.filter(
(p): p is vscode.LanguageModelTextPart =>
p instanceof vscode.LanguageModelTextPart
)
.map(p => p.value)
.join('\n')
}))
};
const res = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json'
}, },
body: JSON.stringify(body),
signal: token.isCancellationRequested ? AbortSignal.abort() : undefined
});
out.appendLine(` /chat/completions status: ${res.status}`);
if (!res.ok || !res.body) {
throw new Error(`Inference Coop: HTTP ${res.status} — ${await res.text()}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
let chunks = 0;
while (true) {
const { done, value } = await reader.read();
if (done) {break;}
buf += decoder.decode(value, { stream: true });
const frames = buf.split('\n\n');
buf = frames.pop() ?? '';
for (const frame of frames) {
for (const line of frame.split('\n')) {
if (!line.startsWith('data:')) {continue;}
const data = line.slice(5).trim();
if (data === '[DONE]') {
out.appendLine(` done, ${chunks} text chunks`);
return;
}
try {
const delta = JSON.parse(data).choices?.[0]?.delta?.content;
if (delta) {
chunks++;
progress.report(new vscode.LanguageModelTextPart(delta));
}
} catch {
/* partial frame, ignore */
}
}
}
}
out.appendLine(` stream ended, ${chunks} text chunks`);
},
async provideTokenCount() { async provideTokenCount() {
return 0; return 0;
} }