@copilotkit/core 1.62.3 → 1.63.1

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
@@ -1005,6 +1005,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1005
1005
  _defineProperty(this, "localAgents", {});
1006
1006
  _defineProperty(this, "remoteAgents", {});
1007
1007
  _defineProperty(this, "_runtimeUrl", void 0);
1008
+ _defineProperty(this, "_connectionInFlight", void 0);
1008
1009
  _defineProperty(this, "_runtimeVersion", void 0);
1009
1010
  _defineProperty(this, "_runtimeConnectionStatus", CopilotKitCoreRuntimeConnectionStatus.Disconnected);
1010
1011
  _defineProperty(this, "_runtimeTransport", "auto");
@@ -1085,10 +1086,28 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1085
1086
  /**
1086
1087
  * Set the runtime URL and update connection
1087
1088
  */
1088
- setRuntimeUrl(runtimeUrl) {
1089
+ setRuntimeUrl(runtimeUrl, options) {
1089
1090
  const normalizedRuntimeUrl = runtimeUrl ? runtimeUrl.replace(/\/$/, "") : void 0;
1090
1091
  if (this._runtimeUrl === normalizedRuntimeUrl) return;
1091
1092
  this._runtimeUrl = normalizedRuntimeUrl;
1093
+ if (options === null || options === void 0 ? void 0 : options.deferConnection) return;
1094
+ this.updateRuntimeConnection();
1095
+ }
1096
+ /**
1097
+ * Start the initial runtime connection if it has not been started yet.
1098
+ *
1099
+ * Backs {@link CopilotKitCore.connect}. Idempotent: it only kicks off a fetch
1100
+ * when a `runtimeUrl` is set and the connection is still `Disconnected` (its
1101
+ * state before any connect attempt). `updateRuntimeConnection` flips the
1102
+ * status to `Connecting` synchronously, so a second call — e.g. React
1103
+ * StrictMode double-invoking the mount effect — bails here. A genuine config
1104
+ * change still reconnects through `setRuntimeUrl`/`setRuntimeTransport`. See
1105
+ * #5801.
1106
+ */
1107
+ connectRuntime() {
1108
+ if (typeof window === "undefined") return;
1109
+ if (!this._runtimeUrl) return;
1110
+ if (this._runtimeConnectionStatus !== CopilotKitCoreRuntimeConnectionStatus.Disconnected) return;
1092
1111
  this.updateRuntimeConnection();
1093
1112
  }
1094
1113
  setRuntimeTransport(runtimeTransport) {
@@ -1233,7 +1252,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1233
1252
  * Update runtime connection and fetch remote agents
1234
1253
  */
1235
1254
  async updateRuntimeConnection() {
1255
+ var _this$_runtimeUrl;
1236
1256
  if (typeof window === "undefined") return;
1257
+ const key = `${(_this$_runtimeUrl = this._runtimeUrl) !== null && _this$_runtimeUrl !== void 0 ? _this$_runtimeUrl : ""}::${this._requestedTransport}`;
1258
+ const inFlight = this._connectionInFlight;
1259
+ if (inFlight && inFlight.key === key) return inFlight.promise;
1260
+ const promise = this.performRuntimeConnection();
1261
+ this._connectionInFlight = {
1262
+ key,
1263
+ promise
1264
+ };
1265
+ promise.finally(() => {
1266
+ var _this$_connectionInFl;
1267
+ if (((_this$_connectionInFl = this._connectionInFlight) === null || _this$_connectionInFl === void 0 ? void 0 : _this$_connectionInFl.promise) === promise) this._connectionInFlight = void 0;
1268
+ });
1269
+ return promise;
1270
+ }
1271
+ async performRuntimeConnection() {
1237
1272
  if (!this.runtimeUrl) {
1238
1273
  this._runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Disconnected;
1239
1274
  this._runtimeVersion = void 0;
@@ -3807,7 +3842,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3807
3842
  return Object.fromEntries(Object.entries(headers).filter((entry) => entry[1] != null));
3808
3843
  }
3809
3844
  var CopilotKitCore = class {
3810
- constructor({ runtimeUrl, runtimeTransport = "auto", headers = {}, credentials, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug }) {
3845
+ constructor({ runtimeUrl, runtimeTransport = "auto", deferInitialConnection = false, headers = {}, credentials, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug }) {
3811
3846
  _defineProperty(this, "_headers", void 0);
3812
3847
  _defineProperty(this, "_credentials", void 0);
3813
3848
  _defineProperty(this, "_properties", void 0);
@@ -3837,7 +3872,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3837
3872
  this.suggestionEngine.initialize(suggestionsConfig);
3838
3873
  this.stateManager.initialize();
3839
3874
  this.agentRegistry.setRuntimeTransport(runtimeTransport);
3840
- this.agentRegistry.setRuntimeUrl(runtimeUrl);
3875
+ this.agentRegistry.setRuntimeUrl(runtimeUrl, { deferConnection: deferInitialConnection });
3841
3876
  this.previousAgentIds = new Set(Object.keys(agents__unsafe_dev_only));
3842
3877
  this.subscribe({
3843
3878
  onRuntimeConnectionStatusChanged: () => {
@@ -3916,6 +3951,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3916
3951
  setRuntimeUrl(runtimeUrl) {
3917
3952
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
3918
3953
  }
3954
+ /**
3955
+ * Start the runtime `/info` connection if it has not been started yet.
3956
+ *
3957
+ * Intended to be driven from a host's commit-phase effect when the core was
3958
+ * constructed with {@link CopilotKitCoreConfig.deferInitialConnection}. Safe
3959
+ * to call repeatedly — it is a no-op once a connection is in progress or
3960
+ * settled, so a double-invoked mount effect (React StrictMode) collapses to a
3961
+ * single request. See #5801.
3962
+ */
3963
+ connect() {
3964
+ this.agentRegistry.connectRuntime();
3965
+ }
3919
3966
  get runtimeTransport() {
3920
3967
  return this.agentRegistry.runtimeTransport;
3921
3968
  }