@cadraos/sdk 0.3.14-p46.4 → 0.3.15

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.
Files changed (38) hide show
  1. package/README.md +104 -53
  2. package/dist/ai-functions/react/index.cjs +176 -0
  3. package/dist/ai-functions/react/index.cjs.map +1 -0
  4. package/dist/ai-functions/react/index.d.cts +129 -0
  5. package/dist/ai-functions/react/index.d.ts +129 -0
  6. package/dist/ai-functions/react/index.js +153 -0
  7. package/dist/ai-functions/react/index.js.map +1 -0
  8. package/dist/ai-functions/server/index.cjs +296 -0
  9. package/dist/ai-functions/server/index.cjs.map +1 -0
  10. package/dist/ai-functions/server/index.d.cts +334 -0
  11. package/dist/ai-functions/server/index.d.ts +334 -0
  12. package/dist/ai-functions/server/index.js +284 -0
  13. package/dist/ai-functions/server/index.js.map +1 -0
  14. package/dist/chat/index.cjs +303 -17
  15. package/dist/chat/index.cjs.map +1 -1
  16. package/dist/chat/index.d.cts +211 -0
  17. package/dist/chat/index.d.ts +211 -0
  18. package/dist/chat/index.js +303 -17
  19. package/dist/chat/index.js.map +1 -1
  20. package/dist/index.cjs +225 -3
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +279 -1
  23. package/dist/index.d.ts +279 -1
  24. package/dist/index.js +219 -4
  25. package/dist/index.js.map +1 -1
  26. package/dist/kanban/index.cjs +218 -3
  27. package/dist/kanban/index.cjs.map +1 -1
  28. package/dist/kanban/index.d.cts +211 -0
  29. package/dist/kanban/index.d.ts +211 -0
  30. package/dist/kanban/index.js +218 -3
  31. package/dist/kanban/index.js.map +1 -1
  32. package/dist/teams/index.cjs +218 -3
  33. package/dist/teams/index.cjs.map +1 -1
  34. package/dist/teams/index.d.cts +211 -0
  35. package/dist/teams/index.d.ts +211 -0
  36. package/dist/teams/index.js +218 -3
  37. package/dist/teams/index.js.map +1 -1
  38. package/package.json +21 -1
package/dist/index.cjs CHANGED
@@ -63,6 +63,14 @@ var CadraError = class extends Error {
63
63
  details;
64
64
  /** Request ID from server (for debugging) */
65
65
  requestId;
66
+ /**
67
+ * The server's own `error.code` exactly as sent on the wire (e.g.
68
+ * `FUNCTION_NOT_PUBLISHED`, `DUPLICATE_REQUEST`, `VARIABLE_TYPE`).
69
+ * Populated by `parseApiError` for EVERY status. `code` keeps its historic
70
+ * per-class values (`conflict`, `server_error`, `validation_error`, …) for
71
+ * compatibility; branch on `serverCode` when you need the precise reason.
72
+ */
73
+ serverCode;
66
74
  constructor(message, code, status, details, requestId) {
67
75
  super(message);
68
76
  this.name = "CadraError";
@@ -140,6 +148,12 @@ function parseApiError(status, body, requestId) {
140
148
  const message = errorBody?.error?.message ?? "An error occurred";
141
149
  const code = errorBody?.error?.code ?? "unknown_error";
142
150
  const details = errorBody?.error?.details;
151
+ const serverCode = typeof errorBody?.error?.code === "string" ? errorBody.error.code : void 0;
152
+ const error = buildApiError(status, message, code, details, requestId);
153
+ if (serverCode !== void 0) error.serverCode = serverCode;
154
+ return error;
155
+ }
156
+ function buildApiError(status, message, code, details, requestId) {
143
157
  switch (status) {
144
158
  case 400:
145
159
  case 422: {
@@ -175,6 +189,42 @@ function parseApiError(status, body, requestId) {
175
189
  }
176
190
  }
177
191
 
192
+ // src/errors/ai-function-errors.ts
193
+ var AiFunctionDuplicateError = class extends ConflictError {
194
+ /** The existing run's execution id. */
195
+ executionId;
196
+ /** The existing run's status (`running`, a terminal status, or `abandoned`). */
197
+ runStatus;
198
+ constructor(message, details, requestId) {
199
+ super(message, details, requestId);
200
+ this.name = "AiFunctionDuplicateError";
201
+ this.serverCode = "DUPLICATE_REQUEST";
202
+ this.executionId = typeof details?.executionId === "string" ? details.executionId : void 0;
203
+ this.runStatus = typeof details?.status === "string" ? details.status : void 0;
204
+ }
205
+ };
206
+ var AiFunctionChargedError = class extends CadraError {
207
+ constructor(source) {
208
+ super(source.message, source.code, source.status, source.details, source.requestId);
209
+ this.name = "AiFunctionChargedError";
210
+ this.serverCode = source.serverCode;
211
+ }
212
+ };
213
+ var AiFunctionParameterError = class extends ValidationError {
214
+ functionKey;
215
+ issues;
216
+ constructor(functionKey, issues) {
217
+ super(
218
+ `Invalid parameters for AI function "${functionKey}": ` + issues.map((i) => `${i.name} (${i.code})`).join(", "),
219
+ issues.map((i) => ({ field: i.name, message: i.code })),
220
+ { functionKey, issues }
221
+ );
222
+ this.name = "AiFunctionParameterError";
223
+ this.functionKey = functionKey;
224
+ this.issues = issues;
225
+ }
226
+ };
227
+
178
228
  // src/utils/retry.ts
179
229
  var DEFAULT_RETRY_CONFIG = {
180
230
  maxRetries: 3,
@@ -239,6 +289,10 @@ var HttpClient = class {
239
289
  constructor(config) {
240
290
  this.config = config;
241
291
  }
292
+ /** The client-wide request timeout (ms) this client was configured with. */
293
+ get timeoutMs() {
294
+ return this.config.timeout;
295
+ }
242
296
  /**
243
297
  * Get authorization header value
244
298
  */
@@ -293,7 +347,8 @@ var HttpClient = class {
293
347
  headers["Idempotency-Key"] = options.idempotencyKey;
294
348
  }
295
349
  const controller = new AbortController();
296
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
350
+ const timeoutMs = options.timeoutMs ?? this.config.timeout;
351
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
297
352
  try {
298
353
  const doFetch = this.config.fetch ?? fetch;
299
354
  const response = await doFetch(url, {
@@ -329,7 +384,7 @@ var HttpClient = class {
329
384
  } catch (error) {
330
385
  clearTimeout(timeoutId);
331
386
  if (error instanceof Error && error.name === "AbortError") {
332
- throw new TimeoutError(`Request timed out after ${this.config.timeout}ms`);
387
+ throw new TimeoutError(`Request timed out after ${options.timeoutMs ?? this.config.timeout}ms`);
333
388
  }
334
389
  if (error instanceof TypeError && error.message.includes("fetch")) {
335
390
  throw new NetworkError(`Network error: ${error.message}`);
@@ -361,7 +416,8 @@ var HttpClient = class {
361
416
  path,
362
417
  body,
363
418
  idempotencyKey: options?.idempotencyKey,
364
- skipRetry: options?.skipRetry
419
+ skipRetry: options?.skipRetry,
420
+ timeoutMs: options?.timeoutMs
365
421
  });
366
422
  }
367
423
  /**
@@ -1120,6 +1176,10 @@ var ExecutionStreamControllerImpl = class {
1120
1176
  toolName,
1121
1177
  result: data.result,
1122
1178
  durationMs: data.durationMs ?? 0,
1179
+ // CAD-243: pass the producer's failure verdict through.
1180
+ ...data.isError === true ? { isError: true } : {},
1181
+ ...typeof data.status === "string" ? { status: data.status } : {},
1182
+ ...typeof data.error === "string" ? { error: data.error } : {},
1123
1183
  forwardedFrom: event.data._forwardedFrom,
1124
1184
  // CAD-87 S1-001 EventIdentity → (execId, seq) reducer dedup (S1-003).
1125
1185
  sourceExecutionId: data.sourceExecutionId ?? data.executionId,
@@ -3358,6 +3418,156 @@ var RemoteRunnersAPI = class extends BaseResourceAPI {
3358
3418
  }
3359
3419
  };
3360
3420
 
3421
+ // src/resources/ai-functions.ts
3422
+ var AI_FUNCTION_RUN_TIMEOUT_MS = 31e4;
3423
+ var AI_FUNCTION_DESCRIPTOR_TTL_MS = 6e4;
3424
+ function checkAiFunctionParameters(parameters, variables) {
3425
+ const issues = [];
3426
+ const declared = /* @__PURE__ */ new Set();
3427
+ for (const p of parameters) {
3428
+ declared.add(p.name);
3429
+ const has = Object.prototype.hasOwnProperty.call(variables, p.name);
3430
+ const value = has ? variables[p.name] : void 0;
3431
+ if (value === void 0 || value === null) {
3432
+ if (p.required) issues.push({ name: p.name, code: "missing" });
3433
+ continue;
3434
+ }
3435
+ switch (p.type) {
3436
+ case "number":
3437
+ if (typeof value !== "number" || !Number.isFinite(value)) {
3438
+ issues.push({ name: p.name, code: "type" });
3439
+ }
3440
+ break;
3441
+ case "boolean":
3442
+ if (typeof value !== "boolean") issues.push({ name: p.name, code: "type" });
3443
+ break;
3444
+ case "enum":
3445
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3446
+ else if (p.options && !p.options.includes(value)) {
3447
+ issues.push({ name: p.name, code: "option" });
3448
+ }
3449
+ break;
3450
+ case "string":
3451
+ case "text":
3452
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3453
+ else if (typeof p.maxLength === "number" && value.length > p.maxLength) {
3454
+ issues.push({ name: p.name, code: "too_long" });
3455
+ }
3456
+ break;
3457
+ case "image":
3458
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3459
+ break;
3460
+ }
3461
+ }
3462
+ for (const name of Object.keys(variables)) {
3463
+ if (!declared.has(name)) issues.push({ name, code: "unknown" });
3464
+ }
3465
+ return issues;
3466
+ }
3467
+ var AiFunctionsAPI = class extends BaseResourceAPI {
3468
+ descriptorCache = /* @__PURE__ */ new Map();
3469
+ constructor(http) {
3470
+ super(http, "/api/v1/ai-functions");
3471
+ }
3472
+ /**
3473
+ * List the functions this org can call — the index (D27a). Each entry carries
3474
+ * the live version's typed parameters; never prompt text.
3475
+ */
3476
+ async list() {
3477
+ const data = await this.httpGet("");
3478
+ const functions = Array.isArray(data) ? data : data?.functions ?? [];
3479
+ for (const d of functions) this.cacheDescriptor(d);
3480
+ return functions;
3481
+ }
3482
+ /**
3483
+ * Get one function's descriptor by key (D27a).
3484
+ * `NotFoundError` (`FUNCTION_NOT_FOUND`) / `ConflictError` (`FUNCTION_NOT_PUBLISHED`).
3485
+ */
3486
+ async get(functionKey) {
3487
+ const data = await this.httpGet(
3488
+ `/${encodeURIComponent(functionKey)}`
3489
+ );
3490
+ const descriptor = data && typeof data === "object" && "function" in data && !("functionKey" in data) ? data.function : data;
3491
+ this.cacheDescriptor(descriptor, functionKey);
3492
+ return descriptor;
3493
+ }
3494
+ /**
3495
+ * Read one of this org's own runs (safe projection, §6.5).
3496
+ */
3497
+ async getRun(executionId) {
3498
+ return this.httpGet(`/runs/${encodeURIComponent(executionId)}`);
3499
+ }
3500
+ /** Forget cached descriptors (one key, or all). */
3501
+ clearDescriptorCache(functionKey) {
3502
+ if (functionKey) this.descriptorCache.delete(functionKey);
3503
+ else this.descriptorCache.clear();
3504
+ }
3505
+ async run(refOrKey, parametersOrOptions, shorthandOpts) {
3506
+ let ref;
3507
+ let options;
3508
+ if (typeof refOrKey === "string") {
3509
+ ref = { functionKey: refOrKey };
3510
+ options = { ...shorthandOpts ?? {}, variables: parametersOrOptions };
3511
+ } else {
3512
+ ref = refOrKey;
3513
+ options = parametersOrOptions;
3514
+ }
3515
+ const variables = options.variables ?? {};
3516
+ const functionKey = "functionKey" in ref ? ref.functionKey : void 0;
3517
+ const shouldCheck = functionKey !== void 0 && options.version === void 0 && options.validate !== false;
3518
+ if (shouldCheck) {
3519
+ const descriptor = await this.getCachedDescriptor(functionKey);
3520
+ const issues = checkAiFunctionParameters(descriptor.parameters, variables);
3521
+ if (issues.length > 0) {
3522
+ throw new AiFunctionParameterError(functionKey, issues);
3523
+ }
3524
+ }
3525
+ const body = {
3526
+ ref,
3527
+ variables,
3528
+ version: options.version,
3529
+ idempotencyKey: options.idempotencyKey ?? generateExecutionId(),
3530
+ params: options.params
3531
+ };
3532
+ try {
3533
+ return await this.httpPost("/run", body, {
3534
+ skipRetry: true,
3535
+ timeoutMs: Math.max(AI_FUNCTION_RUN_TIMEOUT_MS, this.http.timeoutMs ?? 0)
3536
+ });
3537
+ } catch (error) {
3538
+ throw this.mapRunError(error, functionKey);
3539
+ }
3540
+ }
3541
+ // ── internals ──────────────────────────────────────────────────────────────
3542
+ cacheDescriptor(descriptor, key) {
3543
+ if (!descriptor || typeof descriptor !== "object") return;
3544
+ const cacheKey = key ?? descriptor.functionKey;
3545
+ if (!cacheKey) return;
3546
+ this.descriptorCache.set(cacheKey, {
3547
+ descriptor,
3548
+ expiresAt: Date.now() + AI_FUNCTION_DESCRIPTOR_TTL_MS
3549
+ });
3550
+ }
3551
+ async getCachedDescriptor(functionKey) {
3552
+ const hit = this.descriptorCache.get(functionKey);
3553
+ if (hit && hit.expiresAt > Date.now()) return hit.descriptor;
3554
+ return this.get(functionKey);
3555
+ }
3556
+ mapRunError(error, functionKey) {
3557
+ if (!(error instanceof CadraError)) return error;
3558
+ if (error.serverCode === "DUPLICATE_REQUEST") {
3559
+ return new AiFunctionDuplicateError(error.message, error.details, error.requestId);
3560
+ }
3561
+ if (error.details?.charged === true) {
3562
+ return new AiFunctionChargedError(error);
3563
+ }
3564
+ if (functionKey && error.serverCode?.startsWith("VARIABLE_")) {
3565
+ this.descriptorCache.delete(functionKey);
3566
+ }
3567
+ return error;
3568
+ }
3569
+ };
3570
+
3361
3571
  // src/client.ts
3362
3572
  var DEFAULT_BASE_URL = "https://api.cadraos.com";
3363
3573
  var DEFAULT_TIMEOUT = 3e4;
@@ -3404,6 +3614,10 @@ var CadraClient = class {
3404
3614
  * Remote runner operations (list)
3405
3615
  */
3406
3616
  remoteRunners;
3617
+ /**
3618
+ * AI function operations (list, get, run, getRun) — p93
3619
+ */
3620
+ aiFunctions;
3407
3621
  /**
3408
3622
  * Create a new AI SaaS client
3409
3623
  *
@@ -3493,6 +3707,7 @@ var CadraClient = class {
3493
3707
  this.kanban = new KanbanAPI(this.httpClient, getStreamConfig);
3494
3708
  this.remoteAgents = new RemoteAgentsAPI(this.httpClient, getStreamConfig);
3495
3709
  this.remoteRunners = new RemoteRunnersAPI(this.httpClient);
3710
+ this.aiFunctions = new AiFunctionsAPI(this.httpClient);
3496
3711
  }
3497
3712
  /**
3498
3713
  * Validate client options
@@ -3517,6 +3732,12 @@ var CadraClient = class {
3517
3732
 
3518
3733
  exports.AISaaSClient = CadraClient;
3519
3734
  exports.AISaaSError = CadraError;
3735
+ exports.AI_FUNCTION_DESCRIPTOR_TTL_MS = AI_FUNCTION_DESCRIPTOR_TTL_MS;
3736
+ exports.AI_FUNCTION_RUN_TIMEOUT_MS = AI_FUNCTION_RUN_TIMEOUT_MS;
3737
+ exports.AiFunctionChargedError = AiFunctionChargedError;
3738
+ exports.AiFunctionDuplicateError = AiFunctionDuplicateError;
3739
+ exports.AiFunctionParameterError = AiFunctionParameterError;
3740
+ exports.AiFunctionsAPI = AiFunctionsAPI;
3520
3741
  exports.AuthenticationError = AuthenticationError;
3521
3742
  exports.AuthorizationError = AuthorizationError;
3522
3743
  exports.CadraClient = CadraClient;
@@ -3528,6 +3749,7 @@ exports.RateLimitError = RateLimitError;
3528
3749
  exports.ServerError = ServerError;
3529
3750
  exports.TimeoutError = TimeoutError;
3530
3751
  exports.ValidationError = ValidationError;
3752
+ exports.checkAiFunctionParameters = checkAiFunctionParameters;
3531
3753
  exports.collectImagesFromStream = collectImagesFromStream;
3532
3754
  exports.deriveImagesFromToolCalls = deriveImagesFromToolCalls;
3533
3755
  exports.extractImagesFromExecutionOutput = extractImagesFromExecutionOutput;