Skip to content

Commit 4d8be51

Browse files
authored
feat(ui): add artifact update parts processing (#1016)
1 parent 4bd4462 commit 4d8be51

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

apps/beeai-ui/src/api/a2a/client.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import type { TaskStatusUpdateEvent } from '@a2a-js/sdk';
6+
import type { TaskArtifactUpdateEvent, TaskStatusUpdateEvent } from '@a2a-js/sdk';
77
import { A2AClient } from '@a2a-js/sdk/client';
88
import { Subject } from 'rxjs';
99
import { match } from 'ts-pattern';
1010

1111
import type { UIMessagePart, UIUserMessage } from '#modules/messages/types.ts';
1212
import type { ContextId, TaskId } from '#modules/tasks/api/types.ts';
1313
import { getBaseUrl } from '#utils/api/getBaseUrl.ts';
14-
import { isNotNull } from '#utils/helpers.ts';
1514

1615
import { AGENT_ERROR_MESSAGE } from './constants';
17-
import { processFilePart, processMessageMetadata, processTextPart } from './part-processors';
16+
import { processMessageMetadata, processParts } from './part-processors';
1817
import type { ChatRun } from './types';
1918
import { createUserMessage, extractTextFromMessage } from './utils';
2019

@@ -32,23 +31,17 @@ function handleStatusUpdate(event: TaskStatusUpdateEvent): UIMessagePart[] {
3231
}
3332

3433
const metadataParts = processMessageMetadata(message);
34+
const contentParts = processParts(message.parts);
3535

36-
const contentParts = message.parts
37-
.flatMap((part) => {
38-
const processedParts = match(part)
39-
.with({ kind: 'text' }, (part) => processTextPart(part))
40-
.with({ kind: 'file' }, processFilePart)
41-
.otherwise((otherPart) => {
42-
console.warn(`Unsupported part - ${otherPart.kind}`);
36+
return [...metadataParts, ...contentParts];
37+
}
4338

44-
return null;
45-
});
39+
function handleArtifactUpdate(event: TaskArtifactUpdateEvent): UIMessagePart[] {
40+
const { artifact } = event;
4641

47-
return processedParts;
48-
})
49-
.filter(isNotNull);
42+
const contentParts = processParts(artifact.parts);
5043

51-
return [...metadataParts, ...contentParts];
44+
return contentParts;
5245
}
5346

5447
export const buildA2AClient = (providerId: string) => {
@@ -70,9 +63,16 @@ export const buildA2AClient = (providerId: string) => {
7063
.with({ kind: 'status-update' }, (event) => {
7164
taskId = event.taskId;
7265

73-
const messageParts = handleStatusUpdate(event);
66+
const parts = handleStatusUpdate(event);
67+
68+
messageSubject.next({ parts, taskId });
69+
})
70+
.with({ kind: 'artifact-update' }, (event) => {
71+
taskId = event.taskId;
72+
73+
const parts = handleArtifactUpdate(event);
7474

75-
messageSubject.next({ parts: messageParts, taskId });
75+
messageSubject.next({ parts, taskId });
7676
});
7777
}
7878
messageSubject.complete();

apps/beeai-ui/src/api/a2a/part-processors.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import type { FilePart, Message, TextPart } from '@a2a-js/sdk';
6+
import type { FilePart, Message, Part, TextPart } from '@a2a-js/sdk';
7+
import { match } from 'ts-pattern';
78
import { v4 as uuid } from 'uuid';
89

910
import type { UIFilePart, UIMessagePart } from '#modules/messages/types.ts';
@@ -54,3 +55,22 @@ export function processFilePart(part: FilePart): UIMessagePart[] {
5455

5556
return [filePart];
5657
}
58+
59+
export function processParts(parts: Part[]): UIMessagePart[] {
60+
const processedParts = parts
61+
.flatMap((part) => {
62+
const processedParts = match(part)
63+
.with({ kind: 'text' }, (part) => processTextPart(part))
64+
.with({ kind: 'file' }, processFilePart)
65+
.otherwise((otherPart) => {
66+
console.warn(`Unsupported part - ${otherPart.kind}`);
67+
68+
return null;
69+
});
70+
71+
return processedParts;
72+
})
73+
.filter(isNotNull);
74+
75+
return processedParts;
76+
}

0 commit comments

Comments
 (0)