[Dev UI] refactor: migrate RenderedParts from imperative DOM nodes to declarative RenderBlock view-models
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:
- Full-subtree destruction on every streamed token (
vcr.clear()+replaceChildren):- In
RenderedParts, every change toparts()callsvcr.clear(), awaitsconvertPartsToDomNodes(), and callscontainer.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.
- In
- Two-pass
MutationObserverhydration (ReasoningDirective):ReasoningPartRendereremits a raw<aside class="reasoning preview">DOM element, whichReasoningDirectivethen observes via aMutationObserverand replaces with a dynamically created<reasoning>component.
- Unnecessary async
resource()&ViewContainerRefplumbing inRenderedParts:- Classification and normalization of
Part[]are synchronous data operations, yetRenderedPartsmust manage an asyncresource()lifecycle andabortSignalguards because DOM node creation and Markdown parsing are bundled together.
- Classification and normalization of
Proposed Solution
Decouple view-model resolution (Part[] -> RenderBlock[]) from DOM target rendering, similar to src/app/components/json-viewer/render-types:
- Pure Synchronous
resolveRenderBlocks(parts: Part[], format?: string): RenderBlock[]:- Update
PART_RENDERERSto resolve normalizedPart[](and foldedgroupSurfaces(normalizedParts)) into a discriminated union ofRenderBlockdescriptors with deterministic keys (${index}:${kind},a2ui:${surface.key}).
- Update
- Declarative
@for/@switchRendering inRenderedParts:- Replace
renderingResource,vcr.clear(), andreplaceChildren()inRenderedPartswith a synchronouscomputed()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 inrendered-parts.component.htmlusing@for (block of blocks(); track block.key)and@switch (block.kind). - Remove
ReasoningDirective([formatReasoning]) once<reasoning>is rendered directly in the template.
- Replace
- Scoped
contenteditableDOM Serialization forMessage(RAWMode):- Keep
convertPartsToDomNodes(consumingresolveRenderBlocks) scoped toMessage's#multiModalInput(contenteditable) so inline[text][media][text2]wrapping behavior is preserved.
- Keep
Benefits
- Stateful Streaming UX: Stable
track block.keybindings 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(), andReasoningDirective'sMutationObserverinRenderedParts. - Pure View-Model Testing:
resolveRenderBlockscan be unit-tested as pure synchronous data in/out (Part[] -> RenderBlock[]) withoutTestBed, DOM mocks, orViewContainerRefstubs.
Source: genkit-ai/genkit