forked from evanp/inference-coop-copilot
Compare commits
11
Commits
v0.1.0
..
4e50c28def
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e50c28def | ||
|
|
8f1d71d913 | ||
|
|
e9a060af0f | ||
|
|
46b8d7e888 | ||
|
|
0460305327 | ||
|
|
c4bb02a7a2 | ||
|
|
f295cb8535 | ||
|
|
55e2764480 | ||
|
|
cb23c502aa | ||
|
|
4eeace6ce3 | ||
|
|
713a1c1d75 |
No files matched your search
+59
-12
@@ -1,30 +1,77 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [0.0.1] - 2026-11-06
|
<!-- markdownlint-configure-file { "MD024": { "siblings_only": true } } -->
|
||||||
|
|
||||||
Initial development release. Not yet functional for chat.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||||
|
and this project adheres to
|
||||||
|
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.2.2] - 2026-11-06
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Correct the publisher in package.json.
|
||||||
|
|
||||||
|
## [0.2.1] - 2026-11-06
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Correct instructions for setting the API key in README.
|
||||||
|
|
||||||
|
## [0.2.0] - 2026-11-06
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- "Inference Coop: Set API Key" and "Inference Coop: Sign Out" commands.
|
||||||
|
- Model list caching with `onDidChangeLanguageModelChatInformation`, so the
|
||||||
|
picker refreshes when models change instead of requiring a restart.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- **Breaking:** the API key is now stored in VS Code `SecretStorage` instead
|
||||||
|
of the `inferenceCoop.apiKey` setting. Run "Inference Coop: Set API Key"
|
||||||
|
once after upgrading; the gateway base URL is now the only setting.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Models intermittently missing from the Copilot Chat model picker when the
|
||||||
|
provider's initial model fetch lost a race with chat activation.
|
||||||
|
|
||||||
|
## [0.1.0] - 2026-11-06
|
||||||
|
|
||||||
|
First release usable for chat.
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Streaming chat responses via `provideLanguageModelChatResponse`, with
|
||||||
|
text streamed to the UI as it arrives.
|
||||||
|
- Tool calling: tool schemas are sent to the gateway, streamed tool-call
|
||||||
|
fragments are reassembled, and tool results are translated back into the
|
||||||
|
conversation. Verified end to end in agent mode against
|
||||||
|
`tinfoil/gpt-oss-120b`.
|
||||||
- Extension scaffold with esbuild build pipeline and ESLint/tsc checks.
|
- Extension scaffold with esbuild build pipeline and ESLint/tsc checks.
|
||||||
- Registration of a `LanguageModelChatProvider` under the `inference-coop`
|
- Registration of a `LanguageModelChatProvider` under the `inference-coop`
|
||||||
vendor, discoverable via `contributes.languageModelChatProviders`.
|
vendor, discoverable via `contributes.languageModelChatProviders`.
|
||||||
- Model discovery: fetches `GET https://gateway.inference.coop/v1/models`
|
- Model discovery: fetches `GET https://gateway.inference.coop/v1/models`
|
||||||
and filters to chat-capable models, excluding image-generation and
|
and filters to chat-capable models, excluding image-generation and
|
||||||
audio endpoints.
|
audio endpoints.
|
||||||
- `inferenceCoop.apiKey` setting (plain text, temporarily).
|
|
||||||
- "Inference Coop" output channel logging provider activation, model
|
- "Inference Coop" output channel logging provider activation, model
|
||||||
enumeration, and HTTP status codes.
|
enumeration, and HTTP status codes.
|
||||||
|
|
||||||
### Known issues
|
### Changed
|
||||||
|
|
||||||
- Models appear in the Language Models panel but not in the Copilot Chat
|
- Adopted the Apache License 2.0.
|
||||||
model picker. Under investigation; suspected upstream VS Code issue.
|
|
||||||
- `provideLanguageModelChatResponse` sends a streaming request but has not
|
|
||||||
been verified end to end from the chat UI.
|
|
||||||
- No tool calling, image input, or token counting (returns 0).
|
|
||||||
- API key is stored in settings rather than `SecretStorage`.
|
|
||||||
|
|
||||||
### Not yet published
|
## [0.0.1] - 2026-11-06
|
||||||
|
|
||||||
- No Marketplace publisher registered; installed from a local `.vsix`.
|
Initial development release. Not functional for chat.
|
||||||
|
|
||||||
|
[Unreleased]: https://git.inference.coop/evanp/inference-coop-copilot/compare/v0.2.2...HEAD
|
||||||
|
[0.2.2]: https://git.inference.coop/evanp/inference-coop-copilot/compare/v0.2.1...v0.2.2
|
||||||
|
[0.2.1]: https://git.inference.coop/evanp/inference-coop-copilot/compare/v0.2.0...v0.2.1
|
||||||
|
[0.2.0]: https://git.inference.coop/evanp/inference-coop-copilot/compare/v0.1.0...v0.2.0
|
||||||
|
[0.1.0]: https://git.inference.coop/evanp/inference-coop-copilot/releases/tag/v0.1.0
|
||||||
@@ -1,14 +1,37 @@
|
|||||||
# Inference Coop (unofficial)
|
# Inference Coop for Copilot Chat
|
||||||
|
|
||||||
Community-built model provider for [inference.coop](https://inference.coop),
|
Official model provider for [inference.coop](https://inference.coop), usable
|
||||||
usable in GitHub Copilot Chat. Not affiliated with or endorsed by the
|
in GitHub Copilot Chat. An official project of the Inference Cooperative,
|
||||||
Inference Cooperative.
|
maintained in the co-op's Git at
|
||||||
|
[code/inference-coop-copilot](https://git.inference.coop/code/inference-coop-copilot).
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- VS Code 1.145 or newer
|
||||||
|
- GitHub Copilot Chat, installed and signed in
|
||||||
|
- A co-op membership and API key
|
||||||
|
|
||||||
|
Copilot Business/Enterprise: your administrator must leave "Bring Your Own
|
||||||
|
Language Model Key" enabled, or these models will not appear.
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
1. Set `inferenceCoop.apiKey` in your settings to your co-op API key.
|
1. Run **Inference Coop: Set API Key** from the Command Palette and paste
|
||||||
2. Open Copilot Chat and pick an `inference-coop/…` model.
|
your co-op key. It is stored in VS Code's secret storage, not in settings.
|
||||||
|
2. Open Copilot Chat, click the model name, and pick an `inference-coop/…`
|
||||||
|
model. Tool calling works, so agent mode is usable.
|
||||||
|
|
||||||
## Status
|
To use a different gateway, set `inferenceCoop.baseUrl`. To sign out, run
|
||||||
|
**Inference Coop: Sign Out**.
|
||||||
|
|
||||||
Early development. Text chat only so far; tool calling is not implemented.
|
## Limitations
|
||||||
|
|
||||||
|
- No image input; models are advertised as text-and-tools only.
|
||||||
|
- Token counts are estimated rather than computed with a real tokenizer, so
|
||||||
|
context budgeting is approximate.
|
||||||
|
- No inline code completions. Copilot does not support third-party providers
|
||||||
|
for completions; this extension only adds chat models.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "inference-coop-copilot",
|
"name": "inference-coop-copilot",
|
||||||
"version": "0.1.0",
|
"version": "0.2.2",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "inference-coop-copilot",
|
"name": "inference-coop-copilot",
|
||||||
"version": "0.1.0",
|
"version": "0.2.2",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/mocha": "^10.0.10",
|
"@types/mocha": "^10.0.10",
|
||||||
"@types/node": "24.x",
|
"@types/node": "24.x",
|
||||||
|
|||||||
+21
-13
@@ -2,11 +2,11 @@
|
|||||||
"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.1.0",
|
"version": "0.2.2",
|
||||||
"engines": {
|
"engines": {
|
||||||
"vscode": "^1.145.0"
|
"vscode": "^1.145.0"
|
||||||
},
|
},
|
||||||
"publisher": "inference-coop",
|
"publisher": "evanp",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.inference.coop/evanp/inference-coop-copilot.git"
|
"url": "https://git.inference.coop/evanp/inference-coop-copilot.git"
|
||||||
@@ -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."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+262
-125
@@ -1,182 +1,313 @@
|
|||||||
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`, {
|
if (token.isCancellationRequested) {
|
||||||
method: 'POST',
|
out.appendLine(" request cancelled before it started");
|
||||||
headers: {
|
return;
|
||||||
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();
|
// Abort the request as soon as the user cancels, so a cancelled chat
|
||||||
const decoder = new TextDecoder();
|
// stops generating (and stops billing) server-side.
|
||||||
let buf = '';
|
const abort = new AbortController();
|
||||||
let textChunks = 0;
|
const cancelSubscription = token.onCancellationRequested(() =>
|
||||||
const pending = new Map<number, { id?: string; name: string; args: string }>();
|
abort.abort(),
|
||||||
|
);
|
||||||
|
|
||||||
const flushTools = () => {
|
try {
|
||||||
for (const call of pending.values()) {
|
const res = await fetch(`${BASE_URL}/chat/completions`, {
|
||||||
let input: object = {};
|
method: "POST",
|
||||||
try {
|
headers: {
|
||||||
input = JSON.parse(call.args || '{}');
|
Authorization: `Bearer ${key}`,
|
||||||
} catch {
|
"Content-Type": "application/json",
|
||||||
out.appendLine(` WARN bad tool args for ${call.name}: ${call.args}`);
|
},
|
||||||
}
|
body: JSON.stringify(body),
|
||||||
out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`);
|
signal: abort.signal,
|
||||||
progress.report(
|
});
|
||||||
new vscode.LanguageModelToolCallPart(
|
out.appendLine(` /chat/completions status: ${res.status}`);
|
||||||
call.id ?? `call_${Math.random().toString(36).slice(2)}`,
|
if (!res.ok || !res.body) {
|
||||||
call.name,
|
throw new Error(
|
||||||
input
|
`Inference Coop: HTTP ${res.status} — ${await res.text()}`,
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pending.clear();
|
|
||||||
};
|
|
||||||
|
|
||||||
while (true) {
|
const reader = res.body.getReader();
|
||||||
const { done, value } = await reader.read();
|
const decoder = new TextDecoder();
|
||||||
if (done) {break;}
|
let buf = "";
|
||||||
buf += decoder.decode(value, { stream: true });
|
let textChunks = 0;
|
||||||
const frames = buf.split('\n\n');
|
const pending = new Map<
|
||||||
buf = frames.pop() ?? '';
|
number,
|
||||||
|
{ id?: string; name: string; args: string }
|
||||||
|
>();
|
||||||
|
|
||||||
for (const frame of frames) {
|
const flushTools = () => {
|
||||||
for (const line of frame.split('\n')) {
|
for (const call of pending.values()) {
|
||||||
if (!line.startsWith('data:')) {continue;}
|
let input: object = {};
|
||||||
const data = line.slice(5).trim();
|
|
||||||
if (data === '[DONE]') {
|
|
||||||
if (pending.size) {flushTools();}
|
|
||||||
out.appendLine(` done, ${textChunks} text chunks`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let json: any;
|
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(data);
|
input = JSON.parse(call.args || "{}");
|
||||||
} catch {
|
} catch {
|
||||||
continue;
|
out.appendLine(
|
||||||
|
` WARN bad tool args for ${call.name}: ${call.args}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const choice = json.choices?.[0];
|
out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`);
|
||||||
const delta = choice?.delta;
|
progress.report(
|
||||||
if (!delta) {continue;}
|
new vscode.LanguageModelToolCallPart(
|
||||||
|
call.id ?? `call_${Math.random().toString(36).slice(2)}`,
|
||||||
|
call.name,
|
||||||
|
input,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
pending.clear();
|
||||||
|
};
|
||||||
|
|
||||||
if (typeof delta.content === 'string' && delta.content) {
|
while (true) {
|
||||||
textChunks++;
|
if (token.isCancellationRequested) {
|
||||||
progress.report(new vscode.LanguageModelTextPart(delta.content));
|
out.appendLine(" cancelled mid-stream; aborting request");
|
||||||
}
|
abort.abort(); // close the connection so the gateway stops generating
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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 tc of delta.tool_calls ?? []) {
|
for (const frame of frames) {
|
||||||
const i = tc.index ?? 0;
|
for (const line of frame.split("\n")) {
|
||||||
const acc = pending.get(i) ?? { id: undefined, name: '', args: '' };
|
if (!line.startsWith("data:")) {
|
||||||
acc.id = tc.id ?? acc.id;
|
continue;
|
||||||
acc.name = tc.function?.name ?? acc.name;
|
}
|
||||||
acc.args += tc.function?.arguments ?? '';
|
const data = line.slice(5).trim();
|
||||||
pending.set(i, acc);
|
if (data === "[DONE]") {
|
||||||
}
|
if (pending.size) {
|
||||||
|
flushTools();
|
||||||
|
}
|
||||||
|
out.appendLine(` done, ${textChunks} text chunks`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let json: any;
|
||||||
|
try {
|
||||||
|
json = JSON.parse(data);
|
||||||
|
} catch {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const choice = json.choices?.[0];
|
||||||
|
const delta = choice?.delta;
|
||||||
|
if (!delta) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (choice?.finish_reason) {
|
if (typeof delta.content === "string" && delta.content) {
|
||||||
out.appendLine(` finish_reason: ${choice.finish_reason}`);
|
textChunks++;
|
||||||
if (pending.size) {flushTools();}
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (pending.size) {
|
||||||
|
flushTools();
|
||||||
|
}
|
||||||
|
out.appendLine(` stream ended, ${textChunks} text chunks`);
|
||||||
|
} catch (e) {
|
||||||
|
// A cancellation aborts fetch (before headers) or the pending stream
|
||||||
|
// read (mid-response) - both surface as AbortError. That is the user
|
||||||
|
// pressing stop, not a failure: stop quietly instead of erroring.
|
||||||
|
if (
|
||||||
|
token.isCancellationRequested ||
|
||||||
|
(e instanceof Error && e.name === "AbortError")
|
||||||
|
) {
|
||||||
|
out.appendLine(" stream aborted after cancellation");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
} finally {
|
||||||
|
cancelSubscription.dispose();
|
||||||
}
|
}
|
||||||
if (pending.size) {flushTools();}
|
|
||||||
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 +322,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,4 +367,4 @@ function toOpenAIMessages(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deactivate() {}
|
export function deactivate() {}
|
||||||
Reference in new issue
Block a user