Replaces `use-stick-to-bottom` and per-row session rendering with `@tanstack/react-virtual`, matching what Cursor uses. Chat thread (`thread-virtualizer.tsx`): - Natural-flow virtualization (padding spacers, not absolute items) so `position: sticky` on the human bubble still resolves cleanly against the scroller. - Custom at-bottom anchor: pins when armed, disarms on user-driven upward scroll, re-arms at bottom, jumps on session switch + `thread.runStart`. - Loading indicator and `--thread-last-message-clearance` move to a real `[data-slot=aui_composer-clearance]` node; drops the brittle `:nth-last-child(1 of …)` rule that can't fire reliably under virtualization. Sidebar (`virtual-session-list.tsx`): - Flat agents list virtualizes at >=25 rows; pinned and workspace-grouped paths stay direct-render. - `SortableContext` keeps all IDs; only the window mounts; dnd-kit's `setNodeRef` is merged with `virtualizer.measureElement` so rows participate in both DnD hit-testing and TanStack measurement. Drops `use-stick-to-bottom`. Streaming test gets a global `offsetWidth/offsetHeight` stub so the virtualizer's viewport sizing works in jsdom; the scroll-up-doesn't-pull-back invariant still passes.
307 lines
9.1 KiB
TypeScript
307 lines
9.1 KiB
TypeScript
import { ThreadPrimitive, useAuiEvent, useAuiState } from '@assistant-ui/react'
|
|
import { useVirtualizer, type Virtualizer } from '@tanstack/react-virtual'
|
|
import { type ComponentProps, type FC, type ReactNode, useCallback, useEffect, useMemo, useRef } from 'react'
|
|
|
|
import { cn } from '@/lib/utils'
|
|
import { setThreadScrolledUp } from '@/store/thread-scroll'
|
|
|
|
const ESTIMATED_ITEM_HEIGHT = 220
|
|
const OVERSCAN = 4
|
|
const AT_BOTTOM_THRESHOLD = 4
|
|
|
|
type ThreadMessageComponents = ComponentProps<typeof ThreadPrimitive.MessageByIndex>['components']
|
|
|
|
type MessageGroup = { id: string; index: number; kind: 'standalone' } | { id: string; indices: number[]; kind: 'turn' }
|
|
|
|
interface VirtualizedThreadProps {
|
|
clampToComposer: boolean
|
|
components: ThreadMessageComponents
|
|
emptyPlaceholder?: ReactNode
|
|
loadingIndicator?: ReactNode
|
|
sessionKey?: string | null
|
|
}
|
|
|
|
function buildGroups(signature: string): MessageGroup[] {
|
|
if (!signature) {
|
|
return []
|
|
}
|
|
|
|
const messages = signature.split('\n').map(row => {
|
|
const [index, id, role] = row.split(':')
|
|
|
|
return { id, index: Number(index), role }
|
|
})
|
|
|
|
const groups: MessageGroup[] = []
|
|
|
|
for (let i = 0; i < messages.length; i++) {
|
|
const message = messages[i]
|
|
|
|
if (message.role !== 'user') {
|
|
groups.push({ id: message.id, index: message.index, kind: 'standalone' })
|
|
|
|
continue
|
|
}
|
|
|
|
const indices = [message.index]
|
|
|
|
while (i + 1 < messages.length && messages[i + 1].role !== 'user') {
|
|
indices.push(messages[++i].index)
|
|
}
|
|
|
|
groups.push({ id: message.id, indices, kind: 'turn' })
|
|
}
|
|
|
|
return groups
|
|
}
|
|
|
|
export const VirtualizedThread: FC<VirtualizedThreadProps> = ({
|
|
clampToComposer,
|
|
components,
|
|
emptyPlaceholder,
|
|
loadingIndicator,
|
|
sessionKey
|
|
}) => {
|
|
const messageSignature = useAuiState(s =>
|
|
s.thread.messages.map((message, index) => `${index}:${message.id}:${message.role}`).join('\n')
|
|
)
|
|
|
|
const groups = useMemo(() => buildGroups(messageSignature), [messageSignature])
|
|
const renderEmpty = groups.length === 0 && Boolean(emptyPlaceholder)
|
|
const scrollerRef = useRef<HTMLDivElement | null>(null)
|
|
|
|
const virtualizer = useVirtualizer({
|
|
count: groups.length,
|
|
estimateSize: () => ESTIMATED_ITEM_HEIGHT,
|
|
getItemKey: index => groups[index]?.id ?? index,
|
|
getScrollElement: () => scrollerRef.current,
|
|
// Seed the rect so the initial range mounts something before
|
|
// `observeElementRect` reports the real layout (it overrides this).
|
|
initialRect: { height: 600, width: 800 },
|
|
overscan: OVERSCAN
|
|
})
|
|
|
|
useThreadScrollAnchor({
|
|
enabled: !renderEmpty,
|
|
groupCount: groups.length,
|
|
scrollerRef,
|
|
sessionKey: sessionKey ?? null,
|
|
virtualizer
|
|
})
|
|
|
|
const virtualItems = virtualizer.getVirtualItems()
|
|
const totalSize = virtualizer.getTotalSize()
|
|
const paddingTop = virtualItems[0]?.start ?? 0
|
|
const paddingBottom = Math.max(0, totalSize - (virtualItems.at(-1)?.end ?? 0))
|
|
|
|
return (
|
|
<div
|
|
className="relative min-h-0 max-w-full overflow-hidden contain-[layout_paint]"
|
|
style={{ height: clampToComposer ? 'var(--thread-viewport-height)' : '100%' }}
|
|
>
|
|
<div
|
|
className="size-full overflow-x-hidden overflow-y-auto overscroll-contain"
|
|
data-slot="aui_thread-viewport"
|
|
ref={scrollerRef}
|
|
>
|
|
{renderEmpty ? (
|
|
<div
|
|
className="mx-auto grid h-full w-full max-w-(--composer-width) grid-rows-[minmax(0,1fr)_auto] min-w-0 gap-(--conversation-turn-gap) px-6 py-8"
|
|
data-slot="aui_thread-content"
|
|
>
|
|
{emptyPlaceholder}
|
|
</div>
|
|
) : (
|
|
<div
|
|
className={cn(
|
|
'mx-auto flex w-full max-w-(--composer-width) min-w-0 flex-col px-6 pt-[calc(var(--titlebar-height)+1.5rem)]'
|
|
)}
|
|
data-slot="aui_thread-content"
|
|
>
|
|
{/* Natural-flow virtualization: mounted items render as normal
|
|
flex siblings so `position: sticky` on the human bubble
|
|
resolves against the scroller without transform interference.
|
|
Padding spacers reserve scroll space for unmounted items. */}
|
|
<div style={{ paddingBottom: `${paddingBottom}px`, paddingTop: `${paddingTop}px` }}>
|
|
{virtualItems.map(virtualItem => {
|
|
const group = groups[virtualItem.index]
|
|
|
|
if (!group) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className="flex min-w-0 flex-col gap-(--conversation-turn-gap) pb-(--conversation-turn-gap)"
|
|
data-index={virtualItem.index}
|
|
key={virtualItem.key}
|
|
ref={virtualizer.measureElement}
|
|
>
|
|
{group.kind === 'turn' ? (
|
|
<div
|
|
className="composer-human-ai-pair-container relative flex min-w-0 flex-col gap-(--conversation-turn-gap)"
|
|
data-slot="aui_turn-pair"
|
|
>
|
|
{group.indices.map(index => (
|
|
<ThreadPrimitive.MessageByIndex components={components} index={index} key={index} />
|
|
))}
|
|
</div>
|
|
) : (
|
|
<ThreadPrimitive.MessageByIndex components={components} index={group.index} />
|
|
)}
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
{loadingIndicator}
|
|
{clampToComposer && (
|
|
<div
|
|
aria-hidden="true"
|
|
className="shrink-0"
|
|
data-slot="aui_composer-clearance"
|
|
style={{ height: 'var(--thread-last-message-clearance)' }}
|
|
/>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
interface ScrollAnchorOptions {
|
|
enabled: boolean
|
|
groupCount: number
|
|
scrollerRef: React.RefObject<HTMLDivElement | null>
|
|
sessionKey: string | null
|
|
virtualizer: Virtualizer<HTMLDivElement, Element>
|
|
}
|
|
|
|
function useThreadScrollAnchor({ enabled, groupCount, scrollerRef, sessionKey, virtualizer }: ScrollAnchorOptions) {
|
|
// `armed` = parked at bottom, content growth should follow. Cleared on
|
|
// user-driven upward scroll; re-armed when they reach bottom again.
|
|
const armedRef = useRef(true)
|
|
const lastTopRef = useRef(0)
|
|
const prevSessionKeyRef = useRef(sessionKey)
|
|
const prevGroupCountRef = useRef(0)
|
|
|
|
const pinToBottom = useCallback(() => {
|
|
const el = scrollerRef.current
|
|
|
|
if (!el) {
|
|
return
|
|
}
|
|
|
|
el.scrollTop = el.scrollHeight
|
|
lastTopRef.current = el.scrollTop
|
|
}, [scrollerRef])
|
|
|
|
const jumpToBottom = useCallback(() => {
|
|
armedRef.current = true
|
|
|
|
if (groupCount > 0) {
|
|
virtualizer.scrollToIndex(groupCount - 1, { align: 'end', behavior: 'auto' })
|
|
}
|
|
|
|
requestAnimationFrame(() => {
|
|
if (armedRef.current) {
|
|
pinToBottom()
|
|
}
|
|
})
|
|
}, [groupCount, pinToBottom, virtualizer])
|
|
|
|
useEffect(() => () => setThreadScrolledUp(false), [])
|
|
|
|
// Track at-bottom state, dim composer when scrolled up, disarm on user
|
|
// scroll/wheel/touch.
|
|
useEffect(() => {
|
|
const el = scrollerRef.current
|
|
|
|
if (!el) {
|
|
return undefined
|
|
}
|
|
|
|
const disarm = () => {
|
|
armedRef.current = false
|
|
}
|
|
|
|
const onScroll = () => {
|
|
const top = el.scrollTop
|
|
|
|
if (top + 1 < lastTopRef.current) {
|
|
armedRef.current = false
|
|
}
|
|
|
|
lastTopRef.current = top
|
|
|
|
const atBottom = el.scrollHeight - (top + el.clientHeight) <= AT_BOTTOM_THRESHOLD
|
|
|
|
if (atBottom) {
|
|
armedRef.current = true
|
|
}
|
|
|
|
setThreadScrolledUp(!atBottom)
|
|
}
|
|
|
|
const onWheel = (event: WheelEvent) => {
|
|
if (event.deltaY < 0) {
|
|
disarm()
|
|
}
|
|
}
|
|
|
|
el.addEventListener('scroll', onScroll, { passive: true })
|
|
el.addEventListener('wheel', onWheel, { passive: true })
|
|
el.addEventListener('touchmove', disarm, { passive: true })
|
|
|
|
return () => {
|
|
el.removeEventListener('scroll', onScroll)
|
|
el.removeEventListener('wheel', onWheel)
|
|
el.removeEventListener('touchmove', disarm)
|
|
}
|
|
}, [scrollerRef])
|
|
|
|
// Follow content growth (streaming, item measurements, loading indicator)
|
|
// while armed.
|
|
useEffect(() => {
|
|
if (!enabled) {
|
|
return undefined
|
|
}
|
|
|
|
const el = scrollerRef.current
|
|
|
|
if (!el) {
|
|
return undefined
|
|
}
|
|
|
|
const observer = new ResizeObserver(() => {
|
|
if (armedRef.current) {
|
|
pinToBottom()
|
|
}
|
|
})
|
|
|
|
observer.observe(el)
|
|
|
|
if (el.firstElementChild) {
|
|
observer.observe(el.firstElementChild)
|
|
}
|
|
|
|
return () => observer.disconnect()
|
|
}, [enabled, pinToBottom, scrollerRef])
|
|
|
|
// Jump to bottom on session change OR when an empty thread first gets
|
|
// content. Both share the same intent and the same effect.
|
|
useEffect(() => {
|
|
const sessionChanged = prevSessionKeyRef.current !== sessionKey
|
|
const becameNonEmpty = prevGroupCountRef.current === 0 && groupCount > 0
|
|
|
|
prevSessionKeyRef.current = sessionKey
|
|
prevGroupCountRef.current = groupCount
|
|
|
|
if (enabled && (sessionChanged || becameNonEmpty)) {
|
|
jumpToBottom()
|
|
}
|
|
}, [enabled, groupCount, jumpToBottom, sessionKey])
|
|
|
|
useAuiEvent('thread.runStart', jumpToBottom)
|
|
}
|