@copilotkit/core 1.84.1 → 1.84.2-canary.1793809041

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,12 @@ 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") {
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
+ }
675
710
  }));
676
711
  const replayComplete$ = this.observeControlEvent$(input.threadId, channel$, REPLAY_COMPLETE_EVENT, controlCursor).pipe((0, rxjs_operators.tap)(() => {
677
712
  var _options$lifecycle2, _options$lifecycle2$o;
@@ -4158,6 +4193,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4158
4193
  */
4159
4194
  const liveHITLStops = /* @__PURE__ */ new WeakMap();
4160
4195
  /**
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
+ /**
4161
4207
  * The message a failed tool call reports. An error thrown with an empty
4162
4208
  * message still means the call failed, so it gets a fallback rather than an
4163
4209
  * empty string, which everything downstream reads as success.
@@ -4447,7 +4493,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4447
4493
  forwardedProps: this._internal.getPropertiesForAgent(agent.agentId),
4448
4494
  tools: this.buildFrontendTools(agent.agentId),
4449
4495
  context: this._internal.getContextForAgent(agent.agentId)
4450
- }, this.createAgentErrorSubscriber(agent));
4496
+ }, this.createAgentErrorSubscriber(agent, "connect"));
4451
4497
  if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId4 = agent.threadId) !== null && _agent$threadId4 !== void 0 ? _agent$threadId4 : null) !== incomingThreadId) return runAgentResult;
4452
4498
  const controller = new AbortController();
4453
4499
  const replay = (_this$_interactionAbo = this._interactionAbortControllers.get(agent)) !== null && _this$_interactionAbo !== void 0 ? _this$_interactionAbo : {
@@ -4556,7 +4602,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4556
4602
  });
4557
4603
  }, "Subscriber onAgentRunStarted error:");
4558
4604
  let logicalRunId = resumedRunId !== null && resumedRunId !== void 0 ? resumedRunId : runId;
4559
- const agentSubscriber = this.createAgentErrorSubscriber(agent, controller.signal);
4605
+ const agentSubscriber = this.createAgentErrorSubscriber(agent, "run", controller.signal);
4560
4606
  let started = false;
4561
4607
  const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
4562
4608
  const onRunInitialized = agentSubscriber.onRunInitialized;
@@ -4683,7 +4729,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4683
4729
  if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
4684
4730
  existingResult = void 0;
4685
4731
  const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
4686
- if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
4732
+ if (agentMsgIdx !== -1) editAgentMessages(agent, (messages) => messages.splice(agentMsgIdx, 1));
4687
4733
  }
4688
4734
  if (!existingResult) {
4689
4735
  var _this$_interactionAbo2, _interactionControlle;
@@ -4909,7 +4955,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4909
4955
  content: handlerResult.content,
4910
4956
  ...handlerResult.error ? { error: handlerResult.error } : {}
4911
4957
  };
4912
- agent.messages.splice(insertAt, 0, toolMessage);
4958
+ editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
4913
4959
  if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
4914
4960
  }
4915
4961
  return false;
@@ -5008,7 +5054,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5008
5054
  content: toolCallResult,
5009
5055
  ...errorMessage ? { error: errorMessage } : {}
5010
5056
  };
5011
- agent.messages.splice(insertAt, 0, toolMessage);
5057
+ editAgentMessages(agent, (messages) => messages.splice(insertAt, 0, toolMessage));
5012
5058
  if (!errorMessage && (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.followUp) !== false) return true;
5013
5059
  }
5014
5060
  return false;
@@ -5062,7 +5108,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5062
5108
  content: "",
5063
5109
  toolCalls: [assistantToolCall]
5064
5110
  };
5065
- agent.messages.push(assistantMessage);
5111
+ editAgentMessages(agent, (messages) => messages.push(assistantMessage));
5066
5112
  let handlerResult = {
5067
5113
  content: "",
5068
5114
  result: "",
@@ -5085,8 +5131,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5085
5131
  ...handlerResult.error ? { error: handlerResult.error } : {}
5086
5132
  };
5087
5133
  const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
5088
- if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
5089
- else agent.messages.push(toolResultMessage);
5134
+ if (assistantIndex !== -1) editAgentMessages(agent, (messages) => messages.splice(assistantIndex + 1, 0, toolResultMessage));
5135
+ else editAgentMessages(agent, (messages) => messages.push(toolResultMessage));
5090
5136
  if (!handlerResult.error && followUp !== false) {
5091
5137
  if (typeof followUp === "string" && followUp !== "generate") {
5092
5138
  const userMessage = {
@@ -5094,7 +5140,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5094
5140
  role: "user",
5095
5141
  content: followUp
5096
5142
  };
5097
- agent.messages.push(userMessage);
5143
+ editAgentMessages(agent, (messages) => messages.push(userMessage));
5098
5144
  }
5099
5145
  await this._internal.waitForPendingFrameworkUpdates();
5100
5146
  await this.runAgent({ agent });
@@ -5167,7 +5213,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5167
5213
  /**
5168
5214
  * Create an agent error subscriber
5169
5215
  */
5170
- createAgentErrorSubscriber(agent, runSignal) {
5216
+ createAgentErrorSubscriber(agent, stream, runSignal) {
5171
5217
  const emitAgentError = async (error, code, extraContext = {}) => {
5172
5218
  const context = { ...extraContext };
5173
5219
  if (agent.agentId) context.agentId = agent.agentId;
@@ -5198,7 +5244,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5198
5244
  await emitAgentError(rawError, CopilotKitCoreErrorCode.AGENT_RUN_ERROR_EVENT, {
5199
5245
  source: "onRunErrorEvent",
5200
5246
  event,
5201
- runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code
5247
+ runtimeErrorCode: event === null || event === void 0 ? void 0 : event.code,
5248
+ phase: stream === "run" ? "live" : ɵgetConnectRunErrorPhase(event)
5202
5249
  });
5203
5250
  }
5204
5251
  };
@@ -7662,6 +7709,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
7662
7709
  CopilotKitCoreErrorCode["AGENT_CONNECT_FAILED"] = "agent_connect_failed";
7663
7710
  CopilotKitCoreErrorCode["AGENT_RUN_FAILED"] = "agent_run_failed";
7664
7711
  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
+ */
7665
7718
  CopilotKitCoreErrorCode["AGENT_RUN_ERROR_EVENT"] = "agent_run_error_event";
7666
7719
  CopilotKitCoreErrorCode["TOOL_ARGUMENT_PARSE_FAILED"] = "tool_argument_parse_failed";
7667
7720
  CopilotKitCoreErrorCode["TOOL_HANDLER_FAILED"] = "tool_handler_failed";
@@ -11111,6 +11164,7 @@ exports.CopilotKitCoreRuntimeConnectionStatus = CopilotKitCoreRuntimeConnectionS
11111
11164
  exports.CopilotKitHeaderResolutionError = CopilotKitHeaderResolutionError;
11112
11165
  exports.INSPECTOR_THREAD_BRIDGE_PLUGIN_ID = INSPECTOR_THREAD_BRIDGE_PLUGIN_ID;
11113
11166
  exports.IntelligenceAgent = IntelligenceAgent;
11167
+ exports.IntelligenceReplayFailedError = IntelligenceReplayFailedError;
11114
11168
  exports.MEMORY_ERROR_REGISTRY = MEMORY_ERROR_REGISTRY;
11115
11169
  exports.MemoryError = MemoryError;
11116
11170
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;