From a74b1e9f5536158e6b8969af443956213158e661 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 2 Dec 2026 16:58:38 -0700 Subject: [PATCH] fix chat reasoning not splitting after tool calls --- frigate/api/chat.py | 12 +++ web/e2e/specs/chat.spec.ts | 104 ++++++++++++++++++++ web/src/components/chat/ReasoningBubble.tsx | 6 +- web/src/pages/Chat.tsx | 91 +++++++++++++---- 4 files changed, 193 insertions(+), 20 deletions(-) diff --git a/frigate/api/chat.py b/frigate/api/chat.py index 80c065e55f..8749302b26 100644 --- a/frigate/api/chat.py +++ b/frigate/api/chat.py @@ -1667,6 +1667,18 @@ async def chat_completion( yield _emit_chain() yield _emit_chain() + if stream_iterations >= max_iterations: + # Without this the turn ends on tool results with no answer, + # and the client has nothing to tell the user. + logger.warning( + "Max tool iterations (%d) reached in chat stream", max_iterations + ) + yield _emit( + { + "type": "error", + "error": "I reached the maximum number of tool call iterations. Please try rephrasing your question.", + } + ) yield _emit({"type": "done"}) return StreamingResponse( diff --git a/web/e2e/specs/chat.spec.ts b/web/e2e/specs/chat.spec.ts index 956bf15ccd..f1ddf117c8 100644 --- a/web/e2e/specs/chat.spec.ts +++ b/web/e2e/specs/chat.spec.ts @@ -219,6 +219,110 @@ test.describe("Chat — streaming @medium", () => { ).toBeVisible({ timeout: 5_000 }); }); + test("each model call's reasoning renders with its own message", 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: "[]" }, + ]; + await installChatStreamOverride(frigateApp, [ + { type: "reasoning", delta: "first thought" }, + { type: "messages", messages: toolTurn }, + { type: "reasoning", delta: "second thought" }, + { type: "content", delta: "No people found." }, + { + type: "messages", + messages: [ + ...toolTurn, + { role: "assistant", content: "No people found." }, + ], + }, + { type: "done" }, + ]); + 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("No people found.")).toBeVisible({ + timeout: 10_000, + }); + // Two separate, collapsed reasoning bubbles; expanding both shows each + // trace on its own rather than one concatenated block. + const toggles = frigateApp.page.getByRole("button", { + name: /show reasoning/i, + }); + await expect(toggles).toHaveCount(2); + // Expanding one relabels it to "Hide reasoning", so click the first + // remaining "Show" toggle twice. + await toggles.first().click(); + await expect(toggles).toHaveCount(1); + await toggles.first().click(); + await expect( + frigateApp.page.locator("pre", { hasText: /^first thought$/ }), + ).toHaveCount(1); + await expect( + frigateApp.page.locator("pre", { hasText: /^second thought$/ }), + ).toHaveCount(1); + }); + + test("a turn that ends without a final answer keeps its tool calls", 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: "[]" }, + ]; + await installChatStreamOverride(frigateApp, [ + { type: "messages", messages: toolTurn }, + { type: "messages", messages: toolTurn }, + { type: "error", error: "iteration limit" }, + { type: "done" }, + ]); + 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.getByRole("alert").filter({ hasText: /iteration limit/ }), + ).toBeVisible({ timeout: 10_000 }); + await expect(frigateApp.page.getByText(/search objects/i)).toBeVisible(); + }); + test("assistant markdown renders only same-origin images", async ({ frigateApp, }) => { diff --git a/web/src/components/chat/ReasoningBubble.tsx b/web/src/components/chat/ReasoningBubble.tsx index 07dc7f5bec..9e15d1f48f 100644 --- a/web/src/components/chat/ReasoningBubble.tsx +++ b/web/src/components/chat/ReasoningBubble.tsx @@ -34,9 +34,10 @@ export function ReasoningBubble({ const { t } = useTranslation(["views/chat"]); // Open while the model is still mid-thought (no answer tokens yet); // once the answer begins, collapse on its own but let the user reopen. - const [open, setOpen] = useState(true); + // Reasoning that is already finished on mount starts collapsed. + const [open, setOpen] = useState(!answerStarted); const userInteractedRef = useRef(false); - const lastAutoState = useRef(true); + const lastAutoState = useRef(!answerStarted); useEffect(() => { if (userInteractedRef.current) return; @@ -66,6 +67,7 @@ export function ReasoningBubble({