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
|
@@ -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.9.
|
|
234
|
+
var SDK_VERSION = "0.9.2";
|
|
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,23 @@ 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 requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
|
|
380
|
+
_requests = /* @__PURE__ */ new WeakMap();
|
|
381
|
+
/** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
|
|
382
|
+
noteQuotaDrop(count = 1) {
|
|
383
|
+
this._quota.addDrops(count);
|
|
384
|
+
}
|
|
385
|
+
/** True while the exporter is paused for the monthly event quota (S-1). */
|
|
386
|
+
quotaPaused() {
|
|
387
|
+
return this._quota.paused();
|
|
388
|
+
}
|
|
271
389
|
/** The spans of each finished call that could not be serialized, by its batch. */
|
|
272
390
|
_unserializable = /* @__PURE__ */ new WeakMap();
|
|
391
|
+
/**
|
|
392
|
+
* S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
|
|
393
|
+
* failure: it does not touch the circuit breaker.
|
|
394
|
+
*/
|
|
395
|
+
_quota = new QuotaPause();
|
|
273
396
|
_circuitOpenUntil = 0;
|
|
274
397
|
_circuitBreakerThreshold = 5;
|
|
275
398
|
_circuitBreakerCooldownMs = 6e4;
|
|
@@ -306,6 +429,18 @@ var HttpExporter = class {
|
|
|
306
429
|
get attemptsPerExport() {
|
|
307
430
|
return this._maxRetries;
|
|
308
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
|
|
434
|
+
* it. This is the number that was sent, not the most that the call may send
|
|
435
|
+
* (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
|
|
436
|
+
* breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
|
|
437
|
+
* kept per batch, as `takeAck` keeps the ack.
|
|
438
|
+
*/
|
|
439
|
+
takeAttempts(spans) {
|
|
440
|
+
const n = this._requests.get(spans) ?? null;
|
|
441
|
+
this._requests.delete(spans);
|
|
442
|
+
return n;
|
|
443
|
+
}
|
|
309
444
|
/**
|
|
310
445
|
* Returns the counts of the ack that the `export()` call for `spans` received, and clears
|
|
311
446
|
* them. The answer is kept per batch, not on the instance: two processors that share this
|
|
@@ -350,17 +485,26 @@ var HttpExporter = class {
|
|
|
350
485
|
misroute: null,
|
|
351
486
|
noRetry: false,
|
|
352
487
|
status: null,
|
|
353
|
-
error: null
|
|
488
|
+
error: null,
|
|
489
|
+
attempts: 0
|
|
354
490
|
};
|
|
355
491
|
this._calls.set(spans, call);
|
|
356
492
|
try {
|
|
357
493
|
return await this._export(spans, call);
|
|
358
494
|
} finally {
|
|
359
495
|
this._calls.delete(spans);
|
|
496
|
+
this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
|
|
360
497
|
}
|
|
361
498
|
}
|
|
362
499
|
async _export(spans, call) {
|
|
363
500
|
const cancelled = call.cancel.signal;
|
|
501
|
+
if (this._quota.paused()) {
|
|
502
|
+
debug(
|
|
503
|
+
`Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
|
|
504
|
+
);
|
|
505
|
+
this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
|
|
506
|
+
return "success" /* SUCCESS */;
|
|
507
|
+
}
|
|
364
508
|
const parts = [];
|
|
365
509
|
const bad = [];
|
|
366
510
|
let errorClass = "";
|
|
@@ -389,14 +533,13 @@ var HttpExporter = class {
|
|
|
389
533
|
isHalfOpen = true;
|
|
390
534
|
}
|
|
391
535
|
const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
|
|
392
|
-
let attempts = 0;
|
|
393
536
|
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
394
537
|
call.refusalAfterMs = null;
|
|
395
538
|
call.misroute = null;
|
|
396
539
|
call.noRetry = false;
|
|
397
540
|
call.status = null;
|
|
398
541
|
call.error = null;
|
|
399
|
-
attempts++;
|
|
542
|
+
call.attempts++;
|
|
400
543
|
const result = await this._sendRequest(payload, parts.length, call, spans);
|
|
401
544
|
if (cancelled.aborted) {
|
|
402
545
|
this._acks.delete(spans);
|
|
@@ -418,7 +561,7 @@ var HttpExporter = class {
|
|
|
418
561
|
}
|
|
419
562
|
if (call.refusalAfterMs !== null) {
|
|
420
563
|
warn(
|
|
421
|
-
`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.`
|
|
564
|
+
`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.`
|
|
422
565
|
);
|
|
423
566
|
return "failure" /* FAILURE */;
|
|
424
567
|
}
|
|
@@ -569,6 +712,11 @@ var HttpExporter = class {
|
|
|
569
712
|
`HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
|
|
570
713
|
);
|
|
571
714
|
}
|
|
715
|
+
if (ack.quota !== void 0) {
|
|
716
|
+
this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
|
|
717
|
+
} else if (ack.result === "success" /* SUCCESS */) {
|
|
718
|
+
this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
|
|
719
|
+
}
|
|
572
720
|
if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
|
|
573
721
|
if (ack.final) call.noRetry = true;
|
|
574
722
|
return ack.result;
|
|
@@ -635,7 +783,16 @@ async function checkAck(response, spansSent, scrub) {
|
|
|
635
783
|
try {
|
|
636
784
|
payload = await response.json();
|
|
637
785
|
} catch {
|
|
638
|
-
|
|
786
|
+
payload = void 0;
|
|
787
|
+
}
|
|
788
|
+
const discard = parseJsonDiscard(payload, response.headers);
|
|
789
|
+
if (discard !== null) {
|
|
790
|
+
return {
|
|
791
|
+
result: "success" /* SUCCESS */,
|
|
792
|
+
counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
|
|
793
|
+
final: false,
|
|
794
|
+
quota: discard
|
|
795
|
+
};
|
|
639
796
|
}
|
|
640
797
|
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
|
|
641
798
|
return null;
|