26 Commits
Author SHA1 Message Date
evanp 28d736f627 chore: push to VS Marketplace on semver tag 2026-11-08 12:31:32 -05:00
evanp 0d30fd1eb5 0.4.1 2026-11-08 11:53:05 -05:00
evanp 8265975098 fix: vsce build process needs URL locations 2026-11-08 11:52:58 -05:00
evanp 5aaf3f4f97 0.4.0 2026-11-08 11:48:56 -05:00
evanp 90a51287b4 doc: CHANGELOG 0.4.0 2026-11-08 11:48:46 -05:00
evanp a252f90b62 doc: update README with instructions for joining, getting an API key 2026-11-08 11:47:09 -05:00
evanp a789afbd61 chore: update marketplace display name 2026-11-08 11:41:25 -05:00
evanp ac9e439191 fix: remove unused base URL configuration 2026-11-08 11:26:17 -05:00
evanp 215ec19ba1 doc: better visibility in VSCode Extensions Marketplace 2026-11-08 11:22:08 -05:00
evanp e74d97f4ae chore: publish script 2026-11-08 11:09:16 -05:00
evanp b5c2924886 fix: baseContentUrl for publishing 2026-11-08 11:05:17 -05:00
evanp 892151ef92 0.3.0 2026-11-08 10:58:51 -05:00
evanp 503273f17d chore: release v0.3.0 2026-11-08 10:58:43 -05:00
evanp bbf1b56a76 fix: repository URL 2026-11-08 10:55:57 -05:00
evanp 9b6e78e9f5 chore: mark package as official 2026-11-08 10:52:45 -05:00
evanp 4e50c28def Merge remote-tracking branch 'origin/fix/abort-on-cancel' 2026-11-08 10:48:48 -05:00
inference-bot 8f1d71d913 fix: abort in-flight chat requests on cancellation
Cancellation was only checked at POST time; once the SSE stream was
open the reader loop never looked at the token again, so a stopped
chat kept generating server-side (and billing) until the response
ended.

Wire an AbortController to token.onCancellationRequested, pass its
signal to fetch, and check token.isCancellationRequested at the top
of the reader loop so the connection closes as soon as the user
stops the request. AbortError is swallowed (a stop is not a
failure); real HTTP errors still surface, and the cancellation
listener is disposed in a finally.
2026-11-08 15:44:59 +00:00
inference-bot e9a060af0f docs: project is now official
Drop the unofficial framing (title, intro disclaimer, limitations note)
and state the project as official, maintained in the co-op's Git at
code/inference-coop-copilot.
2026-11-07 00:44:54 +00:00
evanp 46b8d7e888 0.2.2 2026-11-06 14:02:02 -05:00
evanp 0460305327 fix: publisher in package.json 2026-11-06 14:01:56 -05:00
evanp c4bb02a7a2 0.2.1 2026-11-06 13:39:18 -05:00
evanp f295cb8535 doc: update README 2026-11-06 13:39:14 -05:00
evanp 55e2764480 0.2.0 2026-11-06 13:33:24 -05:00
evanp cb23c502aa doc: CHANGELOG v0.2.0 2026-11-06 13:33:16 -05:00
evanp 4eeace6ce3 feat: store API key in SecretStorage, add set/sign-out commands 2026-11-06 13:26:34 -05:00
evanp 713a1c1d75 fix: cache model list and fire change event to avoid startup race 2026-11-06 13:13:46 -05:00
10 changed files with 2018 additions and 169 deletions

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
name: Publish to VS Marketplace
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '24'
- name: Verify tag matches package.json version
env:
TAG_REF: ${{ gitea.ref_name }}
run: |
tag="${TAG_REF#v}"
pkg="$(node -p "require('./package.json').version")"
echo "Tag version: $tag"
echo "package.json version: $pkg"
if [ "$tag" != "$pkg" ]; then
echo "::error::Tag v$tag does not match package.json version $pkg"
exit 1
fi
- name: Install dependencies
run: npm ci
- name: Type-check, lint, and bundle
run: npm run package
- name: Publish to VS Marketplace
env:
VSCE_PAT: ${{ secrets.VSCE_PAT }}
run: npx vsce publish -p "$VSCE_PAT" --skip-duplicate
+1
View File
@@ -1,5 +1,6 @@
.vscode/**
.vscode-test/**
.gitea/**
out/**
node_modules/**
src/**
+135 -12
View File
@@ -1,30 +1,153 @@
# 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]
### Added
- Gitea Actions workflow (`.gitea/workflows/publish.yaml`) that publishes to
the VS Code Marketplace when a `vX.Y.Z` tag is pushed. It fails if the tag
disagrees with the `version` in package.json, runs the existing
`npm run package` gate (type-check, lint, production bundle) before
publishing, and passes `--skip-duplicate` so a re-run is a no-op rather
than a failure. Requires repository Actions to be enabled and a
`VSCE_PAT` repository secret.
### Fixed
- Excluded `.gitea/**` from the packaged extension in `.vscodeignore`, so
workflow files are no longer bundled into the VSIX.
## [0.4.1] - 2026-11-08
### Fixed
- Moved the Marketplace `baseContentUrl`/`baseImagesUrl` settings into a
`vsce` block in package.json. vsce only reads them from there (or from
CLI flags) for non-GitHub/GitLab repositories, so packaging previously
failed with "Couldn't detect the repository". Verified the packaged
README now links the icon, LICENSE, and NOTICE with working Gitea URLs.
## [0.4.0] - 2026-11-08
### Added
- Set `baseContentUrl` in package.json to the Gitea raw URL, so LICENSE and
NOTICE links in the README resolve correctly on the Marketplace.
- Added an npm `publish` script running `vsce publish`, with `@vscode/vsce`
as a dev dependency, for releasing to the VS Code Marketplace.
- Added `language-models` and `chat` Marketplace tags, so the extension
appears in the "Install Language Models" discovery flow. The tags take
effect when the next version is published.
- Added an extension icon rendered from the Inference Coop logo, shown on
the Marketplace listing and in the Extensions view.
- Added Marketplace keywords for search discoverability.
- Changed the Marketplace category from "Other" to "AI".
- Changed the Marketplace display name from the package ID to the readable
title, "Inference Coop for Copilot Chat".
- Reworked the README: an "About the Inference Cooperative" section for
prospective members, step-by-step Setup instructions starting with
creating an API key at the dashboard (noting keys are shown only once),
and consistent use of "Inference Cooperative" as the organization name.
### Removed
- Removed the unused `inferenceCoop.baseUrl` setting. The extension only
ever talked to the hardcoded `https://gateway.inference.coop/v1` gateway;
keeping a configurable URL would let a modified setting redirect the
stored API key to a hostile endpoint.
- Removed the membership/API-key line from Requirements, since Setup now
walks through acquiring both.
## [0.3.0] - 2026-11-08
### Changed
- Removed the "(unofficial)" disclaimer from the package description and
NOTICE, and the README; the project is now official, maintained in the
co-op's Git at `code/inference-coop-copilot`.
- Updated the repository URL to `https://git.inference.coop/code/inference-coop-copilot.git`
in package.json and CHANGELOG links.
### Fixed
- Abort in-flight chat requests on cancellation: an `AbortController` wired
to `token.onCancellationRequested` closes the SSE connection as soon as
the user stops the request, instead of the server continuing to generate
until the response ends.
## [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
- "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.
- Registration of a `LanguageModelChatProvider` under the `inference-coop`
vendor, discoverable via `contributes.languageModelChatProviders`.
- Model discovery: fetches `GET https://gateway.inference.coop/v1/models`
and filters to chat-capable models, excluding image-generation and
audio endpoints.
- `inferenceCoop.apiKey` setting (plain text, temporarily).
- "Inference Coop" output channel logging provider activation, model
enumeration, and HTTP status codes.
### Known issues
### Changed
- Models appear in the Language Models panel but not in the Copilot Chat
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`.
- Adopted the Apache License 2.0.
### 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/code/inference-coop-copilot/compare/v0.4.1...HEAD
[0.4.1]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.4.0...v0.4.1
[0.4.0]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.3.0...v0.4.0
[0.3.0]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.2.2...v0.3.0
[0.2.2]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.2.1...v0.2.2
[0.2.1]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.2.0...v0.2.1
[0.2.0]: https://git.inference.coop/code/inference-coop-copilot/compare/v0.1.0...v0.2.0
[0.1.0]: https://git.inference.coop/code/inference-coop-copilot/releases/tag/v0.1.0
+1 -1
View File
@@ -1,4 +1,4 @@
Inference Coop Copilot (unofficial)
Inference Coop Copilot
Copyright 2026 Evan Prodromou
Licensed under the Apache License, Version 2.0.
+53 -8
View File
@@ -1,14 +1,59 @@
# Inference Coop (unofficial)
# Inference Coop for Copilot Chat
Community-built model provider for [inference.coop](https://inference.coop),
usable in GitHub Copilot Chat. Not affiliated with or endorsed by the
Inference Cooperative.
![Inference Coop](images/icon.png)
Official model provider for [inference.coop](https://inference.coop), usable
in GitHub Copilot Chat. An official project of the Inference Cooperative,
maintained in the Inference Cooperative's Git at
[code/inference-coop-copilot](https://git.inference.coop/code/inference-coop-copilot).
## About the Inference Cooperative
The Inference Cooperative is a member-owned cooperative that pools its
members' purchasing power to support privacy-preserving, renewable-energy,
and open-weights AI services. Members choose among powerful, open-weights
models — hosted in encrypted enclaves, on 100% renewable power, or built as
public goods — instead of accepting whatever Big Tech offers.
Membership is a sliding scale starting at $5 a month. Every member gets the
same $15 monthly credit for chat and API use, plus a vote in how the
Inference Cooperative is run. Credits never expire, and you can cancel any time.
To join, visit [inference.coop](https://inference.coop) and sign up. Your
membership includes access to the chat and the developer API — this
extension brings those same models into GitHub Copilot Chat.
## Requirements
- VS Code 1.145 or newer
- GitHub Copilot Chat, installed and signed in
Copilot Business/Enterprise: your administrator must leave "Bring Your Own
Language Model Key" enabled, or these models will not appear.
## Setup
1. Set `inferenceCoop.apiKey` in your settings to your co-op API key.
2. Open Copilot Chat and pick an `inference-coop/…` model.
1. Create an API key at
[dashboard.inference.coop/#keys-card](https://dashboard.inference.coop/#keys-card).
**The key is only shown once, right after it is created** — copy it
somewhere safe before you close the page.
2. Run **Inference Coop: Set API Key** from the Command Palette and paste
the key. It is stored in VS Code's secret storage, not in settings.
3. Open Copilot Chat, click the model name, and pick an `inference-coop/…`
model. Tool calling works, so agent mode is usable.
## Status
To sign out, run **Inference Coop: Sign Out**. (You can create a new key
any time and run Set API Key again — keys are not deleted when you sign
out, so revoke unused keys from the dashboard.)
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).
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

+25
View File
@@ -0,0 +1,25 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 130 130">
<rect width="130" height="130" rx="24" fill="#faf8f5"/>
<circle cx="65" cy="14" r="5" fill="#5b8c5a"/>
<circle cx="58" cy="30" r="6" fill="#7ab87a"/>
<circle cx="74" cy="30" r="4" fill="#6ba86b"/>
<circle cx="51.5" cy="46" r="6.5" fill="#5b8c5a"/>
<circle cx="65" cy="46" r="5" fill="#7ab87a"/>
<circle cx="81" cy="46" r="4" fill="#6ba86b"/>
<circle cx="44" cy="62" r="7" fill="#6ba86b"/>
<circle cx="61" cy="62" r="5.5" fill="#5b8c5a"/>
<circle cx="76" cy="62" r="4.5" fill="#7ab87a"/>
<circle cx="89.5" cy="62" r="3.5" fill="#6ba86b"/>
<circle cx="36.5" cy="78" r="7.5" fill="#5b8c5a"/>
<circle cx="54" cy="78" r="6" fill="#7ab87a"/>
<circle cx="70" cy="78" r="5" fill="#6ba86b"/>
<circle cx="84" cy="78" r="4" fill="#5b8c5a"/>
<circle cx="97.5" cy="78" r="3.5" fill="#7ab87a"/>
<circle cx="44" cy="94" r="6.5" fill="#6ba86b"/>
<circle cx="61" cy="94" r="5.5" fill="#5b8c5a"/>
<circle cx="76" cy="94" r="4.5" fill="#7ab87a"/>
<circle cx="89.5" cy="94" r="3.5" fill="#6ba86b"/>
<circle cx="51.5" cy="110" r="5.5" fill="#5b8c5a"/>
<circle cx="65" cy="110" r="4.5" fill="#7ab87a"/>
<circle cx="81" cy="110" r="3.5" fill="#6ba86b"/>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

+1461 -2
View File
File diff suppressed because it is too large. Load diff
+39 -22
View File
@@ -1,19 +1,36 @@
{
"name": "inference-coop-copilot",
"displayName": "inference-coop-copilot",
"description": "Inference.coop models for Copilot Chat (unofficial)",
"version": "0.1.0",
"displayName": "Inference Coop for Copilot Chat",
"description": "Inference.coop models for Copilot Chat",
"version": "0.4.1",
"icon": "images/icon.png",
"engines": {
"vscode": "^1.145.0"
},
"publisher": "inference-coop",
"publisher": "evanp",
"repository": {
"type": "git",
"url": "https://git.inference.coop/evanp/inference-coop-copilot.git"
"url": "https://git.inference.coop/code/inference-coop-copilot.git"
},
"vsce": {
"baseContentUrl": "https://git.inference.coop/code/inference-coop-copilot/raw/branch/main",
"baseImagesUrl": "https://git.inference.coop/code/inference-coop-copilot/raw/branch/main"
},
"license": "Apache-2.0",
"categories": [
"Other"
"AI"
],
"tags": [
"language-models",
"chat"
],
"keywords": [
"inference-coop",
"inference.coop",
"copilot",
"chat",
"language model",
"AI"
],
"activationEvents": [],
"main": "./dist/extension.js",
@@ -26,8 +43,12 @@
],
"commands": [
{
"command": "inference-coop-copilot.helloWorld",
"title": "Hello World"
"command": "inferenceCoop.setKey",
"title": "Inference Coop: Set API Key"
},
{
"command": "inferenceCoop.clearKey",
"title": "Inference Coop: Sign Out"
}
]
},
@@ -43,32 +64,28 @@
"pretest": "npm run compile-tests && npm run compile && npm run lint",
"check-types": "tsc --noEmit",
"lint": "eslint src",
"publish": "vsce publish",
"test": "vscode-test"
},
"devDependencies": {
"@types/vscode": "^1.145.0",
"@types/mocha": "^10.0.10",
"@types/node": "24.x",
"typescript-eslint": "^8.61.1",
"eslint": "^10.5.0",
"@types/vscode": "^1.145.0",
"@vscode/test-cli": "^0.0.15",
"@vscode/test-electron": "^3.0.0",
"@vscode/vsce": "^4.1.0",
"esbuild": "^0.28.1",
"eslint": "^10.5.0",
"npm-run-all": "^4.1.5",
"typescript": "^6.0.3",
"@vscode/test-cli": "^0.0.15",
"@vscode/test-electron": "^3.0.0"
"typescript-eslint": "^8.61.1"
},
"overrides": {
"diff": "^8.0.4",
"serialize-javascript": "^7.0.6"
},
"configuration": {
"title": "Inference Coop",
"properties": {
"inferenceCoop.apiKey": {
"type": "string",
"default": "",
"description": "API key from the Inference Cooperative dashboard."
}
}
"allowScripts": {
"@vscode/vsce-sign@2.1.0": true,
"esbuild@0.28.2": true
}
}
+261 -124
View File
@@ -1,182 +1,313 @@
import * as vscode from 'vscode';
import * as vscode from "vscode";
const BASE_URL = 'https://gateway.inference.coop/v1';
const PREFIX = 'inference-coop/';
const BASE_URL = "https://gateway.inference.coop/v1";
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) {
const out = vscode.window.createOutputChannel('Inference Coop');
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 changed = new vscode.EventEmitter<void>();
context.subscriptions.push(changed);
const key = vscode.workspace
.getConfiguration('inferenceCoop')
.get<string>('apiKey');
if (!key) {return [];}
let cached: CoopModel[] = [];
let lastFetch = 0;
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`, {
headers: { Authorization: `Bearer ${key}` }
headers: { Authorization: `Bearer ${key}` },
});
out.appendLine(` /models status: ${res.status}`);
if (!res.ok) {return [];}
out.appendLine(`refresh: /models ${res.status}`);
if (!res.ok) {
return cached;
}
const body = (await res.json()) as {
data: Array<{ id: string; mode?: string }>;
};
const models = body.data
.filter(m => !m.mode || m.mode === 'chat')
.map(m => ({
const next: CoopModel[] = body.data
.filter((m) => !m.mode || m.mode === "chat")
.map((m) => ({
id: `${PREFIX}${m.id}`,
name: m.id,
family: 'inference-coop',
version: '1.0.0',
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;
lastFetch = Date.now();
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(` tools offered: ${options.tools?.length ?? 0}`);
const key = vscode.workspace
.getConfiguration('inferenceCoop')
.get<string>('apiKey');
const key = await context.secrets.get(SECRET_KEY);
const body: Record<string, unknown> = {
model: model.id.replace(PREFIX, ''),
model: model.id.replace(PREFIX, ""),
stream: true,
messages: toOpenAIMessages(messages, out)
messages: toOpenAIMessages(messages, out),
};
if (options.tools?.length) {
body.tools = options.tools.map(t => ({
type: 'function',
body.tools = options.tools.map((t) => ({
type: "function",
function: {
name: t.name,
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`, {
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()}`);
if (token.isCancellationRequested) {
out.appendLine(" request cancelled before it started");
return;
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
let textChunks = 0;
const pending = new Map<number, { id?: string; name: string; args: string }>();
// Abort the request as soon as the user cancels, so a cancelled chat
// stops generating (and stops billing) server-side.
const abort = new AbortController();
const cancelSubscription = token.onCancellationRequested(() =>
abort.abort(),
);
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
)
try {
const res = await fetch(`${BASE_URL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${key}`,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
signal: abort.signal,
});
out.appendLine(` /chat/completions status: ${res.status}`);
if (!res.ok || !res.body) {
throw new Error(
`Inference Coop: HTTP ${res.status} — ${await res.text()}`,
);
}
pending.clear();
};
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() ?? '';
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = "";
let textChunks = 0;
const pending = new Map<
number,
{ id?: string; name: string; args: string }
>();
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]') {
if (pending.size) {flushTools();}
out.appendLine(` done, ${textChunks} text chunks`);
return;
}
let json: any;
const flushTools = () => {
for (const call of pending.values()) {
let input: object = {};
try {
json = JSON.parse(data);
input = JSON.parse(call.args || "{}");
} catch {
continue;
out.appendLine(
` WARN bad tool args for ${call.name}: ${call.args}`,
);
}
const choice = json.choices?.[0];
const delta = choice?.delta;
if (!delta) {continue;}
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();
};
if (typeof delta.content === 'string' && delta.content) {
textChunks++;
progress.report(new vscode.LanguageModelTextPart(delta.content));
}
while (true) {
if (token.isCancellationRequested) {
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 ?? []) {
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);
}
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]") {
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) {
out.appendLine(` finish_reason: ${choice.finish_reason}`);
if (pending.size) {flushTools();}
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();
}
}
}
}
}
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() {
return 0;
}
},
};
context.subscriptions.push(
vscode.lm.registerLanguageModelChatProvider('inference-coop', provider)
vscode.lm.registerLanguageModelChatProvider("inference-coop", provider),
);
context.subscriptions.push(
vscode.commands.registerCommand('inference-coop-copilot.helloWorld', () => {
vscode.window.showInformationMessage('Inference Coop extension loaded.');
})
vscode.commands.registerCommand("inference-coop-copilot.helloWorld", () => {
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(
messages: readonly vscode.LanguageModelChatRequestMessage[],
out: vscode.OutputChannel
out: vscode.OutputChannel,
): object[] {
const result: object[] = [];
@@ -191,38 +322,44 @@ function toOpenAIMessages(
} else if (part instanceof vscode.LanguageModelToolCallPart) {
toolCalls.push({
id: part.callId,
type: 'function',
function: { name: part.name, arguments: JSON.stringify(part.input ?? {}) }
type: "function",
function: {
name: part.name,
arguments: JSON.stringify(part.input ?? {}),
},
});
} else if (part instanceof vscode.LanguageModelToolResultPart) {
toolResults.push({
role: 'tool',
role: "tool",
tool_call_id: part.callId,
content: part.content
.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 =
msg.role === vscode.LanguageModelChatMessageRole.Assistant
? 'assistant'
? "assistant"
: msg.role === vscode.LanguageModelChatMessageRole.User
? 'user'
: 'system';
? "user"
: "system";
if (text.length || toolCalls.length) {
const m: Record<string, unknown> = {
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);
}
// OpenAI requires tool results to follow the assistant message that requested them.
result.push(...toolResults);
}