Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

7.7 KB
Raw Blame
/**
 * Follow-decision logic for auto-stick-to-bottom in the message stream (pure logic,
 * unit-testable; issue #75).
 *
 * "Exiting follow" and "resuming follow" are two independent judgments:
 * - Exit: any user intent to scroll up takes effect immediately — detected from the input event
 *   itself for wheel-up / touch-drag-down (even if position doesn't change, e.g. already at the
 *   top), and from a scrollTop regression for scrollbar-drag-up / keyboard. This doesn't rely on
 *   an "80px from bottom" threshold — otherwise a short scroll area with less than 80px of
 *   scrollable slack could never exit, and streaming updates would keep fighting the upward
 *   gesture back and forth.
 * - Resume: only resumes once the user's own scroll brings the viewport back near the bottom
 *   (within 80px). A scroll event that moved nothing changes nothing: a programmatic snap's own
 *   event arrives a frame later with the position the snap already reported, and the user may
 *   have started scrolling up in between, so reading it as "near the bottom" would undo that
 *   intent and let the next streaming update snap the view back down under the gesture. Content
 *   shrinking (e.g. a group collapsing) that clamps scrollTop downward while still touching the
 *   bottom (≤1px) doesn't count as scrolling up.
 * - The first scroll event has no direction to judge from, so it initializes from position
 *   (≥80px from bottom is treated as being at a historical position, i.e. not following) — this
 *   doesn't depend on the call-ordering guarantee of "must stick to bottom programmatically right
 *   after mount." Programmatic sticks therefore go through stickToBottom, which reports the
 *   landed position synchronously: the snap's own scroll event only arrives at the next
 *   rendering update, and content growing in between (an image decoding, a font swap) would
 *   otherwise make that first event look like a historical position and wrongly exit follow.
 * - Live reading: the browser can move the view before it dispatches the scroll event that
 *   reports the move (a key, the scrollbar, a find or outline jump), so a streaming snap first
 *   judges the live position the way that event will (`observed`, via `followToBottom`) and
 *   never undoes a move it has not been told about yet.
 * - Hold: something anchored to the content can hold the view still — the selection menu hangs
 *   off a point in the stream, and a snap would move that point out from under it. A hold only
 *   defers acting on the intent (`snaps`); it never changes the intent (`stick`), so the user's
 *   own scrolling still exits or resumes follow during a hold, and releasing it lets a view that
 *   was following catch up with whatever arrived meanwhile.
 */
export interface ScrollMetrics {
  scrollTop: number;
  scrollHeight: number;
  clientHeight: number;
}

export interface StreamFollow {
  /** Whether the user's intent is to follow: auto-stick to bottom on streaming updates. */
  readonly stick: boolean;
  /** Whether a streaming update may snap the view to the bottom right now: following, and not held. */
  readonly snaps: boolean;
  /** Hold the view where it is (true) or release it (false); see the header for what a hold leaves alone. */
  hold(held: boolean): void;
  /** wheel: deltaY < 0 is scroll-up intent, exits follow immediately. */
  wheel(deltaY: number): void;
  touchStart(clientY: number): void;
  /** Touch drag: finger moving down = content scrolling up, exits follow. */
  touchMove(clientY: number): void;
  touchEnd(): void;
  /** scroll event (user scrolling and programmatic stick-to-bottom share this path): moving up exits; moving down to near the bottom resumes; an event that moved nothing changes nothing; the first event initializes from position. */
  scrolled(m: ScrollMetrics): void;
  /**
   * A live reading of the position taken right before a snap, judged as its scroll event will
   * be. With no position reported yet there is nothing to judge it against, and it is ignored:
   * only a real scroll event or a snap may seed the model.
   */
  observed(m: ScrollMetrics): void;
  /** Explicit re-entry (the back-to-bottom button): resumes follow immediately — the caller scrolls to the bottom right after, and the resulting scroll event sees a bottom position, keeping it stuck. */
  resume(): void;
}

export function createStreamFollow(): StreamFollow {
  let stick = true;
  let held = false;
  let lastTop: number | null = null;
  let touchY: number | null = null;
  const scrolled = (m: ScrollMetrics) => {
    const dist = m.scrollHeight - m.scrollTop - m.clientHeight;
    const prev = lastTop;
    lastTop = m.scrollTop;
    if (prev === null) {
      stick = dist < 80;
      return;
    }
    if (m.scrollTop < prev && dist > 1) {
      stick = false;
      return;
    }
    if (dist < 80 && m.scrollTop !== prev) stick = true;
  };
  return {
    get stick() {
      return stick;
    },
    get snaps() {
      return stick && !held;
    },
    hold(next) {
      held = next;
    },
    wheel(deltaY) {
      if (deltaY < 0) stick = false;
    },
    touchStart(clientY) {
      touchY = clientY;
    },
    touchMove(clientY) {
      if (touchY !== null && clientY > touchY) stick = false;
      touchY = clientY;
    },
    touchEnd() {
      touchY = null;
    },
    scrolled,
    observed(m) {
      if (lastTop !== null) scrolled(m);
    },
    resume() {
      stick = true;
      // Forget the last position: the caller jumps to the bottom right after, and that large
      // downward scroll must not be judged against a stale historical scrollTop.
      lastTop = null;
    },
  };
}

/** Minimal mutable view of a scroll container (structurally satisfied by HTMLElement; fakeable in unit tests). */
export interface ScrollContainer {
  scrollTop: number;
  readonly scrollHeight: number;
  readonly clientHeight: number;
}

/**
 * Programmatic stick-to-bottom: snap the container to its bottom AND synchronously report the
 * landed position to the follow model. The report is the point (the fix for entering a
 * conversation occasionally not landing at the bottom): the browser dispatches the snap's
 * scroll event asynchronously at the next rendering update, and late content growth (an image
 * finishing decode, a font swap, a code block settling) can land in between. That event then
 * reads a live position ≥80px above the new bottom with an unchanged scrollTop, and — as the
 * first scrolled() call — the position initialization would misread it as the user parked at a
 * historical position and exit follow; every later re-snap is gated on `stick`, so the view
 * stayed off-bottom until manual scrolling. Reporting synchronously seeds the model with the
 * snap itself, so the delayed event goes down the ordinary no-regression path and follow
 * survives (growth with an unchanged scrollTop never exits).
 */
export function stickToBottom(el: ScrollContainer, follow: StreamFollow): void {
  el.scrollTop = el.scrollHeight; // The browser clamps to the real bottom; read back the clamped value below.
  follow.scrolled(metricsOf(el));
}

/**
 * The snap a following view takes when the transcript grows or resizes (a stream commit, a
 * resize, the selection menu letting go): the live position is judged first (`observed`), so a
 * move the browser has made but not reported yet exits follow instead of being undone, and the
 * view snaps only if it still follows after that.
 */
export function followToBottom(el: ScrollContainer, follow: StreamFollow): void {
  if (!follow.snaps) return;
  follow.observed(metricsOf(el));
  if (follow.snaps) stickToBottom(el, follow);
}

function metricsOf(el: ScrollContainer): ScrollMetrics {
  return { scrollTop: el.scrollTop, scrollHeight: el.scrollHeight, clientHeight: el.clientHeight };
}