@cadraos/sdk 0.3.5 → 0.3.7

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
@@ -295,7 +295,8 @@ var HttpClient = class {
295
295
  const controller = new AbortController();
296
296
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
297
297
  try {
298
- const response = await fetch(url, {
298
+ const doFetch = this.config.fetch ?? fetch;
299
+ const response = await doFetch(url, {
299
300
  method: options.method,
300
301
  headers,
301
302
  body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
@@ -427,7 +428,8 @@ var SSEClient = class {
427
428
  console.log("[SDK:SSE] Auth header:", this.config.authHeader ? `${this.config.authHeader.substring(0, 30)}...` : "MISSING");
428
429
  console.log("[SDK:SSE] Org ID:", this.config.orgId);
429
430
  try {
430
- const response = await fetch(this.config.url, {
431
+ const doFetch = this.config.fetch ?? fetch;
432
+ const response = await doFetch(this.config.url, {
431
433
  method: "GET",
432
434
  headers: {
433
435
  "Authorization": this.config.authHeader,
@@ -675,6 +677,15 @@ var ExecutionStreamControllerImpl = class {
675
677
  sseClient = null;
676
678
  status = "connecting";
677
679
  reconnectAttempts = 0;
680
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
681
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
682
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
683
+ // the client opens the stream — the norm on the webchat path where msg-api owns
684
+ // the executionId and cadra persists the agent_executions row via an async
685
+ // worker callback). See handleConnectionError.
686
+ hasConnected = false;
687
+ notFoundRetries = 0;
688
+ maxConnectRaceRetries = 3;
678
689
  seenEventIds = /* @__PURE__ */ new Set();
679
690
  _lastEventId = null;
680
691
  activityTimeoutId = null;
@@ -803,6 +814,7 @@ var ExecutionStreamControllerImpl = class {
803
814
  url,
804
815
  authHeader: `Bearer ${authToken}`,
805
816
  orgId: this.options.orgId,
817
+ fetch: this.options.fetch,
806
818
  onEvent: (event) => this.handleEvent(event),
807
819
  onError: (error) => {
808
820
  console.error("[SDK:StreamController] SSE error:", error);
@@ -813,6 +825,8 @@ var ExecutionStreamControllerImpl = class {
813
825
  if (connected) {
814
826
  this.status = "connected";
815
827
  this.reconnectAttempts = 0;
828
+ this.hasConnected = true;
829
+ this.notFoundRetries = 0;
816
830
  this.resetActivityTimeout();
817
831
  }
818
832
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -1102,6 +1116,38 @@ var ExecutionStreamControllerImpl = class {
1102
1116
  return;
1103
1117
  }
1104
1118
  const msg = error instanceof Error ? error.message : String(error);
1119
+ const isNotFound = /\b404\b/.test(msg);
1120
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
1121
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
1122
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
1123
+ this.notFoundRetries++;
1124
+ this.status = "reconnecting";
1125
+ const delay = Math.min(
1126
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
1127
+ this.config.reconnectMaxDelayMs
1128
+ );
1129
+ await new Promise((resolve) => setTimeout(resolve, delay));
1130
+ if (this.status === "closed") {
1131
+ return;
1132
+ }
1133
+ try {
1134
+ await this.connectSSE();
1135
+ } catch (retryError) {
1136
+ await this.handleConnectionError(retryError);
1137
+ }
1138
+ return;
1139
+ }
1140
+ if (this.pollingEnabled) {
1141
+ if (this.sseClient) {
1142
+ this.sseClient.disconnect();
1143
+ this.sseClient = null;
1144
+ }
1145
+ this.status = "fallback_polling";
1146
+ this.emit("connectionChange", { connected: false, usingFallback: true });
1147
+ this.startPolling();
1148
+ return;
1149
+ }
1150
+ }
1105
1151
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
1106
1152
  if (this.sseClient) {
1107
1153
  this.sseClient.disconnect();
@@ -1480,6 +1526,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1480
1526
  apiKey: config.apiKey,
1481
1527
  tokenProvider: config.tokenProvider,
1482
1528
  orgId: config.orgId,
1529
+ fetch: config.fetch,
1483
1530
  getExecution: (id) => this.getExecution(id)
1484
1531
  });
1485
1532
  console.log("[SDK:Agents] Stream controller created - caller must call connect() after registering listeners");
@@ -1526,6 +1573,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1526
1573
  apiKey: config.apiKey,
1527
1574
  tokenProvider: config.tokenProvider,
1528
1575
  orgId: config.orgId,
1576
+ fetch: config.fetch,
1529
1577
  getExecution: (id) => this.getExecution(id)
1530
1578
  });
1531
1579
  console.log("[SDK:Agents] Stream controller created for existing execution - caller must call connect() after registering listeners");
@@ -2374,6 +2422,7 @@ var SessionsAPI = class extends BaseResourceAPI {
2374
2422
  baseUrl: config.baseUrl,
2375
2423
  apiKey: config.apiKey,
2376
2424
  orgId: config.orgId,
2425
+ fetch: config.fetch,
2377
2426
  getExecution: (id) => this.getExecution(id)
2378
2427
  });
2379
2428
  await controller.connect();
@@ -3028,7 +3077,8 @@ var CadraClient = class {
3028
3077
  baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
3029
3078
  orgId: options.orgId,
3030
3079
  timeout: options.timeout ?? DEFAULT_TIMEOUT,
3031
- headers: options.headers
3080
+ headers: options.headers,
3081
+ fetch: options.fetch
3032
3082
  };
3033
3083
  if ("apiKey" in options) {
3034
3084
  this.config.apiKey = options.apiKey;
@@ -3049,7 +3099,8 @@ var CadraClient = class {
3049
3099
  orgId: this.config.orgId,
3050
3100
  timeout: this.config.timeout,
3051
3101
  retry: retryConfig,
3052
- headers: this.config.headers
3102
+ headers: this.config.headers,
3103
+ fetch: this.config.fetch
3053
3104
  };
3054
3105
  this.httpClient = new HttpClient(httpConfig);
3055
3106
  const getStreamConfig = () => ({
@@ -3057,7 +3108,8 @@ var CadraClient = class {
3057
3108
  apiKey: this.config.apiKey,
3058
3109
  token: this.config.token,
3059
3110
  tokenProvider: this.config.tokenProvider,
3060
- orgId: this.config.orgId
3111
+ orgId: this.config.orgId,
3112
+ fetch: this.config.fetch
3061
3113
  });
3062
3114
  this.agents = new AgentsAPI(this.httpClient, getStreamConfig);
3063
3115
  this.skills = new SkillsAPI(this.httpClient);