Skip to content

Open-source, local-first AI agent for real work.

20.7 KB
Raw Blame
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
import type { FollowOutput, IndexLocationWithAlign, StateSnapshot, VirtuosoHandle } from "react-virtuoso";

/**
 * 跟随底部的滑动时间常数:剩余距离按 e^(-t/τ) 衰减,一行(约 24px)的增长约 200ms 滑完。
 * 按时间而不是按帧算,120Hz 与 60Hz 屏上速度一致。
 */
const GLIDE_TIME_CONSTANT_MS = 60;
/** 滑动最多每秒约 60 次推进:120Hz 屏上隔帧推进,出帧减半、肉眼仍是连续的。 */
const MIN_GLIDE_FRAME_MS = 15;
/** 首帧或长时间未推进时按这个间隔计步,避免一次跨太远。 */
const DEFAULT_GLIDE_FRAME_MS = 16;
const MAX_GLIDE_FRAME_MS = 100;
const MAX_CACHED_FEED_STATES = 24;
const SCROLL_SETTLE_DELAY_MS = 180;
const SCROLL_TO_BOTTOM_VISIBILITY_THRESHOLD_PX = 500;
const INITIAL_TAIL_LOCATION: IndexLocationWithAlign = { index: "LAST", align: "end" };

interface CachedFeedState {
	readonly itemIdentity: string | null;
	readonly itemCount: number;
	readonly snapshot: StateSnapshot;
}

interface InitialViewportSelection {
	readonly resetKey: string | null | undefined;
	readonly snapshot?: StateSnapshot;
	readonly initialTopMostItemIndex?: IndexLocationWithAlign | number;
}

const feedStateCache = new Map<string, CachedFeedState>();

function readCachedFeedState(
	key: string | null | undefined,
	itemCount: number,
	itemIdentity: string | null,
): StateSnapshot | undefined {
	if (!key || itemCount === 0) return undefined;
	const cached = feedStateCache.get(key);
	return cached?.itemCount === itemCount && cached.itemIdentity === itemIdentity ? cached.snapshot : undefined;
}

function cacheFeedState(
	key: string | null | undefined,
	itemCount: number,
	itemIdentity: string | null,
	snapshot: StateSnapshot,
): void {
	if (!key || itemCount === 0) return;
	feedStateCache.delete(key);
	feedStateCache.set(key, {
		itemIdentity,
		itemCount,
		snapshot: {
			scrollTop: snapshot.scrollTop,
			ranges: snapshot.ranges.map((range) => ({ ...range })),
		},
	});
	while (feedStateCache.size > MAX_CACHED_FEED_STATES) {
		const oldestKey = feedStateCache.keys().next().value;
		if (oldestKey === undefined) break;
		feedStateCache.delete(oldestKey);
	}
}

function getItemIdentity<T>(items: readonly T[], getItemKey?: (item: T) => string | null): string | null {
	if (!getItemKey || items.length === 0) return null;
	const first = getItemKey(items[0]);
	const last = getItemKey(items[items.length - 1]);
	return `${items.length}:${first ?? ""}:${last ?? ""}`;
}

/** 本帧应滑过的距离:时间衰减,至少 1px(不足 1px 时直接到位),避免尾巴拖很久。 */
function getGlideStep(diff: number, elapsedMs: number): number {
	const eased = diff * (1 - Math.exp(-elapsedMs / GLIDE_TIME_CONSTANT_MS));
	return Math.min(diff, Math.max(1, eased));
}

export interface MessageFeedScrollModel {
	readonly followOutput: FollowOutput;
	readonly initialTopMostItemIndex?: IndexLocationWithAlign | number;
	readonly onAtBottomChange: (atBottom: boolean) => void;
	readonly onTotalListHeightChange: (height: number) => void;
	readonly scrollerElement: HTMLElement | null;
	readonly scrollerRef: (element: HTMLElement | Window | null) => void;
	readonly scrollToBottom: () => void;
	readonly scrollToItem: (index: number) => void;
	readonly showScrollToBottom: boolean;
	readonly virtuosoRef: React.RefObject<VirtuosoHandle | null>;
	readonly restoreStateFrom?: StateSnapshot;
}

export interface MessageFeedScrollModelInput<T> {
	readonly active: boolean;
	readonly items: readonly T[];
	readonly resetKey?: string | null;
	readonly layoutResizing?: boolean;
	readonly initialTargetKey?: string | null;
	readonly getItemKey?: (item: T) => string | null;
	readonly onInitialTargetHandled?: () => void;
	readonly shouldFollowOnAppend?: (item: T) => boolean;
}

/** Feed viewport mechanics with no message schema, global store or product policy. */
export function useMessageFeedScrollModel<T>({
	active,
	items,
	resetKey,
	layoutResizing = false,
	initialTargetKey,
	getItemKey,
	onInitialTargetHandled,
	shouldFollowOnAppend,
}: MessageFeedScrollModelInput<T>): MessageFeedScrollModel {
	const virtuosoRef = useRef<VirtuosoHandle>(null);
	const itemIdentity = getItemIdentity(items, getItemKey);
	const initialViewportSelectionRef = useRef<InitialViewportSelection | null>(null);
	if (initialViewportSelectionRef.current === null || initialViewportSelectionRef.current.resetKey !== resetKey) {
		// Virtuoso 的 restoreStateFrom 是初始化输入。会话先以空列表挂载、随后再补齐历史时,
		// 不应在中途注入旧快照或改写初始索引,否则会覆盖正在进行的底部定位。
		const snapshot = readCachedFeedState(resetKey, items.length, itemIdentity);
		initialViewportSelectionRef.current = {
			resetKey,
			snapshot,
			initialTopMostItemIndex: snapshot === undefined ? INITIAL_TAIL_LOCATION : undefined,
		};
	}
	const { initialTopMostItemIndex, snapshot: restoreStateFrom } = initialViewportSelectionRef.current;
	const scrollerElementRef = useRef<HTMLElement | null>(null);
	const [scrollerElement, setScrollerElement] = useState<HTMLElement | null>(null);
	const [scrollToBottomVisibilityState, setScrollToBottomVisibilityState] = useState(() => ({
		resetKey,
		visible: false,
	}));
	const showScrollToBottom =
		scrollToBottomVisibilityState.resetKey === resetKey && scrollToBottomVisibilityState.visible;
	const layoutResizingRef = useRef(layoutResizing);
	const previousLayoutResizingRef = useRef(layoutResizing);
	layoutResizingRef.current = layoutResizing;
	const shouldInitiallyFollowBottom = !initialTargetKey && restoreStateFrom === undefined;
	const [followOutputState, setFollowOutputState] = useState(() => ({
		resetKey,
		enabled: shouldInitiallyFollowBottom,
	}));
	const followOutputEnabled =
		followOutputState.resetKey === resetKey ? followOutputState.enabled : shouldInitiallyFollowBottom;
	const shouldFollowBottomRef = useRef(followOutputEnabled);
	shouldFollowBottomRef.current = followOutputEnabled;
	const lerpAnimationFrameRef = useRef<number | null>(null);
	const snapAnimationFrameRef = useRef<number | null>(null);
	const visibilityAnimationFrameRef = useRef<number | null>(null);
	const lastGlideAtRef = useRef<number | null>(null);
	const lastTouchYRef = useRef<number | null>(null);
	const lastScrollTopRef = useRef(0);
	const pointerScrollingRef = useRef(false);
	const browsingHistoryRef = useRef(!shouldInitiallyFollowBottom);
	const lastUserScrollDirectionRef = useRef<"up" | "down" | null>(null);
	const activeRef = useRef(active);
	activeRef.current = active;
	const skipNextLerpRef = useRef(false);
	const scrollSettleTimerRef = useRef<number | null>(null);
	const interactionResetKeyRef = useRef(resetKey);
	if (interactionResetKeyRef.current !== resetKey) {
		interactionResetKeyRef.current = resetKey;
		browsingHistoryRef.current = !shouldInitiallyFollowBottom;
		lastUserScrollDirectionRef.current = null;
		pointerScrollingRef.current = false;
	}
	const stateKeyRef = useRef(resetKey);
	const stateItemCountRef = useRef(items.length);
	const stateItemIdentityRef = useRef(itemIdentity);
	stateKeyRef.current = resetKey;
	stateItemCountRef.current = items.length;
	stateItemIdentityRef.current = itemIdentity;
	const setShouldFollowBottom = useCallback((shouldFollow: boolean) => {
		shouldFollowBottomRef.current = shouldFollow;
		const currentResetKey = stateKeyRef.current;
		setFollowOutputState((current) =>
			current.resetKey === currentResetKey && current.enabled === shouldFollow
				? current
				: { resetKey: currentResetKey, enabled: shouldFollow },
		);
	}, []);
	// Only a position the reader chose is remembered. While the feed follows its tail the
	// viewport may be anywhere on the way to the bottom (estimated heights, content still
	// measuring); remembering that would bring the feed back mid-way with following off.
	const captureState = useCallback(() => {
		const key = stateKeyRef.current;
		const itemCount = stateItemCountRef.current;
		const identity = stateItemIdentityRef.current;
		if (!key || itemCount === 0) return;
		if (shouldFollowBottomRef.current) {
			feedStateCache.delete(key);
			return;
		}
		const handle = virtuosoRef.current;
		if (!handle || typeof handle.getState !== "function") return;
		handle.getState((snapshot) => cacheFeedState(key, itemCount, identity, snapshot));
	}, []);
	const updateScrollToBottomVisibility = useCallback(() => {
		visibilityAnimationFrameRef.current = null;
		const element = scrollerElementRef.current;
		const visible = element
			? element.scrollHeight - element.clientHeight - element.scrollTop > SCROLL_TO_BOTTOM_VISIBILITY_THRESHOLD_PX
			: false;
		const currentResetKey = stateKeyRef.current;
		setScrollToBottomVisibilityState((current) =>
			current.resetKey === currentResetKey && current.visible === visible
				? current
				: { resetKey: currentResetKey, visible },
		);
	}, []);
	const scheduleScrollToBottomVisibilityUpdate = useCallback(() => {
		if (visibilityAnimationFrameRef.current !== null) return;
		visibilityAnimationFrameRef.current = requestAnimationFrame(updateScrollToBottomVisibility);
	}, [updateScrollToBottomVisibility]);

	const tickLerp = useCallback((now: number) => {
		const element = scrollerElementRef.current;
		if (!element || !shouldFollowBottomRef.current) {
			lerpAnimationFrameRef.current = null;
			lastGlideAtRef.current = null;
			return;
		}
		const lastGlideAt = lastGlideAtRef.current;
		if (lastGlideAt !== null && now - lastGlideAt < MIN_GLIDE_FRAME_MS) {
			lerpAnimationFrameRef.current = requestAnimationFrame(tickLerp);
			return;
		}
		const elapsed =
			lastGlideAt === null ? DEFAULT_GLIDE_FRAME_MS : Math.min(MAX_GLIDE_FRAME_MS, Math.max(0, now - lastGlideAt));
		const target = Math.max(0, element.scrollHeight - element.clientHeight);
		const diff = target - element.scrollTop;
		if (activeRef.current && diff > element.clientHeight) {
			// 流式中一次长出超过一屏(切回会话、展开大块结果):直接贴底,不拖长动画。
			element.scrollTop = target;
			lastGlideAtRef.current = null;
			lerpAnimationFrameRef.current = null;
			return;
		}
		if (diff > 0.5) {
			lastGlideAtRef.current = now;
			element.scrollTop += getGlideStep(diff, elapsed);
			lerpAnimationFrameRef.current = requestAnimationFrame(tickLerp);
		} else {
			lastGlideAtRef.current = null;
			lerpAnimationFrameRef.current = null;
		}
	}, []);

	const startFollowingBottom = useCallback(() => {
		if (lerpAnimationFrameRef.current === null) {
			lastGlideAtRef.current = null;
			lerpAnimationFrameRef.current = requestAnimationFrame(tickLerp);
		}
	}, [tickLerp]);

	const stopFollowingBottom = useCallback(() => {
		browsingHistoryRef.current = true;
		setShouldFollowBottom(false);
		lastGlideAtRef.current = null;
		if (lerpAnimationFrameRef.current !== null) {
			cancelAnimationFrame(lerpAnimationFrameRef.current);
			lerpAnimationFrameRef.current = null;
		}
	}, [setShouldFollowBottom]);

	const onAtBottomChange = useCallback(
		(atBottom: boolean) => {
			if (!atBottom) return;
			if (browsingHistoryRef.current && lastUserScrollDirectionRef.current !== "down") return;
			browsingHistoryRef.current = false;
			lastUserScrollDirectionRef.current = null;
			setShouldFollowBottom(true);
			startFollowingBottom();
		},
		[setShouldFollowBottom, startFollowingBottom],
	);
	const scrollToItem = useCallback(
		(index: number) => {
			stopFollowingBottom();
			skipNextLerpRef.current = true;
			virtuosoRef.current?.scrollToIndex({ index, align: "start", behavior: "smooth" });
		},
		[stopFollowingBottom],
	);
	const scrollToBottom = useCallback(() => {
		browsingHistoryRef.current = false;
		lastUserScrollDirectionRef.current = null;
		setShouldFollowBottom(true);
		virtuosoRef.current?.scrollToIndex({ index: "LAST", align: "end", behavior: "smooth" });
	}, [setShouldFollowBottom]);

	const onWheel = useCallback(
		(event: WheelEvent) => {
			if (event.deltaY < 0) {
				lastUserScrollDirectionRef.current = "up";
				stopFollowingBottom();
			} else if (event.deltaY > 0) {
				lastUserScrollDirectionRef.current = "down";
			}
		},
		[stopFollowingBottom],
	);
	const onTouchStart = useCallback((event: TouchEvent) => {
		lastTouchYRef.current = event.touches[0]?.clientY ?? null;
	}, []);
	const onTouchMove = useCallback(
		(event: TouchEvent) => {
			const y = event.touches[0]?.clientY;
			if (y == null) return;
			const last = lastTouchYRef.current;
			lastTouchYRef.current = y;
			if (last != null && y > last) {
				lastUserScrollDirectionRef.current = "up";
				stopFollowingBottom();
			} else if (last != null && y < last) {
				lastUserScrollDirectionRef.current = "down";
			}
		},
		[stopFollowingBottom],
	);
	const onPointerDown = useCallback((event: PointerEvent) => {
		if (event.button !== 0) return;
		pointerScrollingRef.current = true;
		lastScrollTopRef.current = scrollerElementRef.current?.scrollTop ?? 0;
	}, []);
	const onPointerEnd = useCallback(() => {
		pointerScrollingRef.current = false;
	}, []);
	const settleScroll = useCallback(() => {
		if (scrollSettleTimerRef.current !== null) {
			window.clearTimeout(scrollSettleTimerRef.current);
			scrollSettleTimerRef.current = null;
		}
		captureState();
	}, [captureState]);
	const scheduleScrollSettle = useCallback(() => {
		if (scrollSettleTimerRef.current !== null) window.clearTimeout(scrollSettleTimerRef.current);
		scrollSettleTimerRef.current = window.setTimeout(settleScroll, SCROLL_SETTLE_DELAY_MS);
	}, [settleScroll]);
	const onScroll = useCallback(() => {
		const element = scrollerElementRef.current;
		if (element && pointerScrollingRef.current) {
			const scrollTop = element.scrollTop;
			if (scrollTop < lastScrollTopRef.current - 0.5) {
				lastUserScrollDirectionRef.current = "up";
				stopFollowingBottom();
			} else if (scrollTop > lastScrollTopRef.current + 0.5) {
				lastUserScrollDirectionRef.current = "down";
			}
			lastScrollTopRef.current = scrollTop;
		}
		scheduleScrollToBottomVisibilityUpdate();
		scheduleScrollSettle();
	}, [scheduleScrollSettle, scheduleScrollToBottomVisibilityUpdate, stopFollowingBottom]);

	const previousResetKeyRef = useRef<string | null | undefined>(resetKey);
	useEffect(() => {
		if (previousResetKeyRef.current === resetKey) return;
		previousResetKeyRef.current = resetKey;
		if (lerpAnimationFrameRef.current !== null) {
			cancelAnimationFrame(lerpAnimationFrameRef.current);
			lerpAnimationFrameRef.current = null;
		}
		browsingHistoryRef.current = !shouldInitiallyFollowBottom;
		lastUserScrollDirectionRef.current = null;
		setShouldFollowBottom(shouldInitiallyFollowBottom);
		skipNextLerpRef.current = true;
	}, [resetKey, setShouldFollowBottom, shouldInitiallyFollowBottom]);

	useEffect(() => {
		if (!initialTargetKey || !getItemKey || items.length === 0) return;
		const index = items.findIndex((item) => getItemKey(item) === initialTargetKey);
		onInitialTargetHandled?.();
		if (index < 0) {
			browsingHistoryRef.current = false;
			lastUserScrollDirectionRef.current = null;
			setShouldFollowBottom(true);
			requestAnimationFrame(() => {
				virtuosoRef.current?.scrollToIndex({ index: "LAST", align: "end", behavior: "auto" });
			});
			return;
		}
		setShouldFollowBottom(false);
		skipNextLerpRef.current = true;
		requestAnimationFrame(() => {
			virtuosoRef.current?.scrollToIndex({ index, align: "center", behavior: "smooth" });
		});
	}, [getItemKey, initialTargetKey, items, onInitialTargetHandled, setShouldFollowBottom]);

	useEffect(() => {
		void items;
		void active;
		if (skipNextLerpRef.current) {
			skipNextLerpRef.current = false;
			return;
		}
		if (shouldFollowBottomRef.current) startFollowingBottom();
	}, [active, items, startFollowingBottom]);

	const previousItemCountRef = useRef(items.length);
	useLayoutEffect(() => {
		const previousCount = previousItemCountRef.current;
		previousItemCountRef.current = items.length;
		const appendedItem = items.at(-1);
		if (
			items.length > previousCount &&
			appendedItem !== undefined &&
			(shouldFollowOnAppend?.(appendedItem) ?? false)
		) {
			browsingHistoryRef.current = false;
			lastUserScrollDirectionRef.current = null;
			setShouldFollowBottom(true);
		}
	}, [items, setShouldFollowBottom, shouldFollowOnAppend]);

	const scrollerRef = useCallback((element: HTMLElement | Window | null) => {
		const next = element instanceof HTMLElement ? element : null;
		scrollerElementRef.current = next;
		lastScrollTopRef.current = next?.scrollTop ?? 0;
		setScrollerElement(next);
		if (next) next.style.overflowAnchor = "none";
	}, []);

	const snapToBottom = useCallback(() => {
		snapAnimationFrameRef.current = null;
		const element = scrollerElementRef.current;
		if (!element || !shouldFollowBottomRef.current) return;
		const target = Math.max(0, element.scrollHeight - element.clientHeight);
		if (Math.abs(target - element.scrollTop) > 0.5) element.scrollTop = target;
	}, []);

	const scheduleSnapToBottom = useCallback(() => {
		if (layoutResizingRef.current || !shouldFollowBottomRef.current || snapAnimationFrameRef.current !== null) {
			return;
		}
		snapAnimationFrameRef.current = requestAnimationFrame(snapToBottom);
	}, [snapToBottom]);

	// 流式期间内容按行增长:直接跳到底会让整屏文字一格一格往上蹦,改为滑过去(超过一屏仍直接贴底,见 tickLerp)。
	const onTotalListHeightChange = useCallback(
		(_height: number) => {
			updateScrollToBottomVisibility();
			// 此刻 scrollHeight 可能还没更新:距离留到下一帧的滑动里再量。
			if (activeRef.current && shouldFollowBottomRef.current && !layoutResizingRef.current) {
				startFollowingBottom();
				return;
			}
			scheduleSnapToBottom();
		},
		[scheduleSnapToBottom, startFollowingBottom, updateScrollToBottomVisibility],
	);

	useEffect(() => {
		const wasResizing = previousLayoutResizingRef.current;
		previousLayoutResizingRef.current = layoutResizing;
		if (!wasResizing || layoutResizing) return;
		const animationFrame = requestAnimationFrame(snapToBottom);
		return () => cancelAnimationFrame(animationFrame);
	}, [layoutResizing, snapToBottom]);

	useEffect(() => {
		const element = scrollerElement;
		if (!element) return;
		element.addEventListener("wheel", onWheel, { passive: true });
		element.addEventListener("touchstart", onTouchStart, { passive: true });
		element.addEventListener("touchmove", onTouchMove, { passive: true });
		element.addEventListener("pointerdown", onPointerDown, { passive: true });
		element.addEventListener("pointerup", onPointerEnd, { passive: true });
		element.addEventListener("pointercancel", onPointerEnd, { passive: true });
		element.addEventListener("scroll", onScroll, { passive: true });
		element.addEventListener("scrollend", settleScroll, { passive: true });
		const resizeObserver = new ResizeObserver(() => {
			updateScrollToBottomVisibility();
			scheduleSnapToBottom();
		});
		resizeObserver.observe(element);
		return () => {
			element.removeEventListener("wheel", onWheel);
			element.removeEventListener("touchstart", onTouchStart);
			element.removeEventListener("touchmove", onTouchMove);
			element.removeEventListener("pointerdown", onPointerDown);
			element.removeEventListener("pointerup", onPointerEnd);
			element.removeEventListener("pointercancel", onPointerEnd);
			element.removeEventListener("scroll", onScroll);
			element.removeEventListener("scrollend", settleScroll);
			resizeObserver.disconnect();
			if (scrollSettleTimerRef.current !== null) {
				window.clearTimeout(scrollSettleTimerRef.current);
				scrollSettleTimerRef.current = null;
			}
			captureState();
			if (snapAnimationFrameRef.current !== null) {
				cancelAnimationFrame(snapAnimationFrameRef.current);
				snapAnimationFrameRef.current = null;
			}
		};
	}, [
		captureState,
		onPointerDown,
		onPointerEnd,
		onScroll,
		onTouchMove,
		onTouchStart,
		onWheel,
		scheduleSnapToBottom,
		scrollerElement,
		settleScroll,
		updateScrollToBottomVisibility,
	]);

	useEffect(
		() => () => {
			if (lerpAnimationFrameRef.current !== null) cancelAnimationFrame(lerpAnimationFrameRef.current);
			if (snapAnimationFrameRef.current !== null) cancelAnimationFrame(snapAnimationFrameRef.current);
			if (visibilityAnimationFrameRef.current !== null) {
				cancelAnimationFrame(visibilityAnimationFrameRef.current);
			}
			if (scrollSettleTimerRef.current !== null) window.clearTimeout(scrollSettleTimerRef.current);
			captureState();
		},
		[captureState],
	);

	// Virtuoso 的 followOutput 只短暂观察内容增长。总高度事件覆盖延迟挂载、
	// Markdown 与工具结果的多轮重测;列表渲染窗口保持固定,不在滚动过程中改布局参数。
	// 流式期间交给上面的滑动跟随:Virtuoso 的 followOutput 会在增长时瞬间贴底,抵消滑动。
	return {
		followOutput: followOutputEnabled && !active ? "auto" : false,
		initialTopMostItemIndex,
		onAtBottomChange,
		onTotalListHeightChange,
		restoreStateFrom,
		scrollerElement,
		scrollerRef,
		scrollToBottom,
		scrollToItem,
		showScrollToBottom,
		virtuosoRef,
	};
}