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
client/AI chat: properly handle read-only chats
Adorian Doran7b93914bd311
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d255476201..2922988f6d 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2070,6 +2070,7 @@"stop": "Stop","retry": "Retry","scroll_to_bottom": "Scroll to latest",+ "read_only_notice": "Replying is not available because this AI chat is read-only.","toc_file": "File: {{name}}","highlights_title": "Highlights ({{count}})","highlights_empty": "No highlights yet. Select text in a message and choose \"Highlight\".",diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsxindex a11eac22e3..998966a8de 100644--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx@@ -11,11 +11,12 @@ import { formatDateTime } from "../../utils/formatters";import ActionButton from "../react/ActionButton.js";import Dropdown from "../react/Dropdown.js";import { FormDropdownDivider, FormListItem } from "../react/FormList.js";-import { useActiveNoteContext, useNote, useNoteProperty, useSpacedUpdate } from "../react/hooks.js";+import { useActiveNoteContext, useNote, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate } from "../react/hooks.js";import { useChatContextMenu } from "../type_widgets/llm_chat/chat_context_menu.js";import { useChatHighlights } from "../type_widgets/llm_chat/chat_highlights.js";import ChatInputBar from "../type_widgets/llm_chat/ChatInputBar.js";import ChatMessageList from "../type_widgets/llm_chat/ChatMessageList.js";+import ChatReadOnlyNotice from "../type_widgets/llm_chat/ChatReadOnlyNotice.js";import type { LlmChatContent } from "../type_widgets/llm_chat/llm_chat_types.js";import { useLlmChat } from "../type_widgets/llm_chat/useLlmChat.js";import RightPanelWidget from "./RightPanelWidget.js";@@ -42,6 +43,9 @@ export default function SidebarChat() {const chatNote = useNote(chatNoteId);const chatTitle = useNoteProperty(chatNote, "title") || t("sidebar_chat.title");+ // A `#readOnly` chat is immutable: reply bar replaced by a notice, mutating commands suppressed.+ const [readOnly] = useNoteLabelBoolean(chatNote, "readOnly");+// Refs for stable access in the spaced update callbackconst chatNoteIdRef = useRef(chatNoteId);chatNoteIdRef.current = chatNoteId;@@ -62,7 +66,7 @@ export default function SidebarChat() {const highlights = useChatHighlights(chat, undefined);// Right-click menu over the timeline, with highlights contributing their add/remove items.- useChatContextMenu({ chat, noteContext: undefined, contextMenuItems: highlights.highlightMenuItems });+ useChatContextMenu({ chat, noteContext: undefined, contextMenuItems: highlights.highlightMenuItems, readOnly });// Save directly via server.put using the string noteId.// This avoids the FNote dependency that useEditorSpacedUpdate requires.@@ -306,12 +310,16 @@ export default function SidebarChat() {className="sidebar-chat-messages"emptyStateText={t("sidebar_chat.empty_state")}/>- <ChatInputBar- chat={chat}- activeNoteId={activeNoteId ?? undefined}- activeNoteTitle={activeNote?.title}- onSubmit={handleSubmit}- />+ {readOnly ? (+ <ChatReadOnlyNotice />+ ) : (+ <ChatInputBar+ chat={chat}+ activeNoteId={activeNoteId ?? undefined}+ activeNoteTitle={activeNote?.title}+ onSubmit={handleSubmit}+ />+ )}</div></RightPanelWidget>);diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.cssnew file mode 100644index 0000000000..80eb1aed8d--- /dev/null+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.css@@ -0,0 +1,7 @@+.llm-chat-read-only-notice {+ margin: 0;+ padding: 0.75em 1em;+ text-align: center;+ color: var(--muted-text-color);+ font-size: 0.9em;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.tsxnew file mode 100644index 0000000000..499f112afd--- /dev/null+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatReadOnlyNotice.tsx@@ -0,0 +1,8 @@+import "./ChatReadOnlyNotice.css";++import { t } from "../../../services/i18n.js";++/** Subtle grayed note shown in place of the reply input when a chat is read-only (`#readOnly`). */+export default function ChatReadOnlyNotice() {+ return <p className="llm-chat-read-only-notice">{t("llm_chat.read_only_notice")}</p>;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsxindex 6dd629668e..da0d0d7c34 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx@@ -3,7 +3,7 @@ import "./LlmChat.css";import { useCallback, useEffect, useRef } from "preact/hooks";import { t } from "../../../services/i18n.js";-import { useEditorSpacedUpdate } from "../../react/hooks.js";+import { useEditorSpacedUpdate, useNoteLabelBoolean } from "../../react/hooks.js";import { TypeWidgetProps } from "../type_widget.js";import { useChatContextMenu } from "./chat_context_menu.js";import { useChatHighlights } from "./chat_highlights.js";@@ -11,12 +11,17 @@ import { useChatMessageJumps } from "./chat_message_jump.js";import { useChatToc } from "./chat_toc.js";import ChatInputBar from "./ChatInputBar.js";import ChatMessageList from "./ChatMessageList.js";+import ChatReadOnlyNotice from "./ChatReadOnlyNotice.js";import type { LlmChatContent } from "./llm_chat_types.js";import { useLlmChat } from "./useLlmChat.js";export default function LlmChat({ note, noteContext }: TypeWidgetProps) {const spacedUpdateRef = useRef<{ scheduleUpdate: () => void }>(null);+ // A `#readOnly` chat is immutable: the reply bar is replaced by a notice and every+ // mutating command is suppressed. Reactive, so toggling the label updates the UI live.+ const [readOnly] = useNoteLabelBoolean(note, "readOnly");+const chat = useLlmChat(// onMessagesChange - trigger save() => spacedUpdateRef.current?.scheduleUpdate(),@@ -35,7 +40,7 @@ export default function LlmChat({ note, noteContext }: TypeWidgetProps) {const highlights = useChatHighlights(chat, noteContext);// Right-click menu over the timeline, with highlights contributing their add/remove items.- useChatContextMenu({ chat, noteContext, contextMenuItems: highlights.highlightMenuItems });+ useChatContextMenu({ chat, noteContext, contextMenuItems: highlights.highlightMenuItems, readOnly });// Make the "Show quote source" links in submitted quotes jump to the referenced message.useChatMessageJumps(chat.scrollContainerRef);@@ -75,13 +80,17 @@ export default function LlmChat({ note, noteContext }: TypeWidgetProps) {className="llm-chat-messages"emptyStateText={t("llm_chat.empty_state")}/>- <ChatInputBar- chat={chat}- onWebSearchChange={triggerSave}- onNoteToolsChange={triggerSave}- onExtendedThinkingChange={triggerSave}- onModelChange={triggerSave}- />+ {readOnly ? (+ <ChatReadOnlyNotice />+ ) : (+ <ChatInputBar+ chat={chat}+ onWebSearchChange={triggerSave}+ onNoteToolsChange={triggerSave}+ onExtendedThinkingChange={triggerSave}+ onModelChange={triggerSave}+ />+ )}</div>);}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_context_menu.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_context_menu.tsindex 3157ba8adc..b024fe3d57 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_context_menu.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_context_menu.ts@@ -37,6 +37,8 @@ export interface ChatContextMenuOptions {noteContext: NoteContext | undefined;/** Builds extra items to add to the message's context menu (e.g. highlight add/remove). */contextMenuItems: ChatContextMenuItemsProvider;+ /** When the chat is read-only (`#readOnly`), the mutating commands are suppressed. */+ readOnly: boolean;}/**@@ -49,9 +51,10 @@ export interface ChatContextMenuOptions {** Preventing the default menu drops the browser's own Copy, so we provide a rich Copy here. Copy stays* available while a reply streams; the message-mutating commands (highlight add/remove, delete) are- * suppressed then.+ * suppressed then. On a read-only chat every mutating command (highlight add/remove, quote, regenerate,+ * delete) is suppressed for the whole conversation — only Copy and Save-to-sub-note remain.*/-export function useChatContextMenu({ chat, noteContext, contextMenuItems }: ChatContextMenuOptions) {+export function useChatContextMenu({ chat, noteContext, contextMenuItems, readOnly }: ChatContextMenuOptions) {const { scrollContainerRef } = chat;// Latest values for the imperative event handler, so it never reads stale state.@@ -59,6 +62,8 @@ export function useChatContextMenu({ chat, noteContext, contextMenuItems }: ChatmessagesRef.current = chat.messages;const isStreamingRef = useRef(chat.isStreaming);isStreamingRef.current = chat.isStreaming;+ const readOnlyRef = useRef(readOnly);+ readOnlyRef.current = readOnly;const setMessagesRef = useRef(chat.setMessages);setMessagesRef.current = chat.setMessages;const appendToInputRef = useRef(chat.appendToInput);@@ -87,6 +92,9 @@ export function useChatContextMenu({ chat, noteContext, contextMenuItems }: Chatif (!messageId || !root) return; // thinking/error messages carry no id → nothing to offerconst streaming = isStreamingRef.current;+ // A read-only chat (`#readOnly`) is immutable: every command that would change the+ // conversation is suppressed, leaving only Copy and Save-to-sub-note.+ const readOnly = readOnlyRef.current;const selectionRange = selectionRangeWithin(container.ownerDocument.getSelection(), root);const hasSelection = !!selectionRange?.toString().trim();const message = messagesRef.current.find(m => m.id === messageId);@@ -101,13 +109,16 @@ export function useChatContextMenu({ chat, noteContext, contextMenuItems }: Chatitems.push({ title: t("llm_chat.copy"), uiIcon: "bx bx-copy", handler: copySelection });}- // Injected commands (highlight add/remove) sit next to the selection commands.- items.push(...contextMenuItems({ messageId, root, selectionRange, streaming, clientX: e.clientX, clientY: e.clientY }));+ // Injected commands (highlight add/remove) sit next to the selection commands. They+ // mutate the stored conversation, so they're dropped on a read-only chat.+ if (!readOnly) {+ items.push(...contextMenuItems({ messageId, root, selectionRange, streaming, clientX: e.clientX, clientY: e.clientY }));+ }// Quote the selection into the reply input. Suppressed while streaming — the input is// read-only then. Text is captured now (the menu can clear the live selection before the// handler runs).- const quotedText = !streaming ? selectionRange?.toString() : undefined;+ const quotedText = !streaming && !readOnly ? selectionRange?.toString() : undefined;if (quotedText?.trim()) {items.push({title: t("llm_chat.quote_selection"),@@ -133,14 +144,14 @@ export function useChatContextMenu({ chat, noteContext, contextMenuItems }: Chathandler: () => void saveMessageToSubNote(parentNotePath, messageMarkdown)});}- if (canRegenerate(hasSelection, message, messagesRef.current, streaming)) {+ if (!readOnly && canRegenerate(hasSelection, message, messagesRef.current, streaming)) {items.push({title: t("llm_chat.regenerate"),uiIcon: "bx bx-revision",handler: () => void regenerateLastReplyRef.current()});}- if (canDeleteMessage(hasSelection, message, streaming)) {+ if (!readOnly && canDeleteMessage(hasSelection, message, streaming)) {items.push({title: t("llm_chat.delete_message"),uiIcon: "bx bx-trash",