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.
- package/README.md +98 -21
- package/dist/frameworks/instructor.cjs +144 -2
- package/dist/frameworks/instructor.cjs.map +1 -1
- package/dist/frameworks/instructor.js +144 -2
- package/dist/frameworks/instructor.js.map +1 -1
- package/dist/frameworks/langchain.cjs +144 -2
- package/dist/frameworks/langchain.cjs.map +1 -1
- package/dist/frameworks/langchain.js +144 -2
- package/dist/frameworks/langchain.js.map +1 -1
- package/dist/frameworks/langgraph.cjs +144 -2
- package/dist/frameworks/langgraph.cjs.map +1 -1
- package/dist/frameworks/langgraph.js +144 -2
- package/dist/frameworks/langgraph.js.map +1 -1
- package/dist/frameworks/llamaindex.cjs +144 -2
- package/dist/frameworks/llamaindex.cjs.map +1 -1
- package/dist/frameworks/llamaindex.js +144 -2
- package/dist/frameworks/llamaindex.js.map +1 -1
- package/dist/index.cjs +329 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +29 -9
- package/dist/index.d.ts +29 -9
- package/dist/index.js +329 -67
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic/index.cjs +144 -2
- package/dist/providers/anthropic/index.cjs.map +1 -1
- package/dist/providers/anthropic/index.js +144 -2
- package/dist/providers/anthropic/index.js.map +1 -1
- package/dist/providers/bedrock/index.cjs +144 -2
- package/dist/providers/bedrock/index.cjs.map +1 -1
- package/dist/providers/bedrock/index.js +144 -2
- package/dist/providers/bedrock/index.js.map +1 -1
- package/dist/providers/cerebras/index.cjs +144 -2
- package/dist/providers/cerebras/index.cjs.map +1 -1
- package/dist/providers/cerebras/index.js +144 -2
- package/dist/providers/cerebras/index.js.map +1 -1
- package/dist/providers/cohere/index.cjs +144 -2
- package/dist/providers/cohere/index.cjs.map +1 -1
- package/dist/providers/cohere/index.js +144 -2
- package/dist/providers/cohere/index.js.map +1 -1
- package/dist/providers/google/index.cjs +144 -2
- package/dist/providers/google/index.cjs.map +1 -1
- package/dist/providers/google/index.js +144 -2
- package/dist/providers/google/index.js.map +1 -1
- package/dist/providers/groq/index.cjs +144 -2
- package/dist/providers/groq/index.cjs.map +1 -1
- package/dist/providers/groq/index.js +144 -2
- package/dist/providers/groq/index.js.map +1 -1
- package/dist/providers/huggingface/index.cjs +144 -2
- package/dist/providers/huggingface/index.cjs.map +1 -1
- package/dist/providers/huggingface/index.js +144 -2
- package/dist/providers/huggingface/index.js.map +1 -1
- package/dist/providers/mistral/index.cjs +144 -2
- package/dist/providers/mistral/index.cjs.map +1 -1
- package/dist/providers/mistral/index.js +144 -2
- package/dist/providers/mistral/index.js.map +1 -1
- package/dist/providers/ollama/index.cjs +144 -2
- package/dist/providers/ollama/index.cjs.map +1 -1
- package/dist/providers/ollama/index.js +144 -2
- package/dist/providers/ollama/index.js.map +1 -1
- package/dist/providers/openai/index.cjs +144 -2
- package/dist/providers/openai/index.cjs.map +1 -1
- package/dist/providers/openai/index.js +144 -2
- package/dist/providers/openai/index.js.map +1 -1
- package/dist/providers/together/index.cjs +144 -2
- package/dist/providers/together/index.cjs.map +1 -1
- package/dist/providers/together/index.js +144 -2
- package/dist/providers/together/index.js.map +1 -1
- package/dist/providers/vercel-ai/index.cjs +144 -2
- package/dist/providers/vercel-ai/index.cjs.map +1 -1
- package/dist/providers/vercel-ai/index.js +144 -2
- package/dist/providers/vercel-ai/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -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.
|
|
463
|
+
var SDK_VERSION = "0.9.1";
|
|
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 cut2 = value.slice(0, MAX_RESETS_AT_CHARS);
|
|
490
|
+
return /^[ -~]*$/.test(cut2) ? cut2 : 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,21 @@ 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 batch processor dropped span(s) from a full queue in the quota pause (S-1). */
|
|
609
|
+
noteQuotaDrop(count = 1) {
|
|
610
|
+
this._quota.addDrops(count);
|
|
611
|
+
}
|
|
612
|
+
/** True while the exporter is paused for the monthly event quota (S-1). */
|
|
613
|
+
quotaPaused() {
|
|
614
|
+
return this._quota.paused();
|
|
615
|
+
}
|
|
500
616
|
/** The spans of each finished call that could not be serialized, by its batch. */
|
|
501
617
|
_unserializable = /* @__PURE__ */ new WeakMap();
|
|
618
|
+
/**
|
|
619
|
+
* S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
|
|
620
|
+
* failure: it does not touch the circuit breaker.
|
|
621
|
+
*/
|
|
622
|
+
_quota = new QuotaPause();
|
|
502
623
|
_circuitOpenUntil = 0;
|
|
503
624
|
_circuitBreakerThreshold = 5;
|
|
504
625
|
_circuitBreakerCooldownMs = 6e4;
|
|
@@ -590,6 +711,13 @@ var HttpExporter = class {
|
|
|
590
711
|
}
|
|
591
712
|
async _export(spans, call) {
|
|
592
713
|
const cancelled = call.cancel.signal;
|
|
714
|
+
if (this._quota.paused()) {
|
|
715
|
+
debug(
|
|
716
|
+
`Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
|
|
717
|
+
);
|
|
718
|
+
this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
|
|
719
|
+
return "success" /* SUCCESS */;
|
|
720
|
+
}
|
|
593
721
|
const parts = [];
|
|
594
722
|
const bad = [];
|
|
595
723
|
let errorClass = "";
|
|
@@ -798,6 +926,11 @@ var HttpExporter = class {
|
|
|
798
926
|
`HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
|
|
799
927
|
);
|
|
800
928
|
}
|
|
929
|
+
if (ack.quota !== void 0) {
|
|
930
|
+
this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
|
|
931
|
+
} else if (ack.result === "success" /* SUCCESS */) {
|
|
932
|
+
this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
|
|
933
|
+
}
|
|
801
934
|
if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
|
|
802
935
|
if (ack.final) call.noRetry = true;
|
|
803
936
|
return ack.result;
|
|
@@ -864,7 +997,16 @@ async function checkAck(response, spansSent, scrub) {
|
|
|
864
997
|
try {
|
|
865
998
|
payload = await response.json();
|
|
866
999
|
} catch {
|
|
867
|
-
|
|
1000
|
+
payload = void 0;
|
|
1001
|
+
}
|
|
1002
|
+
const discard = parseJsonDiscard(payload, response.headers);
|
|
1003
|
+
if (discard !== null) {
|
|
1004
|
+
return {
|
|
1005
|
+
result: "success" /* SUCCESS */,
|
|
1006
|
+
counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
|
|
1007
|
+
final: false,
|
|
1008
|
+
quota: discard
|
|
1009
|
+
};
|
|
868
1010
|
}
|
|
869
1011
|
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
870
1012
|
return null;
|