Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): integrate CKEditor with mentions for input box

Elian Doran5a85249c341d
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.css b/apps/client/src/widgets/sidebar/SidebarChat.css
index 761d05a9ff..420d27a489 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.css
+++ b/apps/client/src/widgets/sidebar/SidebarChat.css
@@ -15,6 +15,7 @@
.llm-chat-input {
font-size: 0.9em;
padding: 0.5em;
+ min-height: 40px;
}
}
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsx
index 7c6f247f14..5c39120416 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx
@@ -151,13 +151,6 @@ export default function SidebarChat() {
await chat.handleSubmit(e);
}, [chatNoteId, chat]);
- const handleKeyDown = useCallback((e: KeyboardEvent) => {
- if (e.key === "Enter" && !e.shiftKey) {
- e.preventDefault();
- handleSubmit(e);
- }
- }, [handleSubmit]);
-
const handleNewChat = useCallback(async () => {
// Save any pending changes before switching
await spacedUpdate.updateNowIfNecessary();
@@ -298,11 +291,9 @@ export default function SidebarChat() {
/>
<ChatInputBar
chat={chat}
- rows={2}
activeNoteId={activeNoteId ?? undefined}
activeNoteTitle={activeNote?.title}
onSubmit={handleSubmit}
- onKeyDown={handleKeyDown}
/>
</div>
</RightPanelWidget>
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
index add5b14d42..0a68f04142 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.css
@@ -11,29 +11,49 @@
flex: 1;
min-height: 60px;
max-height: 200px;
- height: auto;
- field-sizing: content;
- resize: none;
+ overflow-y: auto;
padding: 0.75rem;
- border: 1px solid var(--main-border-color);
+ border: 1px solid var(--main-border-color) !important;
border-radius: 8px;
font-family: inherit;
font-size: inherit;
background: var(--main-background-color);
color: var(--main-text-color);
+ transition: border-color 0.1s linear, box-shadow 0.1s linear;
}
-.llm-chat-input:focus {
+/* CKEditor adds .ck-content / .ck-focused to the same element it took over. */
+.llm-chat-input.ck-content {
outline: none;
+
+ p {
+ margin: 0 !important;
+ }
+}
+
+.llm-chat-input.ck-focused {
border-color: var(--main-selection-color);
box-shadow: 0 0 0 2px var(--main-selection-color-soft, rgba(0, 123, 255, 0.25));
}
-.llm-chat-input:disabled {
+.llm-chat-input.ck-read-only {
opacity: 0.6;
cursor: not-allowed;
}
+.llm-chat-input.ck-content p {
+ margin: 0;
+}
+
+.llm-chat-input.ck-content p + p {
+ margin-top: 0.25rem;
+}
+
+.llm-chat-input.ck-content .mention {
+ color: var(--main-selection-color, #007bff);
+ background: transparent;
+}
+
/* Options row */
.llm-chat-options {
display: flex;
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index b4515d2bb4..9d887f1475 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -1,35 +1,87 @@
import "./ChatInputBar.css";
-import type { RefObject } from "preact";
-import { useState, useCallback } from "preact/hooks";
+import { AttributeEditor as CKEditorAttributeEditor, type CKTextEditor, type MentionFeed } from "@triliumnext/ckeditor5";
+import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
+import link from "../../../services/link.js";
+import note_autocomplete, { type Suggestion } from "../../../services/note_autocomplete.js";
+import options from "../../../services/options.js";
import ActionButton from "../../react/ActionButton.js";
import Button from "../../react/Button.js";
+import CKEditor, { type CKEditorApi } from "../../react/CKEditor.js";
import Dropdown from "../../react/Dropdown.js";
import { FormDropdownDivider, FormDropdownSubmenu, FormListItem, FormListToggleableItem } from "../../react/FormList.js";
-import type { UseLlmChatReturn } from "./useLlmChat.js";
+import { useLegacyImperativeHandlers } from "../../react/hooks.js";
import AddProviderModal, { type LlmProviderConfig } from "../options/llm/AddProviderModal.js";
-import options from "../../../services/options.js";
+import type { UseLlmChatReturn } from "./useLlmChat.js";
+
+const READ_ONLY_LOCK = "llm-chat-streaming";
+
+const mentionFeeds: MentionFeed[] = [
+ {
+ marker: "@",
+ feed: (queryText) => note_autocomplete.autocompleteSourceForCKEditor(queryText),
+ itemRenderer: (rawItem) => {
+ const item = rawItem as Suggestion;
+ const itemElement = document.createElement("button");
+
+ const iconElement = document.createElement("span");
+ let iconClass = item.icon ?? "bx bx-note";
+ if (item.action === "create-note") {
+ iconClass = "bx bx-plus";
+ }
+ iconElement.className = iconClass;
+
+ itemElement.append(iconElement, document.createTextNode(" "));
+ const titleContainer = document.createElement("span");
+ titleContainer.innerHTML = item.highlightedNotePathTitle ?? "";
+ itemElement.append(...titleContainer.childNodes, document.createTextNode(" "));
+
+ return itemElement;
+ },
+ minimumCharacters: 0
+ }
+];
/** Format token count with thousands separators */
function formatTokenCount(tokens: number): string {
return tokens.toLocaleString();
}
+/**
+ * Convert CKEditor HTML into plain text suitable for an LLM prompt.
+ *
+ * Paragraphs and <br> become newlines. Note reference links (anchors with a
+ * `#root/...` href) are rendered as markdown-style `[Title](#root/noteId)` so
+ * the LLM sees both the human-readable title and the addressable note path it
+ * can feed into note tools.
+ */
+function htmlToPlainText(html: string): string {
+ const container = document.createElement("div");
+ container.innerHTML = html;
+ container.querySelectorAll<HTMLAnchorElement>("a[href^='#']").forEach((a) => {
+ const href = a.getAttribute("href") ?? "";
+ const title = (a.textContent ?? "").trim();
+ a.replaceWith(`[${title}](${href})`);
+ });
+ container.querySelectorAll("br").forEach((br) => br.replaceWith("\n"));
+ const blocks = Array.from(container.children) as HTMLElement[];
+ if (blocks.length === 0) {
+ return (container.textContent ?? "").trim();
+ }
+ return blocks.map((b) => b.textContent ?? "").join("\n").trim();
+}
+
interface ChatInputBarProps {
/** The chat hook result */
chat: UseLlmChatReturn;
- /** Number of rows for the textarea (default: 3) */
- rows?: number;
/** Current active note ID (for note context toggle) */
activeNoteId?: string;
/** Current active note title (for note context toggle) */
activeNoteTitle?: string;
/** Custom submit handler (overrides chat.handleSubmit) */
onSubmit?: (e: Event) => void;
- /** Custom key down handler (overrides chat.handleKeyDown) */
- onKeyDown?: (e: KeyboardEvent) => void;
/** Callback when web search toggle changes */
onWebSearchChange?: () => void;
/** Callback when note tools toggle changes */
@@ -42,20 +94,50 @@ interface ChatInputBarProps {
export default function ChatInputBar({
chat,
- rows = 3,
activeNoteId,
activeNoteTitle,
onSubmit,
- onKeyDown,
onWebSearchChange,
onNoteToolsChange,
onExtendedThinkingChange,
onModelChange
}: ChatInputBarProps) {
const [showAddProviderModal, setShowAddProviderModal] = useState(false);
+ const editorApiRef = useRef<CKEditorApi>();
+ const editorInstanceRef = useRef<CKTextEditor>();
+ const editorHtmlRef = useRef<string>("");
+ // Always-fresh submit handler for the editor's enter listener.
+ const submitRef = useRef<(e: Event) => void>(() => {});
const handleSubmit = onSubmit ?? chat.handleSubmit;
- const handleKeyDown = onKeyDown ?? chat.handleKeyDown;
+ submitRef.current = handleSubmit;
+
+ // CKEditor's ReferenceLink plugin calls back into the parent component to
+ // resolve a note's title from its href.
+ useLegacyImperativeHandlers({
+ async loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | null = null) {
+ await link.loadReferenceLinkTitle($el, href);
+ }
+ });
+
+ // Clear the editor when external code resets the prompt (e.g. after submit).
+ useEffect(() => {
+ if (chat.input === "" && editorHtmlRef.current !== "") {
+ editorApiRef.current?.setText("");
+ editorHtmlRef.current = "";
+ }
+ }, [chat.input]);
+
+ // Reflect streaming state into CKEditor's read-only lock.
+ useEffect(() => {
+ const editor = editorInstanceRef.current;
+ if (!editor) return;
+ if (chat.isStreaming) {
+ editor.enableReadOnlyMode(READ_ONLY_LOCK);
+ } else {
+ editor.disableReadOnlyMode(READ_ONLY_LOCK);
+ }
+ }, [chat.isStreaming]);
const handleWebSearchToggle = (newValue: boolean) => {
chat.setEnableWebSearch(newValue);
@@ -129,15 +211,35 @@ export default function ChatInputBar({
return (
<form className="llm-chat-input-form" onSubmit={handleSubmit}>
- <textarea
- ref={chat.textareaRef as RefObject<HTMLTextAreaElement>}
+ <CKEditor
+ apiRef={editorApiRef}
className="llm-chat-input"
- value={chat.input}
- onInput={(e) => chat.setInput((e.target as HTMLTextAreaElement).value)}
- placeholder={t("llm_chat.placeholder")}
- disabled={chat.isStreaming}
- onKeyDown={handleKeyDown}
- rows={rows}
+ editor={CKEditorAttributeEditor}
+ config={{
+ toolbar: { items: [] },
+ placeholder: t("llm_chat.placeholder"),
+ mention: { feeds: mentionFeeds },
+ licenseKey: "GPL",
+ language: "en"
+ }}
+ onChange={(html) => {
+ editorHtmlRef.current = html ?? "";
+ chat.setInput(htmlToPlainText(html ?? ""));
+ }}
+ onInitialized={(editor) => {
+ editorInstanceRef.current = editor;
+ // Enter submits, Shift+Enter falls through to ShiftEnter (soft break).
+ editor.editing.view.document.on(
+ "enter",
+ (event, data) => {
+ if (data.isSoft) return;
+ event.stop();
+ data.preventDefault();
+ submitRef.current(new Event("submit"));
+ },
+ { priority: "high" }
+ );
+ }}
/>
<div className="llm-chat-options">
<div className="llm-chat-model-selector">
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
index a391f0ff61..d85ad8f655 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
@@ -39,7 +39,6 @@ export interface UseLlmChatReturn {
lastPromptTokens: number;
messagesEndRef: RefObject<HTMLDivElement>;
scrollContainerRef: RefObject<HTMLDivElement>;
- textareaRef: RefObject<HTMLTextAreaElement>;
/** Whether a provider is configured and available */
hasProvider: boolean;
/** Whether we're still checking for providers */
@@ -94,7 +93,6 @@ export function useLlmChat(
const [isCheckingProvider, setIsCheckingProvider] = useState<boolean>(true);
const messagesEndRef = useRef<HTMLDivElement>(null);
const scrollContainerRef = useRef<HTMLDivElement>(null);
- const textareaRef = useRef<HTMLTextAreaElement>(null);
const abortControllerRef = useRef<AbortController | null>(null);
const isNearBottomRef = useRef(true);
@@ -519,7 +517,6 @@ export function useLlmChat(
lastPromptTokens,
messagesEndRef,
scrollContainerRef,
- textareaRef,
hasProvider,
isCheckingProvider,