@copilotkit/core 1.68.2 → 1.68.3-canary.1787220377

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
@@ -3148,6 +3148,129 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3148
3148
  }
3149
3149
  };
3150
3150
 
3151
+ //#endregion
3152
+ //#region src/core/subagent-registry.ts
3153
+ /**
3154
+ * Tracks subagent lifecycle events (SUBAGENT_STARTED / FINISHED / ERROR) per
3155
+ * owning agent so consumers can resolve a message's `subagentRunId` to a name,
3156
+ * description, and running status.
3157
+ *
3158
+ * Modeled on {@link SuggestionEngine}: an in-memory `Record` keyed by (owning)
3159
+ * agentId, mutated from streamed AG-UI events, made reactive by calling
3160
+ * `notifySubscribers(onSubagentsChanged)` after every change. It owns its own
3161
+ * per-agent `agent.subscribe` (like {@link StateManager}) so it is fully
3162
+ * self-contained and adds no coupling to the state/message tracking path.
3163
+ *
3164
+ * No-op for runs without subagents (a normal LangGraph run never emits
3165
+ * SUBAGENT_* events), so the registry simply stays empty.
3166
+ */
3167
+ var SubagentRegistry = class {
3168
+ constructor(core) {
3169
+ this.core = core;
3170
+ _defineProperty(this, "_subagents", {});
3171
+ _defineProperty(this, "agentSubscriptions", /* @__PURE__ */ new Map());
3172
+ }
3173
+ /**
3174
+ * Subscribe to an agent's subagent lifecycle events. Idempotent per agentId:
3175
+ * an existing subscription for the same agent is revoked and replaced.
3176
+ */
3177
+ subscribeToAgent(agent) {
3178
+ if (!agent.agentId) return;
3179
+ const agentId = agent.agentId;
3180
+ const existingUnsubscribe = this.agentSubscriptions.get(agentId);
3181
+ if (existingUnsubscribe) {
3182
+ existingUnsubscribe();
3183
+ this.agentSubscriptions.delete(agentId);
3184
+ }
3185
+ let revoked = false;
3186
+ const { unsubscribe } = agent.subscribe({
3187
+ onSubagentStartedEvent: ({ event }) => {
3188
+ if (revoked) return;
3189
+ this.handleStarted(agentId, event);
3190
+ },
3191
+ onSubagentFinishedEvent: ({ event }) => {
3192
+ if (revoked) return;
3193
+ this.handleFinished(agentId, event);
3194
+ },
3195
+ onSubagentErrorEvent: ({ event }) => {
3196
+ if (revoked) return;
3197
+ this.handleError(agentId, event);
3198
+ }
3199
+ });
3200
+ this.agentSubscriptions.set(agentId, () => {
3201
+ revoked = true;
3202
+ unsubscribe();
3203
+ });
3204
+ }
3205
+ /**
3206
+ * Unsubscribe an agent's subscription (symmetric with StateManager cleanup).
3207
+ */
3208
+ unsubscribeFromAgent(agentId) {
3209
+ const unsubscribe = this.agentSubscriptions.get(agentId);
3210
+ if (unsubscribe) {
3211
+ unsubscribe();
3212
+ this.agentSubscriptions.delete(agentId);
3213
+ }
3214
+ }
3215
+ /**
3216
+ * Get the subagents observed for an owning agent, keyed by subagentRunId.
3217
+ * Returns a fresh shallow copy each call so consumers (and React state) see
3218
+ * a new reference when anything changed.
3219
+ */
3220
+ getSubagents(agentId) {
3221
+ return { ...this._subagents[agentId] };
3222
+ }
3223
+ handleStarted(agentId, event) {
3224
+ var _this$_subagents, _this$_subagents$agen;
3225
+ const bucket = (_this$_subagents$agen = (_this$_subagents = this._subagents)[agentId]) !== null && _this$_subagents$agen !== void 0 ? _this$_subagents$agen : _this$_subagents[agentId] = {};
3226
+ bucket[event.subagentRunId] = {
3227
+ subagentRunId: event.subagentRunId,
3228
+ name: event.name,
3229
+ description: event.description,
3230
+ parentSubagentRunId: event.parentSubagentRunId,
3231
+ status: "running"
3232
+ };
3233
+ this.notifySubagentsChanged(agentId);
3234
+ }
3235
+ handleFinished(agentId, event) {
3236
+ const bucket = this._subagents[agentId];
3237
+ const existing = bucket === null || bucket === void 0 ? void 0 : bucket[event.subagentRunId];
3238
+ if (!bucket || !existing) return;
3239
+ bucket[event.subagentRunId] = {
3240
+ ...existing,
3241
+ status: "finished"
3242
+ };
3243
+ this.notifySubagentsChanged(agentId);
3244
+ }
3245
+ handleError(agentId, event) {
3246
+ var _this$_subagents2, _this$_subagents2$age;
3247
+ const bucket = (_this$_subagents2$age = (_this$_subagents2 = this._subagents)[agentId]) !== null && _this$_subagents2$age !== void 0 ? _this$_subagents2$age : _this$_subagents2[agentId] = {};
3248
+ const existing = bucket[event.subagentRunId];
3249
+ bucket[event.subagentRunId] = existing ? {
3250
+ ...existing,
3251
+ status: "error",
3252
+ error: event.message
3253
+ } : {
3254
+ subagentRunId: event.subagentRunId,
3255
+ name: event.subagentRunId,
3256
+ status: "error",
3257
+ error: event.message
3258
+ };
3259
+ this.notifySubagentsChanged(agentId);
3260
+ }
3261
+ async notifySubagentsChanged(agentId) {
3262
+ const subagents = this.getSubagents(agentId);
3263
+ await this.core.notifySubscribers((subscriber) => {
3264
+ var _subscriber$onSubagen;
3265
+ return (_subscriber$onSubagen = subscriber.onSubagentsChanged) === null || _subscriber$onSubagen === void 0 ? void 0 : _subscriber$onSubagen.call(subscriber, {
3266
+ copilotkit: this.core,
3267
+ agentId,
3268
+ subagents
3269
+ });
3270
+ }, "Subscriber onSubagentsChanged error:");
3271
+ }
3272
+ };
3273
+
3151
3274
  //#endregion
3152
3275
  //#region src/core/thread-store-registry.ts
3153
3276
  var ThreadStoreRegistry = class {
@@ -4348,6 +4471,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4348
4471
  _defineProperty(this, "suggestionEngine", void 0);
4349
4472
  _defineProperty(this, "runHandler", void 0);
4350
4473
  _defineProperty(this, "stateManager", void 0);
4474
+ _defineProperty(this, "subagentRegistry", void 0);
4351
4475
  _defineProperty(this, "threadStoreRegistry", void 0);
4352
4476
  _defineProperty(this, "_memoryStore", void 0);
4353
4477
  _defineProperty(this, "previousAgentIds", /* @__PURE__ */ new Set());
@@ -4360,6 +4484,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4360
4484
  this.suggestionEngine = new SuggestionEngine(this);
4361
4485
  this.runHandler = new RunHandler(this);
4362
4486
  this.stateManager = new StateManager(this);
4487
+ this.subagentRegistry = new SubagentRegistry(this);
4363
4488
  this.threadStoreRegistry = new ThreadStoreRegistry(this);
4364
4489
  this.agentRegistry.initialize(agents__unsafe_dev_only);
4365
4490
  this.runHandler.initialize(tools);
@@ -4374,7 +4499,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4374
4499
  },
4375
4500
  onAgentsChanged: ({ agents }) => {
4376
4501
  Object.values(agents).forEach((agent) => {
4377
- if (agent.agentId) this.stateManager.subscribeToAgent(agent);
4502
+ if (agent.agentId) {
4503
+ this.stateManager.subscribeToAgent(agent);
4504
+ this.subagentRegistry.subscribeToAgent(agent);
4505
+ }
4378
4506
  });
4379
4507
  const currentAgentIds = new Set(Object.keys(agents));
4380
4508
  for (const agentId of Object.keys(this.threadStoreRegistry.getAll())) if (this.previousAgentIds.has(agentId) && !currentAgentIds.has(agentId)) try {
@@ -4382,10 +4510,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4382
4510
  } catch (err) {
4383
4511
  console.error(`CopilotKitCore.onAgentsChanged: threadStoreRegistry.unregister failed for "${agentId}":`, err);
4384
4512
  }
4385
- for (const agentId of this.previousAgentIds) if (!currentAgentIds.has(agentId)) try {
4386
- this.stateManager.unsubscribeFromAgent(agentId);
4387
- } catch (err) {
4388
- console.error(`CopilotKitCore.onAgentsChanged: stateManager.unsubscribeFromAgent failed for "${agentId}":`, err);
4513
+ for (const agentId of this.previousAgentIds) if (!currentAgentIds.has(agentId)) {
4514
+ try {
4515
+ this.stateManager.unsubscribeFromAgent(agentId);
4516
+ } catch (err) {
4517
+ console.error(`CopilotKitCore.onAgentsChanged: stateManager.unsubscribeFromAgent failed for "${agentId}":`, err);
4518
+ }
4519
+ try {
4520
+ this.subagentRegistry.unsubscribeFromAgent(agentId);
4521
+ } catch (err) {
4522
+ console.error(`CopilotKitCore.onAgentsChanged: subagentRegistry.unsubscribeFromAgent failed for "${agentId}":`, err);
4523
+ }
4389
4524
  }
4390
4525
  this.previousAgentIds = currentAgentIds;
4391
4526
  }
@@ -4765,6 +4900,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4765
4900
  return this.suggestionEngine.getSuggestions(agentId);
4766
4901
  }
4767
4902
  /**
4903
+ * Subagent registry (delegated to SubagentRegistry).
4904
+ *
4905
+ * Returns the subagents observed for an owning agent during its run(s),
4906
+ * keyed by `subagentRunId`, each carrying `name` / `description` / `status`.
4907
+ * Empty for runs that never emit SUBAGENT_* events.
4908
+ */
4909
+ getSubagents(agentId) {
4910
+ return this.subagentRegistry.getSubagents(agentId);
4911
+ }
4912
+ /**
4768
4913
  * Tool management (delegated to RunHandler)
4769
4914
  */
4770
4915
  addTool(tool) {
@@ -6108,6 +6253,7 @@ exports.MemoryError = MemoryError;
6108
6253
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;
6109
6254
  exports.RunHandler = RunHandler;
6110
6255
  exports.StateManager = StateManager;
6256
+ exports.SubagentRegistry = SubagentRegistry;
6111
6257
  exports.SuggestionEngine = SuggestionEngine;
6112
6258
  exports.ToolCallStatus = ToolCallStatus;
6113
6259
  exports.completePartialMarkdown = completePartialMarkdown;