@copilotkit/core 1.66.2-canary.1785878925 → 1.66.3

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
@@ -2084,10 +2084,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2084
2084
  /**
2085
2085
  * Run an agent
2086
2086
  */
2087
- async runAgent({ agent, forwardedProps, resume, runId }) {
2087
+ async runAgent({ agent, forwardedProps, resume, runId }, continuationHandoff) {
2088
2088
  if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
2089
2089
  this._internal.applyHeadersToAgent(agent);
2090
- if (agent.detachActiveRun) await agent.detachActiveRun();
2090
+ if (agent.detachActiveRun) try {
2091
+ await agent.detachActiveRun();
2092
+ } catch (error) {
2093
+ continuationHandoff === null || continuationHandoff === void 0 || continuationHandoff.cancel();
2094
+ throw error;
2095
+ }
2091
2096
  const isTopLevel = this._runDepth === 0;
2092
2097
  let originalAbortRun;
2093
2098
  if (isTopLevel) {
@@ -2108,21 +2113,40 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2108
2113
  }
2109
2114
  this._runDepth++;
2110
2115
  try {
2111
- const runAgentResult = await agent.runAgent({
2116
+ let logicalRunId = runId;
2117
+ const agentSubscriber = this.createAgentErrorSubscriber(agent);
2118
+ let started = false;
2119
+ const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
2120
+ const onRunInitialized = agentSubscriber.onRunInitialized;
2121
+ agentSubscriber.onRunInitialized = async (params) => {
2122
+ continuationHandoff === null || continuationHandoff === void 0 || continuationHandoff.bind(params.input);
2123
+ return onRunInitialized === null || onRunInitialized === void 0 ? void 0 : onRunInitialized(params);
2124
+ };
2125
+ agentSubscriber.onRunStartedEvent = async (params) => {
2126
+ started = true;
2127
+ if (!continuationHandoff) logicalRunId = params.input.runId;
2128
+ return onRunStartedEvent === null || onRunStartedEvent === void 0 ? void 0 : onRunStartedEvent(params);
2129
+ };
2130
+ const pinRunIdOnWire = runId !== void 0 && !continuationHandoff;
2131
+ const agentRunInput = {
2112
2132
  forwardedProps: {
2113
2133
  ...this._internal.properties,
2114
2134
  ...forwardedProps
2115
2135
  },
2116
2136
  ...resume !== void 0 ? { resume } : {},
2117
- ...runId !== void 0 ? { runId } : {},
2137
+ ...pinRunIdOnWire ? { runId } : {},
2118
2138
  tools: this.buildFrontendTools(agent.agentId),
2119
2139
  context: this._internal.getContextForAgent(agent.agentId)
2120
- }, this.createAgentErrorSubscriber(agent));
2140
+ };
2141
+ const runAgentResult = await agent.runAgent(agentRunInput, agentSubscriber);
2142
+ if (!started) continuationHandoff === null || continuationHandoff === void 0 || continuationHandoff.cancel();
2121
2143
  return await this.processAgentResult({
2122
2144
  runAgentResult,
2123
- agent
2145
+ agent,
2146
+ runId: logicalRunId
2124
2147
  });
2125
2148
  } catch (error) {
2149
+ continuationHandoff === null || continuationHandoff === void 0 || continuationHandoff.cancel();
2126
2150
  const runError = error instanceof Error ? error : new Error(String(error));
2127
2151
  const context = {};
2128
2152
  if (agent.agentId) context.agentId = agent.agentId;
@@ -2143,7 +2167,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2143
2167
  /**
2144
2168
  * Process agent result and execute tools
2145
2169
  */
2146
- async processAgentResult({ runAgentResult, agent, executeFrontendTools = true }) {
2170
+ async processAgentResult({ runAgentResult, agent, runId, executeFrontendTools = true }) {
2147
2171
  var _this$_runAbortContro2;
2148
2172
  const { newMessages } = runAgentResult;
2149
2173
  const agentId = agent.agentId;
@@ -2185,7 +2209,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2185
2209
  if (needsFollowUp && !((_this$_runAbortContro2 = this._runAbortController) === null || _this$_runAbortContro2 === void 0 ? void 0 : _this$_runAbortContro2.signal.aborted)) if (this._runDepth >= MAX_FOLLOW_UP_DEPTH) _copilotkit_shared.logger.warn(`[CopilotKit] Follow-up depth limit (${MAX_FOLLOW_UP_DEPTH}) reached for agent "${agentId}". Stopping recursive follow-up runs to prevent an infinite loop. This usually indicates a tool that keeps requesting a follow-up (e.g. the LLM repeatedly calling the same tool). Consider setting "followUp: false" on the offending tool.`);
2186
2210
  else {
2187
2211
  await this._internal.waitForPendingFrameworkUpdates();
2188
- return await this.runAgent({ agent });
2212
+ const continuationHandoff = this._internal.stateManager.markNextRunAsContinuation(agent, runId);
2213
+ return await this.runAgent({
2214
+ agent,
2215
+ ...runId !== void 0 ? { runId } : {}
2216
+ }, continuationHandoff);
2189
2217
  }
2190
2218
  this._internal.suggestionEngine.reloadSuggestions(agentId);
2191
2219
  return runAgentResult;
@@ -2642,7 +2670,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2642
2670
 
2643
2671
  //#endregion
2644
2672
  //#region src/core/state-manager.ts
2645
- /**
2673
+ const isContinuation = (input) => {
2674
+ var _command, _input$forwardedProps;
2675
+ return input.resume !== void 0 || Object.prototype.hasOwnProperty.call((_command = (_input$forwardedProps = input.forwardedProps) === null || _input$forwardedProps === void 0 ? void 0 : _input$forwardedProps.command) !== null && _command !== void 0 ? _command : {}, "resume");
2676
+ };
2677
+ /**
2646
2678
  * Manages state and message tracking by run for CopilotKitCore.
2647
2679
  * Tracks agent state snapshots and message-to-run associations.
2648
2680
  */
@@ -2653,11 +2685,34 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2653
2685
  _defineProperty(this, "messageToRun", /* @__PURE__ */ new Map());
2654
2686
  _defineProperty(this, "activeRun", /* @__PURE__ */ new Map());
2655
2687
  _defineProperty(this, "agentSubscriptions", /* @__PURE__ */ new Map());
2688
+ _defineProperty(this, "pendingContinuations", /* @__PURE__ */ new WeakMap());
2656
2689
  }
2657
2690
  /**
2658
2691
  * Initialize state tracking for an agent
2659
2692
  */
2660
2693
  initialize() {}
2694
+ markNextRunAsContinuation(agent, expectedRunId) {
2695
+ let pendingForAgent = this.pendingContinuations.get(agent);
2696
+ if (!pendingForAgent) {
2697
+ pendingForAgent = /* @__PURE__ */ new Set();
2698
+ this.pendingContinuations.set(agent, pendingForAgent);
2699
+ }
2700
+ const pending = {
2701
+ active: true,
2702
+ expectedRunId,
2703
+ bind: (input) => {
2704
+ if (pending.active) pending.expectedInput = input;
2705
+ },
2706
+ cancel: () => {
2707
+ if (!pending.active) return;
2708
+ pending.active = false;
2709
+ pendingForAgent.delete(pending);
2710
+ if (pendingForAgent.size === 0) this.pendingContinuations.delete(agent);
2711
+ }
2712
+ };
2713
+ pendingForAgent.add(pending);
2714
+ return pending;
2715
+ }
2661
2716
  /**
2662
2717
  * Subscribe to an agent's events to track state and messages.
2663
2718
  */
@@ -2682,8 +2737,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2682
2737
  const { unsubscribe } = agent.subscribe({
2683
2738
  onRunStartedEvent: ({ input, state }) => {
2684
2739
  if (revoked) return;
2685
- if (runFinished && input.runId === subRunId) subRunId = (0, _ag_ui_client.randomUUID)();
2686
- else subRunId = input.runId;
2740
+ const pendingForAgent = this.pendingContinuations.get(agent);
2741
+ const internalContinuation = [...pendingForAgent !== null && pendingForAgent !== void 0 ? pendingForAgent : []].find((pending) => pending.expectedInput === input);
2742
+ internalContinuation === null || internalContinuation === void 0 || internalContinuation.cancel();
2743
+ if (runFinished && input.runId === subRunId && !internalContinuation && !isContinuation(input)) subRunId = (0, _ag_ui_client.randomUUID)();
2744
+ else {
2745
+ var _internalContinuation;
2746
+ subRunId = (_internalContinuation = internalContinuation === null || internalContinuation === void 0 ? void 0 : internalContinuation.expectedRunId) !== null && _internalContinuation !== void 0 ? _internalContinuation : input.runId;
2747
+ }
2687
2748
  runFinished = false;
2688
2749
  this.handleRunStarted(agent, effectiveInput(input), state);
2689
2750
  },
@@ -2716,6 +2777,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2716
2777
  });
2717
2778
  this.agentSubscriptions.set(agentId, () => {
2718
2779
  revoked = true;
2780
+ this.pendingContinuations.delete(agent);
2719
2781
  unsubscribe();
2720
2782
  });
2721
2783
  }