-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: implement virtualization for ChatView to handle long conversations #6571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
227cd9c
feat: implement virtualization for ChatView to handle long conversations
hannesrudolph 55097ba
debug: add temporary console.log statements for virtualization tracking
hannesrudolph 8d56205
fix: increase memory threshold and optimize performance monitoring
hannesrudolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||
| import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react" | ||||||||
| import { useDeepCompareEffect, useEvent, useMount } from "react-use" | ||||||||
| import debounce from "debounce" | ||||||||
| import { Virtuoso, type VirtuosoHandle } from "react-virtuoso" | ||||||||
| import { Virtuoso } from "react-virtuoso" | ||||||||
| import removeMd from "remove-markdown" | ||||||||
| import { VSCodeButton } from "@vscode/webview-ui-toolkit/react" | ||||||||
| import useSound from "use-sound" | ||||||||
|
|
@@ -58,6 +58,9 @@ import QueuedMessages from "./QueuedMessages" | |||||||
| import { getLatestTodo } from "@roo/todo" | ||||||||
| import { QueuedMessage } from "@roo-code/types" | ||||||||
|
|
||||||||
| // Import the new virtualization utilities | ||||||||
| import { useOptimizedVirtualization } from "./virtualization" | ||||||||
|
|
||||||||
| export interface ChatViewProps { | ||||||||
| isHidden: boolean | ||||||||
| showAnnouncement: boolean | ||||||||
|
|
@@ -167,13 +170,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |||||||
| const [primaryButtonText, setPrimaryButtonText] = useState<string | undefined>(undefined) | ||||||||
| const [secondaryButtonText, setSecondaryButtonText] = useState<string | undefined>(undefined) | ||||||||
| const [didClickCancel, setDidClickCancel] = useState(false) | ||||||||
| const virtuosoRef = useRef<VirtuosoHandle>(null) | ||||||||
| const [expandedRows, setExpandedRows] = useState<Record<number, boolean>>({}) | ||||||||
| const prevExpandedRowsRef = useRef<Record<number, boolean>>() | ||||||||
| const scrollContainerRef = useRef<HTMLDivElement>(null) | ||||||||
| const disableAutoScrollRef = useRef(false) | ||||||||
| const [showScrollToBottom, setShowScrollToBottom] = useState(false) | ||||||||
| const [isAtBottom, setIsAtBottom] = useState(false) | ||||||||
| const lastTtsRef = useRef<string>("") | ||||||||
| const [wasStreaming, setWasStreaming] = useState<boolean>(false) | ||||||||
| const [showCheckpointWarning, setShowCheckpointWarning] = useState<boolean>(false) | ||||||||
|
|
@@ -189,6 +186,12 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |||||||
| const userRespondedRef = useRef<boolean>(false) | ||||||||
| const [currentFollowUpTs, setCurrentFollowUpTs] = useState<number | null>(null) | ||||||||
|
|
||||||||
| // Map the optimized state to the existing state variables for backward compatibility | ||||||||
| const [expandedRows, setExpandedRows] = useState<Record<number, boolean>>({}) | ||||||||
| const prevExpandedRowsRef = useRef<Record<number, boolean>>() | ||||||||
| const disableAutoScrollRef = useRef(false) | ||||||||
| const [showScrollToBottom, setShowScrollToBottom] = useState(false) | ||||||||
|
|
||||||||
| const clineAskRef = useRef(clineAsk) | ||||||||
| useEffect(() => { | ||||||||
| clineAskRef.current = clineAsk | ||||||||
|
|
@@ -434,6 +437,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |||||||
| }, [messages.length]) | ||||||||
|
|
||||||||
| useEffect(() => { | ||||||||
| // Clear expanded rows for new task | ||||||||
| setExpandedRows({}) | ||||||||
| everVisibleMessagesTsRef.current.clear() // Clear for new task | ||||||||
| setCurrentFollowUpTs(null) // Clear follow-up answered state for new task | ||||||||
|
|
@@ -480,6 +484,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |||||||
| prevExpandedRowsRef.current = expandedRows // Store current state for next comparison | ||||||||
| }, [expandedRows]) | ||||||||
|
|
||||||||
| // Define isStreaming before using it in the virtualization hook | ||||||||
| const isStreaming = useMemo(() => { | ||||||||
| // Checking clineAsk isn't enough since messages effect may be called | ||||||||
| // again for a tool for example, set clineAsk to its value, and if the | ||||||||
|
|
@@ -521,6 +526,88 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |||||||
| return false | ||||||||
| }, [modifiedMessages, clineAsk, enableButtons, primaryButtonText]) | ||||||||
|
|
||||||||
| // Use the optimized virtualization hook (after isStreaming is defined) | ||||||||
| const { | ||||||||
| virtuosoRef, | ||||||||
| viewportConfig, | ||||||||
| stateManager, | ||||||||
| scrollManager, | ||||||||
| performanceMonitor, | ||||||||
| handleScroll: handleVirtuosoScroll, | ||||||||
| handleRangeChange, | ||||||||
| handleScrollStateChange, | ||||||||
| scrollToBottom: optimizedScrollToBottom, | ||||||||
| isAtBottom, | ||||||||
| showScrollToBottom: shouldShowScrollButton, | ||||||||
| visibleRange, | ||||||||
| } = useOptimizedVirtualization({ | ||||||||
| messages: modifiedMessages, | ||||||||
| isStreaming, | ||||||||
| isHidden, | ||||||||
| onPerformanceIssue: (metric, value) => { | ||||||||
| console.warn(`ChatView performance issue: ${metric} = ${value}`) | ||||||||
|
||||||||
| console.warn(`ChatView performance issue: ${metric} = ${value}`) | |
| console.warn(`ChatView performance issue: ${metric} = ${value}`) | |
| // Consider: Show a toast notification, adjust viewport config, or report telemetry |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The performance issue callback only logs to console. Consider implementing more robust error handling or user notification for performance degradation in production.