@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.d.cts CHANGED
@@ -41,6 +41,8 @@ interface HttpClientConfig {
41
41
  retry: RetryConfig;
42
42
  /** Custom headers */
43
43
  headers?: Record<string, string>;
44
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
45
+ fetch?: typeof fetch;
44
46
  }
45
47
  /**
46
48
  * Request options
@@ -986,6 +988,8 @@ interface RemoteAgentCreateInput {
986
988
  name: string;
987
989
  description?: string;
988
990
  adapterType: string;
991
+ /** Bind the agent to a specific runner (e.g. a Cadra Desktop install). */
992
+ defaultRunnerId?: number | null;
989
993
  executionTargetKind?: RemoteAgentExecutionTargetKind;
990
994
  adapterConfig?: Record<string, unknown>;
991
995
  inputSchema?: Record<string, unknown>;
@@ -996,6 +1000,8 @@ interface RemoteAgentUpdateInput {
996
1000
  name?: string;
997
1001
  description?: string;
998
1002
  adapterType?: string;
1003
+ /** Re-bind the agent to a specific runner. */
1004
+ defaultRunnerId?: number | null;
999
1005
  executionTargetKind?: RemoteAgentExecutionTargetKind;
1000
1006
  adapterConfig?: Record<string, unknown>;
1001
1007
  inputSchema?: Record<string, unknown>;
@@ -1541,6 +1547,8 @@ interface ExecutionStreamControllerOptions {
1541
1547
  orgId: number;
1542
1548
  /** Function to get execution status (for polling fallback) */
1543
1549
  getExecution: (executionId: string) => Promise<AgentExecution>;
1550
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
1551
+ fetch?: typeof fetch;
1544
1552
  /** Streaming configuration */
1545
1553
  config?: Partial<StreamingConfig>;
1546
1554
  /** Whether polling fallback is enabled (default: true) */
@@ -1880,6 +1888,8 @@ interface StreamConfig {
1880
1888
  token?: string;
1881
1889
  tokenProvider?: () => Promise<string>;
1882
1890
  orgId: number;
1891
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
1892
+ fetch?: typeof fetch;
1883
1893
  }
1884
1894
 
1885
1895
  /**
@@ -3207,6 +3217,8 @@ interface CadraClientOptionsApiKey {
3207
3217
  maxRetries?: number;
3208
3218
  /** Custom headers to include in all requests */
3209
3219
  headers?: Record<string, string>;
3220
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3221
+ fetch?: typeof fetch;
3210
3222
  }
3211
3223
  /**
3212
3224
  * Client options for OAuth token authentication
@@ -3224,6 +3236,8 @@ interface CadraClientOptionsToken {
3224
3236
  maxRetries?: number;
3225
3237
  /** Custom headers to include in all requests */
3226
3238
  headers?: Record<string, string>;
3239
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3240
+ fetch?: typeof fetch;
3227
3241
  }
3228
3242
  /**
3229
3243
  * Client options for token provider authentication
@@ -3241,6 +3255,8 @@ interface CadraClientOptionsTokenProvider {
3241
3255
  maxRetries?: number;
3242
3256
  /** Custom headers to include in all requests */
3243
3257
  headers?: Record<string, string>;
3258
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3259
+ fetch?: typeof fetch;
3244
3260
  }
3245
3261
  /**
3246
3262
  * All client options
package/dist/index.d.ts CHANGED
@@ -41,6 +41,8 @@ interface HttpClientConfig {
41
41
  retry: RetryConfig;
42
42
  /** Custom headers */
43
43
  headers?: Record<string, string>;
44
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
45
+ fetch?: typeof fetch;
44
46
  }
45
47
  /**
46
48
  * Request options
@@ -986,6 +988,8 @@ interface RemoteAgentCreateInput {
986
988
  name: string;
987
989
  description?: string;
988
990
  adapterType: string;
991
+ /** Bind the agent to a specific runner (e.g. a Cadra Desktop install). */
992
+ defaultRunnerId?: number | null;
989
993
  executionTargetKind?: RemoteAgentExecutionTargetKind;
990
994
  adapterConfig?: Record<string, unknown>;
991
995
  inputSchema?: Record<string, unknown>;
@@ -996,6 +1000,8 @@ interface RemoteAgentUpdateInput {
996
1000
  name?: string;
997
1001
  description?: string;
998
1002
  adapterType?: string;
1003
+ /** Re-bind the agent to a specific runner. */
1004
+ defaultRunnerId?: number | null;
999
1005
  executionTargetKind?: RemoteAgentExecutionTargetKind;
1000
1006
  adapterConfig?: Record<string, unknown>;
1001
1007
  inputSchema?: Record<string, unknown>;
@@ -1541,6 +1547,8 @@ interface ExecutionStreamControllerOptions {
1541
1547
  orgId: number;
1542
1548
  /** Function to get execution status (for polling fallback) */
1543
1549
  getExecution: (executionId: string) => Promise<AgentExecution>;
1550
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
1551
+ fetch?: typeof fetch;
1544
1552
  /** Streaming configuration */
1545
1553
  config?: Partial<StreamingConfig>;
1546
1554
  /** Whether polling fallback is enabled (default: true) */
@@ -1880,6 +1888,8 @@ interface StreamConfig {
1880
1888
  token?: string;
1881
1889
  tokenProvider?: () => Promise<string>;
1882
1890
  orgId: number;
1891
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
1892
+ fetch?: typeof fetch;
1883
1893
  }
1884
1894
 
1885
1895
  /**
@@ -3207,6 +3217,8 @@ interface CadraClientOptionsApiKey {
3207
3217
  maxRetries?: number;
3208
3218
  /** Custom headers to include in all requests */
3209
3219
  headers?: Record<string, string>;
3220
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3221
+ fetch?: typeof fetch;
3210
3222
  }
3211
3223
  /**
3212
3224
  * Client options for OAuth token authentication
@@ -3224,6 +3236,8 @@ interface CadraClientOptionsToken {
3224
3236
  maxRetries?: number;
3225
3237
  /** Custom headers to include in all requests */
3226
3238
  headers?: Record<string, string>;
3239
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3240
+ fetch?: typeof fetch;
3227
3241
  }
3228
3242
  /**
3229
3243
  * Client options for token provider authentication
@@ -3241,6 +3255,8 @@ interface CadraClientOptionsTokenProvider {
3241
3255
  maxRetries?: number;
3242
3256
  /** Custom headers to include in all requests */
3243
3257
  headers?: Record<string, string>;
3258
+ /** Optional fetch implementation — inject `expo/fetch` for React Native streaming. Defaults to global fetch. */
3259
+ fetch?: typeof fetch;
3244
3260
  }
3245
3261
  /**
3246
3262
  * All client options
package/dist/index.js CHANGED
@@ -293,7 +293,8 @@ var HttpClient = class {
293
293
  const controller = new AbortController();
294
294
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
295
295
  try {
296
- const response = await fetch(url, {
296
+ const doFetch = this.config.fetch ?? fetch;
297
+ const response = await doFetch(url, {
297
298
  method: options.method,
298
299
  headers,
299
300
  body: options.body !== void 0 ? JSON.stringify(options.body) : void 0,
@@ -425,7 +426,8 @@ var SSEClient = class {
425
426
  console.log("[SDK:SSE] Auth header:", this.config.authHeader ? `${this.config.authHeader.substring(0, 30)}...` : "MISSING");
426
427
  console.log("[SDK:SSE] Org ID:", this.config.orgId);
427
428
  try {
428
- const response = await fetch(this.config.url, {
429
+ const doFetch = this.config.fetch ?? fetch;
430
+ const response = await doFetch(this.config.url, {
429
431
  method: "GET",
430
432
  headers: {
431
433
  "Authorization": this.config.authHeader,
@@ -673,6 +675,15 @@ var ExecutionStreamControllerImpl = class {
673
675
  sseClient = null;
674
676
  status = "connecting";
675
677
  reconnectAttempts = 0;
678
+ // CAD-39 / webchat dual-plane create-connect race. `hasConnected` flips true on
679
+ // the first successful SSE open; `notFoundRetries` bounds retries of a 404
680
+ // encountered BEFORE we ever connect (the run row is persisted slightly AFTER
681
+ // the client opens the stream — the norm on the webchat path where msg-api owns
682
+ // the executionId and cadra persists the agent_executions row via an async
683
+ // worker callback). See handleConnectionError.
684
+ hasConnected = false;
685
+ notFoundRetries = 0;
686
+ maxConnectRaceRetries = 3;
676
687
  seenEventIds = /* @__PURE__ */ new Set();
677
688
  _lastEventId = null;
678
689
  activityTimeoutId = null;
@@ -801,6 +812,7 @@ var ExecutionStreamControllerImpl = class {
801
812
  url,
802
813
  authHeader: `Bearer ${authToken}`,
803
814
  orgId: this.options.orgId,
815
+ fetch: this.options.fetch,
804
816
  onEvent: (event) => this.handleEvent(event),
805
817
  onError: (error) => {
806
818
  console.error("[SDK:StreamController] SSE error:", error);
@@ -811,6 +823,8 @@ var ExecutionStreamControllerImpl = class {
811
823
  if (connected) {
812
824
  this.status = "connected";
813
825
  this.reconnectAttempts = 0;
826
+ this.hasConnected = true;
827
+ this.notFoundRetries = 0;
814
828
  this.resetActivityTimeout();
815
829
  }
816
830
  this.emit("connectionChange", { connected, usingFallback: false });
@@ -1100,6 +1114,38 @@ var ExecutionStreamControllerImpl = class {
1100
1114
  return;
1101
1115
  }
1102
1116
  const msg = error instanceof Error ? error.message : String(error);
1117
+ const isNotFound = /\b404\b/.test(msg);
1118
+ const isAuthFailure = /\b40[13]\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg);
1119
+ if (isNotFound && !isAuthFailure && !this.hasConnected) {
1120
+ if (this.notFoundRetries < this.maxConnectRaceRetries) {
1121
+ this.notFoundRetries++;
1122
+ this.status = "reconnecting";
1123
+ const delay = Math.min(
1124
+ this.config.reconnectBaseDelayMs * this.notFoundRetries,
1125
+ this.config.reconnectMaxDelayMs
1126
+ );
1127
+ await new Promise((resolve) => setTimeout(resolve, delay));
1128
+ if (this.status === "closed") {
1129
+ return;
1130
+ }
1131
+ try {
1132
+ await this.connectSSE();
1133
+ } catch (retryError) {
1134
+ await this.handleConnectionError(retryError);
1135
+ }
1136
+ return;
1137
+ }
1138
+ if (this.pollingEnabled) {
1139
+ if (this.sseClient) {
1140
+ this.sseClient.disconnect();
1141
+ this.sseClient = null;
1142
+ }
1143
+ this.status = "fallback_polling";
1144
+ this.emit("connectionChange", { connected: false, usingFallback: true });
1145
+ this.startPolling();
1146
+ return;
1147
+ }
1148
+ }
1103
1149
  if (/\b4\d\d\b/.test(msg) || /INVALID_API_KEY|INVALID_REQUEST/.test(msg)) {
1104
1150
  if (this.sseClient) {
1105
1151
  this.sseClient.disconnect();
@@ -1478,6 +1524,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1478
1524
  apiKey: config.apiKey,
1479
1525
  tokenProvider: config.tokenProvider,
1480
1526
  orgId: config.orgId,
1527
+ fetch: config.fetch,
1481
1528
  getExecution: (id) => this.getExecution(id)
1482
1529
  });
1483
1530
  console.log("[SDK:Agents] Stream controller created - caller must call connect() after registering listeners");
@@ -1524,6 +1571,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1524
1571
  apiKey: config.apiKey,
1525
1572
  tokenProvider: config.tokenProvider,
1526
1573
  orgId: config.orgId,
1574
+ fetch: config.fetch,
1527
1575
  getExecution: (id) => this.getExecution(id)
1528
1576
  });
1529
1577
  console.log("[SDK:Agents] Stream controller created for existing execution - caller must call connect() after registering listeners");
@@ -2372,6 +2420,7 @@ var SessionsAPI = class extends BaseResourceAPI {
2372
2420
  baseUrl: config.baseUrl,
2373
2421
  apiKey: config.apiKey,
2374
2422
  orgId: config.orgId,
2423
+ fetch: config.fetch,
2375
2424
  getExecution: (id) => this.getExecution(id)
2376
2425
  });
2377
2426
  await controller.connect();
@@ -3026,7 +3075,8 @@ var CadraClient = class {
3026
3075
  baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
3027
3076
  orgId: options.orgId,
3028
3077
  timeout: options.timeout ?? DEFAULT_TIMEOUT,
3029
- headers: options.headers
3078
+ headers: options.headers,
3079
+ fetch: options.fetch
3030
3080
  };
3031
3081
  if ("apiKey" in options) {
3032
3082
  this.config.apiKey = options.apiKey;
@@ -3047,7 +3097,8 @@ var CadraClient = class {
3047
3097
  orgId: this.config.orgId,
3048
3098
  timeout: this.config.timeout,
3049
3099
  retry: retryConfig,
3050
- headers: this.config.headers
3100
+ headers: this.config.headers,
3101
+ fetch: this.config.fetch
3051
3102
  };
3052
3103
  this.httpClient = new HttpClient(httpConfig);
3053
3104
  const getStreamConfig = () => ({
@@ -3055,7 +3106,8 @@ var CadraClient = class {
3055
3106
  apiKey: this.config.apiKey,
3056
3107
  token: this.config.token,
3057
3108
  tokenProvider: this.config.tokenProvider,
3058
- orgId: this.config.orgId
3109
+ orgId: this.config.orgId,
3110
+ fetch: this.config.fetch
3059
3111
  });
3060
3112
  this.agents = new AgentsAPI(this.httpClient, getStreamConfig);
3061
3113
  this.skills = new SkillsAPI(this.httpClient);