This commit is contained in:
Nicolas Mowen
2026-12-03 07:51:43 -07:00
parent a74b1e9f55
commit 0143b2d831
2 changed files with 84 additions and 4 deletions
+63
View File
@@ -411,6 +411,69 @@ test.describe("Chat — stop @medium", () => {
});
});
test.describe("Chat — new chat @medium", () => {
test("New chat during a tool-call turn stays cleared", async ({
frigateApp,
}) => {
const toolTurn = [
{ role: "system", content: "sys" },
{ role: "user", content: "find people" },
{
role: "assistant",
content: null,
tool_calls: [
{
id: "call_1",
type: "function",
function: {
name: "search_objects",
arguments: '{"label":"person"}',
},
},
],
},
{ role: "tool", tool_call_id: "call_1", content: "[]" },
];
// The mocked stream ignores the abort signal, so its remaining chunks
// and onDone still arrive after New chat: the stale request must not
// write them into the cleared chat.
await installChatStreamOverride(
frigateApp,
[
{ type: "messages", messages: toolTurn },
{ type: "content", delta: "Still answering" },
{
type: "messages",
messages: [
...toolTurn,
{ role: "assistant", content: "Still answering" },
],
},
{ type: "done" },
],
{ chunkDelayMs: 400 },
);
await frigateApp.goto("/chat");
const input = frigateApp.page.getByPlaceholder(/ask/i);
await expect(input).toBeVisible({ timeout: 10_000 });
await input.fill("find people");
await input.press("Enter");
await expect(frigateApp.page.getByText(/search objects/i)).toBeVisible({
timeout: 10_000,
});
await frigateApp.page.getByRole("button", { name: "New chat" }).click();
// Outlast the remaining chunks, then confirm nothing came back.
await frigateApp.page.waitForTimeout(1_500);
await expect(
frigateApp.page.getByRole("heading", { level: 1 }),
).toBeVisible();
await expect(frigateApp.page.getByText(/search objects/i)).toHaveCount(0);
await expect(frigateApp.page.getByText("find people")).toHaveCount(0);
});
});
test.describe("Chat — error @medium", () => {
test("non-OK response renders an error banner", async ({ frigateApp }) => {
await installChatStreamOverride(frigateApp, [], { status: 500 });
+21 -4
View File
@@ -187,20 +187,29 @@ export default function ChatPage() {
});
};
// This request owns the chat until New chat or a newer request replaces
// abortRef. Callbacks from a request that lost ownership (including the
// onDone that follows an abort) must not touch the current chat.
const owns = () => abortRef.current === controller;
await streamChatCompletion(
url,
headers,
toWire(messagesToSend),
{
onContentDelta: (delta) =>
setStreaming((s) => (s ? { ...s, content: s.content + delta } : s)),
onContentDelta: (delta) => {
if (!owns()) return;
setStreaming((s) => (s ? { ...s, content: s.content + delta } : s));
},
onReasoningDelta: (delta) => {
if (!owns()) return;
segmentReasoning += delta;
setStreaming((s) =>
s ? { ...s, reasoning: s.reasoning + delta } : s,
);
},
onChain: (fullChain) => {
if (!owns()) return;
// Credit the current call's reasoning to the newest assistant
// message added since the last chain, then start a new segment.
for (let i = fullChain.length - 1; i >= seenChainLength; i--) {
@@ -231,6 +240,7 @@ export default function ChatPage() {
);
},
onStats: (s) => {
if (!owns()) return;
segmentStats = s;
setStreaming((cur) => (cur ? { ...cur, stats: s } : cur));
},
@@ -238,10 +248,12 @@ export default function ChatPage() {
approvals = toolCalls;
},
onError: (message) => {
if (!owns()) return;
hadError = true;
setError(message);
},
onDone: () => {
if (!owns()) return;
abortRef.current = null;
loadingRef.current = false;
setIsLoading(false);
@@ -358,8 +370,10 @@ export default function ChatPage() {
);
const stopGeneration = useCallback(() => {
// Keep abortRef pointing at the stopped request so its onDone can still
// commit the partial chain. A message sent before that onDone fires
// replaces abortRef, and the stale onDone then does nothing.
abortRef.current?.abort();
abortRef.current = null;
loadingRef.current = false;
setIsLoading(false);
setStreaming(null);
@@ -368,8 +382,11 @@ export default function ChatPage() {
}, []);
const startNewChat = useCallback(() => {
abortRef.current?.abort();
// Give up ownership before aborting so the aborted request's onDone
// cannot restore the conversation being cleared.
const controller = abortRef.current;
abortRef.current = null;
controller?.abort();
loadingRef.current = false;
setIsLoading(false);
setStreaming(null);