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
@@ -52,6 +52,10 @@ function warn(msg) {
52
52
  process.stderr.write(`[risicare] WARNING: ${msg}
53
53
  `);
54
54
  }
55
+ function info(msg) {
56
+ process.stderr.write(`[risicare] INFO: ${msg}
57
+ `);
58
+ }
55
59
 
56
60
  // src/utils/text.ts
57
61
  function cutCodePoints(text, max) {
@@ -456,7 +460,111 @@ function attachErrorCode(span, error) {
456
460
  }
457
461
 
458
462
  // src/version.ts
459
- var SDK_VERSION = "0.9.0";
463
+ var SDK_VERSION = "0.9.2";
464
+
465
+ // src/exporters/quota.ts
466
+ var QUOTA_REASON = "monthly_quota_exceeded";
467
+ var QUOTA_HEADER = "X-Risicare-Quota";
468
+ var QUOTA_HEADER_VALUE = "exceeded";
469
+ var DEFAULT_RETRY_AFTER_S = 300;
470
+ var MIN_RETRY_AFTER_S = 1;
471
+ var MAX_RETRY_AFTER_S = 3600;
472
+ var QUOTA_WARN_WINDOW_MS = 36e5;
473
+ var MAX_RESETS_AT_CHARS = 40;
474
+ function asPositiveCount(v) {
475
+ if (typeof v === "number") return Number.isFinite(v) && v > 0;
476
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
477
+ return false;
478
+ }
479
+ function boundedRetryAfter(value) {
480
+ let n;
481
+ if (typeof value === "number") n = value;
482
+ else if (typeof value === "string" && value.trim() !== "") n = Number(value);
483
+ else return DEFAULT_RETRY_AFTER_S;
484
+ if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
485
+ return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
486
+ }
487
+ function boundedResetsAt(value) {
488
+ if (typeof value !== "string" || value.length === 0) return null;
489
+ const cut = value.slice(0, MAX_RESETS_AT_CHARS);
490
+ return /^[ -~]*$/.test(cut) ? cut : null;
491
+ }
492
+ function headerSaysExceeded(headers) {
493
+ try {
494
+ const value = headers?.get?.(QUOTA_HEADER);
495
+ return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
496
+ } catch {
497
+ return false;
498
+ }
499
+ }
500
+ function parseJsonDiscard(payload, headers) {
501
+ const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
502
+ const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
503
+ if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
504
+ return {
505
+ retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
506
+ resetsAt: boundedResetsAt(body.resetsAt)
507
+ };
508
+ }
509
+ return null;
510
+ }
511
+ var EPISODE_END_ACCEPTS = 3;
512
+ var QuotaPause = class {
513
+ constructor(now = () => performance.now()) {
514
+ this.now = now;
515
+ }
516
+ _until = -Infinity;
517
+ _inEpisode = false;
518
+ _accepts = 0;
519
+ _lastWarn = -Infinity;
520
+ _lastInfo = -Infinity;
521
+ /**
522
+ * Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
523
+ * probe lost while the WARNING was held back. The WARNING carries the number.
524
+ */
525
+ _drops = 0;
526
+ addDrops(count) {
527
+ this._drops += count;
528
+ }
529
+ paused() {
530
+ return this.now() < this._until;
531
+ }
532
+ remainingS() {
533
+ return Math.max(0, (this._until - this.now()) / 1e3);
534
+ }
535
+ enter(discard, where, dropped) {
536
+ const t = this.now();
537
+ this._until = t + discard.retryAfterS * 1e3;
538
+ this._inEpisode = true;
539
+ this._accepts = 0;
540
+ if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
541
+ this._lastWarn = t;
542
+ const since = this._drops;
543
+ this._drops = 0;
544
+ warn(
545
+ `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}.` : "")
546
+ );
547
+ } else {
548
+ this._drops += dropped;
549
+ debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
550
+ }
551
+ }
552
+ accepted(where) {
553
+ if (!this._inEpisode) return;
554
+ this._accepts++;
555
+ if (this._accepts < EPISODE_END_ACCEPTS) return;
556
+ this._inEpisode = false;
557
+ this._accepts = 0;
558
+ this._until = -Infinity;
559
+ const t = this.now();
560
+ if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
561
+ this._lastInfo = t;
562
+ info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
563
+ } else {
564
+ debug("Quota pause ended (INFO already written this hour)");
565
+ }
566
+ }
567
+ };
460
568
 
461
569
  // src/exporters/http.ts
462
570
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -497,8 +605,23 @@ var HttpExporter = class {
497
605
  _calls = /* @__PURE__ */ new Map();
498
606
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
499
607
  _acks = /* @__PURE__ */ new WeakMap();
608
+ /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
609
+ _requests = /* @__PURE__ */ new WeakMap();
610
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
611
+ noteQuotaDrop(count = 1) {
612
+ this._quota.addDrops(count);
613
+ }
614
+ /** True while the exporter is paused for the monthly event quota (S-1). */
615
+ quotaPaused() {
616
+ return this._quota.paused();
617
+ }
500
618
  /** The spans of each finished call that could not be serialized, by its batch. */
501
619
  _unserializable = /* @__PURE__ */ new WeakMap();
620
+ /**
621
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
622
+ * failure: it does not touch the circuit breaker.
623
+ */
624
+ _quota = new QuotaPause();
502
625
  _circuitOpenUntil = 0;
503
626
  _circuitBreakerThreshold = 5;
504
627
  _circuitBreakerCooldownMs = 6e4;
@@ -535,6 +658,18 @@ var HttpExporter = class {
535
658
  get attemptsPerExport() {
536
659
  return this._maxRetries;
537
660
  }
661
+ /**
662
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
663
+ * it. This is the number that was sent, not the most that the call may send
664
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
665
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
666
+ * kept per batch, as `takeAck` keeps the ack.
667
+ */
668
+ takeAttempts(spans) {
669
+ const n = this._requests.get(spans) ?? null;
670
+ this._requests.delete(spans);
671
+ return n;
672
+ }
538
673
  /**
539
674
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
540
675
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -579,17 +714,26 @@ var HttpExporter = class {
579
714
  misroute: null,
580
715
  noRetry: false,
581
716
  status: null,
582
- error: null
717
+ error: null,
718
+ attempts: 0
583
719
  };
584
720
  this._calls.set(spans, call);
585
721
  try {
586
722
  return await this._export(spans, call);
587
723
  } finally {
588
724
  this._calls.delete(spans);
725
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
589
726
  }
590
727
  }
591
728
  async _export(spans, call) {
592
729
  const cancelled = call.cancel.signal;
730
+ if (this._quota.paused()) {
731
+ debug(
732
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
733
+ );
734
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
735
+ return "success" /* SUCCESS */;
736
+ }
593
737
  const parts = [];
594
738
  const bad = [];
595
739
  let errorClass = "";
@@ -618,14 +762,13 @@ var HttpExporter = class {
618
762
  isHalfOpen = true;
619
763
  }
620
764
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
621
- let attempts = 0;
622
765
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
623
766
  call.refusalAfterMs = null;
624
767
  call.misroute = null;
625
768
  call.noRetry = false;
626
769
  call.status = null;
627
770
  call.error = null;
628
- attempts++;
771
+ call.attempts++;
629
772
  const result = await this._sendRequest(payload, parts.length, call, spans);
630
773
  if (cancelled.aborted) {
631
774
  this._acks.delete(spans);
@@ -647,7 +790,7 @@ var HttpExporter = class {
647
790
  }
648
791
  if (call.refusalAfterMs !== null) {
649
792
  warn(
650
- `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.`
793
+ `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.`
651
794
  );
652
795
  return "failure" /* FAILURE */;
653
796
  }
@@ -798,6 +941,11 @@ var HttpExporter = class {
798
941
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
799
942
  );
800
943
  }
944
+ if (ack.quota !== void 0) {
945
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
946
+ } else if (ack.result === "success" /* SUCCESS */) {
947
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
948
+ }
801
949
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
802
950
  if (ack.final) call.noRetry = true;
803
951
  return ack.result;
@@ -864,7 +1012,16 @@ async function checkAck(response, spansSent, scrub) {
864
1012
  try {
865
1013
  payload = await response.json();
866
1014
  } catch {
867
- return null;
1015
+ payload = void 0;
1016
+ }
1017
+ const discard = parseJsonDiscard(payload, response.headers);
1018
+ if (discard !== null) {
1019
+ return {
1020
+ result: "success" /* SUCCESS */,
1021
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
1022
+ final: false,
1023
+ quota: discard
1024
+ };
868
1025
  }
869
1026
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
870
1027
  return null;