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
@@ -17,6 +17,9 @@
17
17
  * Returns a Proxy that intercepts messages.create.
18
18
  * The original client is NOT modified.
19
19
  *
20
+ * Patching a proxy that this function returned gives back the same proxy, so the
21
+ * second patch adds no second span.
22
+ *
20
23
  * @param client - An Anthropic client instance
21
24
  * @returns A proxied client with automatic tracing
22
25
  */
@@ -17,6 +17,9 @@
17
17
  * Returns a Proxy that intercepts messages.create.
18
18
  * The original client is NOT modified.
19
19
  *
20
+ * Patching a proxy that this function returned gives back the same proxy, so the
21
+ * second patch adds no second span.
22
+ *
20
23
  * @param client - An Anthropic client instance
21
24
  * @returns A proxied client with automatic tracing
22
25
  */
@@ -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
  }
@@ -1065,6 +1125,13 @@ function readResponseSafely(provider, site, span, read) {
1065
1125
  responseReadFailed(provider, site, error, span);
1066
1126
  }
1067
1127
  }
1128
+ function readRequestSafely(provider, site, span, read) {
1129
+ try {
1130
+ read();
1131
+ } catch (error) {
1132
+ responseReadFailed(provider, site, error, span, "request");
1133
+ }
1134
+ }
1068
1135
 
1069
1136
  // src/utils/stream-proxy.ts
1070
1137
  var HANDOFF = /* @__PURE__ */ new Set(["tee", "toReadableStream", "getReader", "pipeTo", "pipeThrough"]);
@@ -1809,7 +1876,39 @@ function calculateCost(model, promptTokens, completionTokens) {
1809
1876
  function tokenCount(value) {
1810
1877
  return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
1811
1878
  }
1812
- function recordTokenUsage(span, counts, model) {
1879
+ var CACHE_READ_TOKENS_KEY = "gen_ai.usage.cache_read.input_tokens";
1880
+ var CACHE_WRITE_TOKENS_KEY = "gen_ai.usage.cache_write.input_tokens";
1881
+ var REASONING_TOKENS_KEY = "gen_ai.usage.reasoning.output_tokens";
1882
+ function providerInteger(value) {
1883
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : void 0;
1884
+ }
1885
+ function nested(obj, outer, inner) {
1886
+ if (!obj || typeof obj !== "object") return void 0;
1887
+ const details = obj[outer];
1888
+ if (!details || typeof details !== "object") return void 0;
1889
+ return details[inner];
1890
+ }
1891
+ function anthropicUsageDetails(usage) {
1892
+ try {
1893
+ const u = usage && typeof usage === "object" ? usage : {};
1894
+ return {
1895
+ cacheRead: u.cache_read_input_tokens,
1896
+ cacheWrite: u.cache_creation_input_tokens,
1897
+ reasoning: nested(usage, "output_tokens_details", "thinking_tokens")
1898
+ };
1899
+ } catch {
1900
+ return {};
1901
+ }
1902
+ }
1903
+ function recordUsageDetails(span, details) {
1904
+ const cacheRead = providerInteger(details.cacheRead);
1905
+ const cacheWrite = providerInteger(details.cacheWrite);
1906
+ const reasoning = providerInteger(details.reasoning);
1907
+ if (cacheRead !== void 0) span.setAttribute(CACHE_READ_TOKENS_KEY, cacheRead);
1908
+ if (cacheWrite !== void 0) span.setAttribute(CACHE_WRITE_TOKENS_KEY, cacheWrite);
1909
+ if (reasoning !== void 0) span.setAttribute(REASONING_TOKENS_KEY, reasoning);
1910
+ }
1911
+ function recordTokenUsage(span, counts, model, details) {
1813
1912
  const promptTokens = tokenCount(counts.prompt);
1814
1913
  const completionTokens = tokenCount(counts.completion);
1815
1914
  const bothKnown = promptTokens !== void 0 && completionTokens !== void 0;
@@ -1820,6 +1919,7 @@ function recordTokenUsage(span, counts, model) {
1820
1919
  if (completionTokens !== void 0) span.setAttribute("gen_ai.usage.completion_tokens", completionTokens);
1821
1920
  if (totalTokens !== void 0) span.setAttribute("gen_ai.usage.total_tokens", totalTokens);
1822
1921
  if (costUsd !== void 0) span.setAttribute("llm.cost.total_usd", costUsd);
1922
+ if (details) recordUsageDetails(span, details);
1823
1923
  }
1824
1924
 
1825
1925
  // src/utils/assert-client.ts
@@ -1906,7 +2006,7 @@ function enrichSpanFromResponse(span, response) {
1906
2006
  span.setAttribute("gen_ai.response.finish_reasons", [stopReason]);
1907
2007
  }
1908
2008
  if (usage) {
1909
- recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model);
2009
+ recordTokenUsage(span, { prompt: usage.input_tokens, completion: usage.output_tokens }, model, anthropicUsageDetails(usage));
1910
2010
  }
1911
2011
  }
1912
2012
  function createMessagesProxy(originalCreate) {
@@ -1934,9 +2034,23 @@ function createMessagesProxy(originalCreate) {
1934
2034
  const traceContent = tracer.traceContent;
1935
2035
  if (traceContent) {
1936
2036
  captureSystemPrompt(span, params.system);
1937
- capturePromptMessages(span, params.messages, true);
2037
+ readRequestSafely(
2038
+ "anthropic",
2039
+ "capturePromptMessages",
2040
+ span,
2041
+ () => capturePromptMessages(span, params.messages, true)
2042
+ );
1938
2043
  }
1939
2044
  const streamed = {};
2045
+ const noteDetails = (u) => {
2046
+ const d = anthropicUsageDetails(u);
2047
+ const cacheRead = providerInteger(d.cacheRead);
2048
+ const cacheWrite = providerInteger(d.cacheWrite);
2049
+ const reasoning = providerInteger(d.reasoning);
2050
+ if (cacheRead !== void 0) streamed.cacheRead = cacheRead;
2051
+ if (cacheWrite !== void 0) streamed.cacheWrite = cacheWrite;
2052
+ if (reasoning !== void 0) streamed.reasoning = reasoning;
2053
+ };
1940
2054
  return observeProviderCall(
1941
2055
  span,
1942
2056
  {
@@ -1957,19 +2071,29 @@ function createMessagesProxy(originalCreate) {
1957
2071
  streamed.id = e.message.id;
1958
2072
  const u = e.message.usage;
1959
2073
  if (typeof u?.input_tokens === "number") streamed.input = u.input_tokens;
2074
+ noteDetails(u);
1960
2075
  } else if (e?.type === "message_delta") {
1961
2076
  if (e.delta?.stop_reason) streamed.stop_reason = e.delta.stop_reason;
1962
2077
  if (typeof e.usage?.output_tokens === "number") streamed.output = e.usage.output_tokens;
2078
+ noteDetails(e.usage);
1963
2079
  }
1964
2080
  },
1965
2081
  beforeEnd: () => {
1966
- if (streamed.model === void 0 && streamed.input === void 0 && streamed.output === void 0) return;
2082
+ 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) {
2083
+ return;
2084
+ }
1967
2085
  enrichSpanFromResponse(span, {
1968
2086
  model: streamed.model,
1969
2087
  id: streamed.id,
1970
2088
  stop_reason: streamed.stop_reason,
1971
2089
  // A count the stream did not give stays unknown (token-usage.ts).
1972
- usage: { input_tokens: streamed.input, output_tokens: streamed.output }
2090
+ usage: {
2091
+ input_tokens: streamed.input,
2092
+ output_tokens: streamed.output,
2093
+ cache_read_input_tokens: streamed.cacheRead,
2094
+ cache_creation_input_tokens: streamed.cacheWrite,
2095
+ output_tokens_details: streamed.reasoning === void 0 ? void 0 : { thinking_tokens: streamed.reasoning }
2096
+ }
1973
2097
  });
1974
2098
  }
1975
2099
  },
@@ -1977,9 +2101,11 @@ function createMessagesProxy(originalCreate) {
1977
2101
  );
1978
2102
  };
1979
2103
  }
2104
+ var PATCHED = /* @__PURE__ */ new WeakSet();
1980
2105
  function patchAnthropic(client) {
1981
2106
  assertClient("patchAnthropic", client);
1982
- return new Proxy(client, {
2107
+ if (PATCHED.has(client)) return client;
2108
+ const proxy = new Proxy(client, {
1983
2109
  get(target, prop, receiver) {
1984
2110
  const value = Reflect.get(target, prop, receiver);
1985
2111
  if (prop === "messages" && value && typeof value === "object") {
@@ -1996,6 +2122,8 @@ function patchAnthropic(client) {
1996
2122
  return value;
1997
2123
  }
1998
2124
  });
2125
+ PATCHED.add(proxy);
2126
+ return proxy;
1999
2127
  }
2000
2128
  export {
2001
2129
  patchAnthropic