Skip to content

Commit 52a14d6

Browse files
perf(web): streamed assistant text re-renders only its own row
Each assistant delta changed the visible turn items, so the whole timeline and its virtual list re-rendered, and the list ran three extra layout commits, before the streaming row itself updated. The timeline now reads visible items that hold while only streamed assistant text moves, and a streaming row reads its text through its own atom. The timeline still updates on any structural change, including the message finishing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6761387 commit 52a14d6

6 files changed

Lines changed: 127 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ScopedThreadRef } from "@t3tools/contracts";
22
import { type ComponentProps, useLayoutEffect } from "react";
33
import { useIncrementalTimelineEntries } from "../../hooks/useIncrementalTimelineEntries";
44
import type { TimelineEntriesInput, TimelineEntry } from "../../session-logic";
5-
import { useThreadVisibleTurnItems } from "../../state/entities";
5+
import { useTimelineVisibleTurnItems } from "../../state/entities";
66
import {
77
peekRememberedThreadTimeline,
88
rememberReadyThreadTimeline,
@@ -14,7 +14,8 @@ import { MessagesTimeline } from "./MessagesTimeline";
1414
/**
1515
* The timeline with the streamed text. ChatView reads the thread through its quiet
1616
* atoms, which hold through text deltas; this reads the live turn items, so a
17-
* delta re-renders the timeline without re-rendering ChatView.
17+
* reasoning delta re-renders the timeline without re-rendering ChatView. Streamed
18+
* assistant text reaches only its own row, through useAssistantMessageText.
1819
*/
1920
export function ThreadTimeline({
2021
threadDetailRef,
@@ -34,7 +35,7 @@ export function ThreadTimeline({
3435
syncing: boolean;
3536
detailLoading: boolean;
3637
}) {
37-
const visibleTurnItems = useThreadVisibleTurnItems(threadDetailRef);
38+
const visibleTurnItems = useTimelineVisibleTurnItems(threadDetailRef);
3839
const serverEntries = useIncrementalTimelineEntries(activeThreadKey, {
3940
...entriesInput,
4041
visibleTurnItems,

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { McpAppFrame } from "./McpAppFrame";
1212
import { checkpointFileTotals } from "@t3tools/client-runtime/state/thread-checkpoints";
1313
import { ChangedFilesCard } from "./ChangedFilesTree";
1414
import { useFileContextMenuHandler } from "../../fileContextMenu";
15-
import { useProject, useThreadShell } from "../../state/entities";
15+
import { useAssistantMessageText, useProject, useThreadShell } from "../../state/entities";
1616
import { MessageCopyButton } from "./MessageCopyButton";
1717
import { AssistantCitationSource } from "./AssistantCitationSource";
1818
import { resolveAssistantMessageCopyState } from "./MessagesTimeline.logic";
@@ -26,7 +26,13 @@ import { MESSAGE_HEADING_LEVEL, MessageAuthorHeading } from "./TimelineRowParts"
2626

2727
export function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
2828
const ctx = use(TimelineRowCtx);
29-
const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
29+
// The timeline holds a streaming message's row while only its text grows.
30+
const liveText = useAssistantMessageText(
31+
row.message.streaming ? ctx.threadRef : null,
32+
row.projectedItem?.item.id,
33+
);
34+
const text = liveText ?? row.message.text;
35+
const messageText = text || (row.message.streaming ? "" : "(empty response)");
3036

3137
return (
3238
<>

‎apps/web/src/state/entities.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
6565
const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
6666
Object.freeze([]),
6767
).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty"));
68+
const EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM = Atom.make<string | null>(null).pipe(
69+
Atom.withLabel("web-assistant-message-text:empty"),
70+
);
6871
const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
6972
Atom.withLabel("web-thread-last-visited:empty"),
7073
);
@@ -212,6 +215,29 @@ export function useThreadVisibleTurnItems(
212215
);
213216
}
214217

218+
/** Visible turn items held through streamed assistant text; see useAssistantMessageText. */
219+
export function useTimelineVisibleTurnItems(
220+
ref: ScopedThreadRef | null,
221+
): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
222+
return useAtomValue(
223+
ref === null
224+
? EMPTY_VISIBLE_TURN_ITEMS_ATOM
225+
: environmentThreadDetails.timelineVisibleTurnItemsAtom(ref),
226+
);
227+
}
228+
229+
/** One assistant message's live text, or null when the thread does not hold the item. */
230+
export function useAssistantMessageText(
231+
ref: ScopedThreadRef | null,
232+
itemId: string | undefined,
233+
): string | null {
234+
return useAtomValue(
235+
ref === null || itemId === undefined
236+
? EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM
237+
: environmentThreadDetails.assistantMessageTextAtom(ref, itemId),
238+
);
239+
}
240+
215241
/** Visible turn items without their streamed text; see ThreadStructure. */
216242
export function useQuietThreadVisibleTurnItems(
217243
ref: ScopedThreadRef | null,

‎apps/web/test/appScenarios.renders.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ it("app-stream-assistant-turn", async () => {
201201
.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
202202
.toBeVisible();
203203
});
204-
// Only the streaming row changes; history rows must not re-render with it.
204+
// Only the streaming row changes. The timeline renders for the new item and its layout, not
205+
// per delta, and history rows not at all.
206+
expect(counted.components.ConversationTimeline).toBeLessThanOrEqual(3);
205207
expect(counted.components.UserTimelineRow).toBeUndefined();
206208
expect(counted.components.AssistantTimelineRow).toBe(STREAM_DELTAS);
207209
// Each delta adds a paragraph; finished blocks keep their render.

‎packages/client-runtime/src/state/threadDetail.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ describe("quietThreadAtom", () => {
197197
});
198198
});
199199

200+
describe("timelineVisibleTurnItemsAtom", () => {
201+
const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
202+
203+
it("holds through streamed assistant text while the message reads its own text", () => {
204+
const stateAtom = Atom.make(
205+
AsyncResult.success({
206+
...EMPTY_ENVIRONMENT_THREAD_STATE,
207+
data: Option.some<OrchestrationV2ThreadProjection>(withItem(assistant)),
208+
}),
209+
);
210+
const details = createEnvironmentThreadDetailAtoms(() => stateAtom);
211+
const timelineAtom = details.timelineVisibleTurnItemsAtom(ref);
212+
const textAtom = details.assistantMessageTextAtom(ref, assistant.id);
213+
const registry = AtomRegistry.make();
214+
registry.mount(timelineAtom);
215+
registry.mount(textAtom);
216+
const push = (item: OrchestrationV2TurnItem) =>
217+
registry.set(
218+
stateAtom,
219+
AsyncResult.success({
220+
...EMPTY_ENVIRONMENT_THREAD_STATE,
221+
data: Option.some(withItem(item, later)),
222+
}),
223+
);
224+
const first = registry.get(timelineAtom);
225+
226+
push({ ...assistant, text: "Hello", updatedAt: later });
227+
expect(registry.get(timelineAtom)).toBe(first);
228+
expect(registry.get(textAtom)).toBe("Hello");
229+
230+
push({ ...assistant, text: "Hello", streaming: false, status: "completed" });
231+
expect(registry.get(timelineAtom)[0]?.item).toMatchObject({ text: "Hello", streaming: false });
232+
registry.dispose();
233+
});
234+
});
235+
200236
it("only compares replaced entries in a streamed reducer delta", () => {
201237
const size = 100;
202238
let slotReads = 0;

‎packages/client-runtime/src/state/threadDetail.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ function sameVisibleTurnItemStructure(before: VisibleTurnItem, after: VisibleTur
147147
);
148148
}
149149

150+
function isStreamingAssistantMessage(visibleItem: VisibleTurnItem): boolean {
151+
return visibleItem.item.type === "assistant_message" && visibleItem.item.streaming;
152+
}
153+
154+
/** Whether `next` only streams more text into assistant messages that were already streaming. */
155+
function sameApartFromStreamedAssistantText(
156+
previous: OrchestrationV2ThreadProjection["visibleTurnItems"],
157+
next: OrchestrationV2ThreadProjection["visibleTurnItems"],
158+
): boolean {
159+
return sameProjectionArray(
160+
previous,
161+
next,
162+
(before, after) =>
163+
before === after ||
164+
(isStreamingAssistantMessage(before) &&
165+
isStreamingAssistantMessage(after) &&
166+
sameVisibleTurnItemStructure(before, after)),
167+
);
168+
}
169+
150170
/**
151171
* Whether `previous` and `next` have the same ThreadStructure: they differ
152172
* only by text streamed into existing assistant and reasoning items or by a
@@ -266,6 +286,32 @@ export function createEnvironmentThreadDetailAtoms<E>(
266286
);
267287
});
268288

289+
// Holds while only streamed assistant text moves, so the timeline re-renders on structure;
290+
// each streaming message reads its text through assistantMessageTextAtom.
291+
const timelineVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
292+
let previous = EMPTY_VISIBLE_TURN_ITEMS;
293+
return Atom.make((get) => {
294+
const next = get(visibleTurnItemsAtomFamily(key));
295+
if (!sameApartFromStreamedAssistantText(previous, next)) previous = next;
296+
return previous;
297+
}).pipe(
298+
Atom.setIdleTTL(0),
299+
Atom.withLabel(`environment-thread-timeline-visible-turn-items:${key}`),
300+
);
301+
});
302+
303+
const assistantMessageTextAtomFamily = Atom.family((key: string) => {
304+
const separator = key.lastIndexOf("\u0000");
305+
const thread = key.slice(0, separator);
306+
const itemId = key.slice(separator + 1);
307+
return Atom.make((get) => {
308+
const item = get(visibleTurnItemsAtomFamily(thread)).find(
309+
(visibleItem) => visibleItem.item.id === itemId,
310+
)?.item;
311+
return item?.type === "assistant_message" ? item.text : null;
312+
}).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-assistant-message-text:${key}`));
313+
});
314+
269315
const queueWorkflowAtomFamily = Atom.family((key: string) => {
270316
let previous: Pick<
271317
OrchestrationV2ThreadProjection,
@@ -444,6 +490,10 @@ export function createEnvironmentThreadDetailAtoms<E>(
444490
visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
445491
quietVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
446492
quietVisibleTurnItemsAtomFamily(threadKey(ref)),
493+
timelineVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
494+
timelineVisibleTurnItemsAtomFamily(threadKey(ref)),
495+
assistantMessageTextAtom: (ref: ScopedThreadRef, itemId: string) =>
496+
assistantMessageTextAtomFamily(`${threadKey(ref)}\u0000${itemId}`),
447497
statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
448498
sequenceAtom: (ref: ScopedThreadRef) => sequenceAtomFamily(threadKey(ref)),
449499
errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),

0 commit comments

Comments
 (0)