Repository navigation
feat(python/agent): add optional DefaultCatalogSimplifier stage - #4551
jacobsimionato wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the DefaultCatalogSimplifier message transformer, which compacts redundant per-component and per-function catalog IDs on v1.0 surfaces to optimize wire payloads. It also includes comprehensive unit tests verifying various scenarios such as hoisting, multi-catalog preservation, and segment boundaries. A review comment suggests an improvement in the message processing loop to check lifecycle boundaries before protocol versions, ensuring segment boundaries are always respected even across different protocol versions, and recommends using a 'version at least' check for forward compatibility.
| for later_idx in range(idx + 1, len(msg_dicts)): | ||
| later = msg_dicts[later_idx] | ||
| later_ver = later.get("version") | ||
| if ( | ||
| not isinstance(later_ver, str) | ||
| or to_version_number(later_ver) != _V1_0_NUMBER | ||
| ): | ||
| continue | ||
| later_cs = later.get("createSurface") | ||
| if ( | ||
| isinstance(later_cs, dict) | ||
| and later_cs.get("surfaceId") == surface_id | ||
| ): | ||
| break | ||
| later_ds = later.get("deleteSurface") | ||
| if ( | ||
| isinstance(later_ds, dict) | ||
| and later_ds.get("surfaceId") == surface_id | ||
| ): | ||
| break | ||
| later_uc = later.get("updateComponents") | ||
| if ( | ||
| isinstance(later_uc, dict) | ||
| and later_uc.get("surfaceId") == surface_id | ||
| ): | ||
| uc_comps = later_uc.get("components") | ||
| if isinstance(uc_comps, list) and uc_comps: | ||
| segment_entries.append((later_idx, uc_comps)) |
There was a problem hiding this comment.
The current implementation checks the protocol version (later_ver) before checking for lifecycle boundaries (createSurface or deleteSurface). If a message in the batch deletes or recreates the same surface using a different protocol version (e.g., v0.9), the loop will skip it and fail to break. This can cause the simplifier to incorrectly group and compact updateComponents messages from a completely different lifecycle segment.
Checking the lifecycle boundaries first, regardless of the protocol version, ensures segment boundaries are always respected. Additionally, when checking the protocol version, use a 'version at least' check (e.g., < _V1_0_NUMBER to skip older versions) instead of strict inequality to ensure forward compatibility with future versions.
for later_idx in range(idx + 1, len(msg_dicts)):
later = msg_dicts[later_idx]
later_cs = later.get("createSurface")
if (
isinstance(later_cs, dict)
and later_cs.get("surfaceId") == surface_id
):
break
later_ds = later.get("deleteSurface")
if (
isinstance(later_ds, dict)
and later_ds.get("surfaceId") == surface_id
):
break
later_ver = later.get("version")
if (
not isinstance(later_ver, str)
or to_version_number(later_ver) < _V1_0_NUMBER
):
continue
later_uc = later.get("updateComponents")
if (
isinstance(later_uc, dict)
and later_uc.get("surfaceId") == surface_id
):
uc_comps = later_uc.get("components")
if isinstance(uc_comps, list) and uc_comps:
segment_entries.append((later_idx, uc_comps))References
- When checking protocol or specification versions for feature availability, use a 'version at least' check instead of strict equality to ensure forward compatibility with future versions.
6dac5c6 to
6e5d991
Compare
…tion stage Signed-off-by: Jacob Simionato <jsimionato@google.com>
6e5d991 to
33726e6
Compare
Problem
In v1.0, builders (typed Python component classes; the Dart
a2ui_clitool generates them) andMacroExpander(which expands macros, Python functions that become A2UI components server-side) write the full catalog URI onto every component and every catalog function call. That keeps expansion stateless and lets one surface mix catalogs, but a single-catalog surface repeats the same URI everywhere:{"createSurface": {"surfaceId": "main"}} // before {"updateComponents": {"surfaceId": "main", "components": [ {"id": "root", "component": "Card", "child": "t", "@catalogId": "https://a2ui.org/.../basic/catalog.json"}, {"id": "t", "component": "Text", "@catalogId": "https://a2ui.org/.../basic/catalog.json", "text": {"@call": "formatString", "catalogId": "https://a2ui.org/.../basic/catalog.json", "args": {...}}}]}} // After: {"createSurface": {"surfaceId": "main", "catalogId": "https://a2ui.org/.../basic/catalog.json"}}, no per-item copiesThis PR adds an opt-in pipeline stage that compacts those IDs on the way out, without putting catalog state back into
MacroExpander.Terms used below: a component's catalog annotation is
@catalogId; a function call's iscatalogId. A plaincatalogIdkey on a component is not an annotation (it may be a component property), so the stage treats it as ambiguous and leaves that component alone. System functions (names starting with@, such as@index) belong to no catalog and never carry acatalogId. A batch is the message list passed to onetransform_to_transportcall: one full response, or one chunk when streaming.Stack: 9/11 (overview) · previous: #4533 · next: #4534
Reviewer guide: A2UI Python macros: reviewer guide explains how the whole stack fits together.
Summary
DefaultCatalogSimplifier, a stateless v1.0MessageTransformer, exported from the existing publica2ui.catalog_transformerspackage. Use it in aTransformerPipelineor pass it viatransformers=toA2uiGenerator/A2uiRequestProcessor.createSurfacein a batch, the stage applies one rule:createSurfacealready has a non-emptycatalogId, remove annotations that match it. Other catalogs' annotations stay.catalogIdand every component and non-system call for that surface in the batch carries the same URI, setcreateSurface.catalogIdto it and strip.catalogId="",updateComponentswith nocreateSurfaceearlier in the batch, andcallRendererFunctionmessages.python/a2ui_agent/README.mdandpython/a2ui_agent/CHANGELOG.md.Breaking changes: None. The stage is new and only runs when added.
New public API
Where to review
Start here:
python/a2ui_agent/src/a2ui/catalog_transformers/simplifier.py: check which messages count as one surface's part of a batch (they stop at the nextcreateSurfaceordeleteSurfacefor the samesurfaceId), and that any doubt blocks hoisting instead of guessing.python/a2ui_agent/tests/test_default_catalog_simplifier.py: the edge cases. Three tests also feed the output through aMessageProcessorwith strict validation to confirm the client still accepts it.Implementation details
transform_to_transportisasync(theMessageTransformerbase is async-first). It converts messages withto_message_dicts, edits the dicts, and rebuilds withtype(orig).model_validate(...)only for messages it changed. Unchanged messages are returned as the original objects._is_v1_0_messagecomparesto_version_number(msg["version"])to v1.0, so other versions pass through._find_hoistable_catalog_idreturns a URI only if every component is a dict with no plaincatalogId, a non-empty@catalogId, and a non-emptycatalogIdon every non-system@callinside it, with exactly one distinct URI overall._strip_redundant_from_componentsremoves@catalogIdonly when it matches and no plaincatalogIdis present, then calls_strip_nested_function_catalog_ids.@metadataand legacymetadatakeys as opaque._collect_nested_function_catalog_idsstill walks legacymetadata(it may be a custom property holding a@call), so hoisting stays conservative.A2uiRequestProcessor.aopen_stream(), acreateSurfacechunk with no components is not hoisted. A single-catalog first chunk of a mixed surface may be hoisted while later chunks from other catalogs keep their explicit@catalogId. Both outcomes stay valid on the wire.Verification
Measured at the stack tip (all 11 commits applied); this PR's own commit was not run separately:
This PR adds
python/a2ui_agent/tests/test_default_catalog_simplifier.py(10 async tests, several covering more than one case). They cover hoisting with nested calls, multi-catalog and cross-catalog preservation, already-defaulted surfaces, two-turn and v0.9 passthrough,deleteSurface/recreate boundaries, system-function and metadata skipping, explicit empty and conflicting plaincatalogId, interleaved surfaces with input immutability, and anaopen_stream()run with the Express inference format (ExpressFormatFactory).