Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm_chat): keep a tool call waiting until its answer reaches the turn
Elian Doran347c0c60391d
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.tsxindex 7e1e15785c..f7d333aa47 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@@ -504,6 +504,74 @@ describe("useLlmChat", () => {expect(answers.c2).not.toHaveBeenCalled();});+ it("keeps a tool call waiting until the turn receives its answer, so a failed answer can be sent again", async () => {+ type Answer = (approved: boolean, scope?: "chat") => Promise<void>;+ let callbacks: {+ onToolApprovalRequest: (request: { toolCallId: string; toolName: string; toolInput: Record<string, unknown> }, answer: Answer) => void;+ } | undefined;+ let finish: () => void = () => undefined;+ streamChatCompletionMock.mockImplementation(async (_messages, _options, cb) => {+ callbacks = cb;+ await new Promise<void>(resolve => { finish = resolve; });+ cb.onDone();+ });+ let failSend: (error: Error) => void = () => undefined;+ const answers = {+ c1: vi.fn<Answer>()+ .mockImplementationOnce(() => new Promise<void>((_resolve, reject) => { failSend = reject; }))+ .mockResolvedValue(undefined),+ c2: vi.fn<Answer>(async () => {})+ };+ const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);+ await mountChat();+ await act(async () => {+ api().setInput("rename them");+ });+ await act(async () => {+ void api().handleSubmit(new Event("submit"));+ });+ await act(async () => {+ callbacks?.onToolApprovalRequest({ toolCallId: "c1", toolName: "rename_note", toolInput: {} }, answers.c1);+ callbacks?.onToolApprovalRequest({ toolCallId: "c2", toolName: "rename_note", toolInput: {} }, answers.c2);+ });++ // While the answer is on its way, the call still waits and a second click sends nothing.+ await act(async () => {+ api().answerToolCall("c1", true, "chat");+ api().answerToolCall("c1", true, "chat");+ });+ expect(answers.c1).toHaveBeenCalledOnce();+ expect(api().streamingBlocks).toMatchObject([+ { toolCall: { id: "c1", approval: "pending" } },+ { toolCall: { id: "c2", approval: "pending" } }+ ]);++ await act(async () => {+ failSend(new Error("offline"));+ });+ expect(api().streamingBlocks).toMatchObject([+ { toolCall: { id: "c1", approval: "pending" } },+ { toolCall: { id: "c2", approval: "pending" } }+ ]);+ expect(api().allowedTools).toEqual([]);++ await act(async () => {+ api().answerToolCall("c1", true, "chat");+ });+ expect(answers.c1).toHaveBeenCalledTimes(2);+ expect(answers.c2).not.toHaveBeenCalled();+ expect(api().streamingBlocks).toMatchObject([+ { toolCall: { id: "c1", approval: undefined } },+ { toolCall: { id: "c2", approval: undefined } }+ ]);+ expect(api().allowedTools).toEqual([ "rename_note" ]);++ await act(async () => {+ finish();+ });+ consoleError.mockRestore();+ });+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.tsindex 475dc87517..bd67b96c88 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts@@ -89,10 +89,12 @@ function stripQuoteSourcesFromApiContent(content: string | LlmMessagePart[]): st/** A tool call of the running turn that waits for the user. */interface WaitingToolCall {toolName: string;- /** Send the user's answer to the turn. */- answer: (approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => void;- /** Stop waiting on a call the turn approved itself, without answering it. */- release: () => void;+ /** Send the user's answer to the turn; resolves once the turn has received it. */+ 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;}export interface LlmChatOptions {@@ -849,20 +851,14 @@ export function useLlmChat(};contentBlocks.push({ type: "tool_call", toolCall });}- const settle = (approved: boolean) => {- toolApprovalAnswersRef.current.delete(toolCallId);- updateToolCall(toolCallId, { approval: approved ? undefined : "declined" });- setTargetBlocks([...contentBlocks]);- };toolApprovalAnswersRef.current.set(toolCallId, {toolName,- answer: (approved, scope) => {- settle(approved);- answer(approved, scope).catch((e: unknown) => {- console.error("Failed to answer the tool call:", e);- });- },- release: () => settle(true)+ send: answer,+ settle: (approved) => {+ toolApprovalAnswersRef.current.delete(toolCallId);+ updateToolCall(toolCallId, { approval: approved ? undefined : "declined" });+ setTargetBlocks([...contentBlocks]);+ }});setTargetBlocks([...contentBlocks]);},@@ -1014,20 +1010,31 @@ export function useLlmChat(}, [handleSubmit]);/**- * Allow or deny a tool call of the running turn that waits for the user. 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.+ * 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.*/- const answerToolCall = useCallback((toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => {+ const answerToolCall = useCallback(async (toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => {const waiting = toolApprovalAnswersRef.current.get(toolCallId);- if (!waiting) return;- waiting.answer(approved, scope);+ if (!waiting || waiting.sending) return;+ waiting.sending = true;+ try {+ await waiting.send(approved, scope);+ } catch (e) {+ waiting.sending = false;+ console.error("Failed to answer the tool call:", e);+ return;+ }+ 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.release();+ other.settle(true);}}}, []);