-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(web-core): implement client capabilities API in MessageProcessor #826
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
Open
jacobsimionato
wants to merge
16
commits into
google:main
Choose a base branch
from
jacobsimionato:clientCapabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c5972a1
docs: Update renderer guide with detailed architecture
jacobsimionato d91acc1
docs: Make schema types location section less web-centric
jacobsimionato e109ed5
docs: Refactor Function Implementation Rationale section
jacobsimionato 679ebe5
clean up function impelemntation stuff
jacobsimionato 3002c85
docs: Add missing Implementation Topologies section
jacobsimionato 761d27e
docs: Restore Implementation Topologies, Contract of Ownership, Adapt…
jacobsimionato 4dd2332
Merge branch 'main' into guide-update
jacobsimionato 5ab80eb
feat(web-core): implement client capabilities API in MessageProcessor
jacobsimionato eda91f3
Revert changes to renderer guide
jacobsimionato 9897453
Address feedback
jacobsimionato e7bd3ed
Merge branch 'main' into clientCapabilities
jacobsimionato 0184d18
remove patch
jacobsimionato 443f20c
Update inline catalog stuff
jacobsimionato 7cc1587
Update typings of client capabilities etc
jacobsimionato b5210a3
Update renderer guide
jacobsimionato 21019fd
udpate renderer guide for models
jacobsimionato 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import { Catalog, ComponentApi } from "../catalog/types.js"; | |
| import { SurfaceGroupModel } from "../state/surface-group-model.js"; | ||
| import { ComponentModel } from "../state/component-model.js"; | ||
| import { Subscription } from "../common/events.js"; | ||
| import { zodToJsonSchema } from "zod-to-json-schema"; | ||
|
|
||
| import { | ||
| A2uiMessage, | ||
|
|
@@ -29,6 +30,14 @@ import { | |
| } from "../schema/server-to-client.js"; | ||
| import { A2uiStateError, A2uiValidationError } from "../errors.js"; | ||
|
|
||
| /** | ||
| * Options for generating client capabilities. | ||
| */ | ||
| export interface CapabilitiesOptions { | ||
| /** If true, the full definition of all catalogs will be included. */ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the idea for this option was to allow inline catalogs, not to include the full definition of all of them. |
||
| includeInlineCatalogs?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * The central processor for A2UI messages. | ||
| * @template T The concrete type of the ComponentApi. | ||
|
|
@@ -52,6 +61,100 @@ export class MessageProcessor<T extends ComponentApi> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates the a2uiClientCapabilities object for the current processor. | ||
| * | ||
| * @param options Configuration for capability generation. | ||
| * @returns The capabilities object. | ||
| */ | ||
| getClientCapabilities(options?: CapabilitiesOptions): any { | ||
jacobsimionato marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const capabilities: any = { | ||
| "v0.9": { | ||
| supportedCatalogIds: this.catalogs.map((c) => c.id), | ||
| }, | ||
| }; | ||
|
|
||
| if (options?.includeInlineCatalogs) { | ||
| capabilities["v0.9"].inlineCatalogs = this.catalogs.map((c) => | ||
| this.generateInlineCatalog(c), | ||
| ); | ||
| } | ||
|
|
||
| return capabilities; | ||
| } | ||
|
|
||
| private generateInlineCatalog(catalog: Catalog<T>): any { | ||
jacobsimionato marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const components: Record<string, any> = {}; | ||
|
|
||
| for (const [name, api] of catalog.components.entries()) { | ||
| const zodSchema = zodToJsonSchema(api.schema, { | ||
| target: "jsonSchema2019-09", | ||
| }) as any; | ||
|
|
||
| // Clean up Zod-specific artifacts and process REF: tags | ||
| this.processRefs(zodSchema); | ||
|
|
||
| // Wrap in standard A2UI component envelope (ComponentCommon) | ||
| components[name] = { | ||
| allOf: [ | ||
| { $ref: "common_types.json#/$defs/ComponentCommon" }, | ||
| { | ||
| properties: { | ||
| component: { const: name }, | ||
| ...zodSchema.properties, | ||
| }, | ||
| required: ["component", ...(zodSchema.required || [])], | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| catalogId: catalog.id, | ||
| components, | ||
| }; | ||
| } | ||
|
|
||
| private processRefs(node: any): void { | ||
| if (typeof node !== "object" || node === null) return; | ||
|
|
||
| if (Array.isArray(node)) { | ||
| for (const item of node) { | ||
| this.processRefs(item); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for (const key of Object.keys(node)) { | ||
| const val = node[key]; | ||
|
|
||
| if (key === "description" && typeof val === "string") { | ||
| if (val.startsWith("REF:")) { | ||
| const parts = val.substring(4).split("|"); | ||
| const ref = parts[0]; | ||
| const desc = parts[1] || ""; | ||
|
|
||
| // Mutate the parent node to be a $ref | ||
| // We remove other validation keywords that Zod might have added (like type: string) | ||
| // but keep the description. | ||
| for (const k of Object.keys(node)) { | ||
| if (k !== "description") { | ||
| delete node[k]; | ||
| } | ||
| } | ||
| node["$ref"] = ref; | ||
| if (desc) { | ||
| node["description"] = desc; | ||
| } else { | ||
| delete node["description"]; | ||
| } | ||
| } | ||
| } else { | ||
| this.processRefs(val); | ||
| } | ||
| } | ||
| } | ||
jacobsimionato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Subscribes to surface creation events. | ||
| */ | ||
|
|
||
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.
This might be bloating the front end, though. I was tempted to add this in the past too, but decided against it in order to keep the front end lean. If you think it's worth it, we should quantify the size hit.