|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { Injectable } from '@angular/core'; |
| 18 | +import { A2uiRendererService } from '@a2ui/angular/v0_9'; |
| 19 | +import { SurfaceGroupAction, A2uiMessage } from '@a2ui/web_core/v0_9'; |
| 20 | +import { ActionDispatcher } from './action-dispatcher.service'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Context for the 'update_property' event. |
| 24 | + */ |
| 25 | +interface UpdatePropertyContext { |
| 26 | + path: string; |
| 27 | + value: any; |
| 28 | + surfaceId?: string; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Context for the 'submit_form' event. |
| 33 | + */ |
| 34 | +interface SubmitFormContext { |
| 35 | + [key: string]: any; |
| 36 | + name?: string; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A stub service that simulates an A2UI agent. |
| 41 | + * It listens for actions and responds with data model updates or new surfaces. |
| 42 | + */ |
| 43 | +@Injectable({ |
| 44 | + providedIn: 'root', |
| 45 | +}) |
| 46 | +export class AgentStubService { |
| 47 | + /** Log of actions received from the surface. */ |
| 48 | + actionsLog: Array<{ timestamp: Date; action: SurfaceGroupAction }> = []; |
| 49 | + |
| 50 | + constructor( |
| 51 | + private rendererService: A2uiRendererService, |
| 52 | + private dispatcher: ActionDispatcher, |
| 53 | + ) { |
| 54 | + // Subscribe to actions dispatched by the renderer |
| 55 | + this.dispatcher.actions.subscribe((action) => this.handleAction(action)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Pushes actions triggered from the rendered Canvas frame through simulation. |
| 60 | + * - Logs actions into inspector event frame aggregates. |
| 61 | + * - Emulates generic server-side evaluation triggers delaying deferred updates. |
| 62 | + * - Dispatch subsequent node-tree node triggers back over `A2uiRendererService`. |
| 63 | + */ |
| 64 | + handleAction(action: SurfaceGroupAction) { |
| 65 | + console.log('[AgentStub] handleAction action:', action); |
| 66 | + this.actionsLog.push({ timestamp: new Date(), action }); |
| 67 | + |
| 68 | + // Simulate server processing delay |
| 69 | + setTimeout(() => { |
| 70 | + if ('event' in action) { |
| 71 | + const { name, context } = action.event; |
| 72 | + if (name === 'update_property' && context) { |
| 73 | + const { path, value, surfaceId } = context as unknown as UpdatePropertyContext; |
| 74 | + console.log( |
| 75 | + '[AgentStub] update_property path:', |
| 76 | + path, |
| 77 | + 'value:', |
| 78 | + value, |
| 79 | + 'surfaceId:', |
| 80 | + surfaceId, |
| 81 | + ); |
| 82 | + this.rendererService.processMessages([ |
| 83 | + { |
| 84 | + version: 'v0.9', |
| 85 | + updateDataModel: { |
| 86 | + surfaceId: surfaceId || action.surfaceId, |
| 87 | + path: path, |
| 88 | + value: value, |
| 89 | + }, |
| 90 | + }, |
| 91 | + ]); |
| 92 | + } else if (name === 'submit_form' && context) { |
| 93 | + const formData = context as unknown as SubmitFormContext; |
| 94 | + const nameValue = formData.name || 'Anonymous'; |
| 95 | + |
| 96 | + // Respond with an update to the data model in v0.9 layout |
| 97 | + this.rendererService.processMessages([ |
| 98 | + { |
| 99 | + version: 'v0.9', |
| 100 | + updateDataModel: { |
| 101 | + surfaceId: action.surfaceId, |
| 102 | + path: '/form/submitted', |
| 103 | + value: true, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + version: 'v0.9', |
| 108 | + updateDataModel: { |
| 109 | + surfaceId: action.surfaceId, |
| 110 | + path: '/form/responseMessage', |
| 111 | + value: `Hello, ${nameValue}! Your form has been processed.`, |
| 112 | + }, |
| 113 | + }, |
| 114 | + ]); |
| 115 | + } |
| 116 | + } |
| 117 | + }, 50); // Shorter delay for property updates |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Initializes a demo session with an initial set of messages. |
| 122 | + */ |
| 123 | + initializeDemo(initialMessages: A2uiMessage[]) { |
| 124 | + this.rendererService.processMessages(initialMessages); |
| 125 | + } |
| 126 | +} |
0 commit comments