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
feat(llm_chat): start a new chat in the permission mode picked last
Elian Doran70d3574edde1
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.tsindex 83a20df462..319f978f2f 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@@ -209,6 +209,6 @@ export interface LlmChatContent {reasoningEffort?: LlmReasoningEffort;/** The tools the user allowed for this chat, whose calls run without asking. */allowedTools?: string[];- /** Which tool calls of the chat wait for the user; absent means `"always_ask"`. */+ /** Which tool calls of the chat wait for the user; absent means the mode picked last. */permissionMode?: LlmPermissionMode;}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 bd967814a1..7e1e15785c 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@@ -12,8 +12,14 @@ vi.mock("../../../services/llm_chat.js", () => ({const optionsGetJsonMock = vi.hoisted(() => vi.fn());/** The `llmWebSearchProvider` option, unset unless a test picks a search provider. */const webSearchProvider = vi.hoisted(() => ({ id: undefined as string | undefined }));+/** The `llmPermissionMode` option, and every write to it. */+const permissionModeOption = vi.hoisted(() => ({ value: "", save: vi.fn(async (_name: string, _value: unknown) => {}) }));vi.mock("../../../services/options.js", () => ({- default: { getJson: optionsGetJsonMock, get: () => webSearchProvider.id }+ default: {+ getJson: optionsGetJsonMock,+ get: (name: string) => (name === "llmPermissionMode" ? permissionModeOption.value : webSearchProvider.id),+ save: permissionModeOption.save+ }}));// useTriliumEvent subscribes to the app-wide event bus; stub it so the hook@@ -564,7 +570,8 @@ describe("useLlmChat", () => {});expect(api().allowedTools).toEqual([]);});- it("keeps the chat's permission mode, sends it with each turn and saves it", async () => {+ it("keeps the chat's permission mode, sends it with each turn and saves it, and remembers the last one picked", async () => {+ permissionModeOption.value = "always_ask";await mountChat();const sentOptions = async () => {streamChatCompletionMock.mockClear();@@ -577,23 +584,35 @@ describe("useLlmChat", () => {return streamChatCompletionMock.mock.calls[0][1];};+ // A new chat starts in the mode picked last, in any chat.expect(api().permissionMode).toBe("always_ask");expect((await sentOptions()).permissionMode).toBe("always_ask");await act(async () => {- api().setPermissionMode("ask_before_changes");+ api().setPermissionMode("dont_ask");});- expect(api().getContent()).toMatchObject({ permissionMode: "ask_before_changes" });- expect((await sentOptions()).permissionMode).toBe("ask_before_changes");+ expect(api().getContent()).toMatchObject({ permissionMode: "dont_ask" });+ expect((await sentOptions()).permissionMode).toBe("dont_ask");+ expect(permissionModeOption.save).toHaveBeenCalledExactlyOnceWith("llmPermissionMode", "dont_ask");await act(async () => {- api().loadFromContent({ version: 1, messages: [], permissionMode: "dont_ask" });+ api().loadFromContent({ version: 1, messages: [], permissionMode: "always_ask" });});- expect(api().permissionMode).toBe("dont_ask");- // A chat saved before the modes, or with a mode this version does not know.+ expect(api().permissionMode).toBe("always_ask");+ // Opening a chat does not change the remembered mode.+ expect(permissionModeOption.save).toHaveBeenCalledOnce();++ // A chat saved before the modes, or with a mode this version does not know, starts in the+ // remembered mode; with none remembered, in "ask_before_changes".await act(async () => {api().loadFromContent({ version: 1, messages: [], permissionMode: "yolo" as "dont_ask" });});expect(api().permissionMode).toBe("always_ask");+ permissionModeOption.value = "";+ await act(async () => {+ api().loadFromContent({ version: 1, messages: [] });+ });+ expect(api().permissionMode).toBe("ask_before_changes");+ permissionModeOption.save.mockClear();});});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.tsindex 53cfa59533..475dc87517 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts@@ -176,6 +176,7 @@ export interface UseLlmChatReturn {setEnableNoteTools: (value: boolean) => void;setEnableExtendedThinking: (value: boolean) => void;setReasoningEffort: (value: LlmReasoningEffort | undefined) => void;+ /** Set the chat's permission mode, which new chats then start in. */setPermissionMode: (value: LlmPermissionMode) => void;setContextNoteId: (noteId: string | undefined) => void;setChatNoteId: (noteId: string | undefined) => void;@@ -247,7 +248,7 @@ export function useLlmChat(const [enableExtendedThinking, setEnableExtendedThinking] = useState(false);const [reasoningEffort, setReasoningEffort] = useState<LlmReasoningEffort | undefined>(undefined);const [allowedTools, setAllowedTools] = useState<string[]>([]);- const [permissionMode, setPermissionMode] = useState<LlmPermissionMode>("always_ask");+ const [permissionMode, setPermissionMode] = useState<LlmPermissionMode>(rememberedPermissionMode);const [contextNoteId, setContextNoteId] = useState<string | undefined>(initialContextNoteId);const [chatNoteId, setChatNoteIdState] = useState<string | undefined>(initialChatNoteId);const [lastPromptTokens, setLastPromptTokens] = useState<number>(0);@@ -569,7 +570,7 @@ export function useLlmChat(setEnableExtendedThinking(content.enableExtendedThinking);}setReasoningEffort(LLM_REASONING_EFFORTS.find(level => level === content.reasoningEffort));- setPermissionMode(LLM_PERMISSION_MODES.find(mode => mode === content.permissionMode) ?? "always_ask");+ setPermissionMode(LLM_PERMISSION_MODES.find(mode => mode === content.permissionMode) ?? rememberedPermissionMode());setAllowedTools(Array.isArray(content.allowedTools)? content.allowedTools.filter((name): name is string => typeof name === "string"): []);@@ -1035,6 +1036,13 @@ export function useLlmChat(setAllowedTools(tools => tools.filter(tool => tool !== toolName));}, []);+ const choosePermissionMode = useCallback((mode: LlmPermissionMode) => {+ setPermissionMode(mode);+ optionService.save("llmPermissionMode", mode).catch((e: unknown) => {+ console.error("Failed to remember the permission mode:", e);+ });+ }, []);+/** Stop the current generation by aborting the SSE connection. */const stopStreaming = useCallback(() => {if (abortControllerRef.current) {@@ -1104,7 +1112,7 @@ export function useLlmChat(setEnableNoteTools,setEnableExtendedThinking,setReasoningEffort,- setPermissionMode,+ setPermissionMode: choosePermissionMode,setContextNoteId,setChatNoteId,addPendingAttachment,@@ -1125,3 +1133,8 @@ export function useLlmChat(};}+/** The permission mode picked last in any chat, which a new chat starts in. */+function rememberedPermissionMode(): LlmPermissionMode {+ const remembered = optionService.get("llmPermissionMode");+ return LLM_PERMISSION_MODES.find(mode => mode === remembered) ?? "ask_before_changes";+}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.htmlindex d3aed6d835..1906807197 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@@ -284,14 +284,17 @@<p>Which calls ask depends on the chat's permission mode, chosen in the <spanclass="tn-icon bx bx-shield-quarter"></span> <em>Tools</em> menu under <em>Permissions</em>:</p><ul>- <li><em>Always ask</em>, the mode a new chat starts in: every 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).</li>+ <li><em>Always ask</em>: every 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).</li><li><em>Ask before changes</em>: the calls that only read run on their own,and the ones that change your notes (creating, editing, renaming, movingor deleting a note, and setting or deleting an attribute) ask.</li><li><em>Don't ask</em>: every call runs without asking, changes included.</li></ul>+<p>A new chat starts in the mode you picked last, in any chat, or in <em>Ask before changes</em> until+ you pick one. Picking <em>Don't ask</em> therefore carries over to the next+ chat too.</p><p>A tool allowed for the chat runs without asking in every mode. A model'sbuilt-in web search runs on the provider's side and never asks.</p><asidediff --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.htmlindex 50744bc0d6..a0a13ab779 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@@ -128,8 +128,11 @@<li><strong>The current note:</strong> the file icon in the sidebar chat.</li><li><strong>Web search:</strong> also in the <em>Tools</em> menu.</li><li><strong>Which tool calls ask first:</strong> the <em>Permissions</em> submenu- of the <em>Tools</em> menu. <em>Always ask</em>, the mode a new chat starts- in, has every call wait for you, reads included.</li>+ of the <em>Tools</em> menu. <em>Always ask</em> has every call wait for you,+ reads included. A new chat starts in the mode you picked last, so a chat+ after one set to <em>Don't ask</em> asks about nothing either. Until you+ pick a mode, chats start in <em>Ask before changes</em>, where the AI reads+ and searches your notes without asking.</li><li><strong>The editor assistant</strong>: it has no per-run toggles. Whatyou control is whether you run it, what you select before you do and whichmodel answers.</li>diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 5d34cef0dd..c0e011a54e 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -169,10 +169,12 @@ To make a chat ask about a tool again, open the <span class="tn-icon bx bx-shielWhich calls ask depends on the chat's permission mode, chosen in the <span class="tn-icon bx bx-shield-quarter"></span> _Tools_ menu under _Permissions_:-* _Always ask_, the mode a new chat starts in: every 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).+* _Always ask_: every 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).* _Ask before changes_: the calls that only read run on their own, and the ones that change your notes (creating, editing, renaming, moving or deleting a note, and setting or deleting an attribute) ask.* _Don't ask_: every call runs without asking, changes included.+A new chat starts in the mode you picked last, in any chat, or in _Ask before changes_ until you pick one. Picking _Don't ask_ therefore carries over to the next chat too.+A tool allowed for the chat runs without asking in every mode. A model's built-in web search runs on the provider's side and never asks.> [!IMPORTANT]diff --git a/docs/User Guide/User Guide/AI/Privacy.md b/docs/User Guide/User Guide/AI/Privacy.mdindex 395b466c32..de2b58e1dd 100644--- a/docs/User Guide/User Guide/AI/Privacy.md+++ b/docs/User Guide/User Guide/AI/Privacy.md@@ -57,7 +57,7 @@ The same applies to the <a class="reference-link" href="../Note%20Types/Text/In* **Note access**: turn it off in the <span class="tn-icon bx bx-shield-quarter"></span> _Tools_ menu at the bottom of the chat, and the model can read nothing beyond what you type.* **The current note:** the file icon in the sidebar chat.* **Web search:** also in the _Tools_ menu.-* **Which tool calls ask first:** the _Permissions_ submenu of the _Tools_ menu. _Always ask_, the mode a new chat starts in, has every call wait for you, reads included.+* **Which tool calls ask first:** the _Permissions_ submenu of the _Tools_ menu. _Always ask_ has every call wait for you, reads included. A new chat starts in the mode you picked last, so a chat after one set to _Don't ask_ asks about nothing either. Until you pick a mode, chats start in _Ask before changes_, where the AI reads and searches your notes without asking.* **The editor assistant**: it has no per-run toggles. What you control is whether you run it, what you select before you do and which model answers.With note access and note context both off, the provider receives only the words you typed.diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.tsindex 09ab7d26df..7791e577a3 100644--- a/packages/commons/src/lib/options_interface.ts+++ b/packages/commons/src/lib/options_interface.ts@@ -342,6 +342,8 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActillmProviders: string;/** Config id of the search provider the chat's web search uses; empty for the model's built-in search. */llmWebSearchProvider: string;+ /** The permission mode picked last in any chat, which a new chat starts in. */+ llmPermissionMode: string;/** The model the text editor's AI assistant runs on, as JSON `{ providerId, provider, model }`; empty to follow the first configured provider's default. */aiAssistantModel: string;/** Whether the MCP (Model Context Protocol) server endpoint is enabled. */diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.tsindex 1ea835851c..71785afdab 100644--- a/packages/trilium-core/src/routes/api/options.ts+++ b/packages/trilium-core/src/routes/api/options.ts@@ -162,6 +162,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>(["aiEnabled","llmProviders","llmWebSearchProvider",+ "llmPermissionMode","aiAssistantModel","mcpEnabled",// OCR optionsdiff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.tsindex 399a1c639b..d60bc73309 100644--- a/packages/trilium-core/src/services/options_init.ts+++ b/packages/trilium-core/src/services/options_init.ts@@ -370,6 +370,7 @@ const defaultOptions: DefaultOption[] = [{ name: "aiEnabled", value: (optionsMap) => optionsMap.experimentalFeatures?.includes('"llm"') ? "true" : "false", isSynced: true },{ name: "llmProviders", value: "[]", isSynced: true },{ name: "llmWebSearchProvider", value: "", isSynced: true },+ { name: "llmPermissionMode", value: "ask_before_changes", isSynced: true },{ name: "aiAssistantModel", value: "", isSynced: true },{ name: "mcpEnabled", value: "false", isSynced: false },