#6395·genkit

[Dev UI] refactor: migrate RenderedParts from imperative DOM nodes to declarative RenderBlock view-models

Author: MichaelDoyleCreated Sep 21, 2026Updated Sep 21, 2026
Labelsdevui

Problem

Following #6279 (normalizeParts and modular PART_RENDERERS), MessageRenderingService.convertPartsToDomNodes is cleanly decomposed into per-part strategies, but still couples both rendering modes (MessageRenderingMode.RAW and MessageRenderingMode.FORMATTED) to an imperative Promise<Node[]> return type.

While Message (RAW edit mode) genuinely needs raw DOM Node[] inside a single contenteditable="true" container so inline sequences like [text][media/resource][text2] can wrap across lines, forcing RenderedParts (FORMATTED read-only view) through Promise<Node[]> causes several problems:

  1. Full-subtree destruction on every streamed token (vcr.clear() + replaceChildren):
    • In RenderedParts, every change to parts() calls vcr.clear(), awaits convertPartsToDomNodes(), and calls container.nativeElement.replaceChildren(...result.nodes).
    • When a model streams tokens into a trailing text part, all preceding Angular components (<reasoning>, <a2ui-surface-card>, <code-block>, <data-renderer>, <tool-request>) are destroyed and recreated from scratch on every chunk—resetting expanded/collapsed panel state, scroll positions, and re-running syntax highlighting.
  2. Two-pass MutationObserver hydration (ReasoningDirective):
    • ReasoningPartRenderer emits a raw <aside class="reasoning preview"> DOM element, which ReasoningDirective then observes via a MutationObserver and replaces with a dynamically created <reasoning> component.
  3. Unnecessary async resource() & ViewContainerRef plumbing in RenderedParts:
    • Classification and normalization of Part[] are synchronous data operations, yet RenderedParts must manage an async resource() lifecycle and abortSignal guards because DOM node creation and Markdown parsing are bundled together.

Proposed Solution

Decouple view-model resolution (Part[] -> RenderBlock[]) from DOM target rendering, similar to src/app/components/json-viewer/render-types:

  1. Pure Synchronous resolveRenderBlocks(parts: Part[], format?: string): RenderBlock[]:
    • Update PART_RENDERERS to resolve normalized Part[] (and folded groupSurfaces(normalizedParts)) into a discriminated union of RenderBlock descriptors with deterministic keys (${index}:${kind}, a2ui:${surface.key}).
  2. Declarative @for / @switch Rendering in RenderedParts:
    • Replace renderingResource, vcr.clear(), and replaceChildren() in RenderedParts with a synchronous computed() signal (blocks = computed(() => this.messageRenderingService.resolveRenderBlocks(this.parts(), this.format()))).
    • Render Angular components (<reasoning>, <a2ui-surface-card>, <tool-request>, <code-block>, <data-renderer>, and a dedicated <markdown-part> component) declaratively in rendered-parts.component.html using @for (block of blocks(); track block.key) and @switch (block.kind).
    • Remove ReasoningDirective ([formatReasoning]) once <reasoning> is rendered directly in the template.
  3. Scoped contenteditable DOM Serialization for Message (RAW Mode):
    • Keep convertPartsToDomNodes (consuming resolveRenderBlocks) scoped to Message's #multiModalInput (contenteditable) so inline [text][media][text2] wrapping behavior is preserved.

Benefits

  • Stateful Streaming UX: Stable track block.key bindings allow Angular to keep <reasoning>, <a2ui-surface-card>, and <code-block> instances mounted while subsequent parts stream in, preserving user interaction state and avoiding redundant syntax highlighting.
  • Simpler Component Lifecycle: Eliminates vcr.clear(), replaceChildren(), and ReasoningDirective's MutationObserver in RenderedParts.
  • Pure View-Model Testing: resolveRenderBlocks can be unit-tested as pure synchronous data in/out (Part[] -> RenderBlock[]) without TestBed, DOM mocks, or ViewContainerRef stubs.