@copilotkit/core 1.73.3 → 1.75.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.
package/dist/index.umd.js CHANGED
@@ -247,9 +247,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
247
247
  const globalFetch = (...args) => fetch(...args);
248
248
  const CLIENT_AG_UI_EVENT = "ag_ui_event";
249
249
  const REPLAY_COMPLETE_EVENT = "replay_complete";
250
+ const REPLAY_FAILED_EVENT = "replay_failed";
250
251
  const STREAM_IDLE_EVENT = "stream_idle";
251
252
  const STOP_RUN_EVENT = "stop_run";
252
253
  const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
254
+ const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
253
255
  var AgentThreadLockedError = class extends Error {
254
256
  constructor(threadId) {
255
257
  super(threadId ? `Thread ${threadId} is locked` : "Thread is locked");
@@ -472,15 +474,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
472
474
  };
473
475
  }
474
476
  observeThread$(input, credentials, options) {
475
- return this.observeThreadSession$(input, credentials, options).pipe((0, rxjs_operators.catchError)((error) => {
476
- if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
477
- const replayCursor = this.getReconnectCursor(input);
478
- return this.requestJoinCredentials$("connect", input, replayCursor).pipe((0, rxjs.switchMap)((refreshedCredentials) => refreshedCredentials === null ? rxjs.EMPTY : this.observeThread$(this.applyCanonicalRunIdentity(input, refreshedCredentials, { fallbackToInputRunId: options.streamMode === "run" }), refreshedCredentials, {
479
- ...options,
480
- channelMode: "connect",
481
- replayCursor
482
- })));
483
- }));
477
+ const { unopenedRefreshes: previousUnopened = 0, ...sessionOptions } = options;
478
+ return (0, rxjs.defer)(() => {
479
+ let socketOpened = false;
480
+ return this.observeThreadSession$(input, credentials, {
481
+ ...sessionOptions,
482
+ onSocketOpen: () => {
483
+ socketOpened = true;
484
+ }
485
+ }).pipe((0, rxjs_operators.catchError)((error) => {
486
+ if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
487
+ const unopenedRefreshes = socketOpened ? 0 : previousUnopened + 1;
488
+ if (options.streamMode === "run" && unopenedRefreshes > MAX_UNOPENED_CREDENTIAL_REFRESHES) return (0, rxjs.throwError)(() => /* @__PURE__ */ new Error(`Realtime connection to ${credentials.realtime.clientUrl} never opened in ${unopenedRefreshes} connection attempts. The realtime endpoint is unavailable.`));
489
+ const replayCursor = this.getReconnectCursor(input);
490
+ return this.requestJoinCredentials$("connect", input, replayCursor).pipe((0, rxjs.switchMap)((refreshedCredentials) => refreshedCredentials === null ? rxjs.EMPTY : this.observeThread$(this.applyCanonicalRunIdentity(input, refreshedCredentials, { fallbackToInputRunId: options.streamMode === "run" }), refreshedCredentials, {
491
+ ...sessionOptions,
492
+ channelMode: "connect",
493
+ replayCursor,
494
+ unopenedRefreshes
495
+ })));
496
+ }));
497
+ });
484
498
  }
485
499
  observeThreadSession$(input, credentials, options) {
486
500
  return (0, rxjs.defer)(() => {
@@ -522,25 +536,34 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
522
536
  var _this$readEventId;
523
537
  latestObservedReplayCursor = (_this$readEventId = this.readEventId(payload)) !== null && _this$readEventId !== void 0 ? _this$readEventId : latestObservedReplayCursor;
524
538
  }), (0, rxjs_operators.share)());
525
- const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT).pipe((0, rxjs_operators.shareReplay)({
539
+ const controlCursor = {
540
+ applied: null,
541
+ replayFailed: false
542
+ };
543
+ const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
544
+ controlCursor.replayFailed = true;
545
+ }));
546
+ const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.shareReplay)({
526
547
  bufferSize: 1,
527
548
  refCount: true
528
549
  }));
529
- const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT).pipe((0, rxjs_operators.shareReplay)({
550
+ const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor).pipe((0, rxjs_operators.shareReplay)({
530
551
  bufferSize: 1,
531
552
  refCount: true
532
553
  }));
533
554
  const streamIdleCompletion$ = options.streamMode === "connect" ? (0, rxjs.merge)((0, rxjs.combineLatest)([replayComplete$.pipe((0, rxjs_operators.take)(1)), streamIdle$.pipe((0, rxjs_operators.take)(1))]), streamIdle$.pipe((0, rxjs_operators.take)(1), (0, rxjs_operators.filter)((payload) => this.canFallbackCompleteConnect(payload, reconnectCursor, latestObservedReplayCursor)), (0, rxjs_operators.delay)(CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS))).pipe((0, rxjs_operators.take)(1)) : rxjs.EMPTY;
534
555
  const threadCompleted$ = threadEvents$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.endWith)(null), (0, rxjs_operators.take)(1));
535
556
  const terminal$ = (0, rxjs.merge)(threadCompleted$, streamIdleCompletion$);
536
- return (0, rxjs.merge)(this.joinThreadChannel$(channel$), this.observeSocketHealth$(socket$).pipe((0, rxjs_operators.takeUntil)(terminal$)), threadEvents$.pipe((0, rxjs_operators.takeUntil)(streamIdleCompletion$)), replayComplete$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), streamIdleCompletion$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(threadCompleted$))).pipe((0, rxjs_operators.finalize)(() => this.cleanupOwned(ownChannel, ownSocket)));
557
+ return (0, rxjs.merge)(this.joinThreadChannel$(channel$), this.observeSocketHealth$(socket$, options.onSocketOpen).pipe((0, rxjs_operators.takeUntil)(terminal$)), threadEvents$.pipe((0, rxjs_operators.takeUntil)(streamIdleCompletion$)), replayFailed$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), replayComplete$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), streamIdleCompletion$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(threadCompleted$))).pipe((0, rxjs_operators.finalize)(() => this.cleanupOwned(ownChannel, ownSocket)));
537
558
  });
538
559
  }
539
560
  joinThreadChannel$(channel$) {
540
561
  return ɵjoinPhoenixChannel$(channel$);
541
562
  }
542
- observeSocketHealth$(socket$) {
543
- return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$), 5);
563
+ observeSocketHealth$(socket$, onSocketOpen) {
564
+ return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.tap)((signal) => {
565
+ if (signal.type === "open") onSocketOpen === null || onSocketOpen === void 0 || onSocketOpen();
566
+ })), 5);
544
567
  }
545
568
  observeThreadEvents$(threadId, channel$, options) {
546
569
  return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.tap)((payload) => {
@@ -551,8 +574,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
551
574
  errorOnRunError: options.streamMode === "run"
552
575
  })), (0, rxjs.dematerialize)());
553
576
  }
554
- observeControlEvent$(threadId, channel$, eventName) {
555
- return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) => this.updateLastSeenEventIdFromControl(threadId, payload)));
577
+ observeControlEvent$(threadId, channel$, eventName, controlCursor) {
578
+ return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, eventName)), (0, rxjs_operators.tap)((payload) => this.updateLastSeenEventIdFromControl(threadId, payload, controlCursor)));
556
579
  }
557
580
  observeChannelEvent$(channel, eventName) {
558
581
  return ɵobservePhoenixEvent$(channel, eventName);
@@ -603,9 +626,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
603
626
  if (!eventId) return;
604
627
  this.advanceLastSeenEventId(threadId, eventId);
605
628
  }
606
- updateLastSeenEventIdFromControl(threadId, payload) {
629
+ updateLastSeenEventIdFromControl(threadId, payload, controlCursor) {
607
630
  const eventId = this.readControlEventId(payload);
608
631
  if (!eventId) return;
632
+ if (eventId === controlCursor.applied && !controlCursor.replayFailed) return;
633
+ controlCursor.applied = eventId;
609
634
  this.advanceLastSeenEventId(threadId, eventId);
610
635
  }
611
636
  advanceLastSeenEventId(threadId, eventId) {