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.
This commit is contained in:
inference-bot committed 2026-11-07 00:40:40 +00:00
1 parent 46b8d7e888
commit 9798a41745
1 file changed
+127 -94
+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() {