@assistant-ui/react-ag-ui 0.0.31 → 0.0.32
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/index.d.ts +3 -4
- package/dist/index.js +2 -2
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts +85 -82
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtime/AgUiThreadRuntimeCore.js +542 -628
- package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts +39 -36
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +294 -390
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/adapter/run-aggregator.d.ts +54 -51
- package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
- package/dist/runtime/adapter/run-aggregator.js +367 -413
- package/dist/runtime/adapter/run-aggregator.js.map +1 -1
- package/dist/runtime/adapter/subscriber.d.ts +94 -91
- package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
- package/dist/runtime/adapter/subscriber.js +77 -86
- package/dist/runtime/adapter/subscriber.js.map +1 -1
- package/dist/runtime/event-parser.d.ts +9 -5
- package/dist/runtime/event-parser.d.ts.map +1 -1
- package/dist/runtime/event-parser.js +172 -177
- package/dist/runtime/event-parser.js.map +1 -1
- package/dist/runtime/logger.d.ts +7 -4
- package/dist/runtime/logger.d.ts.map +1 -1
- package/dist/runtime/logger.js +7 -3
- package/dist/runtime/logger.js.map +1 -1
- package/dist/runtime/types.d.ts +121 -117
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js +0 -2
- package/dist/useAgUiRuntime.d.ts +10 -6
- package/dist/useAgUiRuntime.d.ts.map +1 -1
- package/dist/useAgUiRuntime.js +136 -142
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +7 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/runtime/types.js.map +0 -1
|
@@ -1,632 +1,546 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { fromAgUiMessages, toAgUiMessages, toAgUiTools, } from "./adapter/conversions.js";
|
|
2
|
+
import { AG_UI_METADATA_NAMESPACE, RunAggregator } from "./adapter/run-aggregator.js";
|
|
3
|
+
import { fromAgUiMessages, toAgUiMessages, toAgUiTools } from "./adapter/conversions.js";
|
|
5
4
|
import { createAgUiSubscriber } from "./adapter/subscriber.js";
|
|
5
|
+
import { fromThreadMessageLike, generateId, generateOptimisticId, isOptimisticId } from "@assistant-ui/core/internal";
|
|
6
|
+
//#region src/runtime/AgUiThreadRuntimeCore.ts
|
|
6
7
|
const symbolResumeShim = Symbol("agui-resume-shim");
|
|
7
|
-
const FALLBACK_USER_STATUS = {
|
|
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
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
case "STATE_DELTA": {
|
|
548
|
-
this.logger.debug?.("[agui] state delta event ignored", event.delta);
|
|
549
|
-
return;
|
|
550
|
-
}
|
|
551
|
-
case "MESSAGES_SNAPSHOT": {
|
|
552
|
-
this.importMessagesSnapshot(event.messages);
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
default:
|
|
556
|
-
aggregator.handle(event);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
importMessagesSnapshot(rawMessages) {
|
|
560
|
-
try {
|
|
561
|
-
const normalized = fromAgUiMessages(rawMessages);
|
|
562
|
-
const converted = [];
|
|
563
|
-
for (const message of normalized) {
|
|
564
|
-
try {
|
|
565
|
-
converted.push(fromThreadMessageLike(message, generateId(), FALLBACK_USER_STATUS));
|
|
566
|
-
}
|
|
567
|
-
catch (error) {
|
|
568
|
-
this.logger.error?.("[agui] failed to import message from snapshot", error);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
this.applyExternalMessages(converted);
|
|
572
|
-
}
|
|
573
|
-
catch (error) {
|
|
574
|
-
this.logger.error?.("[agui] failed to import messages snapshot", error);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
toThreadMessage(message) {
|
|
578
|
-
return fromThreadMessageLike(message, generateId(), FALLBACK_USER_STATUS);
|
|
579
|
-
}
|
|
580
|
-
resetHead(parentId) {
|
|
581
|
-
if (!parentId) {
|
|
582
|
-
if (this.messages.length) {
|
|
583
|
-
this.messages = [];
|
|
584
|
-
}
|
|
585
|
-
return;
|
|
586
|
-
}
|
|
587
|
-
const idx = this.messages.findIndex((message) => message.id === parentId);
|
|
588
|
-
if (idx === -1)
|
|
589
|
-
return;
|
|
590
|
-
this.messages = this.messages.slice(0, idx + 1);
|
|
591
|
-
}
|
|
592
|
-
isTerminalStatus(status) {
|
|
593
|
-
return status?.type === "complete" || status?.type === "incomplete";
|
|
594
|
-
}
|
|
595
|
-
isPersistableStatus(status) {
|
|
596
|
-
if (this.isTerminalStatus(status))
|
|
597
|
-
return true;
|
|
598
|
-
return status?.type === "requires-action" && status.reason === "interrupt";
|
|
599
|
-
}
|
|
600
|
-
recordHistoryEntry(parentId, message) {
|
|
601
|
-
this.appendHistoryItem(parentId, message);
|
|
602
|
-
}
|
|
603
|
-
markPendingAssistantHistory(messageId, parentId) {
|
|
604
|
-
if (!this.history)
|
|
605
|
-
return;
|
|
606
|
-
this.assistantHistoryParents.set(messageId, parentId);
|
|
607
|
-
}
|
|
608
|
-
persistAssistantHistory(messageId) {
|
|
609
|
-
if (!this.history)
|
|
610
|
-
return;
|
|
611
|
-
const parentId = this.assistantHistoryParents.get(messageId);
|
|
612
|
-
if (parentId === undefined)
|
|
613
|
-
return;
|
|
614
|
-
const message = this.messages.find((m) => m.id === messageId);
|
|
615
|
-
if (!message || message.role !== "assistant")
|
|
616
|
-
return;
|
|
617
|
-
if (!this.isPersistableStatus(message.status))
|
|
618
|
-
return;
|
|
619
|
-
this.assistantHistoryParents.delete(messageId);
|
|
620
|
-
this.appendHistoryItem(parentId, message);
|
|
621
|
-
}
|
|
622
|
-
appendHistoryItem(parentId, message) {
|
|
623
|
-
if (!this.history || this.recordedHistoryIds.has(message.id))
|
|
624
|
-
return;
|
|
625
|
-
this.recordedHistoryIds.add(message.id);
|
|
626
|
-
void this.history.append({ parentId, message }).catch((error) => {
|
|
627
|
-
this.recordedHistoryIds.delete(message.id);
|
|
628
|
-
this.logger.error?.("[agui] failed to append history entry", error);
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
}
|
|
8
|
+
const FALLBACK_USER_STATUS = {
|
|
9
|
+
type: "complete",
|
|
10
|
+
reason: "unknown"
|
|
11
|
+
};
|
|
12
|
+
var AgUiThreadRuntimeCore = class {
|
|
13
|
+
agent;
|
|
14
|
+
logger;
|
|
15
|
+
showThinking;
|
|
16
|
+
onError;
|
|
17
|
+
onCancel;
|
|
18
|
+
notifyUpdate;
|
|
19
|
+
runtime;
|
|
20
|
+
messages = [];
|
|
21
|
+
isRunningFlag = false;
|
|
22
|
+
abortController = null;
|
|
23
|
+
stateSnapshot;
|
|
24
|
+
pendingError = null;
|
|
25
|
+
history;
|
|
26
|
+
lastRunConfig;
|
|
27
|
+
assistantHistoryParents = /* @__PURE__ */ new Map();
|
|
28
|
+
recordedHistoryIds = /* @__PURE__ */ new Set();
|
|
29
|
+
_isLoading = false;
|
|
30
|
+
_loadPromise;
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.agent = options.agent;
|
|
33
|
+
this.logger = options.logger;
|
|
34
|
+
this.showThinking = options.showThinking;
|
|
35
|
+
this.onError = options.onError;
|
|
36
|
+
this.onCancel = options.onCancel;
|
|
37
|
+
this.history = options.history;
|
|
38
|
+
this.notifyUpdate = options.notifyUpdate;
|
|
39
|
+
this.installResumeShim();
|
|
40
|
+
}
|
|
41
|
+
updateOptions(options) {
|
|
42
|
+
this.agent = options.agent;
|
|
43
|
+
this.logger = options.logger;
|
|
44
|
+
this.showThinking = options.showThinking;
|
|
45
|
+
this.onError = options.onError;
|
|
46
|
+
this.onCancel = options.onCancel;
|
|
47
|
+
this.history = options.history;
|
|
48
|
+
this.installResumeShim();
|
|
49
|
+
}
|
|
50
|
+
attachRuntime(runtime) {
|
|
51
|
+
this.runtime = runtime;
|
|
52
|
+
}
|
|
53
|
+
detachRuntime() {
|
|
54
|
+
this.runtime = void 0;
|
|
55
|
+
}
|
|
56
|
+
getMessages() {
|
|
57
|
+
return this.messages;
|
|
58
|
+
}
|
|
59
|
+
getState() {
|
|
60
|
+
return this.stateSnapshot;
|
|
61
|
+
}
|
|
62
|
+
isRunning() {
|
|
63
|
+
return this.isRunningFlag;
|
|
64
|
+
}
|
|
65
|
+
get isLoading() {
|
|
66
|
+
return this._isLoading;
|
|
67
|
+
}
|
|
68
|
+
__internal_load() {
|
|
69
|
+
if (this._loadPromise) return this._loadPromise;
|
|
70
|
+
const promise = this.history?.load() ?? Promise.resolve(null);
|
|
71
|
+
this._isLoading = true;
|
|
72
|
+
this._loadPromise = promise.then(async (repo) => {
|
|
73
|
+
if (!repo) return;
|
|
74
|
+
const messages = repo.messages.map((item) => item.message);
|
|
75
|
+
this.applyExternalMessages(messages);
|
|
76
|
+
if (repo.unstable_resume) {
|
|
77
|
+
const parentId = repo.headId ?? messages.at(-1)?.id ?? null;
|
|
78
|
+
await this.startRun(parentId, this.lastRunConfig);
|
|
79
|
+
}
|
|
80
|
+
}).catch((error) => {
|
|
81
|
+
this.logger.error?.("[agui] failed to load history", error);
|
|
82
|
+
this.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
83
|
+
}).finally(() => {
|
|
84
|
+
this._isLoading = false;
|
|
85
|
+
this.notifyUpdate();
|
|
86
|
+
});
|
|
87
|
+
this.notifyUpdate();
|
|
88
|
+
return this._loadPromise;
|
|
89
|
+
}
|
|
90
|
+
async append(message) {
|
|
91
|
+
const startRun = message.startRun ?? message.role === "user";
|
|
92
|
+
if (startRun) this.assertNoPendingInterrupts();
|
|
93
|
+
if (message.sourceId) this.messages = this.messages.filter((entry) => entry.id !== message.sourceId);
|
|
94
|
+
this.resetHead(message.parentId);
|
|
95
|
+
const threadMessage = this.toThreadMessage(message);
|
|
96
|
+
this.messages = [...this.messages, threadMessage];
|
|
97
|
+
this.notifyUpdate();
|
|
98
|
+
this.recordHistoryEntry(message.parentId ?? null, threadMessage);
|
|
99
|
+
if (!startRun) return;
|
|
100
|
+
await this.startRun(threadMessage.id, message.runConfig);
|
|
101
|
+
}
|
|
102
|
+
async edit(message) {
|
|
103
|
+
await this.append(message);
|
|
104
|
+
}
|
|
105
|
+
async reload(parentId, config = {}) {
|
|
106
|
+
this.assertNoPendingInterrupts();
|
|
107
|
+
this.resetHead(parentId);
|
|
108
|
+
this.notifyUpdate();
|
|
109
|
+
await this.startRun(parentId, config.runConfig);
|
|
110
|
+
}
|
|
111
|
+
async cancel() {
|
|
112
|
+
if (!this.abortController) return;
|
|
113
|
+
this.abortController.abort();
|
|
114
|
+
}
|
|
115
|
+
async resume(config) {
|
|
116
|
+
this.assertNoPendingInterrupts();
|
|
117
|
+
if (config.stream) this.logger.debug?.("[agui] resume stream is not supported, falling back to regular run");
|
|
118
|
+
await this.startRun(config.parentId, config.runConfig ?? this.lastRunConfig);
|
|
119
|
+
}
|
|
120
|
+
assertNoPendingInterrupts() {
|
|
121
|
+
if (!this.getPendingInterrupts()) return;
|
|
122
|
+
throw new Error("[agui] cannot start a new run while interrupts are pending; resolve them with submitInterruptResponses()");
|
|
123
|
+
}
|
|
124
|
+
getPendingInterrupts() {
|
|
125
|
+
const assistant = this.messages.findLast((m) => m.role === "assistant");
|
|
126
|
+
if (!assistant || assistant.status?.type !== "requires-action" || assistant.status.reason !== "interrupt") return null;
|
|
127
|
+
const stored = assistant.metadata.custom[AG_UI_METADATA_NAMESPACE]?.interrupts;
|
|
128
|
+
if (!stored?.length) return null;
|
|
129
|
+
return {
|
|
130
|
+
messageId: assistant.id,
|
|
131
|
+
interrupts: stored
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async submitInterruptResponses(responses) {
|
|
135
|
+
const pending = this.getPendingInterrupts();
|
|
136
|
+
if (!pending) throw new Error("[agui] submitInterruptResponses: no pending interrupts on this thread");
|
|
137
|
+
const responsesById = /* @__PURE__ */ new Map();
|
|
138
|
+
for (const entry of responses) {
|
|
139
|
+
if (!entry || typeof entry.interruptId !== "string") throw new Error("[agui] submitInterruptResponses: every entry must have an interruptId");
|
|
140
|
+
if (entry.status !== "resolved" && entry.status !== "cancelled") throw new Error(`[agui] submitInterruptResponses: invalid status "${entry.status}" for interrupt ${entry.interruptId}`);
|
|
141
|
+
if (responsesById.has(entry.interruptId)) throw new Error(`[agui] submitInterruptResponses: duplicate response for interrupt ${entry.interruptId}`);
|
|
142
|
+
responsesById.set(entry.interruptId, entry);
|
|
143
|
+
}
|
|
144
|
+
const openIds = pending.interrupts.map((i) => i.id);
|
|
145
|
+
const missing = openIds.filter((id) => !responsesById.has(id));
|
|
146
|
+
if (missing.length > 0) throw new Error(`[agui] submitInterruptResponses: missing responses for open interrupts: ${missing.join(", ")}`);
|
|
147
|
+
const known = new Set(openIds);
|
|
148
|
+
const unknownIds = [...responsesById.keys()].filter((id) => !known.has(id));
|
|
149
|
+
if (unknownIds.length > 0) throw new Error(`[agui] submitInterruptResponses: unknown interrupt ids: ${unknownIds.join(", ")}`);
|
|
150
|
+
const now = Date.now();
|
|
151
|
+
for (const interrupt of pending.interrupts) {
|
|
152
|
+
if (!interrupt.expiresAt) continue;
|
|
153
|
+
const expiry = new Date(interrupt.expiresAt).getTime();
|
|
154
|
+
if (Number.isNaN(expiry)) throw new Error(`[agui] submitInterruptResponses: interrupt ${interrupt.id} has malformed expiresAt "${interrupt.expiresAt}"`);
|
|
155
|
+
if (expiry <= now) throw new Error(`[agui] submitInterruptResponses: interrupt ${interrupt.id} expired at ${interrupt.expiresAt}`);
|
|
156
|
+
}
|
|
157
|
+
const resume = openIds.map((id) => responsesById.get(id));
|
|
158
|
+
if (this.isRunningFlag) throw new Error("[agui] submitInterruptResponses: a run is already in progress");
|
|
159
|
+
this.clearPendingInterrupts(pending.messageId);
|
|
160
|
+
await this.startRun(pending.messageId, this.lastRunConfig, resume);
|
|
161
|
+
}
|
|
162
|
+
clearPendingInterrupts(messageId) {
|
|
163
|
+
let touched = false;
|
|
164
|
+
this.messages = this.messages.map((message) => {
|
|
165
|
+
if (message.id !== messageId || message.role !== "assistant") return message;
|
|
166
|
+
const assistant = message;
|
|
167
|
+
if (assistant.status?.type !== "requires-action" || assistant.status.reason !== "interrupt") return assistant;
|
|
168
|
+
touched = true;
|
|
169
|
+
const { interrupts: _drop, ...restAgui } = assistant.metadata.custom["agui"] ?? {};
|
|
170
|
+
const newCustom = { ...assistant.metadata.custom };
|
|
171
|
+
if (Object.keys(restAgui).length > 0) newCustom[AG_UI_METADATA_NAMESPACE] = restAgui;
|
|
172
|
+
else delete newCustom[AG_UI_METADATA_NAMESPACE];
|
|
173
|
+
return {
|
|
174
|
+
...assistant,
|
|
175
|
+
status: {
|
|
176
|
+
type: "complete",
|
|
177
|
+
reason: "unknown"
|
|
178
|
+
},
|
|
179
|
+
metadata: {
|
|
180
|
+
...assistant.metadata,
|
|
181
|
+
custom: newCustom
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
if (touched) this.notifyUpdate();
|
|
186
|
+
}
|
|
187
|
+
findMessageIdForToolCall(toolCallId) {
|
|
188
|
+
let fallbackMessageId;
|
|
189
|
+
for (let index = this.messages.length - 1; index >= 0; index--) {
|
|
190
|
+
const message = this.messages[index];
|
|
191
|
+
if (!message || message.role !== "assistant") continue;
|
|
192
|
+
for (const part of message.content) {
|
|
193
|
+
if (part.type !== "tool-call" || part.toolCallId !== toolCallId) continue;
|
|
194
|
+
if (!("result" in part) || part.result === void 0) return message.id;
|
|
195
|
+
fallbackMessageId ??= message.id;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return fallbackMessageId;
|
|
199
|
+
}
|
|
200
|
+
addToolResult(options) {
|
|
201
|
+
let updated = false;
|
|
202
|
+
let shouldResume = false;
|
|
203
|
+
this.messages = this.messages.map((message) => {
|
|
204
|
+
if (message.id !== options.messageId || message.role !== "assistant") return message;
|
|
205
|
+
const assistant = message;
|
|
206
|
+
let matchedToolCall = false;
|
|
207
|
+
const content = assistant.content.map((part) => {
|
|
208
|
+
if (part.type !== "tool-call" || part.toolCallId !== options.toolCallId) return part;
|
|
209
|
+
matchedToolCall = true;
|
|
210
|
+
return {
|
|
211
|
+
...part,
|
|
212
|
+
result: options.result,
|
|
213
|
+
artifact: options.artifact,
|
|
214
|
+
isError: options.isError
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
if (!matchedToolCall) return message;
|
|
218
|
+
updated = true;
|
|
219
|
+
if (assistant.status?.type === "requires-action" && assistant.status.reason === "tool-calls" && content.every((part) => part.type !== "tool-call" || "result" in part && part.result !== void 0)) {
|
|
220
|
+
shouldResume = true;
|
|
221
|
+
return {
|
|
222
|
+
...assistant,
|
|
223
|
+
content,
|
|
224
|
+
status: {
|
|
225
|
+
type: "complete",
|
|
226
|
+
reason: "unknown"
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
...assistant,
|
|
232
|
+
content
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
if (updated) {
|
|
236
|
+
this.notifyUpdate();
|
|
237
|
+
if (shouldResume) {
|
|
238
|
+
this.persistAssistantHistory(options.messageId);
|
|
239
|
+
if (!this.isRunningFlag) this.startRun(options.messageId, this.lastRunConfig).catch((error) => {
|
|
240
|
+
this.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
applyExternalMessages(messages) {
|
|
246
|
+
this.assistantHistoryParents.clear();
|
|
247
|
+
this.messages = [...messages];
|
|
248
|
+
this.recordedHistoryIds.clear();
|
|
249
|
+
for (const message of this.messages) this.recordedHistoryIds.add(message.id);
|
|
250
|
+
this.notifyUpdate();
|
|
251
|
+
}
|
|
252
|
+
loadExternalState(state) {
|
|
253
|
+
this.stateSnapshot = state;
|
|
254
|
+
this.notifyUpdate();
|
|
255
|
+
}
|
|
256
|
+
async startRun(parentId, runConfig, resume) {
|
|
257
|
+
const normalizedRunConfig = runConfig ?? {};
|
|
258
|
+
this.lastRunConfig = normalizedRunConfig;
|
|
259
|
+
this.resetHead(parentId);
|
|
260
|
+
const historicalMessages = [...this.messages];
|
|
261
|
+
const runId = generateId();
|
|
262
|
+
this.pendingError = null;
|
|
263
|
+
const input = this.buildRunInput(runId, normalizedRunConfig, historicalMessages, resume);
|
|
264
|
+
const assistantParentId = parentId ?? this.messages.at(-1)?.id ?? null;
|
|
265
|
+
let assistantMessageId;
|
|
266
|
+
const ensureAssistant = () => {
|
|
267
|
+
if (assistantMessageId) return assistantMessageId;
|
|
268
|
+
const created = this.insertAssistantPlaceholder();
|
|
269
|
+
assistantMessageId = created;
|
|
270
|
+
this.markPendingAssistantHistory(created, assistantParentId ?? null);
|
|
271
|
+
return created;
|
|
272
|
+
};
|
|
273
|
+
const aggregator = new RunAggregator({
|
|
274
|
+
showThinking: this.showThinking,
|
|
275
|
+
logger: this.logger,
|
|
276
|
+
emit: (update) => {
|
|
277
|
+
const resolved = this.updateAssistantMessage(ensureAssistant(), update);
|
|
278
|
+
if (resolved !== assistantMessageId) assistantMessageId = resolved;
|
|
279
|
+
},
|
|
280
|
+
onServerMessageId: (serverId) => {
|
|
281
|
+
const placeholder = ensureAssistant();
|
|
282
|
+
if (placeholder === serverId) return;
|
|
283
|
+
this.reassignAssistantId(placeholder, serverId);
|
|
284
|
+
assistantMessageId = serverId;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
const dispatch = (event) => this.handleEvent(aggregator, event);
|
|
288
|
+
const abortController = new AbortController();
|
|
289
|
+
const abortSignal = abortController.signal;
|
|
290
|
+
this.abortController = abortController;
|
|
291
|
+
abortSignal.addEventListener("abort", () => {
|
|
292
|
+
dispatch({ type: "RUN_CANCELLED" });
|
|
293
|
+
this.finishRun(abortController);
|
|
294
|
+
this.onCancel?.();
|
|
295
|
+
}, { once: true });
|
|
296
|
+
const subscriber = createAgUiSubscriber({
|
|
297
|
+
dispatch,
|
|
298
|
+
runId,
|
|
299
|
+
logger: this.logger,
|
|
300
|
+
onRunFailed: (error) => {
|
|
301
|
+
this.pendingError = error;
|
|
302
|
+
this.onError?.(error);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
aggregator.handle({
|
|
306
|
+
type: "RUN_STARTED",
|
|
307
|
+
runId
|
|
308
|
+
});
|
|
309
|
+
this.setRunning(true);
|
|
310
|
+
try {
|
|
311
|
+
try {
|
|
312
|
+
this.agent.messages = input.messages;
|
|
313
|
+
this.agent.threadId = input.threadId;
|
|
314
|
+
this.agent.state = input.state ?? null;
|
|
315
|
+
} catch {}
|
|
316
|
+
await this.agent.runAgent(input, subscriber, { signal: abortSignal });
|
|
317
|
+
} catch (error) {
|
|
318
|
+
if (!abortSignal.aborted) {
|
|
319
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
320
|
+
dispatch({
|
|
321
|
+
type: "RUN_ERROR",
|
|
322
|
+
message: err.message
|
|
323
|
+
});
|
|
324
|
+
this.onError?.(err);
|
|
325
|
+
this.pendingError = this.pendingError ?? err;
|
|
326
|
+
}
|
|
327
|
+
} finally {
|
|
328
|
+
this.finishRun(abortController);
|
|
329
|
+
}
|
|
330
|
+
if (this.pendingError) {
|
|
331
|
+
const err = this.pendingError;
|
|
332
|
+
this.pendingError = null;
|
|
333
|
+
throw err;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
buildRunInput(runId, runConfig, historyMessages, resume) {
|
|
337
|
+
const threadId = this.agent.threadId || "main";
|
|
338
|
+
const messages = toAgUiMessages(historyMessages ?? this.messages);
|
|
339
|
+
const context = this.runtime?.thread.getModelContext();
|
|
340
|
+
return {
|
|
341
|
+
threadId,
|
|
342
|
+
runId,
|
|
343
|
+
state: this.stateSnapshot ?? null,
|
|
344
|
+
messages,
|
|
345
|
+
tools: toAgUiTools(context?.tools),
|
|
346
|
+
context: context?.system ? [{
|
|
347
|
+
description: "system",
|
|
348
|
+
value: context.system
|
|
349
|
+
}] : [],
|
|
350
|
+
forwardedProps: {
|
|
351
|
+
...context?.callSettings ?? {},
|
|
352
|
+
...context?.config ?? {},
|
|
353
|
+
...runConfig?.custom ? { runConfig: runConfig.custom } : {}
|
|
354
|
+
},
|
|
355
|
+
...resume !== void 0 ? { resume } : {}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
installResumeShim() {
|
|
359
|
+
const agent = this.agent;
|
|
360
|
+
if (agent[symbolResumeShim]) return;
|
|
361
|
+
agent[symbolResumeShim] = true;
|
|
362
|
+
const original = Object.hasOwn(agent, "prepareRunAgentInput") ? agent.prepareRunAgentInput : Object.getPrototypeOf(agent)?.prepareRunAgentInput;
|
|
363
|
+
if (typeof original !== "function") return;
|
|
364
|
+
agent.prepareRunAgentInput = function(params) {
|
|
365
|
+
const input = original.call(this, params);
|
|
366
|
+
if (params?.resume !== void 0 && input && typeof input === "object") return {
|
|
367
|
+
...input,
|
|
368
|
+
resume: params.resume
|
|
369
|
+
};
|
|
370
|
+
return input;
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
setRunning(running) {
|
|
374
|
+
this.isRunningFlag = running;
|
|
375
|
+
this.notifyUpdate();
|
|
376
|
+
}
|
|
377
|
+
finishRun(controller) {
|
|
378
|
+
if (this.abortController === controller) this.abortController = null;
|
|
379
|
+
this.setRunning(false);
|
|
380
|
+
}
|
|
381
|
+
insertAssistantPlaceholder() {
|
|
382
|
+
const id = generateOptimisticId();
|
|
383
|
+
const assistant = {
|
|
384
|
+
id,
|
|
385
|
+
role: "assistant",
|
|
386
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
387
|
+
status: { type: "running" },
|
|
388
|
+
content: [],
|
|
389
|
+
metadata: {
|
|
390
|
+
unstable_state: this.stateSnapshot ?? null,
|
|
391
|
+
unstable_annotations: [],
|
|
392
|
+
unstable_data: [],
|
|
393
|
+
steps: [],
|
|
394
|
+
custom: {}
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
this.messages = [...this.messages, assistant];
|
|
398
|
+
this.notifyUpdate();
|
|
399
|
+
return id;
|
|
400
|
+
}
|
|
401
|
+
reassignAssistantId(oldId, newId) {
|
|
402
|
+
if (oldId === newId) return;
|
|
403
|
+
if (this.messages.some((m) => m.id === newId)) {
|
|
404
|
+
this.logger.debug?.("[agui] reassignAssistantId: server id already present in messages, dropping placeholder", {
|
|
405
|
+
oldId,
|
|
406
|
+
newId
|
|
407
|
+
});
|
|
408
|
+
this.messages = this.messages.filter((m) => m.id !== oldId);
|
|
409
|
+
} else this.messages = this.messages.map((m) => m.id === oldId ? {
|
|
410
|
+
...m,
|
|
411
|
+
id: newId
|
|
412
|
+
} : m);
|
|
413
|
+
const pendingParent = this.assistantHistoryParents.get(oldId);
|
|
414
|
+
if (pendingParent !== void 0) {
|
|
415
|
+
this.assistantHistoryParents.delete(oldId);
|
|
416
|
+
if (!this.assistantHistoryParents.has(newId)) this.assistantHistoryParents.set(newId, pendingParent);
|
|
417
|
+
}
|
|
418
|
+
if (this.recordedHistoryIds.has(oldId)) {
|
|
419
|
+
this.recordedHistoryIds.delete(oldId);
|
|
420
|
+
this.recordedHistoryIds.add(newId);
|
|
421
|
+
}
|
|
422
|
+
this.notifyUpdate();
|
|
423
|
+
}
|
|
424
|
+
updateAssistantMessage(messageId, update) {
|
|
425
|
+
let touched = false;
|
|
426
|
+
let latestStatus;
|
|
427
|
+
this.messages = this.messages.map((message) => {
|
|
428
|
+
if (message.id !== messageId || message.role !== "assistant") return message;
|
|
429
|
+
touched = true;
|
|
430
|
+
const assistant = message;
|
|
431
|
+
const metadata = update.metadata ? this.mergeAssistantMetadata(assistant.metadata, update.metadata) : assistant.metadata;
|
|
432
|
+
latestStatus = update.status ?? assistant.status;
|
|
433
|
+
return {
|
|
434
|
+
...assistant,
|
|
435
|
+
content: update.content ?? assistant.content,
|
|
436
|
+
status: latestStatus,
|
|
437
|
+
metadata
|
|
438
|
+
};
|
|
439
|
+
});
|
|
440
|
+
if (!touched) return messageId;
|
|
441
|
+
let resolvedMessageId = messageId;
|
|
442
|
+
if (latestStatus !== void 0 && latestStatus.type !== "running" && isOptimisticId(messageId)) {
|
|
443
|
+
const stableId = generateId();
|
|
444
|
+
this.reassignAssistantId(messageId, stableId);
|
|
445
|
+
resolvedMessageId = stableId;
|
|
446
|
+
} else this.notifyUpdate();
|
|
447
|
+
if (this.isPersistableStatus(latestStatus)) this.persistAssistantHistory(resolvedMessageId);
|
|
448
|
+
return resolvedMessageId;
|
|
449
|
+
}
|
|
450
|
+
mergeAssistantMetadata(current, incoming) {
|
|
451
|
+
const annotations = incoming.unstable_annotations ? [...current.unstable_annotations, ...incoming.unstable_annotations] : current.unstable_annotations;
|
|
452
|
+
const data = incoming.unstable_data ? [...current.unstable_data, ...incoming.unstable_data] : current.unstable_data;
|
|
453
|
+
const steps = incoming.steps ? [...current.steps, ...incoming.steps] : current.steps;
|
|
454
|
+
return {
|
|
455
|
+
unstable_state: incoming.unstable_state !== void 0 ? incoming.unstable_state : current.unstable_state,
|
|
456
|
+
unstable_annotations: annotations,
|
|
457
|
+
unstable_data: data,
|
|
458
|
+
steps,
|
|
459
|
+
...incoming.timing ? { timing: incoming.timing } : {},
|
|
460
|
+
custom: incoming.custom ? {
|
|
461
|
+
...current.custom,
|
|
462
|
+
...incoming.custom
|
|
463
|
+
} : current.custom
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
handleEvent(aggregator, event) {
|
|
467
|
+
switch (event.type) {
|
|
468
|
+
case "STATE_SNAPSHOT":
|
|
469
|
+
this.stateSnapshot = event.snapshot;
|
|
470
|
+
this.notifyUpdate();
|
|
471
|
+
return;
|
|
472
|
+
case "STATE_DELTA":
|
|
473
|
+
this.logger.debug?.("[agui] state delta event ignored", event.delta);
|
|
474
|
+
return;
|
|
475
|
+
case "MESSAGES_SNAPSHOT":
|
|
476
|
+
this.importMessagesSnapshot(event.messages);
|
|
477
|
+
return;
|
|
478
|
+
default: aggregator.handle(event);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
importMessagesSnapshot(rawMessages) {
|
|
482
|
+
try {
|
|
483
|
+
const normalized = fromAgUiMessages(rawMessages);
|
|
484
|
+
const converted = [];
|
|
485
|
+
for (const message of normalized) try {
|
|
486
|
+
converted.push(fromThreadMessageLike(message, generateId(), FALLBACK_USER_STATUS));
|
|
487
|
+
} catch (error) {
|
|
488
|
+
this.logger.error?.("[agui] failed to import message from snapshot", error);
|
|
489
|
+
}
|
|
490
|
+
this.applyExternalMessages(converted);
|
|
491
|
+
} catch (error) {
|
|
492
|
+
this.logger.error?.("[agui] failed to import messages snapshot", error);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
toThreadMessage(message) {
|
|
496
|
+
return fromThreadMessageLike(message, generateId(), FALLBACK_USER_STATUS);
|
|
497
|
+
}
|
|
498
|
+
resetHead(parentId) {
|
|
499
|
+
if (!parentId) {
|
|
500
|
+
if (this.messages.length) this.messages = [];
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const idx = this.messages.findIndex((message) => message.id === parentId);
|
|
504
|
+
if (idx === -1) return;
|
|
505
|
+
this.messages = this.messages.slice(0, idx + 1);
|
|
506
|
+
}
|
|
507
|
+
isTerminalStatus(status) {
|
|
508
|
+
return status?.type === "complete" || status?.type === "incomplete";
|
|
509
|
+
}
|
|
510
|
+
isPersistableStatus(status) {
|
|
511
|
+
if (this.isTerminalStatus(status)) return true;
|
|
512
|
+
return status?.type === "requires-action" && status.reason === "interrupt";
|
|
513
|
+
}
|
|
514
|
+
recordHistoryEntry(parentId, message) {
|
|
515
|
+
this.appendHistoryItem(parentId, message);
|
|
516
|
+
}
|
|
517
|
+
markPendingAssistantHistory(messageId, parentId) {
|
|
518
|
+
if (!this.history) return;
|
|
519
|
+
this.assistantHistoryParents.set(messageId, parentId);
|
|
520
|
+
}
|
|
521
|
+
persistAssistantHistory(messageId) {
|
|
522
|
+
if (!this.history) return;
|
|
523
|
+
const parentId = this.assistantHistoryParents.get(messageId);
|
|
524
|
+
if (parentId === void 0) return;
|
|
525
|
+
const message = this.messages.find((m) => m.id === messageId);
|
|
526
|
+
if (!message || message.role !== "assistant") return;
|
|
527
|
+
if (!this.isPersistableStatus(message.status)) return;
|
|
528
|
+
this.assistantHistoryParents.delete(messageId);
|
|
529
|
+
this.appendHistoryItem(parentId, message);
|
|
530
|
+
}
|
|
531
|
+
appendHistoryItem(parentId, message) {
|
|
532
|
+
if (!this.history || this.recordedHistoryIds.has(message.id)) return;
|
|
533
|
+
this.recordedHistoryIds.add(message.id);
|
|
534
|
+
this.history.append({
|
|
535
|
+
parentId,
|
|
536
|
+
message
|
|
537
|
+
}).catch((error) => {
|
|
538
|
+
this.recordedHistoryIds.delete(message.id);
|
|
539
|
+
this.logger.error?.("[agui] failed to append history entry", error);
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
//#endregion
|
|
544
|
+
export { AgUiThreadRuntimeCore };
|
|
545
|
+
|
|
632
546
|
//# sourceMappingURL=AgUiThreadRuntimeCore.js.map
|