Skip to content

Commit 9a5dc75

Browse files
t3dotggclaude
andauthored
fix(web): live reasoning stays scrolled to the newest text while it streams (#19334)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d10621d commit 9a5dc75

3 files changed

Lines changed: 136 additions & 5 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,8 +5433,10 @@ function remarkThoughtPreview(fallback: string) {
54335433
function ReasoningTraceContent({ entries }: { entries: ReadonlyArray<TimelineWorkEntry> }) {
54345434
const ctx = use(TimelineRowCtx);
54355435
const { isWorking, latestRunId } = use(TimelineRowActivityCtx);
5436+
const isStreaming = (entry: TimelineWorkEntry) =>
5437+
isWorking && entry.runId === latestRunId && entry.toolLifecycleStatus === "inProgress";
54365438
return (
5437-
<WorkLogDetails>
5439+
<WorkLogDetails followLive={entries.some(isStreaming)}>
54385440
{entries.map((entry) => (
54395441
<ChatMarkdown
54405442
key={entry.id}
@@ -5443,9 +5445,7 @@ function ReasoningTraceContent({ entries }: { entries: ReadonlyArray<TimelineWor
54435445
cwd={ctx.markdownCwd}
54445446
threadRef={ctx.threadRef ?? undefined}
54455447
skills={ctx.skills}
5446-
isStreaming={
5447-
isWorking && entry.runId === latestRunId && entry.toolLifecycleStatus === "inProgress"
5448-
}
5448+
isStreaming={isStreaming(entry)}
54495449
headingLevelOffset={MESSAGE_HEADING_LEVEL}
54505450
onUseArtifactTemplate={ctx.onUseArtifactTemplate}
54515451
onImageExpand={ctx.onImageExpand}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// @vitest-environment jsdom
2+
3+
import { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6+
import { WorkLogDetails } from "./WorkLog";
7+
8+
// jsdom has no layout, so the scroller's geometry is simulated: a 384px
9+
// viewport over content whose height the test grows like a streamed trace.
10+
const VIEWPORT = 384;
11+
let contentHeight = 0;
12+
let scrollTop = 0;
13+
14+
let root: Root;
15+
let host: HTMLDivElement;
16+
17+
function render(followLive: boolean, text: string) {
18+
act(() => {
19+
root.render(
20+
<WorkLogDetails followLive={followLive}>
21+
<p>{text}</p>
22+
</WorkLogDetails>,
23+
);
24+
});
25+
}
26+
27+
function scroller() {
28+
const element = host.firstElementChild;
29+
if (!(element instanceof HTMLDivElement)) throw new Error("missing scroller");
30+
return element;
31+
}
32+
33+
function stream(followLive: boolean, text: string) {
34+
contentHeight += 200;
35+
render(followLive, text);
36+
}
37+
38+
function readerScrollsTo(top: number) {
39+
scroller().scrollTop = top;
40+
act(() => {
41+
scroller().dispatchEvent(new Event("scroll"));
42+
});
43+
}
44+
45+
const maxScrollTop = () => contentHeight - VIEWPORT;
46+
47+
beforeEach(() => {
48+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
49+
contentHeight = 1000;
50+
scrollTop = 0;
51+
Object.defineProperties(HTMLDivElement.prototype, {
52+
clientHeight: { configurable: true, get: () => VIEWPORT },
53+
scrollHeight: { configurable: true, get: () => contentHeight },
54+
scrollTop: {
55+
configurable: true,
56+
get: () => scrollTop,
57+
set: (value: number) => {
58+
scrollTop = Math.max(0, Math.min(value, maxScrollTop()));
59+
},
60+
},
61+
});
62+
host = document.createElement("div");
63+
document.body.append(host);
64+
root = createRoot(host);
65+
});
66+
67+
afterEach(() => {
68+
act(() => root.unmount());
69+
host.remove();
70+
for (const key of ["clientHeight", "scrollHeight", "scrollTop"]) {
71+
Reflect.deleteProperty(HTMLDivElement.prototype, key);
72+
}
73+
vi.unstubAllGlobals();
74+
});
75+
76+
describe("WorkLogDetails followLive", () => {
77+
it("keeps the newest streamed text in view", () => {
78+
render(true, "a");
79+
expect(scrollTop).toBe(maxScrollTop());
80+
81+
stream(true, "ab");
82+
stream(true, "abc");
83+
expect(scrollTop).toBe(maxScrollTop());
84+
});
85+
86+
it("stops following when the reader scrolls up and resumes at the end", () => {
87+
render(true, "a");
88+
readerScrollsTo(100);
89+
90+
stream(true, "ab");
91+
expect(scrollTop).toBe(100);
92+
93+
readerScrollsTo(maxScrollTop());
94+
stream(true, "abc");
95+
expect(scrollTop).toBe(maxScrollTop());
96+
});
97+
98+
it("does not move a trace that is not streaming", () => {
99+
render(false, "a");
100+
stream(false, "ab");
101+
expect(scrollTop).toBe(0);
102+
});
103+
});

‎apps/web/src/components/chat/WorkLog.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { createContext, use, type ComponentProps, type ReactNode } from "react";
1+
import {
2+
createContext,
3+
use,
4+
useLayoutEffect,
5+
useRef,
6+
type ComponentProps,
7+
type ReactNode,
8+
} from "react";
29
import { cn } from "../../lib/utils";
310

411
const GroupedRows = createContext(false);
@@ -122,15 +129,36 @@ export function WorkLogRow({
122129
);
123130
}
124131

132+
/** Distance from the end, in px, that still counts as reading the newest text. */
133+
const LIVE_EDGE_PX = 4;
134+
125135
export function WorkLogDetails({
126136
children,
127137
kind = "text",
138+
followLive = false,
128139
}: {
129140
children: ReactNode;
130141
kind?: "text" | "panel" | "media";
142+
/** Keep streamed text in view. Scrolling up pauses this until the reader returns to the end. */
143+
followLive?: boolean;
131144
}) {
145+
const scrollerRef = useRef<HTMLDivElement>(null);
146+
const atLiveEdgeRef = useRef(true);
147+
// No deps: each streamed chunk re-renders this, and pinning before paint
148+
// shows the new text without a visible jump.
149+
useLayoutEffect(() => {
150+
const scroller = scrollerRef.current;
151+
if (followLive && atLiveEdgeRef.current && scroller) {
152+
scroller.scrollTop = scroller.scrollHeight;
153+
}
154+
});
132155
return (
133156
<div
157+
ref={scrollerRef}
158+
onScroll={(event) => {
159+
const { scrollHeight, scrollTop, clientHeight } = event.currentTarget;
160+
atLiveEdgeRef.current = scrollHeight - scrollTop - clientHeight <= LIVE_EDGE_PX;
161+
}}
134162
className={cn(
135163
"cursor-auto",
136164
kind === "text"

0 commit comments

Comments
 (0)