@copilotkit/core 1.68.1 → 1.68.3-canary.1786716392

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.cjs CHANGED
@@ -3003,6 +3003,124 @@ var StateManager = class {
3003
3003
  }
3004
3004
  };
3005
3005
 
3006
+ //#endregion
3007
+ //#region src/core/subagent-registry.ts
3008
+ /**
3009
+ * Tracks subagent lifecycle events (SUBAGENT_STARTED / FINISHED / ERROR) per
3010
+ * owning agent so consumers can resolve a message's `subagentRunId` to a name,
3011
+ * description, and running status.
3012
+ *
3013
+ * Modeled on {@link SuggestionEngine}: an in-memory `Record` keyed by (owning)
3014
+ * agentId, mutated from streamed AG-UI events, made reactive by calling
3015
+ * `notifySubscribers(onSubagentsChanged)` after every change. It owns its own
3016
+ * per-agent `agent.subscribe` (like {@link StateManager}) so it is fully
3017
+ * self-contained and adds no coupling to the state/message tracking path.
3018
+ *
3019
+ * No-op for runs without subagents (a normal LangGraph run never emits
3020
+ * SUBAGENT_* events), so the registry simply stays empty.
3021
+ */
3022
+ var SubagentRegistry = class {
3023
+ _subagents = {};
3024
+ agentSubscriptions = /* @__PURE__ */ new Map();
3025
+ constructor(core) {
3026
+ this.core = core;
3027
+ }
3028
+ /**
3029
+ * Subscribe to an agent's subagent lifecycle events. Idempotent per agentId:
3030
+ * an existing subscription for the same agent is revoked and replaced.
3031
+ */
3032
+ subscribeToAgent(agent) {
3033
+ if (!agent.agentId) return;
3034
+ const agentId = agent.agentId;
3035
+ const existingUnsubscribe = this.agentSubscriptions.get(agentId);
3036
+ if (existingUnsubscribe) {
3037
+ existingUnsubscribe();
3038
+ this.agentSubscriptions.delete(agentId);
3039
+ }
3040
+ let revoked = false;
3041
+ const { unsubscribe } = agent.subscribe({
3042
+ onSubagentStartedEvent: ({ event }) => {
3043
+ if (revoked) return;
3044
+ this.handleStarted(agentId, event);
3045
+ },
3046
+ onSubagentFinishedEvent: ({ event }) => {
3047
+ if (revoked) return;
3048
+ this.handleFinished(agentId, event);
3049
+ },
3050
+ onSubagentErrorEvent: ({ event }) => {
3051
+ if (revoked) return;
3052
+ this.handleError(agentId, event);
3053
+ }
3054
+ });
3055
+ this.agentSubscriptions.set(agentId, () => {
3056
+ revoked = true;
3057
+ unsubscribe();
3058
+ });
3059
+ }
3060
+ /**
3061
+ * Unsubscribe an agent's subscription (symmetric with StateManager cleanup).
3062
+ */
3063
+ unsubscribeFromAgent(agentId) {
3064
+ const unsubscribe = this.agentSubscriptions.get(agentId);
3065
+ if (unsubscribe) {
3066
+ unsubscribe();
3067
+ this.agentSubscriptions.delete(agentId);
3068
+ }
3069
+ }
3070
+ /**
3071
+ * Get the subagents observed for an owning agent, keyed by subagentRunId.
3072
+ * Returns a fresh shallow copy each call so consumers (and React state) see
3073
+ * a new reference when anything changed.
3074
+ */
3075
+ getSubagents(agentId) {
3076
+ return { ...this._subagents[agentId] };
3077
+ }
3078
+ handleStarted(agentId, event) {
3079
+ const bucket = this._subagents[agentId] ??= {};
3080
+ bucket[event.subagentRunId] = {
3081
+ subagentRunId: event.subagentRunId,
3082
+ name: event.name,
3083
+ description: event.description,
3084
+ parentSubagentRunId: event.parentSubagentRunId,
3085
+ status: "running"
3086
+ };
3087
+ this.notifySubagentsChanged(agentId);
3088
+ }
3089
+ handleFinished(agentId, event) {
3090
+ const bucket = this._subagents[agentId];
3091
+ const existing = bucket?.[event.subagentRunId];
3092
+ if (!bucket || !existing) return;
3093
+ bucket[event.subagentRunId] = {
3094
+ ...existing,
3095
+ status: "finished"
3096
+ };
3097
+ this.notifySubagentsChanged(agentId);
3098
+ }
3099
+ handleError(agentId, event) {
3100
+ const bucket = this._subagents[agentId] ??= {};
3101
+ const existing = bucket[event.subagentRunId];
3102
+ bucket[event.subagentRunId] = existing ? {
3103
+ ...existing,
3104
+ status: "error",
3105
+ error: event.message
3106
+ } : {
3107
+ subagentRunId: event.subagentRunId,
3108
+ name: event.subagentRunId,
3109
+ status: "error",
3110
+ error: event.message
3111
+ };
3112
+ this.notifySubagentsChanged(agentId);
3113
+ }
3114
+ async notifySubagentsChanged(agentId) {
3115
+ const subagents = this.getSubagents(agentId);
3116
+ await this.core.notifySubscribers((subscriber) => subscriber.onSubagentsChanged?.({
3117
+ copilotkit: this.core,
3118
+ agentId,
3119
+ subagents
3120
+ }), "Subscriber onSubagentsChanged error:");
3121
+ }
3122
+ };
3123
+
3006
3124
  //#endregion
3007
3125
  //#region src/core/thread-store-registry.ts
3008
3126
  var ThreadStoreRegistry = class {
@@ -4191,6 +4309,7 @@ var CopilotKitCore = class {
4191
4309
  suggestionEngine;
4192
4310
  runHandler;
4193
4311
  stateManager;
4312
+ subagentRegistry;
4194
4313
  threadStoreRegistry;
4195
4314
  /**
4196
4315
  * The single core-owned memory store, created lazily on first
@@ -4216,6 +4335,7 @@ var CopilotKitCore = class {
4216
4335
  this.suggestionEngine = new SuggestionEngine(this);
4217
4336
  this.runHandler = new RunHandler(this);
4218
4337
  this.stateManager = new StateManager(this);
4338
+ this.subagentRegistry = new SubagentRegistry(this);
4219
4339
  this.threadStoreRegistry = new ThreadStoreRegistry(this);
4220
4340
  this.agentRegistry.initialize(agents__unsafe_dev_only);
4221
4341
  this.runHandler.initialize(tools);
@@ -4230,7 +4350,10 @@ var CopilotKitCore = class {
4230
4350
  },
4231
4351
  onAgentsChanged: ({ agents }) => {
4232
4352
  Object.values(agents).forEach((agent) => {
4233
- if (agent.agentId) this.stateManager.subscribeToAgent(agent);
4353
+ if (agent.agentId) {
4354
+ this.stateManager.subscribeToAgent(agent);
4355
+ this.subagentRegistry.subscribeToAgent(agent);
4356
+ }
4234
4357
  });
4235
4358
  const currentAgentIds = new Set(Object.keys(agents));
4236
4359
  for (const agentId of Object.keys(this.threadStoreRegistry.getAll())) if (this.previousAgentIds.has(agentId) && !currentAgentIds.has(agentId)) try {
@@ -4238,10 +4361,17 @@ var CopilotKitCore = class {
4238
4361
  } catch (err) {
4239
4362
  console.error(`CopilotKitCore.onAgentsChanged: threadStoreRegistry.unregister failed for "${agentId}":`, err);
4240
4363
  }
4241
- for (const agentId of this.previousAgentIds) if (!currentAgentIds.has(agentId)) try {
4242
- this.stateManager.unsubscribeFromAgent(agentId);
4243
- } catch (err) {
4244
- console.error(`CopilotKitCore.onAgentsChanged: stateManager.unsubscribeFromAgent failed for "${agentId}":`, err);
4364
+ for (const agentId of this.previousAgentIds) if (!currentAgentIds.has(agentId)) {
4365
+ try {
4366
+ this.stateManager.unsubscribeFromAgent(agentId);
4367
+ } catch (err) {
4368
+ console.error(`CopilotKitCore.onAgentsChanged: stateManager.unsubscribeFromAgent failed for "${agentId}":`, err);
4369
+ }
4370
+ try {
4371
+ this.subagentRegistry.unsubscribeFromAgent(agentId);
4372
+ } catch (err) {
4373
+ console.error(`CopilotKitCore.onAgentsChanged: subagentRegistry.unsubscribeFromAgent failed for "${agentId}":`, err);
4374
+ }
4245
4375
  }
4246
4376
  this.previousAgentIds = currentAgentIds;
4247
4377
  }
@@ -4611,6 +4741,16 @@ var CopilotKitCore = class {
4611
4741
  return this.suggestionEngine.getSuggestions(agentId);
4612
4742
  }
4613
4743
  /**
4744
+ * Subagent registry (delegated to SubagentRegistry).
4745
+ *
4746
+ * Returns the subagents observed for an owning agent during its run(s),
4747
+ * keyed by `subagentRunId`, each carrying `name` / `description` / `status`.
4748
+ * Empty for runs that never emit SUBAGENT_* events.
4749
+ */
4750
+ getSubagents(agentId) {
4751
+ return this.subagentRegistry.getSubagents(agentId);
4752
+ }
4753
+ /**
4614
4754
  * Tool management (delegated to RunHandler)
4615
4755
  */
4616
4756
  addTool(tool) {
@@ -5905,6 +6045,7 @@ exports.MemoryError = MemoryError;
5905
6045
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;
5906
6046
  exports.RunHandler = RunHandler;
5907
6047
  exports.StateManager = StateManager;
6048
+ exports.SubagentRegistry = SubagentRegistry;
5908
6049
  exports.SuggestionEngine = SuggestionEngine;
5909
6050
  exports.ToolCallStatus = ToolCallStatus;
5910
6051
  exports.completePartialMarkdown = completePartialMarkdown;