VS Code gets the gateway's models from an extension the gateway serves
CI / test (push) Successful in 8m43s

llmgw's own extension for VS Code, plain JavaScript with no dependencies,
packed into a .vsix by the binary at /setup/vscode/llmgw.vsix with the
serving gateway's address in it, so it needs no build, no Node and no
marketplace. It registers llmgw as a language-model provider: the chat's
model picker, agent mode and any extension that asks VS Code for a model
get every chat model the key may use, sized and with tool and vision
capabilities as the catalog says, streamed, tool calls both ways.

VS Code is a tool on the account page's Connect your editor: a key, the
install command, and an Open in VS Code link that hands the key to the
extension's secret storage. The extension's Sign In opens the page with a
state of its own; a link without it is confirmed, the gateway named, since
it decides where code is sent.

Tested in VS Code 1.138 against the PoC: 27 models listed, an answer from
qwen2.5-0.5b-instruct, and qwen3-coder-30b calling a tool and using its
result (internal/vscodeext/e2e).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-24 01:39:50 +02:00
1 parent fea5774fe5
commit 38dfe268c9
17 files changed
+1412 -9

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
# llmgw for VS Code
The models your llmgw gateway serves, in VS Code: in the chat's model
picker, in agent mode, and in any extension that asks VS Code for a language
model. Nothing goes anywhere but the gateway you connect it to.
## Connecting
1. **llmgw: Sign In** from the command palette. It opens your gateway's
account page; sign in there and choose **Make a key and the
configuration** with *VS Code* as the tool. The page hands the key back to
VS Code.
2. Or **llmgw: Enter API Key** and paste a key from that page.
The gateway's address is the `llmgw.gatewayUrl` setting. An extension
downloaded from a gateway has that gateway's address in it already.
The key is kept in VS Code's secret storage. **llmgw: Sign Out** forgets it;
revoke it on the account page to stop it working.
## Which models
Every chat model the key may use. Agent mode acts only through tool calls,
so it needs a model the gateway's catalog says makes them; set
`llmgw.showModelsWithoutTools` to `false` to see only those.
Completion as you type is not part of what VS Code lets a model provider
offer; for that, use an extension such as Continue against the same gateway.
+253
View File
@@ -0,0 +1,253 @@
'use strict';
// llmgw for VS Code: the models a gateway serves, offered to VS Code as a
// language-model provider, so its chat and any extension that asks VS Code
// for a model can use them.
//
// The key lives in VS Code's secret storage, one per gateway, and is put
// there one of two ways: the gateway's account page makes one and hands it
// back through a vscode:// link, or the person pastes one.
const vscode = require('vscode');
const crypto = require('crypto');
const lib = require('./lib');
const VENDOR = 'llmgw';
const SIGN_IN_TTL_MS = 15 * 60 * 1000;
/** @param {vscode.ExtensionContext} context */
function activate(context) {
const changed = new vscode.EventEmitter();
const version = context.extension.packageJSON.version || '0.0.0';
const userAgent = `llmgw-vscode/${version} vscode/${vscode.version}`;
const gateway = () => lib.gatewayBase(vscode.workspace.getConfiguration('llmgw').get('gatewayUrl'));
const secretName = (base) => `llmgw.key:${base}`;
const keyFor = async (base) => (base ? context.secrets.get(secretName(base)) : undefined);
// The sign-in this window started: what the link back must carry to be
// taken without asking.
let pending = null;
async function askGateway() {
const now = gateway();
const typed = await vscode.window.showInputBox({
title: 'llmgw gateway',
prompt: 'The address of your llmgw gateway, such as https://llm.example.com',
value: now,
ignoreFocusOut: true,
validateInput: (v) => (lib.gatewayBase(v) ? undefined : 'An http:// or https:// address'),
});
if (typed === undefined) return '';
const base = lib.gatewayBase(typed);
if (base !== now) {
await vscode.workspace.getConfiguration('llmgw').update('gatewayUrl', base, vscode.ConfigurationTarget.Global);
}
return base;
}
async function signIn() {
const base = gateway() || (await askGateway());
if (!base) return;
pending = { state: crypto.randomBytes(18).toString('base64url'), base, at: Date.now() };
const page = vscode.Uri.parse(`${base}/profile?tool=vscode&state=${pending.state}#connect`);
const opened = await vscode.env.openExternal(page);
if (!opened) return;
vscode.window.showInformationMessage(
`Sign in on ${base} in your browser and choose "Make a key and the configuration". The page sends the key back to VS Code.`,
);
}
async function enterKey() {
const base = gateway() || (await askGateway());
if (!base) return;
const key = await vscode.window.showInputBox({
title: `llmgw key for ${base}`,
prompt: 'A key from your account page on the gateway, starting sk-llmgw-',
password: true,
ignoreFocusOut: true,
validateInput: (v) => (v.trim() ? undefined : 'The key'),
});
if (!key) return;
await storeKey(base, key.trim());
}
async function storeKey(base, key) {
// Tried before it is kept: a wrong key or a gateway that is not there is
// better said now than as an empty model list later.
try {
await listModels(base, key);
} catch (err) {
vscode.window.showErrorMessage(String(err && err.message ? err.message : err));
return;
}
await context.secrets.store(secretName(base), key);
changed.fire();
vscode.window.showInformationMessage(`llmgw: connected to ${base}. Its models are in the chat's model picker.`);
}
async function signOut() {
const base = gateway();
if (!base) return;
await context.secrets.delete(secretName(base));
changed.fire();
vscode.window.showInformationMessage(
`llmgw: signed out of ${base}. The key still works until it is revoked on the gateway's account page.`,
);
}
// The account page's "Open in VS Code" link: vscode://llmgw.llmgw/connect
// with the gateway, the key it just made, and the state this window sent.
// A link from anywhere else is asked about first: a page that could hand
// VS Code a key of its own choosing would also choose where code is sent.
async function handleUri(uri) {
if (uri.path !== '/connect') return;
const q = new URLSearchParams(uri.query);
const base = lib.gatewayBase(q.get('gateway'));
const key = (q.get('key') || '').trim();
if (!base || !key) {
vscode.window.showErrorMessage('llmgw: the link has no gateway or no key in it.');
return;
}
const expected =
pending && pending.base === base && q.get('state') === pending.state && Date.now() - pending.at < SIGN_IN_TTL_MS;
pending = null;
if (!expected) {
const yes = 'Connect';
const choice = await vscode.window.showWarningMessage(
`Connect VS Code to the llmgw gateway at ${base}?`,
{
modal: true,
detail: 'What you ask its models, and the code they are given, will go to that address. Connect only if you just made this key on its account page.',
},
yes,
);
if (choice !== yes) return;
}
if (base !== gateway()) {
await vscode.workspace.getConfiguration('llmgw').update('gatewayUrl', base, vscode.ConfigurationTarget.Global);
}
await storeKey(base, key);
}
async function call(base, key, path, init) {
const res = await fetch(base + path, {
...init,
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
'User-Agent': userAgent,
...(init && init.headers),
},
});
if (!res.ok) throw new Error(lib.errorMessage(res.status, await res.text()));
return res;
}
async function listModels(base, key) {
let res;
try {
res = await call(base, key, '/v1/models', { method: 'GET', signal: AbortSignal.timeout(15000) });
} catch (err) {
if (err && err.message && err.message.startsWith('llmgw:')) throw err;
throw new Error(`llmgw: could not reach ${base} (${err && err.cause ? err.cause.message || err.cause : err}).`);
}
const body = await res.json();
return Array.isArray(body.data) ? body.data : [];
}
const provider = {
onDidChangeLanguageModelChatInformation: changed.event,
async provideLanguageModelChatInformation(options) {
const base = gateway();
const key = await keyFor(base);
if (!base || !key) {
// Offered, not waited for: VS Code is waiting on this list, and a
// notification can sit unanswered for as long as the person likes.
// Signing in fires the change event, and VS Code asks again.
if (!options.silent) {
vscode.window
.showInformationMessage('llmgw: sign in to a gateway to use its models.', 'Sign In', 'Enter API Key')
.then((pick) => {
if (pick === 'Sign In') return signIn();
if (pick === 'Enter API Key') return enterKey();
});
}
return [];
}
try {
const show = vscode.workspace.getConfiguration('llmgw').get('showModelsWithoutTools', true);
return (await listModels(base, key)).map((e) => lib.modelInfo(e, show)).filter(Boolean);
} catch (err) {
if (!options.silent) vscode.window.showErrorMessage(String(err.message || err));
return [];
}
},
async provideLanguageModelChatResponse(model, messages, options, progress, token) {
const base = gateway();
const key = await keyFor(base);
if (!base || !key) throw new Error('llmgw: not signed in. Run "llmgw: Sign In".');
const abort = new AbortController();
const sub = token.onCancellationRequested(() => abort.abort());
try {
const res = await call(base, key, '/v1/chat/completions', {
method: 'POST',
body: JSON.stringify(lib.requestBody(model, messages, options)),
signal: abort.signal,
});
const answer = new lib.Answer();
for await (const data of lib.sseData(res.body)) {
const text = answer.add(data);
if (text) progress.report(new vscode.LanguageModelTextPart(text));
}
for (const c of answer.toolCalls()) {
progress.report(new vscode.LanguageModelToolCallPart(c.id, c.name, c.input));
}
} catch (err) {
if (token.isCancellationRequested) return;
throw err;
} finally {
sub.dispose();
}
},
async provideTokenCount(_model, text) {
if (typeof text === 'string') return lib.tokenGuess(text);
const parts = (text && text.content) || [];
return parts.reduce((n, p) => n + lib.tokenGuess(p && typeof p.value === 'string' ? p.value : JSON.stringify(p)), 0);
},
};
context.subscriptions.push(
changed,
vscode.lm.registerLanguageModelChatProvider(VENDOR, provider),
vscode.window.registerUriHandler({ handleUri }),
vscode.commands.registerCommand('llmgw.signIn', signIn),
vscode.commands.registerCommand('llmgw.enterKey', enterKey),
vscode.commands.registerCommand('llmgw.signOut', signOut),
vscode.commands.registerCommand('llmgw.refresh', () => changed.fire()),
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('llmgw')) changed.fire();
}),
);
// For another extension, or a test, that has a gateway and a key in hand:
// the same as the account page's link, without the question, since the
// caller already runs with every right VS Code gives an extension.
return {
async connect(gatewayUrl, key) {
const base = lib.gatewayBase(gatewayUrl);
if (!base || !key) throw new Error('llmgw: connect needs a gateway address and a key');
if (base !== gateway()) {
await vscode.workspace.getConfiguration('llmgw').update('gatewayUrl', base, vscode.ConfigurationTarget.Global);
}
await storeKey(base, String(key).trim());
},
};
}
function deactivate() {}
module.exports = { activate, deactivate };
+269
View File
@@ -0,0 +1,269 @@
'use strict';
// What the extension does that does not need VS Code: reading the gateway's
// model list, turning VS Code's messages into chat-completion messages, and
// reading the streamed answer back. Kept apart so it can be tested with plain
// Node, against a gateway, without an editor running.
const USER = 1;
const ASSISTANT = 2;
// Not in every VS Code that runs this: a system message, where it exists.
const SYSTEM = 3;
// gatewayBase is the address the person typed, as the API is under it: no
// trailing slash, and no /v1, which people copy from client guides.
function gatewayBase(raw) {
let s = String(raw || '').trim();
if (!s) return '';
// A bare host is https; any other scheme than http(s) is not a gateway.
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(s)) {
if (!/^https?:\/\//i.test(s)) return '';
} else {
s = 'https://' + s;
}
let u;
try {
u = new URL(s);
} catch {
return '';
}
if (u.protocol !== 'https:' && u.protocol !== 'http:') return '';
let path = u.pathname.replace(/\/+$/, '');
if (path.endsWith('/v1')) path = path.slice(0, -3);
return u.origin + path;
}
// modelInfo is what VS Code is told of one entry of /v1/models, or null for
// one that does not chat: an embedding model, or one that makes pictures,
// meshes or anything else through a workflow.
//
// The sizes are the catalog's when it gives them. VS Code trims a
// conversation to maxInputTokens, and a request over what a replica holds is
// refused by the gateway, so guessing high is worse than guessing low.
function modelInfo(entry, showWithoutTools) {
if (!entry || typeof entry.id !== 'string' || !entry.id) return null;
const caps = entry.capabilities || {};
if (caps.embeddings || caps.images || caps.meshes || caps.workflows) return null;
const tools = !!caps.tools;
if (!tools && !showWithoutTools) return null;
const context = Number(entry.context_length) > 0 ? Number(entry.context_length) : 8192;
let output = Number(entry.max_output_tokens) > 0 ? Number(entry.max_output_tokens) : 0;
output = Math.min(output || 4096, Math.max(256, Math.floor(context / 4)));
const detail = entry.alias_of ? `llmgw · ${entry.alias_of}` : 'llmgw';
return {
id: entry.id,
name: entry.id,
family: entry.alias_of || entry.id,
version: 'llmgw',
detail,
tooltip: entry.description || undefined,
maxInputTokens: Math.max(256, context - output),
maxOutputTokens: output,
capabilities: { toolCalling: tools, imageInput: !!caps.vision },
};
}
function isToolCall(p) {
return p && typeof p.callId === 'string' && typeof p.name === 'string' && 'input' in p;
}
function isToolResult(p) {
return p && typeof p.callId === 'string' && Array.isArray(p.content);
}
function isText(p) {
return p && typeof p.value === 'string' && !isToolCall(p) && !isToolResult(p);
}
function isImage(p) {
return p && typeof p.mimeType === 'string' && p.mimeType.startsWith('image/') && p.data instanceof Uint8Array;
}
// resultText flattens a tool result to the text a chat-completion tool
// message carries. Parts that are not text (prompt-tsx trees) go as JSON.
function resultText(content) {
return content
.map((c) => (isText(c) ? c.value : typeof c === 'string' ? c : JSON.stringify(c)))
.join('');
}
// chatMessages turns VS Code's request messages into chat-completion ones.
//
// One VS Code message can hold several kinds of part; the API they are going
// to wants each in its own message: an assistant's tool calls in one, each
// tool result in a tool message straight after it, and only then the text a
// person wrote beside the results.
function chatMessages(messages, vision) {
const out = [];
for (const m of messages) {
const parts = Array.isArray(m.content) ? m.content : [];
if (m.role === ASSISTANT) {
const text = parts.filter(isText).map((p) => p.value).join('');
const calls = parts.filter(isToolCall).map((p) => ({
id: p.callId,
type: 'function',
function: { name: p.name, arguments: JSON.stringify(p.input ?? {}) },
}));
const msg = { role: 'assistant', content: text || (calls.length ? null : '') };
if (calls.length) msg.tool_calls = calls;
out.push(msg);
continue;
}
for (const p of parts.filter(isToolResult)) {
out.push({ role: 'tool', tool_call_id: p.callId, content: resultText(p.content) });
}
const texts = parts.filter(isText).map((p) => p.value);
const images = vision ? parts.filter(isImage) : [];
if (!texts.length && !images.length) continue;
const role = m.role === SYSTEM ? 'system' : 'user';
if (!images.length) {
out.push({ role, content: texts.join('') });
continue;
}
const content = [];
if (texts.length) content.push({ type: 'text', text: texts.join('') });
for (const img of images) {
const b64 = Buffer.from(img.data).toString('base64');
content.push({ type: 'image_url', image_url: { url: `data:${img.mimeType};base64,${b64}` } });
}
out.push({ role, content });
}
return out;
}
// chatTools turns VS Code's tool list into chat-completion functions. A tool
// without a schema still needs one: an object that takes nothing.
function chatTools(tools) {
return (tools || []).map((t) => ({
type: 'function',
function: {
name: t.name,
description: t.description || '',
parameters: t.inputSchema && Object.keys(t.inputSchema).length ? t.inputSchema : { type: 'object', properties: {} },
},
}));
}
// requestBody is the whole chat-completion request. toolMode 2 is VS Code's
// "Required": the model must call one of the tools.
function requestBody(model, messages, options) {
const body = {
model: model.id,
messages: chatMessages(messages, !!(model.capabilities && model.capabilities.imageInput)),
stream: true,
max_tokens: model.maxOutputTokens,
};
const tools = chatTools(options && options.tools);
if (tools.length) {
body.tools = tools;
if (options.toolMode === 2) body.tool_choice = 'required';
}
const mo = (options && options.modelOptions) || {};
for (const k of ['temperature', 'top_p', 'stop', 'seed']) {
if (mo[k] !== undefined) body[k] = mo[k];
}
return body;
}
// sseData yields the data of each server-sent event in a byte stream, as the
// chat-completion stream sends them: "data: {...}" lines, ending "[DONE]".
async function* sseData(stream) {
const decoder = new TextDecoder();
let buf = '';
for await (const chunk of stream) {
buf += decoder.decode(chunk, { stream: true });
let i;
while ((i = buf.indexOf('\n')) >= 0) {
const line = buf.slice(0, i).replace(/\r$/, '');
buf = buf.slice(i + 1);
if (line.startsWith('data:')) yield line.slice(5).trimStart();
}
}
buf += decoder.decode();
if (buf.startsWith('data:')) yield buf.slice(5).trimStart();
}
// Answer collects one streamed answer: its text as it comes, and its tool
// calls, whose arguments arrive in pieces and are only whole at the end.
class Answer {
constructor() {
this.calls = new Map();
this.usage = null;
}
// add takes one event's data and returns the text it carries, if any.
add(data) {
if (data === '[DONE]') return '';
let ev;
try {
ev = JSON.parse(data);
} catch {
return '';
}
if (ev.error) {
const msg = typeof ev.error === 'string' ? ev.error : ev.error.message || JSON.stringify(ev.error);
throw new Error(msg);
}
if (ev.usage) this.usage = ev.usage;
let text = '';
for (const choice of ev.choices || []) {
const d = choice.delta || {};
if (typeof d.content === 'string') text += d.content;
for (const tc of d.tool_calls || []) {
const key = tc.index ?? tc.id ?? this.calls.size;
const c = this.calls.get(key) || { id: '', name: '', args: '' };
if (tc.id) c.id = tc.id;
if (tc.function && tc.function.name) c.name += tc.function.name;
if (tc.function && typeof tc.function.arguments === 'string') c.args += tc.function.arguments;
this.calls.set(key, c);
}
}
return text;
}
// toolCalls is every call the answer made, its arguments parsed. A call
// whose arguments are not JSON goes with them as text, under "input", so
// the tool sees what the model wrote rather than nothing.
toolCalls() {
const out = [];
let n = 0;
for (const c of this.calls.values()) {
n++;
if (!c.name) continue;
let input = {};
if (c.args.trim()) {
try {
input = JSON.parse(c.args);
} catch {
input = { input: c.args };
}
}
out.push({ id: c.id || `call_${n}`, name: c.name, input });
}
return out;
}
}
// errorMessage reads what a gateway said when it refused a request.
function errorMessage(status, bodyText) {
let msg = '';
try {
const j = JSON.parse(bodyText);
const e = j.error;
msg = (typeof e === 'string' ? e : e && e.message) || j.message || '';
} catch {
msg = bodyText.trim().slice(0, 300);
}
if (status === 401) return 'llmgw: the gateway did not accept the key. Run "llmgw: Sign In" to make a new one.';
if (status === 403) return `llmgw: this key may not use that model${msg ? ` (${msg})` : ''}.`;
if (status === 429) return `llmgw: a limit was reached${msg ? `: ${msg}` : ''}.`;
return `llmgw: the gateway answered ${status}${msg ? `: ${msg}` : ''}.`;
}
// tokenGuess is how many tokens a text is, near enough for VS Code to decide
// what fits: about four characters to a token in English and in code.
function tokenGuess(text) {
return Math.ceil(String(text || '').length / 4);
}
module.exports = {
USER, ASSISTANT, SYSTEM,
gatewayBase, modelInfo, chatMessages, chatTools, requestBody, sseData, Answer, errorMessage, tokenGuess,
};
+123
View File
@@ -0,0 +1,123 @@
'use strict';
// node --test internal/vscodeext/ext/lib_test.js
// (VS Code's own runtime works too:
// ELECTRON_RUN_AS_NODE=1 "/Applications/Visual Studio Code.app/Contents/MacOS/Electron" --test …)
const test = require('node:test');
const assert = require('node:assert/strict');
const lib = require('./lib');
// VS Code's parts, as the provider receives them: classes in VS Code,
// told apart here by their fields.
const text = (value) => ({ value });
const call = (callId, name, input) => ({ callId, name, input });
const result = (callId, ...values) => ({ callId, content: values.map(text) });
test('gatewayBase takes what people paste', () => {
assert.equal(lib.gatewayBase('https://llm.example.com/'), 'https://llm.example.com');
assert.equal(lib.gatewayBase('https://llm.example.com/v1'), 'https://llm.example.com');
assert.equal(lib.gatewayBase('llm.example.com'), 'https://llm.example.com');
assert.equal(lib.gatewayBase('http://10.0.0.140:8080/v1/'), 'http://10.0.0.140:8080');
assert.equal(lib.gatewayBase('ftp://x'), '');
assert.equal(lib.gatewayBase(''), '');
});
test('modelInfo keeps chat models and says what the catalog says', () => {
const m = lib.modelInfo({
id: 'coder', alias_of: 'qwen3-coder-30b', context_length: 32768, max_output_tokens: 8192,
capabilities: { tools: true, vision: false },
}, true);
assert.equal(m.id, 'coder');
assert.equal(m.family, 'qwen3-coder-30b');
assert.equal(m.maxOutputTokens, 8192);
assert.equal(m.maxInputTokens, 32768 - 8192);
assert.deepEqual(m.capabilities, { toolCalling: true, imageInput: false });
assert.equal(lib.modelInfo({ id: 'bge-m3', capabilities: { embeddings: true } }, true), null);
assert.equal(lib.modelInfo({ id: 'flux', capabilities: { images: true } }, true), null);
assert.equal(lib.modelInfo({ id: 'sf3d', capabilities: { meshes: true, workflows: true } }, true), null);
assert.equal(lib.modelInfo({ id: 'plain', capabilities: {} }, false), null);
const small = lib.modelInfo({ id: 'small', context_length: 4096, capabilities: {} }, true);
assert.equal(small.maxOutputTokens, 1024, 'a quarter of a small context, not 4096 of it');
});
test('chatMessages puts tool calls and results where chat completions want them', () => {
const msgs = lib.chatMessages([
{ role: lib.USER, content: [text('Read main.go')] },
{ role: lib.ASSISTANT, content: [text('Reading it.'), call('c1', 'read_file', { path: 'main.go' })] },
{ role: lib.USER, content: [result('c1', 'package main'), text('Now summarise it.')] },
], false);
assert.deepEqual(msgs, [
{ role: 'user', content: 'Read main.go' },
{
role: 'assistant', content: 'Reading it.',
tool_calls: [{ id: 'c1', type: 'function', function: { name: 'read_file', arguments: '{"path":"main.go"}' } }],
},
{ role: 'tool', tool_call_id: 'c1', content: 'package main' },
{ role: 'user', content: 'Now summarise it.' },
]);
});
test('an assistant message of calls alone has null content', () => {
const [m] = lib.chatMessages([{ role: lib.ASSISTANT, content: [call('c1', 'ls', {})] }], false);
assert.equal(m.content, null);
assert.equal(m.tool_calls.length, 1);
});
test('images go as data URLs only to a model that takes them', () => {
const img = { mimeType: 'image/png', data: new Uint8Array([1, 2, 3]) };
const [withVision] = lib.chatMessages([{ role: lib.USER, content: [text('what is this'), img] }], true);
assert.deepEqual(withVision.content[1], { type: 'image_url', image_url: { url: 'data:image/png;base64,AQID' } });
const [without] = lib.chatMessages([{ role: lib.USER, content: [text('what is this'), img] }], false);
assert.equal(without.content, 'what is this');
});
test('requestBody asks for tools as functions, and requires one when VS Code does', () => {
const model = { id: 'coder', maxOutputTokens: 2048, capabilities: { imageInput: false } };
const body = lib.requestBody(model, [{ role: lib.USER, content: [text('hi')] }], {
tools: [{ name: 'ls', description: 'list', inputSchema: undefined }], toolMode: 2, modelOptions: { temperature: 0.2 },
});
assert.equal(body.stream, true);
assert.equal(body.max_tokens, 2048);
assert.equal(body.tool_choice, 'required');
assert.equal(body.temperature, 0.2);
assert.deepEqual(body.tools[0].function.parameters, { type: 'object', properties: {} });
});
async function* chunks(...parts) {
for (const p of parts) yield new TextEncoder().encode(p);
}
test('sseData reads events split anywhere', async () => {
const got = [];
for await (const d of lib.sseData(chunks('data: {"a":', '1}\r\n\ndata: [DO', 'NE]\n'))) got.push(d);
assert.deepEqual(got, ['{"a":1}', '[DONE]']);
});
test('Answer joins text and tool-call arguments that arrive in pieces', () => {
const a = new lib.Answer();
const ev = (delta) => JSON.stringify({ choices: [{ delta }] });
assert.equal(a.add(ev({ content: 'Hel' })), 'Hel');
assert.equal(a.add(ev({ content: 'lo' })), 'lo');
a.add(ev({ tool_calls: [{ index: 0, id: 'call_a', function: { name: 'read_file', arguments: '{"pa' } }] }));
a.add(ev({ tool_calls: [{ index: 0, function: { arguments: 'th":"x"}' } }] }));
a.add(ev({ tool_calls: [{ index: 1, id: 'call_b', function: { name: 'ls', arguments: '' } }] }));
assert.equal(a.add('[DONE]'), '');
assert.deepEqual(a.toolCalls(), [
{ id: 'call_a', name: 'read_file', input: { path: 'x' } },
{ id: 'call_b', name: 'ls', input: {} },
]);
});
test('Answer passes on an error the stream carries', () => {
const a = new lib.Answer();
assert.throws(() => a.add(JSON.stringify({ error: { message: 'budget spent' } })), /budget spent/);
});
test('errorMessage says what to do about a refused key', () => {
assert.match(lib.errorMessage(401, '{"error":{"message":"invalid key"}}'), /Sign In/);
assert.match(lib.errorMessage(429, '{"error":{"message":"rate limit"}}'), /rate limit/);
assert.match(lib.errorMessage(500, 'boom'), /500: boom/);
});
+46
View File
@@ -0,0 +1,46 @@
{
"name": "llmgw",
"publisher": "llmgw",
"displayName": "llmgw",
"description": "The models your llmgw gateway serves, in VS Code's chat and in every extension that asks VS Code for a language model.",
"version": "0.0.0",
"license": "UNLICENSED",
"engines": {
"vscode": "^1.104.0"
},
"categories": ["AI", "Chat"],
"keywords": ["llmgw", "llm", "gateway", "self-hosted", "on-premises"],
"main": "./extension.js",
"extensionKind": ["ui", "workspace"],
"activationEvents": ["onStartupFinished", "onUri"],
"capabilities": {
"untrustedWorkspaces": { "supported": true },
"virtualWorkspaces": true
},
"contributes": {
"languageModelChatProviders": [
{ "vendor": "llmgw", "displayName": "llmgw" }
],
"commands": [
{ "command": "llmgw.signIn", "title": "Sign In", "category": "llmgw" },
{ "command": "llmgw.enterKey", "title": "Enter API Key", "category": "llmgw" },
{ "command": "llmgw.signOut", "title": "Sign Out", "category": "llmgw" },
{ "command": "llmgw.refresh", "title": "Refresh Models", "category": "llmgw" }
],
"configuration": {
"title": "llmgw",
"properties": {
"llmgw.gatewayUrl": {
"type": "string",
"default": "",
"markdownDescription": "The gateway's address, such as `https://llm.example.com`: the one the people running it gave you, without `/v1`."
},
"llmgw.showModelsWithoutTools": {
"type": "boolean",
"default": true,
"markdownDescription": "Offer models whose tool calls the gateway does not vouch for. They answer in chat, but an agent needs a model that makes tool calls to read a file or run a command."
}
}
}
}
}