Repository navigation
feat(ts_agent): add macros - #4541
manfredsteyer wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a macro expansion and component tree flattening system for the TypeScript A2UI Agent SDK. It adds utilities to define high-level macro components, expand them into standard catalog components, and flatten component trees into flat lists with deterministic ID allocation. The feedback highlights two important improvements: addressing potential duplicate component IDs when flattening an array of roots without a specified root ID, and preventing a potential TypeError in the macro expander by using optional chaining when retrieving custom envelope properties.
| return root.flatMap((item, index) => | ||
| flattenComponentTree(item, {rootId: rootId ? `${rootId}_${index}` : undefined}), | ||
| ); |
There was a problem hiding this comment.
When root is an array and rootId is not provided, each root component will be flattened with rootId as undefined. This causes all roots to default to the same prefix (e.g., 'root'), leading to duplicate component IDs in the flattened list. To prevent ID collisions, a unique prefix should be provided for each root in the array even when rootId is omitted.
| return root.flatMap((item, index) => | |
| flattenComponentTree(item, {rootId: rootId ? `${rootId}_${index}` : undefined}), | |
| ); | |
| return root.flatMap((item, index) => | |
| flattenComponentTree(item, {rootId: rootId ? rootId + "_" + index : "root_" + index}), | |
| ); |
There was a problem hiding this comment.
Good catch. The collision is real, and Python's flatten_component_tree has it too. The suggested fallback would replace a root's own id with root_<index>, though, and break any reference to that root. So without a rootId, a root now keeps its own id, and only a root without one gets root_<index>. Two roots that still end up with the same ID throw A2uiValidationError, as two components with the same id in one tree already do.
The fix is in 288010b, which belongs to #4540, and KNOWN_GAPS notes the difference from Python.
| owners.every(id => | ||
| this.schemaHelpers.get(id)?.getCustomEnvelopeProperties(name).has('catalogId'), | ||
| ); |
There was a problem hiding this comment.
If this.schemaHelpers.get(id) is defined but getCustomEnvelopeProperties(name) returns undefined (e.g., if the component is not found in the catalog), calling .has('catalogId') on it will throw a TypeError. Using optional chaining and a nullish coalescing fallback ensures defensive programming and prevents potential runtime crashes.
| owners.every(id => | |
| this.schemaHelpers.get(id)?.getCustomEnvelopeProperties(name).has('catalogId'), | |
| ); | |
| owners.every(id => | |
| this.schemaHelpers.get(id)?.getCustomEnvelopeProperties(name)?.has('catalogId') ?? false, | |
| ); |
References
- In TypeScript, when handling partial objects that may not fully implement an interface, defensively default their properties (e.g., using nullish coalescing or logical OR) to prevent runtime TypeErrors.
There was a problem hiding this comment.
getCustomEnvelopeProperties never returns undefined: it's typed ReadonlySet<string> and falls back to an empty set (src/utils/schema-helper.ts). When get(id) is undefined, ?. short-circuits the rest of the chain, the callback returns undefined, and every treats that as false. There's no TypeError to guard against, so I'm leaving it as is.
|
From triage bot, analysis: For humansTL;DR Summary: Why flagged for triage: Suggestion:
Reply:
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/4541For softwareThis analysis does not change after taking the later submitted comments into account: review from This analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision. |
871efcd to
66f5b04
Compare
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.
Port the macros of the Python SDK to @a2ui/agent for v1.0, exported as the Macros namespace so that their value types, such as Action, don't clash with @a2ui/web_core. - defineMacro and the param vocabulary define a macro; the arguments of render are typed from its parameters, and component IDs arrive as ComponentRefs. - MacroExpander adds the macros to the inference catalogs and lowers the model's messages to the transport catalogs: it checks and coerces the arguments, merges the invocation's envelope fields onto the root, resolves macros on surfaces that mix catalogs, tracks surfaces and applies an onError policy. transformToInference passes messages through, as in Python.
|
Hi @manfredsteyer, thank you for raising this PR. We verified the macro implementation and tests and they work well, but the branch is behind main with a merge conflict in schema-helper.ts and three @returns JSDoc tags in coercion.ts and expander.ts that need to use @return for the new lint rule. Could you please rebase on latest main after #4540 and update those three @returns tags so we can review it again? |
66f5b04 to
8287dbe
Compare
|
Thanks, @Varun-S10. This PR is rebased onto main together with #4540, and the three |
Problem
Macros are only available in the Python SDK, so a TypeScript agent can't give the model high-level components that are lowered to catalog components before the messages reach the renderer (#3455).
Changes
This PR ports Python's
MacroExpanderto@a2ui/agentfor v1.0. The macro API is exported as theMacrosnamespace:defineMacroandparamdefine a macro.paramhas the parameter types of the shared macro conformance suite instead of Zod, and the arguments ofrenderare typed from it. Component IDs arrive asComponentRefs, as in Python.MacroExpanderfollows Python's API in camelCase:transformCatalogs,transformToInferenceCatalog,toCatalog,transformToTransportandtransformToInference. It checks and coerces the arguments, merges an invocation's@accessibility,@metadataand@checksonto the root, resolves macros on surfaces that mix catalogs, tracks surfaces and applies anonErrorpolicy.ActionandDynamicString, have the names of@a2ui/web_coretypes.ComponentBuilderNodeandComponentRefstay root exports, since generated builder classes will extend them outside macros.Actionhas onlyeventor onlyfunctionCall, a function call has only@call,argsandcatalogId, an argument set tonullcounts as left out, and two macros with the same name throw.Verification
tests/unit/macros/cover the parameters and their types, argument coercion, the envelope fields, catalog resolution, surface tracking and a round trip throughA2uiGenerator.yarn workspace @a2ui/agent test,lintandformat:checkpass.typecheckreports the same 20 errors asmain, all in test files this PR doesn't touch.