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
+94 -12
+94 -12
View File
@@ -1,43 +1,125 @@
import * as vscode from 'vscode';
const BASE_URL = 'https://gateway.inference.coop/v1';
const PREFIX = 'inference-coop/';
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 key = vscode.workspace
.getConfiguration('inferenceCoop')
.get<string>('apiKey');
if (!key) {
return [];
}
out.appendLine(` key present: ${Boolean(key)}`);
if (!key) {return [];}
const res = await fetch(`${BASE_URL}/models`, {
headers: { Authorization: `Bearer ${key}` }
});
out.appendLine(` /models status: ${res.status}`);
if (!res.ok) {
vscode.window.showErrorMessage(
`Inference Coop: model list failed (HTTP ${res.status})`
);
out.appendLine(` body: ${await res.text()}`);
return [];
}
const body = (await res.json()) as { data: Array<{ id: string; mode?: string }> };
return body.data
const body = (await res.json()) as {
data: Array<{ id: string; mode?: string }>;
};
out.appendLine(` raw models: ${body.data.length}`);
const models = body.data
.filter(m => !m.mode || m.mode === 'chat')
.map(m => ({
id: m.id,
id: `${PREFIX}${m.id}`,
name: m.id,
family: 'inference-coop',
version: '1.0.0',
maxInputTokens: 131072,
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() {
return 0;
}