Repository navigation
feat(ts_agent): port macros (MacroExpander) to @a2ui/agent #4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
manfredsteyer
wants to merge
9
commits into
a2ui-project:main
from
manfredsteyer:feat/ts-agent-macros
+4,303
−6
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b7a8c71
feat(ts_agent): add component tree flattener for macros
manfredsteyer 9526e49
feat(ts_agent): add macro definitions and macro catalogs
manfredsteyer 1e3ec27
feat(ts_agent): lower macro invocations in transport messages
manfredsteyer 2e217e4
feat(ts_agent): coerce v1.0 common types in macro arguments
manfredsteyer 475e523
feat(ts_agent): handle envelope fields and errors when lowering macros
manfredsteyer 6764fa9
feat(ts_agent): resolve macros across mixable v1.0 catalogs
manfredsteyer 9484be8
feat(ts_agent): track surfaces and accept per-call surface catalogs
manfredsteyer 2e307d2
docs(ts_agent): document macros and the component tree
manfredsteyer 3f487a4
fix(ts_agent): tighten macro checks after review
manfredsteyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
typescript/agents/a2ui_agent/src/component-tree/component-node.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * A component written as a nested object, in the shape of the wire component. | ||
| * | ||
| * Any property may hold another node, directly, in an array, or inside a plain object | ||
| * such as `{path, componentId}`. `flattenComponentTree` replaces each nested node with | ||
| * its ID. Every object with a string `component` property counts as a node. | ||
| */ | ||
| export interface ComponentNode { | ||
| component: string; | ||
| /** Leave it out to have an ID allocated. */ | ||
| id?: string; | ||
| [property: string]: unknown; | ||
| } | ||
|
|
||
| /** A component in a slot: a nested node, or the ID of a component already on the surface. */ | ||
| export type ComponentChild = ComponentNode | string; | ||
|
|
||
| /** A component of the flat list `flattenComponentTree` returns. */ | ||
| export type FlatComponent = ComponentNode & {id: string}; | ||
|
|
||
| /** Whether `value` is a component node rather than a literal value. */ | ||
| export function isComponentNode(value: unknown): value is ComponentNode { | ||
| return ( | ||
| typeof value === 'object' && | ||
| value !== null && | ||
| !Array.isArray(value) && | ||
| typeof (value as Record<string, unknown>)['component'] === 'string' | ||
| ); | ||
| } | ||
141 changes: 141 additions & 0 deletions
141
typescript/agents/a2ui_agent/src/component-tree/flatten.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {ComponentChild, ComponentNode, FlatComponent, isComponentNode} from './component-node.js'; | ||
| import {IdAllocator} from './id-allocator.js'; | ||
|
|
||
| /** Options for `flattenComponentTree`. */ | ||
| export interface FlattenComponentTreeOptions { | ||
| /** | ||
| * The ID of the root component, and the prefix of every other ID in the tree. Defaults | ||
| * to the root's own `id`; without one, the prefix is `root` and the root's ID is | ||
| * allocated like any other. | ||
| */ | ||
| rootId?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Flattens a nested component tree into the flat component list of `updateComponents`. | ||
| * | ||
| * Descendants are listed before their parents, so the root comes last. Nested nodes are | ||
| * replaced by their IDs, and ID references are kept as written. A descendant without an | ||
| * `id` gets `<prefix>__<component>_<n>`; one with an `id` gets `<prefix>__<id>`, and no | ||
| * allocated ID ever takes the place of an ID the author set. A node that appears in two | ||
| * places is listed once and referenced from both. Properties set to `undefined` are left | ||
| * out. | ||
| * | ||
| * An array of roots flattens each root on its own, with the prefix `<rootId>_<index>` | ||
| * when `rootId` is given. A root that is an ID reference adds nothing. | ||
| */ | ||
| export function flattenComponentTree( | ||
| root: ComponentChild | readonly ComponentChild[], | ||
| options: FlattenComponentTreeOptions = {}, | ||
| ): FlatComponent[] { | ||
| const {rootId} = options; | ||
| if (Array.isArray(root)) { | ||
| return root.flatMap((item, index) => | ||
| flattenComponentTree(item, {rootId: rootId ? `${rootId}_${index}` : undefined}), | ||
| ); | ||
| } | ||
| if (!isComponentNode(root)) { | ||
| return []; | ||
| } | ||
|
|
||
| const prefix = rootId || root.id || 'root'; | ||
| const reserved = reservedIds(root, prefix); | ||
| if (rootId) { | ||
| reserved.add(rootId); | ||
| } | ||
| const flattener = new TreeFlattener(new IdAllocator(prefix, reserved)); | ||
| return flattener.flatten(root, rootId || root.id); | ||
| } | ||
|
|
||
| /** The IDs the authors set anywhere in the tree, bare and namespaced. */ | ||
| function reservedIds(root: ComponentNode, prefix: string): Set<string> { | ||
| const reserved = new Set<string>(); | ||
| const visited = new Set<object>(); | ||
| const visit = (value: unknown): void => { | ||
| if (typeof value !== 'object' || value === null || visited.has(value)) { | ||
| return; | ||
| } | ||
| visited.add(value); | ||
| if (isComponentNode(value) && value.id) { | ||
| reserved.add(value.id); | ||
| reserved.add(`${prefix}__${value.id}`); | ||
| } | ||
| for (const nested of Object.values(value)) { | ||
| visit(nested); | ||
| } | ||
| }; | ||
| visit(root); | ||
| return reserved; | ||
| } | ||
|
|
||
| class TreeFlattener { | ||
| private readonly components: FlatComponent[] = []; | ||
| private readonly assigned = new Map<ComponentNode, string>(); | ||
|
|
||
| constructor(private readonly allocator: IdAllocator) {} | ||
|
|
||
| flatten(root: ComponentNode, rootId: string | undefined): FlatComponent[] { | ||
| const id = rootId || this.allocator.allocate(root.component); | ||
| this.assigned.set(root, id); | ||
| this.components.push(this.toFlatComponent(root, id)); | ||
| return this.components; | ||
| } | ||
|
|
||
| private emit(node: ComponentNode): string { | ||
| const assigned = this.assigned.get(node); | ||
| if (assigned !== undefined) { | ||
| return assigned; | ||
| } | ||
| // Allocated before the descendants' IDs: counters run in pre-order while the list is | ||
| // in post-order, which the conformance goldens pin. | ||
| const id = this.allocator.allocate(node.component, node.id); | ||
| this.assigned.set(node, id); | ||
| this.components.push(this.toFlatComponent(node, id)); | ||
| return id; | ||
| } | ||
|
|
||
| private toFlatComponent(node: ComponentNode, id: string): FlatComponent { | ||
| const flat: FlatComponent = {component: node.component, id}; | ||
| for (const [key, value] of Object.entries(node)) { | ||
| if (key !== 'component' && key !== 'id' && value !== undefined) { | ||
| flat[key] = this.toWireValue(value); | ||
| } | ||
| } | ||
| return flat; | ||
| } | ||
|
|
||
| private toWireValue(value: unknown): unknown { | ||
| if (Array.isArray(value)) { | ||
| return value.map(item => this.toWireValue(item)); | ||
| } | ||
| if (isComponentNode(value)) { | ||
| return this.emit(value); | ||
| } | ||
| if (typeof value === 'object' && value !== null) { | ||
| const wire: Record<string, unknown> = {}; | ||
| for (const [key, nested] of Object.entries(value)) { | ||
| if (nested !== undefined) { | ||
| wire[key] = this.toWireValue(nested); | ||
| } | ||
| } | ||
| return wire; | ||
| } | ||
| return value; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duck-typing any object with a string
componentkey breaks when a plain data object happens to have acomponentproperty (for example insideparam.dynamicValue(),action.event.context,FunctionCall.args, or@metadata.extensions).flattenComponentTreehoists that data object into the top-levelcomponentsarray and replaces it with an ID string.Please match Python's nominal classes (
ComponentBuilderNodeandComponentRefinpython/a2ui_agent/src/a2ui/builder/core/base_node.pyandblueprints/features/typesafe_builder_api.blueprint.md) and checkinstanceof ComponentBuilderNodeinstead. That also avoids colliding with@a2ui/web_core/v1_0'sComponentNode, and lets future generated builder classes extendComponentBuilderNodedirectly.Here is a failing test to verify the fix:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #4540: nodes are instances of
ComponentBuilderNodenow, withComponentReffor components already on the surface, and the flattener checksinstanceof. Your case is a test in #4541 (expander-transport.test.ts), and #4540 has a flattener-level version of it. A component argument arrives as aComponentRef, as in Python.