The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { MessageId, RunId } from "@t3tools/contracts"; |
| 2 | |
| 3 | export interface TimelineRunObservation { |
| 4 | readonly threadKey: string | null; |
| 5 | readonly hydrated: boolean; |
| 6 | readonly runId: RunId | null; |
| 7 | } |
| 8 | |
| 9 | export 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 | } |
| 35 | import type { RunAttemptId } from "@t3tools/contracts"; |
| 36 | |
| 37 | // Match the titlebar fade inset so draft promotion preserves the first row's position. |
| 38 | export const CHAT_TIMELINE_ANCHOR_OFFSET = 24; |
| 39 | |
| 40 | export type TimelineScrollMode = "following-end" | "anchoring-new-turn" | "free-scrolling"; |
| 41 | |
| 42 | interface 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 | |
| 50 | function 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 | */ |
| 70 | export 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 | |
| 94 | export function resolveDisclosureRevealScrollDelta({ |
| 95 | rowTop, |
| 96 | rowBottom, |
| 97 | viewportTop, |
| 98 | viewportBottom, |
| 99 | composerOverlayHeight, |
| 100 | anchorOffset, |
| 101 | }: { |
| 102 | readonly rowTop: number; |
| 103 | readonly rowBottom: number; |
| 104 | readonly viewportTop: number; |
| 105 | readonly viewportBottom: number; |
| 106 | readonly composerOverlayHeight: number; |
| 107 | readonly anchorOffset: number; |
| 108 | }): number { |
| 109 | const hiddenBelow = rowBottom - (viewportBottom - composerOverlayHeight); |
| 110 | if (!(hiddenBelow > 0)) return 0; |
| 111 | const roomAbove = rowTop - (viewportTop + anchorOffset); |
| 112 | return Math.max(0, Math.min(hiddenBelow, roomAbove)); |
| 113 | } |
| 114 | |
| 115 | export interface RememberedTimelinePosition { |
| 116 | readonly rowId: string; |
| 117 | readonly offsetWithinRow: number; |
| 118 | readonly scrollOffset: number; |
| 119 | readonly atEnd: boolean; |
| 120 | readonly disclosures?: { |
| 121 | readonly runs: ReadonlySet<RunId>; |
| 122 | readonly workGroups: ReadonlySet<string>; |
| 123 | readonly attempts: ReadonlySet<RunAttemptId>; |
| 124 | readonly workGroupState: { |
| 125 | scrollPositions: Map<string, { readonly entryId: string; readonly offset: number }>; |
| 126 | expandedEntries: Set<string>; |
| 127 | }; |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | const rememberedTimelinePositions = new Map<string, RememberedTimelinePosition>(); |
| 132 | |
| 133 | export function readTimelinePosition(threadKey: string) { |
| 134 | return rememberedTimelinePositions.get(threadKey); |
| 135 | } |
| 136 | |
| 137 | export function rememberTimelinePosition( |
| 138 | threadKey: string, |
| 139 | position: RememberedTimelinePosition, |
| 140 | liveFollowEnabled: boolean, |
| 141 | ) { |
| 142 | rememberedTimelinePositions.delete(threadKey); |
| 143 | rememberedTimelinePositions.set(threadKey, { |
| 144 | ...position, |
| 145 | atEnd: liveFollowEnabled || position.atEnd, |
| 146 | }); |
| 147 | if (rememberedTimelinePositions.size > 100) { |
| 148 | const oldest = rememberedTimelinePositions.keys().next().value; |
| 149 | if (oldest !== undefined) rememberedTimelinePositions.delete(oldest); |
| 150 | } |
| 151 | } |