Skip to content

feat(ts_agent): add ComponentBuilderNode and flattenComponentTree - #4540

Open
manfredsteyer wants to merge 2 commits into
a2ui-project:mainfrom
manfredsteyer:feat/ts-agent-component-tree
Open

manfredsteyer wants to merge 2 commits into
a2ui-project:mainfrom
manfredsteyer:feat/ts-agent-component-tree

Conversation

@manfredsteyer

Copy link
Copy Markdown

Problem

@a2ui/agent has no way to build a component tree and flatten it into the component list of updateComponents. Macros need one (#3455), and Python has it in its builder (ComponentBuilderNode, ComponentRef and flatten_component_tree). Also, CatalogSchemaHelper and the component envelope helpers live in src/inference-formats/express/, although macros need them too.

Changes

  1. Move src/inference-formats/express/schema-helper.ts and its test to src/utils/ without changing them, and update the imports and the codebase blueprint. This is a commit of its own.

  2. Add ComponentBuilderNode and ComponentRef, following Python's classes, and flattenComponentTree:

    const tree = new ComponentBuilderNode({
      component: 'Card',
      child: new ComponentBuilderNode({component: 'Text', text: 'Hello'}),
    });
    
    flattenComponentTree(tree, {rootId: 'greeting'});
    // [{component: 'Text', id: 'greeting__text_1', text: 'Hello'},
    //  {component: 'Card', id: 'greeting', child: 'greeting__text_1'}]
  3. Only instances of ComponentBuilderNode count as nodes, so a plain object with a component property, such as a value in an action's context, stays a value. A ComponentRef becomes the ID it refers to and is never emitted or namespaced.

  4. IDs follow Python: a descendant without an id gets <root>__<component>_<n>, one with an id gets <root>__<id>, and a node used in two places is emitted once. Two components with the same explicit id throw A2uiValidationError, where Python emits both under that ID.

Following the review on #4413, the README, CHANGELOG and KNOWN_GAPS.md entries come together with the macros.

Verification

  1. tests/unit/component-tree/flatten.test.ts covers the ID allocation, ComponentRef, shared and cyclic nodes, duplicate IDs, and the plain object with a component property from the review on feat(ts_agent): port macros (MacroExpander) to @a2ui/agent #4413.
  2. yarn workspace @a2ui/agent test, lint and format:check pass. typecheck reports the same 20 errors as main, all in test files this PR doesn't touch.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new component tree flattening utility under src/component-tree/ (comprising ComponentBuilderNode, ComponentRef, flattenComponentTree, and IdAllocator) to flatten nested component structures into a flat list of components with deterministic ID allocation and namespacing. Additionally, schema-helper.ts has been relocated from src/inference-formats/express/ to src/utils/, with all corresponding imports and tests updated. Comprehensive unit tests have been added for the new flattening utility. I have no feedback to provide as there are no review comments.

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Dec 2, 2026
@polina-c

polina-c commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

From triage bot, analysis:

For humans

TL;DR
Case: ditman already started reviewing this change in #4413 and asked for this split. This PR is its first part.
Suggestion: assign polina-c, who owns the review.

Summary:
First of the three PRs that split #4413, which ports macros to @a2ui/agent. It moves schema-helper.ts to src/utils/ and adds ComponentBuilderNode, ComponentRef and flattenComponentTree, which build a component tree and flatten it into the component list of updateComponents.

Why flagged for triage:
This PR has no assignee.

Suggestion:

  • ditman reviewed #4413 and asked to split it into three PRs, which #4540, #4541 and #4542 follow. ditman said that polina-c owns the review after that first round, so assign polina-c to all three.
  • No merge conflicts. A gemini-code-assist comment on #4541 flags possible duplicate IDs in flatten.ts, which this PR adds.
  1. Add labels: type: feature/enhancement, component: agent sdk, lang: typescript
  2. Assign: polina-c
  3. Related items: feat(ts_agent): add macros #4541, test(ts_agent): run the macros conformance suite and document macros #4542, feat(ts_agent): port macros (MacroExpander) to @a2ui/agent #4413

Reply:

From triage:

Assigning this PR to polina-c, who owns the review that ditman started on #4413.

To adjust the analysis and apply its suggestion, open it in the hosted triage UI, or run in your a2ui checkout:

tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4540
For software
<a2ui>
root = TriageAnalysis(
  tldr="""Case: `ditman` already started reviewing this change in [#4413](https://github.com/a2ui-project/a2ui/pull/4413) and asked for this split. This PR is its first part.
Suggestion: assign `polina-c`, who owns the review.""",
  summary="""First of the three PRs that split [#4413](https://github.com/a2ui-project/a2ui/pull/4413), which ports macros to `@a2ui/agent`. It moves `schema-helper.ts` to `src/utils/` and adds `ComponentBuilderNode`, `ComponentRef` and `flattenComponentTree`, which build a component tree and flatten it into the component list of `updateComponents`.""",
  reason="This PR has no assignee.",
  suggestion="""- `ditman` [reviewed](https://github.com/a2ui-project/a2ui/pull/4413#pullrequestreview-6141583583) [#4413](https://github.com/a2ui-project/a2ui/pull/4413) and [asked](https://github.com/a2ui-project/a2ui/pull/4413#pullrequestreview-6141583583) to split it into three PRs, which [#4540](https://github.com/a2ui-project/a2ui/pull/4540), [#4541](https://github.com/a2ui-project/a2ui/pull/4541) and [#4542](https://github.com/a2ui-project/a2ui/pull/4542) follow. `ditman` [said](https://github.com/a2ui-project/a2ui/pull/4413#issuecomment-7320083978) that `polina-c` owns the review after that first round, so assign `polina-c` to all three.
- No merge conflicts. A `gemini-code-assist` [comment](https://github.com/a2ui-project/a2ui/pull/4541#discussion_r4773729587) on [#4541](https://github.com/a2ui-project/a2ui/pull/4541) flags possible duplicate IDs in `flatten.ts`, which this PR adds.""",
  reply="""Assigning this PR to `polina-c`, who owns the review that `ditman` started on [#4413](https://github.com/a2ui-project/a2ui/pull/4413).""",
  added_labels=["type: feature/enhancement", "component: agent sdk", "lang: typescript"],
  new_assignees=["polina-c"],
  related_items=[4541, 4542, 4413]
)
</a2ui>

This analysis does not change after taking the later submitted comments into account: the force-push of the PR branch, which puts into 288010b6b the fix for the duplicate IDs that manfredsteyer describes on #4541.

This analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision.

@Varun-S10

Copy link
Copy Markdown
Collaborator

Hi @manfredsteyer, thank you for raising this PR. We verified the code changes and they work well, but the branch is behind main and has a merge conflict in schema-helper.ts from the recent JSDoc lint update. Could you please rebase or sync your branch with latest main to resolve the conflict so CI and the team can review it?

@Varun-S10 Varun-S10 added status: waiting-for-author-response and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Dec 3, 2026
CatalogSchemaHelper and the component envelope helpers are not specific
to Express: macros need them too. Move schema-helper.ts and its test from
inference-formats/express/ to utils/ without changing them.
Port the component tree of the Python builder: ComponentBuilderNode and
ComponentRef, and flattenComponentTree, which turns a tree of nodes into
the flat component list of updateComponents with deterministic IDs
namespaced under the root.

Nodes are recognized by class, so a plain data object with a component
property stays a value. Two components with the same explicit ID throw
A2uiValidationError instead of producing duplicate IDs.

An array of roots flattened without a rootId keeps each root's own ID
and gives a root without one the ID root_<index>. Python flattens all of
those roots under the prefix root, so their IDs collide.
@manfredsteyer
manfredsteyer force-pushed the feat/ts-agent-component-tree branch from 288010b to ad7d1ab Compare December 3, 2026 12:28
@manfredsteyer

Copy link
Copy Markdown
Author

Thanks, @Varun-S10. I rebased #4540, #4541 and #4542 onto main, and src/utils/schema-helper.ts now carries the JSDoc changes from #4270.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants