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
@@ -16,6 +16,9 @@
16
16
  * Returns a Proxy that intercepts chat.completions.create.
17
17
  * The original client is NOT modified.
18
18
  *
19
+ * Patching a proxy that this function returned gives back the same proxy, so the
20
+ * second patch adds no second span.
21
+ *
19
22
  * @param client - A Cerebras SDK client instance
20
23
  * @returns A proxied client with automatic tracing
21
24
  */
@@ -16,6 +16,9 @@
16
16
  * Returns a Proxy that intercepts chat.completions.create.
17
17
  * The original client is NOT modified.
18
18
  *
19
+ * Patching a proxy that this function returned gives back the same proxy, so the
20
+ * second patch adds no second span.
21
+ *
19
22
  * @param client - A Cerebras SDK client instance
20
23
  * @returns A proxied client with automatic tracing
21
24
  */
@@ -124,6 +124,51 @@ function hasCredentials(url) {
124
124
  }
125
125
  }
126
126
 
127
+ // src/masking.ts
128
+ var CONTENT_IN_JSON_OBJECT = /* @__PURE__ */ new Map([
129
+ [
130
+ "llm.invocation_parameters",
131
+ /* @__PURE__ */ new Set([
132
+ "additionalinstructions",
133
+ // OpenAI assistants run
134
+ "chathistory",
135
+ // Cohere
136
+ "contents",
137
+ // Gemini
138
+ "documents",
139
+ // Cohere RAG
140
+ "functions",
141
+ "input",
142
+ "inputtext",
143
+ // Bedrock Titan
144
+ "instructions",
145
+ "message",
146
+ // Cohere
147
+ "messages",
148
+ "preamble",
149
+ // Cohere
150
+ "prediction",
151
+ // OpenAI predicted output
152
+ "prompt",
153
+ "system",
154
+ // Anthropic
155
+ "systeminstruction",
156
+ // Gemini
157
+ "texts",
158
+ // Cohere embed
159
+ "toolconfig",
160
+ // Gemini
161
+ "tools"
162
+ ])
163
+ ]
164
+ ]);
165
+ var VALUE_RULE_PREFIXES = new Set(
166
+ [...CONTENT_IN_JSON_OBJECT.keys()].flatMap((key) => {
167
+ const segments = key.split(".");
168
+ return segments.slice(1).map((_, i) => segments.slice(0, i + 1).join("."));
169
+ })
170
+ );
171
+
127
172
  // src/exporters/batch.ts
128
173
  var MAX_TIMEOUT_MS = 2 ** 31 - 1;
129
174
  var MAX_BATCH_BYTES = 8 * 1024 * 1024;
@@ -460,7 +505,7 @@ function attachErrorCode(span, error) {
460
505
  }
461
506
 
462
507
  // src/version.ts
463
- var SDK_VERSION = "0.9.1";
508
+ var SDK_VERSION = "0.10.0";
464
509
 
465
510
  // src/exporters/quota.ts
466
511
  var QUOTA_REASON = "monthly_quota_exceeded";
@@ -605,6 +650,8 @@ var HttpExporter = class {
605
650
  _calls = /* @__PURE__ */ new Map();
606
651
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
607
652
  _acks = /* @__PURE__ */ new WeakMap();
653
+ /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
654
+ _requests = /* @__PURE__ */ new WeakMap();
608
655
  /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
609
656
  noteQuotaDrop(count = 1) {
610
657
  this._quota.addDrops(count);
@@ -656,6 +703,18 @@ var HttpExporter = class {
656
703
  get attemptsPerExport() {
657
704
  return this._maxRetries;
658
705
  }
706
+ /**
707
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
708
+ * it. This is the number that was sent, not the most that the call may send
709
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
710
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
711
+ * kept per batch, as `takeAck` keeps the ack.
712
+ */
713
+ takeAttempts(spans) {
714
+ const n = this._requests.get(spans) ?? null;
715
+ this._requests.delete(spans);
716
+ return n;
717
+ }
659
718
  /**
660
719
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
661
720
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -700,13 +759,15 @@ var HttpExporter = class {
700
759
  misroute: null,
701
760
  noRetry: false,
702
761
  status: null,
703
- error: null
762
+ error: null,
763
+ attempts: 0
704
764
  };
705
765
  this._calls.set(spans, call);
706
766
  try {
707
767
  return await this._export(spans, call);
708
768
  } finally {
709
769
  this._calls.delete(spans);
770
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
710
771
  }
711
772
  }
712
773
  async _export(spans, call) {
@@ -746,14 +807,13 @@ var HttpExporter = class {
746
807
  isHalfOpen = true;
747
808
  }
748
809
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
749
- let attempts = 0;
750
810
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
751
811
  call.refusalAfterMs = null;
752
812
  call.misroute = null;
753
813
  call.noRetry = false;
754
814
  call.status = null;
755
815
  call.error = null;
756
- attempts++;
816
+ call.attempts++;
757
817
  const result = await this._sendRequest(payload, parts.length, call, spans);
758
818
  if (cancelled.aborted) {
759
819
  this._acks.delete(spans);
@@ -775,7 +835,7 @@ var HttpExporter = class {
775
835
  }
776
836
  if (call.refusalAfterMs !== null) {
777
837
  warn(
778
- `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.`
838
+ `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.`
779
839
  );
780
840
  return "failure" /* FAILURE */;
781
841
  }
@@ -1046,7 +1106,7 @@ function className(error) {
1046
1106
  }
1047
1107
  return typeof error;
1048
1108
  }
1049
- function responseReadFailed(provider, site, error, span) {
1109
+ function responseReadFailed(provider, site, error, span, part = "response") {
1050
1110
  try {
1051
1111
  const cls = className(error);
1052
1112
  span?.setAttribute("risicare.observer_error", cls);
@@ -1054,7 +1114,7 @@ function responseReadFailed(provider, site, error, span) {
1054
1114
  const key = `${provider}\0${site}\0${cls}`;
1055
1115
  if (logged.has(key) || logged.size >= LOGGED_MAX) return;
1056
1116
  logged.add(key);
1057
- debug(`could not read the ${provider} response in ${site} (${cls}); the span keeps fewer facts`);
1117
+ debug(`could not read the ${provider} ${part} in ${site} (${cls}); the span keeps fewer facts`);
1058
1118
  } catch {
1059
1119
  }
1060
1120
  }
@@ -1809,7 +1869,21 @@ function calculateCost(model, promptTokens, completionTokens) {
1809
1869
  function tokenCount(value) {
1810
1870
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
1811
1871
  }
1812
- function recordTokenUsage(span, counts, model) {
1872
+ var CACHE_READ_TOKENS_KEY = "gen_ai.usage.cache_read.input_tokens";
1873
+ var CACHE_WRITE_TOKENS_KEY = "gen_ai.usage.cache_write.input_tokens";
1874
+ var REASONING_TOKENS_KEY = "gen_ai.usage.reasoning.output_tokens";
1875
+ function providerInteger(value) {
1876
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : void 0;
1877
+ }
1878
+ function recordUsageDetails(span, details) {
1879
+ const cacheRead = providerInteger(details.cacheRead);
1880
+ const cacheWrite = providerInteger(details.cacheWrite);
1881
+ const reasoning = providerInteger(details.reasoning);
1882
+ if (cacheRead !== void 0) span.setAttribute(CACHE_READ_TOKENS_KEY, cacheRead);
1883
+ if (cacheWrite !== void 0) span.setAttribute(CACHE_WRITE_TOKENS_KEY, cacheWrite);
1884
+ if (reasoning !== void 0) span.setAttribute(REASONING_TOKENS_KEY, reasoning);
1885
+ }
1886
+ function recordTokenUsage(span, counts, model, details) {
1813
1887
  const promptTokens = tokenCount(counts.prompt);
1814
1888
  const completionTokens = tokenCount(counts.completion);
1815
1889
  const bothKnown = promptTokens !== void 0 && completionTokens !== void 0;
@@ -1820,6 +1894,7 @@ function recordTokenUsage(span, counts, model) {
1820
1894
  if (completionTokens !== void 0) span.setAttribute("gen_ai.usage.completion_tokens", completionTokens);
1821
1895
  if (totalTokens !== void 0) span.setAttribute("gen_ai.usage.total_tokens", totalTokens);
1822
1896
  if (costUsd !== void 0) span.setAttribute("llm.cost.total_usd", costUsd);
1897
+ if (details) recordUsageDetails(span, details);
1823
1898
  }
1824
1899
 
1825
1900
  // src/utils/assert-client.ts
@@ -1918,9 +1993,11 @@ function createCompletionProxy(originalCreate) {
1918
1993
  );
1919
1994
  };
1920
1995
  }
1996
+ var PATCHED = /* @__PURE__ */ new WeakSet();
1921
1997
  function patchCerebras(client) {
1922
1998
  assertClient("patchCerebras", client);
1923
- return new Proxy(client, {
1999
+ if (PATCHED.has(client)) return client;
2000
+ const proxy = new Proxy(client, {
1924
2001
  get(target, prop, receiver) {
1925
2002
  const value = Reflect.get(target, prop, receiver);
1926
2003
  if (prop === "chat" && value && typeof value === "object") {
@@ -1945,6 +2022,8 @@ function patchCerebras(client) {
1945
2022
  return value;
1946
2023
  }
1947
2024
  });
2025
+ PATCHED.add(proxy);
2026
+ return proxy;
1948
2027
  }
1949
2028
  export {
1950
2029
  patchCerebras