@copilotkit/core 1.75.0 → 1.75.2

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
@@ -23,20 +23,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
23
23
  *
24
24
  * `transformChunks` is still applied — message reassembly is needed either way.
25
25
  *
26
- * This mirrors the base `AbstractAgent.connectAgent` implementation exactly
27
- * apart from that omission, so callers keep the same subscriber notifications,
28
- * detach semantics, and `{ result, newMessages }` return shape.
26
+ * Connection-local replay hooks track the phase before applying events. An
27
+ * explicitly live RUN_ERROR clears busy without closing the connection;
28
+ * historical errors remain data. Connections without hooks retain the existing
29
+ * completion behavior. Subscriber, detach, and result contracts are preserved.
29
30
  *
30
31
  * TODO: Remove this in favour of the base implementation once AG-UI's
31
32
  * AbstractAgent supports opting out of `verifyEvents` for transports whose
32
- * connection life-cycle isn't a single run. As of `@ag-ui/client@0.0.57`
33
+ * connection life-cycle isn't a single run AND preserves the connection-local
34
+ * replay lifecycle and running-state behavior below. Skipping verification alone
35
+ * is insufficient. As of `@ag-ui/client@0.0.57`
33
36
  * `connectAgent(parameters?, subscriber?)` takes no such option.
34
37
  *
35
38
  * @param agent - The agent whose `connect()` stream should be consumed.
36
39
  * @param parameters - Run parameters, forwarded to `prepareRunAgentInput`.
37
40
  * @param subscriber - Optional one-shot subscriber for this connect call.
38
41
  */
39
- async function ɵconnectWithoutEventVerification(agent, parameters, subscriber) {
42
+ async function ɵconnectWithoutEventVerification(agent, parameters, subscriber, connect) {
40
43
  const self = agent;
41
44
  try {
42
45
  var _agent$agentId;
@@ -45,10 +48,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
45
48
  const input = self.prepareRunAgentInput(parameters);
46
49
  let result;
47
50
  const previousMessageIds = new Set(agent.messages.map((m) => m.id));
51
+ let isReplaying = true;
52
+ const liveErrors = /* @__PURE__ */ new WeakSet();
48
53
  const subscribers = [
49
- { onRunFinishedEvent: (event) => {
50
- if (event.outcome === "success") result = event.result;
51
- } },
54
+ {
55
+ onRunStartedEvent: () => {
56
+ agent.isRunning = true;
57
+ },
58
+ onRunErrorEvent: ({ event }) => {
59
+ if (liveErrors.has(event)) agent.isRunning = false;
60
+ },
61
+ onRunFinishedEvent: (event) => {
62
+ if (event.outcome === "success") result = event.result;
63
+ }
64
+ },
52
65
  ...agent.subscribers,
53
66
  subscriber !== null && subscriber !== void 0 ? subscriber : {}
54
67
  ];
@@ -58,7 +71,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
58
71
  self.activeRunCompletionPromise = new Promise((resolve) => {
59
72
  resolveCompletion = resolve;
60
73
  });
61
- const source$ = (0, rxjs.defer)(() => self.connect(input)).pipe((0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(self.activeRunDetach$));
74
+ const lifecycle = {
75
+ onReplayStarted: () => {
76
+ isReplaying = true;
77
+ agent.isRunning = true;
78
+ },
79
+ onReplayFinished: () => {
80
+ isReplaying = false;
81
+ }
82
+ };
83
+ const source$ = (0, rxjs.defer)(() => connect ? connect(input, lifecycle) : self.connect(input)).pipe((0, rxjs_operators.tap)((event) => {
84
+ if (!isReplaying && event.type === _ag_ui_client.EventType.RUN_ERROR) liveErrors.add(event);
85
+ }), (0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(self.activeRunDetach$));
62
86
  const applied$ = self.apply(input, source$, subscribers);
63
87
  await (0, rxjs.lastValueFrom)(self.processApplyEvents(input, applied$, subscribers).pipe((0, rxjs_operators.catchError)((error) => {
64
88
  agent.isRunning = false;
@@ -251,6 +275,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
251
275
  const STREAM_IDLE_EVENT = "stream_idle";
252
276
  const STOP_RUN_EVENT = "stop_run";
253
277
  const CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS = 100;
278
+ const CONNECT_IDLE_WARNING_MS = 3e4;
254
279
  const MAX_UNOPENED_CREDENTIAL_REFRESHES = 2;
255
280
  var AgentThreadLockedError = class extends Error {
256
281
  constructor(threadId) {
@@ -338,7 +363,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
338
363
  ...parameters,
339
364
  runId: this.canonicalRunId
340
365
  };
341
- return ɵconnectWithoutEventVerification(this, effectiveParameters, subscriber);
366
+ return ɵconnectWithoutEventVerification(this, effectiveParameters, subscriber, (input, lifecycle) => this.connect(input, lifecycle));
342
367
  }
343
368
  abortRun() {
344
369
  if (this.activeChannel && this.canonicalRunId) {
@@ -387,17 +412,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
387
412
  * gateway only streams events past it instead of replaying the
388
413
  * entire history every time the chat re-opens a socket.
389
414
  */
390
- connect(input) {
415
+ connect(input, lifecycle) {
391
416
  this.threadId = input.threadId;
392
417
  this.canonicalRunId = null;
393
418
  const replayCursor = this.getReconnectCursor(input);
394
419
  return (0, rxjs.defer)(() => this.requestJoinCredentials$("connect", input, replayCursor)).pipe((0, rxjs.switchMap)((credentials) => {
395
- if (credentials === null) return rxjs.EMPTY;
420
+ if (credentials === null) {
421
+ var _lifecycle$onReplaySt, _lifecycle$onReplayFi;
422
+ lifecycle === null || lifecycle === void 0 || (_lifecycle$onReplaySt = lifecycle.onReplayStarted) === null || _lifecycle$onReplaySt === void 0 || _lifecycle$onReplaySt.call(lifecycle);
423
+ lifecycle === null || lifecycle === void 0 || (_lifecycle$onReplayFi = lifecycle.onReplayFinished) === null || _lifecycle$onReplayFi === void 0 || _lifecycle$onReplayFi.call(lifecycle);
424
+ return rxjs.EMPTY;
425
+ }
396
426
  const canonicalInput = this.applyCanonicalRunIdentity(input, credentials, { fallbackToInputRunId: false });
397
427
  return this.observeThread$(canonicalInput, credentials, {
398
428
  completeOnRunError: false,
399
429
  streamMode: "connect",
400
- replayCursor
430
+ replayCursor,
431
+ lifecycle
401
432
  });
402
433
  }));
403
434
  }
@@ -498,9 +529,26 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
498
529
  }
499
530
  observeThreadSession$(input, credentials, options) {
500
531
  return (0, rxjs.defer)(() => {
501
- var _options$channelMode, _options$replayCursor;
532
+ var _options$lifecycle, _options$lifecycle$on, _options$channelMode, _options$replayCursor;
533
+ (_options$lifecycle = options.lifecycle) === null || _options$lifecycle === void 0 || (_options$lifecycle$on = _options$lifecycle.onReplayStarted) === null || _options$lifecycle$on === void 0 || _options$lifecycle$on.call(_options$lifecycle);
502
534
  let ownSocket = null;
503
535
  let ownChannel = null;
536
+ let replayFinished = false;
537
+ let warnedAboutIdle = false;
538
+ let idleWarning;
539
+ const clearIdleWarning = () => {
540
+ clearTimeout(idleWarning);
541
+ idleWarning = void 0;
542
+ };
543
+ const scheduleIdleWarning = () => {
544
+ clearIdleWarning();
545
+ if (options.streamMode !== "connect" || !replayFinished || warnedAboutIdle) return;
546
+ idleWarning = setTimeout(() => {
547
+ var _this$debugLogger;
548
+ warnedAboutIdle = true;
549
+ (_this$debugLogger = this.debugLogger) === null || _this$debugLogger === void 0 || _this$debugLogger.lifecycle("INTELLIGENCE", "No progress or stream_idle for 30 seconds after replay_complete; the run may still be active or its status unavailable.", { threadId: input.threadId });
550
+ }, CONNECT_IDLE_WARNING_MS);
551
+ };
504
552
  const socket$ = ɵphoenixSocket$({
505
553
  url: credentials.realtime.clientUrl,
506
554
  options: {
@@ -535,6 +583,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
535
583
  const threadEvents$ = this.observeThreadEvents$(input.threadId, channel$, options).pipe((0, rxjs_operators.tap)((payload) => {
536
584
  var _this$readEventId;
537
585
  latestObservedReplayCursor = (_this$readEventId = this.readEventId(payload)) !== null && _this$readEventId !== void 0 ? _this$readEventId : latestObservedReplayCursor;
586
+ scheduleIdleWarning();
538
587
  }), (0, rxjs_operators.share)());
539
588
  const controlCursor = {
540
589
  applied: null,
@@ -543,18 +592,36 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
543
592
  const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
544
593
  controlCursor.replayFailed = true;
545
594
  }));
546
- const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.shareReplay)({
547
- bufferSize: 1,
548
- refCount: true
549
- }));
550
- const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor).pipe((0, rxjs_operators.shareReplay)({
595
+ const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
596
+ var _options$lifecycle2, _options$lifecycle2$o;
597
+ replayFinished = true;
598
+ scheduleIdleWarning();
599
+ (_options$lifecycle2 = options.lifecycle) === null || _options$lifecycle2 === void 0 || (_options$lifecycle2$o = _options$lifecycle2.onReplayFinished) === null || _options$lifecycle2$o === void 0 || _options$lifecycle2$o.call(_options$lifecycle2);
600
+ }), (0, rxjs_operators.share)());
601
+ const streamIdle$ = this.observeControlEvent$(input.threadId, channel$, STREAM_IDLE_EVENT, controlCursor).pipe((0, rxjs_operators.share)());
602
+ const replayRestart$ = (0, rxjs.merge)(this.observeControlEvent$(input.threadId, channel$, "phx_error", controlCursor), ɵobservePhoenixSocketSignals$(socket$).pipe((0, rxjs_operators.filter)((signal) => signal.type === "error"))).pipe((0, rxjs_operators.tap)(() => {
603
+ var _options$lifecycle3, _options$lifecycle3$o;
604
+ replayFinished = false;
605
+ warnedAboutIdle = false;
606
+ clearIdleWarning();
607
+ (_options$lifecycle3 = options.lifecycle) === null || _options$lifecycle3 === void 0 || (_options$lifecycle3$o = _options$lifecycle3.onReplayStarted) === null || _options$lifecycle3$o === void 0 || _options$lifecycle3$o.call(_options$lifecycle3);
608
+ }), (0, rxjs_operators.share)());
609
+ const streamIdleCompletion$ = options.streamMode === "connect" ? replayRestart$.pipe((0, rxjs_operators.startWith)(null), (0, rxjs.switchMap)(() => (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), (0, rxjs_operators.tap)(() => {
610
+ var _this$debugLogger2;
611
+ (_this$debugLogger2 = this.debugLogger) === null || _this$debugLogger2 === void 0 || _this$debugLogger2.lifecycle("INTELLIGENCE", "Completing connection on stream_idle fallback without replay_complete.", {
612
+ threadId: input.threadId,
613
+ fallbackMs: CONNECT_STREAM_IDLE_REPLAY_FALLBACK_MS
614
+ });
615
+ })))), (0, rxjs_operators.take)(1), (0, rxjs_operators.shareReplay)({
551
616
  bufferSize: 1,
552
617
  refCount: true
553
- }));
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;
618
+ })) : rxjs.EMPTY;
555
619
  const threadCompleted$ = threadEvents$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.endWith)(null), (0, rxjs_operators.take)(1));
556
620
  const terminal$ = (0, rxjs.merge)(threadCompleted$, streamIdleCompletion$);
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)));
621
+ return (0, rxjs.merge)(replayFailed$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), replayComplete$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(terminal$)), this.joinThreadChannel$(channel$), this.observeSocketHealth$(socket$, options.onSocketOpen).pipe((0, rxjs_operators.takeUntil)(terminal$)), threadEvents$.pipe((0, rxjs_operators.takeUntil)(streamIdleCompletion$)), streamIdleCompletion$.pipe((0, rxjs_operators.ignoreElements)(), (0, rxjs_operators.takeUntil)(threadCompleted$))).pipe((0, rxjs_operators.finalize)(() => {
622
+ clearIdleWarning();
623
+ this.cleanupOwned(ownChannel, ownSocket);
624
+ }));
558
625
  });
559
626
  }
560
627
  joinThreadChannel$(channel$) {
@@ -1041,7 +1108,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1041
1108
  });
1042
1109
  }
1043
1110
  async connectAgent(parameters, subscriber) {
1044
- if (this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return ɵconnectWithoutEventVerification(this, parameters, subscriber);
1111
+ if (this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return ɵconnectWithoutEventVerification(this, parameters, subscriber, (input, lifecycle) => this.connect(input, lifecycle));
1045
1112
  if (this.delegate) await this.delegate.detachActiveRun();
1046
1113
  await this.resolveDelegate();
1047
1114
  const delegate = this.delegate;
@@ -1062,7 +1129,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1062
1129
  this.isRunning = false;
1063
1130
  },
1064
1131
  onRunErrorEvent: () => {
1065
- this.isRunning = false;
1132
+ this.isRunning = delegate.isRunning;
1133
+ },
1134
+ onRunStartedEvent: () => {
1135
+ this.isRunning = delegate.isRunning;
1066
1136
  }
1067
1137
  });
1068
1138
  const forwardedSubs = this.subscribers.map((s) => delegate.subscribe(s));
@@ -1077,9 +1147,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1077
1147
  for (const sub of forwardedSubs) sub.unsubscribe();
1078
1148
  }
1079
1149
  }
1080
- connect(input) {
1081
- if (this.runtimeMode === "pending" || this.transport === "auto" && this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return (0, rxjs.defer)(() => (0, rxjs.from)(this.ensureRuntimeConfiguration())).pipe((0, rxjs_operators.switchMap)(() => this.connect(input)));
1082
- if (this.runtimeMode === _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _connectViaDelegate).call(this, input);
1150
+ connect(input, lifecycle) {
1151
+ if (this.runtimeMode === "pending" || this.transport === "auto" && this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return (0, rxjs.defer)(() => (0, rxjs.from)(this.ensureRuntimeConfiguration())).pipe((0, rxjs_operators.switchMap)(() => this.connect(input, lifecycle)));
1152
+ if (this.runtimeMode === _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _connectViaDelegate).call(this, input, lifecycle);
1083
1153
  return _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _connectViaHttp).call(this, input);
1084
1154
  }
1085
1155
  run(input) {
@@ -1252,6 +1322,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1252
1322
  delegate.agentId = this.routedAgentId();
1253
1323
  delegate.description = this.description;
1254
1324
  delegate.threadId = this.threadId;
1325
+ delegate.debugLogger = this.debugLogger;
1255
1326
  delegate.setMessages(this.messages);
1256
1327
  delegate.setState(this.state);
1257
1328
  if (hasHeaders(delegate)) delegate.headers = { ...this.headers };
@@ -1307,8 +1378,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1307
1378
  return input;
1308
1379
  }
1309
1380
  }
1310
- function _connectViaDelegate(input) {
1311
- return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.connect(input))));
1381
+ function _connectViaDelegate(input, lifecycle) {
1382
+ return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.connect(input, lifecycle))));
1312
1383
  }
1313
1384
  function _connectViaHttp(unfiltered) {
1314
1385
  const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _applyMessageFilter).call(this, unfiltered);
@@ -5562,6 +5633,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5562
5633
  "onMessagesChanged",
5563
5634
  "onStateChanged",
5564
5635
  "onRunInitialized",
5636
+ "onRunStartedEvent",
5565
5637
  "onRunFinalized",
5566
5638
  "onRunFailed",
5567
5639
  "onRunErrorEvent"
@@ -6168,6 +6240,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
6168
6240
  const fn = sub.onRunInitialized;
6169
6241
  guarded.onRunInitialized = (params) => safeCall("onRunInitialized", fn, params);
6170
6242
  }
6243
+ if (sub.onRunStartedEvent) {
6244
+ const fn = sub.onRunStartedEvent;
6245
+ guarded.onRunStartedEvent = (params) => safeCall("onRunStartedEvent", fn, params);
6246
+ }
6171
6247
  if (sub.onRunFinalized) {
6172
6248
  const fn = sub.onRunFinalized;
6173
6249
  guarded.onRunFinalized = (params) => safeCall("onRunFinalized", fn, params);
@@ -6219,6 +6295,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
6219
6295
  });
6220
6296
  const lifecycleOnly = {};
6221
6297
  if (subscriber.onRunInitialized) lifecycleOnly.onRunInitialized = subscriber.onRunInitialized;
6298
+ if (subscriber.onRunStartedEvent) lifecycleOnly.onRunStartedEvent = subscriber.onRunStartedEvent;
6222
6299
  if (subscriber.onRunFinalized) lifecycleOnly.onRunFinalized = subscriber.onRunFinalized;
6223
6300
  if (subscriber.onRunFailed) lifecycleOnly.onRunFailed = subscriber.onRunFailed;
6224
6301
  if (subscriber.onRunErrorEvent) lifecycleOnly.onRunErrorEvent = subscriber.onRunErrorEvent;