@copilotkitnext/core 0.0.13-alpha.1 → 0.0.14

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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _ag_ui_client from '@ag-ui/client';
2
- import { ToolCall, AbstractAgent, Message, RunAgentResult, Tool, Context, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
2
+ import { ToolCall, AbstractAgent, Message, RunAgentResult, Tool, Context, State, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
3
3
  import { z } from 'zod';
4
4
  import { Observable } from 'rxjs';
5
5
 
@@ -322,6 +322,27 @@ declare enum CopilotKitCoreRuntimeConnectionStatus {
322
322
  Connecting = "connecting",
323
323
  Error = "error"
324
324
  }
325
+ /**
326
+ * Internal interface for delegate classes to access CopilotKitCore methods.
327
+ * This provides type safety while allowing controlled access to private functionality.
328
+ */
329
+ interface CopilotKitCoreFriendsAccess {
330
+ notifySubscribers(handler: (subscriber: CopilotKitCoreSubscriber) => void | Promise<void>, errorMessage: string): Promise<void>;
331
+ emitError(params: {
332
+ error: Error;
333
+ code: CopilotKitCoreErrorCode;
334
+ context?: Record<string, any>;
335
+ }): Promise<void>;
336
+ readonly headers: Readonly<Record<string, string>>;
337
+ readonly properties: Readonly<Record<string, unknown>>;
338
+ readonly context: Readonly<Record<string, Context>>;
339
+ buildFrontendTools(agentId?: string): _ag_ui_client.Tool[];
340
+ getAgent(id: string): AbstractAgent | undefined;
341
+ readonly suggestionEngine: {
342
+ clearSuggestions(agentId: string): void;
343
+ reloadSuggestions(agentId: string): void;
344
+ };
345
+ }
325
346
  declare class CopilotKitCore {
326
347
  private _headers;
327
348
  private _properties;
@@ -330,11 +351,12 @@ declare class CopilotKitCore {
330
351
  private contextStore;
331
352
  private suggestionEngine;
332
353
  private runHandler;
354
+ private stateManager;
333
355
  constructor({ runtimeUrl, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
334
356
  /**
335
- * Internal method used by delegate classes to notify subscribers
357
+ * Internal method used by delegate classes and subclasses to notify subscribers
336
358
  */
337
- private notifySubscribers;
359
+ protected notifySubscribers(handler: (subscriber: CopilotKitCoreSubscriber) => void | Promise<void>, errorMessage: string): Promise<void>;
338
360
  /**
339
361
  * Internal method used by delegate classes to emit errors
340
362
  */
@@ -393,6 +415,12 @@ declare class CopilotKitCore {
393
415
  */
394
416
  connectAgent(params: CopilotKitCoreConnectAgentParams): Promise<_ag_ui_client.RunAgentResult>;
395
417
  runAgent(params: CopilotKitCoreRunAgentParams): Promise<_ag_ui_client.RunAgentResult>;
418
+ /**
419
+ * State management (delegated to StateManager)
420
+ */
421
+ getStateByRun(agentId: string, threadId: string, runId: string): State | undefined;
422
+ getRunIdForMessage(agentId: string, threadId: string, messageId: string): string | undefined;
423
+ getRunIdsForThread(agentId: string, threadId: string): string[];
396
424
  /**
397
425
  * Internal method used by RunHandler to build frontend tools
398
426
  */
@@ -500,6 +528,87 @@ declare class SuggestionEngine {
500
528
  private addStaticSuggestions;
501
529
  }
502
530
 
531
+ /**
532
+ * Manages state and message tracking by run for CopilotKitCore.
533
+ * Tracks agent state snapshots and message-to-run associations.
534
+ */
535
+ declare class StateManager {
536
+ private core;
537
+ private stateByRun;
538
+ private messageToRun;
539
+ private agentSubscriptions;
540
+ constructor(core: CopilotKitCore);
541
+ /**
542
+ * Initialize state tracking for an agent
543
+ */
544
+ initialize(): void;
545
+ /**
546
+ * Subscribe to an agent's events to track state and messages
547
+ */
548
+ subscribeToAgent(agent: AbstractAgent): void;
549
+ /**
550
+ * Unsubscribe from an agent's events
551
+ */
552
+ unsubscribeFromAgent(agentId: string): void;
553
+ /**
554
+ * Get state for a specific run
555
+ * Returns a deep copy to prevent external mutations
556
+ */
557
+ getStateByRun(agentId: string, threadId: string, runId: string): State | undefined;
558
+ /**
559
+ * Get runId associated with a message
560
+ */
561
+ getRunIdForMessage(agentId: string, threadId: string, messageId: string): string | undefined;
562
+ /**
563
+ * Get all states for an agent's thread
564
+ */
565
+ getStatesForThread(agentId: string, threadId: string): Map<string, State>;
566
+ /**
567
+ * Get all run IDs for an agent's thread
568
+ */
569
+ getRunIdsForThread(agentId: string, threadId: string): string[];
570
+ /**
571
+ * Handle run started event
572
+ */
573
+ private handleRunStarted;
574
+ /**
575
+ * Handle run finished event
576
+ */
577
+ private handleRunFinished;
578
+ /**
579
+ * Handle state snapshot event
580
+ */
581
+ private handleStateSnapshot;
582
+ /**
583
+ * Handle state delta event
584
+ */
585
+ private handleStateDelta;
586
+ /**
587
+ * Handle messages snapshot event
588
+ */
589
+ private handleMessagesSnapshot;
590
+ /**
591
+ * Handle new message event
592
+ */
593
+ private handleNewMessage;
594
+ /**
595
+ * Save state for a specific run
596
+ */
597
+ private saveState;
598
+ /**
599
+ * Associate a message with a run
600
+ */
601
+ private associateMessageWithRun;
602
+ /**
603
+ * Clear all state for an agent
604
+ */
605
+ clearAgentState(agentId: string): void;
606
+ /**
607
+ * Clear all state for a thread
608
+ */
609
+ clearThreadState(agentId: string, threadId: string): void;
610
+ }
611
+
503
612
  interface ProxiedCopilotRuntimeAgentConfig extends Omit<HttpAgentConfig, "url"> {
504
613
  runtimeUrl?: string;
505
614
  }
@@ -511,4 +620,4 @@ declare class ProxiedCopilotRuntimeAgent extends HttpAgent {
511
620
 
512
621
  declare function completePartialMarkdown(input: string): string;
513
622
 
514
- export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
623
+ export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _ag_ui_client from '@ag-ui/client';
2
- import { ToolCall, AbstractAgent, Message, RunAgentResult, Tool, Context, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
2
+ import { ToolCall, AbstractAgent, Message, RunAgentResult, Tool, Context, State, HttpAgentConfig, HttpAgent, RunAgentInput, BaseEvent } from '@ag-ui/client';
3
3
  import { z } from 'zod';
4
4
  import { Observable } from 'rxjs';
5
5
 
@@ -322,6 +322,27 @@ declare enum CopilotKitCoreRuntimeConnectionStatus {
322
322
  Connecting = "connecting",
323
323
  Error = "error"
324
324
  }
325
+ /**
326
+ * Internal interface for delegate classes to access CopilotKitCore methods.
327
+ * This provides type safety while allowing controlled access to private functionality.
328
+ */
329
+ interface CopilotKitCoreFriendsAccess {
330
+ notifySubscribers(handler: (subscriber: CopilotKitCoreSubscriber) => void | Promise<void>, errorMessage: string): Promise<void>;
331
+ emitError(params: {
332
+ error: Error;
333
+ code: CopilotKitCoreErrorCode;
334
+ context?: Record<string, any>;
335
+ }): Promise<void>;
336
+ readonly headers: Readonly<Record<string, string>>;
337
+ readonly properties: Readonly<Record<string, unknown>>;
338
+ readonly context: Readonly<Record<string, Context>>;
339
+ buildFrontendTools(agentId?: string): _ag_ui_client.Tool[];
340
+ getAgent(id: string): AbstractAgent | undefined;
341
+ readonly suggestionEngine: {
342
+ clearSuggestions(agentId: string): void;
343
+ reloadSuggestions(agentId: string): void;
344
+ };
345
+ }
325
346
  declare class CopilotKitCore {
326
347
  private _headers;
327
348
  private _properties;
@@ -330,11 +351,12 @@ declare class CopilotKitCore {
330
351
  private contextStore;
331
352
  private suggestionEngine;
332
353
  private runHandler;
354
+ private stateManager;
333
355
  constructor({ runtimeUrl, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
334
356
  /**
335
- * Internal method used by delegate classes to notify subscribers
357
+ * Internal method used by delegate classes and subclasses to notify subscribers
336
358
  */
337
- private notifySubscribers;
359
+ protected notifySubscribers(handler: (subscriber: CopilotKitCoreSubscriber) => void | Promise<void>, errorMessage: string): Promise<void>;
338
360
  /**
339
361
  * Internal method used by delegate classes to emit errors
340
362
  */
@@ -393,6 +415,12 @@ declare class CopilotKitCore {
393
415
  */
394
416
  connectAgent(params: CopilotKitCoreConnectAgentParams): Promise<_ag_ui_client.RunAgentResult>;
395
417
  runAgent(params: CopilotKitCoreRunAgentParams): Promise<_ag_ui_client.RunAgentResult>;
418
+ /**
419
+ * State management (delegated to StateManager)
420
+ */
421
+ getStateByRun(agentId: string, threadId: string, runId: string): State | undefined;
422
+ getRunIdForMessage(agentId: string, threadId: string, messageId: string): string | undefined;
423
+ getRunIdsForThread(agentId: string, threadId: string): string[];
396
424
  /**
397
425
  * Internal method used by RunHandler to build frontend tools
398
426
  */
@@ -500,6 +528,87 @@ declare class SuggestionEngine {
500
528
  private addStaticSuggestions;
501
529
  }
502
530
 
531
+ /**
532
+ * Manages state and message tracking by run for CopilotKitCore.
533
+ * Tracks agent state snapshots and message-to-run associations.
534
+ */
535
+ declare class StateManager {
536
+ private core;
537
+ private stateByRun;
538
+ private messageToRun;
539
+ private agentSubscriptions;
540
+ constructor(core: CopilotKitCore);
541
+ /**
542
+ * Initialize state tracking for an agent
543
+ */
544
+ initialize(): void;
545
+ /**
546
+ * Subscribe to an agent's events to track state and messages
547
+ */
548
+ subscribeToAgent(agent: AbstractAgent): void;
549
+ /**
550
+ * Unsubscribe from an agent's events
551
+ */
552
+ unsubscribeFromAgent(agentId: string): void;
553
+ /**
554
+ * Get state for a specific run
555
+ * Returns a deep copy to prevent external mutations
556
+ */
557
+ getStateByRun(agentId: string, threadId: string, runId: string): State | undefined;
558
+ /**
559
+ * Get runId associated with a message
560
+ */
561
+ getRunIdForMessage(agentId: string, threadId: string, messageId: string): string | undefined;
562
+ /**
563
+ * Get all states for an agent's thread
564
+ */
565
+ getStatesForThread(agentId: string, threadId: string): Map<string, State>;
566
+ /**
567
+ * Get all run IDs for an agent's thread
568
+ */
569
+ getRunIdsForThread(agentId: string, threadId: string): string[];
570
+ /**
571
+ * Handle run started event
572
+ */
573
+ private handleRunStarted;
574
+ /**
575
+ * Handle run finished event
576
+ */
577
+ private handleRunFinished;
578
+ /**
579
+ * Handle state snapshot event
580
+ */
581
+ private handleStateSnapshot;
582
+ /**
583
+ * Handle state delta event
584
+ */
585
+ private handleStateDelta;
586
+ /**
587
+ * Handle messages snapshot event
588
+ */
589
+ private handleMessagesSnapshot;
590
+ /**
591
+ * Handle new message event
592
+ */
593
+ private handleNewMessage;
594
+ /**
595
+ * Save state for a specific run
596
+ */
597
+ private saveState;
598
+ /**
599
+ * Associate a message with a run
600
+ */
601
+ private associateMessageWithRun;
602
+ /**
603
+ * Clear all state for an agent
604
+ */
605
+ clearAgentState(agentId: string): void;
606
+ /**
607
+ * Clear all state for a thread
608
+ */
609
+ clearThreadState(agentId: string, threadId: string): void;
610
+ }
611
+
503
612
  interface ProxiedCopilotRuntimeAgentConfig extends Omit<HttpAgentConfig, "url"> {
504
613
  runtimeUrl?: string;
505
614
  }
@@ -511,4 +620,4 @@ declare class ProxiedCopilotRuntimeAgent extends HttpAgent {
511
620
 
512
621
  declare function completePartialMarkdown(input: string): string;
513
622
 
514
- export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
623
+ export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ __export(index_exports, {
27
27
  CopilotKitCoreRuntimeConnectionStatus: () => CopilotKitCoreRuntimeConnectionStatus,
28
28
  ProxiedCopilotRuntimeAgent: () => ProxiedCopilotRuntimeAgent,
29
29
  RunHandler: () => RunHandler,
30
+ StateManager: () => StateManager,
30
31
  SuggestionEngine: () => SuggestionEngine,
31
32
  ToolCallStatus: () => ToolCallStatus,
32
33
  completePartialMarkdown: () => completePartialMarkdown
@@ -763,7 +764,9 @@ var RunHandler = class {
763
764
  * Run an agent
764
765
  */
765
766
  async runAgent({ agent, withMessages }) {
766
- void this.core.suggestionEngine.clearSuggestions(agent.agentId);
767
+ if (agent.agentId) {
768
+ void this.core.suggestionEngine.clearSuggestions(agent.agentId);
769
+ }
767
770
  if (agent instanceof import_client3.HttpAgent) {
768
771
  agent.headers = { ...this.core.headers };
769
772
  }
@@ -1119,6 +1122,186 @@ function createToolSchema(tool) {
1119
1122
  return schema;
1120
1123
  }
1121
1124
 
1125
+ // src/core/state-manager.ts
1126
+ var StateManager = class {
1127
+ constructor(core) {
1128
+ this.core = core;
1129
+ }
1130
+ // State tracking: agentId -> threadId -> runId -> state
1131
+ stateByRun = /* @__PURE__ */ new Map();
1132
+ // Message tracking: agentId -> threadId -> messageId -> runId
1133
+ messageToRun = /* @__PURE__ */ new Map();
1134
+ // Agent subscriptions for cleanup
1135
+ agentSubscriptions = /* @__PURE__ */ new Map();
1136
+ /**
1137
+ * Initialize state tracking for an agent
1138
+ */
1139
+ initialize() {
1140
+ }
1141
+ /**
1142
+ * Subscribe to an agent's events to track state and messages
1143
+ */
1144
+ subscribeToAgent(agent) {
1145
+ if (!agent.agentId) {
1146
+ return;
1147
+ }
1148
+ const agentId = agent.agentId;
1149
+ this.unsubscribeFromAgent(agentId);
1150
+ const { unsubscribe } = agent.subscribe({
1151
+ onRunStartedEvent: ({ event, state }) => {
1152
+ this.handleRunStarted(agent, event, state);
1153
+ },
1154
+ onRunFinishedEvent: ({ event, state }) => {
1155
+ this.handleRunFinished(agent, event, state);
1156
+ },
1157
+ onStateSnapshotEvent: ({ event, input, state }) => {
1158
+ this.handleStateSnapshot(agent, event, input, state);
1159
+ },
1160
+ onStateDeltaEvent: ({ event, input, state }) => {
1161
+ this.handleStateDelta(agent, event, input, state);
1162
+ },
1163
+ onMessagesSnapshotEvent: ({ event, input, messages }) => {
1164
+ this.handleMessagesSnapshot(agent, event, input, messages);
1165
+ },
1166
+ onNewMessage: ({ message, input }) => {
1167
+ this.handleNewMessage(agent, message, input);
1168
+ }
1169
+ });
1170
+ this.agentSubscriptions.set(agentId, unsubscribe);
1171
+ }
1172
+ /**
1173
+ * Unsubscribe from an agent's events
1174
+ */
1175
+ unsubscribeFromAgent(agentId) {
1176
+ const unsubscribe = this.agentSubscriptions.get(agentId);
1177
+ if (unsubscribe) {
1178
+ unsubscribe();
1179
+ this.agentSubscriptions.delete(agentId);
1180
+ }
1181
+ }
1182
+ /**
1183
+ * Get state for a specific run
1184
+ * Returns a deep copy to prevent external mutations
1185
+ */
1186
+ getStateByRun(agentId, threadId, runId) {
1187
+ const state = this.stateByRun.get(agentId)?.get(threadId)?.get(runId);
1188
+ if (!state) return void 0;
1189
+ return JSON.parse(JSON.stringify(state));
1190
+ }
1191
+ /**
1192
+ * Get runId associated with a message
1193
+ */
1194
+ getRunIdForMessage(agentId, threadId, messageId) {
1195
+ return this.messageToRun.get(agentId)?.get(threadId)?.get(messageId);
1196
+ }
1197
+ /**
1198
+ * Get all states for an agent's thread
1199
+ */
1200
+ getStatesForThread(agentId, threadId) {
1201
+ return this.stateByRun.get(agentId)?.get(threadId) ?? /* @__PURE__ */ new Map();
1202
+ }
1203
+ /**
1204
+ * Get all run IDs for an agent's thread
1205
+ */
1206
+ getRunIdsForThread(agentId, threadId) {
1207
+ const threadStates = this.stateByRun.get(agentId)?.get(threadId);
1208
+ return threadStates ? Array.from(threadStates.keys()) : [];
1209
+ }
1210
+ /**
1211
+ * Handle run started event
1212
+ */
1213
+ handleRunStarted(agent, event, state) {
1214
+ if (!agent.agentId) return;
1215
+ const { threadId, runId } = event;
1216
+ this.saveState(agent.agentId, threadId, runId, state);
1217
+ }
1218
+ /**
1219
+ * Handle run finished event
1220
+ */
1221
+ handleRunFinished(agent, event, state) {
1222
+ if (!agent.agentId) return;
1223
+ const { threadId, runId } = event;
1224
+ this.saveState(agent.agentId, threadId, runId, state);
1225
+ }
1226
+ /**
1227
+ * Handle state snapshot event
1228
+ */
1229
+ handleStateSnapshot(agent, event, input, state) {
1230
+ if (!agent.agentId) return;
1231
+ const { threadId, runId } = input;
1232
+ const mergedState = { ...state, ...event.snapshot };
1233
+ this.saveState(agent.agentId, threadId, runId, mergedState);
1234
+ }
1235
+ /**
1236
+ * Handle state delta event
1237
+ */
1238
+ handleStateDelta(agent, event, input, state) {
1239
+ if (!agent.agentId) return;
1240
+ const { threadId, runId } = input;
1241
+ this.saveState(agent.agentId, threadId, runId, state);
1242
+ }
1243
+ /**
1244
+ * Handle messages snapshot event
1245
+ */
1246
+ handleMessagesSnapshot(agent, event, input, messages) {
1247
+ if (!agent.agentId) return;
1248
+ const { threadId, runId } = input;
1249
+ for (const message of event.messages) {
1250
+ this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
1251
+ }
1252
+ }
1253
+ /**
1254
+ * Handle new message event
1255
+ */
1256
+ handleNewMessage(agent, message, input) {
1257
+ if (!agent.agentId || !input) return;
1258
+ const { threadId, runId } = input;
1259
+ this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
1260
+ }
1261
+ /**
1262
+ * Save state for a specific run
1263
+ */
1264
+ saveState(agentId, threadId, runId, state) {
1265
+ if (!this.stateByRun.has(agentId)) {
1266
+ this.stateByRun.set(agentId, /* @__PURE__ */ new Map());
1267
+ }
1268
+ const agentStates = this.stateByRun.get(agentId);
1269
+ if (!agentStates.has(threadId)) {
1270
+ agentStates.set(threadId, /* @__PURE__ */ new Map());
1271
+ }
1272
+ const threadStates = agentStates.get(threadId);
1273
+ threadStates.set(runId, JSON.parse(JSON.stringify(state)));
1274
+ }
1275
+ /**
1276
+ * Associate a message with a run
1277
+ */
1278
+ associateMessageWithRun(agentId, threadId, messageId, runId) {
1279
+ if (!this.messageToRun.has(agentId)) {
1280
+ this.messageToRun.set(agentId, /* @__PURE__ */ new Map());
1281
+ }
1282
+ const agentMessages = this.messageToRun.get(agentId);
1283
+ if (!agentMessages.has(threadId)) {
1284
+ agentMessages.set(threadId, /* @__PURE__ */ new Map());
1285
+ }
1286
+ const threadMessages = agentMessages.get(threadId);
1287
+ threadMessages.set(messageId, runId);
1288
+ }
1289
+ /**
1290
+ * Clear all state for an agent
1291
+ */
1292
+ clearAgentState(agentId) {
1293
+ this.stateByRun.delete(agentId);
1294
+ this.messageToRun.delete(agentId);
1295
+ }
1296
+ /**
1297
+ * Clear all state for a thread
1298
+ */
1299
+ clearThreadState(agentId, threadId) {
1300
+ this.stateByRun.get(agentId)?.delete(threadId);
1301
+ this.messageToRun.get(agentId)?.delete(threadId);
1302
+ }
1303
+ };
1304
+
1122
1305
  // src/core/core.ts
1123
1306
  var CopilotKitCoreErrorCode = /* @__PURE__ */ ((CopilotKitCoreErrorCode2) => {
1124
1307
  CopilotKitCoreErrorCode2["RUNTIME_INFO_FETCH_FAILED"] = "runtime_info_fetch_failed";
@@ -1146,6 +1329,7 @@ var CopilotKitCore = class {
1146
1329
  contextStore;
1147
1330
  suggestionEngine;
1148
1331
  runHandler;
1332
+ stateManager;
1149
1333
  constructor({
1150
1334
  runtimeUrl,
1151
1335
  headers = {},
@@ -1160,13 +1344,24 @@ var CopilotKitCore = class {
1160
1344
  this.contextStore = new ContextStore(this);
1161
1345
  this.suggestionEngine = new SuggestionEngine(this);
1162
1346
  this.runHandler = new RunHandler(this);
1347
+ this.stateManager = new StateManager(this);
1163
1348
  this.agentRegistry.initialize(agents__unsafe_dev_only);
1164
1349
  this.runHandler.initialize(tools);
1165
1350
  this.suggestionEngine.initialize(suggestionsConfig);
1351
+ this.stateManager.initialize();
1166
1352
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1353
+ this.subscribe({
1354
+ onAgentsChanged: ({ agents }) => {
1355
+ Object.values(agents).forEach((agent) => {
1356
+ if (agent.agentId) {
1357
+ this.stateManager.subscribeToAgent(agent);
1358
+ }
1359
+ });
1360
+ }
1361
+ });
1167
1362
  }
1168
1363
  /**
1169
- * Internal method used by delegate classes to notify subscribers
1364
+ * Internal method used by delegate classes and subclasses to notify subscribers
1170
1365
  */
1171
1366
  async notifySubscribers(handler, errorMessage) {
1172
1367
  await Promise.all(
@@ -1329,6 +1524,18 @@ var CopilotKitCore = class {
1329
1524
  async runAgent(params) {
1330
1525
  return this.runHandler.runAgent(params);
1331
1526
  }
1527
+ /**
1528
+ * State management (delegated to StateManager)
1529
+ */
1530
+ getStateByRun(agentId, threadId, runId) {
1531
+ return this.stateManager.getStateByRun(agentId, threadId, runId);
1532
+ }
1533
+ getRunIdForMessage(agentId, threadId, messageId) {
1534
+ return this.stateManager.getRunIdForMessage(agentId, threadId, messageId);
1535
+ }
1536
+ getRunIdsForThread(agentId, threadId) {
1537
+ return this.stateManager.getRunIdsForThread(agentId, threadId);
1538
+ }
1332
1539
  /**
1333
1540
  * Internal method used by RunHandler to build frontend tools
1334
1541
  */
@@ -1569,6 +1776,7 @@ ${indent}${fence}`;
1569
1776
  CopilotKitCoreRuntimeConnectionStatus,
1570
1777
  ProxiedCopilotRuntimeAgent,
1571
1778
  RunHandler,
1779
+ StateManager,
1572
1780
  SuggestionEngine,
1573
1781
  ToolCallStatus,
1574
1782
  completePartialMarkdown