cencori 1.7.1 → 1.7.3

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.mjs CHANGED
@@ -198,6 +198,7 @@ var AINamespace = class {
198
198
  ];
199
199
  const response = await fetch(`${this.config.baseUrl}/api/ai/chat`, {
200
200
  method: "POST",
201
+ signal: request2.signal,
201
202
  headers: {
202
203
  "CENCORI_API_KEY": this.config.apiKey,
203
204
  "Content-Type": "application/json",
@@ -241,10 +242,12 @@ var AINamespace = class {
241
242
  return {
242
243
  object: parsedObject,
243
244
  usage: {
244
- promptTokens: data.usage?.prompt_tokens ?? 0,
245
- completionTokens: data.usage?.completion_tokens ?? 0,
246
- totalTokens: data.usage?.total_tokens ?? 0
247
- }
245
+ promptTokens: data.usage?.prompt_tokens ?? null,
246
+ completionTokens: data.usage?.completion_tokens ?? null,
247
+ totalTokens: data.usage?.total_tokens ?? null
248
+ },
249
+ costUsd: data.cost_usd ?? null,
250
+ requestId: response.headers.get("X-Request-Id")
248
251
  };
249
252
  }
250
253
  /**
@@ -1762,9 +1765,27 @@ var WebNamespace = class {
1762
1765
  };
1763
1766
 
1764
1767
  // src/embedded/index.ts
1765
- async function request(config, method, path, body, extraHeaders) {
1768
+ var CencoriEmbeddedApiError = class extends Error {
1769
+ constructor(message, status, code, requestId, type, param, retryAfterSeconds = null) {
1770
+ super(`Cencori API error: ${message}`);
1771
+ this.status = status;
1772
+ this.code = code;
1773
+ this.requestId = requestId;
1774
+ this.type = type;
1775
+ this.param = param;
1776
+ this.retryAfterSeconds = retryAfterSeconds;
1777
+ this.name = "CencoriEmbeddedApiError";
1778
+ }
1779
+ };
1780
+ function parseRetryAfterSeconds(value) {
1781
+ if (!value) return null;
1782
+ const seconds = Number.parseInt(value.trim(), 10);
1783
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1784
+ }
1785
+ async function request(config, method, path, body, extraHeaders, options) {
1766
1786
  const response = await fetch(`${config.baseUrl}${path}`, {
1767
1787
  method,
1788
+ signal: options?.signal,
1768
1789
  headers: {
1769
1790
  "CENCORI_API_KEY": config.apiKey,
1770
1791
  "Content-Type": "application/json",
@@ -1776,7 +1797,20 @@ async function request(config, method, path, body, extraHeaders) {
1776
1797
  if (!response.ok) {
1777
1798
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1778
1799
  const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1779
- throw new Error(`Cencori API error: ${message}`);
1800
+ const details = typeof errorData.error === "object" ? errorData.error : null;
1801
+ let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1802
+ if (retryAfterSeconds == null) {
1803
+ const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
1804
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
1805
+ retryAfterSeconds = Math.ceil(secHint);
1806
+ } else {
1807
+ const msHint = details?.retry_after_ms ?? errorData.retry_after_ms;
1808
+ if (typeof msHint === "number" && Number.isFinite(msHint) && msHint >= 0) {
1809
+ retryAfterSeconds = Math.ceil(msHint / 1e3);
1810
+ }
1811
+ }
1812
+ }
1813
+ throw new CencoriEmbeddedApiError(message, response.status, details?.code ?? null, details?.request_id ?? response.headers.get("X-Request-Id"), details?.type ?? null, details?.param ?? null, retryAfterSeconds);
1780
1814
  }
1781
1815
  if (response.status === 204) return void 0;
1782
1816
  return response.json();
@@ -1799,8 +1833,8 @@ var TenantsNamespace = class {
1799
1833
  list(params) {
1800
1834
  return request(this.config, "GET", `/v1/tenants${qs({ limit: params?.limit, cursor: params?.cursor, status: params?.status })}`);
1801
1835
  }
1802
- get(tenantId) {
1803
- return request(this.config, "GET", `/v1/tenants/${tenantId}`);
1836
+ get(tenantId, options) {
1837
+ return request(this.config, "GET", `/v1/tenants/${tenantId}`, void 0, void 0, options);
1804
1838
  }
1805
1839
  update(tenantId, params) {
1806
1840
  return request(this.config, "PATCH", `/v1/tenants/${tenantId}`, params);
@@ -1903,8 +1937,8 @@ var AgentVersionsNamespace = class {
1903
1937
  publish(agentId, version) {
1904
1938
  return request(this.config, "POST", `/v1/agents/${agentId}/versions/${version}/publish`, {});
1905
1939
  }
1906
- test(agentId, version, params) {
1907
- return request(this.config, "POST", `/v1/agents/${agentId}/versions/${version}/test`, params ?? {});
1940
+ test(agentId, version, params, options) {
1941
+ return request(this.config, "POST", `/v1/agents/${agentId}/versions/${version}/test`, params ?? {}, void 0, options);
1908
1942
  }
1909
1943
  deprecate(agentId, version) {
1910
1944
  return request(this.config, "POST", `/v1/agents/${agentId}/versions/${version}/deprecate`, {});
@@ -1946,17 +1980,17 @@ var RunsNamespace = class {
1946
1980
  constructor(config) {
1947
1981
  this.config = config;
1948
1982
  }
1949
- create(agentId, params, idempotencyKey) {
1950
- return request(this.config, "POST", `/v1/agents/${agentId}/runs`, params, idempotencyKey ? { "Idempotency-Key": idempotencyKey } : void 0);
1983
+ create(agentId, params, idempotencyKey, options) {
1984
+ return request(this.config, "POST", `/v1/agents/${agentId}/runs`, params, idempotencyKey ? { "Idempotency-Key": idempotencyKey } : void 0, options);
1951
1985
  }
1952
- get(runId) {
1953
- return request(this.config, "GET", `/v1/runs/${runId}`);
1986
+ get(runId, options) {
1987
+ return request(this.config, "GET", `/v1/runs/${runId}`, void 0, void 0, options);
1954
1988
  }
1955
1989
  events(runId, params) {
1956
1990
  return request(this.config, "GET", `/v1/runs/${runId}/events${qs({ after: params?.after, limit: params?.limit })}`);
1957
1991
  }
1958
- cancel(runId) {
1959
- return request(this.config, "POST", `/v1/runs/${runId}/cancel`, {});
1992
+ cancel(runId, options) {
1993
+ return request(this.config, "POST", `/v1/runs/${runId}/cancel`, {}, void 0, options);
1960
1994
  }
1961
1995
  delegate(runId, params, idempotencyKey) {
1962
1996
  return request(this.config, "POST", `/v1/runs/${runId}/delegate`, params, idempotencyKey ? { "Idempotency-Key": idempotencyKey } : void 0);
@@ -2261,8 +2295,9 @@ var AuthenticationError = class _AuthenticationError extends CencoriError {
2261
2295
  }
2262
2296
  };
2263
2297
  var RateLimitError = class _RateLimitError extends CencoriError {
2264
- constructor(message = "Rate limit exceeded") {
2298
+ constructor(message = "Rate limit exceeded", retryAfterSeconds) {
2265
2299
  super(message, 429, "RATE_LIMIT_EXCEEDED");
2300
+ this.retryAfterSeconds = retryAfterSeconds;
2266
2301
  this.name = "RateLimitError";
2267
2302
  Object.setPrototypeOf(this, _RateLimitError.prototype);
2268
2303
  }
@@ -2386,7 +2421,18 @@ var Cencori = class {
2386
2421
  throw new AuthenticationError(errorData.error || "Invalid API key");
2387
2422
  }
2388
2423
  if (response.status === 429) {
2389
- throw new RateLimitError(errorData.error || "Rate limit exceeded");
2424
+ const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2425
+ const parsed = rawRetryAfter ? Number.parseInt(rawRetryAfter.trim(), 10) : NaN;
2426
+ let retryAfterSeconds = Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
2427
+ if (retryAfterSeconds == null) {
2428
+ const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2429
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
2430
+ retryAfterSeconds = Math.ceil(secHint);
2431
+ } else if (typeof errorData.retry_after_ms === "number" && Number.isFinite(errorData.retry_after_ms) && errorData.retry_after_ms >= 0) {
2432
+ retryAfterSeconds = Math.ceil(errorData.retry_after_ms / 1e3);
2433
+ }
2434
+ }
2435
+ throw new RateLimitError(errorData.error || "Rate limit exceeded", retryAfterSeconds);
2390
2436
  }
2391
2437
  if (response.status === 400 && errorData.reasons) {
2392
2438
  throw new SafetyError(errorData.error, errorData.reasons);
@@ -2753,6 +2799,7 @@ export {
2753
2799
  AgentsNamespace,
2754
2800
  AuthenticationError,
2755
2801
  Cencori,
2802
+ CencoriEmbeddedApiError,
2756
2803
  CencoriError,
2757
2804
  ChatNamespace,
2758
2805
  ClientTokensNamespace,