diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 08a3a2155b..c58b8d22d7 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2446,11 +2446,31 @@ const MessagesTimeline = memo(function MessagesTimeline({ return nextRows; }, [timelineEntries, completionDividerBeforeEntryId, isWorking]); + // Split rows: completed turns are virtualized, current in-progress turn is not. + // The current turn starts at the last user message when actively working. + const currentTurnStartIndex = useMemo(() => { + if (!isWorking) return rows.length; + for (let i = rows.length - 1; i >= 0; i--) { + const row = rows[i]; + if (row?.kind === "message" && row.message.role === "user") return i; + } + return rows.length; + }, [rows, isWorking]); + + const virtualizedRows = useMemo( + () => rows.slice(0, currentTurnStartIndex), + [rows, currentTurnStartIndex], + ); + const currentTurnRows = useMemo( + () => rows.slice(currentTurnStartIndex), + [rows, currentTurnStartIndex], + ); + const rowVirtualizer = useVirtualizer({ - count: rows.length, + count: virtualizedRows.length, getScrollElement: () => scrollContainerRef.current, estimateSize: (index: number) => { - const row = rows[index]; + const row = virtualizedRows[index]; if (!row) return 96; if (row.kind === "work") return 112; if (row.kind === "working") return 40; @@ -2472,292 +2492,306 @@ const MessagesTimeline = memo(function MessagesTimeline({ ); } - return ( -
- {virtualRows.map((virtualRow: VirtualItem) => { - const row = rows[virtualRow.index]; - if (!row) return null; + const renderRowContent = (row: TimelineRow) => ( +
+ {row.kind === "work" && + (() => { + const groupId = row.id; + const groupedEntries = row.groupedEntries; + const isExpanded = expandedWorkGroups[groupId] ?? false; + const hasOverflow = groupedEntries.length > MAX_VISIBLE_WORK_LOG_ENTRIES; + const visibleEntries = + hasOverflow && !isExpanded + ? groupedEntries.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES) + : groupedEntries; + const hiddenCount = groupedEntries.length - visibleEntries.length; + const onlyToolEntries = groupedEntries.every((entry) => entry.tone === "tool"); + const groupLabel = onlyToolEntries + ? groupedEntries.length === 1 + ? "Tool call" + : `Tool calls (${groupedEntries.length})` + : groupedEntries.length === 1 + ? "Work event" + : `Work log (${groupedEntries.length})`; - return ( -
-
- {row.kind === "work" && - (() => { - const groupId = row.id; - const groupedEntries = row.groupedEntries; - const isExpanded = expandedWorkGroups[groupId] ?? false; - const hasOverflow = groupedEntries.length > MAX_VISIBLE_WORK_LOG_ENTRIES; - const visibleEntries = - hasOverflow && !isExpanded - ? groupedEntries.slice(-MAX_VISIBLE_WORK_LOG_ENTRIES) - : groupedEntries; - const hiddenCount = groupedEntries.length - visibleEntries.length; - const onlyToolEntries = groupedEntries.every((entry) => entry.tone === "tool"); - const groupLabel = onlyToolEntries - ? groupedEntries.length === 1 - ? "Tool call" - : `Tool calls (${groupedEntries.length})` - : groupedEntries.length === 1 - ? "Work event" - : `Work log (${groupedEntries.length})`; + return ( +
+
+

+ {groupLabel} +

+ {hasOverflow && ( + + )} +
+
+ {visibleEntries.map((workEntry) => ( +
+ +

+ {workEntry.detail ? ( + <> + {workEntry.label} + + {workEntry.detail} + + + ) : ( + workEntry.label + )} +

+
+ ))} +
+
+ ); + })()} + {row.kind === "message" && + row.message.role === "user" && + (() => { + const userImages = row.message.attachments ?? []; + const canRevertAgentWork = revertTurnCountByUserMessageId.has(row.message.id); + return ( +
+
+ {userImages.length > 0 && ( +
+ {userImages.map( + (image: NonNullable[number]) => ( +
+ {image.previewUrl ? ( + {image.name} + onImageExpand({ src: image.previewUrl!, name: image.name }) + } + /> + ) : ( +
+ {image.name} +
+ )} +
+ ), + )} +
+ )} + {row.message.text && ( +
+                    {row.message.text}
+                  
+ )} +
+
+ {row.message.text && } + {canRevertAgentWork && ( + + )} +
+

+ {formatTimestamp(row.message.createdAt)} +

+
+
+
+ ); + })()} + + {row.kind === "message" && + row.message.role === "assistant" && + (() => { + const messageText = + row.message.text || (row.message.streaming ? "" : "(empty response)"); + return ( + <> + {row.showCompletionDivider && ( +
+ + + {completionSummary ? `Response • ${completionSummary}` : "Response"} + + +
+ )} +
+ + {(() => { + const turnSummary = turnDiffSummaryByAssistantMessageId.get( + row.message.id, + ); + if (!turnSummary) return null; + const checkpointFiles = turnSummary.files; + if (checkpointFiles.length === 0) return null; + const summaryStat = checkpointFiles.reduce( + (acc, file) => { + if ( + typeof file.additions !== "number" || + typeof file.deletions !== "number" + ) { + return acc; + } + return { + additions: acc.additions + file.additions, + deletions: acc.deletions + file.deletions, + }; + }, + { additions: 0, deletions: 0 }, + ); + const changedFileCountLabel = String(checkpointFiles.length); return ( -
-
+
+

- {groupLabel} + Changed files ({changedFileCountLabel}) + {(summaryStat.additions > 0 || summaryStat.deletions > 0) && ( + <> + • + +{summaryStat.additions} + / + + -{summaryStat.deletions} + + + )}

- {hasOverflow && ( - - )} +
-
- {visibleEntries.map((workEntry) => ( -
+ {checkpointFiles.map((file) => ( +
+ ); + })()} + ))}
); })()} +

+ {formatMessageMeta( + row.message.createdAt, + row.message.streaming + ? formatElapsed(row.message.createdAt, nowIso) + : formatElapsed(row.message.createdAt, row.message.completedAt), + )} +

+
+ + ); + })()} - {row.kind === "message" && - row.message.role === "user" && - (() => { - const userImages = row.message.attachments ?? []; - const canRevertAgentWork = revertTurnCountByUserMessageId.has(row.message.id); - return ( -
-
- {userImages.length > 0 && ( -
- {userImages.map( - (image: NonNullable[number]) => ( -
- {image.previewUrl ? ( - {image.name} - onImageExpand({ src: image.previewUrl!, name: image.name }) - } - /> - ) : ( -
- {image.name} -
- )} -
- ), - )} -
- )} - {row.message.text && ( -
-                            {row.message.text}
-                          
- )} -
-
- {row.message.text && } - {canRevertAgentWork && ( - - )} -
-

- {formatTimestamp(row.message.createdAt)} -

-
-
-
- ); - })()} - - {row.kind === "message" && - row.message.role === "assistant" && - (() => { - const messageText = - row.message.text || (row.message.streaming ? "" : "(empty response)"); - return ( - <> - {row.showCompletionDivider && ( -
- - - {completionSummary ? `Response • ${completionSummary}` : "Response"} - - -
- )} -
- - {(() => { - const turnSummary = turnDiffSummaryByAssistantMessageId.get( - row.message.id, - ); - if (!turnSummary) return null; - const checkpointFiles = turnSummary.files; - if (checkpointFiles.length === 0) return null; - const summaryStat = checkpointFiles.reduce( - (acc, file) => { - if ( - typeof file.additions !== "number" || - typeof file.deletions !== "number" - ) { - return acc; - } - return { - additions: acc.additions + file.additions, - deletions: acc.deletions + file.deletions, - }; - }, - { additions: 0, deletions: 0 }, - ); - const changedFileCountLabel = String(checkpointFiles.length); - return ( -
-
-

- Changed files ({changedFileCountLabel}) - {(summaryStat.additions > 0 || summaryStat.deletions > 0) && ( - <> - • - +{summaryStat.additions} - / - - -{summaryStat.deletions} - - - )} -

- -
-
- {checkpointFiles.map((file) => ( - - ))} -
-
- ); - })()} -

- {formatMessageMeta( - row.message.createdAt, - row.message.streaming - ? formatElapsed(row.message.createdAt, nowIso) - : formatElapsed(row.message.createdAt, row.message.completedAt), - )} -

-
- - ); - })()} - - {row.kind === "working" && ( -
- -
- - - - - -
-
- )} -
+ {row.kind === "working" && ( +
+ +
+ + + + +
- ); - })} +
+ )} +
+ ); + + return ( +
+ {/* Virtualized completed turns */} + {virtualizedRows.length > 0 && ( +
+ {virtualRows.map((virtualRow: VirtualItem) => { + const row = virtualizedRows[virtualRow.index]; + if (!row) return null; + + return ( +
+ {renderRowContent(row)} +
+ ); + })} +
+ )} + + {/* Current in-progress turn rendered in normal flow (no virtualization) */} + {currentTurnRows.map((row) => ( +
{renderRowContent(row)}
+ ))}
); });