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
@@ -160,6 +160,51 @@ function hasCredentials(url) {
160
160
  }
161
161
  }
162
162
 
163
+ // src/masking.ts
164
+ var CONTENT_IN_JSON_OBJECT = /* @__PURE__ */ new Map([
165
+ [
166
+ "llm.invocation_parameters",
167
+ /* @__PURE__ */ new Set([
168
+ "additionalinstructions",
169
+ // OpenAI assistants run
170
+ "chathistory",
171
+ // Cohere
172
+ "contents",
173
+ // Gemini
174
+ "documents",
175
+ // Cohere RAG
176
+ "functions",
177
+ "input",
178
+ "inputtext",
179
+ // Bedrock Titan
180
+ "instructions",
181
+ "message",
182
+ // Cohere
183
+ "messages",
184
+ "preamble",
185
+ // Cohere
186
+ "prediction",
187
+ // OpenAI predicted output
188
+ "prompt",
189
+ "system",
190
+ // Anthropic
191
+ "systeminstruction",
192
+ // Gemini
193
+ "texts",
194
+ // Cohere embed
195
+ "toolconfig",
196
+ // Gemini
197
+ "tools"
198
+ ])
199
+ ]
200
+ ]);
201
+ var VALUE_RULE_PREFIXES = new Set(
202
+ [...CONTENT_IN_JSON_OBJECT.keys()].flatMap((key) => {
203
+ const segments = key.split(".");
204
+ return segments.slice(1).map((_, i) => segments.slice(0, i + 1).join("."));
205
+ })
206
+ );
207
+
163
208
  // src/exporters/batch.ts
164
209
  var MAX_TIMEOUT_MS = 2 ** 31 - 1;
165
210
  var MAX_BATCH_BYTES = 8 * 1024 * 1024;
@@ -496,7 +541,7 @@ function attachErrorCode(span, error) {
496
541
  }
497
542
 
498
543
  // src/version.ts
499
- var SDK_VERSION = "0.9.1";
544
+ var SDK_VERSION = "0.10.0";
500
545
 
501
546
  // src/exporters/quota.ts
502
547
  var QUOTA_REASON = "monthly_quota_exceeded";
@@ -641,6 +686,8 @@ var HttpExporter = class {
641
686
  _calls = /* @__PURE__ */ new Map();
642
687
  /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
643
688
  _acks = /* @__PURE__ */ new WeakMap();
689
+ /** The requests that each finished call sent, by its batch. Read once by `takeAttempts(spans)`. */
690
+ _requests = /* @__PURE__ */ new WeakMap();
644
691
  /** The batch processor dropped span(s) from a full queue in the quota pause (S-1). */
645
692
  noteQuotaDrop(count = 1) {
646
693
  this._quota.addDrops(count);
@@ -692,6 +739,18 @@ var HttpExporter = class {
692
739
  get attemptsPerExport() {
693
740
  return this._maxRetries;
694
741
  }
742
+ /**
743
+ * Returns the number of HTTP requests that the `export()` call for `spans` sent, and clears
744
+ * it. This is the number that was sent, not the most that the call may send
745
+ * (`attemptsPerExport`): a 401 or 403 stops the loop after one request, and an open circuit
746
+ * breaker sends none. Returns `null` when no call is on record for `spans`. The answer is
747
+ * kept per batch, as `takeAck` keeps the ack.
748
+ */
749
+ takeAttempts(spans) {
750
+ const n = this._requests.get(spans) ?? null;
751
+ this._requests.delete(spans);
752
+ return n;
753
+ }
695
754
  /**
696
755
  * Returns the counts of the ack that the `export()` call for `spans` received, and clears
697
756
  * them. The answer is kept per batch, not on the instance: two processors that share this
@@ -736,13 +795,15 @@ var HttpExporter = class {
736
795
  misroute: null,
737
796
  noRetry: false,
738
797
  status: null,
739
- error: null
798
+ error: null,
799
+ attempts: 0
740
800
  };
741
801
  this._calls.set(spans, call);
742
802
  try {
743
803
  return await this._export(spans, call);
744
804
  } finally {
745
805
  this._calls.delete(spans);
806
+ this._requests.set(spans, this._endpointHasCredentials ? 0 : call.attempts);
746
807
  }
747
808
  }
748
809
  async _export(spans, call) {
@@ -782,14 +843,13 @@ var HttpExporter = class {
782
843
  isHalfOpen = true;
783
844
  }
784
845
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
785
- let attempts = 0;
786
846
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
787
847
  call.refusalAfterMs = null;
788
848
  call.misroute = null;
789
849
  call.noRetry = false;
790
850
  call.status = null;
791
851
  call.error = null;
792
- attempts++;
852
+ call.attempts++;
793
853
  const result = await this._sendRequest(payload, parts.length, call, spans);
794
854
  if (cancelled.aborted) {
795
855
  this._acks.delete(spans);
@@ -811,7 +871,7 @@ var HttpExporter = class {
811
871
  }
812
872
  if (call.refusalAfterMs !== null) {
813
873
  warn(
814
- `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.`
874
+ `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.`
815
875
  );
816
876
  return "failure" /* FAILURE */;
817
877
  }
@@ -1082,7 +1142,7 @@ function className(error) {
1082
1142
  }
1083
1143
  return typeof error;
1084
1144
  }
1085
- function responseReadFailed(provider, site, error, span) {
1145
+ function responseReadFailed(provider, site, error, span, part = "response") {
1086
1146
  try {
1087
1147
  const cls = className(error);
1088
1148
  span?.setAttribute("risicare.observer_error", cls);
@@ -1090,7 +1150,7 @@ function responseReadFailed(provider, site, error, span) {
1090
1150
  const key = `${provider}\0${site}\0${cls}`;
1091
1151
  if (logged.has(key) || logged.size >= LOGGED_MAX) return;
1092
1152
  logged.add(key);
1093
- debug(`could not read the ${provider} response in ${site} (${cls}); the span keeps fewer facts`);
1153
+ debug(`could not read the ${provider} ${part} in ${site} (${cls}); the span keeps fewer facts`);
1094
1154
  } catch {
1095
1155
  }
1096
1156
  }
@@ -1101,6 +1161,13 @@ function readResponseSafely(provider, site, span, read) {
1101
1161
  responseReadFailed(provider, site, error, span);
1102
1162
  }
1103
1163
  }
1164
+ function readRequestSafely(provider, site, span, read) {
1165
+ try {
1166
+ read();
1167
+ } catch (error) {
1168
+ responseReadFailed(provider, site, error, span, "request");
1169
+ }
1170
+ }
1104
1171
 
1105
1172
  // src/utils/stream-proxy.ts
1106
1173
  var HANDOFF = /* @__PURE__ */ new Set(["tee", "toReadableStream", "getReader", "pipeTo", "pipeThrough"]);
@@ -1845,7 +1912,39 @@ function calculateCost(model, promptTokens, completionTokens) {
1845
1912
  function tokenCount(value) {
1846
1913
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
1847
1914
  }
1848
- function recordTokenUsage(span, counts, model) {
1915
+ var CACHE_READ_TOKENS_KEY = "gen_ai.usage.cache_read.input_tokens";
1916
+ var CACHE_WRITE_TOKENS_KEY = "gen_ai.usage.cache_write.input_tokens";
1917
+ var REASONING_TOKENS_KEY = "gen_ai.usage.reasoning.output_tokens";
1918
+ function providerInteger(value) {
1919
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : void 0;
1920
+ }
1921
+ function nested(obj, outer, inner) {
1922
+ if (!obj || typeof obj !== "object") return void 0;
1923
+ const details = obj[outer];
1924
+ if (!details || typeof details !== "object") return void 0;
1925
+ return details[inner];
1926
+ }
1927
+ function anthropicUsageDetails(usage) {
1928
+ try {
1929
+ const u = usage && typeof usage === "object" ? usage : {};
1930
+ return {
1931
+ cacheRead: u.cache_read_input_tokens,
1932
+ cacheWrite: u.cache_creation_input_tokens,
1933
+ reasoning: nested(usage, "output_tokens_details", "thinking_tokens")
1934
+ };
1935
+ } catch {
1936
+ return {};
1937
+ }
1938
+ }
1939
+ function recordUsageDetails(span, details) {
1940
+ const cacheRead = providerInteger(details.cacheRead);
1941
+ const cacheWrite = providerInteger(details.cacheWrite);
1942
+ const reasoning = providerInteger(details.reasoning);
1943
+ if (cacheRead !== void 0) span.setAttribute(CACHE_READ_TOKENS_KEY, cacheRead);
1944
+ if (cacheWrite !== void 0) span.setAttribute(CACHE_WRITE_TOKENS_KEY, cacheWrite);
1945
+ if (reasoning !== void 0) span.setAttribute(REASONING_TOKENS_KEY, reasoning);
1946
+ }
1947
+ function recordTokenUsage(span, counts, model, details) {
1849
1948
  const promptTokens = tokenCount(counts.prompt);
1850
1949
  const completionTokens = tokenCount(counts.completion);
1851
1950
  const bothKnown = promptTokens !== void 0 && completionTokens !== void 0;
@@ -1856,6 +1955,7 @@ function recordTokenUsage(span, counts, model) {
1856
1955
  if (completionTokens !== void 0) span.setAttribute("gen_ai.usage.completion_tokens", completionTokens);
1857
1956
  if (totalTokens !== void 0) span.setAttribute("gen_ai.usage.total_tokens", totalTokens);
1858
1957
  if (costUsd !== void 0) span.setAttribute("llm.cost.total_usd", costUsd);
1958
+ if (details) recordUsageDetails(span, details);
1859
1959
  }
1860
1960
 
1861
1961
  // src/utils/assert-client.ts
@@ -1942,7 +2042,7 @@ function enrichSpanFromResponse(span, response) {
1942
2042
  span.setAttribute("gen_ai.response.finish_reasons", [stopReason]);
1943
2043
  }
1944
2044
  if (usage) {
1945
- recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model);
2045
+ recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model, anthropicUsageDetails(usage));
1946
2046
  }
1947
2047
  }
1948
2048
  function createMessagesProxy(originalCreate) {
@@ -1970,9 +2070,23 @@ function createMessagesProxy(originalCreate) {
1970
2070
  const traceContent = tracer.traceContent;
1971
2071
  if (traceContent) {
1972
2072
  captureSystemPrompt(span, params.system);
1973
- capturePromptMessages(span, params.messages, true);
2073
+ readRequestSafely(
2074
+ "anthropic",
2075
+ "capturePromptMessages",
2076
+ span,
2077
+ () => capturePromptMessages(span, params.messages, true)
2078
+ );
1974
2079
  }
1975
2080
  const streamed = {};
2081
+ const noteDetails = (u) => {
2082
+ const d = anthropicUsageDetails(u);
2083
+ const cacheRead = providerInteger(d.cacheRead);
2084
+ const cacheWrite = providerInteger(d.cacheWrite);
2085
+ const reasoning = providerInteger(d.reasoning);
2086
+ if (cacheRead !== void 0) streamed.cacheRead = cacheRead;
2087
+ if (cacheWrite !== void 0) streamed.cacheWrite = cacheWrite;
2088
+ if (reasoning !== void 0) streamed.reasoning = reasoning;
2089
+ };
1976
2090
  return observeProviderCall(
1977
2091
  span,
1978
2092
  {
@@ -1993,19 +2107,29 @@ function createMessagesProxy(originalCreate) {
1993
2107
  streamed.id = e.message.id;
1994
2108
  const u = e.message.usage;
1995
2109
  if (typeof u?.input_tokens === "number") streamed.input = u.input_tokens;
2110
+ noteDetails(u);
1996
2111
  } else if (e?.type === "message_delta") {
1997
2112
  if (e.delta?.stop_reason) streamed.stop_reason = e.delta.stop_reason;
1998
2113
  if (typeof e.usage?.output_tokens === "number") streamed.output = e.usage.output_tokens;
2114
+ noteDetails(e.usage);
1999
2115
  }
2000
2116
  },
2001
2117
  beforeEnd: () => {
2002
- if (streamed.model === void 0 && streamed.input === void 0 && streamed.output === void 0) return;
2118
+ if (streamed.model === void 0 && streamed.input === void 0 && streamed.output === void 0 && streamed.cacheRead === void 0 && streamed.cacheWrite === void 0 && streamed.reasoning === void 0) {
2119
+ return;
2120
+ }
2003
2121
  enrichSpanFromResponse(span, {
2004
2122
  model: streamed.model,
2005
2123
  id: streamed.id,
2006
2124
  stop_reason: streamed.stop_reason,
2007
2125
  // A count the stream did not give stays unknown (token-usage.ts).
2008
- usage: { input_tokens: streamed.input, output_tokens: streamed.output }
2126
+ usage: {
2127
+ input_tokens: streamed.input,
2128
+ output_tokens: streamed.output,
2129
+ cache_read_input_tokens: streamed.cacheRead,
2130
+ cache_creation_input_tokens: streamed.cacheWrite,
2131
+ output_tokens_details: streamed.reasoning === void 0 ? void 0 : { thinking_tokens: streamed.reasoning }
2132
+ }
2009
2133
  });
2010
2134
  }
2011
2135
  },
@@ -2013,9 +2137,11 @@ function createMessagesProxy(originalCreate) {
2013
2137
  );
2014
2138
  };
2015
2139
  }
2140
+ var PATCHED = /* @__PURE__ */ new WeakSet();
2016
2141
  function patchAnthropic(client) {
2017
2142
  assertClient("patchAnthropic", client);
2018
- return new Proxy(client, {
2143
+ if (PATCHED.has(client)) return client;
2144
+ const proxy = new Proxy(client, {
2019
2145
  get(target, prop, receiver) {
2020
2146
  const value = Reflect.get(target, prop, receiver);
2021
2147
  if (prop === "messages" && value && typeof value === "object") {
@@ -2032,6 +2158,8 @@ function patchAnthropic(client) {
2032
2158
  return value;
2033
2159
  }
2034
2160
  });
2161
+ PATCHED.add(proxy);
2162
+ return proxy;
2035
2163
  }
2036
2164
  // Annotate the CommonJS export names for ESM import in node:
2037
2165
  0 && (module.exports = {