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
Well the AI chat note type "kinda" works...
perf3ctaaa3ee2697b3
diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.tsindex 056481c7c2..ca9dc4c9c1 100644--- a/src/public/app/components/app_context.ts+++ b/src/public/app/components/app_context.ts@@ -84,6 +84,7 @@ export type CommandMappings = {showLaunchBarSubtree: CommandData;showRevisions: CommandData;showLlmChat: CommandData;+ createAiChat: CommandData;showOptions: CommandData & {section: string;};diff --git a/src/public/app/components/note_context.ts b/src/public/app/components/note_context.tsindex 5799514d14..6c93fcc5bc 100644--- a/src/public/app/components/note_context.ts+++ b/src/public/app/components/note_context.ts@@ -369,11 +369,6 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">const { note, viewScope } = this;- // For llmChat viewMode, show a custom title- if (viewScope?.viewMode === "llmChat") {- return "Chat with Notes";- }-const isNormalView = (viewScope?.viewMode === "default" || viewScope?.viewMode === "contextual-help");let title = (isNormalView ? note.title : `${note.title}: ${viewScope?.viewMode}`);diff --git a/src/public/app/components/root_command_executor.ts b/src/public/app/components/root_command_executor.tsindex 96e917d810..b4e6ed3c0e 100644--- a/src/public/app/components/root_command_executor.ts+++ b/src/public/app/components/root_command_executor.ts@@ -9,10 +9,9 @@ import froca from "../services/froca.js";import utils from "../services/utils.js";import LlmChatPanel from "../widgets/llm_chat_panel.js";import toastService from "../services/toast.js";+import noteCreateService from "../services/note_create.js";export default class RootCommandExecutor extends Component {- private llmChatPanel: any = null;-editReadOnlyNoteCommand() {const noteContext = appContext.tabManager.getActiveContext();if (noteContext?.viewScope) {@@ -231,22 +230,39 @@ export default class RootCommandExecutor extends Component {}}- async showLlmChatCommand() {- console.log("showLlmChatCommand triggered");- toastService.showMessage("Opening LLM Chat...");-+ async createAiChatCommand() {try {- // We'll use the Note Map approach - open a known note ID that corresponds to the LLM chat panel- await appContext.tabManager.openTabWithNoteWithHoisting("_globalNoteMap", {- activate: true,- viewScope: {- viewMode: "llmChat" // We'll need to handle this custom view mode elsewhere- }+ // Create a new AI Chat note+ const parentNoteId = appContext.tabManager.getActiveContextNotePath();++ if (!parentNoteId) {+ toastService.showError("No active note to create AI Chat under");+ return;+ }++ const result = await noteCreateService.createNote(parentNoteId, {+ title: "New AI Chat",+ type: "aiChat",+ content: JSON.stringify({+ messages: [],+ title: "New AI Chat"+ })});++ if (!result.note) {+ toastService.showError("Failed to create AI Chat note");+ return;+ }++ await appContext.tabManager.openTabWithNoteWithHoisting(result.note.noteId, {+ activate: true+ });++ toastService.showMessage("Created new AI Chat note");}catch (e) {- console.error("Error opening LLM Chat:", e);- toastService.showError("Failed to open LLM Chat: " + (e as Error).message);+ console.error("Error creating AI Chat note:", e);+ toastService.showError("Failed to create AI Chat note: " + (e as Error).message);}}}diff --git a/src/public/app/entities/fnote.ts b/src/public/app/entities/fnote.tsindex e2a13852ea..3959e2d0f1 100644--- a/src/public/app/entities/fnote.ts+++ b/src/public/app/entities/fnote.ts@@ -28,7 +28,8 @@ const NOTE_TYPE_ICONS = {doc: "bx bxs-file-doc",contentWidget: "bx bxs-widget",mindMap: "bx bx-sitemap",- geoMap: "bx bx-map-alt"+ geoMap: "bx bx-map-alt",+ aiChat: "bx bx-bot"};/**@@ -36,7 +37,7 @@ const NOTE_TYPE_ICONS = {* end user. Those types should be used only for checking against, they are* not for direct use.*/-export type NoteType = "file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code" | "mindMap" | "geoMap";+export type NoteType = "file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code" | "mindMap" | "geoMap" | "aiChat";export interface NotePathRecord {isArchived: boolean;diff --git a/src/public/app/widgets/buttons/llm_chat_button.ts b/src/public/app/widgets/buttons/ai_chat_button.tssimilarity index 86%rename from src/public/app/widgets/buttons/llm_chat_button.tsrename to src/public/app/widgets/buttons/ai_chat_button.tsindex 94afab977e..5ad3f80333 100644--- a/src/public/app/widgets/buttons/llm_chat_button.ts+++ b/src/public/app/widgets/buttons/ai_chat_button.ts@@ -3,12 +3,12 @@ import type FNote from "../../entities/fnote.js";import options from "../../services/options.js";import CommandButtonWidget from "./command_button.js";-export default class LlmChatButton extends CommandButtonWidget {+export default class AiChatButton extends CommandButtonWidget {constructor(note: FNote) {super();- this.command("showLlmChat")+ this.command("createAiChat").title(() => note.title).icon(() => note.getIcon()).class("launcher-button");@@ -23,5 +23,4 @@ export default class LlmChatButton extends CommandButtonWidget {this.refresh();}}-}diff --git a/src/public/app/widgets/buttons/create_ai_chat_button.ts b/src/public/app/widgets/buttons/create_ai_chat_button.tsnew file mode 100644index 0000000000..1ccd52cdaa--- /dev/null+++ b/src/public/app/widgets/buttons/create_ai_chat_button.ts@@ -0,0 +1,27 @@+import { t } from "../../services/i18n.js";+import options from "../../services/options.js";+import CommandButtonWidget from "./command_button.js";++export default class CreateAiChatButton extends CommandButtonWidget {+ constructor() {+ super();++ this.icon("bx bx-bot")+ .title(t("ai.create_new_ai_chat"))+ .titlePlacement("bottom")+ .command("createAiChat")+ .class("icon-action");+ }++ isEnabled() {+ return options.get("aiEnabled") === "true";+ }++ async refreshWithNote() {+ if (this.isEnabled()) {+ this.$widget.show();+ } else {+ this.$widget.hide();+ }+ }+}diff --git a/src/public/app/widgets/containers/launcher.ts b/src/public/app/widgets/containers/launcher.tsindex 7374969232..e1bfc5a8b9 100644--- a/src/public/app/widgets/containers/launcher.ts+++ b/src/public/app/widgets/containers/launcher.ts@@ -13,7 +13,7 @@ import HistoryNavigationButton from "../buttons/history_navigation.js";import QuickSearchLauncherWidget from "../quick_search_launcher.js";import type FNote from "../../entities/fnote.js";import type { CommandNames } from "../../components/app_context.js";-import LlmChatButton from "../buttons/llm_chat_button.js";+import AiChatButton from "../buttons/ai_chat_button.js";interface InnerWidget extends BasicWidget {settings?: {@@ -124,8 +124,8 @@ export default class LauncherWidget extends BasicWidget {return new TodayLauncher(note);case "quickSearch":return new QuickSearchLauncherWidget(this.isHorizontalLayout);- case "llmChatLauncher":- return new LlmChatButton(note);+ case "aiChatLauncher":+ return new AiChatButton(note);default:throw new Error(`Unrecognized builtin widget ${builtinWidget} for launcher ${note.noteId} "${note.title}"`);}diff --git a/src/public/app/widgets/floating_buttons/help_button.ts b/src/public/app/widgets/floating_buttons/help_button.tsindex b14bfb7b7a..088f95fd1c 100644--- a/src/public/app/widgets/floating_buttons/help_button.ts+++ b/src/public/app/widgets/floating_buttons/help_button.ts@@ -28,7 +28,8 @@ export const byNoteType: Record<Exclude<NoteType, "book">, string | null> = {render: null,search: null,text: null,- webView: null+ webView: null,+ aiChat: null};export const byBookType: Record<ViewTypeOptions, string | null> = {diff --git a/src/public/app/widgets/note_detail.ts b/src/public/app/widgets/note_detail.tsindex 2cd662efc3..eaa87b62d4 100644--- a/src/public/app/widgets/note_detail.ts+++ b/src/public/app/widgets/note_detail.ts@@ -35,8 +35,8 @@ import GeoMapTypeWidget from "./type_widgets/geo_map.js";import utils from "../services/utils.js";import type { NoteType } from "../entities/fnote.js";import type TypeWidget from "./type_widgets/type_widget.js";-import LlmChatTypeWidget from "./type_widgets/llm_chat.js";import { MermaidTypeWidget } from "./type_widgets/mermaid.js";+import AiChatTypeWidget from "./type_widgets/ai_chat.js";const TPL = `<div class="note-detail">@@ -75,7 +75,7 @@ const typeWidgetClasses = {attachmentList: AttachmentListTypeWidget,mindMap: MindMapWidget,geoMap: GeoMapTypeWidget,- llmChat: LlmChatTypeWidget,+ aiChat: AiChatTypeWidget,// Split type editorsmermaid: MermaidTypeWidget@@ -95,7 +95,7 @@ type ExtendedNoteType =| "attachmentDetail"| "attachmentList"| "protectedSession"- | "llmChat";+ | "aiChat";export default class NoteDetailWidget extends NoteContextAwareWidget {@@ -228,9 +228,6 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {if (viewScope?.viewMode === "source") {resultingType = "readOnlyCode";- } else if (viewScope?.viewMode === "llmChat") {- // Special handling for our LLM Chat view mode- resultingType = "llmChat"; // This will need to be added to the ExtendedNoteType} else if (viewScope && viewScope.viewMode === "attachments") {resultingType = viewScope.attachmentId ? "attachmentDetail" : "attachmentList";} else if (type === "text" && (await this.noteContext?.isReadOnly())) {diff --git a/src/public/app/widgets/note_type.ts b/src/public/app/widgets/note_type.tsindex aa9b7628d0..a008a0c615 100644--- a/src/public/app/widgets/note_type.ts+++ b/src/public/app/widgets/note_type.ts@@ -38,6 +38,7 @@ const NOTE_TYPES: NoteTypeMapping[] = [// Misc note types{ type: "render", mime: "", title: t("note_types.render-note"), selectable: true },{ type: "webView", mime: "", title: t("note_types.web-view"), selectable: true },+ { type: "aiChat", mime: "application/json", title: t("note_types.ai-chat"), selectable: true },// Code notes{ type: "code", mime: "text/plain", title: t("note_types.code"), selectable: true },diff --git a/src/public/app/widgets/type_widgets/ai_chat.ts b/src/public/app/widgets/type_widgets/ai_chat.tsnew file mode 100644index 0000000000..6386fb8981--- /dev/null+++ b/src/public/app/widgets/type_widgets/ai_chat.ts@@ -0,0 +1,99 @@+import TypeWidget from "./type_widget.js";+import LlmChatPanel from "../llm_chat_panel.js";+import { type EventData } from "../../components/app_context.js";+import type FNote from "../../entities/fnote.js";+import server from "../../services/server.js";++export default class AiChatTypeWidget extends TypeWidget {+ private llmChatPanel: LlmChatPanel;+ private isInitialized: boolean = false;++ constructor() {+ super();+ this.llmChatPanel = new LlmChatPanel();+ }++ static getType() {+ return "aiChat";+ }++ doRender() {+ this.$widget = $('<div class="ai-chat-widget-container" style="height: 100%;"></div>');+ this.$widget.append(this.llmChatPanel.render());++ return this.$widget;+ }++ async doRefresh(note: FNote | null | undefined) {+ // Initialize the chat panel if not already done+ if (!this.isInitialized) {+ console.log("Initializing AI Chat Panel for note:", note?.noteId);+ await this.llmChatPanel.refresh();+ this.isInitialized = true;+ }++ // If this is a newly created note, we can initialize the content+ if (note) {+ try {+ const content = await note.getContent();+ // Check if content is empty+ if (!content || content === '{}') {+ // Initialize with empty chat history+ await this.saveData({+ messages: [],+ title: note.title+ });+ }+ } catch (e) {+ console.error("Error initializing AI Chat note content:", e);+ }+ }+ }++ async entitiesReloadedEvent(data: EventData<"entitiesReloaded">) {+ // We don't need to refresh on entities reloaded for the chat+ }++ async activeContextChangedEvent(data: EventData<"activeContextChanged">) {+ // Only refresh when this becomes active and we're not initialized yet+ if (this.isActive() && !this.isInitialized) {+ await this.llmChatPanel.refresh();+ this.isInitialized = true;+ }+ }++ // Save chat data to the note+ async saveData(data: any) {+ if (!this.note) {+ return;+ }++ try {+ await server.put(`notes/${this.note.noteId}/content`, {+ content: JSON.stringify(data, null, 2)+ });+ } catch (e) {+ console.error("Error saving AI Chat data:", e);+ }+ }++ // Get data from the note+ async getData() {+ if (!this.note) {+ return null;+ }++ try {+ const content = await this.note.getContent();++ if (!content) {+ return null;+ }++ return JSON.parse(content as string);+ } catch (e) {+ console.error("Error loading AI Chat data:", e);+ return null;+ }+ }+}diff --git a/src/public/app/widgets/type_widgets/llm_chat.ts b/src/public/app/widgets/type_widgets/llm_chat.tsdeleted file mode 100644index 7d4b41faf2..0000000000--- a/src/public/app/widgets/type_widgets/llm_chat.ts+++ /dev/null@@ -1,51 +0,0 @@-import TypeWidget from "./type_widget.js";-import LlmChatPanel from "../llm_chat_panel.js";-import { type EventData } from "../../components/app_context.js";-import type FNote from "../../entities/fnote.js";--export default class LlmChatTypeWidget extends TypeWidget {- private llmChatPanel: LlmChatPanel;- private isInitialized: boolean = false;-- constructor() {- super();- this.llmChatPanel = new LlmChatPanel();- }-- static getType() {- return "llmChat";- }-- doRender() {- this.$widget = $('<div class="llm-chat-widget-container" style="height: 100%;"></div>');- this.$widget.append(this.llmChatPanel.render());-- return this.$widget;- }-- async doRefresh(note: FNote | null | undefined) {- // Initialize only once- if (!this.isInitialized) {- console.log("Initializing LLM Chat Panel");- await this.llmChatPanel.refresh();- this.isInitialized = true;- }- }-- async entitiesReloadedEvent(data: EventData<"entitiesReloaded">) {- // We don't need to refresh on entities reloaded for the chat- }-- async activeContextChangedEvent(data: EventData<"activeContextChanged">) {- // Only refresh when this becomes active and we're not initialized yet- if (this.isActive() && !this.isInitialized) {- await this.llmChatPanel.refresh();- this.isInitialized = true;- }- }-- // Handle data saving - we don't need to save anything- getData() {- return {};- }-}diff --git a/src/public/translations/en/translation.json b/src/public/translations/en/translation.jsonindex a7cd6d7217..5317a733e7 100644--- a/src/public/translations/en/translation.json+++ b/src/public/translations/en/translation.json@@ -1550,7 +1550,8 @@"widget": "Widget","confirm-change": "It is not recommended to change note type when note content is not empty. Do you want to continue anyway?","geo-map": "Geo Map",- "beta-feature": "Beta"+ "beta-feature": "Beta",+ "ai-chat": "AI Chat"},"protect_note": {"toggle-on": "Protect the note",@@ -1826,7 +1827,12 @@"confirm_delete_embeddings": "Are you sure you want to delete all AI embeddings? This will remove all semantic search capabilities until notes are reindexed, which can take a significant amount of time.","empty_key_warning": "Warning: Empty API key. You need to configure your API key in settings.","enable_ai": "Enable AI Features",- "enhanced_context_description": "Uses semantic search to find relevant information across your notes",+ "name": "AI",+ "openai": "OpenAI",+ "use_enhanced_context": "Use enhanced context",+ "enhanced_context_description": "Provides the AI with more context from the note and its related notes for better responses",+ "show_thinking": "Show thinking",+ "show_thinking_description": "Show the AI's chain of thought process","enter_message": "Enter your message...","error_contacting_provider": "Error contacting AI provider. Please check your settings and internet connection.","error_generating_response": "Error generating AI response",@@ -1842,15 +1848,17 @@"notes_indexed": "{{ count }} note indexed","notes_indexed_plural": "{{ count }} notes indexed","reset_embeddings": "Reset Embeddings",- "show_thinking": "Show Thinking",- "show_thinking_description": "Reveals the reasoning process used to generate responses","sources": "Sources","start_indexing": "Start Indexing","use_advanced_context": "Use Advanced Context",- "use_enhanced_context": "Use Enhanced Note Context","processing": {- "common": "Processing..."- }+ "common": "Processing...",+ "thinking": "Thinking...",+ "loading": "Loading...",+ "generating": "Generating..."+ },+ "create_new_ai_chat": "Create new AI Chat"+},"switch_layout_button": {"title_vertical": "Move editing pane to the bottom",diff --git a/src/services/hidden_subtree.ts b/src/services/hidden_subtree.tsindex 4892a36ced..9e991169a7 100644--- a/src/services/hidden_subtree.ts+++ b/src/services/hidden_subtree.ts@@ -53,7 +53,7 @@ export interface HiddenSubtreeItem {| "protectedSession"| "calendar"| "quickSearch"- | "llmChatLauncher";+ | "aiChatLauncher";command?: keyof typeof Command;}@@ -64,7 +64,7 @@ enum Command {createNoteIntoInbox,showRecentChanges,showOptions,- showLlmChat+ createAiChat}/*diff --git a/src/services/hidden_subtree_launcherbar.ts b/src/services/hidden_subtree_launcherbar.tsindex a63cc86bfd..061b7207db 100644--- a/src/services/hidden_subtree_launcherbar.ts+++ b/src/services/hidden_subtree_launcherbar.ts@@ -70,10 +70,10 @@ export default function buildLaunchBarConfig() {{ id: "_lbNoteMap", title: t("hidden-subtree.note-map-title"), type: "launcher", targetNoteId: "_globalNoteMap", icon: "bx bxs-network-chart" },{id: "_lbLlmChat",- title: t("hidden-subtree.llm-chat-title"),+ title: t("hidden-subtree.ai-chat-title"),type: "launcher",icon: "bx bx-bot",- builtinWidget: "llmChatLauncher",+ builtinWidget: "aiChatLauncher",attributes: [{ type: "label", name: "desktopOnly" }]diff --git a/src/services/note_types.ts b/src/services/note_types.tsindex a242fc7b22..3b2dc8d662 100644--- a/src/services/note_types.ts+++ b/src/services/note_types.ts@@ -15,7 +15,8 @@ const noteTypes = [{ type: "doc", defaultMime: "" },{ type: "contentWidget", defaultMime: "" },{ type: "mindMap", defaultMime: "application/json" },- { type: "geoMap", defaultMime: "application/json" }+ { type: "geoMap", defaultMime: "application/json" },+ { type: "aiChat", defaultMime: "application/json" }];function getDefaultMimeForNoteType(typeName: string) {