-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: implement client data model synchronization #866
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6e29e9a
Add work
jacobsimionato 35b0be3
docs: strongly type getClientDataModel in renderer guide
jacobsimionato bf60851
address feedback
jacobsimionato b5c9c64
docs: strongly type interfaces in renderer_guide.md code examples
jacobsimionato 9bedbc9
Resolve merge conflicts and update state models to use A2uiClientAction
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
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
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
90 changes: 90 additions & 0 deletions
90
renderers/web_core/src/v0_9/schema/client-to-server.test.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,90 @@ | ||
| /* | ||
| * Copyright 2026 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 { describe, it } from "node:test"; | ||
| import * as assert from "node:assert"; | ||
| import { A2uiClientMessageSchema, A2uiClientDataModelSchema } from "./client-to-server.js"; | ||
|
|
||
| describe("Client-to-Server Schema Verification", () => { | ||
| it("validates a valid action message", () => { | ||
| const validAction = { | ||
| version: "v0.9", | ||
| action: { | ||
| name: "submit", | ||
| surfaceId: "s1", | ||
| sourceComponentId: "c1", | ||
| timestamp: new Date().toISOString(), | ||
| context: { foo: "bar" }, | ||
| }, | ||
| }; | ||
| const result = A2uiClientMessageSchema.safeParse(validAction); | ||
| assert.ok(result.success, result.success ? "" : result.error.message); | ||
| }); | ||
|
|
||
| it("validates a valid error message (validation failed)", () => { | ||
| const validError = { | ||
| version: "v0.9", | ||
| error: { | ||
| code: "VALIDATION_FAILED", | ||
| surfaceId: "s1", | ||
| path: "/components/0/text", | ||
| message: "Too short", | ||
| }, | ||
| }; | ||
| const result = A2uiClientMessageSchema.safeParse(validError); | ||
| assert.ok(result.success, result.success ? "" : result.error.message); | ||
| }); | ||
|
|
||
| it("validates a valid error message (generic)", () => { | ||
| const validError = { | ||
| version: "v0.9", | ||
| error: { | ||
| code: "INTERNAL_ERROR", | ||
| message: "Something went wrong", | ||
| surfaceId: "s1", | ||
| }, | ||
| }; | ||
| const result = A2uiClientMessageSchema.safeParse(validError); | ||
| assert.ok(result.success, result.success ? "" : result.error.message); | ||
| }); | ||
|
|
||
| it("validates a valid data model message", () => { | ||
| const validDataModel = { | ||
| version: "v0.9", | ||
| surfaces: { | ||
| s1: { user: "Alice" }, | ||
| s2: { cart: [] }, | ||
| }, | ||
| }; | ||
| const result = A2uiClientDataModelSchema.safeParse(validDataModel); | ||
| assert.ok(result.success, result.success ? "" : result.error.message); | ||
| }); | ||
|
|
||
| it("fails on invalid version", () => { | ||
| const invalidAction = { | ||
| version: "v0.8", | ||
| action: { | ||
| name: "submit", | ||
| surfaceId: "s1", | ||
| sourceComponentId: "c1", | ||
| timestamp: new Date().toISOString(), | ||
| context: {}, | ||
| }, | ||
| }; | ||
| const result = A2uiClientMessageSchema.safeParse(invalidAction); | ||
| assert.strictEqual(result.success, false); | ||
| }); | ||
| }); |
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,82 @@ | ||
| /* | ||
| * Copyright 2026 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 { z } from "zod"; | ||
|
|
||
| /** | ||
| * Reports a user-initiated action from a component. | ||
| * Matches 'action' in specification/v0_9/json/client_to_server.json. | ||
| */ | ||
| export const A2uiClientActionSchema = z.object({ | ||
| name: z.string().describe("The name of the action, taken from the component's action.event.name property."), | ||
| surfaceId: z.string().describe("The id of the surface where the event originated."), | ||
| sourceComponentId: z.string().describe("The id of the component that triggered the event."), | ||
| timestamp: z.string().datetime().describe("An ISO 8601 timestamp of when the event occurred."), | ||
| context: z.record(z.any()).describe("A JSON object containing the key-value pairs from the component's action.event.context, after resolving all data bindings."), | ||
| }).strict(); | ||
|
|
||
| /** | ||
| * Reports a client-side validation failure. | ||
| */ | ||
| export const A2uiValidationErrorSchema = z.object({ | ||
| code: z.literal("VALIDATION_FAILED"), | ||
| surfaceId: z.string().describe("The id of the surface where the error occurred."), | ||
| path: z.string().describe("The JSON pointer to the field that failed validation (e.g. '/components/0/text')."), | ||
| message: z.string().describe("A short one or two sentence description of why validation failed."), | ||
| }).strict(); | ||
|
|
||
| /** | ||
| * Reports a generic client-side error. | ||
| */ | ||
| export const A2uiGenericErrorSchema = z.object({ | ||
| code: z.string().refine(c => c !== "VALIDATION_FAILED"), | ||
| message: z.string().describe("A short one or two sentence description of why the error occurred."), | ||
| surfaceId: z.string().describe("The id of the surface where the error occurred."), | ||
| }).passthrough(); | ||
|
|
||
| /** | ||
| * Reports a client-side error. | ||
| * Matches 'error' in specification/v0_9/json/client_to_server.json. | ||
| */ | ||
| export const A2uiClientErrorSchema = z.union([ | ||
| A2uiValidationErrorSchema, | ||
| A2uiGenericErrorSchema, | ||
| ]); | ||
|
|
||
| /** | ||
| * A message sent from the A2UI client to the server. | ||
| * Matches specification/v0_9/json/client_to_server.json. | ||
| */ | ||
| export const A2uiClientMessageSchema = z.object({ | ||
| version: z.literal("v0.9"), | ||
| }).and(z.union([ | ||
| z.object({ action: A2uiClientActionSchema }), | ||
| z.object({ error: A2uiClientErrorSchema }), | ||
| ])); | ||
|
|
||
| /** | ||
| * Schema for the client data model synchronization. | ||
| * Matches specification/v0_9/json/client_data_model.json. | ||
| */ | ||
| export const A2uiClientDataModelSchema = z.object({ | ||
| version: z.literal("v0.9"), | ||
| surfaces: z.record(z.object({}).passthrough()).describe("A map of surface IDs to their current data models."), | ||
| }).strict(); | ||
|
|
||
| export type A2uiClientAction = z.infer<typeof A2uiClientActionSchema>; | ||
| export type A2uiClientError = z.infer<typeof A2uiClientErrorSchema>; | ||
| export type A2uiClientMessage = z.infer<typeof A2uiClientMessageSchema>; | ||
| export type A2uiClientDataModel = z.infer<typeof A2uiClientDataModelSchema>; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.