@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.cjs CHANGED
@@ -209,6 +209,7 @@ const REPLAY_COMPLETE_EVENT = "replay_complete";
209
209
  const STREAM_IDLE_EVENT = "stream_idle";
210
210
  const STOP_RUN_EVENT = "stop_run";
211
211
  const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
212
+ const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
212
213
  var AgentThreadLockedError = class extends Error {
213
214
  constructor(threadId) {
214
215
  super(threadId ? `Thread ${threadId} is locked` : "Thread is locked");
@@ -430,15 +431,27 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
430
431
  };
431
432
  }
432
433
  observeThread$(input, credentials, options) {
433
- return this.observeThreadSession$(input, credentials, options).pipe((0, rxjs_operators.catchError)((error) => {
434
- if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
435
- const replayCursor = this.getReconnectCursor(input);
436
- 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, {
437
- ...options,
438
- channelMode: "connect",
439
- replayCursor
440
- })));
441
- }));
434
+ const { unopenedRefreshes: previousUnopened = 0, ...sessionOptions } = options;
435
+ return (0, rxjs.defer)(() => {
436
+ let socketOpened = false;
437
+ return this.observeThreadSession$(input, credentials, {
438
+ ...sessionOptions,
439
+ onSocketOpen: () => {
440
+ socketOpened = true;
441
+ }
442
+ }).pipe((0, rxjs_operators.catchError)((error) => {
443
+ if (!this.isSocketReconnectExhaustedError(error)) return (0, rxjs.throwError)(() => error);
444
+ const unopenedRefreshes = socketOpened ? 0 : previousUnopened + 1;
445
+ 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.`));
446
+ const replayCursor = this.getReconnectCursor(input);
447
+ 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, {
448
+ ...sessionOptions,
449
+ channelMode: "connect",
450
+ replayCursor,
451
+ unopenedRefreshes
452
+ })));
453
+ }));
454
+ });
442
455
  }
443
456
  observeThreadSession$(input, credentials, options) {
444
457
  return (0, rxjs.defer)(() => {
@@ -489,14 +502,16 @@ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAg
489
502
  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;
490
503
  const threadCompleted$ = threadEvents$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.endWith)(null), (0, rxjs_operators.take)(1));
491
504
  const terminal$ = (0, rxjs.merge)(threadCompleted$, streamIdleCompletion$);
492
- 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)));
505
+ 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)));
493
506
  });
494
507
  }
495
508
  joinThreadChannel$(channel$) {
496
509
  return ɵjoinPhoenixChannel$(channel$);
497
510
  }
498
- observeSocketHealth$(socket$) {
499
- return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$), 5);
511
+ observeSocketHealth$(socket$, onSocketOpen) {
512
+ return ɵobservePhoenixSocketHealth$(ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.tap)((signal) => {
513
+ if (signal.type === "open") onSocketOpen?.();
514
+ })), 5);
500
515
  }
501
516
  observeThreadEvents$(threadId, channel$, options) {
502
517
  return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.tap)((payload) => {