@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
@@ -3672,6 +3672,14 @@ var CadraError = class extends Error {
3672
3672
  details;
3673
3673
  /** Request ID from server (for debugging) */
3674
3674
  requestId;
3675
+ /**
3676
+ * The server's own `error.code` exactly as sent on the wire (e.g.
3677
+ * `FUNCTION_NOT_PUBLISHED`, `DUPLICATE_REQUEST`, `VARIABLE_TYPE`).
3678
+ * Populated by `parseApiError` for EVERY status. `code` keeps its historic
3679
+ * per-class values (`conflict`, `server_error`, `validation_error`, …) for
3680
+ * compatibility; branch on `serverCode` when you need the precise reason.
3681
+ */
3682
+ serverCode;
3675
3683
  constructor(message, code, status, details, requestId) {
3676
3684
  super(message);
3677
3685
  this.name = "CadraError";
@@ -3749,6 +3757,12 @@ function parseApiError(status, body, requestId) {
3749
3757
  const message = errorBody?.error?.message ?? "An error occurred";
3750
3758
  const code = errorBody?.error?.code ?? "unknown_error";
3751
3759
  const details = errorBody?.error?.details;
3760
+ const serverCode = typeof errorBody?.error?.code === "string" ? errorBody.error.code : void 0;
3761
+ const error = buildApiError(status, message, code, details, requestId);
3762
+ if (serverCode !== void 0) error.serverCode = serverCode;
3763
+ return error;
3764
+ }
3765
+ function buildApiError(status, message, code, details, requestId) {
3752
3766
  switch (status) {
3753
3767
  case 400:
3754
3768
  case 422: {
@@ -3784,6 +3798,42 @@ function parseApiError(status, body, requestId) {
3784
3798
  }
3785
3799
  }
3786
3800
 
3801
+ // src/errors/ai-function-errors.ts
3802
+ var AiFunctionDuplicateError = class extends ConflictError {
3803
+ /** The existing run's execution id. */
3804
+ executionId;
3805
+ /** The existing run's status (`running`, a terminal status, or `abandoned`). */
3806
+ runStatus;
3807
+ constructor(message, details, requestId) {
3808
+ super(message, details, requestId);
3809
+ this.name = "AiFunctionDuplicateError";
3810
+ this.serverCode = "DUPLICATE_REQUEST";
3811
+ this.executionId = typeof details?.executionId === "string" ? details.executionId : void 0;
3812
+ this.runStatus = typeof details?.status === "string" ? details.status : void 0;
3813
+ }
3814
+ };
3815
+ var AiFunctionChargedError = class extends CadraError {
3816
+ constructor(source) {
3817
+ super(source.message, source.code, source.status, source.details, source.requestId);
3818
+ this.name = "AiFunctionChargedError";
3819
+ this.serverCode = source.serverCode;
3820
+ }
3821
+ };
3822
+ var AiFunctionParameterError = class extends ValidationError {
3823
+ functionKey;
3824
+ issues;
3825
+ constructor(functionKey, issues) {
3826
+ super(
3827
+ `Invalid parameters for AI function "${functionKey}": ` + issues.map((i) => `${i.name} (${i.code})`).join(", "),
3828
+ issues.map((i) => ({ field: i.name, message: i.code })),
3829
+ { functionKey, issues }
3830
+ );
3831
+ this.name = "AiFunctionParameterError";
3832
+ this.functionKey = functionKey;
3833
+ this.issues = issues;
3834
+ }
3835
+ };
3836
+
3787
3837
  // src/utils/retry.ts
3788
3838
  var DEFAULT_RETRY_CONFIG = {
3789
3839
  maxRetries: 3,
@@ -3848,6 +3898,10 @@ var HttpClient = class {
3848
3898
  constructor(config) {
3849
3899
  this.config = config;
3850
3900
  }
3901
+ /** The client-wide request timeout (ms) this client was configured with. */
3902
+ get timeoutMs() {
3903
+ return this.config.timeout;
3904
+ }
3851
3905
  /**
3852
3906
  * Get authorization header value
3853
3907
  */
@@ -3902,7 +3956,8 @@ var HttpClient = class {
3902
3956
  headers["Idempotency-Key"] = options.idempotencyKey;
3903
3957
  }
3904
3958
  const controller = new AbortController();
3905
- const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
3959
+ const timeoutMs = options.timeoutMs ?? this.config.timeout;
3960
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
3906
3961
  try {
3907
3962
  const doFetch = this.config.fetch ?? fetch;
3908
3963
  const response = await doFetch(url, {
@@ -3938,7 +3993,7 @@ var HttpClient = class {
3938
3993
  } catch (error) {
3939
3994
  clearTimeout(timeoutId);
3940
3995
  if (error instanceof Error && error.name === "AbortError") {
3941
- throw new TimeoutError(`Request timed out after ${this.config.timeout}ms`);
3996
+ throw new TimeoutError(`Request timed out after ${options.timeoutMs ?? this.config.timeout}ms`);
3942
3997
  }
3943
3998
  if (error instanceof TypeError && error.message.includes("fetch")) {
3944
3999
  throw new NetworkError(`Network error: ${error.message}`);
@@ -3970,7 +4025,8 @@ var HttpClient = class {
3970
4025
  path,
3971
4026
  body,
3972
4027
  idempotencyKey: options?.idempotencyKey,
3973
- skipRetry: options?.skipRetry
4028
+ skipRetry: options?.skipRetry,
4029
+ timeoutMs: options?.timeoutMs
3974
4030
  });
3975
4031
  }
3976
4032
  /**
@@ -4719,6 +4775,10 @@ var ExecutionStreamControllerImpl = class {
4719
4775
  toolName,
4720
4776
  result: data.result,
4721
4777
  durationMs: data.durationMs ?? 0,
4778
+ // CAD-243: pass the producer's failure verdict through.
4779
+ ...data.isError === true ? { isError: true } : {},
4780
+ ...typeof data.status === "string" ? { status: data.status } : {},
4781
+ ...typeof data.error === "string" ? { error: data.error } : {},
4722
4782
  forwardedFrom: event.data._forwardedFrom,
4723
4783
  // CAD-87 S1-001 EventIdentity → (execId, seq) reducer dedup (S1-003).
4724
4784
  sourceExecutionId: data.sourceExecutionId ?? data.executionId,
@@ -6957,6 +7017,156 @@ var RemoteRunnersAPI = class extends BaseResourceAPI {
6957
7017
  }
6958
7018
  };
6959
7019
 
7020
+ // src/resources/ai-functions.ts
7021
+ var AI_FUNCTION_RUN_TIMEOUT_MS = 31e4;
7022
+ var AI_FUNCTION_DESCRIPTOR_TTL_MS = 6e4;
7023
+ function checkAiFunctionParameters(parameters, variables) {
7024
+ const issues = [];
7025
+ const declared = /* @__PURE__ */ new Set();
7026
+ for (const p of parameters) {
7027
+ declared.add(p.name);
7028
+ const has = Object.prototype.hasOwnProperty.call(variables, p.name);
7029
+ const value = has ? variables[p.name] : void 0;
7030
+ if (value === void 0 || value === null) {
7031
+ if (p.required) issues.push({ name: p.name, code: "missing" });
7032
+ continue;
7033
+ }
7034
+ switch (p.type) {
7035
+ case "number":
7036
+ if (typeof value !== "number" || !Number.isFinite(value)) {
7037
+ issues.push({ name: p.name, code: "type" });
7038
+ }
7039
+ break;
7040
+ case "boolean":
7041
+ if (typeof value !== "boolean") issues.push({ name: p.name, code: "type" });
7042
+ break;
7043
+ case "enum":
7044
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
7045
+ else if (p.options && !p.options.includes(value)) {
7046
+ issues.push({ name: p.name, code: "option" });
7047
+ }
7048
+ break;
7049
+ case "string":
7050
+ case "text":
7051
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
7052
+ else if (typeof p.maxLength === "number" && value.length > p.maxLength) {
7053
+ issues.push({ name: p.name, code: "too_long" });
7054
+ }
7055
+ break;
7056
+ case "image":
7057
+ if (typeof value !== "string") issues.push({ name: p.name, code: "type" });
7058
+ break;
7059
+ }
7060
+ }
7061
+ for (const name of Object.keys(variables)) {
7062
+ if (!declared.has(name)) issues.push({ name, code: "unknown" });
7063
+ }
7064
+ return issues;
7065
+ }
7066
+ var AiFunctionsAPI = class extends BaseResourceAPI {
7067
+ descriptorCache = /* @__PURE__ */ new Map();
7068
+ constructor(http) {
7069
+ super(http, "/api/v1/ai-functions");
7070
+ }
7071
+ /**
7072
+ * List the functions this org can call — the index (D27a). Each entry carries
7073
+ * the live version's typed parameters; never prompt text.
7074
+ */
7075
+ async list() {
7076
+ const data = await this.httpGet("");
7077
+ const functions = Array.isArray(data) ? data : data?.functions ?? [];
7078
+ for (const d of functions) this.cacheDescriptor(d);
7079
+ return functions;
7080
+ }
7081
+ /**
7082
+ * Get one function's descriptor by key (D27a).
7083
+ * `NotFoundError` (`FUNCTION_NOT_FOUND`) / `ConflictError` (`FUNCTION_NOT_PUBLISHED`).
7084
+ */
7085
+ async get(functionKey) {
7086
+ const data = await this.httpGet(
7087
+ `/${encodeURIComponent(functionKey)}`
7088
+ );
7089
+ const descriptor = data && typeof data === "object" && "function" in data && !("functionKey" in data) ? data.function : data;
7090
+ this.cacheDescriptor(descriptor, functionKey);
7091
+ return descriptor;
7092
+ }
7093
+ /**
7094
+ * Read one of this org's own runs (safe projection, §6.5).
7095
+ */
7096
+ async getRun(executionId) {
7097
+ return this.httpGet(`/runs/${encodeURIComponent(executionId)}`);
7098
+ }
7099
+ /** Forget cached descriptors (one key, or all). */
7100
+ clearDescriptorCache(functionKey) {
7101
+ if (functionKey) this.descriptorCache.delete(functionKey);
7102
+ else this.descriptorCache.clear();
7103
+ }
7104
+ async run(refOrKey, parametersOrOptions, shorthandOpts) {
7105
+ let ref;
7106
+ let options;
7107
+ if (typeof refOrKey === "string") {
7108
+ ref = { functionKey: refOrKey };
7109
+ options = { ...shorthandOpts ?? {}, variables: parametersOrOptions };
7110
+ } else {
7111
+ ref = refOrKey;
7112
+ options = parametersOrOptions;
7113
+ }
7114
+ const variables = options.variables ?? {};
7115
+ const functionKey = "functionKey" in ref ? ref.functionKey : void 0;
7116
+ const shouldCheck = functionKey !== void 0 && options.version === void 0 && options.validate !== false;
7117
+ if (shouldCheck) {
7118
+ const descriptor = await this.getCachedDescriptor(functionKey);
7119
+ const issues = checkAiFunctionParameters(descriptor.parameters, variables);
7120
+ if (issues.length > 0) {
7121
+ throw new AiFunctionParameterError(functionKey, issues);
7122
+ }
7123
+ }
7124
+ const body = {
7125
+ ref,
7126
+ variables,
7127
+ version: options.version,
7128
+ idempotencyKey: options.idempotencyKey ?? generateExecutionId(),
7129
+ params: options.params
7130
+ };
7131
+ try {
7132
+ return await this.httpPost("/run", body, {
7133
+ skipRetry: true,
7134
+ timeoutMs: Math.max(AI_FUNCTION_RUN_TIMEOUT_MS, this.http.timeoutMs ?? 0)
7135
+ });
7136
+ } catch (error) {
7137
+ throw this.mapRunError(error, functionKey);
7138
+ }
7139
+ }
7140
+ // ── internals ──────────────────────────────────────────────────────────────
7141
+ cacheDescriptor(descriptor, key) {
7142
+ if (!descriptor || typeof descriptor !== "object") return;
7143
+ const cacheKey = key ?? descriptor.functionKey;
7144
+ if (!cacheKey) return;
7145
+ this.descriptorCache.set(cacheKey, {
7146
+ descriptor,
7147
+ expiresAt: Date.now() + AI_FUNCTION_DESCRIPTOR_TTL_MS
7148
+ });
7149
+ }
7150
+ async getCachedDescriptor(functionKey) {
7151
+ const hit = this.descriptorCache.get(functionKey);
7152
+ if (hit && hit.expiresAt > Date.now()) return hit.descriptor;
7153
+ return this.get(functionKey);
7154
+ }
7155
+ mapRunError(error, functionKey) {
7156
+ if (!(error instanceof CadraError)) return error;
7157
+ if (error.serverCode === "DUPLICATE_REQUEST") {
7158
+ return new AiFunctionDuplicateError(error.message, error.details, error.requestId);
7159
+ }
7160
+ if (error.details?.charged === true) {
7161
+ return new AiFunctionChargedError(error);
7162
+ }
7163
+ if (functionKey && error.serverCode?.startsWith("VARIABLE_")) {
7164
+ this.descriptorCache.delete(functionKey);
7165
+ }
7166
+ return error;
7167
+ }
7168
+ };
7169
+
6960
7170
  // src/client.ts
6961
7171
  var DEFAULT_BASE_URL = "https://api.cadraos.com";
6962
7172
  var DEFAULT_TIMEOUT = 3e4;
@@ -7003,6 +7213,10 @@ var CadraClient = class {
7003
7213
  * Remote runner operations (list)
7004
7214
  */
7005
7215
  remoteRunners;
7216
+ /**
7217
+ * AI function operations (list, get, run, getRun) — p93
7218
+ */
7219
+ aiFunctions;
7006
7220
  /**
7007
7221
  * Create a new AI SaaS client
7008
7222
  *
@@ -7092,6 +7306,7 @@ var CadraClient = class {
7092
7306
  this.kanban = new KanbanAPI(this.httpClient, getStreamConfig);
7093
7307
  this.remoteAgents = new RemoteAgentsAPI(this.httpClient, getStreamConfig);
7094
7308
  this.remoteRunners = new RemoteRunnersAPI(this.httpClient);
7309
+ this.aiFunctions = new AiFunctionsAPI(this.httpClient);
7095
7310
  }
7096
7311
  /**
7097
7312
  * Validate client options