Skip to content

The agent that grows with you

fix(desktop): let readers scroll away from streaming thoughts

brooklyn!622e98787b8e
diff --git a/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx b/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx
index 944c809c470..694e667bdee 100644
--- a/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx
@@ -197,8 +197,7 @@ const ThinkingDisclosure: FC<{
}
}
- // While the preview is live, pin the scroll container to the bottom on
- // every content growth so the latest tokens are always visible.
+ // Follow new tokens until the user scrolls up to read earlier reasoning.
useEffect(() => {
if (!isPreview) {
return
@@ -216,13 +215,18 @@ const ThinkingDisclosure: FC<{
// scrollHeight read+write per preview per frame. Only actual content
// growth needs the pin; the height rides the RO entry, reflow-free.
let lastHeight = -1
+ let following = true
+
+ const trackScroll = () => {
+ following = el.scrollHeight - el.scrollTop - el.clientHeight < 24
+ }
const pin = (entries: readonly ResizeObserverEntry[]) => {
const height = entries[entries.length - 1]?.borderBoxSize?.[0]?.blockSize ?? -1
const grew = height < 0 || height > lastHeight
lastHeight = height
- if (grew) {
+ if (grew && following) {
el.scrollTop = el.scrollHeight
}
}
@@ -231,8 +235,12 @@ const ThinkingDisclosure: FC<{
// layout already clean (still before paint), avoiding a forced reflow.
const observer = new ResizeObserver(pin)
observer.observe(content)
+ el.addEventListener('scroll', trackScroll, { passive: true })
- return () => observer.disconnect()
+ return () => {
+ observer.disconnect()
+ el.removeEventListener('scroll', trackScroll)
+ }
// Re-run when the disclosure toggles so the observer attaches to the new
// DOM after expand/collapse (refs are conditionally rendered on `open`).
}, [isPreview, open])
@@ -263,7 +271,7 @@ const ThinkingDisclosure: FC<{
// and inherits the disclosure-level opacity fade defined in
// styles.css (~0.67 at rest, 1 on hover/focus). overflow-auto so
// the max-h-40 preview is a real scroller, not a clip.
- 'mt-0.5 w-full min-w-0 max-w-full overflow-auto overscroll-contain wrap-anywhere pb-1',
+ 'mt-0.5 w-full min-w-0 max-w-full overflow-auto overscroll-x-contain overscroll-y-auto wrap-anywhere pb-1',
isPreview && 'max-h-40'
)}
data-slot="aui_thinking-body"
diff --git a/apps/desktop/src/components/assistant-ui/thread/streaming.test.tsx b/apps/desktop/src/components/assistant-ui/thread/streaming.test.tsx
index 4f82314c482..e65df81f3e5 100644
--- a/apps/desktop/src/components/assistant-ui/thread/streaming.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/streaming.test.tsx
@@ -30,6 +30,12 @@ class TestResizeObserver {
resizeObservers.delete(this)
}
+ triggerFor(target: Element, height: number) {
+ if (this.target === target) {
+ this.trigger(height)
+ }
+ }
+
trigger(height: number) {
if (!this.target) {
return
@@ -643,6 +649,54 @@ describe('assistant-ui streaming renderer', () => {
expect(settled).not.toMatch(/\boverflow-hidden\b/)
})
+ it('preserves the thinking reading position on growth and resumes following at the bottom', () => {
+ const { container, rerender } = render(
+ <RunningMessageHarness message={assistantReasoningMessage('First thought.', true)} />
+ )
+
+ const body = container.querySelector<HTMLDivElement>('[data-slot="aui_thinking-body"]')!
+ let height = 600
+ let top = 0
+
+ Object.defineProperties(body, {
+ clientHeight: { configurable: true, get: () => 160 },
+ scrollHeight: { configurable: true, get: () => height },
+ scrollTop: {
+ configurable: true,
+ get: () => top,
+ set: (value: number) => {
+ top = Math.max(0, Math.min(value, height - body.clientHeight))
+ }
+ }
+ })
+
+ const deliverGrowth = () =>
+ act(() => {
+ for (const observer of resizeObservers) {
+ observer.triggerFor(body.firstElementChild!, height)
+ }
+ })
+
+ deliverGrowth()
+ expect(body.scrollTop).toBe(height - body.clientHeight)
+ body.scrollTop = 100
+ fireEvent.scroll(body)
+
+ rerender(<RunningMessageHarness message={assistantReasoningMessage('First thought. More reasoning.', true)} />)
+ height = 900
+ deliverGrowth()
+ expect(body.scrollTop).toBe(100)
+
+ body.scrollTop = height - body.clientHeight
+ fireEvent.scroll(body)
+ rerender(
+ <RunningMessageHarness message={assistantReasoningMessage('First thought. More reasoning. Latest thought.', true)} />
+ )
+ height = 1200
+ deliverGrowth()
+ expect(body.scrollTop).toBe(height - body.clientHeight)
+ })
+
it('does not collapse a live thinking preview when the turn settles', async () => {
const { container, settle } = renderSettlingReasoning()
const toggle = within(container).getByRole('button', { name: /thinking/i })