@copilotkit/core 1.56.5 → 1.57.0-canary.1778078321

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
@@ -602,10 +602,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
602
602
  var _ProxiedCopilotRuntimeAgent_brand = /* @__PURE__ */ new WeakSet();
603
603
  var ProxiedCopilotRuntimeAgent = class ProxiedCopilotRuntimeAgent extends _ag_ui_client.HttpAgent {
604
604
  constructor(config) {
605
- var _config$transport, _ref, _config$agentId, _config$runtimeMode;
605
+ var _config$transport, _ref, _config$runtimeAgentI, _ref2, _config$runtimeMode;
606
606
  const normalizedRuntimeUrl = config.runtimeUrl ? config.runtimeUrl.replace(/\/$/, "") : void 0;
607
607
  const transport = (_config$transport = config.transport) !== null && _config$transport !== void 0 ? _config$transport : "auto";
608
- const runUrl = transport === "single" ? (_ref = normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl) !== null && _ref !== void 0 ? _ref : "" : `${normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl}/agent/${encodeURIComponent((_config$agentId = config.agentId) !== null && _config$agentId !== void 0 ? _config$agentId : "")}/run`;
608
+ const routedId = (_ref = (_config$runtimeAgentI = config.runtimeAgentId) !== null && _config$runtimeAgentI !== void 0 ? _config$runtimeAgentI : config.agentId) !== null && _ref !== void 0 ? _ref : "";
609
+ const runUrl = transport === "single" ? (_ref2 = normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl) !== null && _ref2 !== void 0 ? _ref2 : "" : `${normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl}/agent/${encodeURIComponent(routedId)}/run`;
609
610
  if (!runUrl) throw new Error("ProxiedCopilotRuntimeAgent requires a runtimeUrl when transport is set to 'single'.");
610
611
  super({
611
612
  ...config,
@@ -614,6 +615,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
614
615
  _classPrivateMethodInitSpec(this, _ProxiedCopilotRuntimeAgent_brand);
615
616
  _defineProperty(this, "runtimeUrl", void 0);
616
617
  _defineProperty(this, "credentials", void 0);
618
+ _defineProperty(this, "runtimeAgentId", void 0);
617
619
  _defineProperty(this, "transport", void 0);
618
620
  _defineProperty(this, "singleEndpointUrl", void 0);
619
621
  _defineProperty(this, "runtimeMode", void 0);
@@ -623,6 +625,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
623
625
  _defineProperty(this, "runtimeInfoPromise", void 0);
624
626
  this.runtimeUrl = normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl;
625
627
  this.credentials = config.credentials;
628
+ this.runtimeAgentId = config.runtimeAgentId;
626
629
  this.transport = transport;
627
630
  this.runtimeMode = (_config$runtimeMode = config.runtimeMode) !== null && _config$runtimeMode !== void 0 ? _config$runtimeMode : _copilotkit_shared.RUNTIME_MODE_SSE;
628
631
  this.intelligence = config.intelligence;
@@ -630,6 +633,22 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
630
633
  if (config.debug) this.debug = config.debug;
631
634
  if (this.transport === "single") this.singleEndpointUrl = this.runtimeUrl;
632
635
  }
636
+ /**
637
+ * The agent id used for outbound runtime requests — `runtimeAgentId` when
638
+ * set (manually-registered proxy), otherwise `agentId` (registry id
639
+ * matches runtime id). Subscriber bookkeeping keeps using `agentId`
640
+ * directly.
641
+ *
642
+ * Throws when both are unset: a proxy reaching an HTTP path with no
643
+ * routable id is a bug, and a missing id would otherwise produce a
644
+ * malformed `/agent//run` or `/agent/undefined/connect` URL silently.
645
+ */
646
+ routedAgentId() {
647
+ var _this$runtimeAgentId;
648
+ const id = (_this$runtimeAgentId = this.runtimeAgentId) !== null && _this$runtimeAgentId !== void 0 ? _this$runtimeAgentId : this.agentId;
649
+ if (!id) throw new Error("ProxiedCopilotRuntimeAgent: cannot make a runtime request without an agentId or runtimeAgentId.");
650
+ return id;
651
+ }
633
652
  get capabilities() {
634
653
  return this._capabilities;
635
654
  }
@@ -650,6 +669,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
650
669
  }
651
670
  if (!this.agentId || !this.threadId) return;
652
671
  if (typeof fetch === "undefined") return;
672
+ const routedId = this.routedAgentId();
653
673
  if (this.transport === "single") {
654
674
  if (!this.singleEndpointUrl) return;
655
675
  const headers = new Headers({
@@ -662,7 +682,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
662
682
  body: JSON.stringify({
663
683
  method: "agent/stop",
664
684
  params: {
665
- agentId: this.agentId,
685
+ agentId: routedId,
666
686
  threadId: this.threadId
667
687
  }
668
688
  }),
@@ -673,7 +693,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
673
693
  return;
674
694
  }
675
695
  if (!this.runtimeUrl) return;
676
- const stopPath = `${this.runtimeUrl}/agent/${encodeURIComponent(this.agentId)}/stop/${encodeURIComponent(this.threadId)}`;
696
+ const stopPath = `${this.runtimeUrl}/agent/${encodeURIComponent(routedId)}/stop/${encodeURIComponent(this.threadId)}`;
677
697
  const origin = typeof window !== "undefined" && window.location ? window.location.origin : "http://localhost";
678
698
  const base = new URL(this.runtimeUrl, origin);
679
699
  const stopUrl = new URL(stopPath, base);
@@ -737,6 +757,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
737
757
  const cloned = new ProxiedCopilotRuntimeAgent({
738
758
  runtimeUrl: this.runtimeUrl,
739
759
  agentId: this.agentId,
760
+ runtimeAgentId: this.runtimeAgentId,
740
761
  description: this.description,
741
762
  headers: { ...this.headers },
742
763
  credentials: this.credentials,
@@ -849,17 +870,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
849
870
  }
850
871
  createIntelligenceDelegate() {
851
872
  var _this$intelligence;
852
- if (!this.runtimeUrl || !this.agentId || !((_this$intelligence = this.intelligence) === null || _this$intelligence === void 0 ? void 0 : _this$intelligence.wsUrl)) throw new Error("Intelligence mode requires runtimeUrl, agentId, and intelligence websocket metadata");
873
+ const routedId = this.routedAgentId();
874
+ if (!this.runtimeUrl || !routedId || !((_this$intelligence = this.intelligence) === null || _this$intelligence === void 0 ? void 0 : _this$intelligence.wsUrl)) throw new Error("Intelligence mode requires runtimeUrl, agentId, and intelligence websocket metadata");
853
875
  return new IntelligenceAgent({
854
876
  url: this.intelligence.wsUrl,
855
877
  runtimeUrl: this.runtimeUrl,
856
- agentId: this.agentId,
878
+ agentId: routedId,
857
879
  headers: { ...this.headers },
858
880
  credentials: this.credentials
859
881
  });
860
882
  }
861
883
  syncDelegate(delegate) {
862
- delegate.agentId = this.agentId;
884
+ delegate.agentId = this.routedAgentId();
863
885
  delegate.description = this.description;
864
886
  delegate.threadId = this.threadId;
865
887
  delegate.setMessages(this.messages);
@@ -873,12 +895,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
873
895
  return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.connect(input))));
874
896
  }
875
897
  function _connectViaHttp(input) {
898
+ const routedId = this.routedAgentId();
876
899
  if (this.transport === "single") {
877
900
  if (!this.singleEndpointUrl) throw new Error("Single endpoint transport requires a runtimeUrl");
878
- const requestInit = this.createSingleRouteRequestInit(input, "agent/connect", { agentId: this.agentId });
901
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/connect", { agentId: routedId });
879
902
  return withAbortErrorHandling((0, _ag_ui_client.transformHttpEventStream)((0, _ag_ui_client.runHttpRequest)(this.singleEndpointUrl, requestInit)));
880
903
  }
881
- return withAbortErrorHandling((0, _ag_ui_client.transformHttpEventStream)((0, _ag_ui_client.runHttpRequest)(`${this.runtimeUrl}/agent/${this.agentId}/connect`, this.requestInit(input))));
904
+ return withAbortErrorHandling((0, _ag_ui_client.transformHttpEventStream)((0, _ag_ui_client.runHttpRequest)(`${this.runtimeUrl}/agent/${routedId}/connect`, this.requestInit(input))));
882
905
  }
883
906
  function _runViaDelegate(input) {
884
907
  return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.run(input))));
@@ -886,7 +909,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
886
909
  function _runViaHttp(input) {
887
910
  if (this.transport === "single") {
888
911
  if (!this.singleEndpointUrl) throw new Error("Single endpoint transport requires a runtimeUrl");
889
- const requestInit = this.createSingleRouteRequestInit(input, "agent/run", { agentId: this.agentId });
912
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/run", { agentId: this.routedAgentId() });
890
913
  return withAbortErrorHandling((0, _ag_ui_client.transformHttpEventStream)((0, _ag_ui_client.runHttpRequest)(this.singleEndpointUrl, requestInit)));
891
914
  }
892
915
  return withAbortErrorHandling(_superPropGet(_ProxiedCopilotRuntimeAgent.prototype, "run", this, 2)([input]));
@@ -1016,6 +1039,52 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1016
1039
  this.notifyAgentsChanged();
1017
1040
  }
1018
1041
  /**
1042
+ * Register a proxied agent that routes outbound runtime requests to an
1043
+ * existing runtime agent (`runtimeAgentId`) while exposing a distinct local
1044
+ * registry id (`agentId`). Throws if `agentId` is already taken by either a
1045
+ * local or runtime-discovered agent.
1046
+ *
1047
+ * Use this to mount multiple frontend agents against a single runtime
1048
+ * agent (e.g. a chat-1 / chat-2 pair both proxying to "default") without
1049
+ * implicit per-thread cloning. The returned `unregister` removes the proxy
1050
+ * from the registry and emits `onAgentsChanged`.
1051
+ */
1052
+ registerProxiedAgent({ agentId, runtimeAgentId }) {
1053
+ if (Object.prototype.hasOwnProperty.call(this._agents, agentId)) throw new Error(`CopilotKitCore.registerProxiedAgent: agentId "${agentId}" is already registered. Pick a different agentId, or unregister the existing agent first.`);
1054
+ const friends = this.core;
1055
+ const debug = friends.debug;
1056
+ const agent = new ProxiedCopilotRuntimeAgent({
1057
+ runtimeUrl: this._runtimeUrl,
1058
+ agentId,
1059
+ runtimeAgentId,
1060
+ transport: this._runtimeTransport,
1061
+ credentials: friends.credentials,
1062
+ runtimeMode: this._runtimeUrl ? this._runtimeConnectionStatus === CopilotKitCoreRuntimeConnectionStatus.Connected ? this._runtimeMode : "pending" : _copilotkit_shared.RUNTIME_MODE_SSE,
1063
+ intelligence: this._intelligence,
1064
+ debug: debug ? (0, _copilotkit_shared.resolveDebugConfig)(debug) : void 0
1065
+ });
1066
+ this.applyHeadersToAgent(agent);
1067
+ this.localAgents[agentId] = agent;
1068
+ this._agents = {
1069
+ ...this.localAgents,
1070
+ ...this.remoteAgents
1071
+ };
1072
+ this.notifyAgentsChanged();
1073
+ return {
1074
+ agent,
1075
+ unregister: () => {
1076
+ if (this.localAgents[agentId] === agent) {
1077
+ delete this.localAgents[agentId];
1078
+ this._agents = {
1079
+ ...this.localAgents,
1080
+ ...this.remoteAgents
1081
+ };
1082
+ this.notifyAgentsChanged();
1083
+ }
1084
+ }
1085
+ };
1086
+ }
1087
+ /**
1019
1088
  * Get an agent by ID
1020
1089
  */
1021
1090
  getAgent(id) {
@@ -1306,17 +1375,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1306
1375
  /**
1307
1376
  * Reload suggestions for a specific agent
1308
1377
  * This triggers generation of new suggestions based on current configs
1309
- *
1310
- * @param agentId - The consumer agent ID
1311
- * @param consumerAgent - Optional: the specific agent instance whose messages should be used
1312
- * for availability filtering and context. When running with per-thread clones, the thread
1313
- * clone holds the conversation messages; passing it here ensures dynamic suggestions fire
1314
- * after the first message even though the registry agent has an empty message list.
1315
1378
  */
1316
- reloadSuggestions(agentId, consumerAgent) {
1379
+ reloadSuggestions(agentId) {
1317
1380
  var _agent$messages$lengt, _agent$messages;
1318
1381
  this.clearSuggestions(agentId);
1319
- const agent = consumerAgent !== null && consumerAgent !== void 0 ? consumerAgent : this.core.getAgent(agentId);
1382
+ const agent = this.core.getAgent(agentId);
1320
1383
  const messageCount = (_agent$messages$lengt = agent === null || agent === void 0 || (_agent$messages = agent.messages) === null || _agent$messages === void 0 ? void 0 : _agent$messages.length) !== null && _agent$messages$lengt !== void 0 ? _agent$messages$lengt : 0;
1321
1384
  let hasAnySuggestions = false;
1322
1385
  for (const config of Object.values(this._suggestionsConfig)) {
@@ -1329,7 +1392,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1329
1392
  hasAnySuggestions = true;
1330
1393
  this.notifySuggestionsStartedLoading(agentId);
1331
1394
  }
1332
- this.generateSuggestions(suggestionId, config, agentId, agent);
1395
+ this.generateSuggestions(suggestionId, config, agentId);
1333
1396
  } else if (isStaticSuggestionsConfig(config)) this.addStaticSuggestions(suggestionId, config, agentId);
1334
1397
  }
1335
1398
  }
@@ -1358,13 +1421,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1358
1421
  /**
1359
1422
  * Generate suggestions using a provider agent
1360
1423
  */
1361
- async generateSuggestions(suggestionId, config, consumerAgentId, consumerAgent) {
1424
+ async generateSuggestions(suggestionId, config, consumerAgentId) {
1362
1425
  let agent = void 0;
1363
1426
  try {
1364
1427
  var _config$providerAgent, _this$_runningSuggest3, _config$minSuggestion, _config$maxSuggestion;
1365
1428
  const suggestionsProviderAgent = this.core.getAgent((_config$providerAgent = config.providerAgentId) !== null && _config$providerAgent !== void 0 ? _config$providerAgent : "default");
1366
1429
  if (!suggestionsProviderAgent) throw new Error(`Suggestions provider agent not found: ${config.providerAgentId}`);
1367
- const suggestionsConsumerAgent = consumerAgent !== null && consumerAgent !== void 0 ? consumerAgent : this.core.getAgent(consumerAgentId);
1430
+ const suggestionsConsumerAgent = this.core.getAgent(consumerAgentId);
1368
1431
  if (!suggestionsConsumerAgent) throw new Error(`Suggestions consumer agent not found: ${consumerAgentId}`);
1369
1432
  agent = suggestionsProviderAgent.clone();
1370
1433
  agent.threadId = suggestionId;
@@ -1677,13 +1740,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1677
1740
  agent.setMessages([]);
1678
1741
  agent.setState({});
1679
1742
  if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
1680
- await this._internal.notifySubscribers((subscriber) => {
1681
- var _subscriber$onAgentRu;
1682
- return (_subscriber$onAgentRu = subscriber.onAgentRunStarted) === null || _subscriber$onAgentRu === void 0 ? void 0 : _subscriber$onAgentRu.call(subscriber, {
1683
- copilotkit: this.core,
1684
- agent
1685
- });
1686
- }, "Subscriber onAgentRunStarted error:");
1687
1743
  const runAgentResult = await agent.connectAgent({
1688
1744
  forwardedProps: this._internal.properties,
1689
1745
  tools: this.buildFrontendTools(agent.agentId),
@@ -1714,14 +1770,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1714
1770
  if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
1715
1771
  if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
1716
1772
  if (agent.detachActiveRun) await agent.detachActiveRun();
1717
- this._internal.subscribeAgentToStateManager(agent);
1718
- await this._internal.notifySubscribers((subscriber) => {
1719
- var _subscriber$onAgentRu2;
1720
- return (_subscriber$onAgentRu2 = subscriber.onAgentRunStarted) === null || _subscriber$onAgentRu2 === void 0 ? void 0 : _subscriber$onAgentRu2.call(subscriber, {
1721
- copilotkit: this.core,
1722
- agent
1723
- });
1724
- }, "Subscriber onAgentRunStarted error:");
1725
1773
  const isTopLevel = this._runDepth === 0;
1726
1774
  let originalAbortRun;
1727
1775
  if (isTopLevel) {
@@ -1793,7 +1841,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1793
1841
  await this._internal.waitForPendingFrameworkUpdates();
1794
1842
  return await this.runAgent({ agent });
1795
1843
  }
1796
- this._internal.suggestionEngine.reloadSuggestions(agentId, agent);
1844
+ this._internal.suggestionEngine.reloadSuggestions(agentId);
1797
1845
  return runAgentResult;
1798
1846
  }
1799
1847
  /**
@@ -2222,21 +2270,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2222
2270
  initialize() {}
2223
2271
  /**
2224
2272
  * Subscribe to an agent's events to track state and messages.
2225
- *
2226
- * Registry agents (subscribed via `onAgentsChanged`) use the bare `agentId`
2227
- * key so that `unsubscribeFromAgent(agentId)` can remove them when they
2228
- * are replaced. Per-thread clones (subscribed via `subscribeAgentToStateManager`)
2229
- * pass `{ isClone: true }` to use a composite `agentId:threadId` key, keeping
2230
- * their subscription independent of the registry agent's.
2231
2273
  */
2232
- subscribeToAgent(agent, { isClone = false } = {}) {
2274
+ subscribeToAgent(agent) {
2233
2275
  if (!agent.agentId) return;
2234
2276
  const agentId = agent.agentId;
2235
- const subscriptionKey = isClone && agent.threadId ? `${agentId}:${agent.threadId}` : agentId;
2236
- const existingUnsubscribe = this.agentSubscriptions.get(subscriptionKey);
2277
+ const existingUnsubscribe = this.agentSubscriptions.get(agentId);
2237
2278
  if (existingUnsubscribe) {
2238
2279
  existingUnsubscribe();
2239
- this.agentSubscriptions.delete(subscriptionKey);
2280
+ this.agentSubscriptions.delete(agentId);
2240
2281
  }
2241
2282
  let revoked = false;
2242
2283
  let subRunId;
@@ -2283,16 +2324,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2283
2324
  this.handleNewMessage(agent, message, input ? effectiveInput(input) : void 0);
2284
2325
  }
2285
2326
  });
2286
- this.agentSubscriptions.set(subscriptionKey, () => {
2327
+ this.agentSubscriptions.set(agentId, () => {
2287
2328
  revoked = true;
2288
2329
  unsubscribe();
2289
2330
  });
2290
2331
  }
2291
2332
  /**
2292
- * Unsubscribe a registry agent's subscription (bare `agentId` key).
2293
- * Per-thread clone subscriptions use composite `agentId:threadId` keys and
2294
- * are replaced (not removed) by subsequent subscribeToAgent(agent, { isClone: true })
2295
- * calls for the same (agentId, threadId) pair.
2333
+ * Unsubscribe an agent's subscription.
2296
2334
  */
2297
2335
  unsubscribeFromAgent(agentId) {
2298
2336
  const unsubscribe = this.agentSubscriptions.get(agentId);
@@ -2429,6 +2467,67 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2429
2467
  }
2430
2468
  };
2431
2469
 
2470
+ //#endregion
2471
+ //#region src/core/thread-store-registry.ts
2472
+ var ThreadStoreRegistry = class {
2473
+ constructor(core) {
2474
+ this.core = core;
2475
+ _defineProperty(this, "_stores", {});
2476
+ _defineProperty(this, "_snapshot", null);
2477
+ }
2478
+ register(agentId, store) {
2479
+ if (agentId in this._stores) {
2480
+ const prevStore = this._stores[agentId];
2481
+ delete this._stores[agentId];
2482
+ this._snapshot = null;
2483
+ this.notifyUnregistered(agentId, prevStore).catch((err) => {
2484
+ console.error("ThreadStoreRegistry notifyUnregistered failed:", err);
2485
+ });
2486
+ }
2487
+ this._stores[agentId] = store;
2488
+ this._snapshot = null;
2489
+ this.notifyRegistered(agentId, store).catch((err) => {
2490
+ console.error("ThreadStoreRegistry notifyRegistered failed:", err);
2491
+ });
2492
+ }
2493
+ unregister(agentId) {
2494
+ if (!(agentId in this._stores)) return;
2495
+ const prevStore = this._stores[agentId];
2496
+ delete this._stores[agentId];
2497
+ this._snapshot = null;
2498
+ this.notifyUnregistered(agentId, prevStore).catch((err) => {
2499
+ console.error("ThreadStoreRegistry notifyUnregistered failed:", err);
2500
+ });
2501
+ }
2502
+ get(agentId) {
2503
+ return this._stores[agentId];
2504
+ }
2505
+ getAll() {
2506
+ if (this._snapshot === null) this._snapshot = Object.freeze({ ...this._stores });
2507
+ return this._snapshot;
2508
+ }
2509
+ async notifyRegistered(agentId, store) {
2510
+ await this.core.notifySubscribers((subscriber) => {
2511
+ var _subscriber$onThreadS;
2512
+ return (_subscriber$onThreadS = subscriber.onThreadStoreRegistered) === null || _subscriber$onThreadS === void 0 ? void 0 : _subscriber$onThreadS.call(subscriber, {
2513
+ copilotkit: this.core,
2514
+ agentId,
2515
+ store
2516
+ });
2517
+ }, "Subscriber onThreadStoreRegistered error:");
2518
+ }
2519
+ async notifyUnregistered(agentId, prevStore) {
2520
+ await this.core.notifySubscribers((subscriber) => {
2521
+ var _subscriber$onThreadS2;
2522
+ return (_subscriber$onThreadS2 = subscriber.onThreadStoreUnregistered) === null || _subscriber$onThreadS2 === void 0 ? void 0 : _subscriber$onThreadS2.call(subscriber, {
2523
+ copilotkit: this.core,
2524
+ agentId,
2525
+ prevStore
2526
+ });
2527
+ }, "Subscriber onThreadStoreUnregistered error:");
2528
+ }
2529
+ };
2530
+
2432
2531
  //#endregion
2433
2532
  //#region src/core/core.ts
2434
2533
  let CopilotKitCoreErrorCode = /* @__PURE__ */ function(CopilotKitCoreErrorCode) {
@@ -2498,6 +2597,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2498
2597
  _defineProperty(this, "suggestionEngine", void 0);
2499
2598
  _defineProperty(this, "runHandler", void 0);
2500
2599
  _defineProperty(this, "stateManager", void 0);
2600
+ _defineProperty(this, "threadStoreRegistry", void 0);
2601
+ _defineProperty(this, "previousAgentIds", /* @__PURE__ */ new Set());
2501
2602
  this._headers = headers;
2502
2603
  this._credentials = credentials;
2503
2604
  this._properties = properties;
@@ -2507,16 +2608,30 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2507
2608
  this.suggestionEngine = new SuggestionEngine(this);
2508
2609
  this.runHandler = new RunHandler(this);
2509
2610
  this.stateManager = new StateManager(this);
2611
+ this.threadStoreRegistry = new ThreadStoreRegistry(this);
2510
2612
  this.agentRegistry.initialize(agents__unsafe_dev_only);
2511
2613
  this.runHandler.initialize(tools);
2512
2614
  this.suggestionEngine.initialize(suggestionsConfig);
2513
2615
  this.stateManager.initialize();
2514
2616
  this.agentRegistry.setRuntimeTransport(runtimeTransport);
2515
2617
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
2618
+ this.previousAgentIds = new Set(Object.keys(agents__unsafe_dev_only));
2516
2619
  this.subscribe({ onAgentsChanged: ({ agents }) => {
2517
2620
  Object.values(agents).forEach((agent) => {
2518
2621
  if (agent.agentId) this.stateManager.subscribeToAgent(agent);
2519
2622
  });
2623
+ const currentAgentIds = new Set(Object.keys(agents));
2624
+ for (const agentId of Object.keys(this.threadStoreRegistry.getAll())) if (this.previousAgentIds.has(agentId) && !currentAgentIds.has(agentId)) try {
2625
+ this.threadStoreRegistry.unregister(agentId);
2626
+ } catch (err) {
2627
+ console.error(`CopilotKitCore.onAgentsChanged: threadStoreRegistry.unregister failed for "${agentId}":`, err);
2628
+ }
2629
+ for (const agentId of this.previousAgentIds) if (!currentAgentIds.has(agentId)) try {
2630
+ this.stateManager.unsubscribeFromAgent(agentId);
2631
+ } catch (err) {
2632
+ console.error(`CopilotKitCore.onAgentsChanged: stateManager.unsubscribeFromAgent failed for "${agentId}":`, err);
2633
+ }
2634
+ this.previousAgentIds = currentAgentIds;
2520
2635
  } });
2521
2636
  }
2522
2637
  /**
@@ -2687,6 +2802,28 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2687
2802
  removeAgent__unsafe_dev_only(id) {
2688
2803
  this.agentRegistry.removeAgent__unsafe_dev_only(id);
2689
2804
  }
2805
+ /**
2806
+ * Register a proxied agent against an existing runtime agent. The proxy is
2807
+ * exposed under `agentId` (local registry id) and routes outbound runtime
2808
+ * requests to `runtimeAgentId`. Throws if `agentId` is already taken.
2809
+ *
2810
+ * Returns the minted proxy and an `unregister` handle for cleanup.
2811
+ *
2812
+ * Use this to mount multiple frontend agents against a single runtime
2813
+ * agent (e.g. one per chat window) without implicit per-thread cloning.
2814
+ *
2815
+ * @example
2816
+ * const { agent, unregister } = copilotkit.registerProxiedAgent({
2817
+ * agentId: "chat-1",
2818
+ * runtimeAgentId: "default",
2819
+ * });
2820
+ * // ... <CopilotChat agentId="chat-1" />
2821
+ * // on cleanup:
2822
+ * unregister();
2823
+ */
2824
+ registerProxiedAgent(params) {
2825
+ return this.agentRegistry.registerProxiedAgent(params);
2826
+ }
2690
2827
  getAgent(id) {
2691
2828
  return this.agentRegistry.getAgent(id);
2692
2829
  }
@@ -2700,6 +2837,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2700
2837
  this.contextStore.removeContext(id);
2701
2838
  }
2702
2839
  /**
2840
+ * Thread store registry (delegated to ThreadStoreRegistry)
2841
+ */
2842
+ registerThreadStore(agentId, store) {
2843
+ this.threadStoreRegistry.register(agentId, store);
2844
+ }
2845
+ unregisterThreadStore(agentId) {
2846
+ this.threadStoreRegistry.unregister(agentId);
2847
+ }
2848
+ getThreadStore(agentId) {
2849
+ return this.threadStoreRegistry.get(agentId);
2850
+ }
2851
+ getThreadStores() {
2852
+ return this.threadStoreRegistry.getAll();
2853
+ }
2854
+ /**
2703
2855
  * Suggestions management (delegated to SuggestionEngine)
2704
2856
  */
2705
2857
  addSuggestionsConfig(config) {
@@ -2906,9 +3058,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2906
3058
  getRunIdsForThread(agentId, threadId) {
2907
3059
  return this.stateManager.getRunIdsForThread(agentId, threadId);
2908
3060
  }
2909
- subscribeAgentToStateManager(agent) {
2910
- this.stateManager.subscribeToAgent(agent, { isClone: true });
2911
- }
2912
3061
  /**
2913
3062
  * Internal method used by RunHandler to build frontend tools
2914
3063
  */
@@ -3797,6 +3946,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3797
3946
  setContext(context) {
3798
3947
  store.dispatch(threadAdapterEvents.contextChanged({ context }));
3799
3948
  },
3949
+ refresh() {
3950
+ const { sessionId, context } = store.getState();
3951
+ if (!context) return;
3952
+ store.dispatch(threadRestEvents.listRequested({ sessionId }));
3953
+ },
3800
3954
  fetchNextPage() {
3801
3955
  store.dispatch(threadAdapterEvents.fetchNextPageRequested());
3802
3956
  },
@@ -3831,6 +3985,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3831
3985
  const ɵselectThreadsError = selectThreadsError;
3832
3986
  const ɵselectHasNextPage = selectHasNextPage;
3833
3987
  const ɵselectIsFetchingNextPage = selectIsFetchingNextPage;
3988
+ /**
3989
+ * Number of consecutive WebSocket connection failures after which the
3990
+ * threads channel tears itself down rather than retrying indefinitely.
3991
+ * Exposed for tests so they can assert teardown semantics without
3992
+ * hardcoding the threshold separately from production.
3993
+ */
3994
+ const ɵMAX_SOCKET_RETRIES = MAX_SOCKET_RETRIES;
3834
3995
 
3835
3996
  //#endregion
3836
3997
  exports.AgentRegistry = AgentRegistry;
@@ -3858,6 +4019,7 @@ exports.on = on;
3858
4019
  exports.parseToolArguments = parseToolArguments;
3859
4020
  exports.props = props;
3860
4021
  exports.select = select;
4022
+ exports.ɵMAX_SOCKET_RETRIES = ɵMAX_SOCKET_RETRIES;
3861
4023
  exports.ɵcreateThreadStore = createThreadStore;
3862
4024
  exports.ɵjoinPhoenixChannel$ = ɵjoinPhoenixChannel$;
3863
4025
  exports.ɵobservePhoenixEvent$ = ɵobservePhoenixEvent$;