@assistant-ui/react-a2a 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/A2AMessageAccumulator.d.ts +16 -0
  4. package/dist/A2AMessageAccumulator.d.ts.map +1 -0
  5. package/dist/A2AMessageAccumulator.js +35 -0
  6. package/dist/A2AMessageAccumulator.js.map +1 -0
  7. package/dist/appendA2AChunk.d.ts +3 -0
  8. package/dist/appendA2AChunk.d.ts.map +1 -0
  9. package/dist/appendA2AChunk.js +90 -0
  10. package/dist/appendA2AChunk.js.map +1 -0
  11. package/dist/convertA2AMessages.d.ts +64 -0
  12. package/dist/convertA2AMessages.d.ts.map +1 -0
  13. package/dist/convertA2AMessages.js +93 -0
  14. package/dist/convertA2AMessages.js.map +1 -0
  15. package/dist/index.d.ts +7 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +8 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/testUtils.d.ts +4 -0
  20. package/dist/testUtils.d.ts.map +1 -0
  21. package/dist/testUtils.js +10 -0
  22. package/dist/testUtils.js.map +1 -0
  23. package/dist/types.d.ts +94 -0
  24. package/dist/types.d.ts.map +1 -0
  25. package/dist/types.js +14 -0
  26. package/dist/types.js.map +1 -0
  27. package/dist/useA2AMessages.d.ts +25 -0
  28. package/dist/useA2AMessages.d.ts.map +1 -0
  29. package/dist/useA2AMessages.js +134 -0
  30. package/dist/useA2AMessages.js.map +1 -0
  31. package/dist/useA2ARuntime.d.ts +55 -0
  32. package/dist/useA2ARuntime.d.ts.map +1 -0
  33. package/dist/useA2ARuntime.js +215 -0
  34. package/dist/useA2ARuntime.js.map +1 -0
  35. package/package.json +68 -0
  36. package/src/A2AMessageAccumulator.ts +48 -0
  37. package/src/appendA2AChunk.ts +121 -0
  38. package/src/convertA2AMessages.ts +108 -0
  39. package/src/index.ts +6 -0
  40. package/src/testUtils.ts +11 -0
  41. package/src/types.ts +114 -0
  42. package/src/useA2AMessages.ts +180 -0
  43. package/src/useA2ARuntime.ts +331 -0
@@ -0,0 +1,331 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import {
3
+ A2AMessage,
4
+ A2AToolCall,
5
+ A2AArtifact,
6
+ A2ATaskState,
7
+ A2ASendMessageConfig,
8
+ A2AStreamCallback,
9
+ OnTaskUpdateEventCallback,
10
+ OnArtifactsEventCallback,
11
+ OnErrorEventCallback,
12
+ OnStateUpdateEventCallback,
13
+ OnCustomEventCallback,
14
+ } from "./types";
15
+ import {
16
+ useExternalMessageConverter,
17
+ useExternalStoreRuntime,
18
+ useThread,
19
+ useThreadListItemRuntime,
20
+ } from "@assistant-ui/react";
21
+ import { convertA2AMessage } from "./convertA2AMessages";
22
+ import { useA2AMessages } from "./useA2AMessages";
23
+ import { AttachmentAdapter } from "@assistant-ui/react";
24
+ import { AppendMessage } from "@assistant-ui/react";
25
+ import { ExternalStoreAdapter } from "@assistant-ui/react";
26
+ import { FeedbackAdapter } from "@assistant-ui/react";
27
+ import { SpeechSynthesisAdapter } from "@assistant-ui/react";
28
+ import { appendA2AChunk } from "./appendA2AChunk";
29
+
30
+ const getPendingToolCalls = (messages: A2AMessage[]) => {
31
+ const pendingToolCalls = new Map<string, A2AToolCall>();
32
+ for (const message of messages) {
33
+ if (message.role === "assistant") {
34
+ for (const toolCall of message.tool_calls ?? []) {
35
+ pendingToolCalls.set(toolCall.id, toolCall);
36
+ }
37
+ }
38
+ if (message.role === "tool") {
39
+ pendingToolCalls.delete(message.tool_call_id!);
40
+ }
41
+ }
42
+
43
+ return [...pendingToolCalls.values()];
44
+ };
45
+
46
+ const getMessageContent = (msg: AppendMessage) => {
47
+ const allContent = [
48
+ ...msg.content,
49
+ ...(msg.attachments?.flatMap((a) => a.content) ?? []),
50
+ ];
51
+ const content = allContent.map((part) => {
52
+ const type = part.type;
53
+ switch (type) {
54
+ case "text":
55
+ return { type: "text" as const, text: part.text };
56
+ case "image":
57
+ return { type: "image_url" as const, image_url: { url: part.image } };
58
+
59
+ case "tool-call":
60
+ throw new Error("Tool call appends are not supported.");
61
+
62
+ default:
63
+ const _exhaustiveCheck:
64
+ | "reasoning"
65
+ | "source"
66
+ | "file"
67
+ | "audio"
68
+ | "data" = type;
69
+ throw new Error(
70
+ `Unsupported append message part type: ${_exhaustiveCheck}`,
71
+ );
72
+ }
73
+ });
74
+
75
+ if (content.length === 1 && content[0]?.type === "text") {
76
+ return content[0].text ?? "";
77
+ }
78
+
79
+ return content;
80
+ };
81
+
82
+ const symbolA2ARuntimeExtras = Symbol("a2a-runtime-extras");
83
+ type A2ARuntimeExtras = {
84
+ [symbolA2ARuntimeExtras]: true;
85
+ send: (messages: A2AMessage[], config: A2ASendMessageConfig) => Promise<void>;
86
+ taskState: A2ATaskState | undefined;
87
+ artifacts: A2AArtifact[];
88
+ };
89
+
90
+ const asA2ARuntimeExtras = (extras: unknown): A2ARuntimeExtras => {
91
+ if (
92
+ typeof extras !== "object" ||
93
+ extras == null ||
94
+ !(symbolA2ARuntimeExtras in extras)
95
+ )
96
+ throw new Error(
97
+ "This method can only be called when you are using useA2ARuntime",
98
+ );
99
+
100
+ return extras as A2ARuntimeExtras;
101
+ };
102
+
103
+ export const useA2ATaskState = () => {
104
+ const { taskState } = useThread((t) => asA2ARuntimeExtras(t.extras));
105
+ return taskState;
106
+ };
107
+
108
+ export const useA2AArtifacts = () => {
109
+ const { artifacts } = useThread((t) => asA2ARuntimeExtras(t.extras));
110
+ return artifacts;
111
+ };
112
+
113
+ export const useA2ASend = () => {
114
+ const { send } = useThread((t) => asA2ARuntimeExtras(t.extras));
115
+ return send;
116
+ };
117
+
118
+ export const useA2ARuntime = ({
119
+ autoCancelPendingToolCalls,
120
+ adapters: { attachments, feedback, speech } = {},
121
+ unstable_allowCancellation,
122
+ stream,
123
+ contextId,
124
+ onSwitchToNewThread,
125
+ onSwitchToThread,
126
+ eventHandlers,
127
+ }: {
128
+ /**
129
+ * @deprecated For thread management use `useCloudThreadListRuntime` instead. This option will be removed in a future version.
130
+ */
131
+ contextId?: string | undefined;
132
+ autoCancelPendingToolCalls?: boolean | undefined;
133
+ unstable_allowCancellation?: boolean | undefined;
134
+ stream: A2AStreamCallback<A2AMessage>;
135
+ /**
136
+ * @deprecated For thread management use `useCloudThreadListRuntime` instead. This option will be removed in a future version.
137
+ */
138
+ onSwitchToNewThread?: () => Promise<void> | void;
139
+ onSwitchToThread?: (contextId: string) => Promise<{
140
+ messages: A2AMessage[];
141
+ artifacts?: A2AArtifact[];
142
+ }>;
143
+ adapters?:
144
+ | {
145
+ attachments?: AttachmentAdapter;
146
+ speech?: SpeechSynthesisAdapter;
147
+ feedback?: FeedbackAdapter;
148
+ }
149
+ | undefined;
150
+ /**
151
+ * Event handlers for various A2A stream events
152
+ */
153
+ eventHandlers?:
154
+ | {
155
+ /**
156
+ * Called when task updates are received from the A2A stream
157
+ */
158
+ onTaskUpdate?: OnTaskUpdateEventCallback;
159
+ /**
160
+ * Called when artifacts are received from the A2A stream
161
+ */
162
+ onArtifacts?: OnArtifactsEventCallback;
163
+ /**
164
+ * Called when errors occur during A2A stream processing
165
+ */
166
+ onError?: OnErrorEventCallback;
167
+ /**
168
+ * Called when state updates are received from the A2A stream
169
+ */
170
+ onStateUpdate?: OnStateUpdateEventCallback;
171
+ /**
172
+ * Called when custom events are received from the A2A stream
173
+ */
174
+ onCustomEvent?: OnCustomEventCallback;
175
+ }
176
+ | undefined;
177
+ }) => {
178
+ const {
179
+ taskState,
180
+ artifacts,
181
+ setArtifacts,
182
+ messages,
183
+ sendMessage,
184
+ cancel,
185
+ setMessages,
186
+ } = useA2AMessages({
187
+ appendMessage: appendA2AChunk,
188
+ stream,
189
+ ...(eventHandlers && { eventHandlers }),
190
+ });
191
+
192
+ const [isRunning, setIsRunning] = useState(false);
193
+ const handleSendMessage = async (
194
+ messages: A2AMessage[],
195
+ config: A2ASendMessageConfig,
196
+ ) => {
197
+ try {
198
+ setIsRunning(true);
199
+ await sendMessage(messages, config);
200
+ } catch (error) {
201
+ console.error("Error streaming A2A messages:", error);
202
+ } finally {
203
+ setIsRunning(false);
204
+ }
205
+ };
206
+
207
+ const threadMessages = useExternalMessageConverter({
208
+ callback: convertA2AMessage,
209
+ messages,
210
+ isRunning,
211
+ });
212
+
213
+ const switchToThread = !onSwitchToThread
214
+ ? undefined
215
+ : async (externalId: string) => {
216
+ const { messages, artifacts } = await onSwitchToThread(externalId);
217
+ setMessages(messages);
218
+ if (artifacts) {
219
+ setArtifacts(artifacts);
220
+ }
221
+ };
222
+
223
+ const threadList: NonNullable<
224
+ ExternalStoreAdapter["adapters"]
225
+ >["threadList"] = {
226
+ threadId: contextId,
227
+ onSwitchToNewThread: !onSwitchToNewThread
228
+ ? undefined
229
+ : async () => {
230
+ await onSwitchToNewThread();
231
+ setMessages([]);
232
+ setArtifacts([]);
233
+ },
234
+ onSwitchToThread: switchToThread,
235
+ };
236
+
237
+ const loadingRef = useRef(false);
238
+ const threadListItemRuntime = useThreadListItemRuntime({ optional: true });
239
+ useEffect(() => {
240
+ if (!threadListItemRuntime || !switchToThread || loadingRef.current) return;
241
+
242
+ const externalId = threadListItemRuntime.getState().externalId;
243
+ if (externalId) {
244
+ loadingRef.current = true;
245
+ switchToThread(externalId).finally(() => {
246
+ loadingRef.current = false;
247
+ });
248
+ }
249
+ // eslint-disable-next-line react-hooks/exhaustive-deps
250
+ }, []);
251
+
252
+ return useExternalStoreRuntime({
253
+ isRunning,
254
+ messages: threadMessages,
255
+ adapters: {
256
+ attachments,
257
+ feedback,
258
+ speech,
259
+ threadList,
260
+ },
261
+ extras: {
262
+ [symbolA2ARuntimeExtras]: true,
263
+ taskState,
264
+ artifacts,
265
+ send: handleSendMessage,
266
+ } satisfies A2ARuntimeExtras,
267
+ onNew: (msg) => {
268
+ const cancellations =
269
+ autoCancelPendingToolCalls !== false
270
+ ? getPendingToolCalls(messages).map(
271
+ (t) =>
272
+ ({
273
+ role: "tool",
274
+ tool_call_id: t.id,
275
+ content: JSON.stringify({ cancelled: true }),
276
+ status: {
277
+ type: "incomplete",
278
+ reason: "cancelled",
279
+ },
280
+ }) satisfies A2AMessage & { role: "tool" },
281
+ )
282
+ : [];
283
+
284
+ const config: A2ASendMessageConfig = {};
285
+ if (contextId !== undefined) config.contextId = contextId;
286
+ if (msg.runConfig !== undefined) config.runConfig = msg.runConfig;
287
+ return handleSendMessage(
288
+ [
289
+ ...cancellations,
290
+ {
291
+ role: "user",
292
+ content: getMessageContent(msg),
293
+ },
294
+ ],
295
+ config,
296
+ );
297
+ },
298
+ onAddToolResult: async ({
299
+ toolCallId,
300
+ toolName: _toolName,
301
+ result,
302
+ isError,
303
+ artifact,
304
+ }) => {
305
+ // TODO parallel human in the loop calls
306
+ const message: A2AMessage = {
307
+ role: "tool",
308
+ tool_call_id: toolCallId,
309
+ content: JSON.stringify(result),
310
+ status: isError
311
+ ? { type: "incomplete", reason: "error" }
312
+ : { type: "complete", reason: "stop" },
313
+ };
314
+ if (artifact) {
315
+ message.artifacts = [artifact] as A2AArtifact[];
316
+ }
317
+ const config: A2ASendMessageConfig = {};
318
+ if (contextId !== undefined) config.contextId = contextId;
319
+ await handleSendMessage(
320
+ [message],
321
+ // TODO reuse runconfig here!
322
+ config,
323
+ );
324
+ },
325
+ onCancel: unstable_allowCancellation
326
+ ? async () => {
327
+ cancel();
328
+ }
329
+ : undefined,
330
+ });
331
+ };