@assistant-ui/react-a2a 0.2.15 → 0.2.17
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.
- package/dist/A2AClient.d.ts +45 -42
- package/dist/A2AClient.d.ts.map +1 -1
- package/dist/A2AClient.js +273 -349
- package/dist/A2AClient.js.map +1 -1
- package/dist/A2AThreadRuntimeCore.d.ts +76 -72
- package/dist/A2AThreadRuntimeCore.d.ts.map +1 -1
- package/dist/A2AThreadRuntimeCore.js +400 -479
- package/dist/A2AThreadRuntimeCore.js.map +1 -1
- package/dist/conversions.d.ts +16 -12
- package/dist/conversions.d.ts.map +1 -1
- package/dist/conversions.js +88 -79
- package/dist/conversions.js.map +1 -1
- package/dist/index.d.ts +5 -8
- package/dist/index.js +5 -8
- package/dist/types.d.ts +229 -228
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +5 -4
- package/dist/types.js.map +1 -1
- package/dist/useA2ARuntime.d.ts +39 -45
- package/dist/useA2ARuntime.d.ts.map +1 -1
- package/dist/useA2ARuntime.js +119 -134
- package/dist/useA2ARuntime.js.map +1 -1
- package/package.json +7 -7
- package/src/A2AClient.test.ts +115 -6
- package/src/A2AClient.ts +37 -7
- package/src/A2AThreadRuntimeCore.test.ts +5 -5
- package/src/conversions.test.ts +12 -14
- package/src/useA2ARuntime.ts +15 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -1,483 +1,404 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { a2aMessageToContent, isTerminalTaskState, taskStateToMessageStatus } from "./conversions.js";
|
|
3
|
+
import { fromThreadMessageLike, generateId } from "@assistant-ui/core/internal";
|
|
4
|
+
//#region src/A2AThreadRuntimeCore.ts
|
|
4
5
|
const FALLBACK_USER_STATUS = {
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
type: "complete",
|
|
7
|
+
reason: "unknown"
|
|
7
8
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if (message.id !== messageId || message.role !== "assistant")
|
|
404
|
-
return message;
|
|
405
|
-
return { ...message, content };
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
updateAssistantStatus(messageId, status) {
|
|
409
|
-
let touched = false;
|
|
410
|
-
this.messages = this.messages.map((message) => {
|
|
411
|
-
if (message.id !== messageId || message.role !== "assistant")
|
|
412
|
-
return message;
|
|
413
|
-
touched = true;
|
|
414
|
-
return { ...message, status };
|
|
415
|
-
});
|
|
416
|
-
if (touched) {
|
|
417
|
-
this.notifyUpdate();
|
|
418
|
-
if (status.type === "complete" || status.type === "incomplete") {
|
|
419
|
-
this.persistAssistantHistory(messageId);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
getAssistantStatus(messageId) {
|
|
424
|
-
const msg = this.messages.find((m) => m.id === messageId && m.role === "assistant");
|
|
425
|
-
return msg?.status;
|
|
426
|
-
}
|
|
427
|
-
// --- Lifecycle helpers ---
|
|
428
|
-
setRunning(running) {
|
|
429
|
-
this.isRunningFlag = running;
|
|
430
|
-
this.notifyUpdate();
|
|
431
|
-
}
|
|
432
|
-
finishRun(controller) {
|
|
433
|
-
if (this.abortController === controller) {
|
|
434
|
-
this.abortController = null;
|
|
435
|
-
}
|
|
436
|
-
this.setRunning(false);
|
|
437
|
-
}
|
|
438
|
-
resetHead(parentId) {
|
|
439
|
-
if (!parentId) {
|
|
440
|
-
if (this.messages.length) {
|
|
441
|
-
this.messages = [];
|
|
442
|
-
}
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const idx = this.messages.findIndex((message) => message.id === parentId);
|
|
446
|
-
if (idx === -1)
|
|
447
|
-
return;
|
|
448
|
-
this.messages = this.messages.slice(0, idx + 1);
|
|
449
|
-
}
|
|
450
|
-
// --- History persistence ---
|
|
451
|
-
recordHistoryEntry(parentId, message) {
|
|
452
|
-
this.appendHistoryItem(parentId, message);
|
|
453
|
-
}
|
|
454
|
-
markPendingAssistantHistory(messageId, parentId) {
|
|
455
|
-
if (!this.history)
|
|
456
|
-
return;
|
|
457
|
-
this.assistantHistoryParents.set(messageId, parentId);
|
|
458
|
-
}
|
|
459
|
-
persistAssistantHistory(messageId) {
|
|
460
|
-
if (!this.history)
|
|
461
|
-
return;
|
|
462
|
-
const parentId = this.assistantHistoryParents.get(messageId);
|
|
463
|
-
if (parentId === undefined)
|
|
464
|
-
return;
|
|
465
|
-
const message = this.messages.find((m) => m.id === messageId);
|
|
466
|
-
if (!message || message.role !== "assistant")
|
|
467
|
-
return;
|
|
468
|
-
if (message.status?.type !== "complete" &&
|
|
469
|
-
message.status?.type !== "incomplete")
|
|
470
|
-
return;
|
|
471
|
-
this.assistantHistoryParents.delete(messageId);
|
|
472
|
-
this.appendHistoryItem(parentId, message);
|
|
473
|
-
}
|
|
474
|
-
appendHistoryItem(parentId, message) {
|
|
475
|
-
if (!this.history || this.recordedHistoryIds.has(message.id))
|
|
476
|
-
return;
|
|
477
|
-
this.recordedHistoryIds.add(message.id);
|
|
478
|
-
void this.history.append({ parentId, message }).catch(() => {
|
|
479
|
-
this.recordedHistoryIds.delete(message.id);
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
}
|
|
9
|
+
var A2AThreadRuntimeCore = class {
|
|
10
|
+
client;
|
|
11
|
+
contextId;
|
|
12
|
+
configuration;
|
|
13
|
+
onError;
|
|
14
|
+
onCancel;
|
|
15
|
+
onArtifactComplete;
|
|
16
|
+
history;
|
|
17
|
+
notifyUpdate;
|
|
18
|
+
runtime;
|
|
19
|
+
messages = [];
|
|
20
|
+
isRunningFlag = false;
|
|
21
|
+
abortController = null;
|
|
22
|
+
pendingError = null;
|
|
23
|
+
currentTask;
|
|
24
|
+
currentArtifacts = [];
|
|
25
|
+
agentCardValue;
|
|
26
|
+
assistantHistoryParents = /* @__PURE__ */ new Map();
|
|
27
|
+
recordedHistoryIds = /* @__PURE__ */ new Set();
|
|
28
|
+
_isLoading = false;
|
|
29
|
+
_loadPromise;
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.client = options.client;
|
|
32
|
+
this.contextId = options.contextId;
|
|
33
|
+
this.configuration = options.configuration;
|
|
34
|
+
this.onError = options.onError;
|
|
35
|
+
this.onCancel = options.onCancel;
|
|
36
|
+
this.onArtifactComplete = options.onArtifactComplete;
|
|
37
|
+
this.history = options.history;
|
|
38
|
+
this.notifyUpdate = options.notifyUpdate;
|
|
39
|
+
}
|
|
40
|
+
updateOptions(options) {
|
|
41
|
+
this.client = options.client;
|
|
42
|
+
this.contextId = options.contextId;
|
|
43
|
+
this.configuration = options.configuration;
|
|
44
|
+
this.onError = options.onError;
|
|
45
|
+
this.onCancel = options.onCancel;
|
|
46
|
+
this.onArtifactComplete = options.onArtifactComplete;
|
|
47
|
+
this.history = options.history;
|
|
48
|
+
}
|
|
49
|
+
attachRuntime(runtime) {
|
|
50
|
+
this.runtime = runtime;
|
|
51
|
+
}
|
|
52
|
+
detachRuntime() {
|
|
53
|
+
this.runtime = void 0;
|
|
54
|
+
if (this.abortController) {
|
|
55
|
+
this.abortController.abort();
|
|
56
|
+
this.abortController = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
getRuntime() {
|
|
60
|
+
return this.runtime;
|
|
61
|
+
}
|
|
62
|
+
getMessages() {
|
|
63
|
+
return this.messages;
|
|
64
|
+
}
|
|
65
|
+
getTask() {
|
|
66
|
+
return this.currentTask;
|
|
67
|
+
}
|
|
68
|
+
getArtifacts() {
|
|
69
|
+
return this.currentArtifacts;
|
|
70
|
+
}
|
|
71
|
+
getAgentCard() {
|
|
72
|
+
return this.agentCardValue;
|
|
73
|
+
}
|
|
74
|
+
isRunning() {
|
|
75
|
+
return this.isRunningFlag;
|
|
76
|
+
}
|
|
77
|
+
get isLoading() {
|
|
78
|
+
return this._isLoading;
|
|
79
|
+
}
|
|
80
|
+
__internal_load() {
|
|
81
|
+
if (this._loadPromise) return this._loadPromise;
|
|
82
|
+
this._isLoading = true;
|
|
83
|
+
const historyPromise = this.history?.load() ?? Promise.resolve(null);
|
|
84
|
+
const agentCardPromise = this.client.getAgentCard().catch(() => void 0);
|
|
85
|
+
this._loadPromise = Promise.all([historyPromise, agentCardPromise]).then(([repo, agentCard]) => {
|
|
86
|
+
if (agentCard) this.agentCardValue = agentCard;
|
|
87
|
+
if (repo) {
|
|
88
|
+
const messages = repo.messages.map((item) => item.message);
|
|
89
|
+
this.applyExternalMessages(messages);
|
|
90
|
+
}
|
|
91
|
+
}).catch((error) => {
|
|
92
|
+
this.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
93
|
+
}).finally(() => {
|
|
94
|
+
this._isLoading = false;
|
|
95
|
+
this.notifyUpdate();
|
|
96
|
+
});
|
|
97
|
+
this.notifyUpdate();
|
|
98
|
+
return this._loadPromise;
|
|
99
|
+
}
|
|
100
|
+
async append(message) {
|
|
101
|
+
const startRun = message.startRun ?? message.role === "user";
|
|
102
|
+
if (message.sourceId) this.messages = this.messages.filter((entry) => entry.id !== message.sourceId);
|
|
103
|
+
this.resetHead(message.parentId);
|
|
104
|
+
const threadMessage = fromThreadMessageLike(message, generateId(), FALLBACK_USER_STATUS);
|
|
105
|
+
this.messages = [...this.messages, threadMessage];
|
|
106
|
+
this.notifyUpdate();
|
|
107
|
+
this.recordHistoryEntry(message.parentId ?? null, threadMessage);
|
|
108
|
+
if (!startRun) return;
|
|
109
|
+
await this.startRun(threadMessage);
|
|
110
|
+
}
|
|
111
|
+
async edit(message) {
|
|
112
|
+
await this.append(message);
|
|
113
|
+
}
|
|
114
|
+
async reload(parentId, _config = {}) {
|
|
115
|
+
this.resetHead(parentId);
|
|
116
|
+
this.notifyUpdate();
|
|
117
|
+
for (let i = this.messages.length - 1; i >= 0; i--) if (this.messages[i].role === "user") {
|
|
118
|
+
await this.startRun(this.messages[i]);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async cancel() {
|
|
123
|
+
if (!this.abortController) return;
|
|
124
|
+
this.abortController.abort();
|
|
125
|
+
if (this.currentTask?.id) try {
|
|
126
|
+
const updated = await this.client.cancelTask(this.currentTask.id);
|
|
127
|
+
this.currentTask = updated;
|
|
128
|
+
} catch {}
|
|
129
|
+
}
|
|
130
|
+
applyExternalMessages(messages) {
|
|
131
|
+
this.assistantHistoryParents.clear();
|
|
132
|
+
this.messages = [...messages];
|
|
133
|
+
this.recordedHistoryIds.clear();
|
|
134
|
+
for (const message of this.messages) this.recordedHistoryIds.add(message.id);
|
|
135
|
+
this.currentTask = void 0;
|
|
136
|
+
this.currentArtifacts = [];
|
|
137
|
+
this.notifyUpdate();
|
|
138
|
+
}
|
|
139
|
+
async startRun(userThreadMessage) {
|
|
140
|
+
if (this.abortController) {
|
|
141
|
+
this.abortController.abort();
|
|
142
|
+
this.abortController = null;
|
|
143
|
+
}
|
|
144
|
+
const a2aMessage = this.threadMessageToA2AMessage(userThreadMessage);
|
|
145
|
+
if (this.currentTask && isTerminalTaskState(this.currentTask.status.state)) this.currentTask = void 0;
|
|
146
|
+
this.currentArtifacts = [];
|
|
147
|
+
const assistantParentId = userThreadMessage.id;
|
|
148
|
+
const assistantId = this.insertAssistantPlaceholder();
|
|
149
|
+
this.markPendingAssistantHistory(assistantId, assistantParentId);
|
|
150
|
+
const abortController = new AbortController();
|
|
151
|
+
this.abortController = abortController;
|
|
152
|
+
abortController.signal.addEventListener("abort", () => {
|
|
153
|
+
this.updateAssistantStatus(assistantId, {
|
|
154
|
+
type: "incomplete",
|
|
155
|
+
reason: "cancelled"
|
|
156
|
+
});
|
|
157
|
+
this.finishRun(abortController);
|
|
158
|
+
this.onCancel?.();
|
|
159
|
+
}, { once: true });
|
|
160
|
+
this.setRunning(true);
|
|
161
|
+
const supportsStreaming = this.agentCardValue?.capabilities?.streaming !== false;
|
|
162
|
+
try {
|
|
163
|
+
if (supportsStreaming) await this.runStreaming(a2aMessage, assistantId, abortController);
|
|
164
|
+
else await this.runSync(a2aMessage, assistantId, abortController);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (!abortController.signal.aborted) {
|
|
167
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
168
|
+
this.updateAssistantStatus(assistantId, {
|
|
169
|
+
type: "incomplete",
|
|
170
|
+
reason: "error"
|
|
171
|
+
});
|
|
172
|
+
this.onError?.(err);
|
|
173
|
+
this.pendingError = this.pendingError ?? err;
|
|
174
|
+
}
|
|
175
|
+
} finally {
|
|
176
|
+
this.finishRun(abortController);
|
|
177
|
+
}
|
|
178
|
+
if (this.pendingError) {
|
|
179
|
+
const err = this.pendingError;
|
|
180
|
+
this.pendingError = null;
|
|
181
|
+
throw err;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async runStreaming(a2aMessage, assistantId, abortController) {
|
|
185
|
+
const stream = this.client.streamMessage(a2aMessage, this.configuration, void 0, abortController.signal);
|
|
186
|
+
for await (const event of stream) {
|
|
187
|
+
if (abortController.signal.aborted) break;
|
|
188
|
+
this.handleStreamEvent(assistantId, event);
|
|
189
|
+
}
|
|
190
|
+
if (!abortController.signal.aborted) {
|
|
191
|
+
if (this.getAssistantStatus(assistantId)?.type === "running") this.updateAssistantStatus(assistantId, {
|
|
192
|
+
type: "complete",
|
|
193
|
+
reason: "stop"
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async runSync(a2aMessage, assistantId, abortController) {
|
|
198
|
+
const result = await this.client.sendMessage(a2aMessage, this.configuration, void 0, abortController.signal);
|
|
199
|
+
if (abortController.signal.aborted) return;
|
|
200
|
+
if ("id" in result && "status" in result) this.handleTaskSnapshot(assistantId, result);
|
|
201
|
+
else if ("messageId" in result && "parts" in result) {
|
|
202
|
+
this.handleMessage(assistantId, result);
|
|
203
|
+
this.updateAssistantStatus(assistantId, {
|
|
204
|
+
type: "complete",
|
|
205
|
+
reason: "stop"
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
handleStreamEvent(assistantId, event) {
|
|
210
|
+
switch (event.type) {
|
|
211
|
+
case "statusUpdate":
|
|
212
|
+
this.handleStatusUpdate(assistantId, event.event);
|
|
213
|
+
break;
|
|
214
|
+
case "artifactUpdate":
|
|
215
|
+
this.handleArtifactUpdate(event.event);
|
|
216
|
+
break;
|
|
217
|
+
case "message":
|
|
218
|
+
this.handleMessage(assistantId, event.message);
|
|
219
|
+
break;
|
|
220
|
+
case "task":
|
|
221
|
+
this.handleTaskSnapshot(assistantId, event.task);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
handleStatusUpdate(assistantId, event) {
|
|
226
|
+
if (!this.currentTask) this.currentTask = {
|
|
227
|
+
id: event.taskId,
|
|
228
|
+
contextId: event.contextId,
|
|
229
|
+
status: event.status
|
|
230
|
+
};
|
|
231
|
+
else this.currentTask = {
|
|
232
|
+
...this.currentTask,
|
|
233
|
+
status: event.status
|
|
234
|
+
};
|
|
235
|
+
if (event.contextId) this.contextId = event.contextId;
|
|
236
|
+
if (event.status.message) {
|
|
237
|
+
const content = a2aMessageToContent(event.status.message);
|
|
238
|
+
this.updateAssistantContent(assistantId, content);
|
|
239
|
+
}
|
|
240
|
+
const status = taskStateToMessageStatus(event.status.state);
|
|
241
|
+
this.updateAssistantStatus(assistantId, status);
|
|
242
|
+
this.notifyUpdate();
|
|
243
|
+
}
|
|
244
|
+
handleArtifactUpdate(event) {
|
|
245
|
+
const { artifact, append, lastChunk } = event;
|
|
246
|
+
const existingIdx = this.currentArtifacts.findIndex((a) => a.artifactId === artifact.artifactId);
|
|
247
|
+
let updated;
|
|
248
|
+
if (existingIdx >= 0 && append) {
|
|
249
|
+
const existing = this.currentArtifacts[existingIdx];
|
|
250
|
+
updated = {
|
|
251
|
+
...existing,
|
|
252
|
+
parts: [...existing.parts, ...artifact.parts]
|
|
253
|
+
};
|
|
254
|
+
this.currentArtifacts = [
|
|
255
|
+
...this.currentArtifacts.slice(0, existingIdx),
|
|
256
|
+
updated,
|
|
257
|
+
...this.currentArtifacts.slice(existingIdx + 1)
|
|
258
|
+
];
|
|
259
|
+
} else if (existingIdx >= 0) {
|
|
260
|
+
updated = artifact;
|
|
261
|
+
this.currentArtifacts = [
|
|
262
|
+
...this.currentArtifacts.slice(0, existingIdx),
|
|
263
|
+
updated,
|
|
264
|
+
...this.currentArtifacts.slice(existingIdx + 1)
|
|
265
|
+
];
|
|
266
|
+
} else {
|
|
267
|
+
updated = artifact;
|
|
268
|
+
this.currentArtifacts = [...this.currentArtifacts, updated];
|
|
269
|
+
}
|
|
270
|
+
if (lastChunk) this.onArtifactComplete?.(updated);
|
|
271
|
+
this.notifyUpdate();
|
|
272
|
+
}
|
|
273
|
+
handleMessage(assistantId, message) {
|
|
274
|
+
if (message.role !== "agent") return;
|
|
275
|
+
const content = a2aMessageToContent(message);
|
|
276
|
+
this.updateAssistantContent(assistantId, content);
|
|
277
|
+
this.notifyUpdate();
|
|
278
|
+
}
|
|
279
|
+
handleTaskSnapshot(assistantId, task) {
|
|
280
|
+
this.currentTask = task;
|
|
281
|
+
if (task.contextId) this.contextId = task.contextId;
|
|
282
|
+
if (task.artifacts) this.currentArtifacts = task.artifacts;
|
|
283
|
+
if (task.status.message) {
|
|
284
|
+
const content = a2aMessageToContent(task.status.message);
|
|
285
|
+
this.updateAssistantContent(assistantId, content);
|
|
286
|
+
}
|
|
287
|
+
const status = taskStateToMessageStatus(task.status.state);
|
|
288
|
+
this.updateAssistantStatus(assistantId, status);
|
|
289
|
+
this.notifyUpdate();
|
|
290
|
+
}
|
|
291
|
+
threadMessageToA2AMessage(message) {
|
|
292
|
+
const parts = [];
|
|
293
|
+
if (message.role === "user") {
|
|
294
|
+
for (const part of message.content) if (part.type === "text") parts.push({ text: part.text });
|
|
295
|
+
else if (part.type === "image") parts.push({
|
|
296
|
+
url: part.image,
|
|
297
|
+
mediaType: "image/*"
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
const a2aMsg = {
|
|
301
|
+
messageId: message.id,
|
|
302
|
+
role: "user",
|
|
303
|
+
parts
|
|
304
|
+
};
|
|
305
|
+
if (this.contextId) a2aMsg.contextId = this.contextId;
|
|
306
|
+
if (this.currentTask?.id && !isTerminalTaskState(this.currentTask.status.state)) a2aMsg.taskId = this.currentTask.id;
|
|
307
|
+
return a2aMsg;
|
|
308
|
+
}
|
|
309
|
+
insertAssistantPlaceholder() {
|
|
310
|
+
const id = generateId();
|
|
311
|
+
const assistant = {
|
|
312
|
+
id,
|
|
313
|
+
role: "assistant",
|
|
314
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
315
|
+
status: { type: "running" },
|
|
316
|
+
content: [],
|
|
317
|
+
metadata: {
|
|
318
|
+
unstable_state: null,
|
|
319
|
+
unstable_annotations: [],
|
|
320
|
+
unstable_data: [],
|
|
321
|
+
steps: [],
|
|
322
|
+
custom: {}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
this.messages = [...this.messages, assistant];
|
|
326
|
+
this.notifyUpdate();
|
|
327
|
+
return id;
|
|
328
|
+
}
|
|
329
|
+
updateAssistantContent(messageId, content) {
|
|
330
|
+
this.messages = this.messages.map((message) => {
|
|
331
|
+
if (message.id !== messageId || message.role !== "assistant") return message;
|
|
332
|
+
return {
|
|
333
|
+
...message,
|
|
334
|
+
content
|
|
335
|
+
};
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
updateAssistantStatus(messageId, status) {
|
|
339
|
+
let touched = false;
|
|
340
|
+
this.messages = this.messages.map((message) => {
|
|
341
|
+
if (message.id !== messageId || message.role !== "assistant") return message;
|
|
342
|
+
touched = true;
|
|
343
|
+
return {
|
|
344
|
+
...message,
|
|
345
|
+
status
|
|
346
|
+
};
|
|
347
|
+
});
|
|
348
|
+
if (touched) {
|
|
349
|
+
this.notifyUpdate();
|
|
350
|
+
if (status.type === "complete" || status.type === "incomplete") this.persistAssistantHistory(messageId);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
getAssistantStatus(messageId) {
|
|
354
|
+
return this.messages.find((m) => m.id === messageId && m.role === "assistant")?.status;
|
|
355
|
+
}
|
|
356
|
+
setRunning(running) {
|
|
357
|
+
this.isRunningFlag = running;
|
|
358
|
+
this.notifyUpdate();
|
|
359
|
+
}
|
|
360
|
+
finishRun(controller) {
|
|
361
|
+
if (this.abortController === controller) this.abortController = null;
|
|
362
|
+
this.setRunning(false);
|
|
363
|
+
}
|
|
364
|
+
resetHead(parentId) {
|
|
365
|
+
if (!parentId) {
|
|
366
|
+
if (this.messages.length) this.messages = [];
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const idx = this.messages.findIndex((message) => message.id === parentId);
|
|
370
|
+
if (idx === -1) return;
|
|
371
|
+
this.messages = this.messages.slice(0, idx + 1);
|
|
372
|
+
}
|
|
373
|
+
recordHistoryEntry(parentId, message) {
|
|
374
|
+
this.appendHistoryItem(parentId, message);
|
|
375
|
+
}
|
|
376
|
+
markPendingAssistantHistory(messageId, parentId) {
|
|
377
|
+
if (!this.history) return;
|
|
378
|
+
this.assistantHistoryParents.set(messageId, parentId);
|
|
379
|
+
}
|
|
380
|
+
persistAssistantHistory(messageId) {
|
|
381
|
+
if (!this.history) return;
|
|
382
|
+
const parentId = this.assistantHistoryParents.get(messageId);
|
|
383
|
+
if (parentId === void 0) return;
|
|
384
|
+
const message = this.messages.find((m) => m.id === messageId);
|
|
385
|
+
if (!message || message.role !== "assistant") return;
|
|
386
|
+
if (message.status?.type !== "complete" && message.status?.type !== "incomplete") return;
|
|
387
|
+
this.assistantHistoryParents.delete(messageId);
|
|
388
|
+
this.appendHistoryItem(parentId, message);
|
|
389
|
+
}
|
|
390
|
+
appendHistoryItem(parentId, message) {
|
|
391
|
+
if (!this.history || this.recordedHistoryIds.has(message.id)) return;
|
|
392
|
+
this.recordedHistoryIds.add(message.id);
|
|
393
|
+
this.history.append({
|
|
394
|
+
parentId,
|
|
395
|
+
message
|
|
396
|
+
}).catch(() => {
|
|
397
|
+
this.recordedHistoryIds.delete(message.id);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
//#endregion
|
|
402
|
+
export { A2AThreadRuntimeCore };
|
|
403
|
+
|
|
483
404
|
//# sourceMappingURL=A2AThreadRuntimeCore.js.map
|