Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/app/components/chat/chat.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,50 @@ describe('ChatComponent', () => {
});
});

describe('extractA2uiJsonFromText', () => {
it('should do nothing if message has no text', () => {
const uiEvent = new UiEvent({role: 'bot', event: {} as any});
(component as any).extractA2uiJsonFromText(uiEvent);
expect(uiEvent.a2uiData).toBeUndefined();
});

it('should do nothing if text has no <a2ui-json> tags', () => {
const uiEvent = new UiEvent({
role: 'bot',
text: 'hello world',
event: {} as any
});
(component as any).extractA2uiJsonFromText(uiEvent);
expect(uiEvent.a2uiData).toBeUndefined();
expect(uiEvent.text).toBe('hello world');
});

it('should extract and parse inline <a2ui-json> block, and strip it from text', () => {
const payload = [{beginRendering: {surfaceId: 'cloud_dash'}}];
const uiEvent = new UiEvent({
role: 'bot',
text: `Here is the UI:\n<a2ui-json>\n${JSON.stringify(payload)}\n</a2ui-json>\nEnjoy!`,
event: {} as any
});

(component as any).extractA2uiJsonFromText(uiEvent);
expect(uiEvent.a2uiData).toEqual({beginRendering: {beginRendering: {surfaceId: 'cloud_dash'}}});
expect(uiEvent.text).toBe('Here is the UI:\n\nEnjoy!');
});

it('should keep tags and log warning if JSON parsing fails', () => {
const uiEvent = new UiEvent({
role: 'bot',
text: `broken tags: <a2ui-json>{broken-json}</a2ui-json>`,
event: {} as any
});

(component as any).extractA2uiJsonFromText(uiEvent);
expect(uiEvent.a2uiData).toBeUndefined();
expect(uiEvent.text).toBe('broken tags: <a2ui-json>{broken-json}</a2ui-json>');
});
});

describe('refreshLatestSession', () => {
beforeEach(() => {
mockAgentService.listAppsResponse.next([TEST_APP_1_NAME]);
Expand Down
44 changes: 44 additions & 0 deletions src/app/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,48 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
return a2uiData;
}

private extractA2uiJsonFromText(uiEvent: UiEvent) {
if (!uiEvent.text) return;

const startTag = '<a2ui-json>';
const endTag = '</a2ui-json>';

const startIndex = uiEvent.text.indexOf(startTag);
if (startIndex === -1) return;

const endIndex = uiEvent.text.indexOf(endTag, startIndex + startTag.length);
if (endIndex === -1) return;

const jsonStr = uiEvent.text.substring(startIndex + startTag.length, endIndex).trim();
try {
let parsed = JSON.parse(jsonStr);
if (!Array.isArray(parsed)) {
parsed = [parsed];
}

const a2uiData: any = {};
parsed.forEach((msg: any) => {
if (msg.beginRendering) {
a2uiData.beginRendering = msg;
} else if (msg.surfaceUpdate) {
a2uiData.surfaceUpdate = msg;
} else if (msg.dataModelUpdate) {
a2uiData.dataModelUpdate = msg;
}
});

uiEvent.a2uiData = a2uiData;

// Strip the <a2ui-json> block from uiEvent.text
const beforeText = uiEvent.text.substring(0, startIndex);
const afterText = uiEvent.text.substring(endIndex + endTag.length);
uiEvent.text = (beforeText + afterText).trim();

} catch (e) {
console.warn('Failed to parse inline <a2ui-json> block from text:', e);
}
}

private updateRedirectUri(urlString: string, newRedirectUri: string): string {
try {
const url = new URL(urlString);
Expand Down Expand Up @@ -2153,6 +2195,8 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
this.processPartIntoMessage(part, event, uiEvent);
});

this.extractA2uiJsonFromText(uiEvent);

return uiEvent;
}

Expand Down
Loading