@cadraos/sdk 0.1.11-test → 0.1.12

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
@@ -577,9 +577,11 @@ var DEFAULT_STREAMING_CONFIG = {
577
577
  activityTimeoutMs: 3e4,
578
578
  pollingIntervalMs: 2e3
579
579
  };
580
+ var DEFAULT_POLLING_ENABLED = true;
580
581
  var ExecutionStreamControllerImpl = class {
581
582
  options;
582
583
  config;
584
+ pollingEnabled;
583
585
  sseClient = null;
584
586
  status = "connecting";
585
587
  reconnectAttempts = 0;
@@ -613,6 +615,7 @@ var ExecutionStreamControllerImpl = class {
613
615
  constructor(options) {
614
616
  this.options = options;
615
617
  this.config = { ...DEFAULT_STREAMING_CONFIG, ...options.config };
618
+ this.pollingEnabled = options.pollingEnabled ?? DEFAULT_POLLING_ENABLED;
616
619
  }
617
620
  /**
618
621
  * Get the last event ID (for resume support)
@@ -953,15 +956,61 @@ var ExecutionStreamControllerImpl = class {
953
956
  if (this.status === "closed") {
954
957
  return;
955
958
  }
959
+ await this.recoverState();
960
+ if (this.status === "closed") {
961
+ return;
962
+ }
956
963
  try {
957
964
  await this.connectSSE();
958
965
  } catch {
959
966
  await this.handleConnectionError(error);
960
967
  }
961
968
  } else {
962
- this.status = "fallback_polling";
963
- this.emit("connectionChange", { connected: false, usingFallback: true });
964
- this.startPolling();
969
+ if (this.pollingEnabled) {
970
+ this.status = "fallback_polling";
971
+ this.emit("connectionChange", { connected: false, usingFallback: true });
972
+ this.startPolling();
973
+ } else {
974
+ this.handleError({
975
+ error: "SSE connection failed and polling is disabled",
976
+ errorCode: "connection_failed"
977
+ });
978
+ }
979
+ }
980
+ }
981
+ /**
982
+ * Recover state by checking execution status.
983
+ *
984
+ * Called during reconnection to catch any events missed while SSE was down.
985
+ * If the execution completed while disconnected, emits completion immediately.
986
+ * Otherwise, emits any new logs that weren't seen via SSE.
987
+ */
988
+ async recoverState() {
989
+ try {
990
+ const execution = await this.options.getExecution(this.options.executionId);
991
+ if (execution.logs) {
992
+ for (const log of execution.logs) {
993
+ if (!this.seenEventIds.has(log.uuid)) {
994
+ this.seenEventIds.add(log.uuid);
995
+ this.emit("log", log);
996
+ }
997
+ }
998
+ }
999
+ if (execution.status === "completed") {
1000
+ this.stopPolling();
1001
+ this.handleComplete(execution);
1002
+ return;
1003
+ }
1004
+ if (execution.status === "failed" || execution.status === "cancelled") {
1005
+ this.stopPolling();
1006
+ this.handleError({
1007
+ error: execution.error ?? `Execution ${execution.status}`,
1008
+ errorCode: execution.status
1009
+ });
1010
+ return;
1011
+ }
1012
+ } catch (err) {
1013
+ console.warn("[SDK:StreamController] State recovery failed (non-fatal):", err);
965
1014
  }
966
1015
  }
967
1016
  /**