@copilotkit/core 1.73.3 → 1.74.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
@@ -250,6 +250,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
250
250
  const STREAM_IDLE_EVENT = "stream_idle";
251
251
  const STOP_RUN_EVENT = "stop_run";
252
252
  const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
253
+ const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
253
254
  var AgentThreadLockedError = class extends Error {
254
255
  constructor(threadId) {
255
256
  super(threadId ? `Thread ${threadId} is locked` : "Thread is locked");
@@ -472,15 +473,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
472
473
  };
473
474
  }
474
475
  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
- }));
476
+ const { unopenedRefreshes: previousUnopened = 0, ...sessionOptions } = options;
477
+ return (0, rxjs.defer)(() => {
478
+ let socketOpened = false;
479
+ return this.observeThreadSession$(input, credentials, {
480
+ ...sessionOptions,
481
+ onSocketOpen: () => {
482
+ socketOpened = true;
483
+ }
484
+ }).pipe((0, rxjs_operators.catchError)((error) => {
485
+ if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
486
+ const unopenedRefreshes = socketOpened ? 0 : previousUnopened + 1;
487
+ 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.`));
488
+ const replayCursor = this.getReconnectCursor(input);
489
+ 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, {
490
+ ...sessionOptions,
491
+ channelMode: "connect",
492
+ replayCursor,
493
+ unopenedRefreshes
494
+ })));
495
+ }));
496
+ });
484
497
  }
485
498
  observeThreadSession$(input, credentials, options) {
486
499
  return (0, rxjs.defer)(() => {
@@ -533,14 +546,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
533
546
  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
547
  const threadCompleted$ = threadEvents$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.endWith)(null), (0, rxjs_operators.take)(1));
535
548
  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)));
549
+ 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$)), 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
550
  });
538
551
  }
539
552
  joinThreadChannel$(channel$) {
540
553
  return ɵjoinPhoenixChannel$(channel$);
541
554
  }
542
- observeSocketHealth$(socket$) {
543
- return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$), 5);
555
+ observeSocketHealth$(socket$, onSocketOpen) {
556
+ return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.tap)((signal) => {
557
+ if (signal.type === "open") onSocketOpen === null || onSocketOpen === void 0 || onSocketOpen();
558
+ })), 5);
544
559
  }
545
560
  observeThreadEvents$(threadId, channel$, options) {
546
561
  return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.tap)((payload) => {