@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
@@ -86,6 +86,14 @@ var CadraError = class extends Error {
86
86
  details;
87
87
  /** Request ID from server (for debugging) */
88
88
  requestId;
89
+ /**
90
+ * The server's own `error.code` exactly as sent on the wire (e.g.
91
+ * `FUNCTION_NOT_PUBLISHED`, `DUPLICATE_REQUEST`, `VARIABLE_TYPE`).
92
+ * Populated by `parseApiError` for EVERY status. `code` keeps its historic
93
+ * per-class values (`conflict`, `server_error`, `validation_error`, …) for
94
+ * compatibility; branch on `serverCode` when you need the precise reason.
95
+ */
96
+ serverCode;
89
97
  constructor(message, code, status, details, requestId) {
90
98
  super(message);
91
99
  this.name = "CadraError";
@@ -163,6 +171,12 @@ function parseApiError(status, body, requestId) {
163
171
  const message = errorBody?.error?.message ?? "An error occurred";
164
172
  const code = errorBody?.error?.code ?? "unknown_error";
165
173
  const details = errorBody?.error?.details;
174
+ const serverCode = typeof errorBody?.error?.code === "string" ? errorBody.error.code : void 0;
175
+ const error = buildApiError(status, message, code, details, requestId);
176
+ if (serverCode !== void 0) error.serverCode = serverCode;
177
+ return error;
178
+ }
179
+ function buildApiError(status, message, code, details, requestId) {
166
180
  switch (status) {
167
181
  case 400:
168
182
  case 422: {
@@ -198,6 +212,42 @@ function parseApiError(status, body, requestId) {
198
212
  }
199
213
  }
200
214
 
215
+ // src/errors/ai-function-errors.ts
216
+ var AiFunctionDuplicateError = class extends ConflictError {
217
+ /** The existing run's execution id. */
218
+ executionId;
219
+ /** The existing run's status (`running`, a terminal status, or `abandoned`). */
220
+ runStatus;
221
+ constructor(message, details, requestId) {
222
+ super(message, details, requestId);
223
+ this.name = "AiFunctionDuplicateError";
224
+ this.serverCode = "DUPLICATE_REQUEST";
225
+ this.executionId = typeof details?.executionId === "string" ? details.executionId : void 0;
226
+ this.runStatus = typeof details?.status === "string" ? details.status : void 0;
227
+ }
228
+ };
229
+ var AiFunctionChargedError = class extends CadraError {
230
+ constructor(source) {
231
+ super(source.message, source.code, source.status, source.details, source.requestId);
232
+ this.name = "AiFunctionChargedError";
233
+ this.serverCode = source.serverCode;
234
+ }
235
+ };
236
+ var AiFunctionParameterError = class extends ValidationError {
237
+ functionKey;
238
+ issues;
239
+ constructor(functionKey, issues) {
240
+ super(
241
+ `Invalid parameters for AI function "${functionKey}": ` + issues.map((i) => `${i.name} (${i.code})`).join(", "),
242
+ issues.map((i) => ({ field: i.name, message: i.code })),
243
+ { functionKey, issues }
244
+ );
245
+ this.name = "AiFunctionParameterError";
246
+ this.functionKey = functionKey;
247
+ this.issues = issues;
248
+ }
249
+ };
250
+
201
251
  // src/utils/retry.ts
202
252
  var DEFAULT_RETRY_CONFIG = {
203
253
  maxRetries: 3,
@@ -262,6 +312,10 @@ var HttpClient = class {
262
312
  constructor(config) {
263
313
  this.config = config;
264
314
  }
315
+ /** The client-wide request timeout (ms) this client was configured with. */
316
+ get timeoutMs() {
317
+ return this.config.timeout;
318
+ }
265
319
  /**
266
320
  * Get authorization header value
267
321
  */
@@ -316,7 +370,8 @@ var HttpClient = class {
316
370
  headers["Idempotency-Key"] = options.idempotencyKey;
317
371
  }
318
372
  const controller = new AbortController();
319
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
373
+ const timeoutMs = options.timeoutMs ?? this.config.timeout;
374
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
320
375
  try {
321
376
  const doFetch = this.config.fetch ?? fetch;
322
377
  const response = await doFetch(url, {
@@ -352,7 +407,7 @@ var HttpClient = class {
352
407
  } catch (error) {
353
408
  clearTimeout(timeoutId);
354
409
  if (error instanceof Error && error.name === "AbortError") {
355
- throw new TimeoutError(`Request timed out after ${this.config.timeout}ms`);
410
+ throw new TimeoutError(`Request timed out after ${options.timeoutMs ?? this.config.timeout}ms`);
356
411
  }
357
412
  if (error instanceof TypeError && error.message.includes("fetch")) {
358
413
  throw new NetworkError(`Network error: ${error.message}`);
@@ -384,7 +439,8 @@ var HttpClient = class {
384
439
  path,
385
440
  body,
386
441
  idempotencyKey: options?.idempotencyKey,
387
- skipRetry: options?.skipRetry
442
+ skipRetry: options?.skipRetry,
443
+ timeoutMs: options?.timeoutMs
388
444
  });
389
445
  }
390
446
  /**
@@ -1133,6 +1189,10 @@ var ExecutionStreamControllerImpl = class {
1133
1189
  toolName,
1134
1190
  result: data.result,
1135
1191
  durationMs: data.durationMs ?? 0,
1192
+ // CAD-243: pass the producer's failure verdict through.
1193
+ ...data.isError === true ? { isError: true } : {},
1194
+ ...typeof data.status === "string" ? { status: data.status } : {},
1195
+ ...typeof data.error === "string" ? { error: data.error } : {},
1136
1196
  forwardedFrom: event.data._forwardedFrom,
1137
1197
  // CAD-87 S1-001 EventIdentity → (execId, seq) reducer dedup (S1-003).
1138
1198
  sourceExecutionId: data.sourceExecutionId ?? data.executionId,
@@ -3371,6 +3431,156 @@ var RemoteRunnersAPI = class extends BaseResourceAPI {
3371
3431
  }
3372
3432
  };
3373
3433
 
3434
+ // src/resources/ai-functions.ts
3435
+ var AI_FUNCTION_RUN_TIMEOUT_MS = 31e4;
3436
+ var AI_FUNCTION_DESCRIPTOR_TTL_MS = 6e4;
3437
+ function checkAiFunctionParameters(parameters, variables) {
3438
+ const issues = [];
3439
+ const declared = /* @__PURE__ */ new Set();
3440
+ for (const p of parameters) {
3441
+ declared.add(p.name);
3442
+ const has = Object.prototype.hasOwnProperty.call(variables, p.name);
3443
+ const value = has ? variables[p.name] : void 0;
3444
+ if (value === void 0 || value === null) {
3445
+ if (p.required) issues.push({ name: p.name, code: "missing" });
3446
+ continue;
3447
+ }
3448
+ switch (p.type) {
3449
+ case "number":
3450
+ if (typeof value !== "number" || !Number.isFinite(value)) {
3451
+ issues.push({ name: p.name, code: "type" });
3452
+ }
3453
+ break;
3454
+ case "boolean":
3455
+ if (typeof value !== "boolean") issues.push({ name: p.name, code: "type" });
3456
+ break;
3457
+ case "enum":
3458
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3459
+ else if (p.options && !p.options.includes(value)) {
3460
+ issues.push({ name: p.name, code: "option" });
3461
+ }
3462
+ break;
3463
+ case "string":
3464
+ case "text":
3465
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3466
+ else if (typeof p.maxLength === "number" && value.length > p.maxLength) {
3467
+ issues.push({ name: p.name, code: "too_long" });
3468
+ }
3469
+ break;
3470
+ case "image":
3471
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
3472
+ break;
3473
+ }
3474
+ }
3475
+ for (const name of Object.keys(variables)) {
3476
+ if (!declared.has(name)) issues.push({ name, code: "unknown" });
3477
+ }
3478
+ return issues;
3479
+ }
3480
+ var AiFunctionsAPI = class extends BaseResourceAPI {
3481
+ descriptorCache = /* @__PURE__ */ new Map();
3482
+ constructor(http) {
3483
+ super(http, "/api/v1/ai-functions");
3484
+ }
3485
+ /**
3486
+ * List the functions this org can call — the index (D27a). Each entry carries
3487
+ * the live version's typed parameters; never prompt text.
3488
+ */
3489
+ async list() {
3490
+ const data = await this.httpGet("");
3491
+ const functions = Array.isArray(data) ? data : data?.functions ?? [];
3492
+ for (const d of functions) this.cacheDescriptor(d);
3493
+ return functions;
3494
+ }
3495
+ /**
3496
+ * Get one function's descriptor by key (D27a).
3497
+ * `NotFoundError` (`FUNCTION_NOT_FOUND`) / `ConflictError` (`FUNCTION_NOT_PUBLISHED`).
3498
+ */
3499
+ async get(functionKey) {
3500
+ const data = await this.httpGet(
3501
+ `/${encodeURIComponent(functionKey)}`
3502
+ );
3503
+ const descriptor = data && typeof data === "object" && "function" in data && !("functionKey" in data) ? data.function : data;
3504
+ this.cacheDescriptor(descriptor, functionKey);
3505
+ return descriptor;
3506
+ }
3507
+ /**
3508
+ * Read one of this org's own runs (safe projection, §6.5).
3509
+ */
3510
+ async getRun(executionId) {
3511
+ return this.httpGet(`/runs/${encodeURIComponent(executionId)}`);
3512
+ }
3513
+ /** Forget cached descriptors (one key, or all). */
3514
+ clearDescriptorCache(functionKey) {
3515
+ if (functionKey) this.descriptorCache.delete(functionKey);
3516
+ else this.descriptorCache.clear();
3517
+ }
3518
+ async run(refOrKey, parametersOrOptions, shorthandOpts) {
3519
+ let ref;
3520
+ let options;
3521
+ if (typeof refOrKey === "string") {
3522
+ ref = { functionKey: refOrKey };
3523
+ options = { ...shorthandOpts ?? {}, variables: parametersOrOptions };
3524
+ } else {
3525
+ ref = refOrKey;
3526
+ options = parametersOrOptions;
3527
+ }
3528
+ const variables = options.variables ?? {};
3529
+ const functionKey = "functionKey" in ref ? ref.functionKey : void 0;
3530
+ const shouldCheck = functionKey !== void 0 && options.version === void 0 && options.validate !== false;
3531
+ if (shouldCheck) {
3532
+ const descriptor = await this.getCachedDescriptor(functionKey);
3533
+ const issues = checkAiFunctionParameters(descriptor.parameters, variables);
3534
+ if (issues.length > 0) {
3535
+ throw new AiFunctionParameterError(functionKey, issues);
3536
+ }
3537
+ }
3538
+ const body = {
3539
+ ref,
3540
+ variables,
3541
+ version: options.version,
3542
+ idempotencyKey: options.idempotencyKey ?? generateExecutionId(),
3543
+ params: options.params
3544
+ };
3545
+ try {
3546
+ return await this.httpPost("/run", body, {
3547
+ skipRetry: true,
3548
+ timeoutMs: Math.max(AI_FUNCTION_RUN_TIMEOUT_MS, this.http.timeoutMs ?? 0)
3549
+ });
3550
+ } catch (error) {
3551
+ throw this.mapRunError(error, functionKey);
3552
+ }
3553
+ }
3554
+ // ── internals ──────────────────────────────────────────────────────────────
3555
+ cacheDescriptor(descriptor, key) {
3556
+ if (!descriptor || typeof descriptor !== "object") return;
3557
+ const cacheKey = key ?? descriptor.functionKey;
3558
+ if (!cacheKey) return;
3559
+ this.descriptorCache.set(cacheKey, {
3560
+ descriptor,
3561
+ expiresAt: Date.now() + AI_FUNCTION_DESCRIPTOR_TTL_MS
3562
+ });
3563
+ }
3564
+ async getCachedDescriptor(functionKey) {
3565
+ const hit = this.descriptorCache.get(functionKey);
3566
+ if (hit && hit.expiresAt > Date.now()) return hit.descriptor;
3567
+ return this.get(functionKey);
3568
+ }
3569
+ mapRunError(error, functionKey) {
3570
+ if (!(error instanceof CadraError)) return error;
3571
+ if (error.serverCode === "DUPLICATE_REQUEST") {
3572
+ return new AiFunctionDuplicateError(error.message, error.details, error.requestId);
3573
+ }
3574
+ if (error.details?.charged === true) {
3575
+ return new AiFunctionChargedError(error);
3576
+ }
3577
+ if (functionKey && error.serverCode?.startsWith("VARIABLE_")) {
3578
+ this.descriptorCache.delete(functionKey);
3579
+ }
3580
+ return error;
3581
+ }
3582
+ };
3583
+
3374
3584
  // src/client.ts
3375
3585
  var DEFAULT_BASE_URL = "https://api.cadraos.com";
3376
3586
  var DEFAULT_TIMEOUT = 3e4;
@@ -3417,6 +3627,10 @@ var CadraClient = class {
3417
3627
  * Remote runner operations (list)
3418
3628
  */
3419
3629
  remoteRunners;
3630
+ /**
3631
+ * AI function operations (list, get, run, getRun) — p93
3632
+ */
3633
+ aiFunctions;
3420
3634
  /**
3421
3635
  * Create a new AI SaaS client
3422
3636
  *
@@ -3506,6 +3720,7 @@ var CadraClient = class {
3506
3720
  this.kanban = new KanbanAPI(this.httpClient, getStreamConfig);
3507
3721
  this.remoteAgents = new RemoteAgentsAPI(this.httpClient, getStreamConfig);
3508
3722
  this.remoteRunners = new RemoteRunnersAPI(this.httpClient);
3723
+ this.aiFunctions = new AiFunctionsAPI(this.httpClient);
3509
3724
  }
3510
3725
  /**
3511
3726
  * Validate client options