Repository navigation
test(ts_agent): run the macros conformance suite and document macros - #4542
manfredsteyer wants to merge 4 commits into
Conversation
0d48504 to
f6871b8
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces macro support (v1.0 only) to the TypeScript A2UI Agent SDK, enabling the definition and expansion of high-level components into base catalog components. It adds a component tree builder and flattener, a MacroExpander for lowering macro invocations, and relocates CatalogSchemaHelper to utility schemas. Additionally, comprehensive unit and conformance tests are added, and documentation is updated. Feedback on the changes suggests using optional chaining and nullish coalescing when querying schemaHelpers to prevent potential TypeErrors, and replacing ES2022/ES2023 array methods (findLast and at) with ES6-compatible alternatives to ensure broader environment compatibility.
| owners.length > 0 && | ||
| owners.every(id => | ||
| this.schemaHelpers.get(id)?.getCustomEnvelopeProperties(name).has('catalogId'), |
There was a problem hiding this comment.
To prevent potential runtime errors, use optional chaining and a nullish coalescing fallback when accessing has on the result of getCustomEnvelopeProperties. If schemaHelpers.get(id) is undefined or if getCustomEnvelopeProperties returns undefined, calling .has directly will throw a TypeError. This aligns with defensive programming best practices.
owners.every(id =>
this.schemaHelpers.get(id)?.getCustomEnvelopeProperties(name)?.has('catalogId') ?? false,
);References
- Avoid using the non-null assertion operator on lookup results that may fail. Instead, allow the variable to be nullable and handle the undefined case downstream using optional chaining or nullish coalescing.
There was a problem hiding this comment.
Same as on #4541: getCustomEnvelopeProperties always returns a set, and ?. already covers a missing helper. Leaving it as is.
| const root = | ||
| (instanceId !== undefined | ||
| ? lowered.findLast(component => component['id'] === instanceId) | ||
| : undefined) ?? lowered.at(-1); |
There was a problem hiding this comment.
Using ES2022/ES2023 features like Array.prototype.findLast and Array.prototype.at can cause runtime crashes in environments that do not support or polyfill these newer methods. Using standard ES6 methods like reverse().find() and direct index access [length - 1] is safer and more compatible.
| const root = | |
| (instanceId !== undefined | |
| ? lowered.findLast(component => component['id'] === instanceId) | |
| : undefined) ?? lowered.at(-1); | |
| const root = | |
| (instanceId !== undefined | |
| ? [...lowered].reverse().find(component => component['id'] === instanceId) | |
| : undefined) ?? lowered[lowered.length - 1]; |
There was a problem hiding this comment.
The package compiles against lib: ["es2023"] (tsconfig.json), so findLast and at are part of the runtime it targets. Node has both since version 18, CI runs Node 22, and @a2ui/catalog-mcp already uses at(-1). Copying and reversing the array would only make this harder to read, so I'm keeping it.
|
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/4542For 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. |
f6871b8 to
a008179
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.
Run conformance/agent/macros/macros.yaml with reference macros that mirror Python's macro_suite.py: 48 of its 70 cases pass, and the 22 that need v0.9 macros are skipped. agent/builder/builder.yaml stays unsupported until TypeScript has a typed builder. Document macros and the component tree in the README, CHANGELOG, KNOWN_GAPS.md and the codebase blueprint.
|
Hi @manfredsteyer, thank you for raising this PR. We verified the macros conformance suite (48 of 70 cases pass and 22 v0.9 cases skip as expected) and docs, and they look great, but the branch is behind main with the merge conflict in schema-helper.ts and three @returns JSDoc tags carried from #4540 and #4541. Could you please rebase on latest main so we can review it again? |
a008179 to
bda5838
Compare
|
Thanks, @Varun-S10. This PR is rebased onto main together with #4540 and #4541, and the macros suite still passes 48 cases and skips 22. The commit to review is now bda5838. |
Problem
conformance/agent/macros/macros.yamldoesn't run against@a2ui/agent, and its macros and component tree aren't documented.Changes
macros.yamlwith reference macros that mirror Python'smacro_suite.py. 48 of its 70 cases pass, including16_reverse_passthroughfortransformToInference. The 22 cases that need v0.9 macros are skipped.agent/builder/builder.yamlinUNSUPPORTED_SUITESuntil TypeScript has a typed builder.KNOWN_GAPS.mdgets entries for v1.0-only macros, the missing typed builder and the checks that are stricter than Python's, and the codebase blueprint describes the component tree and the macros.Verification
yarn workspace @a2ui/agent test,lintandformat:checkpass.typecheckreports the same 20 errors asmain, all in test files this PR doesn't touch. The README examples compile against the package.