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
@@ -24,6 +24,9 @@
24
24
  * Returns a Proxy that intercepts chat.completions.create and embeddings.create.
25
25
  * The original client is NOT modified.
26
26
  *
27
+ * Patching a proxy that this function returned gives back the same proxy, so the
28
+ * second patch adds no second span.
29
+ *
27
30
  * @param client - An OpenAI client instance
28
31
  * @returns A proxied client with automatic tracing
29
32
  */
@@ -24,6 +24,9 @@
24
24
  * Returns a Proxy that intercepts chat.completions.create and embeddings.create.
25
25
  * The original client is NOT modified.
26
26
  *
27
+ * Patching a proxy that this function returned gives back the same proxy, so the
28
+ * second patch adds no second span.
29
+ *
27
30
  * @param client - An OpenAI client instance
28
31
  * @returns A proxied client with automatic tracing
29
32
  */
@@ -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,38 @@ 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 openAIUsageDetails(usage) {
1892
+ try {
1893
+ return {
1894
+ cacheRead: nested(usage, "prompt_tokens_details", "cached_tokens"),
1895
+ cacheWrite: nested(usage, "prompt_tokens_details", "cache_write_tokens"),
1896
+ reasoning: nested(usage, "completion_tokens_details", "reasoning_tokens")
1897
+ };
1898
+ } catch {
1899
+ return {};
1900
+ }
1901
+ }
1902
+ function recordUsageDetails(span, details) {
1903
+ const cacheRead = providerInteger(details.cacheRead);
1904
+ const cacheWrite = providerInteger(details.cacheWrite);
1905
+ const reasoning = providerInteger(details.reasoning);
1906
+ if (cacheRead !== void 0) span.setAttribute(CACHE_READ_TOKENS_KEY, cacheRead);
1907
+ if (cacheWrite !== void 0) span.setAttribute(CACHE_WRITE_TOKENS_KEY, cacheWrite);
1908
+ if (reasoning !== void 0) span.setAttribute(REASONING_TOKENS_KEY, reasoning);
1909
+ }
1910
+ function recordTokenUsage(span, counts, model, details) {
1813
1911
  const promptTokens = tokenCount(counts.prompt);
1814
1912
  const completionTokens = tokenCount(counts.completion);
1815
1913
  const bothKnown = promptTokens !== void 0 && completionTokens !== void 0;
@@ -1820,6 +1918,7 @@ function recordTokenUsage(span, counts, model) {
1820
1918
  if (completionTokens !== void 0) span.setAttribute("gen_ai.usage.completion_tokens", completionTokens);
1821
1919
  if (totalTokens !== void 0) span.setAttribute("gen_ai.usage.total_tokens", totalTokens);
1822
1920
  if (costUsd !== void 0) span.setAttribute("llm.cost.total_usd", costUsd);
1921
+ if (details) recordUsageDetails(span, details);
1823
1922
  }
1824
1923
 
1825
1924
  // src/utils/assert-client.ts
@@ -1934,7 +2033,12 @@ function enrichSpanFromResponse(span, response, provider) {
1934
2033
  }
1935
2034
  }
1936
2035
  if (usage) {
1937
- recordTokenUsage(span, { prompt: usage.prompt_tokens, completion: usage.completion_tokens, total: usage.total_tokens }, model);
2036
+ recordTokenUsage(
2037
+ span,
2038
+ { prompt: usage.prompt_tokens, completion: usage.completion_tokens, total: usage.total_tokens },
2039
+ model,
2040
+ openAIUsageDetails(usage)
2041
+ );
1938
2042
  }
1939
2043
  }
1940
2044
  function createChatCompletionProxy(originalCreate, provider) {
@@ -1971,7 +2075,14 @@ function createChatCompletionProxy(originalCreate, provider) {
1971
2075
  span.setLlmFields({ provider, model });
1972
2076
  enrichSpanFromRequest(span, params, provider);
1973
2077
  const traceContent = tracer.traceContent;
1974
- if (traceContent) capturePromptMessages(span, params.messages, false);
2078
+ if (traceContent) {
2079
+ readRequestSafely(
2080
+ "openai",
2081
+ "capturePromptMessages",
2082
+ span,
2083
+ () => capturePromptMessages(span, params.messages, false)
2084
+ );
2085
+ }
1975
2086
  try {
1976
2087
  if (fixCtx) {
1977
2088
  for (const result of fixCtx.appliedFixes) {
@@ -2039,10 +2150,12 @@ function createEmbeddingsProxy(originalCreate, provider) {
2039
2150
  );
2040
2151
  };
2041
2152
  }
2153
+ var PATCHED = /* @__PURE__ */ new WeakSet();
2042
2154
  function patchOpenAI(client) {
2043
2155
  assertClient("patchOpenAI", client);
2156
+ if (PATCHED.has(client)) return client;
2044
2157
  const provider = detectProvider(client);
2045
- return new Proxy(client, {
2158
+ const proxy = new Proxy(client, {
2046
2159
  get(target, prop, receiver) {
2047
2160
  const value = Reflect.get(target, prop, receiver);
2048
2161
  if (prop === "chat" && value && typeof value === "object") {
@@ -2078,6 +2191,8 @@ function patchOpenAI(client) {
2078
2191
  return value;
2079
2192
  }
2080
2193
  });
2194
+ PATCHED.add(proxy);
2195
+ return proxy;
2081
2196
  }
2082
2197
  export {
2083
2198
  patchOpenAI