@copilotkit/core 1.84.2-canary.1793809041 → 1.84.2-canary.openbot-intel-fixes

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,17 +23,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
23
23
 
24
24
  //#endregion
25
25
  //#region src/utils/connect-replay.ts
26
- const connectRunErrorPhases = /* @__PURE__ */ new WeakMap();
27
- /**
28
- * The phase in which a connection received `event`, as recorded when it
29
- * arrived. Events that did not come through a connection with replay
30
- * boundaries report `unknown`.
31
- */
32
- function ɵgetConnectRunErrorPhase(event) {
33
- var _connectRunErrorPhase;
34
- return (_connectRunErrorPhase = connectRunErrorPhases.get(event)) !== null && _connectRunErrorPhase !== void 0 ? _connectRunErrorPhase : "unknown";
35
- }
36
- /**
26
+ /**
37
27
  * Runs an agent's `connect()` stream through the AbstractAgent apply pipeline
38
28
  * with the `verifyEvents` step omitted.
39
29
  *
@@ -88,7 +78,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
88
78
  let result;
89
79
  const previousMessageIds = new Set(agent.messages.map((m) => m.id));
90
80
  let isReplaying = true;
91
- let reportsReplayPhase = false;
92
81
  const liveErrors = /* @__PURE__ */ new WeakSet();
93
82
  const subscribers = [
94
83
  {
@@ -136,19 +125,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
136
125
  };
137
126
  const lifecycle = {
138
127
  onReplayStarted: () => {
139
- reportsReplayPhase = true;
140
128
  isReplaying = true;
141
129
  agent.isRunning = true;
142
130
  },
143
131
  onReplayFinished: () => {
144
- reportsReplayPhase = true;
145
132
  isReplaying = false;
146
133
  }
147
134
  };
148
135
  const source$ = (0, rxjs.defer)(() => new _ag_ui_client.CompatibilityBoundary().run(input, { run: () => connect ? connect(input, lifecycle) : self.connect(input) })).pipe((0, rxjs_operators.tap)((event) => {
149
- if (event.type !== _ag_ui_client.EventType.RUN_ERROR) return;
150
- if (!isReplaying) liveErrors.add(event);
151
- if (reportsReplayPhase) connectRunErrorPhases.set(event, isReplaying ? "replay" : "live");
136
+ if (!isReplaying && event.type === _ag_ui_client.EventType.RUN_ERROR) liveErrors.add(event);
152
137
  }), (0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(activeRunDetach$));
153
138
  const applied$ = self.apply(input, source$, subscribers);
154
139
  await (0, rxjs.lastValueFrom)(self.processApplyEvents(input, applied$, subscribers).pipe((0, rxjs_operators.catchError)((error) => {
@@ -357,20 +342,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
357
342
  }
358
343
  };
359
344
  /**
360
- * The gateway could not replay a live run's events, so the client cannot
361
- * verify the run it is following. The run itself may still be going on the
362
- * server; reconnecting to the thread shows its real state.
363
- */
364
- var IntelligenceReplayFailedError = class extends Error {
365
- constructor(threadId, runId) {
366
- super("The run's events could not be replayed. Reconnect to the thread to check the run's status.");
367
- this.threadId = threadId;
368
- this.runId = runId;
369
- _defineProperty(this, "code", "INTELLIGENCE_REPLAY_FAILED");
370
- this.name = "IntelligenceReplayFailedError";
371
- }
372
- };
373
- /**
374
345
  * Type guard for {@link RunCompletionAware}. Returns true when `agent` exposes
375
346
  * an `activeRunCompletionPromise` property, so callers can await an in-flight
376
347
  * run without an `as unknown as` cast. Returns false for agents that don't
@@ -386,6 +357,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
386
357
  _defineProperty(this, "socket", null);
387
358
  _defineProperty(this, "activeChannel", null);
388
359
  _defineProperty(this, "canonicalRunId", null);
360
+ _defineProperty(this, "runGeneration", 0);
389
361
  _defineProperty(this, "sharedState", void 0);
390
362
  this.config = config;
391
363
  this.sharedState = sharedState;
@@ -453,18 +425,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
453
425
  return ɵconnectWithoutEventVerification(this, effectiveParameters, subscriber, (input, lifecycle) => this.connect(input, lifecycle));
454
426
  }
455
427
  abortRun() {
456
- if (this.activeChannel && this.canonicalRunId) {
457
- const fallback = setTimeout(() => clear(), 5e3);
458
- const clear = () => {
459
- clearTimeout(fallback);
460
- this.detachActiveRun();
461
- this.cleanup();
462
- };
463
- this.activeChannel.push(STOP_RUN_EVENT, { run_id: this.canonicalRunId }).receive("ok", clear).receive("error", clear).receive("timeout", clear);
464
- } else {
428
+ const channel = this.activeChannel;
429
+ const runId = this.canonicalRunId;
430
+ if (!channel || !runId) {
465
431
  this.detachActiveRun();
466
432
  this.cleanup();
433
+ return;
467
434
  }
435
+ const socket = this.socket;
436
+ const generation = this.runGeneration;
437
+ let settled = false;
438
+ const clear = () => {
439
+ if (settled) return;
440
+ settled = true;
441
+ clearTimeout(fallback);
442
+ if (this.runGeneration === generation) {
443
+ this.detachActiveRun();
444
+ this.cleanup();
445
+ } else this.cleanupOwned(channel, socket, runId);
446
+ };
447
+ const fallback = setTimeout(clear, 5e3);
448
+ channel.push(STOP_RUN_EVENT, { run_id: runId }).receive("ok", clear).receive("error", clear).receive("timeout", clear);
468
449
  }
469
450
  /**
470
451
  * Trigger the run via REST, then join the realtime thread channel and relay
@@ -472,6 +453,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
472
453
  */
473
454
  run(input) {
474
455
  if (!isMcpResourceRead(input)) {
456
+ this.runGeneration += 1;
475
457
  this.threadId = input.threadId;
476
458
  this.canonicalRunId = input.runId;
477
459
  }
@@ -512,6 +494,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
512
494
  * entire history every time the chat re-opens a socket.
513
495
  */
514
496
  connect(input, lifecycle) {
497
+ this.runGeneration += 1;
515
498
  this.threadId = input.threadId;
516
499
  this.canonicalRunId = null;
517
500
  const replayCursor = this.getReconnectCursor(input);
@@ -534,9 +517,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
534
517
  /**
535
518
  * Tear down a specific channel + socket pair that belongs to one pipeline.
536
519
  * Only nulls instance references when they still point to the owned resource,
537
- * so a concurrent pipeline's resources are never clobbered.
520
+ * and the run ID only while it is still the owner's, so a concurrent
521
+ * pipeline's resources are never clobbered.
538
522
  */
539
- cleanupOwned(ownChannel, ownSocket) {
523
+ cleanupOwned(ownChannel, ownSocket, ownRunId) {
540
524
  if (ownChannel) {
541
525
  ownChannel.leave();
542
526
  if (this.activeChannel === ownChannel) this.activeChannel = null;
@@ -545,10 +529,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
545
529
  ownSocket.disconnect();
546
530
  if (this.socket === ownSocket) this.socket = null;
547
531
  }
548
- this.canonicalRunId = null;
532
+ if (this.canonicalRunId === ownRunId) this.canonicalRunId = null;
549
533
  }
550
534
  cleanup() {
551
- this.cleanupOwned(this.activeChannel, this.socket);
535
+ this.cleanupOwned(this.activeChannel, this.socket, this.canonicalRunId);
552
536
  }
553
537
  requestJoinCredentials$(mode, input, replayCursor) {
554
538
  return (0, rxjs.defer)(async () => {
@@ -643,6 +627,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
643
627
  (_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);
644
628
  let ownSocket = null;
645
629
  let ownChannel = null;
630
+ const ownRunId = this.canonicalRunId;
646
631
  let replayFinished = false;
647
632
  let warnedAboutIdle = false;
648
633
  let idleWarning;
@@ -701,12 +686,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
701
686
  };
702
687
  const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
703
688
  controlCursor.replayFailed = true;
704
- if (options.streamMode === "run") {
705
- var _input$runId;
706
- if (reconnectCursor === null) this.clearReconnectCursor(input.threadId);
707
- else this.advanceLastSeenEventId(input.threadId, reconnectCursor);
708
- throw new IntelligenceReplayFailedError(input.threadId, (_input$runId = input.runId) !== null && _input$runId !== void 0 ? _input$runId : null);
709
- }
710
689
  }));
711
690
  const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
712
691
  var _options$lifecycle2, _options$lifecycle2$o;
@@ -736,7 +715,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
736
715
  const terminal$ = (0, rxjs.merge)(threadCompleted$, streamIdleCompletion$);
737
716
  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)(() => {
738
717
  clearIdleWarning();
739
- this.cleanupOwned(ownChannel, ownSocket);
718
+ this.cleanupOwned(ownChannel, ownSocket, ownRunId);
740
719
  }));
741
720
  });
742
721
  }
@@ -4193,17 +4172,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4193
4172
  */
4194
4173
  const liveHITLStops = /* @__PURE__ */ new WeakMap();
4195
4174
  /**
4196
- * Edits `agent.messages` by replacing the array instead of changing it in
4197
- * place. The current array may be the one the agent emitted with its last
4198
- * update, which subscribers and stored snapshots still hold. Like the in-place
4199
- * edit it replaces, this notifies no subscriber.
4200
- */
4201
- function editAgentMessages(agent, edit) {
4202
- const messages = [...agent.messages];
4203
- edit(messages);
4204
- agent.messages = messages;
4205
- }
4206
- /**
4207
4175
  * The message a failed tool call reports. An error thrown with an empty
4208
4176
  * message still means the call failed, so it gets a fallback rather than an
4209
4177
  * empty string, which everything downstream reads as success.
@@ -4493,7 +4461,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4493
4461
  forwardedProps: this._internal.getPropertiesForAgent(agent.agentId),
4494
4462
  tools: this.buildFrontendTools(agent.agentId),
4495
4463
  context: this._internal.getContextForAgent(agent.agentId)
4496
- }, this.createAgentErrorSubscriber(agent, "connect"));
4464
+ }, this.createAgentErrorSubscriber(agent));
4497
4465
  if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId4 = agent.threadId) !== null && _agent$threadId4 !== void 0 ? _agent$threadId4 : null) !== incomingThreadId) return runAgentResult;
4498
4466
  const controller = new AbortController();
4499
4467
  const replay = (_this$_interactionAbo = this._interactionAbortControllers.get(agent)) !== null && _this$_interactionAbo !== void 0 ? _this$_interactionAbo : {
@@ -4602,7 +4570,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4602
4570
  });
4603
4571
  }, "Subscriber onAgentRunStarted error:");
4604
4572
  let logicalRunId = resumedRunId !== null && resumedRunId !== void 0 ? resumedRunId : runId;
4605
- const agentSubscriber = this.createAgentErrorSubscriber(agent, "run", controller.signal);
4573
+ const agentSubscriber = this.createAgentErrorSubscriber(agent, controller.signal);
4606
4574
  let started = false;
4607
4575
  const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
4608
4576
  const onRunInitialized = agentSubscriber.onRunInitialized;
@@ -4729,7 +4697,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4729
4697
  if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
4730
4698
  existingResult = void 0;
4731
4699
  const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4732
- if (agentMsgIdx !== -1) editAgentMessages(agent, (messages) => messages.splice(agentMsgIdx, 1));
4700
+ if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
4733
4701
  }
4734
4702
  if (!existingResult) {
4735
4703
  var _this$_interactionAbo2, _interactionControlle;
@@ -4955,7 +4923,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4955
4923
  content: handlerResult.content,
4956
4924
  ...handlerResult.error ? { error: handlerResult.error } : {}
4957
4925
  };
4958
- editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
4926
+ agent.messages.splice(insertAt, 0, toolMessage);
4959
4927
  if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
4960
4928
  }
4961
4929
  return false;
@@ -5054,7 +5022,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5054
5022
  content: toolCallResult,
5055
5023
  ...errorMessage ? { error: errorMessage } : {}
5056
5024
  };
5057
- editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
5025
+ agent.messages.splice(insertAt, 0, toolMessage);
5058
5026
  if (!errorMessage && (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.followUp) !== false) return true;
5059
5027
  }
5060
5028
  return false;
@@ -5108,7 +5076,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5108
5076
  content: "",
5109
5077
  toolCalls: [assistantToolCall]
5110
5078
  };
5111
- editAgentMessages(agent, (messages) => messages.push(assistantMessage));
5079
+ agent.messages.push(assistantMessage);
5112
5080
  let handlerResult = {
5113
5081
  content: "",
5114
5082
  result: "",
@@ -5131,8 +5099,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5131
5099
  ...handlerResult.error ? { error: handlerResult.error } : {}
5132
5100
  };
5133
5101
  const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
5134
- if (assistantIndex !== -1) editAgentMessages(agent, (messages) => messages.splice(assistantIndex + 1, 0, toolResultMessage));
5135
- else editAgentMessages(agent, (messages) => messages.push(toolResultMessage));
5102
+ if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
5103
+ else agent.messages.push(toolResultMessage);
5136
5104
  if (!handlerResult.error && followUp !== false) {
5137
5105
  if (typeof followUp === "string" && followUp !== "generate") {
5138
5106
  const userMessage = {
@@ -5140,7 +5108,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5140
5108
  role: "user",
5141
5109
  content: followUp
5142
5110
  };
5143
- editAgentMessages(agent, (messages) => messages.push(userMessage));
5111
+ agent.messages.push(userMessage);
5144
5112
  }
5145
5113
  await this._internal.waitForPendingFrameworkUpdates();
5146
5114
  await this.runAgent({ agent });
@@ -5213,7 +5181,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5213
5181
  /**
5214
5182
  * Create an agent error subscriber
5215
5183
  */
5216
- createAgentErrorSubscriber(agent, stream, runSignal) {
5184
+ createAgentErrorSubscriber(agent, runSignal) {
5217
5185
  const emitAgentError = async (error, code, extraContext = {}) => {
5218
5186
  const context = { ...extraContext };
5219
5187
  if (agent.agentId) context.agentId = agent.agentId;
@@ -5244,8 +5212,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5244
5212
  await emitAgentError(rawError, CopilotKitCoreErrorCode.AGENT_RUN_ERROR_EVENT, {
5245
5213
  source: "onRunErrorEvent",
5246
5214
  event,
5247
- runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code,
5248
- phase: stream === "run" ? "live" : ɵgetConnectRunErrorPhase(event)
5215
+ runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code
5249
5216
  });
5250
5217
  }
5251
5218
  };
@@ -7709,12 +7676,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
7709
7676
  CopilotKitCoreErrorCode["AGENT_CONNECT_FAILED"] = "agent_connect_failed";
7710
7677
  CopilotKitCoreErrorCode["AGENT_RUN_FAILED"] = "agent_run_failed";
7711
7678
  CopilotKitCoreErrorCode["AGENT_RUN_FAILED_EVENT"] = "agent_run_failed_event";
7712
- /**
7713
- * An agent emitted RUN_ERROR. `context.phase` (`RunErrorPhase`) is
7714
- * `"replay"` when the error belongs to stored thread history replayed on
7715
- * connect, `"live"` when the run is happening now, and `"unknown"` when the
7716
- * transport cannot tell. Show a live-failure notice only for `"live"`.
7717
- */
7718
7679
  CopilotKitCoreErrorCode["AGENT_RUN_ERROR_EVENT"] = "agent_run_error_event";
7719
7680
  CopilotKitCoreErrorCode["TOOL_ARGUMENT_PARSE_FAILED"] = "tool_argument_parse_failed";
7720
7681
  CopilotKitCoreErrorCode["TOOL_HANDLER_FAILED"] = "tool_handler_failed";
@@ -11164,7 +11125,6 @@ exports.CopilotKitCoreRuntimeConnectionStatus = CopilotKitCoreRuntimeConnectionS
11164
11125
  exports.CopilotKitHeaderResolutionError = CopilotKitHeaderResolutionError;
11165
11126
  exports.INSPECTOR_THREAD_BRIDGE_PLUGIN_ID = INSPECTOR_THREAD_BRIDGE_PLUGIN_ID;
11166
11127
  exports.IntelligenceAgent = IntelligenceAgent;
11167
- exports.IntelligenceReplayFailedError = IntelligenceReplayFailedError;
11168
11128
  exports.MEMORY_ERROR_REGISTRY = MEMORY_ERROR_REGISTRY;
11169
11129
  exports.MemoryError = MemoryError;
11170
11130
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;