4 Commits
Author SHA1 Message Date
evanp 4fdaa0ca9d Merge pull request 'Abort in-flight requests when the chat is cancelled' (#1) from code/inference-coop-copilot:fix/abort-on-cancel into main
Reviewed-on: #1
2026-11-07 04:07:19 +00:00
inference-bot 9798a41745 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-07 00:40:40 +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
4 changed files with 139 additions and 99 deletions

No files matched your search

+8 -1
View File
@@ -10,6 +10,12 @@ and this project adheres to
## [Unreleased] ## [Unreleased]
## [0.2.2] - 2026-11-06
### Fixed
- Correct the publisher in package.json.
## [0.2.1] - 2026-11-06 ## [0.2.1] - 2026-11-06
### Fixed ### Fixed
@@ -64,7 +70,8 @@ First release usable for chat.
Initial development release. Not functional for chat. Initial development release. Not functional for chat.
[Unreleased]: https://git.inference.coop/evanp/inference-coop-copilot/compare/v0.2.1...HEAD [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.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.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 [0.1.0]: https://git.inference.coop/evanp/inference-coop-copilot/releases/tag/v0.1.0
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "inference-coop-copilot", "name": "inference-coop-copilot",
"version": "0.2.1", "version": "0.2.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "inference-coop-copilot", "name": "inference-coop-copilot",
"version": "0.2.1", "version": "0.2.2",
"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,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.2.1", "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"
+127 -94
View File
@@ -109,118 +109,151 @@ export function activate(context: vscode.ExtensionContext) {
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< abort.abort(),
number, );
{ id?: string; name: string; args: string }
>();
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),
); signal: abort.signal,
} });
out.appendLine(` tool call: ${call.name} ${JSON.stringify(input)}`); out.appendLine(` /chat/completions status: ${res.status}`);
progress.report( if (!res.ok || !res.body) {
new vscode.LanguageModelToolCallPart( throw new Error(
call.id ?? `call_${Math.random().toString(36).slice(2)}`, `Inference Coop: HTTP ${res.status} — ${await res.text()}`,
call.name,
input,
),
); );
} }
pending.clear();
};
while (true) { const reader = res.body.getReader();
const { done, value } = await reader.read(); const decoder = new TextDecoder();
if (done) { let buf = "";
break; let textChunks = 0;
} const pending = new Map<
buf += decoder.decode(value, { stream: true }); number,
const frames = buf.split("\n\n"); { id?: string; name: string; args: string }
buf = frames.pop() ?? ""; >();
for (const frame of frames) { const flushTools = () => {
for (const line of frame.split("\n")) { for (const call of pending.values()) {
if (!line.startsWith("data:")) { let input: object = {};
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 { 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]; );
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) { 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) ?? { if (!line.startsWith("data:")) {
id: undefined, continue;
name: "", }
args: "", const data = line.slice(5).trim();
}; if (data === "[DONE]") {
acc.id = tc.id ?? acc.id; if (pending.size) {
acc.name = tc.function?.name ?? acc.name; flushTools();
acc.args += tc.function?.arguments ?? ""; }
pending.set(i, acc); 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) { progress.report(new vscode.LanguageModelTextPart(delta.content));
flushTools(); }
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() {