mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-07-15 08:21:16 +03:00
use abort signal
This commit is contained in:
parent
cfa5354c50
commit
b1b43884b8
@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user