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
@@ -66,6 +66,10 @@ function warn(msg) {
66
66
  process.stderr.write(`[risicare] WARNING: ${msg}
67
67
  `);
68
68
  }
69
+ function info(msg) {
70
+ process.stderr.write(`[risicare] INFO: ${msg}
71
+ `);
72
+ }
69
73
 
70
74
  // src/utils/text.ts
71
75
  function cutCodePoints(text, max) {
@@ -278,7 +282,111 @@ function getContext() {
278
282
  }
279
283
 
280
284
  // src/version.ts
281
- var SDK_VERSION = "0.8.0";
285
+ var SDK_VERSION = "0.9.1";
286
+
287
+ // src/exporters/quota.ts
288
+ var QUOTA_REASON = "monthly_quota_exceeded";
289
+ var QUOTA_HEADER = "X-Risicare-Quota";
290
+ var QUOTA_HEADER_VALUE = "exceeded";
291
+ var DEFAULT_RETRY_AFTER_S = 300;
292
+ var MIN_RETRY_AFTER_S = 1;
293
+ var MAX_RETRY_AFTER_S = 3600;
294
+ var QUOTA_WARN_WINDOW_MS = 36e5;
295
+ var MAX_RESETS_AT_CHARS = 40;
296
+ function asPositiveCount(v) {
297
+ if (typeof v === "number") return Number.isFinite(v) && v > 0;
298
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
299
+ return false;
300
+ }
301
+ function boundedRetryAfter(value) {
302
+ let n;
303
+ if (typeof value === "number") n = value;
304
+ else if (typeof value === "string" && value.trim() !== "") n = Number(value);
305
+ else return DEFAULT_RETRY_AFTER_S;
306
+ if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
307
+ return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
308
+ }
309
+ function boundedResetsAt(value) {
310
+ if (typeof value !== "string" || value.length === 0) return null;
311
+ const cut = value.slice(0, MAX_RESETS_AT_CHARS);
312
+ return /^[ -~]*$/.test(cut) ? cut : null;
313
+ }
314
+ function headerSaysExceeded(headers) {
315
+ try {
316
+ const value = headers?.get?.(QUOTA_HEADER);
317
+ return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
318
+ } catch {
319
+ return false;
320
+ }
321
+ }
322
+ function parseJsonDiscard(payload, headers) {
323
+ const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
324
+ const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
325
+ if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
326
+ return {
327
+ retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
328
+ resetsAt: boundedResetsAt(body.resetsAt)
329
+ };
330
+ }
331
+ return null;
332
+ }
333
+ var EPISODE_END_ACCEPTS = 3;
334
+ var QuotaPause = class {
335
+ constructor(now = () => performance.now()) {
336
+ this.now = now;
337
+ }
338
+ _until = -Infinity;
339
+ _inEpisode = false;
340
+ _accepts = 0;
341
+ _lastWarn = -Infinity;
342
+ _lastInfo = -Infinity;
343
+ /**
344
+ * Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
345
+ * probe lost while the WARNING was held back. The WARNING carries the number.
346
+ */
347
+ _drops = 0;
348
+ addDrops(count) {
349
+ this._drops += count;
350
+ }
351
+ paused() {
352
+ return this.now() < this._until;
353
+ }
354
+ remainingS() {
355
+ return Math.max(0, (this._until - this.now()) / 1e3);
356
+ }
357
+ enter(discard, where, dropped) {
358
+ const t = this.now();
359
+ this._until = t + discard.retryAfterS * 1e3;
360
+ this._inEpisode = true;
361
+ this._accepts = 0;
362
+ if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
363
+ this._lastWarn = t;
364
+ const since = this._drops;
365
+ this._drops = 0;
366
+ warn(
367
+ `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}.` : "")
368
+ );
369
+ } else {
370
+ this._drops += dropped;
371
+ debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
372
+ }
373
+ }
374
+ accepted(where) {
375
+ if (!this._inEpisode) return;
376
+ this._accepts++;
377
+ if (this._accepts < EPISODE_END_ACCEPTS) return;
378
+ this._inEpisode = false;
379
+ this._accepts = 0;
380
+ this._until = -Infinity;
381
+ const t = this.now();
382
+ if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
383
+ this._lastInfo = t;
384
+ info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
385
+ } else {
386
+ debug("Quota pause ended (INFO already written this hour)");
387
+ }
388
+ }
389
+ };
282
390
 
283
391
  // src/exporters/http.ts
284
392
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -319,8 +427,21 @@ var HttpExporter = class {
319
427
  _calls = /* @__PURE__ */ new Map();
320
428
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
321
429
  _acks = /* @__PURE__ */ new WeakMap();
430
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
431
+ noteQuotaDrop(count = 1) {
432
+ this._quota.addDrops(count);
433
+ }
434
+ /** True while the exporter is paused for the monthly event quota (S-1). */
435
+ quotaPaused() {
436
+ return this._quota.paused();
437
+ }
322
438
  /** The spans of each finished call that could not be serialized, by its batch. */
323
439
  _unserializable = /* @__PURE__ */ new WeakMap();
440
+ /**
441
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
442
+ * failure: it does not touch the circuit breaker.
443
+ */
444
+ _quota = new QuotaPause();
324
445
  _circuitOpenUntil = 0;
325
446
  _circuitBreakerThreshold = 5;
326
447
  _circuitBreakerCooldownMs = 6e4;
@@ -412,6 +533,13 @@ var HttpExporter = class {
412
533
  }
413
534
  async _export(spans, call) {
414
535
  const cancelled = call.cancel.signal;
536
+ if (this._quota.paused()) {
537
+ debug(
538
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
539
+ );
540
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
541
+ return "success" /* SUCCESS */;
542
+ }
415
543
  const parts = [];
416
544
  const bad = [];
417
545
  let errorClass = "";
@@ -620,6 +748,11 @@ var HttpExporter = class {
620
748
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
621
749
  );
622
750
  }
751
+ if (ack.quota !== void 0) {
752
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
753
+ } else if (ack.result === "success" /* SUCCESS */) {
754
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
755
+ }
623
756
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
624
757
  if (ack.final) call.noRetry = true;
625
758
  return ack.result;
@@ -686,7 +819,16 @@ async function checkAck(response, spansSent, scrub) {
686
819
  try {
687
820
  payload = await response.json();
688
821
  } catch {
689
- return null;
822
+ payload = void 0;
823
+ }
824
+ const discard = parseJsonDiscard(payload, response.headers);
825
+ if (discard !== null) {
826
+ return {
827
+ result: "success" /* SUCCESS */,
828
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
829
+ final: false,
830
+ quota: discard
831
+ };
690
832
  }
691
833
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
692
834
  return null;