Open History to see the last commit
1import type { MessageId, RunId } from "@t3tools/contracts";
2
3export interface TimelineRunObservation {
4 readonly threadKey: string | null;
5 readonly hydrated: boolean;
6 readonly runId: RunId | null;
7}
8
9export function observeTimelineRun(
10 previous: TimelineRunObservation | null,
11 input: TimelineRunObservation & {
12 readonly queued: boolean;
13 readonly messageId: MessageId | null;
14 },
15): { observation: TimelineRunObservation; anchorMessageId: MessageId | null } {
16 const observation = {
17 threadKey: input.threadKey,
18 hydrated: input.hydrated,
19 runId: input.queued ? null : input.runId,
20 };
21 if (previous?.threadKey !== input.threadKey || !previous.hydrated) {
22 return { observation, anchorMessageId: null };
23 }
24 if (
25 !input.hydrated ||
26 input.runId === null ||
27 input.queued ||
28 previous.runId === input.runId ||
29 input.messageId === null
30 ) {
31 return { observation: previous, anchorMessageId: null };
32 }
33 return { observation, anchorMessageId: input.messageId };
34}
35import type { RunAttemptId } from "@t3tools/contracts";
36
37// Match the titlebar fade inset so draft promotion preserves the first row's position.
38export const CHAT_TIMELINE_ANCHOR_OFFSET = 24;
39
40export type TimelineScrollMode = "following-end" | "anchoring-new-turn" | "free-scrolling";
41
42interface TimelineListMeasurementState {
43 readonly data: readonly unknown[];
44 readonly scroll: number;
45 readonly scrollLength: number;
46 readonly positionAtIndex: (index: number) => number | undefined;
47 readonly sizeAtIndex: (index: number) => number | undefined;
48}
49
50function getRowBottom(state: TimelineListMeasurementState, index: number): number | null {
51 const top = state.positionAtIndex(index);
52 const height = state.sizeAtIndex(index);
53 if (
54 typeof top !== "number" ||
55 typeof height !== "number" ||
56 !Number.isFinite(top) ||
57 !Number.isFinite(height)
58 ) {
59 return null;
60 }
61
62 return top + Math.max(1, height);
63}
64
65/**
66 * The list's own content length includes the composer inset spacer, so this
67 * measures from the last row instead. Unknown row positions or an unmeasured
68 * viewport counts as fitting.
69 */
70export function timelineContentOverflowsViewport(
71 state: TimelineListMeasurementState | undefined,
72 input: { readonly composerInset: number; readonly anchorOffset: number },
73): boolean {
74 if (!state || !state.data || state.data.length === 0) {
75 return false;
76 }
77 const scrollLength = state.scrollLength;
78 if (typeof scrollLength !== "number" || !Number.isFinite(scrollLength) || scrollLength <= 0) {
79 return false;
80 }
81 const lastIndex = state.data.length - 1;
82 // Overflow can use an unmeasured tail's lower bound.
83 const lastTop = state.positionAtIndex(lastIndex);
84 const lastBottom =
85 getRowBottom(state, lastIndex) ??
86 (typeof lastTop === "number" && Number.isFinite(lastTop) ? lastTop + 1 : null);
87 if (lastBottom === null) {
88 return false;
89 }
90 const visibleScrollLength = Math.max(0, scrollLength - input.composerInset - input.anchorOffset);
91 return lastBottom > visibleScrollLength;
92}
93
94export interface RememberedTimelinePosition {
95 readonly rowId: string;
96 readonly offsetWithinRow: number;
97 readonly scrollOffset: number;
98 readonly atEnd: boolean;
99 readonly disclosures?: {
100 readonly runs: ReadonlySet<RunId>;
101 readonly workGroups: ReadonlySet<string>;
102 readonly attempts: ReadonlySet<RunAttemptId>;
103 readonly workGroupState: {
104 scrollPositions: Map<string, { readonly entryId: string; readonly offset: number }>;
105 expandedEntries: Set<string>;
106 };
107 };
108}
109
110const rememberedTimelinePositions = new Map<string, RememberedTimelinePosition>();
111
112export function readTimelinePosition(threadKey: string) {
113 return rememberedTimelinePositions.get(threadKey);
114}
115
116export function rememberTimelinePosition(
117 threadKey: string,
118 position: RememberedTimelinePosition,
119 liveFollowEnabled: boolean,
120) {
121 rememberedTimelinePositions.delete(threadKey);
122 rememberedTimelinePositions.set(threadKey, {
123 ...position,
124 atEnd: liveFollowEnabled || position.atEnd,
125 });
126 if (rememberedTimelinePositions.size > 100) {
127 const oldest = rememberedTimelinePositions.keys().next().value;
128 if (oldest !== undefined) rememberedTimelinePositions.delete(oldest);
129 }
130}