@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.
@@ -3303,7 +3303,8 @@ var HttpClient = class {
3303
3303
  const controller = new AbortController();
3304
3304
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
3305
3305
  try {
3306
- const response = await fetch(url, {
3306
+ const doFetch = this.config.fetch ?? fetch;
3307
+ const response = await doFetch(url, {
3307
3308
  method: options.method,
3308
3309
  headers,
3309
3310
  body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
@@ -3435,7 +3436,8 @@ var SSEClient = class {
3435
3436
  console.log("[SDK:SSE] Auth header:", this.config.authHeader ? `${this.config.authHeader.substring(0, 30)}...` : "MISSING");
3436
3437
  console.log("[SDK:SSE] Org ID:", this.config.orgId);
3437
3438
  try {
3438
- const response = await fetch(this.config.url, {
3439
+ const doFetch = this.config.fetch ?? fetch;
3440
+ const response = await doFetch(this.config.url, {
3439
3441
  method: "GET",
3440
3442
  headers: {
3441
3443
  "Authorization": this.config.authHeader,
@@ -3673,6 +3675,15 @@ var ExecutionStreamControllerImpl = class {
3673
3675
  sseClient = null;
3674
3676
  status = "connecting";
3675
3677
  reconnectAttempts = 0;
3678
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
3679
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
3680
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
3681
+ // the client opens the stream — the norm on the webchat path where msg-api owns
3682
+ // the executionId and cadra persists the agent_executions row via an async
3683
+ // worker callback). See handleConnectionError.
3684
+ hasConnected = false;
3685
+ notFoundRetries = 0;
3686
+ maxConnectRaceRetries = 3;
3676
3687
  seenEventIds = /* @__PURE__ */ new Set();
3677
3688
  _lastEventId = null;
3678
3689
  activityTimeoutId = null;
@@ -3801,6 +3812,7 @@ var ExecutionStreamControllerImpl = class {
3801
3812
  url,
3802
3813
  authHeader: `Bearer ${authToken}`,
3803
3814
  orgId: this.options.orgId,
3815
+ fetch: this.options.fetch,
3804
3816
  onEvent: (event) => this.handleEvent(event),
3805
3817
  onError: (error) => {
3806
3818
  console.error("[SDK:StreamController] SSE error:", error);
@@ -3811,6 +3823,8 @@ var ExecutionStreamControllerImpl = class {
3811
3823
  if (connected) {
3812
3824
  this.status = "connected";
3813
3825
  this.reconnectAttempts = 0;
3826
+ this.hasConnected = true;
3827
+ this.notFoundRetries = 0;
3814
3828
  this.resetActivityTimeout();
3815
3829
  }
3816
3830
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -4100,6 +4114,38 @@ var ExecutionStreamControllerImpl = class {
4100
4114
  return;
4101
4115
  }
4102
4116
  const msg = error instanceof Error ? error.message : String(error);
4117
+ const isNotFound = /\b404\b/.test(msg);
4118
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
4119
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
4120
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
4121
+ this.notFoundRetries++;
4122
+ this.status = "reconnecting";
4123
+ const delay = Math.min(
4124
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
4125
+ this.config.reconnectMaxDelayMs
4126
+ );
4127
+ await new Promise((resolve) => setTimeout(resolve, delay));
4128
+ if (this.status === "closed") {
4129
+ return;
4130
+ }
4131
+ try {
4132
+ await this.connectSSE();
4133
+ } catch (retryError) {
4134
+ await this.handleConnectionError(retryError);
4135
+ }
4136
+ return;
4137
+ }
4138
+ if (this.pollingEnabled) {
4139
+ if (this.sseClient) {
4140
+ this.sseClient.disconnect();
4141
+ this.sseClient = null;
4142
+ }
4143
+ this.status = "fallback_polling";
4144
+ this.emit("connectionChange", { connected: false, usingFallback: true });
4145
+ this.startPolling();
4146
+ return;
4147
+ }
4148
+ }
4103
4149
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
4104
4150
  if (this.sseClient) {
4105
4151
  this.sseClient.disconnect();
@@ -4478,6 +4524,7 @@ var AgentsAPI = class extends BaseResourceAPI {
4478
4524
  apiKey: config.apiKey,
4479
4525
  tokenProvider: config.tokenProvider,
4480
4526
  orgId: config.orgId,
4527
+ fetch: config.fetch,
4481
4528
  getExecution: (id) => this.getExecution(id)
4482
4529
  });
4483
4530
  console.log("[SDK:Agents] Stream controller created - caller must call connect() after registering listeners");
@@ -4524,6 +4571,7 @@ var AgentsAPI = class extends BaseResourceAPI {
4524
4571
  apiKey: config.apiKey,
4525
4572
  tokenProvider: config.tokenProvider,
4526
4573
  orgId: config.orgId,
4574
+ fetch: config.fetch,
4527
4575
  getExecution: (id) => this.getExecution(id)
4528
4576
  });
4529
4577
  console.log("[SDK:Agents] Stream controller created for existing execution - caller must call connect() after registering listeners");
@@ -5372,6 +5420,7 @@ var SessionsAPI = class extends BaseResourceAPI {
5372
5420
  baseUrl: config.baseUrl,
5373
5421
  apiKey: config.apiKey,
5374
5422
  orgId: config.orgId,
5423
+ fetch: config.fetch,
5375
5424
  getExecution: (id) => this.getExecution(id)
5376
5425
  });
5377
5426
  await controller.connect();
@@ -6026,7 +6075,8 @@ var CadraClient = class {
6026
6075
  baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
6027
6076
  orgId: options.orgId,
6028
6077
  timeout: options.timeout ?? DEFAULT_TIMEOUT,
6029
- headers: options.headers
6078
+ headers: options.headers,
6079
+ fetch: options.fetch
6030
6080
  };
6031
6081
  if ("apiKey" in options) {
6032
6082
  this.config.apiKey = options.apiKey;
@@ -6047,7 +6097,8 @@ var CadraClient = class {
6047
6097
  orgId: this.config.orgId,
6048
6098
  timeout: this.config.timeout,
6049
6099
  retry: retryConfig,
6050
- headers: this.config.headers
6100
+ headers: this.config.headers,
6101
+ fetch: this.config.fetch
6051
6102
  };
6052
6103
  this.httpClient = new HttpClient(httpConfig);
6053
6104
  const getStreamConfig = () => ({
@@ -6055,7 +6106,8 @@ var CadraClient = class {
6055
6106
  apiKey: this.config.apiKey,
6056
6107
  token: this.config.token,
6057
6108
  tokenProvider: this.config.tokenProvider,
6058
- orgId: this.config.orgId
6109
+ orgId: this.config.orgId,
6110
+ fetch: this.config.fetch
6059
6111
  });
6060
6112
  this.agents = new AgentsAPI(this.httpClient, getStreamConfig);
6061
6113
  this.skills = new SkillsAPI(this.httpClient);