Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm_chat): allow a tool for the rest of a chat

Elian Doran82bad865464e
diff --git a/apps/client/src/services/llm_chat.spec.ts b/apps/client/src/services/llm_chat.spec.ts
index 889eb4cf01..bd3dbba905 100644
--- a/apps/client/src/services/llm_chat.spec.ts
+++ b/apps/client/src/services/llm_chat.spec.ts
@@ -131,10 +131,12 @@ describe("streamChatCompletion", () => {
await streamChatCompletion(messages, config, cb);
expect(cb.onToolApprovalRequest).toHaveBeenCalledExactlyOnceWith(request, expect.any(Function));
- const answer = cb.onToolApprovalRequest.mock.calls[0][1] as (approved: boolean) => Promise<void>;
+ const answer = cb.onToolApprovalRequest.mock.calls[0][1] as (approved: boolean, scope?: "chat") => Promise<void>;
await answer(false);
const { streamId } = JSON.parse((fetchMock.mock.calls[0] as unknown as [string, RequestInit])[1].body as string);
expect(server.post).toHaveBeenCalledExactlyOnceWith("llm-chat/tool-approval", { streamId, toolCallId: "c1", approved: false });
+ await answer(true, "chat");
+ expect(server.post).toHaveBeenLastCalledWith("llm-chat/tool-approval", { streamId, toolCallId: "c1", approved: true, scope: "chat" });
});
it("rethrows an AbortError from fetch (user stopped)", async () => {
diff --git a/apps/client/src/services/llm_chat.ts b/apps/client/src/services/llm_chat.ts
index e26135dd15..a124166e3a 100644
--- a/apps/client/src/services/llm_chat.ts
+++ b/apps/client/src/services/llm_chat.ts
@@ -69,11 +69,12 @@ export interface StreamCallbacks {
onToolUse?: (toolCallId: string, toolName: string, input: Record<string, unknown>) => void;
/**
* A tool call waits for the user to allow or deny it. `answer` sends the decision, and the call
- * then runs or reports itself declined in a `tool_result`.
+ * then runs or reports itself declined in a `tool_result`. With the `"chat"` scope, the turn
+ * also runs its tool's other calls without asking.
*/
onToolApprovalRequest?: (
request: LlmToolApprovalRequest,
- answer: (approved: boolean) => Promise<void>
+ answer: (approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => Promise<void>
) => void;
onToolResult?: (toolCallId: string, toolName: string, result: string, isError?: boolean) => void;
onCitation?: (citation: LlmCitation) => void;
@@ -312,8 +313,11 @@ async function handleChunk(
break;
case "tool_approval_request": {
const { toolCallId, toolName, toolInput } = chunk;
- callbacks.onToolApprovalRequest?.({ toolCallId, toolName, toolInput }, (approved) =>
- answerToolApproval({ streamId, toolCallId, approved }));
+ callbacks.onToolApprovalRequest?.({ toolCallId, toolName, toolInput }, (approved, scope) => {
+ const answer: LlmToolApprovalAnswer = { streamId, toolCallId, approved };
+ if (scope) answer.scope = scope;
+ return answerToolApproval(answer);
+ });
break;
}
case "tool_result":
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index ad7ba8eef5..f012a15186 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2694,7 +2694,11 @@
"tool_approval_prompt": "Allow this tool call?",
"tool_approval_allow": "Allow",
"tool_approval_deny": "Deny",
- "tool_approval_declined": "denied"
+ "tool_approval_declined": "denied",
+ "tool_approval_allow_for_chat": "Allow for this chat",
+ "allowed_tools": "Allowed in this chat",
+ "allowed_tools_count_one": "{{count}} tool",
+ "allowed_tools_count_other": "{{count}} tools"
},
"sidebar_chat": {
"title": "AI Chat",
diff --git a/apps/client/src/widgets/react/Button.spec.tsx b/apps/client/src/widgets/react/Button.spec.tsx
index d2d5cee005..aab5d2ee1d 100644
--- a/apps/client/src/widgets/react/Button.spec.tsx
+++ b/apps/client/src/widgets/react/Button.spec.tsx
@@ -52,4 +52,16 @@ describe("SplitButton", () => {
expect(calls).toEqual([ "search", "execute" ]);
await vi.waitFor(() => expect(document.querySelector(".tn-popup.tn-menu")).toBeNull());
});
+
+ it("takes the look of a primary or small button on both halves", () => {
+ render((
+ <SplitButton text="Allow" kind="primary" size="small">
+ <FormListItem onClick={() => {}}>Allow always</FormListItem>
+ </SplitButton>
+ ), host);
+ const halves = [ ...host.querySelectorAll<HTMLButtonElement>(".btn-group button") ];
+ expect(halves.map(button => [ button.classList.contains("btn-primary"), button.classList.contains("btn-sm") ]))
+ .toEqual([ [ true, true ], [ true, true ] ]);
+ expect(halves[0]?.classList.contains("btn-secondary")).toBe(false);
+ });
});
diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsx
index 9fab8db1a7..6d72051466 100644
--- a/apps/client/src/widgets/react/Button.tsx
+++ b/apps/client/src/widgets/react/Button.tsx
@@ -130,24 +130,27 @@ export function ButtonGroup({ size, className, children }: { size?: "sm" | "lg";
}
/** A button with the related actions in a menu beside it. */
-export function SplitButton({ text, icon, children, ...restProps }: {
+export function SplitButton({ text, icon, kind = "secondary", size = "normal", children, ...restProps }: {
text: string;
/** The icon's full class, as `"bx bx-search"`. */
icon?: string;
+ kind?: "primary" | "secondary";
+ size?: "normal" | "small";
title?: string;
/** Click handler for the main button component (not the split). */
onClick?: () => void;
/** The menu's rows: `FormListItem`s and the other `FormList` rows. */
children: ComponentChildren;
}) {
+ const look = `btn-${kind}${size === "small" ? " btn-sm" : ""}`;
return (
- <ButtonGroup>
- <button type="button" class="btn btn-secondary" {...restProps}>
+ <ButtonGroup size={size === "small" ? "sm" : undefined}>
+ <button type="button" class={`btn ${look}`} {...restProps}>
{icon && <Icon icon={icon} />}
{text}
</button>
<Dropdown
- buttonClassName="btn-secondary dropdown-toggle-split"
+ buttonClassName={`${look} dropdown-toggle-split`}
buttonProps={{ "aria-label": t("split_button.more_actions") }}
noSelectButtonStyle
placement="bottom-end"
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsx
index 61eb2befc1..e7077a8852 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx
@@ -404,6 +404,7 @@ export default function SidebarChat() {
activeNoteId={activeNoteId ?? undefined}
activeNoteTitle={activeNote?.title}
onSubmit={handleSubmit}
+ onAllowedToolsChange={() => spacedUpdate.scheduleUpdate()}
/>
)}
</div>
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index 3e3437102b..0245403e1c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -68,6 +68,8 @@ interface ChatInputBarProps {
onNoteToolsChange?: () => void;
/** Callback when the extended thinking switch or the reasoning effort changes */
onExtendedThinkingChange?: () => void;
+ /** Callback when a tool allowed for the chat is revoked */
+ onAllowedToolsChange?: () => void;
/** Callback when model changes */
onModelChange?: (model: string) => void;
}
@@ -80,6 +82,7 @@ export default function ChatInputBar({
onWebSearchChange,
onNoteToolsChange,
onExtendedThinkingChange,
+ onAllowedToolsChange,
onModelChange
}: ChatInputBarProps) {
// Provider add/edit modal. `modalProvider` undefined = adding; a config = editing.
@@ -170,6 +173,11 @@ export default function ChatInputBar({
onNoteToolsChange?.();
};
+ const handleRevokeAllowedTool = (toolName: string) => {
+ chat.revokeAllowedTool(toolName);
+ onAllowedToolsChange?.();
+ };
+
const handleExtendedThinkingToggle = (newValue: boolean) => {
chat.setEnableExtendedThinking(newValue);
onExtendedThinkingChange?.();
@@ -464,6 +472,8 @@ export default function ChatInputBar({
webSearch={webSearch}
searchProviders={searchProviders}
onWebSearchChoose={chooseWebSearch}
+ allowedTools={chat.allowedTools}
+ onRevokeAllowedTool={handleRevokeAllowedTool}
disabled={chat.isStreaming}
/>
{!currentModel?.reasoningEfforts?.length && (
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx
index 7b94a76bbe..2d999eaa6a 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx
@@ -66,13 +66,15 @@ afterEach(() => {
const TAVILY = { id: "s1", name: "Tavily", provider: "tavily" };
-function renderMenu({ enableNoteTools = false, modelProvider, webSearch = { choice: "disabled", enableWebSearch: false } }: {
+function renderMenu({ enableNoteTools = false, modelProvider, webSearch = { choice: "disabled", enableWebSearch: false }, allowedTools = [] }: {
enableNoteTools?: boolean;
+ allowedTools?: string[];
modelProvider?: string;
webSearch?: WebSearchState;
} = {}) {
const onNoteToolsChange = vi.fn();
const onWebSearchChoose = vi.fn();
+ const onRevokeAllowedTool = vi.fn();
host = document.body.appendChild(document.createElement("div"));
const target = host;
act(() => render(
@@ -83,6 +85,8 @@ function renderMenu({ enableNoteTools = false, modelProvider, webSearch = { choi
webSearch={webSearch}
searchProviders={[ TAVILY ]}
onWebSearchChoose={onWebSearchChoose}
+ allowedTools={allowedTools}
+ onRevokeAllowedTool={onRevokeAllowedTool}
/>, target));
const rows = [ ...target.querySelectorAll<HTMLElement>(".menu .dropdown-item") ];
@@ -104,13 +108,25 @@ function renderMenu({ enableNoteTools = false, modelProvider, webSearch = { choi
marked: () => rows.filter((candidate) => candidate.getAttribute("aria-checked") === "true" || candidate.querySelector(".bx-check"))
.map((candidate) => candidate.textContent),
onNoteToolsChange,
- onWebSearchChoose
+ onWebSearchChoose,
+ onRevokeAllowedTool
};
}
const isOn = (row: HTMLElement | undefined) => row?.querySelector<HTMLInputElement>("input[type='checkbox']")?.checked;
describe("ChatToolsDropdown", () => {
+ it("lists the tools allowed for the chat, and makes the chat ask about a pressed one again", async () => {
+ expect(renderMenu().toggle?.parentElement?.textContent).not.toContain("llm_chat.allowed_tools");
+
+ const menu = renderMenu({ allowedTools: [ "rename_note", "delete_note" ] });
+ const submenus = [ ...(menu.toggle?.parentElement?.querySelectorAll(".submenu-row") ?? []) ].map(row => row.textContent);
+ expect(submenus).toContain("llm_chat.allowed_toolsllm_chat.allowed_tools_count");
+ expect(menu.marked()).toEqual(expect.arrayContaining([ "llm.tools.rename_note", "llm.tools.delete_note" ]));
+ await act(async () => menu.row("llm.tools.rename_note")?.click());
+ expect(menu.onRevokeAllowedTool).toHaveBeenCalledExactlyOnceWith("rename_note");
+ });
+
it("marks the toggle while note access or a usable web search is on", () => {
const off = renderMenu();
expect(off.toggle?.title).toBe("llm_chat.tools");
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx
index e310ab065e..3fdad107e3 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx
@@ -16,11 +16,12 @@ import type { UseLlmChatReturn } from "./useLlmChat.js";
/**
* The Tools menu of the chat input bar: what the model can reach this turn. Note access is a row of
- * its own; web search is a submenu of its sources, whose row shows the chosen one. Its rows leave
+ * its own; web search is a submenu of its sources, whose row shows the chosen one; the tools allowed
+ * for the chat, once there are any, are a submenu that makes the chat ask about one again. Its rows leave
* the menu open, so several can be changed in one visit. Its toggle shares the compact combo box
* styling of the model picker beside it.
*/
-export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange, modelProvider, webSearch, searchProviders, onWebSearchChoose, disabled }: {
+export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange, modelProvider, webSearch, searchProviders, onWebSearchChoose, allowedTools, onRevokeAllowedTool, disabled }: {
enableNoteTools: boolean;
/** The provider type of the current model, whose mark stands for its built-in search. */
modelProvider: string | undefined;
@@ -29,6 +30,9 @@ export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange,
searchProviders: SearchProviderOption[];
/** Called with `"disabled"`, `"builtin"` or the config id of a search provider. */
onWebSearchChoose: (choice: string) => void;
+ /** The tools whose calls run without asking in this chat. */
+ allowedTools: string[];
+ onRevokeAllowedTool: (toolName: string) => void;
disabled?: boolean;
}) {
const anyActive = enableNoteTools || webSearch.enableWebSearch;
@@ -80,6 +84,26 @@ export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange,
{t("llm_chat.manage_search_providers")}
</FormListItem>
</FormDropdownSubmenu>
+
+ {allowedTools.length > 0 && (
+ <FormDropdownSubmenu
+ icon="bx bx-check-double"
+ title={t("llm_chat.allowed_tools")}
+ description={t("llm_chat.allowed_tools_count", { count: allowedTools.length })}
+ >
+ {allowedTools.map((toolName) => (
+ <FormListItem
+ key={toolName}
+ checkable
+ checked
+ closeOnSelect={false}
+ onClick={() => onRevokeAllowedTool(toolName)}
+ >
+ {t(`llm.tools.${toolName}`, { defaultValue: toolName })}
+ </FormListItem>
+ ))}
+ </FormDropdownSubmenu>
+ )}
</Dropdown>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
index aac377c3bf..fc2b132fb0 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
@@ -91,6 +91,7 @@ export default function LlmChat({ note, noteContext, isVisible }: TypeWidgetProp
onWebSearchChange={triggerSave}
onNoteToolsChange={triggerSave}
onExtendedThinkingChange={triggerSave}
+ onAllowedToolsChange={triggerSave}
onModelChange={triggerSave}
/>
)}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
index fec311a15c..7adef35aa6 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -157,7 +157,7 @@
justify-content: flex-end;
gap: 0.5rem;
- > button.btn.btn-sm {
+ button.btn.btn-sm {
min-width: unset;
}
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
index 28585b7b0c..90dd02a400 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
@@ -818,7 +818,7 @@ describe("ToolCallCard", () => {
const buttons = (target: HTMLElement) =>
[ ...target.querySelectorAll<HTMLButtonElement>(".llm-chat-tool-approval button") ];
- it("shows a waiting call with nothing to preview as its line, and answers it from the buttons below", () => {
+ it("shows a waiting call with nothing to preview as its line, and answers it from the buttons below", async () => {
const answer = vi.fn();
const target = renderCard([ PENDING ], answer);
@@ -828,12 +828,26 @@ describe("ToolCallCard", () => {
expect(line?.querySelector(".llm-chat-tool-call-approval-badge")).toBeNull();
expect(line?.querySelector(".bxs-hand")).not.toBeNull();
- const [ deny, allow ] = buttons(target);
+ const [ deny, allow, more ] = buttons(target);
expect([ deny?.textContent?.trim(), allow?.textContent?.trim() ]).toEqual([ "llm_chat.tool_approval_deny", "llm_chat.tool_approval_allow" ]);
act(() => allow?.click());
expect(answer).toHaveBeenCalledExactlyOnceWith("c1", true);
act(() => deny?.click());
expect(answer).toHaveBeenLastCalledWith("c1", false);
+
+ // The menu of the Allow button allows the tool for the rest of the chat.
+ act(() => {
+ more?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
+ more?.click();
+ });
+ const row = await vi.waitFor(() => {
+ const found = document.querySelector<HTMLElement>(".tn-popup.tn-menu li.dropdown-item");
+ if (!found) throw new Error("expected the menu to open");
+ return found;
+ });
+ expect(row.textContent).toBe("llm_chat.tool_approval_allow_for_chat");
+ act(() => row.click());
+ expect(answer).toHaveBeenLastCalledWith("c1", true, "chat");
});
it("opens a waiting writing call on what it would write", () => {
@@ -842,7 +856,7 @@ describe("ToolCallCard", () => {
} ], vi.fn());
expect(target.querySelector(".code-block-stub")?.textContent).toBe("hello");
expect(target.querySelector<HTMLDetailsElement>("details.llm-chat-tool-call")?.open).toBe(true);
- expect(buttons(target)).toHaveLength(2);
+ expect(buttons(target)).toHaveLength(3);
});
it("keeps a waiting call out of the group of its tool, and offers no buttons outside the streaming reply", () => {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
index a06d8cd76d..b85f3866f1 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -1,5 +1,6 @@
import "./ToolCallCard.css";
+import type { LlmToolApprovalAnswer } from "@triliumnext/commons";
import { createContext } from "preact";
import { useContext } from "preact/hooks";
import { Trans } from "react-i18next";
@@ -7,7 +8,8 @@ import { Trans } from "react-i18next";
import appContext from "../../../components/app_context.js";
import { t } from "../../../services/i18n.js";
import ActionButton from "../../react/ActionButton.js";
-import Button from "../../react/Button.js";
+import Button, { SplitButton } from "../../react/Button.js";
+import { FormListItem } from "../../react/FormList.js";
import MaskedIcon from "../../react/MaskedIcon.js";
import { NewNoteLink } from "../../react/NoteLink.js";
import { providerIconUrl } from "../options/llm/provider_icons.js";
@@ -17,11 +19,13 @@ import { isFailedToolCall, type ToolCall } from "./llm_chat_types.js";
import { ExternalLink, getToolCallView, type ToolCallView } from "./ToolCallViews.js";
/**
- * Allows or denies a tool call of the running turn that waits for the user. Provided only around
- * the reply that streams, since a saved reply has nothing left to answer.
+ * Allows or denies a tool call of the running turn that waits for the user, or allows its tool for
+ * the rest of the chat. Provided only around the reply that streams, since a saved reply has nothing
+ * left to answer.
*/
-export const ToolApprovalContext =
- createContext<((toolCallId: string, approved: boolean) => void) | undefined>(undefined);
+export const ToolApprovalContext = createContext<
+ ((toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => void) | undefined
+>(undefined);
interface ToolCallContext {
/** The primary note the tool operates on or created. */
@@ -440,13 +444,17 @@ function ToolApprovalBar({ toolCallId }: { toolCallId: string }) {
text={t("llm_chat.tool_approval_deny")}
onClick={() => answer(toolCallId, false)}
/>
- <Button
+ <SplitButton
size="small"
kind="primary"
icon="bx bx-check"
text={t("llm_chat.tool_approval_allow")}
onClick={() => answer(toolCallId, true)}
- />
+ >
+ <FormListItem icon="bx bx-check-double" onClick={() => answer(toolCallId, true, "chat")}>
+ {t("llm_chat.tool_approval_allow_for_chat")}
+ </FormListItem>
+ </SplitButton>
</div>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
index dd64e7b816..66bc754013 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts
@@ -206,4 +206,6 @@ export interface LlmChatContent {
enableExtendedThinking?: boolean;
/** The effort chosen for a model with levels; absent means the model's default. */
reasoningEffort?: LlmReasoningEffort;
+ /** The tools the user allowed for this chat, whose calls run without asking. */
+ allowedTools?: string[];
}
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 912273608d..c882861a89 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
@@ -483,7 +483,7 @@ describe("useLlmChat", () => {
api().answerToolCall("c1", true);
callbacks?.onToolResult("c1", "delete_note", JSON.stringify({ error: "declined" }), true);
});
- expect(answers.c1).toHaveBeenCalledExactlyOnceWith(false);
+ expect(answers.c1).toHaveBeenCalledExactlyOnceWith(false, undefined);
expect(api().streamingBlocks[0]).toMatchObject({ toolCall: { id: "c1", approval: "declined" } });
// Stopped while waiting: the call that was never answered is no longer waiting.
@@ -497,4 +497,71 @@ describe("useLlmChat", () => {
api().answerToolCall("c2", true);
expect(answers.c2).not.toHaveBeenCalled();
});
+
+ 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: {
+ 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();
+ });
+ const answers = { c1: vi.fn<Answer>(async () => {}), c2: vi.fn<Answer>(async () => {}), c3: vi.fn<Answer>(async () => {}) };
+ const send = async (text: string) => {
+ await act(async () => {
+ api().setInput(text);
+ });
+ await act(async () => {
+ void api().handleSubmit(new Event("submit"));
+ });
+ };
+ await mountChat();
+ await send("rename them");
+ await act(async () => {
+ callbacks?.onToolApprovalRequest({ toolCallId: "c1", toolName: "rename_note", toolInput: {} }, answers.c1);
+ callbacks?.onToolApprovalRequest({ toolCallId: "c2", toolName: "rename_note", toolInput: {} }, answers.c2);
+ callbacks?.onToolApprovalRequest({ toolCallId: "c3", toolName: "delete_note", toolInput: {} }, answers.c3);
+ });
+
+ await act(async () => {
+ api().answerToolCall("c1", true, "chat");
+ });
+ // The turn approves the other calls of the tool itself, so only the first answer is sent.
+ expect(answers.c1).toHaveBeenCalledExactlyOnceWith(true, "chat");
+ expect(answers.c2).not.toHaveBeenCalled();
+ expect(api().streamingBlocks).toMatchObject([
+ { toolCall: { id: "c1", approval: undefined } },
+ { toolCall: { id: "c2", approval: undefined } },
+ { toolCall: { id: "c3", approval: "pending" } }
+ ]);
+ expect(api().allowedTools).toEqual([ "rename_note" ]);
+ expect(api().getContent().allowedTools).toEqual([ "rename_note" ]);
+
+ await act(async () => {
+ finish();
+ });
+ await send("again");
+ expect(streamChatCompletionMock.mock.calls.at(-1)?.[1]).toMatchObject({ allowedTools: [ "rename_note" ] });
+ await act(async () => {
+ finish();
+ });
+
+ await act(async () => {
+ api().revokeAllowedTool("rename_note");
+ });
+ expect(api().allowedTools).toEqual([]);
+ expect(api().getContent().allowedTools).toBeUndefined();
+
+ await act(async () => {
+ api().loadFromContent({ version: 1, messages: [], allowedTools: [ "get_note", 3 as unknown as string ] });
+ });
+ expect(api().allowedTools).toEqual([ "get_note" ]);
+ await act(async () => {
+ api().loadFromContent({ version: 1, messages: [] });
+ });
+ expect(api().allowedTools).toEqual([]);
+ });
});
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 aee331caa9..f96094eb84 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
@@ -1,4 +1,4 @@
-import { LLM_REASONING_EFFORTS, type LlmCitation, type LlmMessage, type LlmMessagePart, type LlmModelInfo, type LlmReasoningEffort, type LlmStreamStatus, type LlmUsage } from "@triliumnext/commons";
+import { LLM_REASONING_EFFORTS, type LlmCitation, type LlmMessage, type LlmMessagePart, type LlmModelInfo, type LlmReasoningEffort, type LlmStreamStatus, type LlmToolApprovalAnswer, type LlmUsage } from "@triliumnext/commons";
import { RefObject } from "preact";
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
@@ -86,6 +86,15 @@ function stripQuoteSourcesFromApiContent(content: string | LlmMessagePart[]): st
return content.map(part => (part.type === "text" ? { ...part, text: stripQuoteSources(part.text) } : part));
}
+/** 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;
+}
+
export interface LlmChatOptions {
/** Default value for enableNoteTools */
defaultEnableNoteTools?: boolean;
@@ -124,6 +133,8 @@ export interface UseLlmChatReturn {
enableExtendedThinking: boolean;
/** The effort chosen for a model with levels; undefined means the model's default. */
reasoningEffort: LlmReasoningEffort | undefined;
+ /** The tools allowed for this chat, whose calls run without asking. */
+ allowedTools: string[];
contextNoteId: string | undefined;
/** The chat note's ID — used as the upload target for attachments. */
chatNoteId: string | undefined;
@@ -180,8 +191,11 @@ export interface UseLlmChatReturn {
refreshModels: () => void;
/** Stop the current generation */
stopStreaming: () => void;
- /** Allow or deny a tool call of the running turn that waits for the user. */
- answerToolCall: (toolCallId: string, approved: boolean) => void;
+ /** Allow or deny a tool call of the running turn that waits for the user. With the
+ * `"chat"` scope, its tool is allowed for the rest of the chat. */
+ answerToolCall: (toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => void;
+ /** Make the chat ask about a tool it allowed again. */
+ revokeAllowedTool: (toolName: string) => void;
/** Re-run the last turn after a failed response (drops the trailing error message) */
retryLast: () => void;
/** Regenerate the last reply: re-run from the last user message, dropping the reply that followed */
@@ -229,6 +243,7 @@ export function useLlmChat(
const [enableNoteTools, setEnableNoteTools] = useState(defaultEnableNoteTools);
const [enableExtendedThinking, setEnableExtendedThinking] = useState(false);
const [reasoningEffort, setReasoningEffort] = useState<LlmReasoningEffort | undefined>(undefined);
+ const [allowedTools, setAllowedTools] = useState<string[]>([]);
const [contextNoteId, setContextNoteId] = useState<string | undefined>(initialContextNoteId);
const [chatNoteId, setChatNoteIdState] = useState<string | undefined>(initialChatNoteId);
const [lastPromptTokens, setLastPromptTokens] = useState<number>(0);
@@ -242,8 +257,9 @@ export function useLlmChat(
const scrollContainerRef = useRef<HTMLDivElement>(null);
const bottomSpacerRef = useRef<HTMLDivElement>(null);
const abortControllerRef = useRef<AbortController | null>(null);
- // How to answer each tool call of the running turn that waits for the user, by call id.
- const toolApprovalAnswersRef = useRef(new Map<string, (approved: boolean) => void>());
+ // The tool calls of the running turn that wait for the user, by call id: how to answer one,
+ // and how to stop waiting on one the turn approved itself.
+ const toolApprovalAnswersRef = useRef(new Map<string, WaitingToolCall>());
const anchorRafRef = useRef<number | null>(null);
// A one-shot scroll to run after the next messages render: "anchor" parks a fresh
// reply near the top (sending/retrying), "bottom" jumps to the latest content
@@ -270,6 +286,8 @@ export function useLlmChat(
enableWebSearchRef.current = enableWebSearch;
const enableNoteToolsRef = useRef(enableNoteTools);
enableNoteToolsRef.current = enableNoteTools;
+ const allowedToolsRef = useRef(allowedTools);
+ allowedToolsRef.current = allowedTools;
const enableExtendedThinkingRef = useRef(enableExtendedThinking);
enableExtendedThinkingRef.current = enableExtendedThinking;
const reasoningEffortRef = useRef(reasoningEffort);
@@ -545,6 +563,9 @@ export function useLlmChat(
setEnableExtendedThinking(content.enableExtendedThinking);
}
setReasoningEffort(LLM_REASONING_EFFORTS.find(level => level === content.reasoningEffort));
+ setAllowedTools(Array.isArray(content.allowedTools)
+ ? content.allowedTools.filter((name): name is string => typeof name === "string")
+ : []);
// Restore last prompt tokens from the most recent message with usage
const lastUsage = [...(content.messages || [])].reverse().find(m => m.usage)?.usage;
setLastPromptTokens(lastUsage?.promptTokens ?? 0);
@@ -568,6 +589,9 @@ export function useLlmChat(
if (reasoningEffortRef.current) {
content.reasoningEffort = reasoningEffortRef.current;
}
+ if (allowedToolsRef.current.length) {
+ content.allowedTools = allowedToolsRef.current;
+ }
return content;
}, [supportsExtendedThinking]);
@@ -659,6 +683,9 @@ export function useLlmChat(
if (reasoningEffort && matchedModel?.reasoningEfforts?.length) {
streamOptions.reasoningEffort = reasoningEffort;
}
+ if (allowedTools.length) {
+ streamOptions.allowedTools = allowedTools;
+ }
const abortController = new AbortController();
abortControllerRef.current = abortController;
@@ -812,13 +839,20 @@ export function useLlmChat(
};
contentBlocks.push({ type: "tool_call", toolCall });
}
- toolApprovalAnswersRef.current.set(toolCallId, (approved) => {
+ const settle = (approved: boolean) => {
toolApprovalAnswersRef.current.delete(toolCallId);
updateToolCall(toolCallId, { approval: approved ? undefined : "declined" });
setTargetBlocks([...contentBlocks]);
- answer(approved).catch((e: unknown) => {
- console.error("Failed to answer the tool call:", e);
- });
+ };
+ 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)
});
setTargetBlocks([...contentBlocks]);
},
@@ -894,7 +928,7 @@ export function useLlmChat(
setIsStreaming(false);
abortControllerRef.current = null;
});
- }, [selectedModel, selectedProvider, selectedProviderId, availableModels, enableWebSearch, enableNoteTools, enableExtendedThinking, reasoningEffort, contextNoteId, supportsExtendedThinking, setMessages, smoothAppend, smoothDrain, smoothReset]);
+ }, [selectedModel, selectedProvider, selectedProviderId, availableModels, enableWebSearch, enableNoteTools, enableExtendedThinking, reasoningEffort, allowedTools, contextNoteId, supportsExtendedThinking, setMessages, smoothAppend, smoothDrain, smoothReset]);
const handleSubmit = useCallback(async (e: Event) => {
e.preventDefault();
@@ -969,9 +1003,27 @@ export function useLlmChat(
}
}, [handleSubmit]);
- /** Allow or deny a tool call of the running turn that waits for the user. */
- const answerToolCall = useCallback((toolCallId: string, approved: boolean) => {
- toolApprovalAnswersRef.current.get(toolCallId)?.(approved);
+ /**
+ * 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.
+ */
+ const answerToolCall = useCallback((toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]) => {
+ const waiting = toolApprovalAnswersRef.current.get(toolCallId);
+ if (!waiting) return;
+ waiting.answer(approved, scope);
+ 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();
+ }
+ }
+ }, []);
+
+ const revokeAllowedTool = useCallback((toolName: string) => {
+ setAllowedTools(tools => tools.filter(tool => tool !== toolName));
}, []);
/** Stop the current generation by aborting the SSE connection. */
@@ -1015,6 +1067,7 @@ export function useLlmChat(
enableNoteTools,
enableExtendedThinking,
reasoningEffort,
+ allowedTools,
contextNoteId,
chatNoteId,
lastPromptTokens,
@@ -1055,6 +1108,7 @@ export function useLlmChat(
refreshModels,
stopStreaming,
answerToolCall,
+ revokeAllowedTool,
retryLast,
regenerateLastReply
};
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 565aa06cfe..a6163fb658 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
@@ -270,10 +270,17 @@
of an edit. The reply waits until you answer.</p>
<ul>
<li>To let the call run, press <em>Allow</em>.</li>
+ <li>To stop asking about a tool for the rest of the chat, open the menu next
+ to <em>Allow</em> and choose <em>Allow for this chat</em>. The call runs,
+ and so do the other calls of that tool still waiting and every later one
+ in this chat. Other tools keep asking, and a new chat asks again.</li>
<li>To refuse it, press <em>Deny</em>. The AI is told that you declined, and
its reply carries on without the call, which is marked <em>denied</em>.</li>
<li>Stopping the reply denies every call that is still waiting.</li>
</ul>
+<p>To make a chat ask about a tool again, open the <span class="tn-icon bx bx-shield-quarter"></span> <em>Tools</em> menu
+ at the bottom of the chat and, under <em>Allowed in this chat</em>, press
+ the tool. The submenu is shown once the chat has allowed a tool.</p>
<p>Every tool call asks, including the ones that only read: searching notes,
reading a note or an attachment, and reading a web page through a search
provider. A model's built-in web search runs on the provider's side and
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html
index a4c1dec87f..7ccb10c0ab 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html
@@ -67,10 +67,11 @@
<ul>
<li>Tools let the model search your tree and read notes, attributes and attachments
beyond the current one.</li>
- <li>It decides what to fetch, and each call waits for you to allow it (except
- with the <a class="reference-link" href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a>).
- There is no per-note permission system, so the reachable scope is your
- whole tree.</li>
+ <li>It decides what to fetch, and each call waits for you to allow it, unless
+ you allowed that tool for the chat (and except with the <a class="reference-link"
+ href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a>). There
+ is no per-note permission system, so the reachable scope is your whole
+ tree.</li>
</ul>
</li>
<li><strong>Attachments and mentions</strong>, when you add them: images,
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index 4ced1e1242..e164e6c7fd 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -161,9 +161,12 @@ When the reply stops moving for a couple of seconds while the AI is still at wor
Before the AI runs a tool, the chat asks whether to allow it. The tool's line is marked with a <span class="tn-icon bx bxs-hand"></span> icon and has _Allow_ and _Deny_ buttons below it. When the call would write to a note, the line opens on the text it would write or the changes of an edit. The reply waits until you answer.
* To let the call run, press _Allow_.
+* To stop asking about a tool for the rest of the chat, open the menu next to _Allow_ and choose _Allow for this chat_. The call runs, and so do the other calls of that tool still waiting and every later one in this chat. Other tools keep asking, and a new chat asks again.
* To refuse it, press _Deny_. The AI is told that you declined, and its reply carries on without the call, which is marked _denied_.
* Stopping the reply denies every call that is still waiting.
+To make a chat ask about a tool again, open the <span class="tn-icon bx bx-shield-quarter"></span> _Tools_ menu at the bottom of the chat and, under _Allowed in this chat_, press the tool. The submenu is shown once the chat has allowed a tool.
+
Every tool call asks, including the ones that only read: searching notes, reading a note or an attachment, and reading a web page through a search provider. A model's built-in web search runs on the provider's side and does not ask.
> [!IMPORTANT]
diff --git a/docs/User Guide/User Guide/AI/Privacy.md b/docs/User Guide/User Guide/AI/Privacy.md
index f1be3238f5..e03f68fcb1 100644
--- a/docs/User Guide/User Guide/AI/Privacy.md
+++ b/docs/User Guide/User Guide/AI/Privacy.md
@@ -28,7 +28,7 @@ This is the reason the provider list is grouped this way, and why Trilium pre-se
* Notes above a size threshold send a size hint instead of the text, and the model must call a tool to read further.
* **Any note the model asks for, when note access is enabled.**
* Tools let the model search your tree and read notes, attributes and attachments beyond the current one.
- * It decides what to fetch, and each call waits for you to allow it (except with the <a class="reference-link" href="Providers/Subscription-based%20providers.md">Subscription-based providers</a>). There is no per-note permission system, so the reachable scope is your whole tree.
+ * It decides what to fetch, and each call waits for you to allow it, unless you allowed that tool for the chat (and except with the <a class="reference-link" href="Providers/Subscription-based%20providers.md">Subscription-based providers</a>). There is no per-note permission system, so the reachable scope is your whole tree.
* **Attachments and mentions**, when you add them: images, PDFs and text files are sent to the provider, and an `@` mention makes that note fetchable.
* **Your search query**, when web search is enabled, the model's search terms reach the provider's own search infrastructure, or the search provider chosen in the _Tools_ menu (Brave Search, Tavily, Exa, Serper, Perplexity or your SearXNG instance).
* **The pages the model reads**, when web search goes through a search provider: Trilium fetches each page the model asks for from the site itself, so the site sees a request from the machine running Trilium. The page's text is then sent to the model's provider.
diff --git a/packages/commons/src/lib/llm_api.ts b/packages/commons/src/lib/llm_api.ts
index d3079369bf..8eb621be1e 100644
--- a/packages/commons/src/lib/llm_api.ts
+++ b/packages/commons/src/lib/llm_api.ts
@@ -118,6 +118,8 @@ export interface LlmChatConfig {
contextNoteId?: string;
/** The note ID of the chat note (used for auto-renaming on first message) */
chatNoteId?: string;
+ /** The tools the user allowed for the whole chat, whose calls run without asking. */
+ allowedTools?: string[];
}
/**
@@ -262,6 +264,11 @@ export interface LlmToolApprovalAnswer {
streamId: string;
toolCallId: string;
approved: boolean;
+ /**
+ * `"chat"` allows the call's tool for the rest of the chat: its other calls in the turn run
+ * without asking. Ignored for a denial.
+ */
+ scope?: "chat";
}
/**
diff --git a/packages/trilium-core/src/routes/api/llm.spec.ts b/packages/trilium-core/src/routes/api/llm.spec.ts
index 3186b6c507..143520a691 100644
--- a/packages/trilium-core/src/routes/api/llm.spec.ts
+++ b/packages/trilium-core/src/routes/api/llm.spec.ts
@@ -221,9 +221,20 @@ describe("answerToolCall", () => {
expect(answerToolCall({ body: { streamId: "s2", toolCallId: "c1", approved: true } })).toEqual({ answered: false });
});
+ it("allows the tool for the rest of the turn when the answer is scoped to the chat", async () => {
+ const approvals = openToolApprovals("s1");
+ const decision = approvals?.request({ toolCallId: "c1", toolName: "create_note", toolInput: {} });
+
+ expect(answerToolCall({ body: { streamId: "s1", toolCallId: "c1", approved: true, scope: "chat" } })).toEqual({ answered: true });
+ await expect(decision).resolves.toBe(true);
+ await expect(approvals?.request({ toolCallId: "c2", toolName: "create_note", toolInput: {} })).resolves.toBe(true);
+ });
+
it("rejects an answer that names no call or gives no decision", () => {
expect(() => answerToolCall({ body: { streamId: "s1", toolCallId: "c1" } })).toThrow(/approved are required/);
expect(() => answerToolCall({ body: { streamId: "s1", approved: true } })).toThrow(/approved are required/);
expect(() => answerToolCall({ body: { toolCallId: "c1", approved: true } })).toThrow(/approved are required/);
+ expect(() => answerToolCall({ body: { streamId: "s1", toolCallId: "c1", approved: true, scope: "forever" as "chat" } }))
+ .toThrow(/scope/);
});
});
diff --git a/packages/trilium-core/src/routes/api/llm.ts b/packages/trilium-core/src/routes/api/llm.ts
index 0cb2a1fb30..86be71af41 100644
--- a/packages/trilium-core/src/routes/api/llm.ts
+++ b/packages/trilium-core/src/routes/api/llm.ts
@@ -97,11 +97,14 @@ export function startChatStream(req: { body: ChatStreamRequest }) {
* was already answered, which a second click or a stopped turn can cause.
*/
export function answerToolCall(req: { body: Partial<LlmToolApprovalAnswer> }) {
- const { streamId, toolCallId, approved } = req.body ?? {};
+ const { streamId, toolCallId, approved, scope } = req.body ?? {};
if (!streamId || !toolCallId || typeof approved !== "boolean") {
throw new ValidationError("streamId, toolCallId and approved are required");
}
- return { answered: answerToolApproval(streamId, toolCallId, approved) };
+ if (scope !== undefined && scope !== "chat") {
+ throw new ValidationError("scope must be \"chat\" when given");
+ }
+ return { answered: answerToolApproval(streamId, toolCallId, approved, scope) };
}
/** Stop a completion started by {@link startChatStream}; unknown ids are a no-op. */
diff --git a/packages/trilium-core/src/services/llm/chat.spec.ts b/packages/trilium-core/src/services/llm/chat.spec.ts
index d1ec848a2b..d206b0075e 100644
--- a/packages/trilium-core/src/services/llm/chat.spec.ts
+++ b/packages/trilium-core/src/services/llm/chat.spec.ts
@@ -206,6 +206,16 @@ describe("runChat", () => {
expect(answerToolApproval("s1", "c1", true)).toBe(false);
});
+ it("runs a call of a tool the chat allowed without announcing it", async () => {
+ state.chunkNative = true;
+ state.asksApproval = true;
+ const turn = runChat("s1", HELLO, { allowedTools: [ "create_note" ] });
+
+ expect((await turn.next()).value).toEqual(TOOL_USE);
+ expect((await turn.next()).value).toEqual({ type: "text", content: "approved: true" });
+ expect((await turn.next()).done).toBe(true);
+ });
+
it("declines a waiting call when the turn is stopped", async () => {
state.chunkNative = true;
state.asksApproval = true;
diff --git a/packages/trilium-core/src/services/llm/chat.ts b/packages/trilium-core/src/services/llm/chat.ts
index 57dc73dd33..21e60243ce 100644
--- a/packages/trilium-core/src/services/llm/chat.ts
+++ b/packages/trilium-core/src/services/llm/chat.ts
@@ -25,8 +25,9 @@ import { resolveToolRegistries } from "./tools/index.js";
* ends with a `done` chunk only when the turn completed — an errored one stops
* after its `error` chunk, so a caller that needs a terminator must add its own.
*
- * Every tool call waits for the user: it is announced as a `tool_approval_request` chunk
- * and answered through `answerToolApproval()`, keyed by `streamId`.
+ * Every tool call waits for the user, unless the chat allowed its tool (`allowedTools`): it is
+ * announced as a `tool_approval_request` chunk and answered through `answerToolApproval()`,
+ * keyed by `streamId`.
*
* @param streamId the id the client gave the turn, unique among the turns running.
* @param abortSignal stops the turn early: the provider aborts its completion and any tool
@@ -38,7 +39,10 @@ export async function* runChat(
requestConfig: LlmProviderConfig = {},
abortSignal?: AbortSignal
): AsyncGenerator<LlmStreamChunk> {
- const toolApprovals = openToolApprovals(streamId, abortSignal);
+ const allowedTools = Array.isArray(requestConfig.allowedTools)
+ ? requestConfig.allowedTools.filter((name): name is string => typeof name === "string")
+ : [];
+ const toolApprovals = openToolApprovals(streamId, abortSignal, allowedTools);
if (!toolApprovals) {
yield { type: "error", error: `Stream '${streamId}' is already running.` };
return;
diff --git a/packages/trilium-core/src/services/llm/tool_approval.spec.ts b/packages/trilium-core/src/services/llm/tool_approval.spec.ts
index 41f74499f1..b927ccd25b 100644
--- a/packages/trilium-core/src/services/llm/tool_approval.spec.ts
+++ b/packages/trilium-core/src/services/llm/tool_approval.spec.ts
@@ -130,3 +130,36 @@ describe("ToolApprovals", () => {
expect(open.answer("c3", true)).toBe(false);
});
});
+
+describe("ToolApprovals allowed for the chat", () => {
+ it("runs a tool allowed for the chat without asking", async () => {
+ const approvals = new ToolApprovals(undefined, [ "rename_note" ]);
+ const onRequest = vi.fn();
+ approvals.onRequest = onRequest;
+
+ await expect(approvals.request({ toolCallId: "c1", toolName: "rename_note", toolInput: {} })).resolves.toBe(true);
+ expect(onRequest).not.toHaveBeenCalled();
+ void approvals.request({ toolCallId: "c2", toolName: "delete_note", toolInput: {} });
+ expect(onRequest).toHaveBeenCalledOnce();
+ });
+
+ it("allows the tool of a call allowed for the chat, for the calls that wait and the ones to come", async () => {
+ const approvals = new ToolApprovals();
+ const first = approvals.request({ toolCallId: "c1", toolName: "rename_note", toolInput: {} });
+ const parallel = approvals.request({ toolCallId: "c2", toolName: "rename_note", toolInput: {} });
+ const other = approvals.request({ toolCallId: "c3", toolName: "delete_note", toolInput: {} });
+
+ expect(approvals.answer("c1", true, "chat")).toBe(true);
+ await expect(first).resolves.toBe(true);
+ await expect(parallel).resolves.toBe(true);
+ await expect(approvals.request({ toolCallId: "c4", toolName: "rename_note", toolInput: {} })).resolves.toBe(true);
+
+ // A denial is never remembered, whatever its scope.
+ expect(approvals.answer("c3", false, "chat")).toBe(true);
+ await expect(other).resolves.toBe(false);
+ const asked = vi.fn();
+ approvals.onRequest = asked;
+ void approvals.request({ toolCallId: "c5", toolName: "delete_note", toolInput: {} });
+ expect(asked).toHaveBeenCalledOnce();
+ });
+});
diff --git a/packages/trilium-core/src/services/llm/tool_approval.ts b/packages/trilium-core/src/services/llm/tool_approval.ts
index fe1bd04cd5..908114ec05 100644
--- a/packages/trilium-core/src/services/llm/tool_approval.ts
+++ b/packages/trilium-core/src/services/llm/tool_approval.ts
@@ -9,27 +9,35 @@
* client does not send back.
*/
-import type { LlmToolApprovalRequest } from "@triliumnext/commons";
+import type { LlmToolApprovalAnswer, LlmToolApprovalRequest } from "@triliumnext/commons";
/** The reason the model is given for a call the user declined. */
export const TOOL_CALL_DECLINED_REASON = "The user declined this tool call.";
/** The tool calls of one turn that wait for the user's answer. */
export class ToolApprovals {
- private readonly pending = new Map<string, (approved: boolean) => void>();
+ private readonly pending = new Map<string, { toolName: string; settle: (approved: boolean) => void }>();
+ /** The tools whose calls run without asking. */
+ private readonly allowedTools: Set<string>;
/** Called with every request, so the turn can pass it on to the client. */
onRequest?: (request: LlmToolApprovalRequest) => void;
- constructor(private readonly abortSignal?: AbortSignal) {}
+ constructor(private readonly abortSignal?: AbortSignal, allowedTools: Iterable<string> = []) {
+ this.allowedTools = new Set(allowedTools);
+ }
/**
* Ask the user about a tool call. Resolves to whether it was approved; a turn that
- * is stopped, or ends, before the user answers declines the call.
+ * is stopped, or ends, before the user answers declines the call. A call of an allowed
+ * tool is approved without asking.
*/
request(request: LlmToolApprovalRequest): Promise<boolean> {
if (this.abortSignal?.aborted) {
return Promise.resolve(false);
}
+ if (this.allowedTools.has(request.toolName)) {
+ return Promise.resolve(true);
+ }
return new Promise((resolve) => {
const settle = (approved: boolean) => {
@@ -39,21 +47,36 @@ export class ToolApprovals {
};
const onAbort = () => settle(false);
this.abortSignal?.addEventListener("abort", onAbort, { once: true });
- this.pending.set(request.toolCallId, settle);
+ this.pending.set(request.toolCallId, { toolName: request.toolName, settle });
this.onRequest?.(request);
});
}
- /** Settle a pending call. Returns `false` when no call with that id is waiting. */
- answer(toolCallId: string, approved: boolean): boolean {
- const settle = this.pending.get(toolCallId);
- settle?.(approved);
- return !!settle;
+ /**
+ * Settle a pending call. Approved with the `"chat"` scope, its tool is allowed from then on,
+ * and the other calls of it that wait are approved too. Returns `false` when no call with
+ * that id is waiting.
+ */
+ answer(toolCallId: string, approved: boolean, scope?: LlmToolApprovalAnswer["scope"]): boolean {
+ const call = this.pending.get(toolCallId);
+ if (!call) {
+ return false;
+ }
+ call.settle(approved);
+ if (approved && scope === "chat") {
+ this.allowedTools.add(call.toolName);
+ for (const waiting of [ ...this.pending.values() ]) {
+ if (waiting.toolName === call.toolName) {
+ waiting.settle(true);
+ }
+ }
+ }
+ return true;
}
/** Decline every call that still waits. */
declineAll(): void {
- for (const settle of [ ...this.pending.values() ]) {
+ for (const { settle } of [ ...this.pending.values() ]) {
settle(false);
}
}
@@ -62,17 +85,18 @@ export class ToolApprovals {
const openApprovals = new Map<string, ToolApprovals>();
/**
- * Open the approvals of a turn under its stream id. Returns `undefined` when a turn
- * with that id is already running.
+ * Open the approvals of a turn under its stream id, with the tools the chat allowed. Returns
+ * `undefined` when a turn with that id is already running.
*/
export function openToolApprovals(
streamId: string,
- abortSignal?: AbortSignal
+ abortSignal?: AbortSignal,
+ allowedTools?: Iterable<string>
): ToolApprovals | undefined {
if (openApprovals.has(streamId)) {
return undefined;
}
- const approvals = new ToolApprovals(abortSignal);
+ const approvals = new ToolApprovals(abortSignal, allowedTools);
openApprovals.set(streamId, approvals);
return approvals;
}
@@ -84,13 +108,14 @@ export function closeToolApprovals(streamId: string): void {
}
/**
- * Pass on the user's answer to a tool call. Returns `false` when the turn has ended or
- * the call was already answered.
+ * Pass on the user's answer to a tool call. Returns `false` when the turn has ended or the call
+ * was already answered.
*/
export function answerToolApproval(
streamId: string,
toolCallId: string,
- approved: boolean
+ approved: boolean,
+ scope?: LlmToolApprovalAnswer["scope"]
): boolean {
- return openApprovals.get(streamId)?.answer(toolCallId, approved) ?? false;
+ return openApprovals.get(streamId)?.answer(toolCallId, approved, scope) ?? false;
}