@cadraos/sdk 0.1.10 → 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.js CHANGED
@@ -52,7 +52,7 @@ var BaseResourceAPI = class {
52
52
  };
53
53
 
54
54
  // src/errors/sdk-errors.ts
55
- var AISaaSError = class extends Error {
55
+ var CadraError = class extends Error {
56
56
  /** Error code */
57
57
  code;
58
58
  /** HTTP status code */
@@ -63,7 +63,7 @@ var AISaaSError = class extends Error {
63
63
  requestId;
64
64
  constructor(message, code, status, details, requestId) {
65
65
  super(message);
66
- this.name = "AISaaSError";
66
+ this.name = "CadraError";
67
67
  this.code = code;
68
68
  this.status = status;
69
69
  this.details = details;
@@ -73,25 +73,25 @@ var AISaaSError = class extends Error {
73
73
  }
74
74
  }
75
75
  };
76
- var AuthenticationError = class extends AISaaSError {
76
+ var AuthenticationError = class extends CadraError {
77
77
  constructor(message = "Authentication failed", code = "authentication_error", details, requestId) {
78
78
  super(message, code, 401, details, requestId);
79
79
  this.name = "AuthenticationError";
80
80
  }
81
81
  };
82
- var AuthorizationError = class extends AISaaSError {
82
+ var AuthorizationError = class extends CadraError {
83
83
  constructor(message = "Access denied", code = "authorization_error", details, requestId) {
84
84
  super(message, code, 403, details, requestId);
85
85
  this.name = "AuthorizationError";
86
86
  }
87
87
  };
88
- var NotFoundError = class extends AISaaSError {
88
+ var NotFoundError = class extends CadraError {
89
89
  constructor(message = "Resource not found", code = "not_found", details, requestId) {
90
90
  super(message, code, 404, details, requestId);
91
91
  this.name = "NotFoundError";
92
92
  }
93
93
  };
94
- var ValidationError = class extends AISaaSError {
94
+ var ValidationError = class extends CadraError {
95
95
  /** Field-level validation errors */
96
96
  errors;
97
97
  constructor(message = "Validation failed", errors = [], details, requestId) {
@@ -100,7 +100,7 @@ var ValidationError = class extends AISaaSError {
100
100
  this.errors = errors;
101
101
  }
102
102
  };
103
- var RateLimitError = class extends AISaaSError {
103
+ var RateLimitError = class extends CadraError {
104
104
  /** Seconds until rate limit resets */
105
105
  retryAfter;
106
106
  constructor(message = "Rate limit exceeded", retryAfter, details, requestId) {
@@ -109,25 +109,25 @@ var RateLimitError = class extends AISaaSError {
109
109
  this.retryAfter = retryAfter;
110
110
  }
111
111
  };
112
- var ServerError = class extends AISaaSError {
112
+ var ServerError = class extends CadraError {
113
113
  constructor(message = "Server error", status = 500, details, requestId) {
114
114
  super(message, "server_error", status, details, requestId);
115
115
  this.name = "ServerError";
116
116
  }
117
117
  };
118
- var ConflictError = class extends AISaaSError {
118
+ var ConflictError = class extends CadraError {
119
119
  constructor(message = "Resource conflict", details, requestId) {
120
120
  super(message, "conflict", 409, details, requestId);
121
121
  this.name = "ConflictError";
122
122
  }
123
123
  };
124
- var TimeoutError = class extends AISaaSError {
124
+ var TimeoutError = class extends CadraError {
125
125
  constructor(message = "Request timed out", details, requestId) {
126
126
  super(message, "timeout", 408, details, requestId);
127
127
  this.name = "TimeoutError";
128
128
  }
129
129
  };
130
- var NetworkError = class extends AISaaSError {
130
+ var NetworkError = class extends CadraError {
131
131
  constructor(message = "Network error", details) {
132
132
  super(message, "network_error", 0, details);
133
133
  this.name = "NetworkError";
@@ -169,7 +169,7 @@ function parseApiError(status, body, requestId) {
169
169
  if (status >= 500) {
170
170
  return new ServerError(message, status, details, requestId);
171
171
  }
172
- return new AISaaSError(message, code, status, details, requestId);
172
+ return new CadraError(message, code, status, details, requestId);
173
173
  }
174
174
  }
175
175
 
@@ -257,15 +257,21 @@ var HttpClient = class {
257
257
  * Build full URL with query parameters
258
258
  */
259
259
  buildUrl(path, query) {
260
- const url = new URL(path, this.config.baseUrl);
260
+ const base = this.config.baseUrl.replace(/\/+$/, "");
261
+ let url = `${base}${path}`;
261
262
  if (query) {
263
+ const params = new URLSearchParams();
262
264
  for (const [key, value] of Object.entries(query)) {
263
265
  if (value !== void 0) {
264
- url.searchParams.set(key, String(value));
266
+ params.set(key, String(value));
265
267
  }
266
268
  }
269
+ const qs = params.toString();
270
+ if (qs) {
271
+ url += `?${qs}`;
272
+ }
267
273
  }
268
- return url.toString();
274
+ return url;
269
275
  }
270
276
  /**
271
277
  * Make an HTTP request
@@ -569,9 +575,11 @@ var DEFAULT_STREAMING_CONFIG = {
569
575
  activityTimeoutMs: 3e4,
570
576
  pollingIntervalMs: 2e3
571
577
  };
578
+ var DEFAULT_POLLING_ENABLED = true;
572
579
  var ExecutionStreamControllerImpl = class {
573
580
  options;
574
581
  config;
582
+ pollingEnabled;
575
583
  sseClient = null;
576
584
  status = "connecting";
577
585
  reconnectAttempts = 0;
@@ -605,6 +613,7 @@ var ExecutionStreamControllerImpl = class {
605
613
  constructor(options) {
606
614
  this.options = options;
607
615
  this.config = { ...DEFAULT_STREAMING_CONFIG, ...options.config };
616
+ this.pollingEnabled = options.pollingEnabled ?? DEFAULT_POLLING_ENABLED;
608
617
  }
609
618
  /**
610
619
  * Get the last event ID (for resume support)
@@ -665,7 +674,7 @@ var ExecutionStreamControllerImpl = class {
665
674
  return Promise.resolve(this.finalExecution);
666
675
  }
667
676
  if (this.finalError) {
668
- return Promise.reject(new AISaaSError(
677
+ return Promise.reject(new CadraError(
669
678
  this.finalError.error,
670
679
  this.finalError.errorCode ?? "execution_error",
671
680
  500
@@ -684,7 +693,8 @@ var ExecutionStreamControllerImpl = class {
684
693
  * Connect to SSE endpoint
685
694
  */
686
695
  async connectSSE() {
687
- const url = `${this.options.baseUrl}/agents/runs/${this.options.executionId}/stream`;
696
+ const base = this.options.baseUrl.replace(/\/+$/, "");
697
+ const url = `${base}/api/v1/agents/runs/${this.options.executionId}/stream`;
688
698
  let authToken = this.options.apiKey;
689
699
  if (!authToken && this.options.tokenProvider) {
690
700
  authToken = await this.options.tokenProvider();
@@ -919,7 +929,7 @@ var ExecutionStreamControllerImpl = class {
919
929
  this.finalError = error;
920
930
  this.emit("error", error);
921
931
  if (this.waitReject) {
922
- this.waitReject(new AISaaSError(
932
+ this.waitReject(new CadraError(
923
933
  error.error,
924
934
  error.errorCode ?? "execution_error",
925
935
  500
@@ -944,15 +954,61 @@ var ExecutionStreamControllerImpl = class {
944
954
  if (this.status === "closed") {
945
955
  return;
946
956
  }
957
+ await this.recoverState();
958
+ if (this.status === "closed") {
959
+ return;
960
+ }
947
961
  try {
948
962
  await this.connectSSE();
949
963
  } catch {
950
964
  await this.handleConnectionError(error);
951
965
  }
952
966
  } else {
953
- this.status = "fallback_polling";
954
- this.emit("connectionChange", { connected: false, usingFallback: true });
955
- this.startPolling();
967
+ if (this.pollingEnabled) {
968
+ this.status = "fallback_polling";
969
+ this.emit("connectionChange", { connected: false, usingFallback: true });
970
+ this.startPolling();
971
+ } else {
972
+ this.handleError({
973
+ error: "SSE connection failed and polling is disabled",
974
+ errorCode: "connection_failed"
975
+ });
976
+ }
977
+ }
978
+ }
979
+ /**
980
+ * Recover state by checking execution status.
981
+ *
982
+ * Called during reconnection to catch any events missed while SSE was down.
983
+ * If the execution completed while disconnected, emits completion immediately.
984
+ * Otherwise, emits any new logs that weren't seen via SSE.
985
+ */
986
+ async recoverState() {
987
+ try {
988
+ const execution = await this.options.getExecution(this.options.executionId);
989
+ if (execution.logs) {
990
+ for (const log of execution.logs) {
991
+ if (!this.seenEventIds.has(log.uuid)) {
992
+ this.seenEventIds.add(log.uuid);
993
+ this.emit("log", log);
994
+ }
995
+ }
996
+ }
997
+ if (execution.status === "completed") {
998
+ this.stopPolling();
999
+ this.handleComplete(execution);
1000
+ return;
1001
+ }
1002
+ if (execution.status === "failed" || execution.status === "cancelled") {
1003
+ this.stopPolling();
1004
+ this.handleError({
1005
+ error: execution.error ?? `Execution ${execution.status}`,
1006
+ errorCode: execution.status
1007
+ });
1008
+ return;
1009
+ }
1010
+ } catch (err) {
1011
+ console.warn("[SDK:StreamController] State recovery failed (non-fatal):", err);
956
1012
  }
957
1013
  }
958
1014
  /**
@@ -1037,7 +1093,7 @@ function createExecutionStreamController(options) {
1037
1093
  // src/resources/agents.ts
1038
1094
  var AgentsAPI = class extends BaseResourceAPI {
1039
1095
  constructor(http, getStreamConfig) {
1040
- super(http, "/agents");
1096
+ super(http, "/api/v1/agents");
1041
1097
  this.getStreamConfig = getStreamConfig;
1042
1098
  }
1043
1099
  // ===== LIST Operations =====
@@ -1313,7 +1369,7 @@ var AgentsAPI = class extends BaseResourceAPI {
1313
1369
  // src/resources/skills.ts
1314
1370
  var SkillsAPI = class extends BaseResourceAPI {
1315
1371
  constructor(http) {
1316
- super(http, "/skills");
1372
+ super(http, "/api/v1/skills");
1317
1373
  }
1318
1374
  // ===== LIST Operations =====
1319
1375
  /**
@@ -1457,7 +1513,7 @@ var SkillsAPI = class extends BaseResourceAPI {
1457
1513
  // src/resources/tools.ts
1458
1514
  var ToolsAPI = class extends BaseResourceAPI {
1459
1515
  constructor(http) {
1460
- super(http, "/tools");
1516
+ super(http, "/api/v1/tools");
1461
1517
  }
1462
1518
  // ===== LIST Operations =====
1463
1519
  /**
@@ -1620,7 +1676,7 @@ var ToolsAPI = class extends BaseResourceAPI {
1620
1676
  // src/resources/artifacts.ts
1621
1677
  var ArtifactsAPI = class extends BaseResourceAPI {
1622
1678
  constructor(http) {
1623
- super(http, "/artifacts");
1679
+ super(http, "/api/v1/artifacts");
1624
1680
  }
1625
1681
  // ===== LIST Operations =====
1626
1682
  /**
@@ -1816,7 +1872,7 @@ var ArtifactsAPI = class extends BaseResourceAPI {
1816
1872
  // src/resources/sessions.ts
1817
1873
  var SessionsAPI = class extends BaseResourceAPI {
1818
1874
  constructor(http, getStreamConfig) {
1819
- super(http, "/sessions");
1875
+ super(http, "/api/v1/sessions");
1820
1876
  this.getStreamConfig = getStreamConfig;
1821
1877
  }
1822
1878
  // ===== LIST Operations =====
@@ -2047,9 +2103,9 @@ var SessionsAPI = class extends BaseResourceAPI {
2047
2103
  };
2048
2104
 
2049
2105
  // src/client.ts
2050
- var DEFAULT_BASE_URL = "https://api.aisaas.com/v1";
2106
+ var DEFAULT_BASE_URL = "https://api.cadraos.com";
2051
2107
  var DEFAULT_TIMEOUT = 3e4;
2052
- var AISaaSClient = class {
2108
+ var CadraClient = class {
2053
2109
  httpClient;
2054
2110
  config;
2055
2111
  /**
@@ -2080,19 +2136,19 @@ var AISaaSClient = class {
2080
2136
  * @example
2081
2137
  * ```typescript
2082
2138
  * // API key authentication
2083
- * const client = new AISaaSClient({
2139
+ * const client = new CadraClient({
2084
2140
  * apiKey: 'sk_...',
2085
2141
  * orgId: 123,
2086
2142
  * });
2087
2143
  *
2088
2144
  * // OAuth token authentication
2089
- * const client = new AISaaSClient({
2145
+ * const client = new CadraClient({
2090
2146
  * token: 'eyJ...',
2091
2147
  * orgId: 123,
2092
2148
  * });
2093
2149
  *
2094
2150
  * // Token provider (for automatic refresh)
2095
- * const client = new AISaaSClient({
2151
+ * const client = new CadraClient({
2096
2152
  * tokenProvider: async () => myAuth.getAccessToken(),
2097
2153
  * orgId: 123,
2098
2154
  * });
@@ -2100,6 +2156,19 @@ var AISaaSClient = class {
2100
2156
  */
2101
2157
  constructor(options) {
2102
2158
  this.validateOptions(options);
2159
+ if (options.baseUrl) {
2160
+ try {
2161
+ const parsed = new URL(options.baseUrl);
2162
+ if (/\/(api|v\d)/.test(parsed.pathname)) {
2163
+ if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
2164
+ console.warn(
2165
+ `[CadraOS SDK] baseUrl should be the origin only (e.g., "https://api.cadraos.com" or "http://localhost:3000"). The SDK automatically prepends "/api/v1/" to all requests. Received: "${options.baseUrl}"`
2166
+ );
2167
+ }
2168
+ }
2169
+ } catch {
2170
+ }
2171
+ }
2103
2172
  this.config = {
2104
2173
  baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
2105
2174
  orgId: options.orgId,
@@ -2162,6 +2231,6 @@ var AISaaSClient = class {
2162
2231
  }
2163
2232
  };
2164
2233
 
2165
- export { AISaaSClient, AISaaSError, AuthenticationError, AuthorizationError, ConflictError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError };
2234
+ export { CadraClient as AISaaSClient, CadraError as AISaaSError, AuthenticationError, AuthorizationError, CadraClient, CadraError, ConflictError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError };
2166
2235
  //# sourceMappingURL=index.js.map
2167
2236
  //# sourceMappingURL=index.js.map