risicare 0.8.0 → 0.9.1

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 (72) hide show
  1. package/README.md +98 -21
  2. package/dist/frameworks/instructor.cjs +144 -2
  3. package/dist/frameworks/instructor.cjs.map +1 -1
  4. package/dist/frameworks/instructor.js +144 -2
  5. package/dist/frameworks/instructor.js.map +1 -1
  6. package/dist/frameworks/langchain.cjs +144 -2
  7. package/dist/frameworks/langchain.cjs.map +1 -1
  8. package/dist/frameworks/langchain.js +144 -2
  9. package/dist/frameworks/langchain.js.map +1 -1
  10. package/dist/frameworks/langgraph.cjs +144 -2
  11. package/dist/frameworks/langgraph.cjs.map +1 -1
  12. package/dist/frameworks/langgraph.js +144 -2
  13. package/dist/frameworks/langgraph.js.map +1 -1
  14. package/dist/frameworks/llamaindex.cjs +144 -2
  15. package/dist/frameworks/llamaindex.cjs.map +1 -1
  16. package/dist/frameworks/llamaindex.js +144 -2
  17. package/dist/frameworks/llamaindex.js.map +1 -1
  18. package/dist/index.cjs +329 -67
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +29 -9
  21. package/dist/index.d.ts +29 -9
  22. package/dist/index.js +329 -67
  23. package/dist/index.js.map +1 -1
  24. package/dist/providers/anthropic/index.cjs +144 -2
  25. package/dist/providers/anthropic/index.cjs.map +1 -1
  26. package/dist/providers/anthropic/index.js +144 -2
  27. package/dist/providers/anthropic/index.js.map +1 -1
  28. package/dist/providers/bedrock/index.cjs +144 -2
  29. package/dist/providers/bedrock/index.cjs.map +1 -1
  30. package/dist/providers/bedrock/index.js +144 -2
  31. package/dist/providers/bedrock/index.js.map +1 -1
  32. package/dist/providers/cerebras/index.cjs +144 -2
  33. package/dist/providers/cerebras/index.cjs.map +1 -1
  34. package/dist/providers/cerebras/index.js +144 -2
  35. package/dist/providers/cerebras/index.js.map +1 -1
  36. package/dist/providers/cohere/index.cjs +144 -2
  37. package/dist/providers/cohere/index.cjs.map +1 -1
  38. package/dist/providers/cohere/index.js +144 -2
  39. package/dist/providers/cohere/index.js.map +1 -1
  40. package/dist/providers/google/index.cjs +144 -2
  41. package/dist/providers/google/index.cjs.map +1 -1
  42. package/dist/providers/google/index.js +144 -2
  43. package/dist/providers/google/index.js.map +1 -1
  44. package/dist/providers/groq/index.cjs +144 -2
  45. package/dist/providers/groq/index.cjs.map +1 -1
  46. package/dist/providers/groq/index.js +144 -2
  47. package/dist/providers/groq/index.js.map +1 -1
  48. package/dist/providers/huggingface/index.cjs +144 -2
  49. package/dist/providers/huggingface/index.cjs.map +1 -1
  50. package/dist/providers/huggingface/index.js +144 -2
  51. package/dist/providers/huggingface/index.js.map +1 -1
  52. package/dist/providers/mistral/index.cjs +144 -2
  53. package/dist/providers/mistral/index.cjs.map +1 -1
  54. package/dist/providers/mistral/index.js +144 -2
  55. package/dist/providers/mistral/index.js.map +1 -1
  56. package/dist/providers/ollama/index.cjs +144 -2
  57. package/dist/providers/ollama/index.cjs.map +1 -1
  58. package/dist/providers/ollama/index.js +144 -2
  59. package/dist/providers/ollama/index.js.map +1 -1
  60. package/dist/providers/openai/index.cjs +144 -2
  61. package/dist/providers/openai/index.cjs.map +1 -1
  62. package/dist/providers/openai/index.js +144 -2
  63. package/dist/providers/openai/index.js.map +1 -1
  64. package/dist/providers/together/index.cjs +144 -2
  65. package/dist/providers/together/index.cjs.map +1 -1
  66. package/dist/providers/together/index.js +144 -2
  67. package/dist/providers/together/index.js.map +1 -1
  68. package/dist/providers/vercel-ai/index.cjs +144 -2
  69. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  70. package/dist/providers/vercel-ai/index.js +144 -2
  71. package/dist/providers/vercel-ai/index.js.map +1 -1
  72. package/package.json +1 -1
@@ -23,6 +23,10 @@ function warn(msg) {
23
23
  process.stderr.write(`[risicare] WARNING: ${msg}
24
24
  `);
25
25
  }
26
+ function info(msg) {
27
+ process.stderr.write(`[risicare] INFO: ${msg}
28
+ `);
29
+ }
26
30
 
27
31
  // src/utils/text.ts
28
32
  function cutCodePoints(text, max) {
@@ -227,7 +231,111 @@ var SUPPLEMENTAL_RULES = [
227
231
  ];
228
232
 
229
233
  // src/version.ts
230
- var SDK_VERSION = "0.8.0";
234
+ var SDK_VERSION = "0.9.1";
235
+
236
+ // src/exporters/quota.ts
237
+ var QUOTA_REASON = "monthly_quota_exceeded";
238
+ var QUOTA_HEADER = "X-Risicare-Quota";
239
+ var QUOTA_HEADER_VALUE = "exceeded";
240
+ var DEFAULT_RETRY_AFTER_S = 300;
241
+ var MIN_RETRY_AFTER_S = 1;
242
+ var MAX_RETRY_AFTER_S = 3600;
243
+ var QUOTA_WARN_WINDOW_MS = 36e5;
244
+ var MAX_RESETS_AT_CHARS = 40;
245
+ function asPositiveCount(v) {
246
+ if (typeof v === "number") return Number.isFinite(v) && v > 0;
247
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
248
+ return false;
249
+ }
250
+ function boundedRetryAfter(value) {
251
+ let n;
252
+ if (typeof value === "number") n = value;
253
+ else if (typeof value === "string" && value.trim() !== "") n = Number(value);
254
+ else return DEFAULT_RETRY_AFTER_S;
255
+ if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
256
+ return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
257
+ }
258
+ function boundedResetsAt(value) {
259
+ if (typeof value !== "string" || value.length === 0) return null;
260
+ const cut = value.slice(0, MAX_RESETS_AT_CHARS);
261
+ return /^[ -~]*$/.test(cut) ? cut : null;
262
+ }
263
+ function headerSaysExceeded(headers) {
264
+ try {
265
+ const value = headers?.get?.(QUOTA_HEADER);
266
+ return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
267
+ } catch {
268
+ return false;
269
+ }
270
+ }
271
+ function parseJsonDiscard(payload, headers) {
272
+ const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
273
+ const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
274
+ if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
275
+ return {
276
+ retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
277
+ resetsAt: boundedResetsAt(body.resetsAt)
278
+ };
279
+ }
280
+ return null;
281
+ }
282
+ var EPISODE_END_ACCEPTS = 3;
283
+ var QuotaPause = class {
284
+ constructor(now = () => performance.now()) {
285
+ this.now = now;
286
+ }
287
+ _until = -Infinity;
288
+ _inEpisode = false;
289
+ _accepts = 0;
290
+ _lastWarn = -Infinity;
291
+ _lastInfo = -Infinity;
292
+ /**
293
+ * Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
294
+ * probe lost while the WARNING was held back. The WARNING carries the number.
295
+ */
296
+ _drops = 0;
297
+ addDrops(count) {
298
+ this._drops += count;
299
+ }
300
+ paused() {
301
+ return this.now() < this._until;
302
+ }
303
+ remainingS() {
304
+ return Math.max(0, (this._until - this.now()) / 1e3);
305
+ }
306
+ enter(discard, where, dropped) {
307
+ const t = this.now();
308
+ this._until = t + discard.retryAfterS * 1e3;
309
+ this._inEpisode = true;
310
+ this._accepts = 0;
311
+ if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
312
+ this._lastWarn = t;
313
+ const since = this._drops;
314
+ this._drops = 0;
315
+ warn(
316
+ `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}.` : "")
317
+ );
318
+ } else {
319
+ this._drops += dropped;
320
+ debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
321
+ }
322
+ }
323
+ accepted(where) {
324
+ if (!this._inEpisode) return;
325
+ this._accepts++;
326
+ if (this._accepts < EPISODE_END_ACCEPTS) return;
327
+ this._inEpisode = false;
328
+ this._accepts = 0;
329
+ this._until = -Infinity;
330
+ const t = this.now();
331
+ if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
332
+ this._lastInfo = t;
333
+ info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
334
+ } else {
335
+ debug("Quota pause ended (INFO already written this hour)");
336
+ }
337
+ }
338
+ };
231
339
 
232
340
  // src/exporters/http.ts
233
341
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -268,8 +376,21 @@ var HttpExporter = class {
268
376
  _calls = /* @__PURE__ */ new Map();
269
377
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
270
378
  _acks = /* @__PURE__ */ new WeakMap();
379
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
380
+ noteQuotaDrop(count = 1) {
381
+ this._quota.addDrops(count);
382
+ }
383
+ /** True while the exporter is paused for the monthly event quota (S-1). */
384
+ quotaPaused() {
385
+ return this._quota.paused();
386
+ }
271
387
  /** The spans of each finished call that could not be serialized, by its batch. */
272
388
  _unserializable = /* @__PURE__ */ new WeakMap();
389
+ /**
390
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
391
+ * failure: it does not touch the circuit breaker.
392
+ */
393
+ _quota = new QuotaPause();
273
394
  _circuitOpenUntil = 0;
274
395
  _circuitBreakerThreshold = 5;
275
396
  _circuitBreakerCooldownMs = 6e4;
@@ -361,6 +482,13 @@ var HttpExporter = class {
361
482
  }
362
483
  async _export(spans, call) {
363
484
  const cancelled = call.cancel.signal;
485
+ if (this._quota.paused()) {
486
+ debug(
487
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
488
+ );
489
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
490
+ return "success" /* SUCCESS */;
491
+ }
364
492
  const parts = [];
365
493
  const bad = [];
366
494
  let errorClass = "";
@@ -569,6 +697,11 @@ var HttpExporter = class {
569
697
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
570
698
  );
571
699
  }
700
+ if (ack.quota !== void 0) {
701
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
702
+ } else if (ack.result === "success" /* SUCCESS */) {
703
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
704
+ }
572
705
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
573
706
  if (ack.final) call.noRetry = true;
574
707
  return ack.result;
@@ -635,7 +768,16 @@ async function checkAck(response, spansSent, scrub) {
635
768
  try {
636
769
  payload = await response.json();
637
770
  } catch {
638
- return null;
771
+ payload = void 0;
772
+ }
773
+ const discard = parseJsonDiscard(payload, response.headers);
774
+ if (discard !== null) {
775
+ return {
776
+ result: "success" /* SUCCESS */,
777
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
778
+ final: false,
779
+ quota: discard
780
+ };
639
781
  }
640
782
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
641
783
  return null;