Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm_chat): keep an answer the reply ended before acknowledging

Elian Doran1a2a3ebcb1a4
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx
index f7d333aa47..66f38d697e 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx
@@ -572,6 +572,50 @@ describe("useLlmChat", () => {
consoleError.mockRestore();
});
+ it("keeps the answers on their way when the reply ends before they arrive", async () => {
+ type Answer = (approved: boolean, scope?: "chat") => Promise<void>;
+ let callbacks: {
+ onToolApprovalRequest: (request: { toolCallId: string; toolName: string; toolInput: Record<string, unknown> }, answer: Answer) => void;
+ onToolResult: (toolCallId: string, toolName: string, result: string, isError?: boolean) => void;
+ } | undefined;
+ let finish: () => void = () => undefined;
+ streamChatCompletionMock.mockImplementation(async (_messages, _options, cb) => {
+ callbacks = cb;
+ await new Promise<void>(resolve => { finish = resolve; });
+ cb.onDone();
+ });
+ const delivered: (() => void)[] = [];
+ const answer = vi.fn<Answer>(() => new Promise<void>(resolve => { delivered.push(resolve); }));
+ await mountChat();
+ await act(async () => {
+ api().setInput("tidy up");
+ });
+ await act(async () => {
+ void api().handleSubmit(new Event("submit"));
+ });
+ await act(async () => {
+ callbacks?.onToolApprovalRequest({ toolCallId: "c1", toolName: "delete_note", toolInput: {} }, answer);
+ callbacks?.onToolApprovalRequest({ toolCallId: "c2", toolName: "rename_note", toolInput: {} }, answer);
+ });
+
+ await act(async () => {
+ api().answerToolCall("c1", false);
+ api().answerToolCall("c2", true, "chat");
+ callbacks?.onToolResult("c1", "delete_note", JSON.stringify({ error: "declined" }), true);
+ callbacks?.onToolResult("c2", "rename_note", "renamed");
+ finish();
+ });
+ await act(async () => {
+ for (const resolve of delivered) resolve();
+ });
+
+ expect(api().allowedTools).toEqual([ "rename_note" ]);
+ expect(api().messages.at(-1)?.content).toMatchObject([
+ { toolCall: { id: "c1", approval: "declined" } },
+ { toolCall: { id: "c2", approval: undefined, result: "renamed" } }
+ ]);
+ });
+
it("allows a tool for the chat: releases its waiting calls, sends and saves the list, and revokes it", async () => {
type Answer = (approved: boolean, scope?: "chat") => Promise<void>;
let callbacks: {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
index bd67b96c88..557024fa18 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
@@ -93,8 +93,8 @@ interface WaitingToolCall {
send: (approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => Promise<void>;
/** Stop waiting on the call, showing it declined or not. */
settle: (approved: boolean) => void;
- /** Whether an answer is on its way, so a second click sends nothing. */
- sending?: boolean;
+ /** The answer on its way to the turn, if any; while it is, a second click sends nothing. */
+ answering?: boolean;
}
export interface LlmChatOptions {
@@ -709,10 +709,18 @@ export function useLlmChat(
// the finalized message.
smoothDrain();
+ // The reply can end before the turn acknowledges an answer it already acted on.
+ for (const [toolCallId, waiting] of toolApprovalAnswersRef.current) {
+ if (waiting.answering !== undefined) {
+ updateToolCall(toolCallId, { approval: waiting.answering ? undefined : "declined" });
+ }
+ }
+
// Mark any in-progress tool calls as stopped so they don't show infinite spinners.
// Also clear `inputStreaming` so a half-streamed JSON arg list doesn't render.
for (const [i, block] of contentBlocks.entries()) {
- if (block.type === "tool_call" && !block.toolCall.result) {
+ if (block.type !== "tool_call") continue;
+ if (!block.toolCall.result) {
contentBlocks[i] = {
type: "tool_call",
toolCall: {
@@ -723,6 +731,8 @@ export function useLlmChat(
approval: undefined
}
};
+ } else if (block.toolCall.approval === "pending") {
+ contentBlocks[i] = { type: "tool_call", toolCall: { ...block.toolCall, approval: undefined } };
}
}
@@ -1013,25 +1023,30 @@ export function useLlmChat(
* Allow or deny a tool call of the running turn that waits for the user. The call keeps waiting
* until the turn has received the answer, so an answer that fails to send can be given again.
* Allowed for the chat, its tool is added to `allowedTools`, and the other calls of it that wait
- * stop waiting, since the turn approves them itself.
+ * stop waiting, since the turn approves them itself. When the reply ended while the answer was on
+ * its way, `finalizeStream()` already saved the call with it, and only `allowedTools` changes.
*/
const answerToolCall = useCallback(async (toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => {
const waiting = toolApprovalAnswersRef.current.get(toolCallId);
- if (!waiting || waiting.sending) return;
- waiting.sending = true;
+ if (!waiting || waiting.answering !== undefined) return;
+ const chatNoteId = chatNoteIdRef.current;
+ waiting.answering = approved;
try {
await waiting.send(approved, scope);
} catch (e) {
- waiting.sending = false;
+ waiting.answering = undefined;
console.error("Failed to answer the tool call:", e);
return;
}
+
+ if (approved && scope === "chat" && chatNoteIdRef.current === chatNoteId) {
+ setAllowedTools(tools => (tools.includes(waiting.toolName) ? tools : [ ...tools, waiting.toolName ]));
+ }
if (toolApprovalAnswersRef.current.get(toolCallId) !== waiting) return;
waiting.settle(approved);
if (!approved || scope !== "chat") return;
- setAllowedTools(tools => (tools.includes(waiting.toolName) ? tools : [ ...tools, waiting.toolName ]));
for (const other of [ ...toolApprovalAnswersRef.current.values() ]) {
if (other.toolName === waiting.toolName) {
other.settle(true);