Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

5.7 KB
Raw Blame
import { transcriptHasLayout } from "./transcript-scroll";

export type TranscriptReadingPosition = {
  pinned: boolean;
  messageId?: string;
  offset: number;
  scrollTop: number;
  windowSize: number;
};
export type TranscriptDisclosureChoice = { open: boolean; revealRequest?: number };
type ReadingState = {
  position?: TranscriptReadingPosition;
  disclosures: Map<string, TranscriptDisclosureChoice>;
  delegationDisclosures?: Map<string, Map<string, TranscriptDisclosureChoice>>;
};

// Renderer-lifetime bookmarks, independent of the bounded DOM and message caches.
// No messages, elements, subscriptions, or persisted user data are retained here.
const readingStates = new Map<string, ReadingState>();
export function sessionReadingState(sessionId: string): ReadingState {
  let state = readingStates.get(sessionId);
  if (!state) {
    state = { disclosures: new Map() };
    readingStates.set(sessionId, state);
  }
  return state;
}

export function transcriptDisclosureChoices(sessionId: string, delegationId?: string) {
  const state = sessionReadingState(sessionId);
  if (delegationId === undefined) return state.disclosures;
  const tabs = state.delegationDisclosures ??= new Map();
  let choices = tabs.get(delegationId);
  if (!choices) {
    choices = new Map();
    tabs.set(delegationId, choices);
  }
  return choices;
}
export function getTranscriptReadingPosition(sessionId: string | undefined) {
  return sessionId ? readingStates.get(sessionId)?.position : undefined;
}

export type TranscriptRevealOwnership = "reading-position" | "pinned" | "retained";

/**
 * Which effect owns a pane reveal. A recorded unpinned bookmark wins over a
 * stale follow flag: follow mode can still read `true` when a send or retry hid
 * the pane, and bottoming there would undo the position the reader is meant to
 * return to (issue #1771).
 */
export function transcriptRevealOwnership(
  bookmark: TranscriptReadingPosition | undefined,
  pending: boolean,
  pinned: boolean,
): TranscriptRevealOwnership {
  if (bookmark?.pinned === false || pending) return "reading-position";
  return pinned ? "pinned" : "retained";
}
export function forgetTranscriptReadingState(sessionId: string) {
  readingStates.delete(sessionId);
}

/** Sample only a visible layout; hidden panes must never replace a bookmark. */
export function captureTranscriptReadingPosition(
  scroller: HTMLElement,
  content: HTMLElement,
  pinned: boolean,
  windowSize: number,
): TranscriptReadingPosition | undefined {
  if (!transcriptHasLayout(scroller)) return;
  if (pinned) return { pinned, offset: 0, scrollTop: scroller.scrollTop, windowSize };
  const viewport = scroller.getBoundingClientRect();
  // Answer groups can acquire an earlier first message when a page is prepended.
  // Anchor the visible message itself, whose identity and local offset survive
  // regrouping; use a turn only for visible content without a message node.
  for (const selector of ["[data-message-id]", "[data-minimap-id]"]) {
    for (const row of content.querySelectorAll<HTMLElement>(selector)) {
      const rect = row.getBoundingClientRect();
      if (rect.bottom <= viewport.top || rect.top >= viewport.bottom || rect.height === 0) continue;
      const messageId = row.dataset.messageId ?? row.dataset.minimapId;
      if (messageId) return { pinned, messageId, offset: rect.top - viewport.top,
        scrollTop: scroller.scrollTop, windowSize };
    }
  }
}

function readingPositionRow(content: HTMLElement, messageId: string): HTMLElement | null {
  const id = CSS.escape(messageId);
  return content.querySelector<HTMLElement>(`[data-message-id="${id}"]`)
    ?? content.querySelector<HTMLElement>(`[data-minimap-id="${id}"]`);
}

/** Test the intended reading offset, before compensating a changed layout. */
export function isTranscriptReadingAnchorVisible(
  scroller: HTMLElement,
  content: HTMLElement,
  position: TranscriptReadingPosition,
): boolean {
  if (!transcriptHasLayout(scroller) || !position.messageId) return false;
  const height = readingPositionRow(content, position.messageId)?.getBoundingClientRect().height ?? 0;
  return height > 0 && position.offset < scroller.clientHeight && position.offset + height > 0;
}

/** Returns false until the bookmarked row is actually mounted. */
export function restoreTranscriptReadingPosition(
  scroller: HTMLElement,
  content: HTMLElement,
  position: TranscriptReadingPosition,
): boolean {
  if (!transcriptHasLayout(scroller)) return false;
  if (position.messageId) {
    const row = readingPositionRow(content, position.messageId);
    if (row) {
      const rect = row.getBoundingClientRect();
      if (rect.height > 0) {
        const delta = rect.top - scroller.getBoundingClientRect().top - position.offset;
        // Even assigning the same scrollTop interrupts native smooth travel.
        // Ignore subpixel rounding instead of repeatedly cancelling that input.
        if (Math.abs(delta) > 0.5) scroller.scrollTop += delta;
        return true;
      }
    }
  }
  // The anchored row is not mounted. A reveal mounts only a trailing slice of
  // history (lib/transcript-window.ts:23-28), so an older bookmark can point
  // at a row that is not in the DOM yet. The restore hook waits for the history
  // lookup to finish before applying this fallback; otherwise its alignment
  // deadline could abort a slow lookup.
  //
  // The bookmark already recorded the absolute offset it was taken at, so fall
  // back to it for an approximate position. `false` is reserved for a bookmark
  // with no usable offset at all, which only a never-laid-out pane produces.
  if (!Number.isFinite(position.scrollTop) || position.scrollTop < 0) return false;
  if (Math.abs(scroller.scrollTop - position.scrollTop) > 0.5) scroller.scrollTop = position.scrollTop;
  return true;
}