@copilotkit/core 1.55.0-next.8 → 1.55.0-next.9

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
@@ -1243,11 +1243,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1243
1243
  /**
1244
1244
  * Reload suggestions for a specific agent
1245
1245
  * This triggers generation of new suggestions based on current configs
1246
+ *
1247
+ * @param agentId - The consumer agent ID
1248
+ * @param consumerAgent - Optional: the specific agent instance whose messages should be used
1249
+ * for availability filtering and context. When running with per-thread clones, the thread
1250
+ * clone holds the conversation messages; passing it here ensures dynamic suggestions fire
1251
+ * after the first message even though the registry agent has an empty message list.
1246
1252
  */
1247
- reloadSuggestions(agentId) {
1253
+ reloadSuggestions(agentId, consumerAgent) {
1248
1254
  var _agent$messages$lengt, _agent$messages;
1249
1255
  this.clearSuggestions(agentId);
1250
- const agent = this.core.getAgent(agentId);
1256
+ const agent = consumerAgent !== null && consumerAgent !== void 0 ? consumerAgent : this.core.getAgent(agentId);
1251
1257
  if (!agent) return;
1252
1258
  const messageCount = (_agent$messages$lengt = (_agent$messages = agent.messages) === null || _agent$messages === void 0 ? void 0 : _agent$messages.length) !== null && _agent$messages$lengt !== void 0 ? _agent$messages$lengt : 0;
1253
1259
  let hasAnySuggestions = false;
@@ -1260,7 +1266,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1260
1266
  hasAnySuggestions = true;
1261
1267
  this.notifySuggestionsStartedLoading(agentId);
1262
1268
  }
1263
- this.generateSuggestions(suggestionId, config, agentId);
1269
+ this.generateSuggestions(suggestionId, config, agentId, agent);
1264
1270
  } else if (isStaticSuggestionsConfig(config)) this.addStaticSuggestions(suggestionId, config, agentId);
1265
1271
  }
1266
1272
  }
@@ -1289,13 +1295,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1289
1295
  /**
1290
1296
  * Generate suggestions using a provider agent
1291
1297
  */
1292
- async generateSuggestions(suggestionId, config, consumerAgentId) {
1298
+ async generateSuggestions(suggestionId, config, consumerAgentId, consumerAgent) {
1293
1299
  let agent = void 0;
1294
1300
  try {
1295
1301
  var _config$providerAgent, _this$_suggestions$co, _this$_runningSuggest3, _config$minSuggestion, _config$maxSuggestion;
1296
1302
  const suggestionsProviderAgent = this.core.getAgent((_config$providerAgent = config.providerAgentId) !== null && _config$providerAgent !== void 0 ? _config$providerAgent : "default");
1297
1303
  if (!suggestionsProviderAgent) throw new Error(`Suggestions provider agent not found: ${config.providerAgentId}`);
1298
- const suggestionsConsumerAgent = this.core.getAgent(consumerAgentId);
1304
+ const suggestionsConsumerAgent = consumerAgent !== null && consumerAgent !== void 0 ? consumerAgent : this.core.getAgent(consumerAgentId);
1299
1305
  if (!suggestionsConsumerAgent) throw new Error(`Suggestions consumer agent not found: ${consumerAgentId}`);
1300
1306
  agent = suggestionsProviderAgent.clone();
1301
1307
  agent.threadId = suggestionId;
@@ -1636,6 +1642,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1636
1642
  if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
1637
1643
  if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
1638
1644
  if (agent.detachActiveRun) await agent.detachActiveRun();
1645
+ this._internal.subscribeAgentToStateManager(agent);
1639
1646
  const isTopLevel = this._runDepth === 0;
1640
1647
  let originalAbortRun;
1641
1648
  if (isTopLevel) {
@@ -1707,7 +1714,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1707
1714
  await this._internal.waitForPendingFrameworkUpdates();
1708
1715
  return await this.runAgent({ agent });
1709
1716
  }
1710
- this._internal.suggestionEngine.reloadSuggestions(agentId);
1717
+ this._internal.suggestionEngine.reloadSuggestions(agentId, agent);
1711
1718
  return runAgentResult;
1712
1719
  }
1713
1720
  /**
@@ -2127,6 +2134,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2127
2134
  this.core = core;
2128
2135
  _defineProperty(this, "stateByRun", /* @__PURE__ */ new Map());
2129
2136
  _defineProperty(this, "messageToRun", /* @__PURE__ */ new Map());
2137
+ _defineProperty(this, "activeRun", /* @__PURE__ */ new Map());
2130
2138
  _defineProperty(this, "agentSubscriptions", /* @__PURE__ */ new Map());
2131
2139
  }
2132
2140
  /**
@@ -2134,36 +2142,73 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2134
2142
  */
2135
2143
  initialize() {}
2136
2144
  /**
2137
- * Subscribe to an agent's events to track state and messages
2145
+ * Subscribe to an agent's events to track state and messages.
2146
+ *
2147
+ * Registry agents (subscribed via `onAgentsChanged`) use the bare `agentId`
2148
+ * key so that `unsubscribeFromAgent(agentId)` can remove them when they
2149
+ * are replaced. Per-thread clones (subscribed via `subscribeAgentToStateManager`)
2150
+ * pass `{ isClone: true }` to use a composite `agentId:threadId` key, keeping
2151
+ * their subscription independent of the registry agent's.
2138
2152
  */
2139
- subscribeToAgent(agent) {
2153
+ subscribeToAgent(agent, { isClone = false } = {}) {
2140
2154
  if (!agent.agentId) return;
2141
2155
  const agentId = agent.agentId;
2142
- this.unsubscribeFromAgent(agentId);
2156
+ const subscriptionKey = isClone && agent.threadId ? `${agentId}:${agent.threadId}` : agentId;
2157
+ const existingUnsubscribe = this.agentSubscriptions.get(subscriptionKey);
2158
+ if (existingUnsubscribe) {
2159
+ existingUnsubscribe();
2160
+ this.agentSubscriptions.delete(subscriptionKey);
2161
+ }
2162
+ let revoked = false;
2163
+ let subRunId;
2164
+ let runFinished = false;
2165
+ const effectiveInput = (input) => {
2166
+ var _subRunId;
2167
+ return {
2168
+ ...input,
2169
+ runId: (_subRunId = subRunId) !== null && _subRunId !== void 0 ? _subRunId : input.runId
2170
+ };
2171
+ };
2143
2172
  const { unsubscribe } = agent.subscribe({
2144
- onRunStartedEvent: ({ event, state }) => {
2145
- this.handleRunStarted(agent, event, state);
2173
+ onRunStartedEvent: ({ input, state }) => {
2174
+ if (revoked) return;
2175
+ if (runFinished && input.runId === subRunId) subRunId = (0, _ag_ui_client.randomUUID)();
2176
+ else subRunId = input.runId;
2177
+ runFinished = false;
2178
+ this.handleRunStarted(agent, effectiveInput(input), state);
2146
2179
  },
2147
- onRunFinishedEvent: ({ event, state }) => {
2148
- this.handleRunFinished(agent, event, state);
2180
+ onRunFinishedEvent: ({ input, state }) => {
2181
+ if (revoked) return;
2182
+ runFinished = true;
2183
+ this.handleRunFinished(agent, effectiveInput(input), state);
2149
2184
  },
2150
2185
  onStateSnapshotEvent: ({ event, input, state }) => {
2151
- this.handleStateSnapshot(agent, event, input, state);
2186
+ if (revoked) return;
2187
+ this.handleStateSnapshot(agent, event, effectiveInput(input), state);
2152
2188
  },
2153
2189
  onStateDeltaEvent: ({ event, input, state }) => {
2154
- this.handleStateDelta(agent, event, input, state);
2190
+ if (revoked) return;
2191
+ this.handleStateDelta(agent, event, effectiveInput(input), state);
2155
2192
  },
2156
2193
  onMessagesSnapshotEvent: ({ event, input, messages }) => {
2157
- this.handleMessagesSnapshot(agent, event, input, messages);
2194
+ if (revoked) return;
2195
+ this.handleMessagesSnapshot(agent, event, effectiveInput(input), messages);
2158
2196
  },
2159
2197
  onNewMessage: ({ message, input }) => {
2160
- this.handleNewMessage(agent, message, input);
2198
+ if (revoked) return;
2199
+ this.handleNewMessage(agent, message, input ? effectiveInput(input) : void 0);
2161
2200
  }
2162
2201
  });
2163
- this.agentSubscriptions.set(agentId, unsubscribe);
2202
+ this.agentSubscriptions.set(subscriptionKey, () => {
2203
+ revoked = true;
2204
+ unsubscribe();
2205
+ });
2164
2206
  }
2165
2207
  /**
2166
- * Unsubscribe from an agent's events
2208
+ * Unsubscribe a registry agent's subscription (bare `agentId` key).
2209
+ * Per-thread clone subscriptions use composite `agentId:threadId` keys and
2210
+ * are replaced (not removed) by subsequent subscribeToAgent(agent, { isClone: true })
2211
+ * calls for the same (agentId, threadId) pair.
2167
2212
  */
2168
2213
  unsubscribeFromAgent(agentId) {
2169
2214
  const unsubscribe = this.agentSubscriptions.get(agentId);
@@ -2207,18 +2252,20 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2207
2252
  /**
2208
2253
  * Handle run started event
2209
2254
  */
2210
- handleRunStarted(agent, event, state) {
2255
+ handleRunStarted(agent, input, state) {
2211
2256
  if (!agent.agentId) return;
2212
- const { threadId, runId } = event;
2213
- this.saveState(agent.agentId, threadId, runId, state);
2257
+ const { threadId, runId } = input;
2258
+ this.activeRun.set(`${agent.agentId}:${threadId}`, runId);
2259
+ if (state && Object.keys(state).length > 0) this.saveState(agent.agentId, threadId, runId, state);
2214
2260
  }
2215
2261
  /**
2216
2262
  * Handle run finished event
2217
2263
  */
2218
- handleRunFinished(agent, event, state) {
2264
+ handleRunFinished(agent, input, state) {
2219
2265
  if (!agent.agentId) return;
2220
- const { threadId, runId } = event;
2221
- this.saveState(agent.agentId, threadId, runId, state);
2266
+ const { threadId, runId } = input;
2267
+ this.activeRun.delete(`${agent.agentId}:${threadId}`);
2268
+ if (state && Object.keys(state).length > 0) this.saveState(agent.agentId, threadId, runId, state);
2222
2269
  }
2223
2270
  /**
2224
2271
  * Handle state snapshot event
@@ -2252,7 +2299,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2252
2299
  * Handle new message event
2253
2300
  */
2254
2301
  handleNewMessage(agent, message, input) {
2255
- if (!agent.agentId || !input) return;
2302
+ if (!agent.agentId) return;
2303
+ if (!input) {
2304
+ var _agent$threadId;
2305
+ const threadId = (_agent$threadId = agent.threadId) !== null && _agent$threadId !== void 0 ? _agent$threadId : "";
2306
+ const runId = this.activeRun.get(`${agent.agentId}:${threadId}`);
2307
+ if (runId) this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
2308
+ return;
2309
+ }
2256
2310
  const { threadId, runId } = input;
2257
2311
  this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
2258
2312
  }
@@ -2572,6 +2626,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2572
2626
  getRunIdsForThread(agentId, threadId) {
2573
2627
  return this.stateManager.getRunIdsForThread(agentId, threadId);
2574
2628
  }
2629
+ subscribeAgentToStateManager(agent) {
2630
+ this.stateManager.subscribeToAgent(agent, { isClone: true });
2631
+ }
2575
2632
  /**
2576
2633
  * Internal method used by RunHandler to build frontend tools
2577
2634
  */