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.
- package/CHANGELOG.md +1236 -0
- package/README.md +18 -0
- package/dist/frameworks/instructor.cjs +163 -6
- package/dist/frameworks/instructor.cjs.map +1 -1
- package/dist/frameworks/instructor.js +163 -6
- package/dist/frameworks/instructor.js.map +1 -1
- package/dist/frameworks/langchain.cjs +163 -6
- package/dist/frameworks/langchain.cjs.map +1 -1
- package/dist/frameworks/langchain.js +163 -6
- package/dist/frameworks/langchain.js.map +1 -1
- package/dist/frameworks/langgraph.cjs +163 -6
- package/dist/frameworks/langgraph.cjs.map +1 -1
- package/dist/frameworks/langgraph.js +163 -6
- package/dist/frameworks/langgraph.js.map +1 -1
- package/dist/frameworks/llamaindex.cjs +163 -6
- package/dist/frameworks/llamaindex.cjs.map +1 -1
- package/dist/frameworks/llamaindex.js +163 -6
- package/dist/frameworks/llamaindex.js.map +1 -1
- package/dist/index.cjs +274 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +274 -18
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic/index.cjs +168 -7
- package/dist/providers/anthropic/index.cjs.map +1 -1
- package/dist/providers/anthropic/index.d.cts +3 -0
- package/dist/providers/anthropic/index.d.ts +3 -0
- package/dist/providers/anthropic/index.js +168 -7
- package/dist/providers/anthropic/index.js.map +1 -1
- package/dist/providers/bedrock/index.cjs +168 -7
- package/dist/providers/bedrock/index.cjs.map +1 -1
- package/dist/providers/bedrock/index.d.cts +3 -0
- package/dist/providers/bedrock/index.d.ts +3 -0
- package/dist/providers/bedrock/index.js +168 -7
- package/dist/providers/bedrock/index.js.map +1 -1
- package/dist/providers/cerebras/index.cjs +168 -7
- package/dist/providers/cerebras/index.cjs.map +1 -1
- package/dist/providers/cerebras/index.d.cts +3 -0
- package/dist/providers/cerebras/index.d.ts +3 -0
- package/dist/providers/cerebras/index.js +168 -7
- package/dist/providers/cerebras/index.js.map +1 -1
- package/dist/providers/cohere/index.cjs +168 -7
- package/dist/providers/cohere/index.cjs.map +1 -1
- package/dist/providers/cohere/index.d.cts +3 -0
- package/dist/providers/cohere/index.d.ts +3 -0
- package/dist/providers/cohere/index.js +168 -7
- package/dist/providers/cohere/index.js.map +1 -1
- package/dist/providers/google/index.cjs +168 -7
- package/dist/providers/google/index.cjs.map +1 -1
- package/dist/providers/google/index.d.cts +3 -0
- package/dist/providers/google/index.d.ts +3 -0
- package/dist/providers/google/index.js +168 -7
- package/dist/providers/google/index.js.map +1 -1
- package/dist/providers/groq/index.cjs +168 -7
- package/dist/providers/groq/index.cjs.map +1 -1
- package/dist/providers/groq/index.d.cts +3 -0
- package/dist/providers/groq/index.d.ts +3 -0
- package/dist/providers/groq/index.js +168 -7
- package/dist/providers/groq/index.js.map +1 -1
- package/dist/providers/huggingface/index.cjs +166 -6
- package/dist/providers/huggingface/index.cjs.map +1 -1
- package/dist/providers/huggingface/index.js +166 -6
- package/dist/providers/huggingface/index.js.map +1 -1
- package/dist/providers/mistral/index.cjs +168 -7
- package/dist/providers/mistral/index.cjs.map +1 -1
- package/dist/providers/mistral/index.d.cts +3 -0
- package/dist/providers/mistral/index.d.ts +3 -0
- package/dist/providers/mistral/index.js +168 -7
- package/dist/providers/mistral/index.js.map +1 -1
- package/dist/providers/ollama/index.cjs +168 -7
- package/dist/providers/ollama/index.cjs.map +1 -1
- package/dist/providers/ollama/index.d.cts +3 -0
- package/dist/providers/ollama/index.d.ts +3 -0
- package/dist/providers/ollama/index.js +168 -7
- package/dist/providers/ollama/index.js.map +1 -1
- package/dist/providers/openai/index.cjs +168 -7
- package/dist/providers/openai/index.cjs.map +1 -1
- package/dist/providers/openai/index.d.cts +3 -0
- package/dist/providers/openai/index.d.ts +3 -0
- package/dist/providers/openai/index.js +168 -7
- package/dist/providers/openai/index.js.map +1 -1
- package/dist/providers/together/index.cjs +168 -7
- package/dist/providers/together/index.cjs.map +1 -1
- package/dist/providers/together/index.d.cts +3 -0
- package/dist/providers/together/index.d.ts +3 -0
- package/dist/providers/together/index.js +168 -7
- package/dist/providers/together/index.js.map +1 -1
- package/dist/providers/vercel-ai/index.cjs +163 -6
- package/dist/providers/vercel-ai/index.cjs.map +1 -1
- package/dist/providers/vercel-ai/index.js +163 -6
- package/dist/providers/vercel-ai/index.js.map +1 -1
- package/package.json +3 -2
|
@@ -36,6 +36,10 @@ function warn(msg) {
|
|
|
36
36
|
process.stderr.write(`[risicare] WARNING: ${msg}
|
|
37
37
|
`);
|
|
38
38
|
}
|
|
39
|
+
function info(msg) {
|
|
40
|
+
process.stderr.write(`[risicare] INFO: ${msg}
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
39
43
|
|
|
40
44
|
// src/utils/text.ts
|
|
41
45
|
function cutCodePoints(text, max) {
|
|
@@ -253,7 +257,111 @@ function runWithContext(overrides, fn) {
|
|
|
253
257
|
}
|
|
254
258
|
|
|
255
259
|
// src/version.ts
|
|
256
|
-
var SDK_VERSION = "0.9.
|
|
260
|
+
var SDK_VERSION = "0.9.2";
|
|
261
|
+
|
|
262
|
+
// src/exporters/quota.ts
|
|
263
|
+
var QUOTA_REASON = "monthly_quota_exceeded";
|
|
264
|
+
var QUOTA_HEADER = "X-Risicare-Quota";
|
|
265
|
+
var QUOTA_HEADER_VALUE = "exceeded";
|
|
266
|
+
var DEFAULT_RETRY_AFTER_S = 300;
|
|
267
|
+
var MIN_RETRY_AFTER_S = 1;
|
|
268
|
+
var MAX_RETRY_AFTER_S = 3600;
|
|
269
|
+
var QUOTA_WARN_WINDOW_MS = 36e5;
|
|
270
|
+
var MAX_RESETS_AT_CHARS = 40;
|
|
271
|
+
function asPositiveCount(v) {
|
|
272
|
+
if (typeof v === "number") return Number.isFinite(v) && v > 0;
|
|
273
|
+
if (typeof v === "string" && /^[0-9]+$/.test(v)) return Number(v) > 0;
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
function boundedRetryAfter(value) {
|
|
277
|
+
let n;
|
|
278
|
+
if (typeof value === "number") n = value;
|
|
279
|
+
else if (typeof value === "string" && value.trim() !== "") n = Number(value);
|
|
280
|
+
else return DEFAULT_RETRY_AFTER_S;
|
|
281
|
+
if (!Number.isFinite(n)) return DEFAULT_RETRY_AFTER_S;
|
|
282
|
+
return Math.min(Math.max(n, MIN_RETRY_AFTER_S), MAX_RETRY_AFTER_S);
|
|
283
|
+
}
|
|
284
|
+
function boundedResetsAt(value) {
|
|
285
|
+
if (typeof value !== "string" || value.length === 0) return null;
|
|
286
|
+
const cut = value.slice(0, MAX_RESETS_AT_CHARS);
|
|
287
|
+
return /^[ -~]*$/.test(cut) ? cut : null;
|
|
288
|
+
}
|
|
289
|
+
function headerSaysExceeded(headers) {
|
|
290
|
+
try {
|
|
291
|
+
const value = headers?.get?.(QUOTA_HEADER);
|
|
292
|
+
return typeof value === "string" && value.trim().toLowerCase() === QUOTA_HEADER_VALUE;
|
|
293
|
+
} catch {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function parseJsonDiscard(payload, headers) {
|
|
298
|
+
const body = payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : {};
|
|
299
|
+
const stored = (asPositiveCount(body.accepted) || asPositiveCount(body.spansAccepted)) === true;
|
|
300
|
+
if (body.reason === QUOTA_REASON || headerSaysExceeded(headers) && !stored) {
|
|
301
|
+
return {
|
|
302
|
+
retryAfterS: boundedRetryAfter(body.retryAfterSeconds),
|
|
303
|
+
resetsAt: boundedResetsAt(body.resetsAt)
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
var EPISODE_END_ACCEPTS = 3;
|
|
309
|
+
var QuotaPause = class {
|
|
310
|
+
constructor(now = () => performance.now()) {
|
|
311
|
+
this.now = now;
|
|
312
|
+
}
|
|
313
|
+
_until = -Infinity;
|
|
314
|
+
_inEpisode = false;
|
|
315
|
+
_accepts = 0;
|
|
316
|
+
_lastWarn = -Infinity;
|
|
317
|
+
_lastInfo = -Infinity;
|
|
318
|
+
/**
|
|
319
|
+
* Spans dropped since the last WARNING: the full queue's (`addDrops`) and the batches that a
|
|
320
|
+
* probe lost while the WARNING was held back. The WARNING carries the number.
|
|
321
|
+
*/
|
|
322
|
+
_drops = 0;
|
|
323
|
+
addDrops(count) {
|
|
324
|
+
this._drops += count;
|
|
325
|
+
}
|
|
326
|
+
paused() {
|
|
327
|
+
return this.now() < this._until;
|
|
328
|
+
}
|
|
329
|
+
remainingS() {
|
|
330
|
+
return Math.max(0, (this._until - this.now()) / 1e3);
|
|
331
|
+
}
|
|
332
|
+
enter(discard, where, dropped) {
|
|
333
|
+
const t = this.now();
|
|
334
|
+
this._until = t + discard.retryAfterS * 1e3;
|
|
335
|
+
this._inEpisode = true;
|
|
336
|
+
this._accepts = 0;
|
|
337
|
+
if (t - this._lastWarn >= QUOTA_WARN_WINDOW_MS) {
|
|
338
|
+
this._lastWarn = t;
|
|
339
|
+
const since = this._drops;
|
|
340
|
+
this._drops = 0;
|
|
341
|
+
warn(
|
|
342
|
+
`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}.` : "")
|
|
343
|
+
);
|
|
344
|
+
} else {
|
|
345
|
+
this._drops += dropped;
|
|
346
|
+
debug(`Quota discard again: ${dropped} span(s); paused for ${Math.round(discard.retryAfterS)} s`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
accepted(where) {
|
|
350
|
+
if (!this._inEpisode) return;
|
|
351
|
+
this._accepts++;
|
|
352
|
+
if (this._accepts < EPISODE_END_ACCEPTS) return;
|
|
353
|
+
this._inEpisode = false;
|
|
354
|
+
this._accepts = 0;
|
|
355
|
+
this._until = -Infinity;
|
|
356
|
+
const t = this.now();
|
|
357
|
+
if (t - this._lastInfo >= QUOTA_WARN_WINDOW_MS) {
|
|
358
|
+
this._lastInfo = t;
|
|
359
|
+
info(`Risicare: ${where} accepts spans again; the export pause for the quota has ended.`);
|
|
360
|
+
} else {
|
|
361
|
+
debug("Quota pause ended (INFO already written this hour)");
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
257
365
|
|
|
258
366
|
// src/exporters/http.ts
|
|
259
367
|
var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
|
|
@@ -294,8 +402,23 @@ var HttpExporter = class {
|
|
|
294
402
|
_calls = /* @__PURE__ */ new Map();
|
|
295
403
|
/** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
|
|
296
404
|
_acks = /* @__PURE__ */ new WeakMap();
|
|
405
|
+
/** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
|
|
406
|
+
_requests = /* @__PURE__ */ new WeakMap();
|
|
407
|
+
/** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
|
|
408
|
+
noteQuotaDrop(count = 1) {
|
|
409
|
+
this._quota.addDrops(count);
|
|
410
|
+
}
|
|
411
|
+
/** True while the exporter is paused for the monthly event quota (S-1). */
|
|
412
|
+
quotaPaused() {
|
|
413
|
+
return this._quota.paused();
|
|
414
|
+
}
|
|
297
415
|
/** The spans of each finished call that could not be serialized, by its batch. */
|
|
298
416
|
_unserializable = /* @__PURE__ */ new WeakMap();
|
|
417
|
+
/**
|
|
418
|
+
* S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
|
|
419
|
+
* failure: it does not touch the circuit breaker.
|
|
420
|
+
*/
|
|
421
|
+
_quota = new QuotaPause();
|
|
299
422
|
_circuitOpenUntil = 0;
|
|
300
423
|
_circuitBreakerThreshold = 5;
|
|
301
424
|
_circuitBreakerCooldownMs = 6e4;
|
|
@@ -332,6 +455,18 @@ var HttpExporter = class {
|
|
|
332
455
|
get attemptsPerExport() {
|
|
333
456
|
return this._maxRetries;
|
|
334
457
|
}
|
|
458
|
+
/**
|
|
459
|
+
* Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
|
|
460
|
+
* it. This is the number that was sent, not the most that the call may send
|
|
461
|
+
* (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
|
|
462
|
+
* breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
|
|
463
|
+
* kept per batch, as `takeAck` keeps the ack.
|
|
464
|
+
*/
|
|
465
|
+
takeAttempts(spans) {
|
|
466
|
+
const n = this._requests.get(spans) ?? null;
|
|
467
|
+
this._requests.delete(spans);
|
|
468
|
+
return n;
|
|
469
|
+
}
|
|
335
470
|
/**
|
|
336
471
|
* Returns the counts of the ack that the `export()` call for `spans` received, and clears
|
|
337
472
|
* them. The answer is kept per batch, not on the instance: two processors that share this
|
|
@@ -376,17 +511,26 @@ var HttpExporter = class {
|
|
|
376
511
|
misroute: null,
|
|
377
512
|
noRetry: false,
|
|
378
513
|
status: null,
|
|
379
|
-
error: null
|
|
514
|
+
error: null,
|
|
515
|
+
attempts: 0
|
|
380
516
|
};
|
|
381
517
|
this._calls.set(spans, call);
|
|
382
518
|
try {
|
|
383
519
|
return await this._export(spans, call);
|
|
384
520
|
} finally {
|
|
385
521
|
this._calls.delete(spans);
|
|
522
|
+
this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
|
|
386
523
|
}
|
|
387
524
|
}
|
|
388
525
|
async _export(spans, call) {
|
|
389
526
|
const cancelled = call.cancel.signal;
|
|
527
|
+
if (this._quota.paused()) {
|
|
528
|
+
debug(
|
|
529
|
+
`Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
|
|
530
|
+
);
|
|
531
|
+
this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
|
|
532
|
+
return "success" /* SUCCESS */;
|
|
533
|
+
}
|
|
390
534
|
const parts = [];
|
|
391
535
|
const bad = [];
|
|
392
536
|
let errorClass = "";
|
|
@@ -415,14 +559,13 @@ var HttpExporter = class {
|
|
|
415
559
|
isHalfOpen = true;
|
|
416
560
|
}
|
|
417
561
|
const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
|
|
418
|
-
let attempts = 0;
|
|
419
562
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
420
563
|
call.refusalAfterMs = null;
|
|
421
564
|
call.misroute = null;
|
|
422
565
|
call.noRetry = false;
|
|
423
566
|
call.status = null;
|
|
424
567
|
call.error = null;
|
|
425
|
-
attempts++;
|
|
568
|
+
call.attempts++;
|
|
426
569
|
const result = await this._sendRequest(payload, parts.length, call, spans);
|
|
427
570
|
if (cancelled.aborted) {
|
|
428
571
|
this._acks.delete(spans);
|
|
@@ -444,7 +587,7 @@ var HttpExporter = class {
|
|
|
444
587
|
}
|
|
445
588
|
if (call.refusalAfterMs !== null) {
|
|
446
589
|
warn(
|
|
447
|
-
`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.`
|
|
590
|
+
`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.`
|
|
448
591
|
);
|
|
449
592
|
return "failure" /* FAILURE */;
|
|
450
593
|
}
|
|
@@ -595,6 +738,11 @@ var HttpExporter = class {
|
|
|
595
738
|
`HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
|
|
596
739
|
);
|
|
597
740
|
}
|
|
741
|
+
if (ack.quota !== void 0) {
|
|
742
|
+
this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
|
|
743
|
+
} else if (ack.result === "success" /* SUCCESS */) {
|
|
744
|
+
this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
|
|
745
|
+
}
|
|
598
746
|
if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
|
|
599
747
|
if (ack.final) call.noRetry = true;
|
|
600
748
|
return ack.result;
|
|
@@ -661,7 +809,16 @@ async function checkAck(response, spansSent, scrub) {
|
|
|
661
809
|
try {
|
|
662
810
|
payload = await response.json();
|
|
663
811
|
} catch {
|
|
664
|
-
|
|
812
|
+
payload = void 0;
|
|
813
|
+
}
|
|
814
|
+
const discard = parseJsonDiscard(payload, response.headers);
|
|
815
|
+
if (discard !== null) {
|
|
816
|
+
return {
|
|
817
|
+
result: "success" /* SUCCESS */,
|
|
818
|
+
counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
|
|
819
|
+
final: false,
|
|
820
|
+
quota: discard
|
|
821
|
+
};
|
|
665
822
|
}
|
|
666
823
|
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
667
824
|
return null;
|