@assistant-ui/react-ag-ui 0.0.58 → 0.0.59

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 CHANGED
@@ -1,5 +1,5 @@
1
- import { AgUiInterrupt, AgUiInterruptReason, AgUiResumeEntry, AgUiRunFinishedOutcome, UseAgUiRuntimeAdapters, UseAgUiRuntimeOptions, UseAgUiThreadListAdapter } from "./runtime/types.js";
1
+ import { AgUiInterrupt, AgUiInterruptReason, AgUiResumeEntry, AgUiResumeTranscript, AgUiRunFinishedOutcome, UseAgUiRuntimeAdapters, UseAgUiRuntimeOptions, UseAgUiThreadListAdapter } from "./runtime/types.js";
2
2
  import { useAgUiInterrupts, useAgUiSendA2uiAction, useAgUiSetState, useAgUiState, useAgUiSteerAway, useAgUiSubmitInterruptResponses } from "./hooks.js";
3
3
  import { AgUiAssistantRuntime, useAgUiRuntime } from "./useAgUiRuntime.js";
4
4
  import { AgUiMessage, FromAgUiMessagesOptions, fromAgUiMessages, toAgUiMessages } from "./runtime/adapter/conversions.js";
5
- export { type AgUiAssistantRuntime, type AgUiInterrupt, type AgUiInterruptReason, type AgUiMessage, type AgUiResumeEntry, type AgUiRunFinishedOutcome, type FromAgUiMessagesOptions, type UseAgUiRuntimeAdapters, type UseAgUiRuntimeOptions, type UseAgUiThreadListAdapter, fromAgUiMessages, toAgUiMessages, useAgUiInterrupts, useAgUiRuntime, useAgUiSendA2uiAction, useAgUiSetState, useAgUiState, useAgUiSteerAway, useAgUiSubmitInterruptResponses };
5
+ export { type AgUiAssistantRuntime, type AgUiInterrupt, type AgUiInterruptReason, type AgUiMessage, type AgUiResumeEntry, type AgUiResumeTranscript, type AgUiRunFinishedOutcome, type FromAgUiMessagesOptions, type UseAgUiRuntimeAdapters, type UseAgUiRuntimeOptions, type UseAgUiThreadListAdapter, fromAgUiMessages, toAgUiMessages, useAgUiInterrupts, useAgUiRuntime, useAgUiSendA2uiAction, useAgUiSetState, useAgUiState, useAgUiSteerAway, useAgUiSubmitInterruptResponses };
@@ -1,5 +1,5 @@
1
1
  import { Logger } from "./logger.js";
2
- import { AgUiInterrupt, AgUiResumeEntry } from "./types.js";
2
+ import { AgUiInterrupt, AgUiResumeEntry, AgUiResumeTranscript } from "./types.js";
3
3
  import { AddToolResultOptions, AppendMessage, AssistantRuntime, ChatModelRunOptions, ChatModelRunResult, CreateAppendMessage, ExportedMessageRepository, RespondToToolApprovalOptions, ThreadHistoryAdapter, ThreadMessage } from "@assistant-ui/core";
4
4
  import { AbstractAgent } from "@ag-ui/client";
5
5
  import { ReadonlyJSONValue } from "assistant-stream/utils";
@@ -16,6 +16,7 @@ type CoreOptions = {
16
16
  agent: AbstractAgent;
17
17
  logger: Logger;
18
18
  showThinking: boolean;
19
+ resumeTranscript?: AgUiResumeTranscript | undefined;
19
20
  autoCancelPendingToolCalls?: boolean | undefined;
20
21
  onError?: (error: Error) => void;
21
22
  onCancel?: () => void;
@@ -26,6 +27,7 @@ declare class AgUiThreadRuntimeCore {
26
27
  private agent;
27
28
  private logger;
28
29
  private showThinking;
30
+ private resumeTranscript;
29
31
  private autoCancelPendingToolCalls;
30
32
  private onError;
31
33
  private onCancel;
@@ -37,7 +39,6 @@ declare class AgUiThreadRuntimeCore {
37
39
  private abortController;
38
40
  private activeRunAgent;
39
41
  private stateSnapshot;
40
- private pendingError;
41
42
  private history;
42
43
  private lastRunConfig;
43
44
  private readonly assistantHistoryParents;
@@ -47,8 +48,8 @@ declare class AgUiThreadRuntimeCore {
47
48
  private _isLoading;
48
49
  private _loadPromise;
49
50
  private _loadRequested;
50
- private pendingResumeMessageId;
51
- private pendingA2uiResume;
51
+ private pendingResume;
52
+ private pendingA2uiResumeOwner;
52
53
  private pendingA2uiAction;
53
54
  constructor(options: CoreOptions);
54
55
  updateOptions(options: Omit<CoreOptions, "notifyUpdate">): void;
@@ -68,6 +69,8 @@ declare class AgUiThreadRuntimeCore {
68
69
  runConfig?: RunConfig;
69
70
  }): Promise<void>;
70
71
  cancel(): Promise<void>;
72
+ private abortActiveRun;
73
+ private supersedeActiveRun;
71
74
  resume(config: ResumeRunConfig): Promise<void>;
72
75
  resumeInFlightRun(messages: readonly ThreadMessage[]): Promise<void>;
73
76
  private assertNoPendingInterrupts;
@@ -117,6 +120,15 @@ declare class AgUiThreadRuntimeCore {
117
120
  private startRun;
118
121
  private consumeResumeStream;
119
122
  private buildRunInput;
123
+ /**
124
+ * An anchor the thread no longer carries falls back to the whole transcript.
125
+ * The anchor is missing exactly when a snapshot import replaced the messages
126
+ * this resume was raised against, and a host that expects a delta tolerates
127
+ * being told more than it knows, while one rebuilding the run from the
128
+ * transcript cannot recover from being told less.
129
+ */
130
+ private selectRunMessages;
131
+ private clearDeferredContinuations;
120
132
  private setRunning;
121
133
  private finishRun;
122
134
  private insertAssistantPlaceholder;
@@ -1 +1 @@
1
- {"version":3,"file":"AgUiThreadRuntimeCore.d.ts","names":[],"sources":["../../src/runtime/AgUiThreadRuntimeCore.ts"],"mappings":";;;;;;KA8EK,YAAY,YAAY;KACxB,gBACH,SAAS,wBACN,eAAe;KACf;EACH;EACA;EACA,WAAW;EACX,SAAS;;KAGN;EACH,OAAO;EACP,QAAQ;EACR;EACA;EACA,WAAW,OAAO;EAClB;EACA,UAAU;EACV;;cAoCW;UACH;UACA;UACA;UACA;UACA;UACA;mBACS;mBACA;UAET;mBACS;UAgBT;UACA;UAGA;UACA;UACA;UACA;UACA;mBACS;mBACA;mBACA;mBACA;UACT;UACA;UACA;UACA;UACA;UACA;EAEI,YAAA,SAAS;EAWrB,cAAc,SAAS,KAAK;EAoB5B,cAAc,SAAS;EAIvB;EAOA,wBAAwB;EAIxB,wBAAwB;EAIxB,YAAY;EAIZ;MAII;EAIJ,mBAAmB;EAsDb,OAAO,SAAS,gBAAgB;UAW9B;UAQA;EAiBF,KAAK,SAAS,gBAAgB;EAI9B,OACJ,yBACA;IAAU,YAAY;MACrB;EAMG,UAAU;EAcV,OAAO,QAAQ,kBAAkB;EAUjC,kBAAkB,mBAAmB,kBAAkB;UAwBrD;UAOA;EAgBR;IACE;IACA,qBAAqB;;EAavB;IACE;IACA;;EAaI,yBACJ,oBAAoB,oBACnB;;;;;;;UAuCW;;;;;;;;EAed,YAAY;UAYJ;EAwBF,sBACJ,SAAS,+BACR;EAsEG,UACJ,SAAS,qBACT,qBAAqB,oBACpB;UAyCK;UAkBA;UAiCA;UA0BA;EAmCR,yBAAyB;UAiBjB;EAiBR,cAAc,SAAS;EA+BvB,eAAe,QAAQ;UA4Bf;UAYA;UAiCA;EAUR,sBAAsB,mBAAmB;EAkDzC,kBAAkB,OAAO;EAKzB,SACE,MACI,sBACE,MAAM,kCAAkC;EAOhD;UAKc;UAyPA;UA2DN;UAkCA;UAKA;UAQA;UAyBA;UAkEA;UAiDA;UA0BA;UA6BA;UAgEA;UA0EA;UA6DA;UAuDA;UAQA;UAIA;UAKA;UAMA;UAQA;UAsDA;UAqBA"}
1
+ {"version":3,"file":"AgUiThreadRuntimeCore.d.ts","names":[],"sources":["../../src/runtime/AgUiThreadRuntimeCore.ts"],"mappings":";;;;;;KAmFK,YAAY,YAAY;KACxB,gBACH,SAAS,wBACN,eAAe;KACf;EACH;EACA;EACA,WAAW;EACX,SAAS;;KAaN;EACH,OAAO;EACP,QAAQ;EACR;EACA,mBAAmB;EACnB;EACA,WAAW,OAAO;EAClB;EACA,UAAU;EACV;;cAoCW;UACH;UACA;UACA;UACA;UACA;UACA;UACA;mBACS;mBACA;UAET;mBACS;UAgBT;UACA;UAGA;UACA;UACA;UACA;mBACS;mBACA;mBACA;mBACA;UACT;UACA;UACA;UACA;UAEA;UACA;EAEI,YAAA,SAAS;EAYrB,cAAc,SAAS,KAAK;EAqB5B,cAAc,SAAS;EAIvB;EAOA,wBAAwB;EAIxB,wBAAwB;EAIxB,YAAY;EAIZ;MAII;EAIJ,mBAAmB;EAsDb,OAAO,SAAS,gBAAgB;UAgB9B;UAQA;EAiBF,KAAK,SAAS,gBAAgB;EAI9B,OACJ,yBACA;IAAU,YAAY;MACrB;EAMG,UAAU;UAWR;UAiBA;EAaF,OAAO,QAAQ,kBAAkB;EAUjC,kBAAkB,mBAAmB,kBAAkB;UAwBrD;UAOA;EAgBR;IACE;IACA,qBAAqB;;EAavB;IACE;IACA;;EAaI,yBACJ,oBAAoB,oBACnB;;;;;;;UAuCW;;;;;;;;EAkBd,YAAY;UAYJ;EAwBF,sBACJ,SAAS,+BACR;EAsEG,UACJ,SAAS,qBACT,qBAAqB,oBACpB;UA4CK;UAkBA;UAiCA;UA2BA;EAmCR,yBAAyB;UAiBjB;EAiBR,cAAc,SAAS;EA+BvB,eAAe,QAAQ;UA6Bf;UAaA;UAiCA;EAUR,sBAAsB,mBAAmB;EA6DzC,kBAAkB,OAAO;EAKzB,SACE,MACI,sBACE,MAAM,kCAAkC;EAOhD;UAKc;UA0PA;UA2DN;;;;;;;;UA4CA;UAaA;UAUA;UAKA;UAOA;UAyBA;UAkEA;UAiDA;UA0BA;UA6BA;UAgEA;UA0EA;UA6DA;UAuDA;UAQA;UAIA;UAKA;UAMA;UAQA;UAsDA;UAqBA"}
@@ -36,6 +36,7 @@ var AgUiThreadRuntimeCore = class {
36
36
  agent;
37
37
  logger;
38
38
  showThinking;
39
+ resumeTranscript;
39
40
  autoCancelPendingToolCalls;
40
41
  onError;
41
42
  onCancel;
@@ -60,7 +61,6 @@ var AgUiThreadRuntimeCore = class {
60
61
  abortController = null;
61
62
  activeRunAgent = null;
62
63
  stateSnapshot;
63
- pendingError = null;
64
64
  history;
65
65
  lastRunConfig;
66
66
  assistantHistoryParents = /* @__PURE__ */ new Map();
@@ -70,13 +70,14 @@ var AgUiThreadRuntimeCore = class {
70
70
  _isLoading = false;
71
71
  _loadPromise;
72
72
  _loadRequested = false;
73
- pendingResumeMessageId = null;
74
- pendingA2uiResume = false;
73
+ pendingResume = null;
74
+ pendingA2uiResumeOwner = null;
75
75
  pendingA2uiAction;
76
76
  constructor(options) {
77
77
  this.agent = options.agent;
78
78
  this.logger = options.logger;
79
79
  this.showThinking = options.showThinking;
80
+ this.resumeTranscript = options.resumeTranscript ?? "full";
80
81
  this.autoCancelPendingToolCalls = options.autoCancelPendingToolCalls;
81
82
  this.onError = options.onError;
82
83
  this.onCancel = options.onCancel;
@@ -87,6 +88,7 @@ var AgUiThreadRuntimeCore = class {
87
88
  this.agent = options.agent;
88
89
  this.logger = options.logger;
89
90
  this.showThinking = options.showThinking;
91
+ this.resumeTranscript = options.resumeTranscript ?? "full";
90
92
  this.autoCancelPendingToolCalls = options.autoCancelPendingToolCalls;
91
93
  this.onError = options.onError;
92
94
  this.onCancel = options.onCancel;
@@ -150,12 +152,14 @@ var AgUiThreadRuntimeCore = class {
150
152
  }
151
153
  async append(message) {
152
154
  const startRun = message.startRun ?? message.role === "user";
155
+ let ownsThread = true;
153
156
  if (startRun) {
154
157
  this.assertNoPendingInterrupts();
155
158
  this.maybeAutoCancelPendingToolCalls();
159
+ ownsThread = this.supersedeActiveRun();
156
160
  }
157
161
  const threadMessageId = this.appendEntry(message);
158
- if (!startRun) return;
162
+ if (!startRun || !ownsThread) return;
159
163
  await this.startRun(threadMessageId, message.runConfig);
160
164
  }
161
165
  maybeAutoCancelPendingToolCalls() {
@@ -184,12 +188,26 @@ var AgUiThreadRuntimeCore = class {
184
188
  await this.startRun(parentId, config.runConfig);
185
189
  }
186
190
  async cancel() {
187
- if (!this.abortController) return;
191
+ this.abortActiveRun();
192
+ }
193
+ abortActiveRun() {
194
+ const controller = this.abortController;
195
+ if (!controller) return;
188
196
  try {
189
197
  (this.activeRunAgent ?? this.agent).abortRun();
190
198
  } finally {
191
- this.abortController.abort();
199
+ controller.abort();
200
+ }
201
+ }
202
+ supersedeActiveRun() {
203
+ try {
204
+ this.abortActiveRun();
205
+ } catch (error) {
206
+ this.logger.error?.("[agui] agent abortRun failed", error);
192
207
  }
208
+ if (this.abortController === null) return true;
209
+ this.logger.debug?.("[agui] onCancel started a replacement run; dropping the superseding send");
210
+ return false;
193
211
  }
194
212
  async resume(config) {
195
213
  this.assertNoPendingInterrupts();
@@ -259,7 +277,10 @@ var AgUiThreadRuntimeCore = class {
259
277
  */
260
278
  async resumeWithResponses(messageId, resume) {
261
279
  this.clearPendingInterrupts(messageId, resume);
262
- await this.startRun(messageId, this.lastRunConfig, resume);
280
+ await this.startRun(messageId, this.lastRunConfig, {
281
+ entries: resume,
282
+ interruptedMessageId: messageId
283
+ });
263
284
  }
264
285
  /**
265
286
  * A rejected decision reaches the caller through the approval seam, and
@@ -331,7 +352,10 @@ var AgUiThreadRuntimeCore = class {
331
352
  const normalized = this.toAppendMessage(message);
332
353
  this.clearPendingInterrupts(pending.messageId, resume);
333
354
  const threadMessageId = this.appendEntry(normalized);
334
- await this.startRun(threadMessageId, normalized.runConfig, resume);
355
+ await this.startRun(threadMessageId, normalized.runConfig, {
356
+ entries: resume,
357
+ interruptedMessageId: pending.messageId
358
+ });
335
359
  }
336
360
  resolveSteerAwayResume(interrupts, responses) {
337
361
  const responsesById = this.collectInterruptResponses("steerAway", interrupts, responses ?? []);
@@ -368,7 +392,7 @@ var AgUiThreadRuntimeCore = class {
368
392
  };
369
393
  return {
370
394
  createdAt: message.createdAt ?? /* @__PURE__ */ new Date(),
371
- parentId: message.parentId ?? this.session.headId,
395
+ parentId: message.parentId === void 0 ? this.session.headId : message.parentId,
372
396
  sourceId: message.sourceId ?? null,
373
397
  role: message.role ?? "user",
374
398
  content: message.content,
@@ -471,16 +495,21 @@ var AgUiThreadRuntimeCore = class {
471
495
  delete userAction.type;
472
496
  if (!("timestamp" in userAction)) userAction.timestamp = (/* @__PURE__ */ new Date()).toISOString();
473
497
  this.pendingA2uiAction = userAction;
474
- if (this.isRunningFlag) {
475
- this.pendingA2uiResume = true;
498
+ const owner = this.abortController;
499
+ if (owner) {
500
+ this.pendingA2uiResumeOwner = owner;
476
501
  return;
477
502
  }
478
503
  this.startResumeRun(parentId);
479
504
  }
480
505
  maybeResumeAfterToolResults(messageId) {
481
506
  if (!this.maybeCompleteAfterToolResults(messageId)) return;
482
- if (this.isRunningFlag) {
483
- this.pendingResumeMessageId = messageId;
507
+ const owner = this.abortController;
508
+ if (owner) {
509
+ this.pendingResume = {
510
+ owner,
511
+ messageId
512
+ };
484
513
  return;
485
514
  }
486
515
  this.startResumeRun(messageId);
@@ -513,7 +542,7 @@ var AgUiThreadRuntimeCore = class {
513
542
  });
514
543
  }
515
544
  applyExternalMessages(messages) {
516
- this.pendingA2uiResume = false;
545
+ this.pendingA2uiResumeOwner = null;
517
546
  this.pendingA2uiAction = void 0;
518
547
  this.assistantHistoryParents.clear();
519
548
  if (messages.length === 0) this.session.clear();
@@ -551,6 +580,8 @@ var AgUiThreadRuntimeCore = class {
551
580
  }
552
581
  this.snapshotHistoryIds.clear();
553
582
  for (const { message } of this.getMessageRepository().messages) this.snapshotHistoryIds.add(message.id);
583
+ const resumeTarget = this.pendingResume?.messageId;
584
+ if (resumeTarget !== void 0 && !this.session.getMessages().some((message) => message.id === resumeTarget)) this.pendingResume = null;
554
585
  this.notifyUpdate();
555
586
  }
556
587
  loadExternalState(state) {
@@ -566,13 +597,14 @@ var AgUiThreadRuntimeCore = class {
566
597
  this.notifyUpdate();
567
598
  }
568
599
  async startRun(parentId, runConfig, resume, resumeStream) {
600
+ if (!this.supersedeActiveRun()) return;
569
601
  const normalizedRunConfig = runConfig ?? {};
570
602
  this.lastRunConfig = normalizedRunConfig;
571
603
  const parent = parentId === null ? void 0 : this.session.tryGetMessage(parentId);
572
604
  const shouldEagerlyInsertAssistant = parentId !== null && parent !== void 0 && parentId !== this.session.headId;
573
605
  const historicalMessages = [...shouldEagerlyInsertAssistant ? this.session.tryGetMessages(parentId) ?? this.session.getMessages() : this.session.getMessages()];
574
606
  const runStartMessageIds = new Set(this.getMessageRepository().messages.map(({ message }) => message.id));
575
- this.pendingError = null;
607
+ let pendingError = null;
576
608
  const assistantParentId = parent ? parentId : this.session.headId;
577
609
  let assistantMessageId;
578
610
  let assistantCollided = false;
@@ -647,7 +679,7 @@ var AgUiThreadRuntimeCore = class {
647
679
  type: "incomplete",
648
680
  reason: "cancelled"
649
681
  } });
650
- await this.consumeResumeStream(resumeStream, {
682
+ pendingError = await this.consumeResumeStream(resumeStream, {
651
683
  runConfig: normalizedRunConfig,
652
684
  threadId: this.agent.threadId || "main",
653
685
  parentId: assistantParentId,
@@ -656,7 +688,7 @@ var AgUiThreadRuntimeCore = class {
656
688
  ensureAssistant,
657
689
  applyUpdate,
658
690
  getAssistantMessageId: () => assistantMessageId
659
- });
691
+ }) ?? null;
660
692
  } else {
661
693
  const runId = generateId();
662
694
  aggregator.handle({
@@ -670,7 +702,7 @@ var AgUiThreadRuntimeCore = class {
670
702
  logger: this.logger,
671
703
  onRunFailed: (error) => {
672
704
  if (abortSignal.aborted) return;
673
- this.pendingError = error;
705
+ pendingError = error;
674
706
  invokeRuntimeCallback("onError", this.onError, error);
675
707
  }
676
708
  });
@@ -689,28 +721,24 @@ var AgUiThreadRuntimeCore = class {
689
721
  message: err.message
690
722
  });
691
723
  invokeRuntimeCallback("onError", this.onError, err);
692
- this.pendingError = this.pendingError ?? err;
724
+ pendingError ??= err;
693
725
  }
694
726
  } finally {
695
727
  this.finishRun(abortController);
696
728
  }
697
- if (this.pendingError) {
698
- const err = this.pendingError;
729
+ if (pendingError) {
730
+ const err = pendingError;
699
731
  this.reportedErrors.add(err);
700
- this.pendingError = null;
701
- this.pendingResumeMessageId = null;
702
- this.pendingA2uiResume = false;
703
- this.pendingA2uiAction = void 0;
732
+ this.clearDeferredContinuations(abortController);
704
733
  throw err;
705
734
  }
706
- if (this.pendingResumeMessageId !== null) {
707
- const resumeMessageId = this.pendingResumeMessageId;
708
- this.pendingResumeMessageId = null;
709
- if (!abortSignal.aborted) this.startResumeRun(resumeMessageId);
710
- else this.pendingA2uiAction = void 0;
735
+ if (this.pendingResume?.owner === abortController) {
736
+ const { messageId } = this.pendingResume;
737
+ this.pendingResume = null;
738
+ if (!abortSignal.aborted) this.startResumeRun(messageId);
711
739
  }
712
- if (this.pendingA2uiResume) {
713
- this.pendingA2uiResume = false;
740
+ if (this.pendingA2uiResumeOwner === abortController) {
741
+ this.pendingA2uiResumeOwner = null;
714
742
  if (!abortSignal.aborted && this.pendingA2uiAction !== void 0) {
715
743
  if (this.getPendingInterrupts()) {
716
744
  this.pendingA2uiAction = void 0;
@@ -729,7 +757,7 @@ var AgUiThreadRuntimeCore = class {
729
757
  }
730
758
  async consumeResumeStream(stream, ctx) {
731
759
  this.pendingA2uiAction = void 0;
732
- this.pendingA2uiResume = false;
760
+ this.pendingA2uiResumeOwner = null;
733
761
  const assistantId = ctx.ensureAssistant();
734
762
  const currentId = () => ctx.getAssistantMessageId() ?? assistantId;
735
763
  const options = {
@@ -748,11 +776,11 @@ var AgUiThreadRuntimeCore = class {
748
776
  };
749
777
  try {
750
778
  for await (const result of stream(options)) {
751
- if (ctx.abortSignal.aborted) return;
779
+ if (ctx.abortSignal.aborted) return void 0;
752
780
  ctx.applyUpdate(result);
753
781
  }
754
782
  } catch (error) {
755
- if (ctx.abortSignal.aborted) return;
783
+ if (ctx.abortSignal.aborted) return void 0;
756
784
  const err = error instanceof Error ? error : new Error(String(error));
757
785
  ctx.applyUpdate({ status: {
758
786
  type: "incomplete",
@@ -760,10 +788,9 @@ var AgUiThreadRuntimeCore = class {
760
788
  error: err.message
761
789
  } });
762
790
  invokeRuntimeCallback("onError", this.onError, err);
763
- this.pendingError = this.pendingError ?? err;
764
- return;
791
+ return err;
765
792
  }
766
- if (ctx.abortSignal.aborted) return;
793
+ if (ctx.abortSignal.aborted) return void 0;
767
794
  const current = this.session.tryGetMessage(currentId())?.message;
768
795
  if (!current || current.status?.type === "running") ctx.applyUpdate({ status: {
769
796
  type: "complete",
@@ -772,7 +799,7 @@ var AgUiThreadRuntimeCore = class {
772
799
  }
773
800
  buildRunInput(runId, runConfig, historyMessages, resume) {
774
801
  const threadId = this.agent.threadId || "main";
775
- const messages = toAgUiMessages(historyMessages ?? this.session.getMessages());
802
+ const messages = toAgUiMessages(this.selectRunMessages(historyMessages ?? this.session.getMessages(), resume));
776
803
  const context = this.runtime?.thread.getModelContext();
777
804
  const input = {
778
805
  threadId,
@@ -790,20 +817,39 @@ var AgUiThreadRuntimeCore = class {
790
817
  ...runConfig?.custom ? { runConfig: runConfig.custom } : {},
791
818
  ...this.pendingA2uiAction ? { a2uiAction: { userAction: this.pendingA2uiAction } } : {}
792
819
  },
793
- ...resume !== void 0 ? { resume } : {}
820
+ ...resume !== void 0 ? { resume: resume.entries } : {}
794
821
  };
795
822
  this.pendingA2uiAction = void 0;
796
823
  return input;
797
824
  }
825
+ /**
826
+ * An anchor the thread no longer carries falls back to the whole transcript.
827
+ * The anchor is missing exactly when a snapshot import replaced the messages
828
+ * this resume was raised against, and a host that expects a delta tolerates
829
+ * being told more than it knows, while one rebuilding the run from the
830
+ * transcript cannot recover from being told less.
831
+ */
832
+ selectRunMessages(messages, resume) {
833
+ if (resume === void 0 || this.resumeTranscript !== "appended") return messages;
834
+ const anchor = messages.findIndex((message) => message.id === resume.interruptedMessageId);
835
+ if (anchor === -1) return messages;
836
+ return messages.slice(anchor + 1);
837
+ }
838
+ clearDeferredContinuations(owner) {
839
+ if (this.pendingResume?.owner === owner) this.pendingResume = null;
840
+ if (this.pendingA2uiResumeOwner === owner) {
841
+ this.pendingA2uiResumeOwner = null;
842
+ this.pendingA2uiAction = void 0;
843
+ }
844
+ }
798
845
  setRunning(running) {
799
846
  this.isRunningFlag = running;
800
847
  this.notifyUpdate();
801
848
  }
802
849
  finishRun(controller) {
803
- if (this.abortController === controller) {
804
- this.abortController = null;
805
- this.activeRunAgent = null;
806
- }
850
+ if (this.abortController !== controller) return;
851
+ this.abortController = null;
852
+ this.activeRunAgent = null;
807
853
  this.setRunning(false);
808
854
  }
809
855
  insertAssistantPlaceholder(parentId, id = generateOptimisticId()) {