Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): say the reply is still working when it pauses mid-stream

Elian Doran652b9d16a6a5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 1615a4499a..1963741840 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2657,7 +2657,8 @@
"attachment_unsupported_type": "\"{{name}}\" isn't a supported attachment type. Use an image (PNG, JPEG, GIF, WebP), a PDF, or a text/source file.",
"stream_status": {
"starting_agent": "Starting the agent. This can take a while.",
- "waiting_for_reply": "Waiting for the reply…"
+ "waiting_for_reply": "Waiting for the reply…",
+ "still_working": "Still working…"
},
"show_tool_call_details": "Show input and output",
"tool_call_no_result": "No result yet.",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css
index 950a930009..f84db9310a 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.css
@@ -78,6 +78,11 @@
&.chat-stream-status-waiting {
animation: chat-stream-status-appear 0.2s ease-out 0.6s backwards;
}
+
+ &.chat-stream-status-idle {
+ margin-top: 0.5rem;
+ animation: chat-stream-status-appear 0.2s ease-out;
+ }
}
@keyframes chat-stream-status-appear {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx
index 56b0ef15de..906885584c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx
@@ -1,6 +1,6 @@
import { createRef, render } from "preact";
import { act } from "preact/test-utils";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../../../services/i18n.js", () => ({
t: (key: string) => key
@@ -23,11 +23,13 @@ afterEach(() => {
}
});
+let chat: UseLlmChatReturn | undefined;
+
/** Render the list for a streaming turn with nothing stored yet. */
function renderStreamingTurn(streamingBlocks: ContentBlock[], streamingStatus: string | null = "starting_agent") {
host = document.body.appendChild(document.createElement("div"));
const target = host;
- const chat = {
+ chat = {
messages: [],
isStreaming: true,
streamingStatus,
@@ -40,10 +42,18 @@ function renderStreamingTurn(streamingBlocks: ContentBlock[], streamingStatus: s
showScrollToBottom: false,
scrollToBottom: () => undefined
} as unknown as UseLlmChatReturn;
- act(() => render(<ChatMessageList chat={chat} emptyStateText="empty" />, target));
+ act(() => render(<ChatMessageList chat={chat as UseLlmChatReturn} emptyStateText="empty" />, target));
return target;
}
+/** Render the turn `renderStreamingTurn` started again, with the blocks it has streamed since. */
+function rerenderStreamingTurn(streamingBlocks: ContentBlock[]) {
+ const target = host;
+ if (!target || !chat) throw new Error("No streaming turn rendered");
+ chat = { ...chat, streamingBlocks };
+ act(() => render(<ChatMessageList chat={chat as UseLlmChatReturn} emptyStateText="empty" />, target));
+}
+
describe("ChatMessageList stream status", () => {
it("names what the turn waits on until the reply's first content replaces it", () => {
const waiting = renderStreamingTurn([]);
@@ -63,3 +73,34 @@ describe("ChatMessageList stream status", () => {
expect(row?.classList.contains("chat-stream-status-waiting")).toBe(true);
});
});
+
+describe("ChatMessageList idle stream", () => {
+ beforeEach(() => vi.useFakeTimers());
+ afterEach(() => vi.useRealTimers());
+
+ const idleRow = (target: HTMLElement) => target.querySelector(".chat-stream-status");
+
+ it("says it is still working once the reply stops changing, until it moves again", () => {
+ const target = renderStreamingTurn([ { type: "text", content: "Hello" } ], null);
+ act(() => { vi.advanceTimersByTime(1900); });
+ expect(idleRow(target)).toBeNull();
+
+ act(() => { vi.advanceTimersByTime(100); });
+ expect(idleRow(target)?.textContent).toBe("llm_chat.stream_status.still_working");
+
+ rerenderStreamingTurn([ { type: "text", content: "Hello there" } ]);
+ expect(idleRow(target)).toBeNull();
+ });
+
+ it("leaves a thought or a running tool to show its own spinner", () => {
+ const thinking = renderStreamingTurn([ { type: "thinking", content: "Hmm" } ], null);
+ act(() => { vi.advanceTimersByTime(5000); });
+ expect(thinking.querySelector(".llm-chat-thinking-live")).not.toBeNull();
+ expect(idleRow(thinking)).toBeNull();
+
+ rerenderStreamingTurn([ { type: "tool_call", toolCall: { id: "1", toolName: "web_search", input: { query: "x" } } } ]);
+ act(() => { vi.advanceTimersByTime(5000); });
+ expect(thinking.querySelector(".llm-chat-tool-call")).not.toBeNull();
+ expect(idleRow(thinking)).toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx
index e79e64bd81..e77dc8dbe7 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx
@@ -1,15 +1,18 @@
import "./ChatMessageList.css";
-import { useMemo } from "preact/hooks";
+import { useEffect, useMemo, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import ActionButton from "../../react/ActionButton.js";
import LoadingSpinner from "../../react/LoadingSpinner.js";
import NoItems from "../../react/NoItems.js";
import ChatMessage from "./ChatMessage.js";
-import type { StoredMessage } from "./llm_chat_types.js";
+import type { ContentBlock, StoredMessage } from "./llm_chat_types.js";
import type { UseLlmChatReturn } from "./useLlmChat.js";
+/** How long a streaming reply goes without changing before the list says it is still working. */
+const STREAM_IDLE_MS = 2000;
+
interface ChatMessageListProps {
/** The chat hook result. */
chat: UseLlmChatReturn;
@@ -26,6 +29,7 @@ interface ChatMessageListProps {
*/
export default function ChatMessageList({ chat, emptyStateText, className }: ChatMessageListProps) {
const { messages, isStreaming, retryLast } = chat;
+ const isStreamIdle = useStreamIdle(chat.streamingBlocks, isStreaming);
// Rebuilt only when the timeline itself changes: renders caused by anything else
// (streaming commits, toggles) skip the O(messages) vnode allocation and the
@@ -73,6 +77,12 @@ export default function ChatMessageList({ chat, emptyStateText, className }: Cha
isStreaming
/>
)}
+ {isStreaming && streamingMessage && isStreamIdle && !showsOwnProgress(chat.streamingBlocks) && (
+ <div className="chat-stream-status chat-stream-status-idle" role="status">
+ <LoadingSpinner />
+ {t("llm_chat.stream_status.still_working")}
+ </div>
+ )}
<div ref={chat.messagesEndRef} className="chat-messages-end" aria-hidden="true" />
<div ref={chat.bottomSpacerRef} className="chat-bottom-spacer" aria-hidden="true" />
</div>
@@ -88,3 +98,21 @@ export default function ChatMessageList({ chat, emptyStateText, className }: Cha
</div>
);
}
+
+/** Whether `blocks` has gone {@link STREAM_IDLE_MS} without changing while the turn streams. */
+function useStreamIdle(blocks: ContentBlock[], isStreaming: boolean) {
+ const [isIdle, setIsIdle] = useState(false);
+ useEffect(() => {
+ setIsIdle(false);
+ if (!isStreaming) return;
+ const timer = setTimeout(() => setIsIdle(true), STREAM_IDLE_MS);
+ return () => clearTimeout(timer);
+ }, [blocks, isStreaming]);
+ return isIdle;
+}
+
+/** A thought being streamed and a tool call awaiting its result each show a spinner of their own. */
+function showsOwnProgress(blocks: ContentBlock[]) {
+ const last = blocks.at(-1);
+ return last?.type === "thinking" || (last?.type === "tool_call" && !last.toolCall.result);
+}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
index 8a67175809..33704cb5bc 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
@@ -211,6 +211,9 @@ class="admonition note">
AI sent to a tool and what it got back, hover the line and press the <span
class="tn-icon bx bx-code-alt"></span> button next to it; on touch screens
the button is always shown.</p>
+<p>When the reply stops moving for a couple of seconds while the AI is still
+ at work, for example while it reads what a tool returned, a <em>Still working…</em> line
+ appears under it until the next part of the reply arrives.</p>
<aside class="admonition warning">
<p>Currently there is <strong>no permission management</strong> implemented
for note tools, meaning that the LLM could potentially remove existing
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index 25f4057841..3930c94866 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -135,6 +135,8 @@ Here are a few tools that Trilium provides for the LLM:
Each tool the AI uses appears in the chat as a single line naming the tool and the note or query it worked on; a renamed note shows its previous title struck through, a deleted note its title struck through, a moved note where it was moved from and to, an attribute the AI read, set or deleted as a pill (struck through once deleted), an attachment by its title, note and size, and a web page the AI read as a link. A line folds open only when there is something to read inline: the notes a search found, the pages a web search found, the icons an icon search found, the type, attributes or start of the content of a note the AI read, all the attributes of a note, the text of an attachment or of a web page, the User Guide pages the AI looked up or its table of contents (both open in the help panel), the children or the whole subtree of a note, what the AI wrote into a note it created, rewrote or added to, the changes of an edit, or the reason a tool failed. A search also shows how many notes it found, the part of the tree it was confined to, if any, and how many of the matches the AI asked for; each note it lists opens with a click. To see exactly what the AI sent to a tool and what it got back, hover the line and press the <span class="tn-icon bx bx-code-alt"></span> button next to it; on touch screens the button is always shown.
+When the reply stops moving for a couple of seconds while the AI is still at work, for example while it reads what a tool returned, a _Still working…_ line appears under it until the next part of the reply arrives.
+
> [!WARNING]
> Currently there is **no permission management** implemented for note tools, meaning that the LLM could potentially remove existing notes or clutter the tree with notes. Generally most actions are easily reversible (deleting the notes, restoring deleted notes, reverting modifications to a note), but there are some that are harder to revert (e.g. setting an attribute because there is no attribute history).