Compare commits

...
2 Commits
Author SHA1 Message Date
evanp 76da3ac01e chore: package version 0.1.0 2026-11-06 13:05:44 -05:00
evanp ce03d6d902 feat: tool handling 2026-11-06 12:59:34 -05:00
3 changed files with 130 additions and 36 deletions

No files matched your search

+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "inference-coop-copilot", "name": "inference-coop-copilot",
"version": "0.0.1", "version": "0.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "inference-coop-copilot", "name": "inference-coop-copilot",
"version": "0.0.1", "version": "0.1.0",
"devDependencies": { "devDependencies": {
"@types/mocha": "^10.0.10", "@types/mocha": "^10.0.10",
"@types/node": "24.x", "@types/node": "24.x",
+2 -2
View File
@@ -2,7 +2,7 @@
"name": "inference-coop-copilot", "name": "inference-coop-copilot",
"displayName": "inference-coop-copilot", "displayName": "inference-coop-copilot",
"description": "Inference.coop models for Copilot Chat (unofficial)", "description": "Inference.coop models for Copilot Chat (unofficial)",
"version": "0.0.1", "version": "0.1.0",
"engines": { "engines": {
"vscode": "^1.145.0" "vscode": "^1.145.0"
}, },
@@ -71,4 +71,4 @@
} }
} }
} }
} }
+126 -32
View File
@@ -15,23 +15,17 @@ export function activate(context: vscode.ExtensionContext) {
const key = vscode.workspace const key = vscode.workspace
.getConfiguration('inferenceCoop') .getConfiguration('inferenceCoop')
.get<string>('apiKey'); .get<string>('apiKey');
out.appendLine(` key present: ${Boolean(key)}`);
if (!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}`); out.appendLine(` /models status: ${res.status}`);
if (!res.ok) { if (!res.ok) {return [];}
out.appendLine(` body: ${await res.text()}`);
return [];
}
const body = (await res.json()) as { const body = (await res.json()) as {
data: Array<{ id: string; mode?: string }>; data: Array<{ id: string; mode?: string }>;
}; };
out.appendLine(` raw models: ${body.data.length}`);
const models = body.data const models = body.data
.filter(m => !m.mode || m.mode === 'chat') .filter(m => !m.mode || m.mode === 'chat')
.map(m => ({ .map(m => ({
@@ -43,36 +37,36 @@ 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`); out.appendLine(` returning ${models.length} models`);
return models; return models;
}, },
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}`);
const key = vscode.workspace const key = vscode.workspace
.getConfiguration('inferenceCoop') .getConfiguration('inferenceCoop')
.get<string>('apiKey'); .get<string>('apiKey');
const body = { const body: Record<string, unknown> = {
model: model.id.replace(PREFIX, ''), model: model.id.replace(PREFIX, ''),
stream: true, stream: true,
messages: messages.map(m => ({ messages: toOpenAIMessages(messages, out)
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')
}))
}; };
if (options.tools?.length) {
body.tools = options.tools.map(t => ({
type: 'function',
function: {
name: t.name,
description: t.description,
parameters: t.inputSchema ?? { type: 'object', properties: {} }
}
}));
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: {
@@ -90,34 +84,78 @@ export function activate(context: vscode.ExtensionContext) {
const reader = res.body.getReader(); const reader = res.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buf = ''; let buf = '';
let chunks = 0; let textChunks = 0;
const pending = new Map<number, { id?: string; name: string; args: string }>();
const flushTools = () => {
for (const call of pending.values()) {
let input: object = {};
try {
input = JSON.parse(call.args || '{}');
} catch {
out.appendLine(` WARN bad tool args for ${call.name}: ${call.args}`);
}
out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`);
progress.report(
new vscode.LanguageModelToolCallPart(
call.id ?? `call_${Math.random().toString(36).slice(2)}`,
call.name,
input
)
);
}
pending.clear();
};
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]') {
out.appendLine(` done, ${chunks} text chunks`); if (pending.size) {flushTools();}
out.appendLine(` done, ${textChunks} text chunks`);
return; return;
} }
let json: any;
try { try {
const delta = JSON.parse(data).choices?.[0]?.delta?.content; json = JSON.parse(data);
if (delta) {
chunks++;
progress.report(new vscode.LanguageModelTextPart(delta));
}
} catch { } catch {
/* partial frame, ignore */ continue;
}
const choice = json.choices?.[0];
const delta = choice?.delta;
if (!delta) {continue;}
if (typeof delta.content === 'string' && delta.content) {
textChunks++;
progress.report(new vscode.LanguageModelTextPart(delta.content));
}
for (const tc of delta.tool_calls ?? []) {
const i = tc.index ?? 0;
const acc = pending.get(i) ?? { id: undefined, name: '', args: '' };
acc.id = tc.id ?? acc.id;
acc.name = tc.function?.name ?? acc.name;
acc.args += tc.function?.arguments ?? '';
pending.set(i, acc);
}
if (choice?.finish_reason) {
out.appendLine(` finish_reason: ${choice.finish_reason}`);
if (pending.size) {flushTools();}
} }
} }
} }
} }
out.appendLine(` stream ended, ${chunks} text chunks`); if (pending.size) {flushTools();}
out.appendLine(` stream ended, ${textChunks} text chunks`);
}, },
async provideTokenCount() { async provideTokenCount() {
@@ -136,4 +174,60 @@ export function activate(context: vscode.ExtensionContext) {
); );
} }
function toOpenAIMessages(
messages: readonly vscode.LanguageModelChatRequestMessage[],
out: vscode.OutputChannel
): object[] {
const result: object[] = [];
for (const msg of messages) {
const text: string[] = [];
const toolCalls: object[] = [];
const toolResults: object[] = [];
for (const part of msg.content ?? []) {
if (part instanceof vscode.LanguageModelTextPart) {
text.push(part.value);
} else if (part instanceof vscode.LanguageModelToolCallPart) {
toolCalls.push({
id: part.callId,
type: 'function',
function: { name: part.name, arguments: JSON.stringify(part.input ?? {}) }
});
} else if (part instanceof vscode.LanguageModelToolResultPart) {
toolResults.push({
role: 'tool',
tool_call_id: part.callId,
content: part.content
.map((c: unknown) =>
c instanceof vscode.LanguageModelTextPart ? c.value : JSON.stringify(c)
)
.join('\n')
});
}
}
const role =
msg.role === vscode.LanguageModelChatMessageRole.Assistant
? 'assistant'
: msg.role === vscode.LanguageModelChatMessageRole.User
? 'user'
: 'system';
if (text.length || toolCalls.length) {
const m: Record<string, unknown> = {
role,
content: text.length ? text.join('\n') : null
};
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);
}
out.appendLine(` translated ${result.length} messages`);
return result;
}
export function deactivate() {} export function deactivate() {}