Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

perf(desktop): virtualize off-screen chat messages

Josué279bc6b4c8f6
diff --git a/desktop-app/ui/src/components/agents/ChatThread.tsx b/desktop-app/ui/src/components/agents/ChatThread.tsx
index 010fba650..f3d139a72 100644
--- a/desktop-app/ui/src/components/agents/ChatThread.tsx
+++ b/desktop-app/ui/src/components/agents/ChatThread.tsx
@@ -1,6 +1,7 @@
import {
Fragment,
type ReactElement,
+ type ReactNode,
useCallback,
useEffect,
useMemo,
@@ -55,6 +56,70 @@ type RenderableChatMessage = AgentChatMessage & {
messageKey: string
}
+const VIRTUAL_MESSAGE_GROUPS_PER_CHUNK = 8
+
+function VirtualizedMessageChunk({
+ estimatedHeight,
+ initiallyVisible,
+ render,
+}: {
+ estimatedHeight: number
+ initiallyVisible: boolean
+ render: () => ReactNode
+}) {
+ const containerRef = useRef<HTMLDivElement | null>(null)
+ const [visible, setVisible] = useState(initiallyVisible)
+ const measuredHeightRef = useRef(estimatedHeight)
+
+ useEffect(() => {
+ const element = containerRef.current
+ if (!element || typeof IntersectionObserver === 'undefined') {
+ setVisible(true)
+ return
+ }
+ const observer = new IntersectionObserver(
+ entries => {
+ const entry = entries[0]
+ if (!entry) return
+ if (entry.isIntersecting) {
+ setVisible(true)
+ } else if (measuredHeightRef.current > 0) {
+ setVisible(false)
+ }
+ },
+ {
+ root: element.closest('.chat-thread'),
+ rootMargin: '1000px 0px',
+ }
+ )
+ observer.observe(element)
+ return () => observer.disconnect()
+ }, [])
+
+ useEffect(() => {
+ const element = containerRef.current
+ if (!element || !visible || typeof ResizeObserver === 'undefined') return
+ const updateHeight = () => {
+ const height = element.getBoundingClientRect().height
+ if (height > 0) measuredHeightRef.current = height
+ }
+ updateHeight()
+ const observer = new ResizeObserver(updateHeight)
+ observer.observe(element)
+ return () => observer.disconnect()
+ }, [visible])
+
+ return (
+ <div
+ ref={containerRef}
+ className="virtualized-message-chunk"
+ style={visible ? undefined : { height: measuredHeightRef.current }}
+ >
+ {visible ? render() : null}
+ </div>
+ )
+}
+
type WorkflowArtifactScope = {
namespace: string
name: string
@@ -223,6 +288,17 @@ export function ChatThread({ showAgentLabel = false }: ChatThreadProps) {
})),
[groupedMessages]
)
+ const messageGroupChunks = useMemo(() => {
+ const indexedGroups = groupedWithKeys.map((group, groupIndex) => ({ group, groupIndex }))
+ return Array.from(
+ { length: Math.ceil(indexedGroups.length / VIRTUAL_MESSAGE_GROUPS_PER_CHUNK) },
+ (_, chunkIndex) =>
+ indexedGroups.slice(
+ chunkIndex * VIRTUAL_MESSAGE_GROUPS_PER_CHUNK,
+ (chunkIndex + 1) * VIRTUAL_MESSAGE_GROUPS_PER_CHUNK
+ )
+ )
+ }, [groupedWithKeys])
const localMessageIds = useMemo(
() => new Set(activeMessages.map(message => message.id)),
@@ -460,186 +536,209 @@ export function ChatThread({ showAgentLabel = false }: ChatThreadProps) {
disabled={olderMessagesLoading}
onClick={() => void handleLoadOlderMessages()}
>
- {olderMessagesLoading ? 'Loading older messages...' : 'Load older messages'}
+ {olderMessagesLoading ? 'Loading older messages…' : 'Load older messages'}
</Button>
</div>
)}
{activeChatId &&
- groupedWithKeys.map((group, groupIndex) => {
- const prevGroup = groupIndex > 0 ? groupedWithKeys[groupIndex - 1] : null
-
- return (
- <Fragment key={group.groupKey}>
- <section className={`chat-group ${group.role}`}>
- {group.items.map((message, messageIndex) => {
- const parsedUserMessage =
- message.role === 'user' ? parseChatMessageDisplay(message.content) : null
- const displayContent = parsedUserMessage?.content ?? message.content
- const displayAttachments =
- message.role === 'user'
- ? message.attachments && message.attachments.length
- ? message.attachments
- : (parsedUserMessage?.attachments ?? [])
- : message.attachments && message.attachments.length
- ? message.attachments
- : []
- const hasResponseFileAttachment = displayAttachments.some(
- attachment => attachment.type === 'response_file'
- )
- const isJson = looksLikeJson(displayContent)
- const htmlPreview =
- message.role === 'assistant' ? extractHtmlVisualization(displayContent) : null
- const hasHtmlArtifactMention =
- message.role === 'assistant' &&
- /\b[a-zA-Z0-9][a-zA-Z0-9._-]*\.html?\b/i.test(displayContent)
- const showMetaRow = message.role === 'assistant' || message.role === 'user'
- const copyContent = message.role === 'user' ? displayContent : message.content
- const metaRowRoleClass =
- message.role === 'user'
- ? 'chat-message-meta-row--user'
- : message.role === 'assistant'
- ? 'chat-message-meta-row--assistant'
- : ''
- const isLastAssistantMessage =
- group.role === 'assistant' && messageIndex === group.items.length - 1
- const workflowArtifactScopeForAssistant =
- message.role === 'assistant' && prevGroup?.role === 'user'
- ? (prevGroup.items
- .map(prevMessage =>
- extractWorkflowArtifactScopeFromProgress(
- progressByMessageId[prevMessage.id]
- )
- )
- .find((scope): scope is WorkflowArtifactScope => scope !== null) ?? null)
- : null
- return (
- <article
- key={message.messageKey}
- data-testid={message.role === 'assistant' ? 'agent-response' : undefined}
- className={`chat-bubble ${message.role}${message.isError ? ' chat-bubble--error' : ''}${
- htmlPreview || hasHtmlArtifactMention ? ' chat-bubble--wide' : ''
- }`}
- >
- {message.isError ? (
- <div className="error-bubble-content">
- <div className="error-bubble-icon">✕</div>
- <div className="error-bubble-body">
- <div className="error-bubble-label">
- {AGENT_ERROR_CODE_LABELS[message.errorCode ?? ''] ?? 'Error'}
- {message.errorProvider
- ? ` · ${message.errorProvider.toUpperCase()}`
- : ''}
- </div>
- <div className="error-bubble-message">
- {message.content.length > 180
- ? `${message.content.slice(0, 177)}...`
- : message.content}
- </div>
- <details className="error-bubble-details">
- <summary>Details</summary>
- <pre className="error-bubble-details-text">{message.content}</pre>
- </details>
- </div>
- </div>
- ) : (
- <>
- {htmlPreview && (
- <SecureHtmlPreview
- html={htmlPreview}
- previewId={message.messageKey}
- title="HTML visualization"
- maxBytes={HTML_PREVIEW_INLINE_MAX_BYTES}
- />
- )}
- {isJson ? (
- <pre className="message-block json-content">{displayContent}</pre>
- ) : message.role === 'assistant' ? (
- <div className="message-block markdown-content">
- <ReactMarkdown remarkPlugins={[remarkGfm]}>
- {displayContent}
- </ReactMarkdown>
- {selectedAgent && !hasResponseFileAttachment && (
- <MessageArtifactActions
- hostRef={selectedAgent}
- content={displayContent}
- />
- )}
- {workflowArtifactScopeForAssistant ? (
- <WorkflowRunArtifactActions
- workflow={workflowArtifactScopeForAssistant}
- />
- ) : null}
- </div>
- ) : displayContent ? (
- <p className="message-block">{displayContent}</p>
- ) : null}
- <MessageAttachmentList attachments={displayAttachments} />
- </>
- )}
- {isLastAssistantMessage &&
- prevGroup?.role === 'user' &&
- (() => {
- // Prefer the live progress (keyed by the user message);
- // fall back to the assistant's persisted toolSteps after
- // a reload, when the live map is empty (#582).
- const liveNodes = prevGroup.items
- .map(msg => renderProgressStepper(msg, 'completed'))
- .filter((node): node is ReactElement => node != null)
- if (liveNodes.length > 0) return liveNodes
- return renderHydratedToolSteps(message)
- })()}
- {showMetaRow && (
- <footer className={`chat-message-meta-row ${metaRowRoleClass}`.trim()}>
- <div className="chat-message-meta-main">
- <span className="chat-time-label">
- {formatChatTimestamp(message.timestamp || Date.now())}
- </span>
- {message.role === 'assistant' && message.tokens && (
- <MessageTokens tokens={message.tokens} />
- )}
- </div>
- <IconButton
- className={`message-copy-button message-copy-button--inline${
- copiedMessageKey === message.messageKey ? ' copied' : ''
- }`}
- onClick={() => void handleCopyMessage(message.messageKey, copyContent)}
- aria-label={
- copiedMessageKey === message.messageKey ? 'Copied' : 'Copy text'
+ messageGroupChunks.map((chunk, chunkIndex) => (
+ <VirtualizedMessageChunk
+ key={chunk[0]?.group.groupKey ?? `chunk-${chunkIndex}`}
+ estimatedHeight={Math.max(240, chunk.length * 180)}
+ initiallyVisible={chunkIndex >= messageGroupChunks.length - 2}
+ render={() =>
+ chunk.map(({ group, groupIndex }) => {
+ const prevGroup = groupIndex > 0 ? groupedWithKeys[groupIndex - 1] : null
+
+ return (
+ <Fragment key={group.groupKey}>
+ <section className={`chat-group ${group.role}`}>
+ {group.items.map((message, messageIndex) => {
+ const parsedUserMessage =
+ message.role === 'user' ? parseChatMessageDisplay(message.content) : null
+ const displayContent = parsedUserMessage?.content ?? message.content
+ const displayAttachments =
+ message.role === 'user'
+ ? message.attachments && message.attachments.length
+ ? message.attachments
+ : (parsedUserMessage?.attachments ?? [])
+ : message.attachments && message.attachments.length
+ ? message.attachments
+ : []
+ const hasResponseFileAttachment = displayAttachments.some(
+ attachment => attachment.type === 'response_file'
+ )
+ const isJson = looksLikeJson(displayContent)
+ const htmlPreview =
+ message.role === 'assistant'
+ ? extractHtmlVisualization(displayContent)
+ : null
+ const hasHtmlArtifactMention =
+ message.role === 'assistant' &&
+ /\b[a-zA-Z0-9][a-zA-Z0-9._-]*\.html?\b/i.test(displayContent)
+ const showMetaRow = message.role === 'assistant' || message.role === 'user'
+ const copyContent =
+ message.role === 'user' ? displayContent : message.content
+ const metaRowRoleClass =
+ message.role === 'user'
+ ? 'chat-message-meta-row--user'
+ : message.role === 'assistant'
+ ? 'chat-message-meta-row--assistant'
+ : ''
+ const isLastAssistantMessage =
+ group.role === 'assistant' && messageIndex === group.items.length - 1
+ const workflowArtifactScopeForAssistant =
+ message.role === 'assistant' && prevGroup?.role === 'user'
+ ? (prevGroup.items
+ .map(prevMessage =>
+ extractWorkflowArtifactScopeFromProgress(
+ progressByMessageId[prevMessage.id]
+ )
+ )
+ .find((scope): scope is WorkflowArtifactScope => scope !== null) ??
+ null)
+ : null
+ return (
+ <article
+ key={message.messageKey}
+ data-testid={
+ message.role === 'assistant' ? 'agent-response' : undefined
}
- label={copiedMessageKey === message.messageKey ? 'Copied' : 'Copy text'}
- size="xs"
- variant="ghost"
+ className={`chat-bubble ${message.role}${message.isError ? ' chat-bubble--error' : ''}${
+ htmlPreview || hasHtmlArtifactMention ? ' chat-bubble--wide' : ''
+ }`}
>
- <svg viewBox="0 0 24 24" aria-hidden="true">
- <path fill="none" d="M0 0h24v24H0V0z" />
- <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
- </svg>
- {copiedMessageKey === message.messageKey && (
- <span className="copy-tooltip">Copied!</span>
+ {message.isError ? (
+ <div className="error-bubble-content">
+ <div className="error-bubble-icon">✕</div>
+ <div className="error-bubble-body">
+ <div className="error-bubble-label">
+ {AGENT_ERROR_CODE_LABELS[message.errorCode ?? ''] ?? 'Error'}
+ {message.errorProvider
+ ? ` · ${message.errorProvider.toUpperCase()}`
+ : ''}
+ </div>
+ <div className="error-bubble-message">
+ {message.content.length > 180
+ ? `${message.content.slice(0, 177)}...`
+ : message.content}
+ </div>
+ <details className="error-bubble-details">
+ <summary>Details</summary>
+ <pre className="error-bubble-details-text">
+ {message.content}
+ </pre>
+ </details>
+ </div>
+ </div>
+ ) : (
+ <>
+ {htmlPreview && (
+ <SecureHtmlPreview
+ html={htmlPreview}
+ previewId={message.messageKey}
+ title="HTML visualization"
+ maxBytes={HTML_PREVIEW_INLINE_MAX_BYTES}
+ />
+ )}
+ {isJson ? (
+ <pre className="message-block json-content">{displayContent}</pre>
+ ) : message.role === 'assistant' ? (
+ <div className="message-block markdown-content">
+ <ReactMarkdown remarkPlugins={[remarkGfm]}>
+ {displayContent}
+ </ReactMarkdown>
+ {selectedAgent && !hasResponseFileAttachment && (
+ <MessageArtifactActions
+ hostRef={selectedAgent}
+ content={displayContent}
+ />
+ )}
+ {workflowArtifactScopeForAssistant ? (
+ <WorkflowRunArtifactActions
+ workflow={workflowArtifactScopeForAssistant}
+ />
+ ) : null}
+ </div>
+ ) : displayContent ? (
+ <p className="message-block">{displayContent}</p>
+ ) : null}
+ <MessageAttachmentList attachments={displayAttachments} />
+ </>
)}
- </IconButton>
- </footer>
- )}
- </article>
- )
- })}
- </section>
- {group.role === 'user' &&
- group.items.map(message => {
- const node = renderProgressStepper(message, 'non-completed')
- if (!node) return null
- return (
- <section
- key={`progress-${message.messageKey}`}
- className="chat-group assistant"
- >
- <div className="chat-bubble assistant">{node}</div>
+ {isLastAssistantMessage &&
+ prevGroup?.role === 'user' &&
+ (() => {
+ // Prefer the live progress (keyed by the user message);
+ // fall back to the assistant's persisted toolSteps after
+ // a reload, when the live map is empty (#582).
+ const liveNodes = prevGroup.items
+ .map(msg => renderProgressStepper(msg, 'completed'))
+ .filter((node): node is ReactElement => node != null)
+ if (liveNodes.length > 0) return liveNodes
+ return renderHydratedToolSteps(message)
+ })()}
+ {showMetaRow && (
+ <footer
+ className={`chat-message-meta-row ${metaRowRoleClass}`.trim()}
+ >
+ <div className="chat-message-meta-main">
+ <span className="chat-time-label">
+ {formatChatTimestamp(message.timestamp || Date.now())}
+ </span>
+ {message.role === 'assistant' && message.tokens && (
+ <MessageTokens tokens={message.tokens} />
+ )}
+ </div>
+ <IconButton
+ className={`message-copy-button message-copy-button--inline${
+ copiedMessageKey === message.messageKey ? ' copied' : ''
+ }`}
+ onClick={() =>
+ void handleCopyMessage(message.messageKey, copyContent)
+ }
+ aria-label={
+ copiedMessageKey === message.messageKey ? 'Copied' : 'Copy text'
+ }
+ label={
+ copiedMessageKey === message.messageKey ? 'Copied' : 'Copy text'
+ }
+ size="xs"
+ variant="ghost"
+ >
+ <svg viewBox="0 0 24 24" aria-hidden="true">
+ <path fill="none" d="M0 0h24v24H0V0z" />
+ <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
+ </svg>
+ {copiedMessageKey === message.messageKey && (
+ <span className="copy-tooltip">Copied!</span>
+ )}
+ </IconButton>
+ </footer>
+ )}
+ </article>
+ )
+ })}
</section>
- )
- })}
- </Fragment>
- )
- })}
+ {group.role === 'user' &&
+ group.items.map(message => {
+ const node = renderProgressStepper(message, 'non-completed')
+ if (!node) return null
+ return (
+ <section
+ key={`progress-${message.messageKey}`}
+ className="chat-group assistant"
+ >
+ <div className="chat-bubble assistant">{node}</div>
+ </section>
+ )
+ })}
+ </Fragment>
+ )
+ })
+ }
+ />
+ ))}
{activeChatId && chatMessagesLoading && groupedWithKeys.length === 0 && (
<section className="chat-group assistant" aria-label="Loading conversation">
<div className="chat-bubble assistant chat-loading-bubble">
diff --git a/desktop-app/ui/src/styles.css b/desktop-app/ui/src/styles.css
index 7ce5e7237..0cd1420c4 100644
--- a/desktop-app/ui/src/styles.css
+++ b/desktop-app/ui/src/styles.css
@@ -6049,6 +6049,12 @@ h3 {
padding-block: var(--space-1);
}
+.virtualized-message-chunk {
+ display: grid;
+ gap: var(--space-2);
+ min-width: 0;
+}
+
.chat-empty-state {
align-content: center;
justify-items: center;