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
@@ -29,6 +29,10 @@ function warn(msg) {
29
29
  process.stderr.write(`[risicare] WARNING: ${msg}
30
30
  `);
31
31
  }
32
+ function info(msg) {
33
+ process.stderr.write(`[risicare] INFO: ${msg}
34
+ `);
35
+ }
32
36
 
33
37
  // src/utils/text.ts
34
38
  function cutCodePoints(text, max) {
@@ -241,7 +245,111 @@ function getContext() {
241
245
  }
242
246
 
243
247
  // src/version.ts
244
- var SDK_VERSION = "0.8.0";
248
+ var SDK_VERSION = "0.9.1";
249
+
250
+ // src/exporters/quota.ts
251
+ var QUOTA_REASON = "monthly_quota_exceeded";
252
+ var QUOTA_HEADER = "X-Risicare-Quota";
253
+ var QUOTA_HEADER_VALUE = "exceeded";
254
+ var DEFAULT_RETRY_AFTER_S = 300;
255
+ var MIN_RETRY_AFTER_S = 1;
256
+ var MAX_RETRY_AFTER_S = 3600;
257
+ var QUOTA_WARN_WINDOW_MS = 36e5;
258
+ var MAX_RESETS_AT_CHARS = 40;
259
+ function asPositiveCount(v) {
260
+ if (typeof v === "number") return Number.isFinite(v) && v > 0;
261
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
262
+ return false;
263
+ }
264
+ function boundedRetryAfter(value) {
265
+ let n;
266
+ if (typeof value === "number") n = value;
267
+ else if (typeof value === "string" && value.trim() !== "") n = Number(value);
268
+ else return DEFAULT_RETRY_AFTER_S;
269
+ if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
270
+ return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
271
+ }
272
+ function boundedResetsAt(value) {
273
+ if (typeof value !== "string" || value.length === 0) return null;
274
+ const cut = value.slice(0, MAX_RESETS_AT_CHARS);
275
+ return /^[ -~]*$/.test(cut) ? cut : null;
276
+ }
277
+ function headerSaysExceeded(headers) {
278
+ try {
279
+ const value = headers?.get?.(QUOTA_HEADER);
280
+ return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
281
+ } catch {
282
+ return false;
283
+ }
284
+ }
285
+ function parseJsonDiscard(payload, headers) {
286
+ const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
287
+ const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
288
+ if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
289
+ return {
290
+ retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
291
+ resetsAt: boundedResetsAt(body.resetsAt)
292
+ };
293
+ }
294
+ return null;
295
+ }
296
+ var EPISODE_END_ACCEPTS = 3;
297
+ var QuotaPause = class {
298
+ constructor(now = () => performance.now()) {
299
+ this.now = now;
300
+ }
301
+ _until = -Infinity;
302
+ _inEpisode = false;
303
+ _accepts = 0;
304
+ _lastWarn = -Infinity;
305
+ _lastInfo = -Infinity;
306
+ /**
307
+ * Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
308
+ * probe lost while the WARNING was held back. The WARNING carries the number.
309
+ */
310
+ _drops = 0;
311
+ addDrops(count) {
312
+ this._drops += count;
313
+ }
314
+ paused() {
315
+ return this.now() < this._until;
316
+ }
317
+ remainingS() {
318
+ return Math.max(0, (this._until - this.now()) / 1e3);
319
+ }
320
+ enter(discard, where, dropped) {
321
+ const t = this.now();
322
+ this._until = t + discard.retryAfterS * 1e3;
323
+ this._inEpisode = true;
324
+ this._accepts = 0;
325
+ if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
326
+ this._lastWarn = t;
327
+ const since = this._drops;
328
+ this._drops = 0;
329
+ warn(
330
+ `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}.` : "")
331
+ );
332
+ } else {
333
+ this._drops += dropped;
334
+ debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
335
+ }
336
+ }
337
+ accepted(where) {
338
+ if (!this._inEpisode) return;
339
+ this._accepts++;
340
+ if (this._accepts < EPISODE_END_ACCEPTS) return;
341
+ this._inEpisode = false;
342
+ this._accepts = 0;
343
+ this._until = -Infinity;
344
+ const t = this.now();
345
+ if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
346
+ this._lastInfo = t;
347
+ info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
348
+ } else {
349
+ debug("Quota pause ended (INFO already written this hour)");
350
+ }
351
+ }
352
+ };
245
353
 
246
354
  // src/exporters/http.ts
247
355
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -282,8 +390,21 @@ var HttpExporter = class {
282
390
  _calls = /* @__PURE__ */ new Map();
283
391
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
284
392
  _acks = /* @__PURE__ */ new WeakMap();
393
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
394
+ noteQuotaDrop(count = 1) {
395
+ this._quota.addDrops(count);
396
+ }
397
+ /** True while the exporter is paused for the monthly event quota (S-1). */
398
+ quotaPaused() {
399
+ return this._quota.paused();
400
+ }
285
401
  /** The spans of each finished call that could not be serialized, by its batch. */
286
402
  _unserializable = /* @__PURE__ */ new WeakMap();
403
+ /**
404
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
405
+ * failure: it does not touch the circuit breaker.
406
+ */
407
+ _quota = new QuotaPause();
287
408
  _circuitOpenUntil = 0;
288
409
  _circuitBreakerThreshold = 5;
289
410
  _circuitBreakerCooldownMs = 6e4;
@@ -375,6 +496,13 @@ var HttpExporter = class {
375
496
  }
376
497
  async _export(spans, call) {
377
498
  const cancelled = call.cancel.signal;
499
+ if (this._quota.paused()) {
500
+ debug(
501
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
502
+ );
503
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
504
+ return "success" /* SUCCESS */;
505
+ }
378
506
  const parts = [];
379
507
  const bad = [];
380
508
  let errorClass = "";
@@ -583,6 +711,11 @@ var HttpExporter = class {
583
711
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
584
712
  );
585
713
  }
714
+ if (ack.quota !== void 0) {
715
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
716
+ } else if (ack.result === "success" /* SUCCESS */) {
717
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
718
+ }
586
719
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
587
720
  if (ack.final) call.noRetry = true;
588
721
  return ack.result;
@@ -649,7 +782,16 @@ async function checkAck(response, spansSent, scrub) {
649
782
  try {
650
783
  payload = await response.json();
651
784
  } catch {
652
- return null;
785
+ payload = void 0;
786
+ }
787
+ const discard = parseJsonDiscard(payload, response.headers);
788
+ if (discard !== null) {
789
+ return {
790
+ result: "success" /* SUCCESS */,
791
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
792
+ final: false,
793
+ quota: discard
794
+ };
653
795
  }
654
796
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
655
797
  return null;