From b072c5fee401a5ffc8f5dc437570c6d8b7a5b049 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 2 May 2025 12:07:15 -0700 Subject: [PATCH] WIP: message abort controllers --- core/core.ts | 94 ++++++++++++++++++------------------ core/edit/streamDiffLines.ts | 9 +++- core/llm/streamChat.ts | 62 +++++++++++------------- 3 files changed, 80 insertions(+), 85 deletions(-) diff --git a/core/core.ts b/core/core.ts index e5c9eeab0..36b4a399c 100644 --- a/core/core.ts +++ b/core/core.ts @@ -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, - msg: Message, -): AsyncGenerator { - 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; @@ -114,7 +76,18 @@ export class Core { this.globalContext.get("indexingPaused") === true, ); - private abortedMessageIds: Set = new Set(); + private messageAbortControllers = new Map(); + 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( 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)); diff --git a/core/edit/streamDiffLines.ts b/core/edit/streamDiffLines.ts index 320962b7c..bcf74bf80 100644 --- a/core/edit/streamDiffLines.ts +++ b/core/edit/streamDiffLines.ts @@ -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 { 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; diff --git a/core/llm/streamChat.ts b/core/llm/streamChat.ts index 223b5b20e..c899e437f 100644 --- a/core/llm/streamChat.ts +++ b/core/llm/streamChat.ts @@ -8,7 +8,7 @@ import { TTS } from "../util/tts"; export async function* llmStreamChat( configHandler: ConfigHandler, - abortedMessageIds: Set, + abortController: AbortController, msg: Message, ide: IDE, messenger: IMessenger, @@ -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; }