use abort signal

This commit is contained in:
Josh Hawkins 2026-04-09 09:07:15 -05:00
parent cfa5354c50
commit b1b43884b8

View File

@ -25,6 +25,7 @@ export async function streamChatCompletion(
headers: Record<string, string>, headers: Record<string, string>,
apiMessages: { role: string; content: string }[], apiMessages: { role: string; content: string }[],
callbacks: StreamChatCallbacks, callbacks: StreamChatCallbacks,
signal?: AbortSignal,
): Promise<void> { ): Promise<void> {
const { const {
updateMessages, updateMessages,
@ -38,6 +39,7 @@ export async function streamChatCompletion(
method: "POST", method: "POST",
headers, headers,
body: JSON.stringify({ messages: apiMessages, stream: true }), body: JSON.stringify({ messages: apiMessages, stream: true }),
signal,
}); });
if (!res.ok) { if (!res.ok) {
@ -152,11 +154,15 @@ export async function streamChatCompletion(
return next; return next;
}); });
} }
} catch { } catch (err) {
onError(defaultErrorMessage); if (err instanceof DOMException && err.name === "AbortError") {
updateMessages((prev) => // User stopped generation — not an error
prev.filter((m) => !(m.role === "assistant" && m.content === "")), } else {
); onError(defaultErrorMessage);
updateMessages((prev) =>
prev.filter((m) => !(m.role === "assistant" && m.content === "")),
);
}
} finally { } finally {
onDone(); onDone();
} }