Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): add usage underneath the message

Elian Dorana860803cc4e3
diff --git a/apps/client/src/widgets/sidebar/SidebarChat.css b/apps/client/src/widgets/sidebar/SidebarChat.css
index a2506ee409..f852b219ea 100644
--- a/apps/client/src/widgets/sidebar/SidebarChat.css
+++ b/apps/client/src/widgets/sidebar/SidebarChat.css
@@ -30,11 +30,18 @@
/* Reuse llm-chat-message styles but make them more compact */
.sidebar-chat-messages .llm-chat-message {
padding: 0.5rem 0.75rem;
+ margin-top: 0;
margin-bottom: 0;
max-width: 100%;
font-size: 0.9rem;
}
+/* Usage footer: pull it closer to the message above by collapsing the flex gap */
+.sidebar-chat-messages .llm-chat-usage {
+ margin-top: -0.35rem;
+ margin-bottom: 0;
+}
+
.sidebar-chat-messages .llm-chat-message-role {
font-size: 0.75rem;
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
index 1c3cedf4c4..1bfd71a1e7 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -126,83 +126,85 @@ export default function ChatMessage({ message, isStreaming }: Props) {
const hasBlockContent = Array.isArray(message.content);
return (
- <div className={messageClasses}>
- <div className="llm-chat-message-role">
- {isError ? "Error" : roleLabel}
- </div>
- <div className="llm-chat-message-content">
- {message.role === "assistant" && !isError ? (
- hasBlockContent ? (
- renderContentBlocks(message.content as ContentBlock[], isStreaming)
+ <>
+ <div className={messageClasses}>
+ <div className="llm-chat-message-role">
+ {isError ? "Error" : roleLabel}
+ </div>
+ <div className="llm-chat-message-content">
+ {message.role === "assistant" && !isError ? (
+ hasBlockContent ? (
+ renderContentBlocks(message.content as ContentBlock[], isStreaming)
+ ) : (
+ <>
+ <div
+ className="llm-chat-markdown"
+ dangerouslySetInnerHTML={{ __html: renderedContent || "" }}
+ />
+ {isStreaming && <span className="llm-chat-cursor" />}
+ </>
+ )
) : (
- <>
- <div
- className="llm-chat-markdown"
- dangerouslySetInnerHTML={{ __html: renderedContent || "" }}
- />
- {isStreaming && <span className="llm-chat-cursor" />}
- </>
- )
- ) : (
- textContent
+ textContent
+ )}
+ </div>
+ {legacyToolCalls && legacyToolCalls.length > 0 && (
+ <details className="llm-chat-tool-calls">
+ <summary className="llm-chat-tool-calls-summary">
+ <span className="bx bx-wrench" />
+ {t("llm_chat.tool_calls", { count: legacyToolCalls.length })}
+ </summary>
+ <div className="llm-chat-tool-calls-list">
+ {legacyToolCalls.map((tool) => (
+ <ToolCallCard key={tool.id} toolCall={tool} />
+ ))}
+ </div>
+ </details>
)}
- </div>
- {legacyToolCalls && legacyToolCalls.length > 0 && (
- <details className="llm-chat-tool-calls">
- <summary className="llm-chat-tool-calls-summary">
- <span className="bx bx-wrench" />
- {t("llm_chat.tool_calls", { count: legacyToolCalls.length })}
- </summary>
- <div className="llm-chat-tool-calls-list">
- {legacyToolCalls.map((tool) => (
- <ToolCallCard key={tool.id} toolCall={tool} />
- ))}
- </div>
- </details>
- )}
- {message.citations && message.citations.length > 0 && (
- <div className="llm-chat-citations">
- <div className="llm-chat-citations-label">
- <span className="bx bx-link" />
- {t("llm_chat.sources")}
- </div>
- <ul className="llm-chat-citations-list">
- {message.citations.map((citation, idx) => {
- // Determine display text: title, URL hostname, or cited text
- let displayText = citation.title;
- if (!displayText && citation.url) {
- try {
- displayText = new URL(citation.url).hostname;
- } catch {
- displayText = citation.url;
+ {message.citations && message.citations.length > 0 && (
+ <div className="llm-chat-citations">
+ <div className="llm-chat-citations-label">
+ <span className="bx bx-link" />
+ {t("llm_chat.sources")}
+ </div>
+ <ul className="llm-chat-citations-list">
+ {message.citations.map((citation, idx) => {
+ // Determine display text: title, URL hostname, or cited text
+ let displayText = citation.title;
+ if (!displayText && citation.url) {
+ try {
+ displayText = new URL(citation.url).hostname;
+ } catch {
+ displayText = citation.url;
+ }
+ }
+ if (!displayText) {
+ displayText = citation.citedText?.slice(0, 50) || `Source ${idx + 1}`;
}
- }
- if (!displayText) {
- displayText = citation.citedText?.slice(0, 50) || `Source ${idx + 1}`;
- }
- return (
- <li key={idx}>
- {citation.url ? (
- <a
- href={citation.url}
- target="_blank"
- rel="noopener noreferrer"
- title={citation.citedText || citation.url}
- >
- {displayText}
- </a>
- ) : (
- <span title={citation.citedText}>
- {displayText}
- </span>
- )}
- </li>
- );
- })}
- </ul>
- </div>
- )}
+ return (
+ <li key={idx}>
+ {citation.url ? (
+ <a
+ href={citation.url}
+ target="_blank"
+ rel="noopener noreferrer"
+ title={citation.citedText || citation.url}
+ >
+ {displayText}
+ </a>
+ ) : (
+ <span title={citation.citedText}>
+ {displayText}
+ </span>
+ )}
+ </li>
+ );
+ })}
+ </ul>
+ </div>
+ )}
+ </div>
{message.usage && typeof message.usage.promptTokens === "number" && (
<div className="llm-chat-usage">
{message.usage.model && (
@@ -227,6 +229,6 @@ export default function ChatMessage({ message, isStreaming }: Props) {
)}
</div>
)}
- </div>
+ </>
);
}
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 29788d0ad8..15dee696bc 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css
@@ -13,13 +13,22 @@
}
.llm-chat-message {
- margin-bottom: 1rem;
+ margin-top: 1rem;
padding: 0.75rem 1rem;
border-radius: 8px;
max-width: 85%;
user-select: text;
}
+.llm-chat-message:first-child {
+ margin-top: 0;
+}
+
+/* Collapse gap between the usage footer and the next message */
+.llm-chat-usage + .llm-chat-message {
+ margin-top: 0.5rem;
+}
+
.llm-chat-message-user {
background: var(--accented-background-color);
margin-left: auto;
@@ -609,15 +618,14 @@
color: var(--danger-color, #dc3545);
}
-/* Token usage display */
+/* Token usage display (sits below the message bubble) */
.llm-chat-usage {
display: flex;
align-items: center;
gap: 0.375rem;
- margin-top: 0.75rem;
- padding-top: 0.5rem;
- border-top: 1px solid var(--main-border-color);
- font-size: 0.75rem;
+ margin-top: 0.25rem;
+ padding: 0 0.5rem;
+ font-size: 0.7rem;
color: var(--muted-text-color);
cursor: default;
}