Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: add "Save as note" for sidebar chat messages

Adorian Doran80707a93adf2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 1e5aa34e73..9a7c723be8 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2083,7 +2083,8 @@
"show_quote_source": "Show quote source",
"copy_message": "Copy message",
"save_to_subnote": "Save to sub-note",
- "save_to_subnote_failed": "Failed to create a note from the message.",
+ "save_as_note": "Save as note",
+ "save_note_failed": "Failed to create a note from the message.",
"regenerate": "Regenerate",
"delete_message": "Delete message",
"delete_message_confirm": "Delete this message? This operation cannot be undone.",
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.ts
index b024fe3d57..d5a4f2dd0c 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
@@ -9,7 +9,7 @@ import { canCopyMessage, copyMessageToClipboard } from "./chat_copy.js";
import { canDeleteMessage, removeMessage } from "./chat_delete.js";
import { buildQuoteMarkdown } from "./chat_quote.js";
import { canRegenerate } from "./chat_regenerate.js";
-import { canSaveToSubNote, saveMessageToSubNote } from "./chat_save.js";
+import { canSaveAsNote, canSaveToSubNote, saveMessageAsInboxNote, saveMessageToSubNote } from "./chat_save.js";
import { getMessageText } from "./llm_chat_types.js";
import type { UseLlmChatReturn } from "./useLlmChat.js";
@@ -144,6 +144,15 @@ export function useChatContextMenu({ chat, noteContext, contextMenuItems, readOn
handler: () => void saveMessageToSubNote(parentNotePath, messageMarkdown)
});
}
+ // Sidebar chat has no parent note to save under, so offer saving the message to the inbox
+ // as its own note instead. Create-only, so it stays available on a read-only chat.
+ if (!noteContextRef.current && canSaveAsNote(hasSelection, messageMarkdown)) {
+ items.push({
+ title: t("llm_chat.save_as_note"),
+ uiIcon: "bx bx-save",
+ handler: () => void saveMessageAsInboxNote(messageMarkdown)
+ });
+ }
if (!readOnly && canRegenerate(hasSelection, message, messagesRef.current, streaming)) {
items.push({
title: t("llm_chat.regenerate"),
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_save.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_save.ts
index 5de07d0278..191f2788f7 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_save.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_save.ts
@@ -1,3 +1,5 @@
+import appContext from "../../../components/app_context.js";
+import dateNoteService from "../../../services/date_notes.js";
import { t } from "../../../services/i18n.js";
import note_create from "../../../services/note_create.js";
import toast from "../../../services/toast.js";
@@ -14,19 +16,59 @@ export function canSaveToSubNote(notePath: string | null | undefined, hasSelecti
return !!notePath && !hasSelection && markdown.trim().length > 0;
}
+/** Save a message as a child note under `parentNotePath`, opening it in the active tab (note chats). */
+export function saveMessageToSubNote(parentNotePath: string, markdown: string) {
+ return createNoteFromMessage(markdown, { parentNotePath, open: "activeTab" });
+}
+
+/**
+ * Whether the "Save as note" command should be offered: the sidebar chat's equivalent of "Save to
+ * sub-note" (which needs a parent note the sidebar doesn't have). Same conditions minus the parent:
+ * no active text selection, and the message has text to save.
+ */
+export function canSaveAsNote(hasSelection: boolean, markdown: string): boolean {
+ return !hasSelection && markdown.trim().length > 0;
+}
+
+/** Save a message as a note in the inbox, opening it in a new tab (sidebar chat, which has no parent). */
+export async function saveMessageAsInboxNote(markdown: string) {
+ const inboxNote = await dateNoteService.getInboxNote();
+ if (!inboxNote) {
+ toast.showError(t("llm_chat.save_note_failed"));
+ return;
+ }
+ return createNoteFromMessage(markdown, { parentNotePath: inboxNote.noteId, open: "newTab" });
+}
+
+interface CreateNoteFromMessageOpts {
+ /** The note path (or bare note id) to create the note under. */
+ parentNotePath: string;
+ /** "activeTab" navigates the current tab in place; "newTab" opens a fresh tab. Both focus the title. */
+ open: "activeTab" | "newTab";
+}
+
/**
- * Create a text child note from a message's markdown under `parentNotePath` and open it in the active
- * tab. The content is rendered from the message's markdown source (not scraped from the rendered chat
- * DOM), so it lands in CKEditor's storage form — math, mermaid diagrams, and code survive intact.
- * Quote attribution lines are stripped (their message-id anchors are meaningless outside the chat).
- * The note is created untitled with the title field focused, so the user names it themselves.
+ * Create an untitled text note from a message's markdown under `parentNotePath` and focus its title so
+ * the user names it. The content is rendered from the message's markdown source (not scraped from the
+ * rendered chat DOM), so it lands in CKEditor's storage form — math, mermaid diagrams, and code survive
+ * intact. Quote attribution lines are stripped (their message-id anchors are meaningless outside the
+ * chat). Errors surface as a toast.
*/
-export async function saveMessageToSubNote(parentNotePath: string, markdown: string) {
+async function createNoteFromMessage(markdown: string, { parentNotePath, open }: CreateNoteFromMessageOpts) {
try {
const content = renderMarkdown(stripQuoteSources(markdown));
- await note_create.createNote(parentNotePath, { content, type: "text", focus: "title" });
+ if (open === "activeTab") {
+ await note_create.createNote(parentNotePath, { content, type: "text", focus: "title" });
+ return;
+ }
+ // Create without navigating the active tab, then open the note in a new tab with its title focused.
+ const { note } = await note_create.createNote(parentNotePath, { content, type: "text", activate: false });
+ if (note) {
+ await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, { activate: true });
+ void appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });
+ }
} catch (e) {
- console.error("Failed to create sub-note from message:", e);
- toast.showError(t("llm_chat.save_to_subnote_failed"));
+ console.error("Failed to create a note from message:", e);
+ toast.showError(t("llm_chat.save_note_failed"));
}
}