Skip to content

Auto-scroll doesn't trigger for tool calls and large custom elements #2282

Closed

Description

@cixzhang

Problem

Auto-scroll works reliably when markdown is streaming but fails when tool calls, tool results, or large custom elements (widgets, embedded cards) appear in the chat layout.

Expected Behavior

The chat should auto-scroll to keep the latest content visible regardless of whether the new content is streaming text or a discrete block addition (tool call, widget, etc.).

Actual Behavior

  • ✅ Streaming markdown: auto-scroll follows (content grows character-by-character)
  • ❌ Tool calls appearing: no auto-scroll
  • ❌ Large custom elements / widgets: no auto-scroll
  • ❌ Tool results expanding: no auto-scroll

Root Cause Analysis

The auto-scroll system uses a ResizeObserver on the content wrapper div with a requestAnimationFrame debounce:

function onResize() {
  if (rafId != null) return;  // ← Deduplicates: only one pending rAF at a time
  rafId = requestAnimationFrame(() => {
    rafId = null;
    performScroll();
  });
}

const observer = new ResizeObserver(onResize);
observer.observe(contentEl);

Why this works for streaming text:
Each text delta causes continuous DOM growth → ResizeObserver fires repeatedly → auto-scroll keeps up.

Why this fails for discrete block additions:

  1. rAF deduplication drops resize events — if a resize fires while a previous rAF is already queued, the new one is silently ignored.

  2. Batched state updates — multiple events applied in a single setState() create DOM elements in one React commit. The height change happens atomically rather than incrementally.

  3. Fixed-height containers — tool piles and embedded cards use fixed dimensions / overflow: hidden, so child content changes don't propagate upward as continuous resizes.

  4. overflow: hidden on column wrappers — each rendered block is wrapped in a div with overflow-hidden, preventing child content from influencing parent layout measurements beyond the initial render.

Suggested Fix

Supplement the ResizeObserver with one of:

  1. MutationObserver on the content wrapper watching childList changes — trigger performScroll() after a microtask when children are added.

  2. Effect-based scroll on block count — track total block count and trigger scroll when it increases.

  3. Explicit scroll trigger in the event processor — after state updates for tool_call/tool_result events, schedule a scroll via queueMicrotask.

  4. Remove the rAF deduplication guard — or use a time-based throttle instead so rapid resizes still eventually scroll.

Reproduction

  1. Open any active chat session
  2. Send a message that triggers multiple tool calls
  3. Observe: the chat does not auto-scroll to show tool call blocks as they appear
  4. Compare: send a message that produces a long markdown response — auto-scroll works fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions