Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Transcript: a down step keeps the reader's row when rows let go above them shorten the content; 20-row steps (#6722)

Milind Sonif7b4ff145082
diff --git a/src/hooks/use-transcript-viewport.test.ts b/src/hooks/use-transcript-viewport.test.ts
index dcee889587..51017a9600 100644
--- a/src/hooks/use-transcript-viewport.test.ts
+++ b/src/hooks/use-transcript-viewport.test.ts
@@ -92,15 +92,17 @@ type Row = { id: string; role?: "user" | "bot" };
const rows = (count: number, from = 0): Row[] => Array.from({ length: count }, (_, index) => ({ id: `m${from + index}` }));
const ROW = 50;
+let heightOf = (_row: Row) => ROW;
class Scroller {
rows = 0;
+ content = 0;
extra = 0;
scrollTop = 0;
clientHeight = 400;
clientWidth = 400;
calls: ScrollToOptions[] = [];
get scrollHeight() {
- return this.rows * ROW + this.extra;
+ return this.content + this.extra;
}
get bottom() {
return Math.max(0, this.scrollHeight - this.clientHeight);
@@ -114,12 +116,16 @@ class Scroller {
}
}
-/** The mounted rows, each ROW tall, laid out from the scroller's top. */
+/** The mounted rows, each `heightOf` tall (ROW unless a test says otherwise), laid out from the scroller's top. */
const transcriptOf = (shown: readonly Row[]) => ({
- querySelectorAll: () => shown.map((row, index) => {
- const top = index * ROW - scroller.scrollTop;
- return { dataset: { mid: row.id }, getBoundingClientRect: () => ({ top, bottom: top + ROW, height: ROW }) };
- }),
+ querySelectorAll: () => {
+ let below = 0;
+ return shown.map((row) => {
+ const top = below - scroller.scrollTop, height = heightOf(row);
+ below += height;
+ return { dataset: { mid: row.id }, getBoundingClientRect: () => ({ top, bottom: top + height, height }) };
+ });
+ },
});
type Props = Parameters<typeof useTranscriptViewport<Row>>[0];
@@ -141,6 +147,7 @@ function mount(initial: Partial<Props> = {}) {
result.scrollRef.current = scroller as unknown as HTMLDivElement;
result.transcriptRef.current = transcriptOf(result.windowedMessages) as unknown as HTMLDivElement;
scroller.rows = result.windowedMessages.length;
+ scroller.content = result.windowedMessages.reduce((sum, row) => sum + heightOf(row), 0);
scroller.scrollTop = Math.min(scroller.scrollTop, scroller.bottom);
};
const flushFrames = () => {
@@ -178,6 +185,7 @@ function unmount() {
beforeEach(() => {
react.runtime.slots = [];
scroller = new Scroller();
+ heightOf = () => ROW;
frames = [];
keyListeners = new Set();
observers = 0;
@@ -510,6 +518,31 @@ describe("transcript viewport", () => {
expect(view.current.laterCount).toBe(180 - TRANSCRIPT_WINDOW_STEP);
});
+ it("keeps the reader's row in place when the rows let go above them leave the content shorter than where they were", () => {
+ heightOf = (row) => Number(row.id.slice(1)) < 1_000 ? 8 * ROW : ROW;
+ store.state.focusMessage = { threadId: "thread", messageId: "m1000", nonce: 1, consumed: false };
+ const view = mount({ messages: rows(3_000) });
+ const reading = () => {
+ const top = (view.current.transcriptRef.current as unknown as ReturnType<typeof transcriptOf>).querySelectorAll()
+ .find((row) => row.getBoundingClientRect().bottom > 0)!;
+ return { id: top.dataset.mid, offset: top.getBoundingClientRect().top };
+ };
+ const scrollTo = (top: number) => view.act(() => {
+ scroller.scrollTop = top;
+ view.current.scrollHandlers.onScroll();
+ });
+ while (view.current.windowedMessages.length < TRANSCRIPT_WINDOW_MAX) {
+ scrollTo(scroller.bottom - 2_000);
+ scrollTo(scroller.bottom - 100);
+ }
+ scrollTo(scroller.bottom - 2_000);
+ scroller.scrollTop = scroller.bottom - 500;
+ const before = reading();
+ view.act(() => view.current.scrollHandlers.onScroll());
+ expect(view.current.windowedMessages[0]?.id, "the step let tall rows go above the reader").toBe(`m${940 + TRANSCRIPT_WINDOW_STEP}`);
+ expect(reading()).toEqual(before);
+ });
+
it("pages on past a search result as the reader scrolls down, and follows only from the newest row", () => {
store.state.focusMessage = { threadId: "thread", messageId: "m10", nonce: 1, consumed: false };
const view = mount({ messages: rows(300) });
diff --git a/src/hooks/use-transcript-viewport.ts b/src/hooks/use-transcript-viewport.ts
index 3bc3ffb8bc..0a0127656f 100644
--- a/src/hooks/use-transcript-viewport.ts
+++ b/src/hooks/use-transcript-viewport.ts
@@ -358,7 +358,10 @@ export function useTranscriptViewport<T extends { id: string; role?: string }>({
break;
}
}
- el.scrollTop += moved ?? el.scrollHeight - held.height;
+ // Rows let go above the reader can leave the content shorter than where they were, and the browser has already
+ // pulled scrollTop back to the new end; the last scroll seen is where they were.
+ const clamped = el.scrollTop >= el.scrollHeight - el.clientHeight - 1 && lastTop.current > el.scrollTop;
+ el.scrollTop = (clamped ? lastTop.current : el.scrollTop) + (moved ?? el.scrollHeight - held.height);
lastTop.current = el.scrollTop;
});
diff --git a/src/lib/transcript-window.test.ts b/src/lib/transcript-window.test.ts
index 94a01313fb..5f77518acc 100644
--- a/src/lib/transcript-window.test.ts
+++ b/src/lib/transcript-window.test.ts
@@ -227,10 +227,10 @@ describe("a two-sided window", () => {
it("keeps the rows it had on each step, so the row under the reader stays drawn", () => {
const total = 1_000;
- expect(TRANSCRIPT_WINDOW_STEP).toBe(30);
+ expect(TRANSCRIPT_WINDOW_STEP).toBe(20);
const up = windowEarlier({ start: 500, end: 740 }, total);
- expect(up).toEqual({ start: 470, end: 710 });
- const down = windowLater({ start: 470, end: 710 }, total);
+ expect(up).toEqual({ start: 480, end: 720 });
+ const down = windowLater({ start: 480, end: 720 }, total);
expect(down).toEqual({ start: 500, end: 740 });
});
@@ -241,7 +241,7 @@ describe("a two-sided window", () => {
});
it("moves over an older page put in front: one step of it at the top, the same rows anywhere else", () => {
- expect(windowAfterPrepend({ start: 0, end: null }, 200, 400)).toEqual({ start: 170, end: null });
+ expect(windowAfterPrepend({ start: 0, end: null }, 200, 400)).toEqual({ start: 200 - TRANSCRIPT_WINDOW_STEP, end: null });
expect(windowAfterPrepend({ start: 0, end: null }, 50, 100)).toEqual({ start: 50 - TRANSCRIPT_WINDOW_STEP, end: null });
expect(windowAfterPrepend({ start: 40, end: 200 }, 200, 600)).toEqual({ start: 240, end: 400 });
});
diff --git a/src/lib/transcript-window.ts b/src/lib/transcript-window.ts
index 82b6830ad9..d467e37dab 100644
--- a/src/lib/transcript-window.ts
+++ b/src/lib/transcript-window.ts
@@ -2,7 +2,7 @@ export const TRANSCRIPT_WINDOW_SIZE = 120;
/** The most rows drawn at once while the reader is away from the end: one step past it unloads rows at the far edge. */
export const TRANSCRIPT_WINDOW_MAX = 2 * TRANSCRIPT_WINDOW_SIZE;
/** Rows one scroll step draws at a window edge: small enough to mount without a long frame. */
-export const TRANSCRIPT_WINDOW_STEP = 30;
+export const TRANSCRIPT_WINDOW_STEP = 20;
/** The most rows drawn while a text selection holds rows the reader has scrolled away from; past it the selection is set aside. */
export const SELECTION_HOLD_MAX = 2 * TRANSCRIPT_WINDOW_MAX;