risicare 0.9.0 → 0.9.2

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 (93) hide show
  1. package/CHANGELOG.md +1236 -0
  2. package/README.md +18 -0
  3. package/dist/frameworks/instructor.cjs +163 -6
  4. package/dist/frameworks/instructor.cjs.map +1 -1
  5. package/dist/frameworks/instructor.js +163 -6
  6. package/dist/frameworks/instructor.js.map +1 -1
  7. package/dist/frameworks/langchain.cjs +163 -6
  8. package/dist/frameworks/langchain.cjs.map +1 -1
  9. package/dist/frameworks/langchain.js +163 -6
  10. package/dist/frameworks/langchain.js.map +1 -1
  11. package/dist/frameworks/langgraph.cjs +163 -6
  12. package/dist/frameworks/langgraph.cjs.map +1 -1
  13. package/dist/frameworks/langgraph.js +163 -6
  14. package/dist/frameworks/langgraph.js.map +1 -1
  15. package/dist/frameworks/llamaindex.cjs +163 -6
  16. package/dist/frameworks/llamaindex.cjs.map +1 -1
  17. package/dist/frameworks/llamaindex.js +163 -6
  18. package/dist/frameworks/llamaindex.js.map +1 -1
  19. package/dist/index.cjs +274 -18
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +7 -1
  22. package/dist/index.d.ts +7 -1
  23. package/dist/index.js +274 -18
  24. package/dist/index.js.map +1 -1
  25. package/dist/providers/anthropic/index.cjs +168 -7
  26. package/dist/providers/anthropic/index.cjs.map +1 -1
  27. package/dist/providers/anthropic/index.d.cts +3 -0
  28. package/dist/providers/anthropic/index.d.ts +3 -0
  29. package/dist/providers/anthropic/index.js +168 -7
  30. package/dist/providers/anthropic/index.js.map +1 -1
  31. package/dist/providers/bedrock/index.cjs +168 -7
  32. package/dist/providers/bedrock/index.cjs.map +1 -1
  33. package/dist/providers/bedrock/index.d.cts +3 -0
  34. package/dist/providers/bedrock/index.d.ts +3 -0
  35. package/dist/providers/bedrock/index.js +168 -7
  36. package/dist/providers/bedrock/index.js.map +1 -1
  37. package/dist/providers/cerebras/index.cjs +168 -7
  38. package/dist/providers/cerebras/index.cjs.map +1 -1
  39. package/dist/providers/cerebras/index.d.cts +3 -0
  40. package/dist/providers/cerebras/index.d.ts +3 -0
  41. package/dist/providers/cerebras/index.js +168 -7
  42. package/dist/providers/cerebras/index.js.map +1 -1
  43. package/dist/providers/cohere/index.cjs +168 -7
  44. package/dist/providers/cohere/index.cjs.map +1 -1
  45. package/dist/providers/cohere/index.d.cts +3 -0
  46. package/dist/providers/cohere/index.d.ts +3 -0
  47. package/dist/providers/cohere/index.js +168 -7
  48. package/dist/providers/cohere/index.js.map +1 -1
  49. package/dist/providers/google/index.cjs +168 -7
  50. package/dist/providers/google/index.cjs.map +1 -1
  51. package/dist/providers/google/index.d.cts +3 -0
  52. package/dist/providers/google/index.d.ts +3 -0
  53. package/dist/providers/google/index.js +168 -7
  54. package/dist/providers/google/index.js.map +1 -1
  55. package/dist/providers/groq/index.cjs +168 -7
  56. package/dist/providers/groq/index.cjs.map +1 -1
  57. package/dist/providers/groq/index.d.cts +3 -0
  58. package/dist/providers/groq/index.d.ts +3 -0
  59. package/dist/providers/groq/index.js +168 -7
  60. package/dist/providers/groq/index.js.map +1 -1
  61. package/dist/providers/huggingface/index.cjs +166 -6
  62. package/dist/providers/huggingface/index.cjs.map +1 -1
  63. package/dist/providers/huggingface/index.js +166 -6
  64. package/dist/providers/huggingface/index.js.map +1 -1
  65. package/dist/providers/mistral/index.cjs +168 -7
  66. package/dist/providers/mistral/index.cjs.map +1 -1
  67. package/dist/providers/mistral/index.d.cts +3 -0
  68. package/dist/providers/mistral/index.d.ts +3 -0
  69. package/dist/providers/mistral/index.js +168 -7
  70. package/dist/providers/mistral/index.js.map +1 -1
  71. package/dist/providers/ollama/index.cjs +168 -7
  72. package/dist/providers/ollama/index.cjs.map +1 -1
  73. package/dist/providers/ollama/index.d.cts +3 -0
  74. package/dist/providers/ollama/index.d.ts +3 -0
  75. package/dist/providers/ollama/index.js +168 -7
  76. package/dist/providers/ollama/index.js.map +1 -1
  77. package/dist/providers/openai/index.cjs +168 -7
  78. package/dist/providers/openai/index.cjs.map +1 -1
  79. package/dist/providers/openai/index.d.cts +3 -0
  80. package/dist/providers/openai/index.d.ts +3 -0
  81. package/dist/providers/openai/index.js +168 -7
  82. package/dist/providers/openai/index.js.map +1 -1
  83. package/dist/providers/together/index.cjs +168 -7
  84. package/dist/providers/together/index.cjs.map +1 -1
  85. package/dist/providers/together/index.d.cts +3 -0
  86. package/dist/providers/together/index.d.ts +3 -0
  87. package/dist/providers/together/index.js +168 -7
  88. package/dist/providers/together/index.js.map +1 -1
  89. package/dist/providers/vercel-ai/index.cjs +163 -6
  90. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  91. package/dist/providers/vercel-ai/index.js +163 -6
  92. package/dist/providers/vercel-ai/index.js.map +1 -1
  93. package/package.json +3 -2
@@ -88,6 +88,10 @@ function warn(msg) {
88
88
  process.stderr.write(`[risicare] WARNING: ${msg}
89
89
  `);
90
90
  }
91
+ function info(msg) {
92
+ process.stderr.write(`[risicare] INFO: ${msg}
93
+ `);
94
+ }
91
95
 
92
96
  // src/utils/text.ts
93
97
  function cutCodePoints(text, max) {
@@ -492,7 +496,111 @@ function attachErrorCode(span, error) {
492
496
  }
493
497
 
494
498
  // src/version.ts
495
- var SDK_VERSION = "0.9.0";
499
+ var SDK_VERSION = "0.9.2";
500
+
501
+ // src/exporters/quota.ts
502
+ var QUOTA_REASON = "monthly_quota_exceeded";
503
+ var QUOTA_HEADER = "X-Risicare-Quota";
504
+ var QUOTA_HEADER_VALUE = "exceeded";
505
+ var DEFAULT_RETRY_AFTER_S = 300;
506
+ var MIN_RETRY_AFTER_S = 1;
507
+ var MAX_RETRY_AFTER_S = 3600;
508
+ var QUOTA_WARN_WINDOW_MS = 36e5;
509
+ var MAX_RESETS_AT_CHARS = 40;
510
+ function asPositiveCount(v) {
511
+ if (typeof v === "number") return Number.isFinite(v) && v > 0;
512
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
513
+ return false;
514
+ }
515
+ function boundedRetryAfter(value) {
516
+ let n;
517
+ if (typeof value === "number") n = value;
518
+ else if (typeof value === "string" && value.trim() !== "") n = Number(value);
519
+ else return DEFAULT_RETRY_AFTER_S;
520
+ if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
521
+ return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
522
+ }
523
+ function boundedResetsAt(value) {
524
+ if (typeof value !== "string" || value.length === 0) return null;
525
+ const cut = value.slice(0, MAX_RESETS_AT_CHARS);
526
+ return /^[ -~]*$/.test(cut) ? cut : null;
527
+ }
528
+ function headerSaysExceeded(headers) {
529
+ try {
530
+ const value = headers?.get?.(QUOTA_HEADER);
531
+ return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
532
+ } catch {
533
+ return false;
534
+ }
535
+ }
536
+ function parseJsonDiscard(payload, headers) {
537
+ const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
538
+ const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
539
+ if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
540
+ return {
541
+ retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
542
+ resetsAt: boundedResetsAt(body.resetsAt)
543
+ };
544
+ }
545
+ return null;
546
+ }
547
+ var EPISODE_END_ACCEPTS = 3;
548
+ var QuotaPause = class {
549
+ constructor(now = () => performance.now()) {
550
+ this.now = now;
551
+ }
552
+ _until = -Infinity;
553
+ _inEpisode = false;
554
+ _accepts = 0;
555
+ _lastWarn = -Infinity;
556
+ _lastInfo = -Infinity;
557
+ /**
558
+ * Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
559
+ * probe lost while the WARNING was held back. The WARNING carries the number.
560
+ */
561
+ _drops = 0;
562
+ addDrops(count) {
563
+ this._drops += count;
564
+ }
565
+ paused() {
566
+ return this.now() < this._until;
567
+ }
568
+ remainingS() {
569
+ return Math.max(0, (this._until - this.now()) / 1e3);
570
+ }
571
+ enter(discard, where, dropped) {
572
+ const t = this.now();
573
+ this._until = t + discard.retryAfterS * 1e3;
574
+ this._inEpisode = true;
575
+ this._accepts = 0;
576
+ if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
577
+ this._lastWarn = t;
578
+ const since = this._drops;
579
+ this._drops = 0;
580
+ warn(
581
+ `Risicare: the monthly event quota is used up (monthly_quota_exceeded). ${where} discarded ${dropped} span(s) and will discard new ones until the quota resets (resetsAt=${discard.resetsAt ?? "unknown"}) or the plan changes. The SDK pauses export and sends one batch again in ${Math.round(discard.retryAfterS)} s. Spans that end in the meantime wait in the queue. When it is full the oldest spans are dropped (the most recent are kept) and counted as quota_exceeded in droppedSpansByReason; a batch that is discarded again is dropped and counted the same way. This is not an error and the SDK does not retry the batch. At most one such WARNING an hour.` + (since > 0 ? ` Spans dropped for the quota since the last WARNING: ${since}.` : "")
582
+ );
583
+ } else {
584
+ this._drops += dropped;
585
+ debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
586
+ }
587
+ }
588
+ accepted(where) {
589
+ if (!this._inEpisode) return;
590
+ this._accepts++;
591
+ if (this._accepts < EPISODE_END_ACCEPTS) return;
592
+ this._inEpisode = false;
593
+ this._accepts = 0;
594
+ this._until = -Infinity;
595
+ const t = this.now();
596
+ if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
597
+ this._lastInfo = t;
598
+ info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
599
+ } else {
600
+ debug("Quota pause ended (INFO already written this hour)");
601
+ }
602
+ }
603
+ };
496
604
 
497
605
  // src/exporters/http.ts
498
606
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -533,8 +641,23 @@ var HttpExporter = class {
533
641
  _calls = /* @__PURE__ */ new Map();
534
642
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
535
643
  _acks = /* @__PURE__ */ new WeakMap();
644
+ /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
645
+ _requests = /* @__PURE__ */ new WeakMap();
646
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
647
+ noteQuotaDrop(count = 1) {
648
+ this._quota.addDrops(count);
649
+ }
650
+ /** True while the exporter is paused for the monthly event quota (S-1). */
651
+ quotaPaused() {
652
+ return this._quota.paused();
653
+ }
536
654
  /** The spans of each finished call that could not be serialized, by its batch. */
537
655
  _unserializable = /* @__PURE__ */ new WeakMap();
656
+ /**
657
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
658
+ * failure: it does not touch the circuit breaker.
659
+ */
660
+ _quota = new QuotaPause();
538
661
  _circuitOpenUntil = 0;
539
662
  _circuitBreakerThreshold = 5;
540
663
  _circuitBreakerCooldownMs = 6e4;
@@ -571,6 +694,18 @@ var HttpExporter = class {
571
694
  get attemptsPerExport() {
572
695
  return this._maxRetries;
573
696
  }
697
+ /**
698
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
699
+ * it. This is the number that was sent, not the most that the call may send
700
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
701
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
702
+ * kept per batch, as `takeAck` keeps the ack.
703
+ */
704
+ takeAttempts(spans) {
705
+ const n = this._requests.get(spans) ?? null;
706
+ this._requests.delete(spans);
707
+ return n;
708
+ }
574
709
  /**
575
710
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
576
711
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -615,17 +750,26 @@ var HttpExporter = class {
615
750
  misroute: null,
616
751
  noRetry: false,
617
752
  status: null,
618
- error: null
753
+ error: null,
754
+ attempts: 0
619
755
  };
620
756
  this._calls.set(spans, call);
621
757
  try {
622
758
  return await this._export(spans, call);
623
759
  } finally {
624
760
  this._calls.delete(spans);
761
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
625
762
  }
626
763
  }
627
764
  async _export(spans, call) {
628
765
  const cancelled = call.cancel.signal;
766
+ if (this._quota.paused()) {
767
+ debug(
768
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
769
+ );
770
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
771
+ return "success" /* SUCCESS */;
772
+ }
629
773
  const parts = [];
630
774
  const bad = [];
631
775
  let errorClass = "";
@@ -654,14 +798,13 @@ var HttpExporter = class {
654
798
  isHalfOpen = true;
655
799
  }
656
800
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
657
- let attempts = 0;
658
801
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
659
802
  call.refusalAfterMs = null;
660
803
  call.misroute = null;
661
804
  call.noRetry = false;
662
805
  call.status = null;
663
806
  call.error = null;
664
- attempts++;
807
+ call.attempts++;
665
808
  const result = await this._sendRequest(payload, parts.length, call, spans);
666
809
  if (cancelled.aborted) {
667
810
  this._acks.delete(spans);
@@ -683,7 +826,7 @@ var HttpExporter = class {
683
826
  }
684
827
  if (call.refusalAfterMs !== null) {
685
828
  warn(
686
- `Risicare: the ingest endpoint refused this batch for capacity (HTTP 429/503) after ${attempts} attempt(s). This is a healthy server shedding load, not an outage. This export of the batch failed; the circuit breaker does not count it. Reduce batch size or export rate if it persists.`
829
+ `Risicare: the ingest endpoint refused this batch for capacity (HTTP 429/503) after ${call.attempts} attempt(s). This is a healthy server shedding load, not an outage. This export of the batch failed; the circuit breaker does not count it. Reduce batch size or export rate if it persists.`
687
830
  );
688
831
  return "failure" /* FAILURE */;
689
832
  }
@@ -834,6 +977,11 @@ var HttpExporter = class {
834
977
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
835
978
  );
836
979
  }
980
+ if (ack.quota !== void 0) {
981
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
982
+ } else if (ack.result === "success" /* SUCCESS */) {
983
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
984
+ }
837
985
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
838
986
  if (ack.final) call.noRetry = true;
839
987
  return ack.result;
@@ -900,7 +1048,16 @@ async function checkAck(response, spansSent, scrub) {
900
1048
  try {
901
1049
  payload = await response.json();
902
1050
  } catch {
903
- return null;
1051
+ payload = void 0;
1052
+ }
1053
+ const discard = parseJsonDiscard(payload, response.headers);
1054
+ if (discard !== null) {
1055
+ return {
1056
+ result: "success" /* SUCCESS */,
1057
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
1058
+ final: false,
1059
+ quota: discard
1060
+ };
904
1061
  }
905
1062
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
906
1063
  return null;
@@ -1812,9 +1969,11 @@ function createCompletionProxy(originalCreate) {
1812
1969
  );
1813
1970
  };
1814
1971
  }
1972
+ var PATCHED = /* @__PURE__ */ new WeakSet();
1815
1973
  function patchCerebras(client) {
1816
1974
  assertClient("patchCerebras", client);
1817
- return new Proxy(client, {
1975
+ if (PATCHED.has(client)) return client;
1976
+ const proxy = new Proxy(client, {
1818
1977
  get(target, prop, receiver) {
1819
1978
  const value = Reflect.get(target, prop, receiver);
1820
1979
  if (prop === "chat" && value && typeof value === "object") {
@@ -1839,6 +1998,8 @@ function patchCerebras(client) {
1839
1998
  return value;
1840
1999
  }
1841
2000
  });
2001
+ PATCHED.add(proxy);
2002
+ return proxy;
1842
2003
  }
1843
2004
  // Annotate the CommonJS export names for ESM import in node:
1844
2005
  0 && (module.exports = {