@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.
@@ -689,6 +689,15 @@ var ExecutionStreamControllerImpl = class {
689
689
  sseClient = null;
690
690
  status = "connecting";
691
691
  reconnectAttempts = 0;
692
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
693
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
694
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
695
+ // the client opens the stream — the norm on the webchat path where msg-api owns
696
+ // the executionId and cadra persists the agent_executions row via an async
697
+ // worker callback). See handleConnectionError.
698
+ hasConnected = false;
699
+ notFoundRetries = 0;
700
+ maxConnectRaceRetries = 3;
692
701
  seenEventIds = /* @__PURE__ */ new Set();
693
702
  _lastEventId = null;
694
703
  activityTimeoutId = null;
@@ -828,6 +837,8 @@ var ExecutionStreamControllerImpl = class {
828
837
  if (connected) {
829
838
  this.status = "connected";
830
839
  this.reconnectAttempts = 0;
840
+ this.hasConnected = true;
841
+ this.notFoundRetries = 0;
831
842
  this.resetActivityTimeout();
832
843
  }
833
844
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -1117,6 +1128,38 @@ var ExecutionStreamControllerImpl = class {
1117
1128
  return;
1118
1129
  }
1119
1130
  const msg = error instanceof Error ? error.message : String(error);
1131
+ const isNotFound = /\b404\b/.test(msg);
1132
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
1133
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
1134
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
1135
+ this.notFoundRetries++;
1136
+ this.status = "reconnecting";
1137
+ const delay = Math.min(
1138
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
1139
+ this.config.reconnectMaxDelayMs
1140
+ );
1141
+ await new Promise((resolve) => setTimeout(resolve, delay));
1142
+ if (this.status === "closed") {
1143
+ return;
1144
+ }
1145
+ try {
1146
+ await this.connectSSE();
1147
+ } catch (retryError) {
1148
+ await this.handleConnectionError(retryError);
1149
+ }
1150
+ return;
1151
+ }
1152
+ if (this.pollingEnabled) {
1153
+ if (this.sseClient) {
1154
+ this.sseClient.disconnect();
1155
+ this.sseClient = null;
1156
+ }
1157
+ this.status = "fallback_polling";
1158
+ this.emit("connectionChange", { connected: false, usingFallback: true });
1159
+ this.startPolling();
1160
+ return;
1161
+ }
1162
+ }
1120
1163
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
1121
1164
  if (this.sseClient) {
1122
1165
  this.sseClient.disconnect();
@@ -1366,7 +1409,6 @@ var AgentsAPI = class extends BaseResourceAPI {
1366
1409
  super(http, "/api/v1/agents");
1367
1410
  this.getStreamConfig = getStreamConfig;
1368
1411
  }
1369
- getStreamConfig;
1370
1412
  // ===== LIST Operations =====
1371
1413
  /**
1372
1414
  * List all agents in the organization
@@ -2272,7 +2314,6 @@ var SessionsAPI = class extends BaseResourceAPI {
2272
2314
  super(http, "/api/v1/sessions");
2273
2315
  this.getStreamConfig = getStreamConfig;
2274
2316
  }
2275
- getStreamConfig;
2276
2317
  // ===== LIST Operations =====
2277
2318
  /**
2278
2319
  * List all sessions in the organization
@@ -2592,7 +2633,6 @@ var KanbanAPI = class extends BaseResourceAPI {
2592
2633
  super(http, "/api/v1/kanban");
2593
2634
  this.getStreamConfig = getStreamConfig;
2594
2635
  }
2595
- getStreamConfig;
2596
2636
  // ===== BOARD =====
2597
2637
  /**
2598
2638
  * Get the current org's board (lanes in declared order). Auto-provisions
@@ -2794,7 +2834,6 @@ var RemoteAgentsAPI = class extends BaseResourceAPI {
2794
2834
  super(http, "/api/v1/remote-agents");
2795
2835
  this.getStreamConfig = getStreamConfig;
2796
2836
  }
2797
- getStreamConfig;
2798
2837
  /**
2799
2838
  * List all remote agents in the organization
2800
2839
  *
@@ -8598,7 +8637,8 @@ function AgentChat({
8598
8637
  hideHeaderWhileEmpty = false,
8599
8638
  hideTypingIndicator = false,
8600
8639
  onInputChange,
8601
- onInputControlReady
8640
+ onInputControlReady,
8641
+ injectedMessages
8602
8642
  }) {
8603
8643
  const {
8604
8644
  messages,
@@ -8662,8 +8702,9 @@ function AgentChat({
8662
8702
  },
8663
8703
  [enableAttachments, attachments, sendMessage]
8664
8704
  );
8705
+ const renderedMessages = injectedMessages && injectedMessages.length > 0 ? [...messages, ...injectedMessages].filter((m, i, arr) => arr.findIndex((x) => x.id === m.id) === i).sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()) : messages;
8665
8706
  const messageListProps = {
8666
- messages,
8707
+ messages: renderedMessages,
8667
8708
  welcomeMessage: hideHeader ? void 0 : welcomeMessage,
8668
8709
  renderWelcome: hideHeader ? void 0 : renderWelcome,
8669
8710
  isLoading,