8000 fix(chat): route A2UI button actions to the agent by huong8373tt-beep · Pull Request #558 · google/adk-web · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/app/components/chat/chat.component.spec.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import {Location} from '@angular/common';
import {Catalog} from '@a2ui/angular/v0_8';
import {Catalog, MessageProcessor} from '@a2ui/angular/v0_8';
import {HttpErrorResponse} from '@angular/common/http';
import {ChangeDetectionStrategy, Component, ElementRef, ErrorHandler, Input} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
Expand Down Expand Up @@ -188,6 +188,7 @@ describe('ChatComponent', () => {
let mockUiStateService: MockUiStateService;
let mockErrorHandler: jasmine.SpyObj<ErrorHandler>;
let mockAgentBuilderService: jasmine.SpyObj<any>;
let a2uiMessageProcessor: MessageProcessor;

beforeEach(async () => {
mockTelemetryService = new MockTelemetryService();
Expand Down Expand Up @@ -326,11 +327,42 @@ describe('ChatComponent', () => {

fixture = TestBed.createComponent(ChatComponent);
component = fixture.componentInstance;
a2uiMessageProcessor = TestBed.inject(MessageProcessor);
fixture.detectChanges();
await fixture.whenStable();
});

describe('Component Initialization', () => {
it('routes A2UI user actions through the current session and completes dispatch', async () => {
component.appName = TEST_APP_1_NAME;
component.sessionId = SESSION_1_ID;
spyOn(component, 'sendMessage').and.resolveTo();

const completion = a2uiMessageProcessor.dispatch({
userAction: {
name: 'clicked',
surfaceId: 'surface-1',
sourceComponentId: 'button-1',
timestamp: '2026-10-31T00:00:00.000Z',
context: {choice: 'yes'},
},
});

await expectAsync(completion).toBeResolvedTo([]);
expect(component.sendMessage).toHaveBeenCalledWith({
role: 'user',
parts: [{text: JSON.stringify({
userAction: {
name: 'clicked',
surfaceId: 'surface-1',
sourceComponentId: 'button-1',
timestamp: '2026-10-31T00:00:00.000Z',
context: {choice: 'yes'},
},
})}],
});
});

it('should create', () => {
expect(component).toBeTruthy();
});
Expand Down
25 changes: 25 additions & 0 deletions src/app/components/chat/chat.component.ts
8000
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import { AsyncPipe, DOCUMENT, DecimalPipe, NgClass, NgComponentOutlet } from '@angular/common';
import { MessageProcessor } from '@a2ui/angular/v0_8';
import { HttpErrorResponse } from '@angular/common/http';
import { AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, Component, computed, ElementRef, effect, HostListener, inject, Injectable, OnDestroy, OnInit, Renderer2, signal, Type, viewChild, WritableSignal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
Expand Down Expand Up @@ -199,6 +200,7 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
private readonly _snackbarService = inject(SnackbarService);
private readonly activatedRoute = inject(ActivatedRoute);
private readonly agentService = inject(AGENT_SERVICE);
private readonly a2uiMessageProcessor = inject(MessageProcessor);
private readonly artifactService = inject(ARTIFACT_SERVICE);
private readonly changeDetectorRef = inject(ChangeDetectorRef);
private readonly dialog = inject(MatDialog);
Expand Down Expand Up @@ -232,6 +234,7 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
});

private activeSseSubscription?: Subscription;
private a2uiActionSubscription?: Subscription;

chatPanel = viewChild(ChatPanelComponent);
canvasComponent = viewChild.required(CanvasComponent);
Expand Down Expand Up @@ -828,6 +831,27 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
}

ngOnInit(): void {
this.a2uiActionSubscription = this.a2uiMessageProcessor.events.subscribe(
({message, completion}) => {
const userAction = (message as any)?.userAction;
if (!userAction) {
completion.next([]);
completion.complete();
return;
}

this.sendMessage({
role: 'user',
parts: [{text: JSON.stringify({userAction})}],
}).then(
() => {
completion.next([]);
completion.complete();
},
(error) => completion.error(error),
);
},
);
this.checkScreenSize();
if (this.isMobile()) {
this.showSidePanel = false;
Expand Down Expand Up @@ -2244,6 +2268,7 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
}

ngOnDestroy(): void {
this.a2uiActionSubscription?.unsubscribe();
this.handleStopMessage();
this.streamChatService.closeStream();
}
Expand Down
Loading
0