cencori 1.7.2 → 1.7.4

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
@@ -1766,16 +1766,38 @@ var WebNamespace = class {
1766
1766
 
1767
1767
  // src/embedded/index.ts
1768
1768
  var CencoriEmbeddedApiError = class extends Error {
1769
- constructor(message, status, code, requestId, type, param) {
1769
+ constructor(message, status, code, requestId, type, param, retryAfterSeconds = null) {
1770
1770
  super(`Cencori API error: ${message}`);
1771
1771
  this.status = status;
1772
1772
  this.code = code;
1773
1773
  this.requestId = requestId;
1774
1774
  this.type = type;
1775
1775
  this.param = param;
1776
+ this.retryAfterSeconds = retryAfterSeconds;
1776
1777
  this.name = "CencoriEmbeddedApiError";
1777
1778
  }
1778
1779
  };
1780
+ var HTTP_DATE_PATTERNS = [
1781
+ // IMF-fixdate, e.g. Sun, 06 Nov 1994 08:49:37 GMT (what toUTCString emits)
1782
+ /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/,
1783
+ // RFC 850, e.g. Sunday, 06-Nov-94 08:49:37 GMT
1784
+ /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/,
1785
+ // asctime, e.g. Sun Nov 6 08:49:37 1994
1786
+ /^[A-Za-z]{3} [A-Za-z]{3} ( \d|\d{2}) \d{2}:\d{2}:\d{2} \d{4}$/
1787
+ ];
1788
+ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
1789
+ if (!value) return null;
1790
+ const trimmed = value.trim();
1791
+ if (!trimmed) return null;
1792
+ if (/^[0-9]+$/.test(trimmed)) {
1793
+ const seconds = Number.parseInt(trimmed, 10);
1794
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1795
+ }
1796
+ if (!HTTP_DATE_PATTERNS.some((pattern) => pattern.test(trimmed))) return null;
1797
+ const timestamp = Date.parse(trimmed);
1798
+ if (!Number.isFinite(timestamp)) return null;
1799
+ return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
1800
+ }
1779
1801
  async function request(config, method, path, body, extraHeaders, options) {
1780
1802
  const response = await fetch(`${config.baseUrl}${path}`, {
1781
1803
  method,
@@ -1792,7 +1814,19 @@ async function request(config, method, path, body, extraHeaders, options) {
1792
1814
  const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
1793
1815
  const message = typeof errorData.error === "string" ? errorData.error : errorData.error?.message || response.statusText;
1794
1816
  const details = typeof errorData.error === "object" ? errorData.error : null;
1795
- throw new CencoriEmbeddedApiError(message, response.status, details?.code ?? null, details?.request_id ?? response.headers.get("X-Request-Id"), details?.type ?? null, details?.param ?? null);
1817
+ let retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("Retry-After") ?? response.headers.get("retry-after"));
1818
+ if (retryAfterSeconds == null) {
1819
+ const secHint = details?.retry_after_seconds ?? errorData.retry_after_seconds ?? details?.retry_after ?? errorData.retry_after;
1820
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
1821
+ retryAfterSeconds = Math.ceil(secHint);
1822
+ } else {
1823
+ const msHint = details?.retry_after_ms ?? errorData.retry_after_ms;
1824
+ if (typeof msHint === "number" && Number.isFinite(msHint) && msHint >= 0) {
1825
+ retryAfterSeconds = Math.ceil(msHint / 1e3);
1826
+ }
1827
+ }
1828
+ }
1829
+ 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);
1796
1830
  }
1797
1831
  if (response.status === 204) return void 0;
1798
1832
  return response.json();
@@ -2277,8 +2311,9 @@ var AuthenticationError = class _AuthenticationError extends CencoriError {
2277
2311
  }
2278
2312
  };
2279
2313
  var RateLimitError = class _RateLimitError extends CencoriError {
2280
- constructor(message = "Rate limit exceeded") {
2314
+ constructor(message = "Rate limit exceeded", retryAfterSeconds) {
2281
2315
  super(message, 429, "RATE_LIMIT_EXCEEDED");
2316
+ this.retryAfterSeconds = retryAfterSeconds;
2282
2317
  this.name = "RateLimitError";
2283
2318
  Object.setPrototypeOf(this, _RateLimitError.prototype);
2284
2319
  }
@@ -2402,7 +2437,17 @@ var Cencori = class {
2402
2437
  throw new AuthenticationError(errorData.error || "Invalid API key");
2403
2438
  }
2404
2439
  if (response.status === 429) {
2405
- throw new RateLimitError(errorData.error || "Rate limit exceeded");
2440
+ const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2441
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2442
+ if (retryAfterSeconds == null) {
2443
+ const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2444
+ if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {
2445
+ retryAfterSeconds = Math.ceil(secHint);
2446
+ } else if (typeof errorData.retry_after_ms === "number" && Number.isFinite(errorData.retry_after_ms) && errorData.retry_after_ms >= 0) {
2447
+ retryAfterSeconds = Math.ceil(errorData.retry_after_ms / 1e3);
2448
+ }
2449
+ }
2450
+ throw new RateLimitError(errorData.error || "Rate limit exceeded", retryAfterSeconds);
2406
2451
  }
2407
2452
  if (response.status === 400 && errorData.reasons) {
2408
2453
  throw new SafetyError(errorData.error, errorData.reasons);