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): history button
Elian Doran7817890cfe74
diff --git a/apps/client/src/services/date_notes.ts b/apps/client/src/services/date_notes.tsindex cad14d39fc..f452e55dec 100644--- a/apps/client/src/services/date_notes.ts+++ b/apps/client/src/services/date_notes.ts@@ -120,6 +120,19 @@ async function getOrCreateLlmChat() {return await froca.getNote(note.noteId);}+export interface RecentLlmChat {+ noteId: string;+ title: string;+ dateModified: string;+}++/**+ * Gets a list of recent LLM chats for the history popup.+ */+async function getRecentLlmChats(limit: number = 10): Promise<RecentLlmChat[]> {+ return await server.get<RecentLlmChat[]>(`special-notes/recent-llm-chats?limit=${limit}`);+}+export default {getInboxNote,getTodayNote,@@ -133,5 +146,6 @@ export default {createSearchNote,createLlmChat,getMostRecentLlmChat,- getOrCreateLlmChat+ getOrCreateLlmChat,+ getRecentLlmChats};diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 3bdaa4c51d..20a9ae46ae 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1637,7 +1637,10 @@"launcher_title": "Open AI Chat","new_chat": "Start new chat","save_chat": "Save chat to notes",- "empty_state": "Start a conversation"+ "empty_state": "Start a conversation",+ "history": "Chat history",+ "recent_chats": "Recent chats",+ "no_chats": "No previous chats"},"shared_switch": {"shared": "Shared",diff --git a/apps/client/src/widgets/sidebar/SidebarChat.css b/apps/client/src/widgets/sidebar/SidebarChat.cssindex 37f06d51d7..31239ad0a3 100644--- a/apps/client/src/widgets/sidebar/SidebarChat.css+++ b/apps/client/src/widgets/sidebar/SidebarChat.css@@ -177,3 +177,30 @@.sidebar-chat-messages .llm-chat-markdown code {font-size: 0.85em;}++/* History dropdown */+.sidebar-chat-history-empty {+ padding: 0.75rem 1rem;+ text-align: center;+ color: var(--muted-text-color);+ font-size: 0.9rem;+}++.sidebar-chat-history-item-content {+ display: flex;+ flex-direction: column;+ min-width: 0;+}++.sidebar-chat-history-item-content span,+.sidebar-chat-history-item-content strong {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++.sidebar-chat-history-date {+ font-size: 0.75rem;+ color: var(--muted-text-color);+ margin-top: 0.125rem;+}diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsxindex 83d56db8dd..d196c5582c 100644--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx@@ -1,9 +1,13 @@+import type { Dropdown as BootstrapDropdown } from "bootstrap";import { useCallback, useEffect, useRef, useState } from "preact/hooks";-import dateNoteService from "../../services/date_notes.js";+import dateNoteService, { type RecentLlmChat } from "../../services/date_notes.js";import { t } from "../../services/i18n.js";import server from "../../services/server.js";import ActionButton from "../react/ActionButton.js";+import Dropdown from "../react/Dropdown.js";+import { FormListItem } from "../react/FormList.js";+import Icon from "../react/Icon.js";import NoItems from "../react/NoItems.js";import ChatMessage from "../type_widgets/llm_chat/ChatMessage.js";import type { LlmChatContent } from "../type_widgets/llm_chat/llm_chat_types.js";@@ -19,7 +23,9 @@ import "./SidebarChat.css";export default function SidebarChat() {const [chatNoteId, setChatNoteId] = useState<string | null>(null);const [shouldSave, setShouldSave] = useState(false);+ const [recentChats, setRecentChats] = useState<RecentLlmChat[]>([]);const saveTimeoutRef = useRef<ReturnType<typeof setTimeout>>();+ const historyDropdownRef = useRef<BootstrapDropdown | null>(null);// Use shared chat hook with sidebar-specific optionsconst chat = useLlmChat(@@ -159,6 +165,32 @@ export default function SidebarChat() {}}, [chatNoteId, chat]);+ const loadRecentChats = useCallback(async () => {+ try {+ const chats = await dateNoteService.getRecentLlmChats(10);+ setRecentChats(chats);+ } catch (err) {+ console.error("Failed to load recent chats:", err);+ }+ }, []);++ const handleSelectChat = useCallback(async (noteId: string) => {+ historyDropdownRef.current?.hide();++ if (noteId === chatNoteId) return;++ try {+ const blob = await server.get<{ content: string }>(`notes/${noteId}/blob`);+ if (blob?.content) {+ const parsed: LlmChatContent = JSON.parse(blob.content);+ setChatNoteId(noteId);+ chat.loadFromContent(parsed);+ }+ } catch (err) {+ console.error("Failed to load selected chat:", err);+ }+ }, [chatNoteId, chat]);+return (<RightPanelWidgetid="sidebar-chat"@@ -172,6 +204,40 @@ export default function SidebarChat() {title={t("sidebar_chat.new_chat")}onClick={handleNewChat}/>+ <Dropdown+ text={<Icon icon="bx bx-history" />}+ title={t("sidebar_chat.history")}+ iconAction+ hideToggleArrow+ dropdownContainerClassName="tn-dropdown-menu-scrollable"+ dropdownOptions={{ popperConfig: { strategy: "fixed" } }}+ dropdownRef={historyDropdownRef}+ onShown={loadRecentChats}+ >+ {recentChats.length === 0 ? (+ <li className="sidebar-chat-history-empty">+ {t("sidebar_chat.no_chats")}+ </li>+ ) : (+ recentChats.map(chatItem => (+ <FormListItem+ key={chatItem.noteId}+ icon="bx bx-message-square-dots"+ className={chatItem.noteId === chatNoteId ? "active" : ""}+ onClick={() => handleSelectChat(chatItem.noteId)}+ >+ <div className="sidebar-chat-history-item-content">+ {chatItem.noteId === chatNoteId+ ? <strong>{chatItem.title}</strong>+ : <span>{chatItem.title}</span>}+ <span className="sidebar-chat-history-date">+ {new Date(chatItem.dateModified).toLocaleDateString()}+ </span>+ </div>+ </FormListItem>+ ))+ )}+ </Dropdown><ActionButtonicon="bx bx-save"text=""diff --git a/apps/server/src/routes/api/special_notes.ts b/apps/server/src/routes/api/special_notes.tsindex efcc4b93de..a15db9df5a 100644--- a/apps/server/src/routes/api/special_notes.ts+++ b/apps/server/src/routes/api/special_notes.ts@@ -100,6 +100,11 @@ function getOrCreateLlmChat() {return specialNotesService.getOrCreateLlmChat();}+function getRecentLlmChats(req: Request) {+ const limit = parseInt(req.query.limit as string) || 10;+ return specialNotesService.getRecentLlmChats(limit);+}+function saveLlmChat(req: Request) {return specialNotesService.saveLlmChat(req.body.llmChatNoteId);}@@ -140,6 +145,7 @@ export default {createLlmChat,getMostRecentLlmChat,getOrCreateLlmChat,+ getRecentLlmChats,saveLlmChat,createLauncher,resetLauncher,diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.tsindex cf19ce24e4..21779abc4e 100644--- a/apps/server/src/routes/routes.ts+++ b/apps/server/src/routes/routes.ts@@ -295,6 +295,7 @@ function register(app: express.Application) {apiRoute(PST, "/api/special-notes/llm-chat", specialNotesRoute.createLlmChat);apiRoute(GET, "/api/special-notes/most-recent-llm-chat", specialNotesRoute.getMostRecentLlmChat);apiRoute(GET, "/api/special-notes/get-or-create-llm-chat", specialNotesRoute.getOrCreateLlmChat);+ apiRoute(GET, "/api/special-notes/recent-llm-chats", specialNotesRoute.getRecentLlmChats);apiRoute(PST, "/api/special-notes/save-llm-chat", specialNotesRoute.saveLlmChat);apiRoute(PST, "/api/special-notes/launchers/:noteId/reset", specialNotesRoute.resetLauncher);apiRoute(PST, "/api/special-notes/launchers/:parentNoteId/:launcherType", specialNotesRoute.createLauncher);diff --git a/apps/server/src/services/special_notes.ts b/apps/server/src/services/special_notes.tsindex 7a090b667d..5ace38a479 100644--- a/apps/server/src/services/special_notes.ts+++ b/apps/server/src/services/special_notes.ts@@ -175,6 +175,28 @@ function getOrCreateLlmChat() {return createLlmChat();}+/**+ * Gets a list of recent LLM chat notes.+ * Used by sidebar chat history popup.+ */+function getRecentLlmChats(limit: number = 10) {+ const results = searchService.searchNotes(+ "note.type = llmChat",+ new SearchContext({+ ancestorNoteId: "_llmChat",+ limit,+ orderBy: "note.utcDateModified",+ orderDirection: "desc"+ })+ );++ return results.map(note => ({+ noteId: note.noteId,+ title: note.title,+ dateModified: note.utcDateModified+ }));+}+function getLlmChatHome() {const workspaceNote = hoistedNoteService.getWorkspaceNote();if (!workspaceNote) {@@ -371,6 +393,7 @@ export default {createLlmChat,getMostRecentLlmChat,getOrCreateLlmChat,+ getRecentLlmChats,saveLlmChat,createLauncher,resetLauncher,