@copilotkit/core 1.62.3 → 1.63.0

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
@@ -889,6 +889,7 @@ var AgentRegistry = class {
889
889
  localAgents = {};
890
890
  remoteAgents = {};
891
891
  _runtimeUrl;
892
+ _connectionInFlight;
892
893
  _runtimeVersion;
893
894
  _runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Disconnected;
894
895
  _runtimeTransport = "auto";
@@ -981,10 +982,28 @@ var AgentRegistry = class {
981
982
  /**
982
983
  * Set the runtime URL and update connection
983
984
  */
984
- setRuntimeUrl(runtimeUrl) {
985
+ setRuntimeUrl(runtimeUrl, options) {
985
986
  const normalizedRuntimeUrl = runtimeUrl ? runtimeUrl.replace(/\/$/, "") : void 0;
986
987
  if (this._runtimeUrl === normalizedRuntimeUrl) return;
987
988
  this._runtimeUrl = normalizedRuntimeUrl;
989
+ if (options?.deferConnection) return;
990
+ this.updateRuntimeConnection();
991
+ }
992
+ /**
993
+ * Start the initial runtime connection if it has not been started yet.
994
+ *
995
+ * Backs {@link CopilotKitCore.connect}. Idempotent: it only kicks off a fetch
996
+ * when a `runtimeUrl` is set and the connection is still `Disconnected` (its
997
+ * state before any connect attempt). `updateRuntimeConnection` flips the
998
+ * status to `Connecting` synchronously, so a second call — e.g. React
999
+ * StrictMode double-invoking the mount effect — bails here. A genuine config
1000
+ * change still reconnects through `setRuntimeUrl`/`setRuntimeTransport`. See
1001
+ * #5801.
1002
+ */
1003
+ connectRuntime() {
1004
+ if (typeof window === "undefined") return;
1005
+ if (!this._runtimeUrl) return;
1006
+ if (this._runtimeConnectionStatus !== CopilotKitCoreRuntimeConnectionStatus.Disconnected) return;
988
1007
  this.updateRuntimeConnection();
989
1008
  }
990
1009
  setRuntimeTransport(runtimeTransport) {
@@ -1130,6 +1149,20 @@ var AgentRegistry = class {
1130
1149
  */
1131
1150
  async updateRuntimeConnection() {
1132
1151
  if (typeof window === "undefined") return;
1152
+ const key = `${this._runtimeUrl ?? ""}::${this._requestedTransport}`;
1153
+ const inFlight = this._connectionInFlight;
1154
+ if (inFlight && inFlight.key === key) return inFlight.promise;
1155
+ const promise = this.performRuntimeConnection();
1156
+ this._connectionInFlight = {
1157
+ key,
1158
+ promise
1159
+ };
1160
+ promise.finally(() => {
1161
+ if (this._connectionInFlight?.promise === promise) this._connectionInFlight = void 0;
1162
+ });
1163
+ return promise;
1164
+ }
1165
+ async performRuntimeConnection() {
1133
1166
  if (!this.runtimeUrl) {
1134
1167
  this._runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Disconnected;
1135
1168
  this._runtimeVersion = void 0;
@@ -3681,7 +3714,7 @@ var CopilotKitCore = class {
3681
3714
  * store that was registered prior to that initial notification.
3682
3715
  */
3683
3716
  previousAgentIds = /* @__PURE__ */ new Set();
3684
- constructor({ runtimeUrl, runtimeTransport = "auto", headers = {}, credentials, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug }) {
3717
+ constructor({ runtimeUrl, runtimeTransport = "auto", deferInitialConnection = false, headers = {}, credentials, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug }) {
3685
3718
  this._headers = normalizeHeaders(headers);
3686
3719
  this._credentials = credentials;
3687
3720
  this._properties = properties;
@@ -3697,7 +3730,7 @@ var CopilotKitCore = class {
3697
3730
  this.suggestionEngine.initialize(suggestionsConfig);
3698
3731
  this.stateManager.initialize();
3699
3732
  this.agentRegistry.setRuntimeTransport(runtimeTransport);
3700
- this.agentRegistry.setRuntimeUrl(runtimeUrl);
3733
+ this.agentRegistry.setRuntimeUrl(runtimeUrl, { deferConnection: deferInitialConnection });
3701
3734
  this.previousAgentIds = new Set(Object.keys(agents__unsafe_dev_only));
3702
3735
  this.subscribe({
3703
3736
  onRuntimeConnectionStatusChanged: () => {
@@ -3773,6 +3806,18 @@ var CopilotKitCore = class {
3773
3806
  setRuntimeUrl(runtimeUrl) {
3774
3807
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
3775
3808
  }
3809
+ /**
3810
+ * Start the runtime `/info` connection if it has not been started yet.
3811
+ *
3812
+ * Intended to be driven from a host's commit-phase effect when the core was
3813
+ * constructed with {@link CopilotKitCoreConfig.deferInitialConnection}. Safe
3814
+ * to call repeatedly — it is a no-op once a connection is in progress or
3815
+ * settled, so a double-invoked mount effect (React StrictMode) collapses to a
3816
+ * single request. See #5801.
3817
+ */
3818
+ connect() {
3819
+ this.agentRegistry.connectRuntime();
3820
+ }
3776
3821
  get runtimeTransport() {
3777
3822
  return this.agentRegistry.runtimeTransport;
3778
3823
  }