Skip to content

Commit 32c13d9

Browse files
feat(app): virtualize session message rendering (anomalyco#736)
1 parent 88aa040 commit 32c13d9

4 files changed

Lines changed: 98 additions & 9 deletions

File tree

‎packages/app/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@solid-primitives/event-bus": "^1.1.2",
3939
"@solid-primitives/storage": "^4.3.3",
4040
"@solidjs/router": "^0.15.4",
41+
"@tanstack/solid-virtual": "^3.13.19",
4142
"@tauri-apps/api": "^2.0.0",
4243
"@tauri-apps/plugin-deep-link": "^2.4.7",
4344
"@tauri-apps/plugin-dialog": "~2.6.0",

‎packages/app/src/app/components/session/message-list.tsx‎

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { For, Show, createMemo, createSignal, onCleanup } from "solid-js";
1+
import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
22
import type { JSX } from "solid-js";
33
import type { Part } from "@opencode-ai/sdk/v2/client";
44
import { Check, ChevronDown, ChevronRight, Copy, Eye, File, FileEdit, FolderSearch, Pencil, Search, Sparkles, Terminal } from "lucide-solid";
5+
import { createVirtualizer } from "@tanstack/solid-virtual";
56

67
import type { MessageGroup, MessageWithParts, StepGroupMode } from "../../types";
78
import { groupMessageParts, isUserVisiblePart, summarizeStep } from "../../utils";
@@ -20,6 +21,8 @@ export type MessageListProps = {
2021
activeSearchMessageId?: string | null;
2122
searchHighlightQuery?: string;
2223
workspaceRoot?: string;
24+
scrollElement?: () => HTMLElement | null | undefined;
25+
virtualizationThreshold?: number;
2326
footer?: JSX.Element;
2427
};
2528

@@ -427,6 +430,43 @@ export default function MessageList(props: MessageListProps) {
427430
};
428431
};
429432

433+
const blockContainsMessageId = (block: MessageBlockItem, messageId: string) => {
434+
if (!messageId) return false;
435+
if (block.kind === "steps-cluster") {
436+
return block.messageIds.includes(messageId);
437+
}
438+
return block.messageId === messageId;
439+
};
440+
441+
const virtualizationThreshold = () => props.virtualizationThreshold ?? 30;
442+
const shouldVirtualize = createMemo(() => messageBlocks().length >= virtualizationThreshold());
443+
444+
const virtualizer = createVirtualizer<HTMLElement, HTMLDivElement>({
445+
get count() {
446+
return shouldVirtualize() ? messageBlocks().length : 0;
447+
},
448+
getScrollElement: () => props.scrollElement?.() ?? null,
449+
estimateSize: () => 220,
450+
overscan: 6,
451+
getItemKey: (index) => {
452+
const block = messageBlocks()[index];
453+
if (!block) return `block-${index}`;
454+
if (block.kind === "steps-cluster") {
455+
return `steps-${block.messageIds.join(",")}`;
456+
}
457+
return `message-${block.messageId}`;
458+
},
459+
});
460+
461+
createEffect(() => {
462+
if (!shouldVirtualize()) return;
463+
const activeMessageId = props.activeSearchMessageId?.trim();
464+
if (!activeMessageId) return;
465+
const index = messageBlocks().findIndex((block) => blockContainsMessageId(block, activeMessageId));
466+
if (index < 0) return;
467+
virtualizer.scrollToIndex(index, { align: "center" });
468+
});
469+
430470
/** Compact single-line step row */
431471
const StepRow = (rowProps: { part: Part; isUser: boolean; groupMode?: StepGroupMode }) => {
432472
const summary = createMemo(() => summarizeStep(rowProps.part));
@@ -809,10 +849,7 @@ export default function MessageList(props: MessageListProps) {
809849
);
810850
};
811851

812-
return (
813-
<div class="space-y-4 pb-24" style={{ contain: "layout paint style" }}>
814-
<For each={messageBlocks()}>
815-
{(block, blockIndex) => {
852+
const renderBlock = (block: MessageBlockItem, blockIndex: number) => {
816853
const blockMessageIds = block.kind === "steps-cluster" ? block.messageIds : [block.messageId];
817854
const hasSearchMatch = blockMessageIds.some((id) => props.searchMatchMessageIds?.has(id));
818855
const hasActiveSearchMatch = blockMessageIds.some((id) => id === props.activeSearchMessageId);
@@ -828,7 +865,7 @@ export default function MessageList(props: MessageListProps) {
828865
class={`flex group ${block.isUser ? "justify-end" : "justify-start"}`.trim()}
829866
data-message-role={block.isUser ? "user" : "assistant"}
830867
data-message-id={block.messageIds[0] ?? ""}
831-
style={blockPerfStyle(blockIndex())}
868+
style={blockPerfStyle(blockIndex)}
832869
>
833870
<div
834871
class={`w-full relative ${
@@ -854,7 +891,7 @@ export default function MessageList(props: MessageListProps) {
854891
class={`flex group ${block.isUser ? "justify-end" : "justify-start"}`.trim()}
855892
data-message-role={block.isUser ? "user" : "assistant"}
856893
data-message-id={block.messageId}
857-
style={blockPerfStyle(blockIndex())}
894+
style={blockPerfStyle(blockIndex)}
858895
>
859896
<div
860897
class={`w-full relative ${
@@ -951,8 +988,40 @@ export default function MessageList(props: MessageListProps) {
951988
</div>
952989
</div>
953990
);
954-
}}
955-
</For>
991+
};
992+
993+
return (
994+
<div class="space-y-4 pb-24" style={{ contain: "layout paint style" }}>
995+
<Show
996+
when={shouldVirtualize()}
997+
fallback={
998+
<For each={messageBlocks()}>
999+
{(block, index) => renderBlock(block, index())}
1000+
</For>
1001+
}
1002+
>
1003+
<div style={{ height: `${virtualizer.getTotalSize()}px`, position: "relative" }}>
1004+
<For each={virtualizer.getVirtualItems()}>
1005+
{(item) => {
1006+
const block = () => messageBlocks()[item.index];
1007+
return (
1008+
<div
1009+
ref={(el) => virtualizer.measureElement(el)}
1010+
style={{
1011+
position: "absolute",
1012+
top: "0",
1013+
left: "0",
1014+
width: "100%",
1015+
transform: `translateY(${item.start}px)`,
1016+
}}
1017+
>
1018+
<Show when={block()}>{(value) => renderBlock(value(), item.index)}</Show>
1019+
</div>
1020+
);
1021+
}}
1022+
</For>
1023+
</div>
1024+
</Show>
9561025
<Show when={props.footer}>{props.footer}</Show>
9571026
</div>
9581027
);

‎packages/app/src/app/pages/session.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,7 @@ export default function SessionView(props: SessionViewProps) {
36183618
searchMatchMessageIds={searchMatchMessageIds()}
36193619
activeSearchMessageId={activeSearchHit()?.messageId ?? null}
36203620
searchHighlightQuery={searchQueryDebounced().trim()}
3621+
scrollElement={() => chatContainerEl}
36213622
footer={
36223623
showRunIndicator() ? (
36233624
<div class="flex justify-start pl-2">

‎pnpm-lock.yaml‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)