Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): extract chat input bar into separate component

Elian Doran734ef5533a9e
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.css b/apps/client/src/widgets/sidebar/SidebarChat.css
index 31239ad0a3..28109fec73 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.css
+++ b/apps/client/src/widgets/sidebar/SidebarChat.css
@@ -35,108 +35,6 @@
max-width: 100%;
}
-/* Input area */
-.sidebar-chat-input-area {
- padding: 0.5rem;
- border-top: 1px solid var(--main-border-color);
- display: flex;
- flex-direction: column;
- gap: 0.375rem;
-}
-
-.sidebar-chat-input {
- width: 100%;
- min-height: 50px;
- max-height: 120px;
- resize: vertical;
- padding: 0.5rem;
- border: 1px solid var(--main-border-color);
- border-radius: 6px;
- font-family: inherit;
- font-size: 0.9rem;
- background: var(--main-background-color);
- color: var(--main-text-color);
-}
-
-.sidebar-chat-input:focus {
- outline: none;
- border-color: var(--main-selection-color);
- box-shadow: 0 0 0 2px var(--main-selection-color-soft, rgba(0, 123, 255, 0.25));
-}
-
-.sidebar-chat-input:disabled {
- opacity: 0.6;
- cursor: not-allowed;
-}
-
-.sidebar-chat-actions {
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 0.5rem;
-}
-
-.sidebar-chat-options {
- display: flex;
- gap: 0.5rem;
-}
-
-.sidebar-chat-toggle {
- display: flex;
- align-items: center;
- justify-content: center;
- cursor: pointer;
- color: var(--muted-text-color);
- padding: 0.25rem;
-}
-
-.sidebar-chat-toggle input[type="checkbox"] {
- display: none;
-}
-
-.sidebar-chat-toggle .bx {
- font-size: 1.1rem;
-}
-
-.sidebar-chat-toggle:has(input:checked) {
- color: var(--main-text-color);
-}
-
-.sidebar-chat-toggle:has(input:checked) .bx {
- color: var(--main-selection-color);
-}
-
-.sidebar-chat-toggle:has(input:disabled) {
- opacity: 0.5;
- cursor: not-allowed;
-}
-
-.sidebar-chat-send-btn {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 0.5rem 0.75rem;
- background: var(--button-background-color);
- border: 1px solid var(--button-border-color);
- border-radius: 6px;
- cursor: pointer;
- color: var(--button-text-color);
- transition: background-color 0.15s ease;
-}
-
-.sidebar-chat-send-btn .bx {
- font-size: 1rem;
-}
-
-.sidebar-chat-send-btn:hover:not(:disabled) {
- background: var(--button-hover-background-color, var(--button-background-color));
-}
-
-.sidebar-chat-send-btn:disabled {
- opacity: 0.5;
- cursor: not-allowed;
-}
-
/* Make the sidebar chat widget grow to fill available space when expanded */
.widget.grow:not(.collapsed) {
min-height: 0;
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.tsx b/apps/client/src/widgets/sidebar/SidebarChat.tsx
index 2a1d866213..d52a5cd07b 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.tsx
+++ b/apps/client/src/widgets/sidebar/SidebarChat.tsx
@@ -8,6 +8,7 @@ import ActionButton from "../react/ActionButton.js";
import Dropdown from "../react/Dropdown.js";
import { FormListItem } from "../react/FormList.js";
import NoItems from "../react/NoItems.js";
+import ChatInputBar from "../type_widgets/llm_chat/ChatInputBar.js";
import ChatMessage from "../type_widgets/llm_chat/ChatMessage.js";
import type { LlmChatContent } from "../type_widgets/llm_chat/llm_chat_types.js";
import { useLlmChat } from "../type_widgets/llm_chat/useLlmChat.js";
@@ -291,48 +292,12 @@ export default function SidebarChat() {
)}
<div ref={chat.messagesEndRef} />
</div>
- <div className="sidebar-chat-input-area">
- <textarea
- ref={chat.textareaRef}
- className="sidebar-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={2}
- />
- <div className="sidebar-chat-actions">
- <div className="sidebar-chat-options">
- <label className="sidebar-chat-toggle" title={t("llm_chat.web_search")}>
- <input
- type="checkbox"
- checked={chat.enableWebSearch}
- onChange={() => chat.setEnableWebSearch(!chat.enableWebSearch)}
- disabled={chat.isStreaming}
- />
- <span className="bx bx-globe" />
- </label>
- <label className="sidebar-chat-toggle" title={t("llm_chat.note_tools")}>
- <input
- type="checkbox"
- checked={chat.enableNoteTools}
- onChange={() => chat.setEnableNoteTools(!chat.enableNoteTools)}
- disabled={chat.isStreaming}
- />
- <span className="bx bx-note" />
- </label>
- </div>
- <button
- type="button"
- className="sidebar-chat-send-btn"
- disabled={chat.isStreaming || !chat.input.trim()}
- onClick={handleSubmit}
- >
- <span className="bx bx-send" />
- </button>
- </div>
- </div>
+ <ChatInputBar
+ chat={chat}
+ onSubmit={handleSubmit}
+ onKeyDown={handleKeyDown}
+ compact
+ />
</div>
</RightPanelWidget>
);
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
new file mode 100644
index 0000000000..555f48b8f1
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -0,0 +1,210 @@
+import type { RefObject } from "preact";
+import type { UseLlmChatReturn } from "./useLlmChat.js";
+import { t } from "../../../services/i18n.js";
+import FormDropdownList from "../../react/FormDropdownList.js";
+
+/** Format token count with thousands separators */
+function formatTokenCount(tokens: number): string {
+ return tokens.toLocaleString();
+}
+
+interface ChatInputBarProps {
+ /** The chat hook result */
+ chat: UseLlmChatReturn;
+ /** 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 */
+ onNoteToolsChange?: () => void;
+ /** Callback when extended thinking toggle changes */
+ onExtendedThinkingChange?: () => void;
+ /** Callback when model changes */
+ onModelChange?: (model: string) => void;
+ /** Whether to show the model selector */
+ showModelSelector?: boolean;
+ /** Whether to show the extended thinking toggle */
+ showExtendedThinking?: boolean;
+ /** Whether to show the context pie chart */
+ showContextPie?: boolean;
+ /** Compact mode for sidebar */
+ compact?: boolean;
+}
+
+export default function ChatInputBar({
+ chat,
+ onSubmit,
+ onKeyDown,
+ onWebSearchChange,
+ onNoteToolsChange,
+ onExtendedThinkingChange,
+ onModelChange,
+ showModelSelector = false,
+ showExtendedThinking = false,
+ showContextPie = false,
+ compact = false
+}: ChatInputBarProps) {
+ const handleSubmit = onSubmit ?? chat.handleSubmit;
+ const handleKeyDown = onKeyDown ?? chat.handleKeyDown;
+
+ const handleWebSearchToggle = () => {
+ chat.setEnableWebSearch(!chat.enableWebSearch);
+ onWebSearchChange?.();
+ };
+
+ const handleNoteToolsToggle = () => {
+ chat.setEnableNoteTools(!chat.enableNoteTools);
+ onNoteToolsChange?.();
+ };
+
+ const handleExtendedThinkingToggle = () => {
+ chat.setEnableExtendedThinking(!chat.enableExtendedThinking);
+ onExtendedThinkingChange?.();
+ };
+
+ const handleModelSelect = (model: string) => {
+ chat.setSelectedModel(model);
+ onModelChange?.(model);
+ };
+
+ if (compact) {
+ return (
+ <div className="llm-chat-input-bar compact">
+ <textarea
+ ref={chat.textareaRef as RefObject<HTMLTextAreaElement>}
+ 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={2}
+ />
+ <div className="llm-chat-input-bar-actions">
+ <div className="llm-chat-input-bar-options">
+ <label className="llm-chat-toggle" title={t("llm_chat.web_search")}>
+ <input
+ type="checkbox"
+ checked={chat.enableWebSearch}
+ onChange={handleWebSearchToggle}
+ disabled={chat.isStreaming}
+ />
+ <span className="bx bx-globe" />
+ </label>
+ <label className="llm-chat-toggle" title={t("llm_chat.note_tools")}>
+ <input
+ type="checkbox"
+ checked={chat.enableNoteTools}
+ onChange={handleNoteToolsToggle}
+ disabled={chat.isStreaming}
+ />
+ <span className="bx bx-note" />
+ </label>
+ </div>
+ <button
+ type="button"
+ className="llm-chat-send-btn"
+ disabled={chat.isStreaming || !chat.input.trim()}
+ onClick={handleSubmit}
+ >
+ <span className="bx bx-send" />
+ </button>
+ </div>
+ </div>
+ );
+ }
+
+ // Full mode for main LLM chat
+ return (
+ <form className="llm-chat-input-form" onSubmit={handleSubmit}>
+ <div className="llm-chat-input-row">
+ <textarea
+ ref={chat.textareaRef as RefObject<HTMLTextAreaElement>}
+ 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={3}
+ />
+ <button
+ type="submit"
+ className="llm-chat-send-btn"
+ disabled={chat.isStreaming || !chat.input.trim()}
+ >
+ {chat.isStreaming ? t("llm_chat.sending") : t("llm_chat.send")}
+ </button>
+ </div>
+ <div className="llm-chat-options">
+ {showModelSelector && (
+ <div className="llm-chat-model-selector">
+ <span className="bx bx-chip" />
+ <FormDropdownList
+ values={chat.availableModels}
+ keyProperty="id"
+ titleProperty="name"
+ descriptionProperty="costDescription"
+ currentValue={chat.selectedModel}
+ onChange={handleModelSelect}
+ disabled={chat.isStreaming}
+ buttonClassName="llm-chat-model-select"
+ />
+ </div>
+ )}
+ <label className="llm-chat-toggle">
+ <input
+ type="checkbox"
+ checked={chat.enableWebSearch}
+ onChange={handleWebSearchToggle}
+ disabled={chat.isStreaming}
+ />
+ <span className="bx bx-globe" />
+ {t("llm_chat.web_search")}
+ </label>
+ <label className="llm-chat-toggle">
+ <input
+ type="checkbox"
+ checked={chat.enableNoteTools}
+ onChange={handleNoteToolsToggle}
+ disabled={chat.isStreaming}
+ />
+ <span className="bx bx-note" />
+ {t("llm_chat.note_tools")}
+ </label>
+ {showExtendedThinking && (
+ <label className="llm-chat-toggle">
+ <input
+ type="checkbox"
+ checked={chat.enableExtendedThinking}
+ onChange={handleExtendedThinkingToggle}
+ disabled={chat.isStreaming}
+ />
+ <span className="bx bx-brain" />
+ {t("llm_chat.extended_thinking")}
+ </label>
+ )}
+ {showContextPie && chat.lastPromptTokens > 0 && (() => {
+ const currentModel = chat.availableModels.find(m => m.id === chat.selectedModel);
+ const contextWindow = currentModel?.contextWindow || 200000;
+ const percentage = Math.min((chat.lastPromptTokens / contextWindow) * 100, 100);
+ const isWarning = percentage > 75;
+ const isCritical = percentage > 90;
+ const color = isCritical ? "var(--danger-color, #d9534f)" : isWarning ? "var(--warning-color, #f0ad4e)" : "var(--main-selection-color, #007bff)";
+
+ return (
+ <div
+ className="llm-chat-context-pie"
+ title={`${formatTokenCount(chat.lastPromptTokens)} / ${formatTokenCount(contextWindow)} ${t("llm_chat.tokens")} (${percentage.toFixed(0)}%)`}
+ style={{
+ background: `conic-gradient(${color} ${percentage}%, var(--accented-background-color) ${percentage}%)`
+ }}
+ />
+ );
+ })()}
+ </div>
+ </form>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
index fb8ade6518..a72ce5f134 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
@@ -575,3 +575,96 @@
cursor: help;
flex-shrink: 0;
}
+
+/* Compact mode for sidebar */
+.llm-chat-input-bar.compact {
+ padding: 0.5rem;
+ border-top: 1px solid var(--main-border-color);
+ display: flex;
+ flex-direction: column;
+ gap: 0.375rem;
+}
+
+.llm-chat-input-bar.compact .llm-chat-input {
+ width: 100%;
+ min-height: 50px;
+ max-height: 120px;
+ resize: vertical;
+ padding: 0.5rem;
+ border: 1px solid var(--main-border-color);
+ border-radius: 6px;
+ font-family: inherit;
+ font-size: 0.9rem;
+ background: var(--main-background-color);
+ color: var(--main-text-color);
+}
+
+.llm-chat-input-bar-actions {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 0.5rem;
+}
+
+.llm-chat-input-bar-options {
+ display: flex;
+ gap: 0.5rem;
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ cursor: pointer;
+ color: var(--muted-text-color);
+ padding: 0.25rem;
+ gap: 0;
+ font-size: inherit;
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle input[type="checkbox"] {
+ display: none;
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle .bx {
+ font-size: 1.1rem;
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle:has(input:checked) {
+ color: var(--main-text-color);
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle:has(input:checked) .bx {
+ color: var(--main-selection-color);
+}
+
+.llm-chat-input-bar.compact .llm-chat-toggle:has(input:disabled) {
+ opacity: 0.5;
+ cursor: not-allowed;
+}
+
+.llm-chat-input-bar.compact .llm-chat-send-btn {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ padding: 0.5rem 0.75rem;
+ background: var(--button-background-color);
+ border: 1px solid var(--button-border-color);
+ border-radius: 6px;
+ cursor: pointer;
+ color: var(--button-text-color);
+ transition: background-color 0.15s ease;
+}
+
+.llm-chat-input-bar.compact .llm-chat-send-btn .bx {
+ font-size: 1rem;
+}
+
+.llm-chat-input-bar.compact .llm-chat-send-btn:hover:not(:disabled) {
+ background: var(--button-hover-background-color, var(--button-background-color));
+}
+
+.llm-chat-input-bar.compact .llm-chat-send-btn:disabled {
+ opacity: 0.5;
+ cursor: not-allowed;
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
index 1b86b2ab2b..a3e33c38f9 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
@@ -2,18 +2,13 @@ import { useCallback, useEffect, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
import { useEditorSpacedUpdate } from "../../react/hooks.js";
-import FormDropdownList from "../../react/FormDropdownList.js";
import { TypeWidgetProps } from "../type_widget.js";
+import ChatInputBar from "./ChatInputBar.js";
import ChatMessage from "./ChatMessage.js";
import type { LlmChatContent } from "./llm_chat_types.js";
import { useLlmChat } from "./useLlmChat.js";
import "./LlmChat.css";
-/** Format token count with thousands separators */
-function formatTokenCount(tokens: number): string {
- return tokens.toLocaleString();
-}
-
export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
const [shouldSave, setShouldSave] = useState(false);
@@ -54,25 +49,9 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
}
}, [shouldSave, spacedUpdate]);
- const toggleWebSearch = useCallback(() => {
- chat.setEnableWebSearch(!chat.enableWebSearch);
- setShouldSave(true);
- }, [chat]);
-
- const toggleNoteTools = useCallback(() => {
- chat.setEnableNoteTools(!chat.enableNoteTools);
- setShouldSave(true);
- }, [chat]);
-
- const toggleExtendedThinking = useCallback(() => {
- chat.setEnableExtendedThinking(!chat.enableExtendedThinking);
+ const triggerSave = useCallback(() => {
setShouldSave(true);
- }, [chat]);
-
- const handleModelChange = useCallback((newModel: string) => {
- chat.setSelectedModel(newModel);
- setShouldSave(true);
- }, [chat]);
+ }, []);
return (
<div className="llm-chat-container">
@@ -117,90 +96,16 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
)}
<div ref={chat.messagesEndRef} />
</div>
- <form className="llm-chat-input-form" onSubmit={chat.handleSubmit}>
- <div className="llm-chat-input-row">
- <textarea
- ref={chat.textareaRef}
- 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={chat.handleKeyDown}
- rows={3}
- />
- <button
- type="submit"
- className="llm-chat-send-btn"
- disabled={chat.isStreaming || !chat.input.trim()}
- >
- {chat.isStreaming ? t("llm_chat.sending") : t("llm_chat.send")}
- </button>
- </div>
- <div className="llm-chat-options">
- <div className="llm-chat-model-selector">
- <span className="bx bx-chip" />
- <FormDropdownList
- values={chat.availableModels}
- keyProperty="id"
- titleProperty="name"
- descriptionProperty="costDescription"
- currentValue={chat.selectedModel}
- onChange={handleModelChange}
- disabled={chat.isStreaming}
- buttonClassName="llm-chat-model-select"
- />
- </div>
- <label className="llm-chat-toggle">
- <input
- type="checkbox"
- checked={chat.enableWebSearch}
- onChange={toggleWebSearch}
- disabled={chat.isStreaming}
- />
- <span className="bx bx-globe" />
- {t("llm_chat.web_search")}
- </label>
- <label className="llm-chat-toggle">
- <input
- type="checkbox"
- checked={chat.enableNoteTools}
- onChange={toggleNoteTools}
- disabled={chat.isStreaming}
- />
- <span className="bx bx-note" />
- {t("llm_chat.note_tools")}
- </label>
- <label className="llm-chat-toggle">
- <input
- type="checkbox"
- checked={chat.enableExtendedThinking}
- onChange={toggleExtendedThinking}
- disabled={chat.isStreaming}
- />
- <span className="bx bx-brain" />
- {t("llm_chat.extended_thinking")}
- </label>
- {chat.lastPromptTokens > 0 && (() => {
- const currentModel = chat.availableModels.find(m => m.id === chat.selectedModel);
- const contextWindow = currentModel?.contextWindow || 200000;
- const percentage = Math.min((chat.lastPromptTokens / contextWindow) * 100, 100);
- const isWarning = percentage > 75;
- const isCritical = percentage > 90;
- const color = isCritical ? "var(--danger-color, #d9534f)" : isWarning ? "var(--warning-color, #f0ad4e)" : "var(--main-selection-color, #007bff)";
-
- return (
- <div
- className="llm-chat-context-pie"
- title={`${formatTokenCount(chat.lastPromptTokens)} / ${formatTokenCount(contextWindow)} ${t("llm_chat.tokens")} (${percentage.toFixed(0)}%)`}
- style={{
- background: `conic-gradient(${color} ${percentage}%, var(--accented-background-color) ${percentage}%)`
- }}
- />
- );
- })()}
- </div>
- </form>
+ <ChatInputBar
+ chat={chat}
+ onWebSearchChange={triggerSave}
+ onNoteToolsChange={triggerSave}
+ onExtendedThinkingChange={triggerSave}
+ onModelChange={triggerSave}
+ showModelSelector
+ showExtendedThinking
+ showContextPie
+ />
</div>
);
}