@copilotkitnext/core 0.0.13-alpha.0 → 0.0.13

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.mjs CHANGED
@@ -732,7 +732,9 @@ var RunHandler = class {
732
732
  * Run an agent
733
733
  */
734
734
  async runAgent({ agent, withMessages }) {
735
- void this.core.suggestionEngine.clearSuggestions(agent.agentId);
735
+ if (agent.agentId) {
736
+ void this.core.suggestionEngine.clearSuggestions(agent.agentId);
737
+ }
736
738
  if (agent instanceof HttpAgent3) {
737
739
  agent.headers = { ...this.core.headers };
738
740
  }
@@ -1088,6 +1090,186 @@ function createToolSchema(tool) {
1088
1090
  return schema;
1089
1091
  }
1090
1092
 
1093
+ // src/core/state-manager.ts
1094
+ var StateManager = class {
1095
+ constructor(core) {
1096
+ this.core = core;
1097
+ }
1098
+ // State tracking: agentId -> threadId -> runId -> state
1099
+ stateByRun = /* @__PURE__ */ new Map();
1100
+ // Message tracking: agentId -> threadId -> messageId -> runId
1101
+ messageToRun = /* @__PURE__ */ new Map();
1102
+ // Agent subscriptions for cleanup
1103
+ agentSubscriptions = /* @__PURE__ */ new Map();
1104
+ /**
1105
+ * Initialize state tracking for an agent
1106
+ */
1107
+ initialize() {
1108
+ }
1109
+ /**
1110
+ * Subscribe to an agent's events to track state and messages
1111
+ */
1112
+ subscribeToAgent(agent) {
1113
+ if (!agent.agentId) {
1114
+ return;
1115
+ }
1116
+ const agentId = agent.agentId;
1117
+ this.unsubscribeFromAgent(agentId);
1118
+ const { unsubscribe } = agent.subscribe({
1119
+ onRunStartedEvent: ({ event, state }) => {
1120
+ this.handleRunStarted(agent, event, state);
1121
+ },
1122
+ onRunFinishedEvent: ({ event, state }) => {
1123
+ this.handleRunFinished(agent, event, state);
1124
+ },
1125
+ onStateSnapshotEvent: ({ event, input, state }) => {
1126
+ this.handleStateSnapshot(agent, event, input, state);
1127
+ },
1128
+ onStateDeltaEvent: ({ event, input, state }) => {
1129
+ this.handleStateDelta(agent, event, input, state);
1130
+ },
1131
+ onMessagesSnapshotEvent: ({ event, input, messages }) => {
1132
+ this.handleMessagesSnapshot(agent, event, input, messages);
1133
+ },
1134
+ onNewMessage: ({ message, input }) => {
1135
+ this.handleNewMessage(agent, message, input);
1136
+ }
1137
+ });
1138
+ this.agentSubscriptions.set(agentId, unsubscribe);
1139
+ }
1140
+ /**
1141
+ * Unsubscribe from an agent's events
1142
+ */
1143
+ unsubscribeFromAgent(agentId) {
1144
+ const unsubscribe = this.agentSubscriptions.get(agentId);
1145
+ if (unsubscribe) {
1146
+ unsubscribe();
1147
+ this.agentSubscriptions.delete(agentId);
1148
+ }
1149
+ }
1150
+ /**
1151
+ * Get state for a specific run
1152
+ * Returns a deep copy to prevent external mutations
1153
+ */
1154
+ getStateByRun(agentId, threadId, runId) {
1155
+ const state = this.stateByRun.get(agentId)?.get(threadId)?.get(runId);
1156
+ if (!state) return void 0;
1157
+ return JSON.parse(JSON.stringify(state));
1158
+ }
1159
+ /**
1160
+ * Get runId associated with a message
1161
+ */
1162
+ getRunIdForMessage(agentId, threadId, messageId) {
1163
+ return this.messageToRun.get(agentId)?.get(threadId)?.get(messageId);
1164
+ }
1165
+ /**
1166
+ * Get all states for an agent's thread
1167
+ */
1168
+ getStatesForThread(agentId, threadId) {
1169
+ return this.stateByRun.get(agentId)?.get(threadId) ?? /* @__PURE__ */ new Map();
1170
+ }
1171
+ /**
1172
+ * Get all run IDs for an agent's thread
1173
+ */
1174
+ getRunIdsForThread(agentId, threadId) {
1175
+ const threadStates = this.stateByRun.get(agentId)?.get(threadId);
1176
+ return threadStates ? Array.from(threadStates.keys()) : [];
1177
+ }
1178
+ /**
1179
+ * Handle run started event
1180
+ */
1181
+ handleRunStarted(agent, event, state) {
1182
+ if (!agent.agentId) return;
1183
+ const { threadId, runId } = event;
1184
+ this.saveState(agent.agentId, threadId, runId, state);
1185
+ }
1186
+ /**
1187
+ * Handle run finished event
1188
+ */
1189
+ handleRunFinished(agent, event, state) {
1190
+ if (!agent.agentId) return;
1191
+ const { threadId, runId } = event;
1192
+ this.saveState(agent.agentId, threadId, runId, state);
1193
+ }
1194
+ /**
1195
+ * Handle state snapshot event
1196
+ */
1197
+ handleStateSnapshot(agent, event, input, state) {
1198
+ if (!agent.agentId) return;
1199
+ const { threadId, runId } = input;
1200
+ const mergedState = { ...state, ...event.snapshot };
1201
+ this.saveState(agent.agentId, threadId, runId, mergedState);
1202
+ }
1203
+ /**
1204
+ * Handle state delta event
1205
+ */
1206
+ handleStateDelta(agent, event, input, state) {
1207
+ if (!agent.agentId) return;
1208
+ const { threadId, runId } = input;
1209
+ this.saveState(agent.agentId, threadId, runId, state);
1210
+ }
1211
+ /**
1212
+ * Handle messages snapshot event
1213
+ */
1214
+ handleMessagesSnapshot(agent, event, input, messages) {
1215
+ if (!agent.agentId) return;
1216
+ const { threadId, runId } = input;
1217
+ for (const message of event.messages) {
1218
+ this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
1219
+ }
1220
+ }
1221
+ /**
1222
+ * Handle new message event
1223
+ */
1224
+ handleNewMessage(agent, message, input) {
1225
+ if (!agent.agentId || !input) return;
1226
+ const { threadId, runId } = input;
1227
+ this.associateMessageWithRun(agent.agentId, threadId, message.id, runId);
1228
+ }
1229
+ /**
1230
+ * Save state for a specific run
1231
+ */
1232
+ saveState(agentId, threadId, runId, state) {
1233
+ if (!this.stateByRun.has(agentId)) {
1234
+ this.stateByRun.set(agentId, /* @__PURE__ */ new Map());
1235
+ }
1236
+ const agentStates = this.stateByRun.get(agentId);
1237
+ if (!agentStates.has(threadId)) {
1238
+ agentStates.set(threadId, /* @__PURE__ */ new Map());
1239
+ }
1240
+ const threadStates = agentStates.get(threadId);
1241
+ threadStates.set(runId, JSON.parse(JSON.stringify(state)));
1242
+ }
1243
+ /**
1244
+ * Associate a message with a run
1245
+ */
1246
+ associateMessageWithRun(agentId, threadId, messageId, runId) {
1247
+ if (!this.messageToRun.has(agentId)) {
1248
+ this.messageToRun.set(agentId, /* @__PURE__ */ new Map());
1249
+ }
1250
+ const agentMessages = this.messageToRun.get(agentId);
1251
+ if (!agentMessages.has(threadId)) {
1252
+ agentMessages.set(threadId, /* @__PURE__ */ new Map());
1253
+ }
1254
+ const threadMessages = agentMessages.get(threadId);
1255
+ threadMessages.set(messageId, runId);
1256
+ }
1257
+ /**
1258
+ * Clear all state for an agent
1259
+ */
1260
+ clearAgentState(agentId) {
1261
+ this.stateByRun.delete(agentId);
1262
+ this.messageToRun.delete(agentId);
1263
+ }
1264
+ /**
1265
+ * Clear all state for a thread
1266
+ */
1267
+ clearThreadState(agentId, threadId) {
1268
+ this.stateByRun.get(agentId)?.delete(threadId);
1269
+ this.messageToRun.get(agentId)?.delete(threadId);
1270
+ }
1271
+ };
1272
+
1091
1273
  // src/core/core.ts
1092
1274
  var CopilotKitCoreErrorCode = /* @__PURE__ */ ((CopilotKitCoreErrorCode2) => {
1093
1275
  CopilotKitCoreErrorCode2["RUNTIME_INFO_FETCH_FAILED"] = "runtime_info_fetch_failed";
@@ -1115,6 +1297,7 @@ var CopilotKitCore = class {
1115
1297
  contextStore;
1116
1298
  suggestionEngine;
1117
1299
  runHandler;
1300
+ stateManager;
1118
1301
  constructor({
1119
1302
  runtimeUrl,
1120
1303
  headers = {},
@@ -1129,13 +1312,24 @@ var CopilotKitCore = class {
1129
1312
  this.contextStore = new ContextStore(this);
1130
1313
  this.suggestionEngine = new SuggestionEngine(this);
1131
1314
  this.runHandler = new RunHandler(this);
1315
+ this.stateManager = new StateManager(this);
1132
1316
  this.agentRegistry.initialize(agents__unsafe_dev_only);
1133
1317
  this.runHandler.initialize(tools);
1134
1318
  this.suggestionEngine.initialize(suggestionsConfig);
1319
+ this.stateManager.initialize();
1135
1320
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1321
+ this.subscribe({
1322
+ onAgentsChanged: ({ agents }) => {
1323
+ Object.values(agents).forEach((agent) => {
1324
+ if (agent.agentId) {
1325
+ this.stateManager.subscribeToAgent(agent);
1326
+ }
1327
+ });
1328
+ }
1329
+ });
1136
1330
  }
1137
1331
  /**
1138
- * Internal method used by delegate classes to notify subscribers
1332
+ * Internal method used by delegate classes and subclasses to notify subscribers
1139
1333
  */
1140
1334
  async notifySubscribers(handler, errorMessage) {
1141
1335
  await Promise.all(
@@ -1298,6 +1492,18 @@ var CopilotKitCore = class {
1298
1492
  async runAgent(params) {
1299
1493
  return this.runHandler.runAgent(params);
1300
1494
  }
1495
+ /**
1496
+ * State management (delegated to StateManager)
1497
+ */
1498
+ getStateByRun(agentId, threadId, runId) {
1499
+ return this.stateManager.getStateByRun(agentId, threadId, runId);
1500
+ }
1501
+ getRunIdForMessage(agentId, threadId, messageId) {
1502
+ return this.stateManager.getRunIdForMessage(agentId, threadId, messageId);
1503
+ }
1504
+ getRunIdsForThread(agentId, threadId) {
1505
+ return this.stateManager.getRunIdsForThread(agentId, threadId);
1506
+ }
1301
1507
  /**
1302
1508
  * Internal method used by RunHandler to build frontend tools
1303
1509
  */
@@ -1537,6 +1743,7 @@ export {
1537
1743
  CopilotKitCoreRuntimeConnectionStatus,
1538
1744
  ProxiedCopilotRuntimeAgent,
1539
1745
  RunHandler,
1746
+ StateManager,
1540
1747
  SuggestionEngine,
1541
1748
  ToolCallStatus,
1542
1749
  completePartialMarkdown