risicare 0.9.1 → 0.10.0

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 +1419 -0
  2. package/README.md +37 -19
  3. package/dist/frameworks/instructor.cjs +65 -5
  4. package/dist/frameworks/instructor.cjs.map +1 -1
  5. package/dist/frameworks/instructor.js +65 -5
  6. package/dist/frameworks/instructor.js.map +1 -1
  7. package/dist/frameworks/langchain.cjs +65 -5
  8. package/dist/frameworks/langchain.cjs.map +1 -1
  9. package/dist/frameworks/langchain.js +65 -5
  10. package/dist/frameworks/langchain.js.map +1 -1
  11. package/dist/frameworks/langgraph.cjs +65 -5
  12. package/dist/frameworks/langgraph.cjs.map +1 -1
  13. package/dist/frameworks/langgraph.js +65 -5
  14. package/dist/frameworks/langgraph.js.map +1 -1
  15. package/dist/frameworks/llamaindex.cjs +65 -5
  16. package/dist/frameworks/llamaindex.cjs.map +1 -1
  17. package/dist/frameworks/llamaindex.js +65 -5
  18. package/dist/frameworks/llamaindex.js.map +1 -1
  19. package/dist/index.cjs +405 -27
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +14 -5
  22. package/dist/index.d.ts +14 -5
  23. package/dist/index.js +405 -27
  24. package/dist/index.js.map +1 -1
  25. package/dist/providers/anthropic/index.cjs +141 -13
  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 +141 -13
  30. package/dist/providers/anthropic/index.js.map +1 -1
  31. package/dist/providers/bedrock/index.cjs +88 -9
  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 +88 -9
  36. package/dist/providers/bedrock/index.js.map +1 -1
  37. package/dist/providers/cerebras/index.cjs +88 -9
  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 +88 -9
  42. package/dist/providers/cerebras/index.js.map +1 -1
  43. package/dist/providers/cohere/index.cjs +88 -9
  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 +88 -9
  48. package/dist/providers/cohere/index.js.map +1 -1
  49. package/dist/providers/google/index.cjs +88 -9
  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 +88 -9
  54. package/dist/providers/google/index.js.map +1 -1
  55. package/dist/providers/groq/index.cjs +88 -9
  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 +88 -9
  60. package/dist/providers/groq/index.js.map +1 -1
  61. package/dist/providers/huggingface/index.cjs +86 -8
  62. package/dist/providers/huggingface/index.cjs.map +1 -1
  63. package/dist/providers/huggingface/index.js +86 -8
  64. package/dist/providers/huggingface/index.js.map +1 -1
  65. package/dist/providers/mistral/index.cjs +88 -9
  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 +88 -9
  70. package/dist/providers/mistral/index.js.map +1 -1
  71. package/dist/providers/ollama/index.cjs +88 -9
  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 +88 -9
  76. package/dist/providers/ollama/index.js.map +1 -1
  77. package/dist/providers/openai/index.cjs +126 -11
  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 +126 -11
  82. package/dist/providers/openai/index.js.map +1 -1
  83. package/dist/providers/together/index.cjs +88 -9
  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 +88 -9
  88. package/dist/providers/together/index.js.map +1 -1
  89. package/dist/providers/vercel-ai/index.cjs +67 -7
  90. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  91. package/dist/providers/vercel-ai/index.js +67 -7
  92. package/dist/providers/vercel-ai/index.js.map +1 -1
  93. package/package.json +3 -2
package/dist/index.cjs CHANGED
@@ -367,7 +367,49 @@ var CONTENT_KEYS_EXACT = /* @__PURE__ */ new Set([
367
367
  "audio.transcript",
368
368
  "traceloop.entity.input",
369
369
  // OpenLLMetry
370
- "traceloop.entity.output"
370
+ "traceloop.entity.output",
371
+ // Gateway corpus v2 (R41): the content keys of more frameworks. The same 75
372
+ // keys as `CONTENT_KEYS_EXACT` in the gateway's `content_control.rs`.
373
+ "agent.backstory",
374
+ // OpenInference
375
+ "agent.goal",
376
+ "llm.available_functions",
377
+ "llm.tools",
378
+ "ai.prompt",
379
+ // Vercel AI SDK
380
+ "ai.prompt.messages",
381
+ "ai.prompt.tools",
382
+ "ai.response.files",
383
+ "ai.response.object",
384
+ "ai.response.reasoning",
385
+ "ai.response.text",
386
+ "ai.response.toolCalls",
387
+ "ai.result.object",
388
+ // the older Vercel names
389
+ "ai.result.text",
390
+ "ai.result.toolCalls",
391
+ "ai.toolCall.args",
392
+ "ai.toolCall.result",
393
+ "ai.value",
394
+ // the text of an embedding call
395
+ "ai.values",
396
+ "final_result",
397
+ // Pydantic AI
398
+ "model_request_parameters",
399
+ "pydantic_ai.all_messages",
400
+ "tool_arguments",
401
+ "tool_response",
402
+ "gcp.vertex.agent.data",
403
+ // Google ADK
404
+ "gcp.vertex.agent.llm_request",
405
+ "gcp.vertex.agent.llm_response",
406
+ "gcp.vertex.agent.tool_call_args",
407
+ "gcp.vertex.agent.tool_response",
408
+ "gen_ai.task.input",
409
+ // OpenLLMetry
410
+ "gen_ai.task.output",
411
+ "sk.available_functions"
412
+ // Semantic Kernel
371
413
  ]);
372
414
  var CONTENT_LAST_SEGMENT_IN_CONTEXT = /* @__PURE__ */ new Map([
373
415
  ["function_call_arguments_json", /* @__PURE__ */ new Set()],
@@ -375,7 +417,15 @@ var CONTENT_LAST_SEGMENT_IN_CONTEXT = /* @__PURE__ */ new Map([
375
417
  ["data", /* @__PURE__ */ new Set(["message_content"])],
376
418
  ["url", /* @__PURE__ */ new Set(["image"])],
377
419
  // `image.url` can hold base64 data
378
- ["metadata", /* @__PURE__ */ new Set(["document", "documents"])]
420
+ ["metadata", /* @__PURE__ */ new Set(["document", "documents"])],
421
+ // Gateway corpus v2: a list of tool definitions. The `description` of one tool
422
+ // on a tool span (`tool.description`) is not content; the descriptions in a list are.
423
+ ["description", /* @__PURE__ */ new Set(["definitions", "functions", "tools"])],
424
+ ["json_schema", /* @__PURE__ */ new Set(["tool", "tools"])],
425
+ ["parameters", /* @__PURE__ */ new Set(["definitions", "functions", "tool", "tools"])],
426
+ // Gateway corpus v2: the Mastra span I/O, `mastra.<span type>.input|output`.
427
+ ["input", /* @__PURE__ */ new Set(["mastra"])],
428
+ ["output", /* @__PURE__ */ new Set(["mastra"])]
379
429
  ]);
380
430
  var CONTENT_CONTAINER_SEGMENTS = /* @__PURE__ */ new Set([
381
431
  "message",
@@ -388,8 +438,23 @@ var CONTENT_CONTAINER_SEGMENTS = /* @__PURE__ */ new Set([
388
438
  "documents",
389
439
  "tool_call",
390
440
  "tool_calls",
391
- "function"
441
+ "function",
442
+ // Gateway corpus v2: LiteLLM `gen_ai.completion.{i}.function_call.arguments` and
443
+ // OpenInference `llm.prompts.{i}.prompt.text`.
444
+ "function_call",
445
+ "prompt",
446
+ "prompts"
447
+ ]);
448
+ var CONTENT_FIRST_SEGMENTS = /* @__PURE__ */ new Set(["crewai"]);
449
+ var CONTENT_EVENT_NAMES = /* @__PURE__ */ new Set([
450
+ "gen_ai.assistant.message",
451
+ "gen_ai.choice",
452
+ "gen_ai.system.message",
453
+ "gen_ai.tool.message",
454
+ "gen_ai.user.message"
392
455
  ]);
456
+ var CONTENT_EVENT_PREFIX = "gen_ai.content.";
457
+ var CONTENT_EVENT_KEEP_KEYS = /* @__PURE__ */ new Set(["gen_ai.provider.name", "gen_ai.system"]);
393
458
  var CONTENT_KEYS_KEEP_EXACT = /* @__PURE__ */ new Set([
394
459
  "gen_ai.response.content_type",
395
460
  "gen_ai.response.content_blocks",
@@ -407,10 +472,60 @@ var KEEP_LAST_SEGMENTS = /* @__PURE__ */ new Set([
407
472
  "index",
408
473
  "model",
409
474
  "finish_reason",
410
- "mime_type"
475
+ "mime_type",
476
+ // Gateway v2 at risicare-ingestion main (PR 94): a model name
477
+ // (`crewai.agent.llm`) and the crew process type are not content.
478
+ "llm",
479
+ "process"
411
480
  ]);
412
481
  var KEEP_LAST_SUFFIXES = ["_length", "_tokens", "_count"];
482
+ var CONTENT_IN_JSON_OBJECT = /* @__PURE__ */ new Map([
483
+ [
484
+ "llm.invocation_parameters",
485
+ /* @__PURE__ */ new Set([
486
+ "additionalinstructions",
487
+ // OpenAI assistants run
488
+ "chathistory",
489
+ // Cohere
490
+ "contents",
491
+ // Gemini
492
+ "documents",
493
+ // Cohere RAG
494
+ "functions",
495
+ "input",
496
+ "inputtext",
497
+ // Bedrock Titan
498
+ "instructions",
499
+ "message",
500
+ // Cohere
501
+ "messages",
502
+ "preamble",
503
+ // Cohere
504
+ "prediction",
505
+ // OpenAI predicted output
506
+ "prompt",
507
+ "system",
508
+ // Anthropic
509
+ "systeminstruction",
510
+ // Gemini
511
+ "texts",
512
+ // Cohere embed
513
+ "toolconfig",
514
+ // Gemini
515
+ "tools"
516
+ ])
517
+ ]
518
+ ]);
519
+ var VALUE_RULE_PREFIXES = new Set(
520
+ [...CONTENT_IN_JSON_OBJECT.keys()].flatMap((key) => {
521
+ const segments = key.split(".");
522
+ return segments.slice(1).map((_, i) => segments.slice(0, i + 1).join("."));
523
+ })
524
+ );
413
525
  var CONTENT_OMITTED_MARKER = "<risicare:content-omitted>";
526
+ function hasKeepLastSegment(last) {
527
+ return KEEP_LAST_SEGMENTS.has(last) || KEEP_LAST_SUFFIXES.some((x) => last.endsWith(x));
528
+ }
414
529
  function isContentKey(key) {
415
530
  if (CONTENT_KEYS_KEEP_EXACT.has(key)) return false;
416
531
  if (CONTENT_KEYS_EXACT.has(key) || key.endsWith("_content")) return true;
@@ -420,25 +535,220 @@ function isContentKey(key) {
420
535
  if (context && (context.size === 0 || segments.slice(0, -1).some((s) => context.has(s)))) {
421
536
  return true;
422
537
  }
423
- if (KEEP_LAST_SEGMENTS.has(last) || KEEP_LAST_SUFFIXES.some((x) => last.endsWith(x))) {
538
+ if (hasKeepLastSegment(last)) {
424
539
  return false;
425
540
  }
541
+ if (segments.length > 1 && CONTENT_FIRST_SEGMENTS.has(segments[0])) return true;
426
542
  if (last === "content") return true;
427
543
  if (last === "text" || last === "arguments") {
428
544
  return segments.slice(0, -1).some((s) => CONTENT_CONTAINER_SEGMENTS.has(s));
429
545
  }
430
546
  return false;
431
547
  }
432
- function stripAttributes(attrs) {
548
+ function isContentEvent(name) {
549
+ return CONTENT_EVENT_NAMES.has(name) || name.startsWith(CONTENT_EVENT_PREFIX);
550
+ }
551
+ function isContentEventAttribute(key) {
552
+ if (isContentKey(key)) return true;
553
+ const last = key.slice(key.lastIndexOf(".") + 1);
554
+ return !(CONTENT_KEYS_KEEP_EXACT.has(key) || CONTENT_EVENT_KEEP_KEYS.has(key) || hasKeepLastSegment(last));
555
+ }
556
+ function jsonObjectContentKeys(keyPath, key) {
557
+ for (const [ruleKey, keys] of CONTENT_IN_JSON_OBJECT) {
558
+ if (ruleKey === keyPath || ruleKey === key) return keys;
559
+ }
560
+ return void 0;
561
+ }
562
+ function isJsonObject(value) {
563
+ return typeof value === "object" && value !== null && !Array.isArray(value);
564
+ }
565
+ function flatValueRule(keyPath, key) {
566
+ for (const [ruleKey, keys] of CONTENT_IN_JSON_OBJECT) {
567
+ for (const k of [keyPath, key]) {
568
+ if (k.startsWith(`${ruleKey}.`)) return [k.slice(ruleKey.length + 1), keys];
569
+ }
570
+ }
571
+ return void 0;
572
+ }
573
+ function normalisedKey(key) {
574
+ return key.replace(/_/g, "").replace(/[A-Z]/g, (c) => c.toLowerCase());
575
+ }
576
+ function namesListedKey(key, keys) {
577
+ return key.split(".").some((part) => keys.has(normalisedKey(part)));
578
+ }
579
+ function isKeyRuleText(keyPath, key) {
580
+ return isContentKey(keyPath) || isContentKey(key);
581
+ }
582
+ function startsAsJson(text) {
583
+ return /^[ \t\n\r]*[{[]/.test(text);
584
+ }
585
+ function parseJson(text) {
586
+ return JSON.parse(text, (_key, value) => {
587
+ if (typeof value === "number" && !Number.isFinite(value)) {
588
+ throw new RangeError("not JSON: a number out of range");
589
+ }
590
+ return value;
591
+ });
592
+ }
593
+ function jsonCopy(value) {
594
+ return parseJson(
595
+ JSON.stringify(value, (_key, v) => {
596
+ if (typeof v === "number" && !Number.isFinite(v)) throw new RangeError("not JSON: a non-finite number");
597
+ return v;
598
+ })
599
+ );
600
+ }
601
+ function omitInJsonObject(value, keyPath, keys) {
602
+ try {
603
+ let parsed;
604
+ if (typeof value === "string") parsed = parseJson(value);
605
+ else if (typeof value === "object" && value !== null) parsed = jsonCopy(value);
606
+ if (!isJsonObject(parsed)) return [CONTENT_OMITTED_MARKER, 1];
607
+ let n = 0;
608
+ for (const k of Object.keys(parsed)) {
609
+ const [entry, count] = omitInParameterEntry(parsed[k], `${keyPath}.${k}`, k, keys);
610
+ parsed[k] = entry;
611
+ n += count;
612
+ }
613
+ return [typeof value === "string" ? JSON.stringify(parsed) : parsed, n];
614
+ } catch {
615
+ return [CONTENT_OMITTED_MARKER, 1];
616
+ }
617
+ }
618
+ function omitInParameterEntry(value, keyPath, key, keys) {
619
+ if (namesListedKey(key, keys) || isKeyRuleText(keyPath, key)) return [CONTENT_OMITTED_MARKER, 1];
620
+ return omitInParameter(value, keyPath, keys);
621
+ }
622
+ function omitInParameter(value, path, keys) {
433
623
  let n = 0;
434
- for (const k of Object.keys(attrs)) {
435
- if (isContentKey(k)) {
436
- attrs[k] = CONTENT_OMITTED_MARKER;
437
- n++;
624
+ if (Array.isArray(value)) {
625
+ value.forEach((item, i) => {
626
+ const [entry, count] = omitInParameter(item, `${path}.${i}`, keys);
627
+ value[i] = entry;
628
+ n += count;
629
+ });
630
+ } else if (isJsonObject(value)) {
631
+ for (const k of Object.keys(value)) {
632
+ const [entry, count] = omitInParameterEntry(value[k], `${path}.${k}`, k, keys);
633
+ value[k] = entry;
634
+ n += count;
635
+ }
636
+ } else if (typeof value === "string" && startsAsJson(value)) {
637
+ try {
638
+ const [tree, count] = omitInParameter(parseJson(value), path, keys);
639
+ return [count ? JSON.stringify(tree) : value, count];
640
+ } catch {
641
+ return [CONTENT_OMITTED_MARKER, 1];
642
+ }
643
+ }
644
+ return [value, n];
645
+ }
646
+ function omitFlat(value, keyPath, key, rest, keys) {
647
+ if (namesListedKey(rest, keys) || isKeyRuleText(keyPath, key)) return [CONTENT_OMITTED_MARKER, 1];
648
+ try {
649
+ if (typeof value === "object" && value !== null) {
650
+ const [tree, n] = omitInParameter(jsonCopy(value), keyPath, keys);
651
+ return n ? [tree, n] : [value, 0];
652
+ }
653
+ return omitInParameter(value, keyPath, keys);
654
+ } catch {
655
+ return [CONTENT_OMITTED_MARKER, 1];
656
+ }
657
+ }
658
+ function omitBelow(value, path) {
659
+ const text = typeof value === "string" ? value : value instanceof String ? value.valueOf() : void 0;
660
+ if (text !== void 0) {
661
+ let tree;
662
+ try {
663
+ tree = parseJson(text);
664
+ } catch {
665
+ return startsAsJson(text) ? [CONTENT_OMITTED_MARKER, 1] : [value, 0];
666
+ }
667
+ if (typeof tree !== "object" || tree === null) return [value, 0];
668
+ try {
669
+ const [n, found] = omitBelowParsed(tree, path);
670
+ return found ? [JSON.stringify(tree), n] : [value, 0];
671
+ } catch {
672
+ return [CONTENT_OMITTED_MARKER, 1];
438
673
  }
439
674
  }
675
+ if (typeof value !== "object" || value === null) return [value, 0];
676
+ try {
677
+ const tree = jsonCopy(value);
678
+ const [n, found] = omitBelowParsed(tree, path);
679
+ return found ? [tree, n] : [value, 0];
680
+ } catch {
681
+ return [CONTENT_OMITTED_MARKER, 1];
682
+ }
683
+ }
684
+ function omitBelowParsed(tree, path) {
685
+ let n = 0;
686
+ let found = false;
687
+ if (Array.isArray(tree)) {
688
+ tree.forEach((item, i) => {
689
+ const child = `${path}.${i}`;
690
+ if (!VALUE_RULE_PREFIXES.has(child)) return;
691
+ const [count, below] = omitBelowParsed(item, child);
692
+ n += count;
693
+ found ||= below;
694
+ });
695
+ } else if (isJsonObject(tree)) {
696
+ for (const k of Object.keys(tree)) {
697
+ const child = `${path}.${k}`;
698
+ const keys = jsonObjectContentKeys(child, k);
699
+ const flat = keys ? void 0 : flatValueRule(child, k);
700
+ if (keys) {
701
+ const [entry, count] = omitInJsonObject(tree[k], child, keys);
702
+ tree[k] = entry;
703
+ n += count;
704
+ found = true;
705
+ } else if (flat) {
706
+ const [entry, count] = omitFlat(tree[k], child, k, flat[0], flat[1]);
707
+ tree[k] = entry;
708
+ n += count;
709
+ found = true;
710
+ } else if (VALUE_RULE_PREFIXES.has(child)) {
711
+ const [count, below] = omitBelowParsed(tree[k], child);
712
+ n += count;
713
+ found ||= below;
714
+ }
715
+ }
716
+ }
717
+ return [n, found];
718
+ }
719
+ function reachesValueRule(key) {
720
+ return CONTENT_IN_JSON_OBJECT.has(key) || VALUE_RULE_PREFIXES.has(key) || flatValueRule(key, key) !== void 0;
721
+ }
722
+ function stripAttributes(attrs, rule = isContentKey) {
723
+ let n = 0;
724
+ for (const k of Object.keys(attrs)) {
725
+ const keys = rule === isContentKey ? jsonObjectContentKeys(k, k) : void 0;
726
+ const flat = rule === isContentKey && !keys ? flatValueRule(k, k) : void 0;
727
+ let result;
728
+ if (keys) result = omitInJsonObject(attrs[k], k, keys);
729
+ else if (flat) result = omitFlat(attrs[k], k, k, flat[0], flat[1]);
730
+ else if (rule(k)) result = [CONTENT_OMITTED_MARKER, 1];
731
+ else if (VALUE_RULE_PREFIXES.has(k)) result = omitBelow(attrs[k], k);
732
+ else continue;
733
+ attrs[k] = result[0];
734
+ n += result[1];
735
+ }
440
736
  return n;
441
737
  }
738
+ function stripOwned(holder, rule = isContentKey) {
739
+ const attrs = holder.attributes;
740
+ if (!attrs || typeof attrs !== "object" || !Object.keys(attrs).some((k) => rule(k) || reachesValueRule(k))) {
741
+ return 0;
742
+ }
743
+ const copy = { ...attrs };
744
+ try {
745
+ holder.attributes = copy;
746
+ } catch {
747
+ return stripAttributes(attrs, rule);
748
+ }
749
+ if (holder.attributes !== copy) return stripAttributes(attrs, rule);
750
+ return stripAttributes(copy, rule);
751
+ }
442
752
  function stripContent(span) {
443
753
  let stripped = 0;
444
754
  if (span.attributes && typeof span.attributes === "object") {
@@ -447,14 +757,15 @@ function stripContent(span) {
447
757
  if (Array.isArray(span.events)) {
448
758
  for (const event of span.events) {
449
759
  if (event?.attributes && typeof event.attributes === "object") {
450
- stripped += stripAttributes(event.attributes);
760
+ const messageEvent = typeof event.name === "string" && isContentEvent(event.name);
761
+ stripped += stripOwned(event, messageEvent ? isContentEventAttribute : isContentKey);
451
762
  }
452
763
  }
453
764
  }
454
765
  if (Array.isArray(span.links)) {
455
766
  for (const link of span.links) {
456
767
  if (link?.attributes && typeof link.attributes === "object") {
457
- stripped += stripAttributes(link.attributes);
768
+ stripped += stripOwned(link);
458
769
  }
459
770
  }
460
771
  }
@@ -646,6 +957,12 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
646
957
  _beforeExitHandler = null;
647
958
  // Retry tracking for failed batches (Audit #5)
648
959
  _retryCounts = /* @__PURE__ */ new Map();
960
+ /**
961
+ * The HTTP requests that the failed rounds of each re-queued span sent, by span id (issue
962
+ * #57). It has the same life as `_retryCounts`: `_forgetRetry` ends both. NaN = a round for
963
+ * which the exporter did not say. Empty for an exporter that does not state it.
964
+ */
965
+ _retryRequests = /* @__PURE__ */ new Map();
649
966
  static MAX_RETRIES = 3;
650
967
  _lastRefusalWarnAt = Number.NEGATIVE_INFINITY;
651
968
  _lastUnackWarnAt = Number.NEGATIVE_INFINITY;
@@ -860,6 +1177,7 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
860
1177
  await shutdownExporter(exporter, Math.max(0, shutdownDeadline - Date.now()));
861
1178
  }
862
1179
  this._retryCounts.clear();
1180
+ this._retryRequests.clear();
863
1181
  debug(
864
1182
  `BatchSpanProcessor shutdown. Exported: ${this.exportedSpans}, Dropped: ${this.droppedSpans}, Failed: ${this.failedExports}`
865
1183
  );
@@ -876,7 +1194,7 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
876
1194
  if (this._queue.length >= this._maxQueueSize) {
877
1195
  if (this._quotaPaused()) {
878
1196
  const oldest = this._queue.shift();
879
- if (oldest !== void 0) this._retryCounts.delete(oldest.spanId);
1197
+ if (oldest !== void 0) this._forgetRetry(oldest.spanId);
880
1198
  this._recordDropped("quota_exceeded");
881
1199
  for (const exporter of this._exporters) {
882
1200
  try {
@@ -1071,13 +1389,37 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
1071
1389
  }
1072
1390
  }
1073
1391
  }
1074
- /** The requests in one export round, when every exporter that delivers states it. */
1075
- _requestsPerRound() {
1392
+ /** The one exporter that delivers, when there is exactly one. */
1393
+ _soleDeliverer() {
1076
1394
  const delivering = this._exporters.filter((e) => e.countsAsDelivery !== false);
1077
- if (delivering.length !== 1) return void 0;
1078
- const n = delivering[0].attemptsPerExport;
1395
+ return delivering.length === 1 ? delivering[0] : void 0;
1396
+ }
1397
+ /** The most requests in one export round, when every exporter that delivers states it. */
1398
+ _requestsPerRound() {
1399
+ const n = this._soleDeliverer()?.attemptsPerExport;
1079
1400
  return typeof n === "number" && Number.isFinite(n) && n > 0 ? n : void 0;
1080
1401
  }
1402
+ /**
1403
+ * The HTTP requests that the failed round for `batch` really sent (issue #57). `undefined`
1404
+ * when the exporter that delivers does not state it, so that the WARNING keeps its "up to"
1405
+ * text. NaN when the exporter has no record of the round.
1406
+ */
1407
+ _requestsSent(batch) {
1408
+ const exporter = this._soleDeliverer();
1409
+ if (exporter === void 0 || typeof exporter.takeAttempts !== "function") return void 0;
1410
+ try {
1411
+ const n = exporter.takeAttempts(batch);
1412
+ return typeof n === "number" && Number.isFinite(n) && n >= 0 ? n : Number.NaN;
1413
+ } catch (e) {
1414
+ debug(`takeAttempts of ${exporter.name} failed: ${describeThrown(e)}`);
1415
+ return Number.NaN;
1416
+ }
1417
+ }
1418
+ /** End the retry tracking of one span (retry-map drain sites 1 and 2 of 3, and the queue drop). */
1419
+ _forgetRetry(spanId) {
1420
+ this._retryCounts.delete(spanId);
1421
+ this._retryRequests.delete(spanId);
1422
+ }
1081
1423
  /**
1082
1424
  * One export round.
1083
1425
  *
@@ -1140,7 +1482,7 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
1140
1482
  this._countDelivered(batch.length - unserializable.size, ack);
1141
1483
  batchExported = true;
1142
1484
  for (const span of batch) {
1143
- this._retryCounts.delete(span.spanId);
1485
+ this._forgetRetry(span.spanId);
1144
1486
  }
1145
1487
  }
1146
1488
  } else {
@@ -1156,16 +1498,21 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
1156
1498
  if (unserializable.size > 0) this._recordDropped("unacknowledged", unserializable.size);
1157
1499
  if (!batchExported) {
1158
1500
  let dropped = 0;
1501
+ const sent = this._requestsSent(batch);
1502
+ const droppedRequests = [];
1159
1503
  const retryable = batch.filter((span) => {
1160
1504
  if (unserializable.has(span)) return false;
1161
1505
  const count = (this._retryCounts.get(span.spanId) ?? 0) + 1;
1506
+ const requests = sent === void 0 ? void 0 : (this._retryRequests.get(span.spanId) ?? 0) + sent;
1162
1507
  if (count > _BatchSpanProcessor.MAX_RETRIES) {
1163
- this._retryCounts.delete(span.spanId);
1508
+ this._forgetRetry(span.spanId);
1509
+ if (requests !== void 0) droppedRequests.push(requests);
1164
1510
  this._recordDropped("transport_refused");
1165
1511
  dropped++;
1166
1512
  return false;
1167
1513
  }
1168
1514
  this._retryCounts.set(span.spanId, count);
1515
+ if (requests !== void 0) this._retryRequests.set(span.spanId, requests);
1169
1516
  return true;
1170
1517
  });
1171
1518
  if (retryable.length > 0) {
@@ -1178,8 +1525,21 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
1178
1525
  this._lastRefusalWarnAt = now;
1179
1526
  const rounds = _BatchSpanProcessor.MAX_RETRIES + 1;
1180
1527
  const perRound = this._requestsPerRound();
1528
+ const stated = droppedRequests.length > 0 && droppedRequests.every((n) => Number.isFinite(n));
1529
+ let requestsText = "";
1530
+ if (stated) {
1531
+ let lo = droppedRequests[0];
1532
+ let hi = lo;
1533
+ for (const n of droppedRequests) {
1534
+ if (n < lo) lo = n;
1535
+ if (n > hi) hi = n;
1536
+ }
1537
+ requestsText = lo === hi ? ` (${hi} HTTP ${hi === 1 ? "request" : "requests"} sent)` : ` (${lo} to ${hi} HTTP requests sent)`;
1538
+ } else if (perRound !== void 0) {
1539
+ requestsText = ` (up to ${rounds * perRound} HTTP requests: ${perRound} in each round)`;
1540
+ }
1181
1541
  warn(
1182
- `${dropped} span(s) DISCARDED (reason=transport_refused) after ${rounds} failed export rounds each` + (perRound !== void 0 ? ` (up to ${rounds * perRound} HTTP requests: ${perRound} in each round)` : "") + ` \u2014 the endpoint is not accepting spans and the retry budget is spent. ${this.droppedSpansByReason.transport_refused} span(s) discarded this way so far in this process. This data is gone; check the apiKey and the endpoint.`
1542
+ `${dropped} span(s) DISCARDED (reason=transport_refused) after ${rounds} failed export rounds each` + requestsText + ` \u2014 the endpoint is not accepting spans and the retry budget is spent. ${this.droppedSpansByReason.transport_refused} span(s) discarded this way so far in this process. This data is gone; check the apiKey and the endpoint.`
1183
1543
  );
1184
1544
  }
1185
1545
  }
@@ -1237,7 +1597,7 @@ function resolveConfig(config) {
1237
1597
  const environment = config?.environment || env.RISICARE_ENVIRONMENT || "development";
1238
1598
  const serviceName = config?.serviceName ?? env.RISICARE_SERVICE_NAME;
1239
1599
  const serviceVersion = config?.serviceVersion ?? env.RISICARE_SERVICE_VERSION;
1240
- const traceContent = config?.traceContent ?? parseBool(env.RISICARE_TRACE_CONTENT, false);
1600
+ const traceContent = config?.traceContent ?? parseBool(env.RISICARE_TRACE_CONTENT, true);
1241
1601
  const sampleRateRaw = config?.sampleRate ?? parseFloat(env.RISICARE_SAMPLE_RATE ?? "1.0");
1242
1602
  const debug2 = config?.debug ?? parseBool(env.RISICARE_DEBUG, false);
1243
1603
  const compress = config?.compress ?? parseBool(env.RISICARE_COMPRESS, false);
@@ -2442,7 +2802,7 @@ function markCancel(span, error) {
2442
2802
  }
2443
2803
 
2444
2804
  // src/version.ts
2445
- var SDK_VERSION = "0.9.1";
2805
+ var SDK_VERSION = "0.10.0";
2446
2806
 
2447
2807
  // src/exporters/quota.ts
2448
2808
  var QUOTA_REASON = "monthly_quota_exceeded";
@@ -2587,6 +2947,8 @@ var HttpExporter = class {
2587
2947
  _calls = /* @__PURE__ */ new Map();
2588
2948
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
2589
2949
  _acks = /* @__PURE__ */ new WeakMap();
2950
+ /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
2951
+ _requests = /* @__PURE__ */ new WeakMap();
2590
2952
  /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
2591
2953
  noteQuotaDrop(count = 1) {
2592
2954
  this._quota.addDrops(count);
@@ -2638,6 +3000,18 @@ var HttpExporter = class {
2638
3000
  get attemptsPerExport() {
2639
3001
  return this._maxRetries;
2640
3002
  }
3003
+ /**
3004
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
3005
+ * it. This is the number that was sent, not the most that the call may send
3006
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
3007
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
3008
+ * kept per batch, as `takeAck` keeps the ack.
3009
+ */
3010
+ takeAttempts(spans) {
3011
+ const n = this._requests.get(spans) ?? null;
3012
+ this._requests.delete(spans);
3013
+ return n;
3014
+ }
2641
3015
  /**
2642
3016
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
2643
3017
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -2682,13 +3056,15 @@ var HttpExporter = class {
2682
3056
  misroute: null,
2683
3057
  noRetry: false,
2684
3058
  status: null,
2685
- error: null
3059
+ error: null,
3060
+ attempts: 0
2686
3061
  };
2687
3062
  this._calls.set(spans, call);
2688
3063
  try {
2689
3064
  return await this._export(spans, call);
2690
3065
  } finally {
2691
3066
  this._calls.delete(spans);
3067
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
2692
3068
  }
2693
3069
  }
2694
3070
  async _export(spans, call) {
@@ -2728,14 +3104,13 @@ var HttpExporter = class {
2728
3104
  isHalfOpen = true;
2729
3105
  }
2730
3106
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
2731
- let attempts = 0;
2732
3107
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
2733
3108
  call.refusalAfterMs = null;
2734
3109
  call.misroute = null;
2735
3110
  call.noRetry = false;
2736
3111
  call.status = null;
2737
3112
  call.error = null;
2738
- attempts++;
3113
+ call.attempts++;
2739
3114
  const result = await this._sendRequest(payload, parts.length, call, spans);
2740
3115
  if (cancelled.aborted) {
2741
3116
  this._acks.delete(spans);
@@ -2757,7 +3132,7 @@ var HttpExporter = class {
2757
3132
  }
2758
3133
  if (call.refusalAfterMs !== null) {
2759
3134
  warn(
2760
- `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.`
3135
+ `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.`
2761
3136
  );
2762
3137
  return "failure" /* FAILURE */;
2763
3138
  }
@@ -4523,7 +4898,10 @@ function init(config) {
4523
4898
  "environment",
4524
4899
  "serviceName",
4525
4900
  "serviceVersion",
4526
- "sampleRate"
4901
+ "sampleRate",
4902
+ // Capture is on by default (R9): a library's early init() must not
4903
+ // make the program's traceContent: false void in silence.
4904
+ "traceContent"
4527
4905
  ];
4528
4906
  for (const k of checkedKeys) {
4529
4907
  if (incoming[k] !== existing.config[k]) {