2 Commits
2 changed files with 198 additions and 86 deletions

No files matched your search

+19 -11
View File
@@ -28,8 +28,26 @@
{ {
"command": "inference-coop-copilot.helloWorld", "command": "inference-coop-copilot.helloWorld",
"title": "Hello World" "title": "Hello World"
},
{
"command": "inferenceCoop.setKey",
"title": "Inference Coop: Set API Key"
},
{
"command": "inferenceCoop.clearKey",
"title": "Inference Coop: Sign Out"
}
],
"configuration": {
"title": "Inference Coop",
"properties": {
"inferenceCoop.baseUrl": {
"type": "string",
"default": "https://gateway.inference.coop/v1",
"description": "Gateway base URL. Leave default unless testing."
}
}
} }
]
}, },
"scripts": { "scripts": {
"vscode:prepublish": "npm run package", "vscode:prepublish": "npm run package",
@@ -60,15 +78,5 @@
"overrides": { "overrides": {
"diff": "^8.0.4", "diff": "^8.0.4",
"serialize-javascript": "^7.0.6" "serialize-javascript": "^7.0.6"
},
"configuration": {
"title": "Inference Coop",
"properties": {
"inferenceCoop.apiKey": {
"type": "string",
"default": "",
"description": "API key from the Inference Cooperative dashboard."
}
}
} }
} }
+179 -75
View File
@@ -1,107 +1,156 @@
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/'; const PREFIX = "inference-coop/";
const SECRET_KEY = "inferenceCoop.apiKey";
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 = await context.secrets.get(SECRET_KEY);
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 { const body = (await res.json()) as {
data: Array<{ id: string; mode?: string }>; data: Array<{ id: string; mode?: string }>;
}; };
const models = body.data const next: CoopModel[] = 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}`,
name: m.id, name: m.id,
family: 'inference-coop', family: "inference-coop",
version: '1.0.0', version: "1.0.0",
maxInputTokens: 131072, maxInputTokens: 131072,
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,
) {
out.appendLine(`provideLanguageModelChatResponse: ${model.id}`); out.appendLine(`provideLanguageModelChatResponse: ${model.id}`);
out.appendLine(` tools offered: ${options.tools?.length ?? 0}`); out.appendLine(` tools offered: ${options.tools?.length ?? 0}`);
const key = vscode.workspace const key = await context.secrets.get(SECRET_KEY);
.getConfiguration('inferenceCoop')
.get<string>('apiKey');
const body: Record<string, unknown> = { const body: Record<string, unknown> = {
model: model.id.replace(PREFIX, ''), model: model.id.replace(PREFIX, ""),
stream: true, stream: true,
messages: toOpenAIMessages(messages, out) messages: toOpenAIMessages(messages, out),
}; };
if (options.tools?.length) { if (options.tools?.length) {
body.tools = options.tools.map(t => ({ body.tools = options.tools.map((t) => ({
type: 'function', type: "function",
function: { function: {
name: t.name, name: t.name,
description: t.description, description: t.description,
parameters: t.inputSchema ?? { type: 'object', properties: {} } parameters: t.inputSchema ?? { type: "object", properties: {} },
} },
})); }));
body.tool_choice = 'auto'; body.tool_choice = "auto";
} }
const res = await fetch(`${BASE_URL}/chat/completions`, { const res = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST', method: "POST",
headers: { headers: {
Authorization: `Bearer ${key}`, Authorization: `Bearer ${key}`,
'Content-Type': 'application/json' "Content-Type": "application/json",
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
signal: token.isCancellationRequested ? AbortSignal.abort() : undefined signal: token.isCancellationRequested ? AbortSignal.abort() : undefined,
}); });
out.appendLine(` /chat/completions status: ${res.status}`); out.appendLine(` /chat/completions status: ${res.status}`);
if (!res.ok || !res.body) { if (!res.ok || !res.body) {
throw new Error(`Inference Coop: HTTP ${res.status} — ${await res.text()}`); throw new Error(
`Inference Coop: HTTP ${res.status} — ${await res.text()}`,
);
} }
const reader = res.body.getReader(); const reader = res.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buf = ''; let buf = "";
let textChunks = 0; let textChunks = 0;
const pending = new Map<number, { id?: string; name: string; args: string }>(); const pending = new Map<
number,
{ id?: string; name: string; args: string }
>();
const flushTools = () => { const flushTools = () => {
for (const call of pending.values()) { for (const call of pending.values()) {
let input: object = {}; let input: object = {};
try { try {
input = JSON.parse(call.args || '{}'); input = JSON.parse(call.args || "{}");
} catch { } catch {
out.appendLine(` WARN bad tool args for ${call.name}: ${call.args}`); out.appendLine(
` WARN bad tool args for ${call.name}: ${call.args}`,
);
} }
out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`); out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`);
progress.report( progress.report(
new vscode.LanguageModelToolCallPart( new vscode.LanguageModelToolCallPart(
call.id ?? `call_${Math.random().toString(36).slice(2)}`, call.id ?? `call_${Math.random().toString(36).slice(2)}`,
call.name, call.name,
input input,
) ),
); );
} }
pending.clear(); pending.clear();
@@ -109,17 +158,23 @@ export function activate(context: vscode.ExtensionContext) {
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) {break;} if (done) {
break;
}
buf += decoder.decode(value, { stream: true }); buf += decoder.decode(value, { stream: true });
const frames = buf.split('\n\n'); const frames = buf.split("\n\n");
buf = frames.pop() ?? ''; buf = frames.pop() ?? "";
for (const frame of frames) { for (const frame of frames) {
for (const line of frame.split('\n')) { for (const line of frame.split("\n")) {
if (!line.startsWith('data:')) {continue;} if (!line.startsWith("data:")) {
continue;
}
const data = line.slice(5).trim(); const data = line.slice(5).trim();
if (data === '[DONE]') { if (data === "[DONE]") {
if (pending.size) {flushTools();} if (pending.size) {
flushTools();
}
out.appendLine(` done, ${textChunks} text chunks`); out.appendLine(` done, ${textChunks} text chunks`);
return; return;
} }
@@ -131,52 +186,95 @@ export function activate(context: vscode.ExtensionContext) {
} }
const choice = json.choices?.[0]; const choice = json.choices?.[0];
const delta = choice?.delta; const delta = choice?.delta;
if (!delta) {continue;} if (!delta) {
continue;
}
if (typeof delta.content === 'string' && delta.content) { if (typeof delta.content === "string" && delta.content) {
textChunks++; textChunks++;
progress.report(new vscode.LanguageModelTextPart(delta.content)); progress.report(new vscode.LanguageModelTextPart(delta.content));
} }
for (const tc of delta.tool_calls ?? []) { for (const tc of delta.tool_calls ?? []) {
const i = tc.index ?? 0; const i = tc.index ?? 0;
const acc = pending.get(i) ?? { id: undefined, name: '', args: '' }; const acc = pending.get(i) ?? {
id: undefined,
name: "",
args: "",
};
acc.id = tc.id ?? acc.id; acc.id = tc.id ?? acc.id;
acc.name = tc.function?.name ?? acc.name; acc.name = tc.function?.name ?? acc.name;
acc.args += tc.function?.arguments ?? ''; acc.args += tc.function?.arguments ?? "";
pending.set(i, acc); pending.set(i, acc);
} }
if (choice?.finish_reason) { if (choice?.finish_reason) {
out.appendLine(` finish_reason: ${choice.finish_reason}`); out.appendLine(` finish_reason: ${choice.finish_reason}`);
if (pending.size) {flushTools();} if (pending.size) {
flushTools();
} }
} }
} }
} }
if (pending.size) {flushTools();} }
if (pending.size) {
flushTools();
}
out.appendLine(` stream ended, ${textChunks} text chunks`); out.appendLine(` stream ended, ${textChunks} text chunks`);
}, },
async provideTokenCount() { async provideTokenCount() {
return 0; return 0;
} },
}; };
context.subscriptions.push( context.subscriptions.push(
vscode.lm.registerLanguageModelChatProvider('inference-coop', provider) vscode.lm.registerLanguageModelChatProvider("inference-coop", provider),
); );
context.subscriptions.push( context.subscriptions.push(
vscode.commands.registerCommand('inference-coop-copilot.helloWorld', () => { vscode.commands.registerCommand("inference-coop-copilot.helloWorld", () => {
vscode.window.showInformationMessage('Inference Coop extension loaded.'); vscode.window.showInformationMessage("Inference Coop extension loaded.");
}) }),
);
context.subscriptions.push(
vscode.commands.registerCommand("inferenceCoop.setKey", async () => {
const key = await vscode.window.showInputBox({
title: "Inference Coop API key",
prompt: "Paste your key from the co-op dashboard.",
password: true,
ignoreFocusOut: true,
validateInput: (v) =>
v.trim().length < 16
? "That looks too short to be an API key."
: undefined,
});
if (!key) {return;}
await context.secrets.store(SECRET_KEY, key.trim());
cached = [];
lastFetch = 0;
await refresh(true);
vscode.window.showInformationMessage(
`Inference Coop: signed in, ${cached.length} models available.`,
);
}),
);
context.subscriptions.push(
vscode.commands.registerCommand("inferenceCoop.clearKey", async () => {
await context.secrets.delete(SECRET_KEY);
cached = [];
lastFetch = 0;
changed.fire();
vscode.window.showInformationMessage("Inference Coop: signed out.");
}),
); );
} }
function toOpenAIMessages( function toOpenAIMessages(
messages: readonly vscode.LanguageModelChatRequestMessage[], messages: readonly vscode.LanguageModelChatRequestMessage[],
out: vscode.OutputChannel out: vscode.OutputChannel,
): object[] { ): object[] {
const result: object[] = []; const result: object[] = [];
@@ -191,38 +289,44 @@ function toOpenAIMessages(
} else if (part instanceof vscode.LanguageModelToolCallPart) { } else if (part instanceof vscode.LanguageModelToolCallPart) {
toolCalls.push({ toolCalls.push({
id: part.callId, id: part.callId,
type: 'function', type: "function",
function: { name: part.name, arguments: JSON.stringify(part.input ?? {}) } function: {
name: part.name,
arguments: JSON.stringify(part.input ?? {}),
},
}); });
} else if (part instanceof vscode.LanguageModelToolResultPart) { } else if (part instanceof vscode.LanguageModelToolResultPart) {
toolResults.push({ toolResults.push({
role: 'tool', role: "tool",
tool_call_id: part.callId, tool_call_id: part.callId,
content: part.content content: part.content
.map((c: unknown) => .map((c: unknown) =>
c instanceof vscode.LanguageModelTextPart ? c.value : JSON.stringify(c) c instanceof vscode.LanguageModelTextPart
? c.value
: JSON.stringify(c),
) )
.join('\n') .join("\n"),
}); });
} }
} }
const role = const role =
msg.role === vscode.LanguageModelChatMessageRole.Assistant msg.role === vscode.LanguageModelChatMessageRole.Assistant
? 'assistant' ? "assistant"
: msg.role === vscode.LanguageModelChatMessageRole.User : msg.role === vscode.LanguageModelChatMessageRole.User
? 'user' ? "user"
: 'system'; : "system";
if (text.length || toolCalls.length) { if (text.length || toolCalls.length) {
const m: Record<string, unknown> = { const m: Record<string, unknown> = {
role, role,
content: text.length ? text.join('\n') : null content: text.length ? text.join("\n") : null,
}; };
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);
} }