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.
Files changed (93) hide show
  1. package/CHANGELOG.md +1236 -0
  2. package/README.md +18 -0
  3. package/dist/frameworks/instructor.cjs +163 -6
  4. package/dist/frameworks/instructor.cjs.map +1 -1
  5. package/dist/frameworks/instructor.js +163 -6
  6. package/dist/frameworks/instructor.js.map +1 -1
  7. package/dist/frameworks/langchain.cjs +163 -6
  8. package/dist/frameworks/langchain.cjs.map +1 -1
  9. package/dist/frameworks/langchain.js +163 -6
  10. package/dist/frameworks/langchain.js.map +1 -1
  11. package/dist/frameworks/langgraph.cjs +163 -6
  12. package/dist/frameworks/langgraph.cjs.map +1 -1
  13. package/dist/frameworks/langgraph.js +163 -6
  14. package/dist/frameworks/langgraph.js.map +1 -1
  15. package/dist/frameworks/llamaindex.cjs +163 -6
  16. package/dist/frameworks/llamaindex.cjs.map +1 -1
  17. package/dist/frameworks/llamaindex.js +163 -6
  18. package/dist/frameworks/llamaindex.js.map +1 -1
  19. package/dist/index.cjs +274 -18
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +7 -1
  22. package/dist/index.d.ts +7 -1
  23. package/dist/index.js +274 -18
  24. package/dist/index.js.map +1 -1
  25. package/dist/providers/anthropic/index.cjs +168 -7
  26. package/dist/providers/anthropic/index.cjs.map +1 -1
  27. package/dist/providers/anthropic/index.d.cts +3 -0
  28. package/dist/providers/anthropic/index.d.ts +3 -0
  29. package/dist/providers/anthropic/index.js +168 -7
  30. package/dist/providers/anthropic/index.js.map +1 -1
  31. package/dist/providers/bedrock/index.cjs +168 -7
  32. package/dist/providers/bedrock/index.cjs.map +1 -1
  33. package/dist/providers/bedrock/index.d.cts +3 -0
  34. package/dist/providers/bedrock/index.d.ts +3 -0
  35. package/dist/providers/bedrock/index.js +168 -7
  36. package/dist/providers/bedrock/index.js.map +1 -1
  37. package/dist/providers/cerebras/index.cjs +168 -7
  38. package/dist/providers/cerebras/index.cjs.map +1 -1
  39. package/dist/providers/cerebras/index.d.cts +3 -0
  40. package/dist/providers/cerebras/index.d.ts +3 -0
  41. package/dist/providers/cerebras/index.js +168 -7
  42. package/dist/providers/cerebras/index.js.map +1 -1
  43. package/dist/providers/cohere/index.cjs +168 -7
  44. package/dist/providers/cohere/index.cjs.map +1 -1
  45. package/dist/providers/cohere/index.d.cts +3 -0
  46. package/dist/providers/cohere/index.d.ts +3 -0
  47. package/dist/providers/cohere/index.js +168 -7
  48. package/dist/providers/cohere/index.js.map +1 -1
  49. package/dist/providers/google/index.cjs +168 -7
  50. package/dist/providers/google/index.cjs.map +1 -1
  51. package/dist/providers/google/index.d.cts +3 -0
  52. package/dist/providers/google/index.d.ts +3 -0
  53. package/dist/providers/google/index.js +168 -7
  54. package/dist/providers/google/index.js.map +1 -1
  55. package/dist/providers/groq/index.cjs +168 -7
  56. package/dist/providers/groq/index.cjs.map +1 -1
  57. package/dist/providers/groq/index.d.cts +3 -0
  58. package/dist/providers/groq/index.d.ts +3 -0
  59. package/dist/providers/groq/index.js +168 -7
  60. package/dist/providers/groq/index.js.map +1 -1
  61. package/dist/providers/huggingface/index.cjs +166 -6
  62. package/dist/providers/huggingface/index.cjs.map +1 -1
  63. package/dist/providers/huggingface/index.js +166 -6
  64. package/dist/providers/huggingface/index.js.map +1 -1
  65. package/dist/providers/mistral/index.cjs +168 -7
  66. package/dist/providers/mistral/index.cjs.map +1 -1
  67. package/dist/providers/mistral/index.d.cts +3 -0
  68. package/dist/providers/mistral/index.d.ts +3 -0
  69. package/dist/providers/mistral/index.js +168 -7
  70. package/dist/providers/mistral/index.js.map +1 -1
  71. package/dist/providers/ollama/index.cjs +168 -7
  72. package/dist/providers/ollama/index.cjs.map +1 -1
  73. package/dist/providers/ollama/index.d.cts +3 -0
  74. package/dist/providers/ollama/index.d.ts +3 -0
  75. package/dist/providers/ollama/index.js +168 -7
  76. package/dist/providers/ollama/index.js.map +1 -1
  77. package/dist/providers/openai/index.cjs +168 -7
  78. package/dist/providers/openai/index.cjs.map +1 -1
  79. package/dist/providers/openai/index.d.cts +3 -0
  80. package/dist/providers/openai/index.d.ts +3 -0
  81. package/dist/providers/openai/index.js +168 -7
  82. package/dist/providers/openai/index.js.map +1 -1
  83. package/dist/providers/together/index.cjs +168 -7
  84. package/dist/providers/together/index.cjs.map +1 -1
  85. package/dist/providers/together/index.d.cts +3 -0
  86. package/dist/providers/together/index.d.ts +3 -0
  87. package/dist/providers/together/index.js +168 -7
  88. package/dist/providers/together/index.js.map +1 -1
  89. package/dist/providers/vercel-ai/index.cjs +163 -6
  90. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  91. package/dist/providers/vercel-ai/index.js +163 -6
  92. package/dist/providers/vercel-ai/index.js.map +1 -1
  93. package/package.json +3 -2
@@ -41,6 +41,9 @@ declare function deriveModelFamily(modelId: string): string;
41
41
  * Non-AI commands (ListFoundationModels, etc.) pass through unmodified.
42
42
  * The original client is NOT modified.
43
43
  *
44
+ * Patching a proxy that this function returned gives back the same proxy, so the
45
+ * second patch adds no second span.
46
+ *
44
47
  * @param client - A BedrockRuntimeClient instance
45
48
  * @returns A proxied client with automatic tracing
46
49
  */
@@ -41,6 +41,9 @@ declare function deriveModelFamily(modelId: string): string;
41
41
  * Non-AI commands (ListFoundationModels, etc.) pass through unmodified.
42
42
  * The original client is NOT modified.
43
43
  *
44
+ * Patching a proxy that this function returned gives back the same proxy, so the
45
+ * second patch adds no second span.
46
+ *
44
47
  * @param client - A BedrockRuntimeClient instance
45
48
  * @returns A proxied client with automatic tracing
46
49
  */
@@ -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.9.0";
248
+ var SDK_VERSION = "0.9.2";
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,23 @@ 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 requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
394
+ _requests = /* @__PURE__ */ new WeakMap();
395
+ /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
396
+ noteQuotaDrop(count = 1) {
397
+ this._quota.addDrops(count);
398
+ }
399
+ /** True while the exporter is paused for the monthly event quota (S-1). */
400
+ quotaPaused() {
401
+ return this._quota.paused();
402
+ }
285
403
  /** The spans of each finished call that could not be serialized, by its batch. */
286
404
  _unserializable = /* @__PURE__ */ new WeakMap();
405
+ /**
406
+ * S-1: the paused state after a monthly-quota discard (see `quota.ts`). A discard is not a
407
+ * failure: it does not touch the circuit breaker.
408
+ */
409
+ _quota = new QuotaPause();
287
410
  _circuitOpenUntil = 0;
288
411
  _circuitBreakerThreshold = 5;
289
412
  _circuitBreakerCooldownMs = 6e4;
@@ -320,6 +443,18 @@ var HttpExporter = class {
320
443
  get attemptsPerExport() {
321
444
  return this._maxRetries;
322
445
  }
446
+ /**
447
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
448
+ * it. This is the number that was sent, not the most that the call may send
449
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
450
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
451
+ * kept per batch, as `takeAck` keeps the ack.
452
+ */
453
+ takeAttempts(spans) {
454
+ const n = this._requests.get(spans) ?? null;
455
+ this._requests.delete(spans);
456
+ return n;
457
+ }
323
458
  /**
324
459
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
325
460
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -364,17 +499,26 @@ var HttpExporter = class {
364
499
  misroute: null,
365
500
  noRetry: false,
366
501
  status: null,
367
- error: null
502
+ error: null,
503
+ attempts: 0
368
504
  };
369
505
  this._calls.set(spans, call);
370
506
  try {
371
507
  return await this._export(spans, call);
372
508
  } finally {
373
509
  this._calls.delete(spans);
510
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
374
511
  }
375
512
  }
376
513
  async _export(spans, call) {
377
514
  const cancelled = call.cancel.signal;
515
+ if (this._quota.paused()) {
516
+ debug(
517
+ `Export skipped: monthly quota pause, ${Math.round(this._quota.remainingS())} s left; ${spans.length} span(s) dropped`
518
+ );
519
+ this._acks.set(spans, { accepted: 0, rejected: 0, quotaDropped: spans.length });
520
+ return "success" /* SUCCESS */;
521
+ }
378
522
  const parts = [];
379
523
  const bad = [];
380
524
  let errorClass = "";
@@ -403,14 +547,13 @@ var HttpExporter = class {
403
547
  isHalfOpen = true;
404
548
  }
405
549
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
406
- let attempts = 0;
407
550
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
408
551
  call.refusalAfterMs = null;
409
552
  call.misroute = null;
410
553
  call.noRetry = false;
411
554
  call.status = null;
412
555
  call.error = null;
413
- attempts++;
556
+ call.attempts++;
414
557
  const result = await this._sendRequest(payload, parts.length, call, spans);
415
558
  if (cancelled.aborted) {
416
559
  this._acks.delete(spans);
@@ -432,7 +575,7 @@ var HttpExporter = class {
432
575
  }
433
576
  if (call.refusalAfterMs !== null) {
434
577
  warn(
435
- `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.`
578
+ `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.`
436
579
  );
437
580
  return "failure" /* FAILURE */;
438
581
  }
@@ -583,6 +726,11 @@ var HttpExporter = class {
583
726
  `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
584
727
  );
585
728
  }
729
+ if (ack.quota !== void 0) {
730
+ this._quota.enter(ack.quota, `The ingest endpoint ${this._safeEndpoint}`, spansSent);
731
+ } else if (ack.result === "success" /* SUCCESS */) {
732
+ this._quota.accepted(`the ingest endpoint ${this._safeEndpoint}`);
733
+ }
586
734
  if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
587
735
  if (ack.final) call.noRetry = true;
588
736
  return ack.result;
@@ -649,7 +797,16 @@ async function checkAck(response, spansSent, scrub) {
649
797
  try {
650
798
  payload = await response.json();
651
799
  } catch {
652
- return null;
800
+ payload = void 0;
801
+ }
802
+ const discard = parseJsonDiscard(payload, response.headers);
803
+ if (discard !== null) {
804
+ return {
805
+ result: "success" /* SUCCESS */,
806
+ counts: { accepted: 0, rejected: 0, quotaDropped: spansSent },
807
+ final: false,
808
+ quota: discard
809
+ };
653
810
  }
654
811
  if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
655
812
  return null;
@@ -1173,9 +1330,11 @@ function createSendProxy(originalSend) {
1173
1330
  );
1174
1331
  };
1175
1332
  }
1333
+ var PATCHED = /* @__PURE__ */ new WeakSet();
1176
1334
  function patchBedrock(client) {
1177
1335
  assertClient("patchBedrock", client);
1178
- return new Proxy(client, {
1336
+ if (PATCHED.has(client)) return client;
1337
+ const proxy = new Proxy(client, {
1179
1338
  get(target, prop, receiver) {
1180
1339
  const value = Reflect.get(target, prop, receiver);
1181
1340
  if (prop === "send" && typeof value === "function") {
@@ -1184,6 +1343,8 @@ function patchBedrock(client) {
1184
1343
  return value;
1185
1344
  }
1186
1345
  });
1346
+ PATCHED.add(proxy);
1347
+ return proxy;
1187
1348
  }
1188
1349
  export {
1189
1350
  deriveModelFamily,