Skip to content

Commit e5d79ae

Browse files
authored
fix(tui): load transcript history on demand (#42388)
1 parent b66f01b commit e5d79ae

1 file changed

Lines changed: 47 additions & 65 deletions

File tree

‎packages/tui/src/routes/session/index.tsx‎

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,9 @@ addDefaultParsers(parsers.parsers)
115115
const NAVIGATION_SLACK_ID = "session-navigation-slack"
116116
const BACKGROUND_TOOL_HINT_DELAY = 1_000
117117

118-
// Tail-first transcript mounting: rows mounted with the session, then backfill cadence.
119-
// The tail comfortably overfills a tall viewport; backfill drains a 200-message transcript
120-
// in a few hundred milliseconds without a perceptible pause.
118+
// The tail comfortably overfills a tall viewport; older rows mount as the reader approaches them.
121119
const TRANSCRIPT_TAIL_ROWS = 40
122120
const TRANSCRIPT_BACKFILL_CHUNK = 60
123-
const TRANSCRIPT_BACKFILL_DELAY = 120
124121
type PendingAction = "steer" | "queue" | "cancel"
125122

126123
const context = createContext<{
@@ -362,32 +359,33 @@ export function Session() {
362359
})
363360
}
364361

365-
// Tail-first transcript mounting: only the newest rows mount when the session opens, and the
366-
// rest backfill in chunks shortly after, so switching to a long session costs the visible tail
367-
// instead of the whole transcript. Until backfill pins the count, the hidden span derives from
368-
// the row count, so it needs no effect ordering; the clamp keeps at least a tail visible when a
369-
// re-reduce shrinks the transcript. Streaming appends land at the end of the visible slice.
362+
// Tail-first transcript mounting: only the newest rows mount when the session opens. Older rows
363+
// mount on demand near the top, keeping inactive tabs cheap to tear down. Until the first chunk
364+
// pins the count, the hidden span derives from the row count, so streaming appends remain visible.
370365
const [hiddenRows, setHiddenRows] = createSignal<number>()
371366
const hidden = createMemo(() => Math.max(0, Math.min(hiddenRows() ?? Infinity, rows.length - TRANSCRIPT_TAIL_ROWS)))
372367
const visibleRows = createMemo(() => (hidden() === 0 ? rows : rows.slice(hidden())))
373-
createEffect(() => {
368+
let revealingOlderRows = false
369+
const revealOlderRows = (scrollBy = 0) => {
374370
const current = hidden()
375-
if (current === 0) return
376-
// Until the first chunk pins hiddenRows, appends change hidden() and reset this timer, so
377-
// backfill waits for a pause in streaming before starting. Once pinned, it drains on a fixed
378-
// cadence undisturbed by appends.
379-
const timer = setTimeout(() => {
380-
const before = scroll && !scroll.isDestroyed ? scroll.scrollHeight : undefined
381-
const viewportBottom = before === undefined ? 0 : scroll.scrollTop + scroll.viewport.height
382-
setHiddenRows(Math.max(0, current - TRANSCRIPT_BACKFILL_CHUNK))
383-
if (before === undefined) return
384-
// Sticky scroll holds bottom-anchored readers through the mount; compensation is only for
385-
// readers who have scrolled up.
386-
if (viewportBottom >= before - 1) return
387-
afterLayout(() => scroll.scrollBy(scroll.scrollHeight - before))
388-
}, TRANSCRIPT_BACKFILL_DELAY)
389-
onCleanup(() => clearTimeout(timer))
390-
})
371+
if (
372+
revealingOlderRows ||
373+
current === 0 ||
374+
!scroll ||
375+
scroll.isDestroyed ||
376+
scroll.scrollTop > scroll.viewport.height
377+
)
378+
return false
379+
revealingOlderRows = true
380+
const before = scroll.scrollHeight
381+
setHiddenRows(Math.max(0, current - TRANSCRIPT_BACKFILL_CHUNK))
382+
afterLayout(() => {
383+
revealingOlderRows = false
384+
scroll.scrollBy(scroll.scrollHeight - before + scrollBy)
385+
updateAwayFromBottom()
386+
})
387+
return true
388+
}
391389
/** Message navigation needs the full transcript mounted before walking or jumping. */
392390
const ensureAllRows = (continuation: () => void) => {
393391
if (hidden() === 0) return continuation()
@@ -536,78 +534,57 @@ export function Session() {
536534
}, 50)
537535
}
538536

537+
function moveTranscript(delta: number) {
538+
clearMessageNavigation()
539+
if (delta >= 0 || !revealOlderRows(delta)) {
540+
scroll.scrollBy(delta)
541+
updateAwayFromBottom()
542+
}
543+
dialog.clear()
544+
}
545+
539546
const globalCommands = [
540547
{
541548
id: "session.page.up",
542549
title: "Page up",
543550
group: "Session",
544551
palette: undefined,
545-
run: () => {
546-
clearMessageNavigation()
547-
scroll.scrollBy(-scroll.height / 2)
548-
updateAwayFromBottom()
549-
dialog.clear()
550-
},
552+
run: () => moveTranscript(-scroll.height / 2),
551553
},
552554
{
553555
id: "session.page.down",
554556
title: "Page down",
555557
group: "Session",
556558
palette: undefined,
557-
run: () => {
558-
clearMessageNavigation()
559-
scroll.scrollBy(scroll.height / 2)
560-
updateAwayFromBottom()
561-
dialog.clear()
562-
},
559+
run: () => moveTranscript(scroll.height / 2),
563560
},
564561
{
565562
id: "session.line.up",
566563
title: "Line up",
567564
group: "Session",
568565
palette: undefined,
569-
run: () => {
570-
clearMessageNavigation()
571-
scroll.scrollBy(-1)
572-
updateAwayFromBottom()
573-
dialog.clear()
574-
},
566+
run: () => moveTranscript(-1),
575567
},
576568
{
577569
id: "session.line.down",
578570
title: "Line down",
579571
group: "Session",
580572
palette: undefined,
581-
run: () => {
582-
clearMessageNavigation()
583-
scroll.scrollBy(1)
584-
updateAwayFromBottom()
585-
dialog.clear()
586-
},
573+
run: () => moveTranscript(1),
587574
},
588575
{
589576
id: "session.half.page.up",
590577
title: "Half page up",
591578
group: "Session",
592579
palette: undefined,
593-
run: () => {
594-
clearMessageNavigation()
595-
scroll.scrollBy(-scroll.height / 4)
596-
updateAwayFromBottom()
597-
dialog.clear()
598-
},
580+
run: () => moveTranscript(-scroll.height / 4),
599581
},
600582
{
601583
id: "session.half.page.down",
602584
title: "Half page down",
603585
group: "Session",
604586
palette: undefined,
605-
run: () => {
606-
clearMessageNavigation()
607-
scroll.scrollBy(scroll.height / 4)
608-
updateAwayFromBottom()
609-
dialog.clear()
610-
},
587+
run: () => moveTranscript(scroll.height / 4),
611588
},
612589
]
613590

@@ -619,8 +596,10 @@ export function Session() {
619596
palette: undefined,
620597
run: () => {
621598
clearMessageNavigation()
622-
scroll.scrollTo(0)
623-
updateAwayFromBottom()
599+
ensureAllRows(() => {
600+
scroll.scrollTo(0)
601+
updateAwayFromBottom()
602+
})
624603
dialog.clear()
625604
},
626605
},
@@ -1102,7 +1081,10 @@ export function Session() {
11021081
stickyStart="bottom"
11031082
flexGrow={1}
11041083
scrollAcceleration={scrollAcceleration()}
1105-
onMouseScroll={updateAwayFromBottom}
1084+
onMouseScroll={(event) => {
1085+
if (event.scroll?.direction === "up" && revealOlderRows()) return
1086+
updateAwayFromBottom()
1087+
}}
11061088
>
11071089
<For each={visibleRows()}>
11081090
{(row, index) => (

0 commit comments

Comments
 (0)