@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.
@@ -318,7 +318,8 @@ var HttpClient = class {
318
318
  const controller = new AbortController();
319
319
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
320
320
  try {
321
- const response = await fetch(url, {
321
+ const doFetch = this.config.fetch ?? fetch;
322
+ const response = await doFetch(url, {
322
323
  method: options.method,
323
324
  headers,
324
325
  body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
@@ -450,7 +451,8 @@ var SSEClient = class {
450
451
  console.log("[SDK:SSE] Auth header:", this.config.authHeader ? `${this.config.authHeader.substring(0, 30)}...` : "MISSING");
451
452
  console.log("[SDK:SSE] Org ID:", this.config.orgId);
452
453
  try {
453
- const response = await fetch(this.config.url, {
454
+ const doFetch = this.config.fetch ?? fetch;
455
+ const response = await doFetch(this.config.url, {
454
456
  method: "GET",
455
457
  headers: {
456
458
  "Authorization": this.config.authHeader,
@@ -688,6 +690,15 @@ var ExecutionStreamControllerImpl = class {
688
690
  sseClient = null;
689
691
  status = "connecting";
690
692
  reconnectAttempts = 0;
693
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
694
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
695
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
696
+ // the client opens the stream — the norm on the webchat path where msg-api owns
697
+ // the executionId and cadra persists the agent_executions row via an async
698
+ // worker callback). See handleConnectionError.
699
+ hasConnected = false;
700
+ notFoundRetries = 0;
701
+ maxConnectRaceRetries = 3;
691
702
  seenEventIds = /* @__PURE__ */ new Set();
692
703
  _lastEventId = null;
693
704
  activityTimeoutId = null;
@@ -816,6 +827,7 @@ var ExecutionStreamControllerImpl = class {
816
827
  url,
817
828
  authHeader: `Bearer ${authToken}`,
818
829
  orgId: this.options.orgId,
830
+ fetch: this.options.fetch,
819
831
  onEvent: (event) => this.handleEvent(event),
820
832
  onError: (error) => {
821
833
  console.error("[SDK:StreamController] SSE error:", error);
@@ -826,6 +838,8 @@ var ExecutionStreamControllerImpl = class {
826
838
  if (connected) {
827
839
  this.status = "connected";
828
840
  this.reconnectAttempts = 0;
841
+ this.hasConnected = true;
842
+ this.notFoundRetries = 0;
829
843
  this.resetActivityTimeout();
830
844
  }
831
845
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -1115,6 +1129,38 @@ var ExecutionStreamControllerImpl = class {
1115
1129
  return;
1116
1130
  }
1117
1131
  const msg = error instanceof Error ? error.message : String(error);
1132
+ const isNotFound = /\b404\b/.test(msg);
1133
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
1134
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
1135
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
1136
+ this.notFoundRetries++;
1137
+ this.status = "reconnecting";
1138
+ const delay = Math.min(
1139
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
1140
+ this.config.reconnectMaxDelayMs
1141
+ );
1142
+ await new Promise((resolve) => setTimeout(resolve, delay));
1143
+ if (this.status === "closed") {
1144
+ return;
1145
+ }
1146
+ try {
1147
+ await this.connectSSE();
1148
+ } catch (retryError) {
1149
+ await this.handleConnectionError(retryError);
1150
+ }
1151
+ return;
1152
+ }
1153
+ if (this.pollingEnabled) {
1154
+ if (this.sseClient) {
1155
+ this.sseClient.disconnect();
1156
+ this.sseClient = null;
1157
+ }
1158
+ this.status = "fallback_polling";
1159
+ this.emit("connectionChange", { connected: false, usingFallback: true });
1160
+ this.startPolling();
1161
+ return;
1162
+ }
1163
+ }
1118
1164
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
1119
1165
  if (this.sseClient) {
1120
1166
  this.sseClient.disconnect();
@@ -1493,6 +1539,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1493
1539
  apiKey: config.apiKey,
1494
1540
  tokenProvider: config.tokenProvider,
1495
1541
  orgId: config.orgId,
1542
+ fetch: config.fetch,
1496
1543
  getExecution: (id) => this.getExecution(id)
1497
1544
  });
1498
1545
  console.log("[SDK:Agents] Stream controller created - caller must call connect() after registering listeners");
@@ -1539,6 +1586,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1539
1586
  apiKey: config.apiKey,
1540
1587
  tokenProvider: config.tokenProvider,
1541
1588
  orgId: config.orgId,
1589
+ fetch: config.fetch,
1542
1590
  getExecution: (id) => this.getExecution(id)
1543
1591
  });
1544
1592
  console.log("[SDK:Agents] Stream controller created for existing execution - caller must call connect() after registering listeners");
@@ -2387,6 +2435,7 @@ var SessionsAPI = class extends BaseResourceAPI {
2387
2435
  baseUrl: config.baseUrl,
2388
2436
  apiKey: config.apiKey,
2389
2437
  orgId: config.orgId,
2438
+ fetch: config.fetch,
2390
2439
  getExecution: (id) => this.getExecution(id)
2391
2440
  });
2392
2441
  await controller.connect();
@@ -3041,7 +3090,8 @@ var CadraClient = class {
3041
3090
  baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
3042
3091
  orgId: options.orgId,
3043
3092
  timeout: options.timeout ?? DEFAULT_TIMEOUT,
3044
- headers: options.headers
3093
+ headers: options.headers,
3094
+ fetch: options.fetch
3045
3095
  };
3046
3096
  if ("apiKey" in options) {
3047
3097
  this.config.apiKey = options.apiKey;
@@ -3062,7 +3112,8 @@ var CadraClient = class {
3062
3112
  orgId: this.config.orgId,
3063
3113
  timeout: this.config.timeout,
3064
3114
  retry: retryConfig,
3065
- headers: this.config.headers
3115
+ headers: this.config.headers,
3116
+ fetch: this.config.fetch
3066
3117
  };
3067
3118
  this.httpClient = new HttpClient(httpConfig);
3068
3119
  const getStreamConfig = () => ({
@@ -3070,7 +3121,8 @@ var CadraClient = class {
3070
3121
  apiKey: this.config.apiKey,
3071
3122
  token: this.config.token,
3072
3123
  tokenProvider: this.config.tokenProvider,
3073
- orgId: this.config.orgId
3124
+ orgId: this.config.orgId,
3125
+ fetch: this.config.fetch
3074
3126
  });
3075
3127
  this.agents = new AgentsAPI(this.httpClient, getStreamConfig);
3076
3128
  this.skills = new SkillsAPI(this.httpClient);