@cadraos/sdk 0.3.14-p46.4 → 0.3.14

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 +299 -17
  15. package/dist/chat/index.cjs.map +1 -1
  16. package/dist/chat/index.d.cts +208 -0
  17. package/dist/chat/index.d.ts +208 -0
  18. package/dist/chat/index.js +299 -17
  19. package/dist/chat/index.js.map +1 -1
  20. package/dist/index.cjs +221 -3
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +269 -1
  23. package/dist/index.d.ts +269 -1
  24. package/dist/index.js +215 -4
  25. package/dist/index.js.map +1 -1
  26. package/dist/kanban/index.cjs +214 -3
  27. package/dist/kanban/index.cjs.map +1 -1
  28. package/dist/kanban/index.d.cts +208 -0
  29. package/dist/kanban/index.d.ts +208 -0
  30. package/dist/kanban/index.js +214 -3
  31. package/dist/kanban/index.js.map +1 -1
  32. package/dist/teams/index.cjs +214 -3
  33. package/dist/teams/index.cjs.map +1 -1
  34. package/dist/teams/index.d.cts +208 -0
  35. package/dist/teams/index.d.ts +208 -0
  36. package/dist/teams/index.js +214 -3
  37. package/dist/teams/index.js.map +1 -1
  38. package/package.json +21 -1
package/dist/index.js CHANGED
@@ -61,6 +61,14 @@ var CadraError = class extends Error {
61
61
  details;
62
62
  /** Request ID from server (for debugging) */
63
63
  requestId;
64
+ /**
65
+ * The server's own `error.code` exactly as sent on the wire (e.g.
66
+ * `FUNCTION_NOT_PUBLISHED`, `DUPLICATE_REQUEST`, `VARIABLE_TYPE`).
67
+ * Populated by `parseApiError` for EVERY status. `code` keeps its historic
68
+ * per-class values (`conflict`, `server_error`, `validation_error`, …) for
69
+ * compatibility; branch on `serverCode` when you need the precise reason.
70
+ */
71
+ serverCode;
64
72
  constructor(message, code, status, details, requestId) {
65
73
  super(message);
66
74
  this.name = "CadraError";
@@ -138,6 +146,12 @@ function parseApiError(status, body, requestId) {
138
146
  const message = errorBody?.error?.message ?? "An error occurred";
139
147
  const code = errorBody?.error?.code ?? "unknown_error";
140
148
  const details = errorBody?.error?.details;
149
+ const serverCode = typeof errorBody?.error?.code === "string" ? errorBody.error.code : void 0;
150
+ const error = buildApiError(status, message, code, details, requestId);
151
+ if (serverCode !== void 0) error.serverCode = serverCode;
152
+ return error;
153
+ }
154
+ function buildApiError(status, message, code, details, requestId) {
141
155
  switch (status) {
142
156
  case 400:
143
157
  case 422: {
@@ -173,6 +187,42 @@ function parseApiError(status, body, requestId) {
173
187
  }
174
188
  }
175
189
 
190
+ // src/errors/ai-function-errors.ts
191
+ var AiFunctionDuplicateError = class extends ConflictError {
192
+ /** The existing run's execution id. */
193
+ executionId;
194
+ /** The existing run's status (`running`, a terminal status, or `abandoned`). */
195
+ runStatus;
196
+ constructor(message, details, requestId) {
197
+ super(message, details, requestId);
198
+ this.name = "AiFunctionDuplicateError";
199
+ this.serverCode = "DUPLICATE_REQUEST";
200
+ this.executionId = typeof details?.executionId === "string" ? details.executionId : void 0;
201
+ this.runStatus = typeof details?.status === "string" ? details.status : void 0;
202
+ }
203
+ };
204
+ var AiFunctionChargedError = class extends CadraError {
205
+ constructor(source) {
206
+ super(source.message, source.code, source.status, source.details, source.requestId);
207
+ this.name = "AiFunctionChargedError";
208
+ this.serverCode = source.serverCode;
209
+ }
210
+ };
211
+ var AiFunctionParameterError = class extends ValidationError {
212
+ functionKey;
213
+ issues;
214
+ constructor(functionKey, issues) {
215
+ super(
216
+ `Invalid parameters for AI function "${functionKey}": ` + issues.map((i) => `${i.name} (${i.code})`).join(", "),
217
+ issues.map((i) => ({ field: i.name, message: i.code })),
218
+ { functionKey, issues }
219
+ );
220
+ this.name = "AiFunctionParameterError";
221
+ this.functionKey = functionKey;
222
+ this.issues = issues;
223
+ }
224
+ };
225
+
176
226
  // src/utils/retry.ts
177
227
  var DEFAULT_RETRY_CONFIG = {
178
228
  maxRetries: 3,
@@ -291,7 +341,8 @@ var HttpClient = class {
291
341
  headers["Idempotency-Key"] = options.idempotencyKey;
292
342
  }
293
343
  const controller = new AbortController();
294
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
344
+ const timeoutMs = options.timeoutMs ?? this.config.timeout;
345
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
295
346
  try {
296
347
  const doFetch = this.config.fetch ?? fetch;
297
348
  const response = await doFetch(url, {
@@ -327,7 +378,7 @@ var HttpClient = class {
327
378
  } catch (error) {
328
379
  clearTimeout(timeoutId);
329
380
  if (error instanceof Error && error.name === "AbortError") {
330
- throw new TimeoutError(`Request timed out after ${this.config.timeout}ms`);
381
+ throw new TimeoutError(`Request timed out after ${options.timeoutMs ?? this.config.timeout}ms`);
331
382
  }
332
383
  if (error instanceof TypeError && error.message.includes("fetch")) {
333
384
  throw new NetworkError(`Network error: ${error.message}`);
@@ -359,7 +410,8 @@ var HttpClient = class {
359
410
  path,
360
411
  body,
361
412
  idempotencyKey: options?.idempotencyKey,
362
- skipRetry: options?.skipRetry
413
+ skipRetry: options?.skipRetry,
414
+ timeoutMs: options?.timeoutMs
363
415
  });
364
416
  }
365
417
  /**
@@ -1118,6 +1170,10 @@ var ExecutionStreamControllerImpl = class {
1118
1170
  toolName,
1119
1171
  result: data.result,
1120
1172
  durationMs: data.durationMs ?? 0,
1173
+ // CAD-243: pass the producer's failure verdict through.
1174
+ ...data.isError === true ? { isError: true } : {},
1175
+ ...typeof data.status === "string" ? { status: data.status } : {},
1176
+ ...typeof data.error === "string" ? { error: data.error } : {},
1121
1177
  forwardedFrom: event.data._forwardedFrom,
1122
1178
  // CAD-87 S1-001 EventIdentity → (execId, seq) reducer dedup (S1-003).
1123
1179
  sourceExecutionId: data.sourceExecutionId ?? data.executionId,
@@ -3356,6 +3412,156 @@ var RemoteRunnersAPI = class extends BaseResourceAPI {
3356
3412
  }
3357
3413
  };
3358
3414
 
3415
+ // src/resources/ai-functions.ts
3416
+ var AI_FUNCTION_RUN_TIMEOUT_MS = 6e4;
3417
+ var AI_FUNCTION_DESCRIPTOR_TTL_MS = 6e4;
3418
+ function checkAiFunctionParameters(parameters, variables) {
3419
+ const issues = [];
3420
+ const declared = /* @__PURE__ */ new Set();
3421
+ for (const p of parameters) {
3422
+ declared.add(p.name);
3423
+ const has = Object.prototype.hasOwnProperty.call(variables, p.name);
3424
+ const value = has ? variables[p.name] : void 0;
3425
+ if (value === void 0 || value === null) {
3426
+ if (p.required) issues.push({ name: p.name, code: "missing" });
3427
+ continue;
3428
+ }
3429
+ switch (p.type) {
3430
+ case "number":
3431
+ if (typeof value !== "number" || !Number.isFinite(value)) {
3432
+ issues.push({ name: p.name, code: "type" });
3433
+ }
3434
+ break;
3435
+ case "boolean":
3436
+ if (typeof value !== "boolean") issues.push({ name: p.name, code: "type" });
3437
+ break;
3438
+ case "enum":
3439
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3440
+ else if (p.options && !p.options.includes(value)) {
3441
+ issues.push({ name: p.name, code: "option" });
3442
+ }
3443
+ break;
3444
+ case "string":
3445
+ case "text":
3446
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3447
+ else if (typeof p.maxLength === "number" && value.length > p.maxLength) {
3448
+ issues.push({ name: p.name, code: "too_long" });
3449
+ }
3450
+ break;
3451
+ case "image":
3452
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3453
+ break;
3454
+ }
3455
+ }
3456
+ for (const name of Object.keys(variables)) {
3457
+ if (!declared.has(name)) issues.push({ name, code: "unknown" });
3458
+ }
3459
+ return issues;
3460
+ }
3461
+ var AiFunctionsAPI = class extends BaseResourceAPI {
3462
+ descriptorCache = /* @__PURE__ */ new Map();
3463
+ constructor(http) {
3464
+ super(http, "/api/v1/ai-functions");
3465
+ }
3466
+ /**
3467
+ * List the functions this org can call — the index (D27a). Each entry carries
3468
+ * the live version's typed parameters; never prompt text.
3469
+ */
3470
+ async list() {
3471
+ const data = await this.httpGet("");
3472
+ const functions = Array.isArray(data) ? data : data?.functions ?? [];
3473
+ for (const d of functions) this.cacheDescriptor(d);
3474
+ return functions;
3475
+ }
3476
+ /**
3477
+ * Get one function's descriptor by key (D27a).
3478
+ * `NotFoundError` (`FUNCTION_NOT_FOUND`) / `ConflictError` (`FUNCTION_NOT_PUBLISHED`).
3479
+ */
3480
+ async get(functionKey) {
3481
+ const data = await this.httpGet(
3482
+ `/${encodeURIComponent(functionKey)}`
3483
+ );
3484
+ const descriptor = data && typeof data === "object" && "function" in data && !("functionKey" in data) ? data.function : data;
3485
+ this.cacheDescriptor(descriptor, functionKey);
3486
+ return descriptor;
3487
+ }
3488
+ /**
3489
+ * Read one of this org's own runs (safe projection, §6.5).
3490
+ */
3491
+ async getRun(executionId) {
3492
+ return this.httpGet(`/runs/${encodeURIComponent(executionId)}`);
3493
+ }
3494
+ /** Forget cached descriptors (one key, or all). */
3495
+ clearDescriptorCache(functionKey) {
3496
+ if (functionKey) this.descriptorCache.delete(functionKey);
3497
+ else this.descriptorCache.clear();
3498
+ }
3499
+ async run(refOrKey, parametersOrOptions, shorthandOpts) {
3500
+ let ref;
3501
+ let options;
3502
+ if (typeof refOrKey === "string") {
3503
+ ref = { functionKey: refOrKey };
3504
+ options = { ...shorthandOpts ?? {}, variables: parametersOrOptions };
3505
+ } else {
3506
+ ref = refOrKey;
3507
+ options = parametersOrOptions;
3508
+ }
3509
+ const variables = options.variables ?? {};
3510
+ const functionKey = "functionKey" in ref ? ref.functionKey : void 0;
3511
+ const shouldCheck = functionKey !== void 0 && options.version === void 0 && options.validate !== false;
3512
+ if (shouldCheck) {
3513
+ const descriptor = await this.getCachedDescriptor(functionKey);
3514
+ const issues = checkAiFunctionParameters(descriptor.parameters, variables);
3515
+ if (issues.length > 0) {
3516
+ throw new AiFunctionParameterError(functionKey, issues);
3517
+ }
3518
+ }
3519
+ const body = {
3520
+ ref,
3521
+ variables,
3522
+ version: options.version,
3523
+ idempotencyKey: options.idempotencyKey ?? generateExecutionId(),
3524
+ params: options.params
3525
+ };
3526
+ try {
3527
+ return await this.httpPost("/run", body, {
3528
+ skipRetry: true,
3529
+ timeoutMs: AI_FUNCTION_RUN_TIMEOUT_MS
3530
+ });
3531
+ } catch (error) {
3532
+ throw this.mapRunError(error, functionKey);
3533
+ }
3534
+ }
3535
+ // ── internals ──────────────────────────────────────────────────────────────
3536
+ cacheDescriptor(descriptor, key) {
3537
+ if (!descriptor || typeof descriptor !== "object") return;
3538
+ const cacheKey = key ?? descriptor.functionKey;
3539
+ if (!cacheKey) return;
3540
+ this.descriptorCache.set(cacheKey, {
3541
+ descriptor,
3542
+ expiresAt: Date.now() + AI_FUNCTION_DESCRIPTOR_TTL_MS
3543
+ });
3544
+ }
3545
+ async getCachedDescriptor(functionKey) {
3546
+ const hit = this.descriptorCache.get(functionKey);
3547
+ if (hit && hit.expiresAt > Date.now()) return hit.descriptor;
3548
+ return this.get(functionKey);
3549
+ }
3550
+ mapRunError(error, functionKey) {
3551
+ if (!(error instanceof CadraError)) return error;
3552
+ if (error.serverCode === "DUPLICATE_REQUEST") {
3553
+ return new AiFunctionDuplicateError(error.message, error.details, error.requestId);
3554
+ }
3555
+ if (error.details?.charged === true) {
3556
+ return new AiFunctionChargedError(error);
3557
+ }
3558
+ if (functionKey && error.serverCode?.startsWith("VARIABLE_")) {
3559
+ this.descriptorCache.delete(functionKey);
3560
+ }
3561
+ return error;
3562
+ }
3563
+ };
3564
+
3359
3565
  // src/client.ts
3360
3566
  var DEFAULT_BASE_URL = "https://api.cadraos.com";
3361
3567
  var DEFAULT_TIMEOUT = 3e4;
@@ -3402,6 +3608,10 @@ var CadraClient = class {
3402
3608
  * Remote runner operations (list)
3403
3609
  */
3404
3610
  remoteRunners;
3611
+ /**
3612
+ * AI function operations (list, get, run, getRun) — p93
3613
+ */
3614
+ aiFunctions;
3405
3615
  /**
3406
3616
  * Create a new AI SaaS client
3407
3617
  *
@@ -3491,6 +3701,7 @@ var CadraClient = class {
3491
3701
  this.kanban = new KanbanAPI(this.httpClient, getStreamConfig);
3492
3702
  this.remoteAgents = new RemoteAgentsAPI(this.httpClient, getStreamConfig);
3493
3703
  this.remoteRunners = new RemoteRunnersAPI(this.httpClient);
3704
+ this.aiFunctions = new AiFunctionsAPI(this.httpClient);
3494
3705
  }
3495
3706
  /**
3496
3707
  * Validate client options
@@ -3513,6 +3724,6 @@ var CadraClient = class {
3513
3724
  }
3514
3725
  };
3515
3726
 
3516
- export { CadraClient as AISaaSClient, CadraError as AISaaSError, AuthenticationError, AuthorizationError, CadraClient, CadraError, ConflictError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError, collectImagesFromStream, deriveImagesFromToolCalls, extractImagesFromExecutionOutput, extractImagesFromResult };
3727
+ export { CadraClient as AISaaSClient, CadraError as AISaaSError, AI_FUNCTION_DESCRIPTOR_TTL_MS, AI_FUNCTION_RUN_TIMEOUT_MS, AiFunctionChargedError, AiFunctionDuplicateError, AiFunctionParameterError, AiFunctionsAPI, AuthenticationError, AuthorizationError, CadraClient, CadraError, ConflictError, NetworkError, NotFoundError, RateLimitError, ServerError, TimeoutError, ValidationError, checkAiFunctionParameters, collectImagesFromStream, deriveImagesFromToolCalls, extractImagesFromExecutionOutput, extractImagesFromResult };
3517
3728
  //# sourceMappingURL=index.js.map
3518
3729
  //# sourceMappingURL=index.js.map