@copilotkit/core 1.79.1 → 1.79.2-canary.1792172552

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,7 +23,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
23
23
 
24
24
  //#endregion
25
25
  //#region src/utils/connect-replay.ts
26
- /**
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
+ /**
27
37
  * Runs an agent's `connect()` stream through the AbstractAgent apply pipeline
28
38
  * with the `verifyEvents` step omitted.
29
39
  *
@@ -78,6 +88,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
78
88
  let result;
79
89
  const previousMessageIds = new Set(agent.messages.map((m) => m.id));
80
90
  let isReplaying = true;
91
+ let reportsReplayPhase = false;
81
92
  const liveErrors = /* @__PURE__ */ new WeakSet();
82
93
  const subscribers = [
83
94
  {
@@ -125,15 +136,19 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
125
136
  };
126
137
  const lifecycle = {
127
138
  onReplayStarted: () => {
139
+ reportsReplayPhase = true;
128
140
  isReplaying = true;
129
141
  agent.isRunning = true;
130
142
  },
131
143
  onReplayFinished: () => {
144
+ reportsReplayPhase = true;
132
145
  isReplaying = false;
133
146
  }
134
147
  };
135
148
  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) => {
136
- if (!isReplaying && event.type === _ag_ui_client.EventType.RUN_ERROR) liveErrors.add(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");
137
152
  }), (0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(activeRunDetach$));
138
153
  const applied$ = self.apply(input, source$, subscribers);
139
154
  await (0, rxjs.lastValueFrom)(self.processApplyEvents(input, applied$, subscribers).pipe((0, rxjs_operators.catchError)((error) => {
@@ -342,6 +357,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
342
357
  }
343
358
  };
344
359
  /**
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
+ /**
345
374
  * Type guard for {@link RunCompletionAware}. Returns true when `agent` exposes
346
375
  * an `activeRunCompletionPromise` property, so callers can await an in-flight
347
376
  * run without an `as unknown as` cast. Returns false for agents that don't
@@ -672,6 +701,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
672
701
  };
673
702
  const replayFailed$ = channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, REPLAY_FAILED_EVENT)), (0, rxjs_operators.tap)(() => {
674
703
  controlCursor.replayFailed = true;
704
+ if (options.streamMode === "run") throw new IntelligenceReplayFailedError(credentials.threadId, credentials.runId);
675
705
  }));
676
706
  const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
677
707
  var _options$lifecycle2, _options$lifecycle2$o;
@@ -3607,6 +3637,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3607
3637
  * takes no reason and whose signals never expose one.
3608
3638
  */
3609
3639
  const liveHITLStops = /* @__PURE__ */ new WeakMap();
3640
+ /**
3641
+ * Edits `agent.messages` by replacing the array instead of changing it in
3642
+ * place. The current array may be the one the agent emitted with its last
3643
+ * update, which subscribers and stored snapshots still hold. Like the in-place
3644
+ * edit it replaces, this notifies no subscriber.
3645
+ */
3646
+ function editAgentMessages(agent, edit) {
3647
+ const messages = [...agent.messages];
3648
+ edit(messages);
3649
+ agent.messages = messages;
3650
+ }
3610
3651
  function discardAbortedResult(signal, discardOnAbort, errorMessage) {
3611
3652
  var _liveHITLStops$get;
3612
3653
  return !!(discardOnAbort && (signal === null || signal === void 0 ? void 0 : signal.aborted) && !(((_liveHITLStops$get = liveHITLStops.get(signal)) === null || _liveHITLStops$get === void 0 ? void 0 : _liveHITLStops$get()) && errorMessage));
@@ -3873,7 +3914,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3873
3914
  forwardedProps: this._internal.properties,
3874
3915
  tools: this.buildFrontendTools(agent.agentId),
3875
3916
  context: this._internal.getContextForAgent(agent.agentId)
3876
- }, this.createAgentErrorSubscriber(agent));
3917
+ }, this.createAgentErrorSubscriber(agent, "connect"));
3877
3918
  if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId4 = agent.threadId) !== null && _agent$threadId4 !== void 0 ? _agent$threadId4 : null) !== incomingThreadId) return runAgentResult;
3878
3919
  const controller = new AbortController();
3879
3920
  const replay = (_this$_interactionAbo = this._interactionAbortControllers.get(agent)) !== null && _this$_interactionAbo !== void 0 ? _this$_interactionAbo : {
@@ -3967,7 +4008,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3967
4008
  this._runDepth++;
3968
4009
  try {
3969
4010
  let logicalRunId = resumedRunId !== null && resumedRunId !== void 0 ? resumedRunId : runId;
3970
- const agentSubscriber = this.createAgentErrorSubscriber(agent);
4011
+ const agentSubscriber = this.createAgentErrorSubscriber(agent, "run");
3971
4012
  let started = false;
3972
4013
  const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
3973
4014
  const onRunInitialized = agentSubscriber.onRunInitialized;
@@ -4062,7 +4103,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4062
4103
  if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
4063
4104
  existingResult = void 0;
4064
4105
  const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4065
- if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
4106
+ if (agentMsgIdx !== -1) editAgentMessages(agent, (messages) => messages.splice(agentMsgIdx, 1));
4066
4107
  }
4067
4108
  if (!existingResult) {
4068
4109
  var _this$_interactionAbo2, _interactionControlle;
@@ -4243,7 +4284,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4243
4284
  toolCallId: toolCall.id,
4244
4285
  content: handlerResult.content
4245
4286
  };
4246
- agent.messages.splice(insertAt, 0, toolMessage);
4287
+ editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
4247
4288
  if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
4248
4289
  }
4249
4290
  return false;
@@ -4343,7 +4384,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4343
4384
  toolCallId: toolCall.id,
4344
4385
  content: toolCallResult
4345
4386
  };
4346
- agent.messages.splice(insertAt, 0, toolMessage);
4387
+ editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
4347
4388
  if (!errorMessage && (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.followUp) !== false) return true;
4348
4389
  }
4349
4390
  return false;
@@ -4397,7 +4438,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4397
4438
  content: "",
4398
4439
  toolCalls: [assistantToolCall]
4399
4440
  };
4400
- agent.messages.push(assistantMessage);
4441
+ editAgentMessages(agent, (messages) => messages.push(assistantMessage));
4401
4442
  let handlerResult = {
4402
4443
  content: "",
4403
4444
  result: "",
@@ -4419,8 +4460,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4419
4460
  content: handlerResult.content
4420
4461
  };
4421
4462
  const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
4422
- if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
4423
- else agent.messages.push(toolResultMessage);
4463
+ if (assistantIndex !== -1) editAgentMessages(agent, (messages) => messages.splice(assistantIndex + 1, 0, toolResultMessage));
4464
+ else editAgentMessages(agent, (messages) => messages.push(toolResultMessage));
4424
4465
  if (!handlerResult.error && followUp !== false) {
4425
4466
  if (typeof followUp === "string" && followUp !== "generate") {
4426
4467
  const userMessage = {
@@ -4428,7 +4469,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4428
4469
  role: "user",
4429
4470
  content: followUp
4430
4471
  };
4431
- agent.messages.push(userMessage);
4472
+ editAgentMessages(agent, (messages) => messages.push(userMessage));
4432
4473
  }
4433
4474
  await this._internal.waitForPendingFrameworkUpdates();
4434
4475
  await this.runAgent({ agent });
@@ -4501,7 +4542,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4501
4542
  /**
4502
4543
  * Create an agent error subscriber
4503
4544
  */
4504
- createAgentErrorSubscriber(agent) {
4545
+ createAgentErrorSubscriber(agent, stream) {
4505
4546
  const emitAgentError = async (error, code, extraContext = {}) => {
4506
4547
  const context = { ...extraContext };
4507
4548
  if (agent.agentId) context.agentId = agent.agentId;
@@ -4525,7 +4566,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4525
4566
  await emitAgentError(rawError, CopilotKitCoreErrorCode.AGENT_RUN_ERROR_EVENT, {
4526
4567
  source: "onRunErrorEvent",
4527
4568
  event,
4528
- runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code
4569
+ runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code,
4570
+ phase: stream === "run" ? "live" : ɵgetConnectRunErrorPhase(event)
4529
4571
  });
4530
4572
  }
4531
4573
  };
@@ -6969,6 +7011,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
6969
7011
  CopilotKitCoreErrorCode["AGENT_CONNECT_FAILED"] = "agent_connect_failed";
6970
7012
  CopilotKitCoreErrorCode["AGENT_RUN_FAILED"] = "agent_run_failed";
6971
7013
  CopilotKitCoreErrorCode["AGENT_RUN_FAILED_EVENT"] = "agent_run_failed_event";
7014
+ /**
7015
+ * An agent emitted RUN_ERROR. `context.phase` (`RunErrorPhase`) is
7016
+ * `"replay"` when the error belongs to stored thread history replayed on
7017
+ * connect, `"live"` when the run is happening now, and `"unknown"` when the
7018
+ * transport cannot tell. Show a live-failure notice only for `"live"`.
7019
+ */
6972
7020
  CopilotKitCoreErrorCode["AGENT_RUN_ERROR_EVENT"] = "agent_run_error_event";
6973
7021
  CopilotKitCoreErrorCode["TOOL_ARGUMENT_PARSE_FAILED"] = "tool_argument_parse_failed";
6974
7022
  CopilotKitCoreErrorCode["TOOL_HANDLER_FAILED"] = "tool_handler_failed";
@@ -9093,6 +9141,7 @@ exports.CopilotKitCoreRuntimeConnectionStatus = CopilotKitCoreRuntimeConnectionS
9093
9141
  exports.CopilotKitHeaderResolutionError = CopilotKitHeaderResolutionError;
9094
9142
  exports.INSPECTOR_THREAD_BRIDGE_PLUGIN_ID = INSPECTOR_THREAD_BRIDGE_PLUGIN_ID;
9095
9143
  exports.IntelligenceAgent = IntelligenceAgent;
9144
+ exports.IntelligenceReplayFailedError = IntelligenceReplayFailedError;
9096
9145
  exports.MEMORY_ERROR_REGISTRY = MEMORY_ERROR_REGISTRY;
9097
9146
  exports.MemoryError = MemoryError;
9098
9147
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;