WIP: message abort controllers

This commit is contained in:
Dallin Romney 2025-05-02 12:07:15 -07:00
parent 7f926cad37
commit b072c5fee4
3 changed files with 80 additions and 85 deletions

View file

@ -35,7 +35,6 @@ import { TTS } from "./util/tts";
import {
ContextItemWithId,
DiffLine,
IdeSettings,
ModelDescription,
RangeInFile,
@ -63,43 +62,6 @@ import { llmStreamChat } from "./llm/streamChat";
import type { FromCoreProtocol, ToCoreProtocol } from "./protocol";
import type { IMessenger, Message } from "./protocol/messenger";
async function* streamDiffLinesGenerator(
configHandler: ConfigHandler,
abortedMessageIds: Set<string>,
msg: Message<ToCoreProtocol["streamDiffLines"][0]>,
): AsyncGenerator<DiffLine> {
const data = msg.data;
const { config } = await configHandler.loadConfig();
if (!config) {
throw new Error("Failed to load config");
}
const llm = config.selectedModelByRole.chat;
if (!llm) {
throw new Error("No chat model selected");
}
for await (const diffLine of streamDiffLines({
highlighted: data.highlighted,
prefix: data.prefix,
suffix: data.suffix,
llm,
rules: config.rules,
input: data.input,
language: data.language,
onlyOneInsertion: false,
overridePrompt: undefined,
})) {
if (abortedMessageIds.has(msg.messageId)) {
abortedMessageIds.delete(msg.messageId);
break;
}
yield diffLine;
}
}
export class Core {
configHandler: ConfigHandler;
codebaseIndexerPromise: Promise<CodebaseIndexer>;
@ -114,7 +76,18 @@ export class Core {
this.globalContext.get("indexingPaused") === true,
);
private abortedMessageIds: Set<string> = new Set();
private messageAbortControllers = new Map<string, AbortController>();
private addMessageAbortController(messageId: string): AbortController {
const controller = new AbortController();
this.messageAbortControllers.set(messageId, controller);
controller.signal.addEventListener("abort", () => {
this.messageAbortControllers.delete(messageId);
});
return controller;
}
private abortMessage(messageId: string) {
this.messageAbortControllers.get(messageId)?.abort();
}
invoke<T extends keyof ToCoreProtocol>(
messageType: T,
@ -283,7 +256,7 @@ export class Core {
});
on("abort", (msg) => {
this.abortedMessageIds.add(msg.messageId);
this.abortMessage(msg.messageId);
});
on("ping", (msg) => {
@ -452,15 +425,16 @@ export class Core {
}
});
on("llm/streamChat", (msg) =>
llmStreamChat(
on("llm/streamChat", (msg) => {
const abortController = this.addMessageAbortController(msg.messageId);
return llmStreamChat(
this.configHandler,
this.abortedMessageIds,
abortController,
msg,
this.ide,
this.messenger,
),
);
);
});
on("llm/complete", async (msg) => {
const model = (await this.configHandler.loadConfig()).config
@ -514,9 +488,33 @@ export class Core {
this.completionProvider.cancel();
});
on("streamDiffLines", (msg) =>
streamDiffLinesGenerator(this.configHandler, this.abortedMessageIds, msg),
);
on("streamDiffLines", async (msg) => {
const abortController = this.addMessageAbortController(msg.messageId);
const { config } = await this.configHandler.loadConfig();
if (!config) {
throw new Error("Failed to load config");
}
const llm = config.selectedModelByRole.chat;
if (!llm) {
throw new Error("No chat model selected");
}
const { data } = msg;
return streamDiffLines({
highlighted: data.highlighted,
prefix: data.prefix,
suffix: data.suffix,
llm,
rules: config.rules,
input: data.input,
language: data.language,
onlyOneInsertion: false,
overridePrompt: undefined,
abortController,
});
});
on("completeOnboarding", this.handleCompleteOnboarding.bind(this));

View file

@ -67,6 +67,7 @@ export async function* streamDiffLines({
onlyOneInsertion,
overridePrompt,
rules,
abortController,
}: {
prefix: string;
highlighted: string;
@ -77,6 +78,7 @@ export async function* streamDiffLines({
onlyOneInsertion: boolean;
overridePrompt: ChatMessage[] | undefined;
rules: RuleWithSource[];
abortController: AbortController;
}): AsyncGenerator<DiffLine> {
void Telemetry.capture(
"inlineEdit",
@ -158,12 +160,12 @@ export async function* streamDiffLines({
const completion =
typeof prompt === "string"
? llm.streamComplete(prompt, new AbortController().signal, {
? llm.streamComplete(prompt, abortController.signal, {
raw: true,
prediction,
reasoning: false,
})
: llm.streamChat(prompt, new AbortController().signal, {
: llm.streamChat(prompt, abortController.signal, {
prediction,
reasoning: false,
});
@ -190,6 +192,9 @@ export async function* streamDiffLines({
let seenGreen = false;
for await (const diffLine of diffLines) {
if (abortController.signal.aborted) {
break;
}
yield diffLine;
if (diffLine.type === "new") {
seenGreen = true;

View file

@ -8,7 +8,7 @@ import { TTS } from "../util/tts";
export async function* llmStreamChat(
configHandler: ConfigHandler,
abortedMessageIds: Set<string>,
abortController: AbortController,
msg: Message<ToCoreProtocol["llm/streamChat"][0]>,
ide: IDE,
messenger: IMessenger<ToCoreProtocol, FromCoreProtocol>,
@ -75,52 +75,44 @@ export async function* llmStreamChat(
selectedCode,
config,
fetch: (url, init) =>
fetchwithRequestOptions(url, init, config.requestOptions),
fetchwithRequestOptions(
url,
{
...init,
signal: abortController.signal,
},
config.requestOptions,
),
completionOptions,
});
const checkActiveInterval = setInterval(() => {
if (abortedMessageIds.has(msg.messageId)) {
abortedMessageIds.delete(msg.messageId);
clearInterval(checkActiveInterval);
let next = await gen.next();
while (!next.done) {
if (abortController.signal.aborted) {
next = await gen.return(errorPromptLog);
break;
}
}, 100);
try {
let next = await gen.next();
while (!next.done) {
if (abortedMessageIds.has(msg.messageId)) {
abortedMessageIds.delete(msg.messageId);
next = await gen.return(errorPromptLog);
clearInterval(checkActiveInterval);
break;
}
if (next.value) {
yield {
role: "assistant",
content: next.value,
};
}
next = await gen.next();
if (next.value) {
yield {
role: "assistant",
content: next.value,
};
}
if (!next.done) {
throw new Error("Will never happen");
}
return next.value;
} catch (e) {
throw e;
} finally {
clearInterval(checkActiveInterval);
next = await gen.next();
}
if (!next.done) {
throw new Error("Will never happen");
}
return next.value;
} else {
const gen = model.streamChat(
messages,
new AbortController().signal,
abortController.signal,
completionOptions,
);
let next = await gen.next();
while (!next.done) {
if (abortedMessageIds.has(msg.messageId)) {
abortedMessageIds.delete(msg.messageId);
if (abortController.signal.aborted) {
next = await gen.return(errorPromptLog);
break;
}