@cadraos/sdk 0.3.6 → 0.3.8

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.js CHANGED
@@ -675,6 +675,15 @@ var ExecutionStreamControllerImpl = class {
675
675
  sseClient = null;
676
676
  status = "connecting";
677
677
  reconnectAttempts = 0;
678
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
679
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
680
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
681
+ // the client opens the stream — the norm on the webchat path where msg-api owns
682
+ // the executionId and cadra persists the agent_executions row via an async
683
+ // worker callback). See handleConnectionError.
684
+ hasConnected = false;
685
+ notFoundRetries = 0;
686
+ maxConnectRaceRetries = 3;
678
687
  seenEventIds = /* @__PURE__ */ new Set();
679
688
  _lastEventId = null;
680
689
  activityTimeoutId = null;
@@ -814,6 +823,8 @@ var ExecutionStreamControllerImpl = class {
814
823
  if (connected) {
815
824
  this.status = "connected";
816
825
  this.reconnectAttempts = 0;
826
+ this.hasConnected = true;
827
+ this.notFoundRetries = 0;
817
828
  this.resetActivityTimeout();
818
829
  }
819
830
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -1103,6 +1114,38 @@ var ExecutionStreamControllerImpl = class {
1103
1114
  return;
1104
1115
  }
1105
1116
  const msg = error instanceof Error ? error.message : String(error);
1117
+ const isNotFound = /\b404\b/.test(msg);
1118
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
1119
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
1120
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
1121
+ this.notFoundRetries++;
1122
+ this.status = "reconnecting";
1123
+ const delay = Math.min(
1124
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
1125
+ this.config.reconnectMaxDelayMs
1126
+ );
1127
+ await new Promise((resolve) => setTimeout(resolve, delay));
1128
+ if (this.status === "closed") {
1129
+ return;
1130
+ }
1131
+ try {
1132
+ await this.connectSSE();
1133
+ } catch (retryError) {
1134
+ await this.handleConnectionError(retryError);
1135
+ }
1136
+ return;
1137
+ }
1138
+ if (this.pollingEnabled) {
1139
+ if (this.sseClient) {
1140
+ this.sseClient.disconnect();
1141
+ this.sseClient = null;
1142
+ }
1143
+ this.status = "fallback_polling";
1144
+ this.emit("connectionChange", { connected: false, usingFallback: true });
1145
+ this.startPolling();
1146
+ return;
1147
+ }
1148
+ }
1106
1149
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
1107
1150
  if (this.sseClient) {
1108
1151
  this.sseClient.disconnect();
@@ -1352,7 +1395,6 @@ var AgentsAPI = class extends BaseResourceAPI {
1352
1395
  super(http, "/api/v1/agents");
1353
1396
  this.getStreamConfig = getStreamConfig;
1354
1397
  }
1355
- getStreamConfig;
1356
1398
  // ===== LIST Operations =====
1357
1399
  /**
1358
1400
  * List all agents in the organization
@@ -2258,7 +2300,6 @@ var SessionsAPI = class extends BaseResourceAPI {
2258
2300
  super(http, "/api/v1/sessions");
2259
2301
  this.getStreamConfig = getStreamConfig;
2260
2302
  }
2261
- getStreamConfig;
2262
2303
  // ===== LIST Operations =====
2263
2304
  /**
2264
2305
  * List all sessions in the organization
@@ -2578,7 +2619,6 @@ var KanbanAPI = class extends BaseResourceAPI {
2578
2619
  super(http, "/api/v1/kanban");
2579
2620
  this.getStreamConfig = getStreamConfig;
2580
2621
  }
2581
- getStreamConfig;
2582
2622
  // ===== BOARD =====
2583
2623
  /**
2584
2624
  * Get the current org's board (lanes in declared order). Auto-provisions
@@ -2780,7 +2820,6 @@ var RemoteAgentsAPI = class extends BaseResourceAPI {
2780
2820
  super(http, "/api/v1/remote-agents");
2781
2821
  this.getStreamConfig = getStreamConfig;
2782
2822
  }
2783
- getStreamConfig;
2784
2823
  /**
2785
2824
  * List all remote agents in the organization
2786
2825
  *