From d8ef2859aaded8ed0590241532fda442454154f0 Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Mon, 24 Apr 2023 20:54:13 +0100 Subject: [PATCH 1/9] add stop generation button --- src/lib/components/StopGeneratingBtn.svelte | 22 +++++++++++++++++++++ src/lib/components/chat/ChatWindow.svelte | 4 +++- src/routes/conversation/[id]/+page.svelte | 6 ++++++ src/routes/r/[id]/+page.svelte | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/StopGeneratingBtn.svelte diff --git a/src/lib/components/StopGeneratingBtn.svelte b/src/lib/components/StopGeneratingBtn.svelte new file mode 100644 index 00000000000..4c9fe008c15 --- /dev/null +++ b/src/lib/components/StopGeneratingBtn.svelte @@ -0,0 +1,22 @@ + + +{#if visible} + +{/if} diff --git a/src/lib/components/chat/ChatWindow.svelte b/src/lib/components/chat/ChatWindow.svelte index 13daf4ef4f2..6c7067a024f 100644 --- a/src/lib/components/chat/ChatWindow.svelte +++ b/src/lib/components/chat/ChatWindow.svelte @@ -3,10 +3,11 @@ import { createEventDispatcher } from "svelte"; import CarbonSendAltFilled from "~icons/carbon/send-alt-filled"; + import CarbonExport from "~icons/carbon/export"; import ChatMessages from "./ChatMessages.svelte"; import ChatInput from "./ChatInput.svelte"; - import CarbonExport from "~icons/carbon/export"; + import StopGeneratingBtn from "../StopGeneratingBtn.svelte"; import { PUBLIC_MODEL_NAME } from "$env/static/public"; export let messages: Message[] = []; @@ -24,6 +25,7 @@
+
{ if (loading) return; diff --git a/src/routes/conversation/[id]/+page.svelte b/src/routes/conversation/[id]/+page.svelte index 0e1c1f048b8..e287edc638d 100644 --- a/src/routes/conversation/[id]/+page.svelte +++ b/src/routes/conversation/[id]/+page.svelte @@ -17,6 +17,7 @@ let messages = data.messages; let lastLoadedMessages = data.messages; + let isAborted = false; // Since we modify the messages array locally, we don't want to reset it if an old version is passed $: if (data.messages !== lastLoadedMessages) { @@ -57,6 +58,10 @@ pending = false; if (!data || conversationId !== $page.params.id) break; + if (isAborted) { + isAborted = false; + break; + } // final message if (data.generated_text) { @@ -130,4 +135,5 @@ {messages} on:message={(message) => writeMessage(message.detail)} on:share={() => shareConversation($page.params.id, data.title)} + on:stop={() => (isAborted = true)} /> diff --git a/src/routes/r/[id]/+page.svelte b/src/routes/r/[id]/+page.svelte index 42e8b16cadb..8e883b2de37 100644 --- a/src/routes/r/[id]/+page.svelte +++ b/src/routes/r/[id]/+page.svelte @@ -58,6 +58,7 @@ createConversation(ev.detail)} on:share={shareConversation} + on:stop={shareConversation} messages={data.messages} {loading} /> From e9c2adf0e801c484d426c6a4cdd88d1bb8e9e190 Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Tue, 25 Apr 2023 10:42:34 +0100 Subject: [PATCH 2/9] update button position to be within the input + use CSS for transition instead of JS --- src/lib/components/StopGeneratingBtn.svelte | 23 ++++++++++----------- src/lib/components/chat/ChatWindow.svelte | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/lib/components/StopGeneratingBtn.svelte b/src/lib/components/StopGeneratingBtn.svelte index 4c9fe008c15..3ebe9a7d4de 100644 --- a/src/lib/components/StopGeneratingBtn.svelte +++ b/src/lib/components/StopGeneratingBtn.svelte @@ -1,8 +1,7 @@ -{#if visible} - -{/if} + diff --git a/src/lib/components/chat/ChatWindow.svelte b/src/lib/components/chat/ChatWindow.svelte index 6c7067a024f..313c4bfd04f 100644 --- a/src/lib/components/chat/ChatWindow.svelte +++ b/src/lib/components/chat/ChatWindow.svelte @@ -25,7 +25,7 @@
- + { if (loading) return; From c3f73e8dd04688f0079ecada7ce14aa41c2c7e03 Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Tue, 25 Apr 2023 11:34:33 +0100 Subject: [PATCH 3/9] refactor stop generating button to not have useless dispatch event --- src/lib/components/StopGeneratingBtn.svelte | 6 +----- src/lib/components/chat/ChatWindow.svelte | 8 ++++++-- src/routes/r/[id]/+page.svelte | 1 - 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/lib/components/StopGeneratingBtn.svelte b/src/lib/components/StopGeneratingBtn.svelte index 3ebe9a7d4de..eede3e2a71c 100644 --- a/src/lib/components/StopGeneratingBtn.svelte +++ b/src/lib/components/StopGeneratingBtn.svelte @@ -1,21 +1,17 @@ diff --git a/src/lib/components/chat/ChatWindow.svelte b/src/lib/components/chat/ChatWindow.svelte index 313c4bfd04f..04619f0d385 100644 --- a/src/lib/components/chat/ChatWindow.svelte +++ b/src/lib/components/chat/ChatWindow.svelte @@ -17,7 +17,7 @@ let message: string; - const dispatch = createEventDispatcher<{ message: string; share: void }>(); + const dispatch = createEventDispatcher<{ message: string; share: void; stop: void }>();
@@ -25,7 +25,11 @@
- + dispatch("stop")} + /> { if (loading) return; diff --git a/src/routes/r/[id]/+page.svelte b/src/routes/r/[id]/+page.svelte index 8e883b2de37..42e8b16cadb 100644 --- a/src/routes/r/[id]/+page.svelte +++ b/src/routes/r/[id]/+page.svelte @@ -58,7 +58,6 @@ createConversation(ev.detail)} on:share={shareConversation} - on:stop={shareConversation} messages={data.messages} {loading} /> From 25fabae256ae2d4eda30a933cccdda2c69f16cd6 Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Tue, 25 Apr 2023 12:43:52 +0100 Subject: [PATCH 4/9] fix stop generation button positioning on mobile + add z-index reset on parent --- src/lib/components/chat/ChatWindow.svelte | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/components/chat/ChatWindow.svelte b/src/lib/components/chat/ChatWindow.svelte index 04619f0d385..e7488e20722 100644 --- a/src/lib/components/chat/ChatWindow.svelte +++ b/src/lib/components/chat/ChatWindow.svelte @@ -23,11 +23,11 @@
dispatch("stop")} /> Date: Tue, 25 Apr 2023 12:46:29 +0100 Subject: [PATCH 5/9] fix typo --- src/lib/components/chat/ChatWindow.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/chat/ChatWindow.svelte b/src/lib/components/chat/ChatWindow.svelte index e7488e20722..ad913d435f9 100644 --- a/src/lib/components/chat/ChatWindow.svelte +++ b/src/lib/components/chat/ChatWindow.svelte @@ -26,7 +26,7 @@ class="flex flex-col pointer-events-none [&>*]:pointer-events-auto max-md:border-t dark:border-gray-800 items-center max-md:dark:bg-gray-900 max-md:bg-white bg-gradient-to-t from-white via-white/80 to-white/0 dark:from-gray-900 dark:via-gray-80 dark:to-gray-900/0 justify-center absolute inset-x-0 max-w-3xl xl:max-w-4xl mx-auto px-3.5 sm:px-5 bottom-0 py-4 md:py-8 w-full z-0" > dispatch("stop")} /> From 421ca027d19bef8f14a59260c8b1a9acf9f6eb7b Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Wed, 26 Apr 2023 13:40:51 +0200 Subject: [PATCH 6/9] =?UTF-8?q?=E2=9C=A8=20Add=20backend=20code=20to=20abo?= =?UTF-8?q?rt=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/server/abortedGenerations.ts | 29 ++++++++++++ src/lib/server/database.ts | 6 ++- src/lib/types/AbortedGeneration.ts | 9 ++++ src/lib/utils/concatUint8Arrays.ts | 12 +++++ src/routes/conversation/[id]/+server.ts | 46 +++++++++++++++---- .../[id]/stop-generating/+server.ts | 27 +++++++++++ 6 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 src/lib/server/abortedGenerations.ts create mode 100644 src/lib/types/AbortedGeneration.ts create mode 100644 src/lib/utils/concatUint8Arrays.ts create mode 100644 src/routes/conversation/[id]/stop-generating/+server.ts diff --git a/src/lib/server/abortedGenerations.ts b/src/lib/server/abortedGenerations.ts new file mode 100644 index 00000000000..575cf637bfe --- /dev/null +++ b/src/lib/server/abortedGenerations.ts @@ -0,0 +1,29 @@ +// Shouldn't be needed if we dove into sveltekit internals, see https://github.com/huggingface/chat-ui/pull/88#issuecomment-1523173850 + +import { setTimeout } from "node:timers/promises"; +import { collections } from "./database"; + +let closed = false; +process.on("SIGINT", () => { + closed = true; +}); + +export let abortedGenerations: Map = new Map(); + +async function maintainAbortedGenerations() { + while (!closed) { + await setTimeout(1000); + + try { + const aborts = await collections.abortedGenerations.find({}).sort({ createdAt: 1 }).toArray(); + + abortedGenerations = new Map( + aborts.map(({ conversationId, createdAt }) => [conversationId.toString(), createdAt]) + ); + } catch (err) { + console.error(err); + } + } +} + +maintainAbortedGenerations(); diff --git a/src/lib/server/database.ts b/src/lib/server/database.ts index b2ef44534de..042f8fb5125 100644 --- a/src/lib/server/database.ts +++ b/src/lib/server/database.ts @@ -2,6 +2,7 @@ import { MONGODB_URL, MONGODB_DB_NAME } from "$env/static/private"; import { MongoClient } from "mongodb"; import type { Conversation } from "$lib/types/Conversation"; import type { SharedConversation } from "$lib/types/SharedConversation"; +import type { AbortedGeneration } from "$lib/types/AbortedGeneration"; const client = new MongoClient(MONGODB_URL, { // directConnection: true @@ -13,11 +14,14 @@ const db = client.db(MONGODB_DB_NAME); const conversations = db.collection("conversations"); const sharedConversations = db.collection("sharedConversations"); +const abortedGenerations = db.collection("abortedGenerations"); export { client, db }; -export const collections = { conversations, sharedConversations }; +export const collections = { conversations, sharedConversations, abortedGenerations }; client.on("open", () => { conversations.createIndex({ sessionId: 1, updatedAt: -1 }); + abortedGenerations.createIndex({ updatedAt: 1 }, { expireAfterSeconds: 30 }); + abortedGenerations.createIndex({ conversationId: 1 }, { unique: true }); sharedConversations.createIndex({ hash: 1 }, { unique: true }); }); diff --git a/src/lib/types/AbortedGeneration.ts b/src/lib/types/AbortedGeneration.ts new file mode 100644 index 00000000000..76eb83eeb36 --- /dev/null +++ b/src/lib/types/AbortedGeneration.ts @@ -0,0 +1,9 @@ +// Ideally shouldn't be needed, see https://github.com/huggingface/chat-ui/pull/88#issuecomment-1523173850 + +import type { Conversation } from "./Conversation"; + +export interface AbortedGeneration { + createdAt: Date; + updatedAt: Date; + conversationId: Conversation["_id"]; +} diff --git a/src/lib/utils/concatUint8Arrays.ts b/src/lib/utils/concatUint8Arrays.ts new file mode 100644 index 00000000000..e53396eca7e --- /dev/null +++ b/src/lib/utils/concatUint8Arrays.ts @@ -0,0 +1,12 @@ +import { sum } from "./sum"; + +export function concatUint8Arrays(arrays: Uint8Array[]): Uint8Array { + const totalLength = sum(arrays.map((a) => a.length)); + const result = new Uint8Array(totalLength); + let offset = 0; + for (const array of arrays) { + result.set(array, offset); + offset += array.length; + } + return result; +} diff --git a/src/routes/conversation/[id]/+server.ts b/src/routes/conversation/[id]/+server.ts index d1e32cea16c..d079bf86df2 100644 --- a/src/routes/conversation/[id]/+server.ts +++ b/src/routes/conversation/[id]/+server.ts @@ -1,18 +1,21 @@ import { PUBLIC_SEP_TOKEN } from "$env/static/public"; import { buildPrompt } from "$lib/buildPrompt.js"; +import { abortedGenerations } from "$lib/server/abortedGenerations.js"; import { collections } from "$lib/server/database.js"; import { modelEndpoint } from "$lib/server/modelEndpoint.js"; import type { Message } from "$lib/types/Message.js"; +import { concatUint8Arrays } from "$lib/utils/concatUint8Arrays.js"; import { streamToAsyncIterable } from "$lib/utils/streamToAsyncIterable"; -import { sum } from "$lib/utils/sum"; import { trimPrefix } from "$lib/utils/trimPrefix.js"; import { trimSuffix } from "$lib/utils/trimSuffix.js"; +import type { TextGenerationStreamOutput } from "@huggingface/inference"; import { error } from "@sveltejs/kit"; import { ObjectId } from "mongodb"; export async function POST({ request, fetch, locals, params }) { // todo: add validation on params.id const convId = new ObjectId(params.id); + const date = new Date(); const conv = await collections.conversations.findOne({ _id: convId, @@ -31,6 +34,8 @@ export async function POST({ request, fetch, locals, params }) { const randomEndpoint = modelEndpoint(); + const abortController = new AbortController(); + const resp = await fetch(randomEndpoint.endpoint, { headers: { "Content-Type": request.headers.get("Content-Type") ?? "application/json", @@ -41,12 +46,13 @@ export async function POST({ request, fetch, locals, params }) { ...json, inputs: prompt, }), + signal: abortController.signal, }); const [stream1, stream2] = resp.body!.tee(); async function saveMessage() { - let generated_text = await parseGeneratedText(stream2); + let generated_text = await parseGeneratedText(stream2, convId, date, abortController); // We could also check if PUBLIC_ASSISTANT_MESSAGE_TOKEN is present and use it to slice the text if (generated_text.startsWith(prompt)) { @@ -97,19 +103,41 @@ export async function DELETE({ locals, params }) { return new Response(); } -async function parseGeneratedText(stream: ReadableStream): Promise { +async function parseGeneratedText( + stream: ReadableStream, + conversationId: ObjectId, + promptedAt: Date, + abortController: AbortController +): Promise { const inputs: Uint8Array[] = []; for await (const input of streamToAsyncIterable(stream)) { inputs.push(input); + + const date = abortedGenerations.get(conversationId.toString()); + + if (date && date > promptedAt) { + abortController.abort("Cancelled by user"); + const completeInput = concatUint8Arrays(inputs); + + const lines = new TextDecoder() + .decode(completeInput) + .split("\n") + .filter((line) => line.startsWith("data:")); + + const tokens = lines.map((line) => { + try { + const json: TextGenerationStreamOutput = JSON.parse(line.slice("data:".length)); + return json.token.text; + } catch { + return ""; + } + }); + return tokens.join(""); + } } // Merge inputs into a single Uint8Array - const completeInput = new Uint8Array(sum(inputs.map((input) => input.length))); - let offset = 0; - for (const input of inputs) { - completeInput.set(input, offset); - offset += input.length; - } + const completeInput = concatUint8Arrays(inputs); // Get last line starting with "data:" and parse it as JSON to get the generated text const message = new TextDecoder().decode(completeInput); diff --git a/src/routes/conversation/[id]/stop-generating/+server.ts b/src/routes/conversation/[id]/stop-generating/+server.ts new file mode 100644 index 00000000000..b27c0ccf2aa --- /dev/null +++ b/src/routes/conversation/[id]/stop-generating/+server.ts @@ -0,0 +1,27 @@ +import { collections } from "$lib/server/database"; +import { error } from "@sveltejs/kit"; +import { ObjectId } from "mongodb"; + +/** + * Ideally, we'd be able to detect the client-side abort, see https://github.com/huggingface/chat-ui/pull/88#issuecomment-1523173850 + */ +export async function POST({ params, locals }) { + const conversationId = new ObjectId(params.id); + + const conversation = await collections.conversations.findOne({ + _id: conversationId, + sessionId: locals.sessionId, + }); + + if (!conversation) { + throw error(404, "Conversation not found"); + } + + await collections.abortedGenerations.updateOne( + { conversationId }, + { $set: { updatedAt: new Date() }, $setOnInsert: { createdAt: new Date() } }, + { upsert: true } + ); + + return new Response(); +} From 89415c8855816b330b4bf93b39affbfdf5c68aa7 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Wed, 26 Apr 2023 13:46:47 +0200 Subject: [PATCH 7/9] =?UTF-8?q?=E2=9C=A8=20Update=20frontend=20to=20call?= =?UTF-8?q?=20abort=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/conversation/[id]/+page.svelte | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/routes/conversation/[id]/+page.svelte b/src/routes/conversation/[id]/+page.svelte index 50711e03d8f..96951b5bb54 100644 --- a/src/routes/conversation/[id]/+page.svelte +++ b/src/routes/conversation/[id]/+page.svelte @@ -56,9 +56,22 @@ for await (const data of response) { pending = false; - if (!data || conversationId !== $page.params.id) break; + if (!data) { + break; + } + + if (conversationId !== $page.params.id) { + fetch(`${base}/conversation/${conversationId}/stop-generating`, { + method: "POST", + }).catch(console.error); + break; + } + if (isAborted) { isAborted = false; + fetch(`${base}/conversation/${conversationId}/stop-generating`, { + method: "POST", + }).catch(console.error); break; } @@ -96,6 +109,7 @@ if (!message.trim()) return; try { + isAborted = false; loading = true; pending = true; From 74119a8ebaf0706d7ab747969a835ef0e6fc49e1 Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Wed, 26 Apr 2023 13:13:22 +0100 Subject: [PATCH 8/9] add abort controller on messages + update to latest hf.js --- package-lock.json | 8 ++++---- package.json | 2 +- src/routes/conversation/[id]/+page.svelte | 10 ++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86884376e92..eff6d80f2b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "chat-ui", "version": "0.0.1", "dependencies": { - "@huggingface/inference": "^2.1.2", + "@huggingface/inference": "^2.1.3", "autoprefixer": "^10.4.14", "date-fns": "^2.29.3", "dotenv": "^16.0.3", @@ -473,9 +473,9 @@ } }, "node_modules/@huggingface/inference": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.1.2.tgz", - "integrity": "sha512-Qb+yVq13a1FVOLaQK0GOwDfp95VfIWP7uR83tAyluSkvpwAHaByVKw4zNGwMl3u3FxoDRn6snNGo54wTWApPzw==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.1.3.tgz", + "integrity": "sha512-H9xL+piy11eMPAaRUeHa6guiyJMsAndjY90mTddbHHzEsx2Iekm4OuYJTF5F6vYcmlJ1BQdT+HYbr+aGSC9YbQ==", "engines": { "node": ">=18" } diff --git a/package.json b/package.json index 85d25f24fd9..dc1b3a94ffd 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ }, "type": "module", "dependencies": { - "@huggingface/inference": "^2.1.2", + "@huggingface/inference": "^2.1.3", "autoprefixer": "^10.4.14", "date-fns": "^2.29.3", "dotenv": "^16.0.3", diff --git a/src/routes/conversation/[id]/+page.svelte b/src/routes/conversation/[id]/+page.svelte index 96951b5bb54..09b5bec3456 100644 --- a/src/routes/conversation/[id]/+page.svelte +++ b/src/routes/conversation/[id]/+page.svelte @@ -29,6 +29,8 @@ async function getTextGenerationStream(inputs: string) { let conversationId = $page.params.id; + const controller = new AbortController(); + const abortSignal = controller.signal; const response = textGenerationStream( { @@ -50,17 +52,24 @@ }, { use_cache: false, + fetch: (url: string, info: any) => + fetch(url, { ...info, signal: abortSignal }).catch((err) => { + console.error(err); + }), } ); for await (const data of response) { pending = false; + if (!data || conversationId !== $page.params.id) break; + if (!data) { break; } if (conversationId !== $page.params.id) { + controller.abort(); fetch(`${base}/conversation/${conversationId}/stop-generating`, { method: "POST", }).catch(console.error); @@ -68,6 +77,7 @@ } if (isAborted) { + controller.abort(); isAborted = false; fetch(`${base}/conversation/${conversationId}/stop-generating`, { method: "POST", From 6635d812f1636926c883c9e5d632ce5b837ff51c Mon Sep 17 00:00:00 2001 From: Adrien Denat Date: Wed, 26 Apr 2023 13:35:23 +0100 Subject: [PATCH 9/9] Revert "add abort controller on messages + update to latest hf.js" This reverts commit 74119a8ebaf0706d7ab747969a835ef0e6fc49e1. --- package-lock.json | 8 ++++---- package.json | 2 +- src/routes/conversation/[id]/+page.svelte | 10 ---------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index eff6d80f2b1..86884376e92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "chat-ui", "version": "0.0.1", "dependencies": { - "@huggingface/inference": "^2.1.3", + "@huggingface/inference": "^2.1.2", "autoprefixer": "^10.4.14", "date-fns": "^2.29.3", "dotenv": "^16.0.3", @@ -473,9 +473,9 @@ } }, "node_modules/@huggingface/inference": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.1.3.tgz", - "integrity": "sha512-H9xL+piy11eMPAaRUeHa6guiyJMsAndjY90mTddbHHzEsx2Iekm4OuYJTF5F6vYcmlJ1BQdT+HYbr+aGSC9YbQ==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@huggingface/inference/-/inference-2.1.2.tgz", + "integrity": "sha512-Qb+yVq13a1FVOLaQK0GOwDfp95VfIWP7uR83tAyluSkvpwAHaByVKw4zNGwMl3u3FxoDRn6snNGo54wTWApPzw==", "engines": { "node": ">=18" } diff --git a/package.json b/package.json index dc1b3a94ffd..85d25f24fd9 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ }, "type": "module", "dependencies": { - "@huggingface/inference": "^2.1.3", + "@huggingface/inference": "^2.1.2", "autoprefixer": "^10.4.14", "date-fns": "^2.29.3", "dotenv": "^16.0.3", diff --git a/src/routes/conversation/[id]/+page.svelte b/src/routes/conversation/[id]/+page.svelte index 09b5bec3456..96951b5bb54 100644 --- a/src/routes/conversation/[id]/+page.svelte +++ b/src/routes/conversation/[id]/+page.svelte @@ -29,8 +29,6 @@ async function getTextGenerationStream(inputs: string) { let conversationId = $page.params.id; - const controller = new AbortController(); - const abortSignal = controller.signal; const response = textGenerationStream( { @@ -52,24 +50,17 @@ }, { use_cache: false, - fetch: (url: string, info: any) => - fetch(url, { ...info, signal: abortSignal }).catch((err) => { - console.error(err); - }), } ); for await (const data of response) { pending = false; - if (!data || conversationId !== $page.params.id) break; - if (!data) { break; } if (conversationId !== $page.params.id) { - controller.abort(); fetch(`${base}/conversation/${conversationId}/stop-generating`, { method: "POST", }).catch(console.error); @@ -77,7 +68,6 @@ } if (isAborted) { - controller.abort(); isAborted = false; fetch(`${base}/conversation/${conversationId}/stop-generating`, { method: "POST",