@posthog/ai 8.22.3 → 8.22.5

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 (53) hide show
  1. package/dist/adk/index.cjs +40 -8
  2. package/dist/adk/index.cjs.map +1 -1
  3. package/dist/adk/index.mjs +40 -8
  4. package/dist/adk/index.mjs.map +1 -1
  5. package/dist/anthropic/index.cjs +40 -8
  6. package/dist/anthropic/index.cjs.map +1 -1
  7. package/dist/anthropic/index.mjs +40 -8
  8. package/dist/anthropic/index.mjs.map +1 -1
  9. package/dist/claude-agent-sdk/index.cjs +40 -8
  10. package/dist/claude-agent-sdk/index.cjs.map +1 -1
  11. package/dist/claude-agent-sdk/index.mjs +40 -8
  12. package/dist/claude-agent-sdk/index.mjs.map +1 -1
  13. package/dist/gemini/index.cjs +40 -8
  14. package/dist/gemini/index.cjs.map +1 -1
  15. package/dist/gemini/index.mjs +40 -8
  16. package/dist/gemini/index.mjs.map +1 -1
  17. package/dist/genkit/index.cjs +86 -19
  18. package/dist/genkit/index.cjs.map +1 -1
  19. package/dist/genkit/index.mjs +86 -19
  20. package/dist/genkit/index.mjs.map +1 -1
  21. package/dist/index.cjs +40 -8
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.mjs +40 -8
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/langchain/index.cjs +64 -13
  26. package/dist/langchain/index.cjs.map +1 -1
  27. package/dist/langchain/index.mjs +64 -13
  28. package/dist/langchain/index.mjs.map +1 -1
  29. package/dist/langchain/middleware/index.cjs +64 -13
  30. package/dist/langchain/middleware/index.cjs.map +1 -1
  31. package/dist/langchain/middleware/index.mjs +64 -13
  32. package/dist/langchain/middleware/index.mjs.map +1 -1
  33. package/dist/openai/index.cjs +40 -8
  34. package/dist/openai/index.cjs.map +1 -1
  35. package/dist/openai/index.mjs +40 -8
  36. package/dist/openai/index.mjs.map +1 -1
  37. package/dist/openai-agents/index.cjs +2 -2
  38. package/dist/openai-agents/index.cjs.map +1 -1
  39. package/dist/openai-agents/index.mjs +2 -2
  40. package/dist/openai-agents/index.mjs.map +1 -1
  41. package/dist/otel/index.browser.cjs +39 -7
  42. package/dist/otel/index.browser.cjs.map +1 -1
  43. package/dist/otel/index.browser.mjs +39 -7
  44. package/dist/otel/index.browser.mjs.map +1 -1
  45. package/dist/otel/index.cjs +39 -7
  46. package/dist/otel/index.cjs.map +1 -1
  47. package/dist/otel/index.mjs +39 -7
  48. package/dist/otel/index.mjs.map +1 -1
  49. package/dist/vercel/index.cjs +40 -8
  50. package/dist/vercel/index.cjs.map +1 -1
  51. package/dist/vercel/index.mjs +40 -8
  52. package/dist/vercel/index.mjs.map +1 -1
  53. package/package.json +3 -3
@@ -21,20 +21,52 @@ const withoutMcpCredentials = (config) => {
21
21
  for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
22
22
  return redacted;
23
23
  };
24
- const normalizeKey = (key) => key.toLowerCase().replace(/[-_]/g, "");
24
+ const normalizeKey = (key) => key.toLowerCase().replace(/[-_\s]/g, "");
25
25
  const isPlainObject = (value) => {
26
26
  if (!isObject(value)) return false;
27
27
  const prototype = Object.getPrototypeOf(value);
28
28
  return prototype === Object.prototype || prototype === null;
29
29
  };
30
+ const MAX_REDACT_DEPTH = 64;
30
31
  /**
31
- * Replaces the value of every key in `keys` (normalized) with `[REDACTED]`, through plain objects and arrays.
32
- * Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
32
+ * Replaces the value of every key that `isCredentialKey` accepts (normalized) with `[REDACTED]`, through plain
33
+ * objects, arrays and JSON strings. Other strings that name a credential key or form field are replaced whole, and values nested deeper than 64 levels become
34
+ * `[Truncated]`. Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
33
35
  */
34
- function redactKeys(value, keys) {
35
- if (Array.isArray(value)) return value.map((item) => redactKeys(item, keys));
36
+ function redactKeys(value, isCredentialKey, depth = 0) {
37
+ if (depth > MAX_REDACT_DEPTH) return "[Truncated]";
38
+ if (typeof value === "string") return redactJsonString(value, isCredentialKey, depth);
39
+ if (Array.isArray(value)) return value.map((item) => redactKeys(item, isCredentialKey, depth + 1));
36
40
  if (!isPlainObject(value)) return value;
37
- return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, keys.has(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, keys)]));
41
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, isCredentialKey(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, isCredentialKey, depth + 1)]));
42
+ }
43
+ const QUOTED_KEY = /["']([^"'\\\s:]{1,128})\\*["']\s*:/g;
44
+ const FORM_FIELD = /(?:^|[?&#;\s'"])([^&=?#;\s'"]+)=/g;
45
+ function namesCredential(text, isCredentialKey) {
46
+ for (const pattern of [QUOTED_KEY, FORM_FIELD]) for (const [, name] of text.matchAll(pattern)) if (isCredentialKey(normalizeKey(name))) return true;
47
+ return false;
48
+ }
49
+ function redactJsonString(value, isCredentialKey, depth) {
50
+ const namesOne = namesCredential(value, isCredentialKey);
51
+ const trimmed = value.trim();
52
+ if (/^[[{"]/.test(trimmed)) {
53
+ let parsed;
54
+ try {
55
+ parsed = JSON.parse(trimmed);
56
+ } catch {
57
+ return namesOne ? "[REDACTED]" : value;
58
+ }
59
+ const redacted = JSON.stringify(redactKeys(parsed, isCredentialKey, depth + 1));
60
+ return namesOne || !serializesTo(parsed, redacted) ? redacted : value;
61
+ }
62
+ return namesOne ? "[REDACTED]" : value;
63
+ }
64
+ function serializesTo(value, json) {
65
+ try {
66
+ return JSON.stringify(value) === json;
67
+ } catch {
68
+ return false;
69
+ }
38
70
  }
39
71
  const GEMINI_TOOL_FIELDS = new Set([
40
72
  "codeExecution",
@@ -65,7 +97,7 @@ const isGeminiTool = (tool) => Object.keys(tool).map(normalizeKey).some((key) =>
65
97
  */
66
98
  function redactGeminiToolDefinition(tool) {
67
99
  if (!isPlainObject(tool)) return isObject(tool) ? { type: "callable_tool" } : tool;
68
- return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys((0, _posthog_core.toJsonSafeValue)(value), GEMINI_CREDENTIAL_KEYS)]));
100
+ return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys((0, _posthog_core.toJsonSafeValue)(value), (key) => GEMINI_CREDENTIAL_KEYS.has(key))]));
69
101
  }
70
102
  /** Hides the credentials of a remote MCP or Gemini tool definition; everything else is kept as sent. */
71
103
  function redactMcpToolDefinition(tool) {
@@ -435,7 +467,7 @@ function sanitizeValues(obj) {
435
467
  }
436
468
  //#endregion
437
469
  //#region package.json
438
- var version = "8.22.3";
470
+ var version = "8.22.5";
439
471
  //#endregion
440
472
  //#region src/serializeError.ts
441
473
  const DEFAULT_MAX_DEPTH = 3;
@@ -629,6 +661,16 @@ function responsesStopReason(response) {
629
661
  }
630
662
  //#endregion
631
663
  //#region src/langchain/callbacks.ts
664
+ const CACHE_INCLUSIVE_PROVIDERS = /* @__PURE__ */ new Set([
665
+ "openai",
666
+ "azure",
667
+ "openrouter"
668
+ ]);
669
+ const INPUT_OR_CACHE_TOKEN_KEYS = [
670
+ "$ai_input_tokens",
671
+ "$ai_cache_read_input_tokens",
672
+ "$ai_cache_creation_input_tokens"
673
+ ];
632
674
  const isLangGraphControlFlow = (error) => error.is_bubble_up === true;
633
675
  var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.BaseCallbackHandler {
634
676
  constructor(options) {
@@ -923,6 +965,8 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
923
965
  eventProperties["$ai_cache_creation_5m_input_tokens"] = additionalTokenData.cacheWrite5mInputTokens;
924
966
  eventProperties["$ai_cache_creation_1h_input_tokens"] = additionalTokenData.cacheWrite1hInputTokens;
925
967
  }
968
+ const callerReportsTokens = INPUT_OR_CACHE_TOKEN_KEYS.some((key) => Object.prototype.hasOwnProperty.call(this.properties, key));
969
+ if (additionalTokenData.cacheReportingExclusive !== void 0 && !callerReportsTokens) eventProperties["$ai_cache_reporting_exclusive"] = additionalTokenData.cacheReportingExclusive;
926
970
  if (additionalTokenData.reasoningTokens) eventProperties["$ai_reasoning_tokens"] = additionalTokenData.reasoningTokens;
927
971
  if (additionalTokenData.webSearchCount !== void 0) eventProperties["$ai_web_search_count"] = additionalTokenData.webSearchCount;
928
972
  const stopReason = this._extractStopReason(output);
@@ -1074,6 +1118,8 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
1074
1118
  ["completionTokens", "output"],
1075
1119
  ["input_tokens", "input"],
1076
1120
  ["output_tokens", "output"],
1121
+ ["prompt_tokens", "input"],
1122
+ ["completion_tokens", "output"],
1077
1123
  ["prompt_token_count", "input"],
1078
1124
  ["candidates_token_count", "output"],
1079
1125
  ["inputTokenCount", "input"],
@@ -1128,10 +1174,9 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
1128
1174
  let isAnthropic = false;
1129
1175
  if (provider && provider.toLowerCase() === "anthropic") isAnthropic = true;
1130
1176
  else if (model && model.toLowerCase().includes("anthropic")) isAnthropic = true;
1131
- if (isAnthropic && inputIncludesCacheTokens && parsedUsage.input) {
1132
- const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
1133
- if (cacheTokens > 0) parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
1134
- }
1177
+ const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
1178
+ if (isAnthropic && inputIncludesCacheTokens && parsedUsage.input && cacheTokens > 0) parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
1179
+ if (!isAnthropic && (usage.prompt_tokens != null || inputIncludesCacheTokens && usage.cache_read_input_tokens == null && usage.cache_creation_input_tokens == null) && cacheTokens > 0 && parsedUsage.input >= cacheTokens && provider && CACHE_INCLUSIVE_PROVIDERS.has(provider.toLowerCase())) additionalTokenData.cacheReportingExclusive = false;
1135
1180
  return [
1136
1181
  parsedUsage.input,
1137
1182
  parsedUsage.output,
@@ -1144,8 +1189,10 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
1144
1189
  let normalizedGenerationUsage;
1145
1190
  let rawGenerationUsage;
1146
1191
  let fallbackGenerationUsage;
1192
+ let generationCount = 0;
1147
1193
  for (const generation of response.generations ?? []) for (const genChunk of generation) {
1148
1194
  const generationInfo = genChunk.generationInfo ?? {};
1195
+ generationCount++;
1149
1196
  const message = "message" in genChunk ? genChunk.message : void 0;
1150
1197
  const messageUsage = message && typeof message === "object" && "usage_metadata" in message ? message.usage_metadata : void 0;
1151
1198
  normalizedGenerationUsage = firstNonEmptyUsage(normalizedGenerationUsage, messageUsage, generationInfo.usage_metadata);
@@ -1165,7 +1212,11 @@ var LangChainCallbackHandler = class extends _langchain_core_callbacks_base.Base
1165
1212
  if (response.llmOutput != null) for (const key of llmUsageKeys) {
1166
1213
  const llmUsage = response.llmOutput[key];
1167
1214
  if (!isNonEmptyUsage(llmUsage)) continue;
1168
- return this._parseUsageModel(llmUsage, provider, model, key !== "usage", llmUsage);
1215
+ const usageWithCacheDetails = key !== "usage" && generationCount === 1 && llmUsage.input_token_details == null && normalizedGenerationUsage?.input_token_details != null && (llmUsage.promptTokens ?? llmUsage.prompt_tokens ?? llmUsage.input_tokens) === normalizedGenerationUsage.input_tokens ? {
1216
+ ...llmUsage,
1217
+ input_token_details: normalizedGenerationUsage.input_token_details
1218
+ } : llmUsage;
1219
+ return this._parseUsageModel(usageWithCacheDetails, provider, model, key !== "usage", llmUsage);
1169
1220
  }
1170
1221
  if (isNonEmptyUsage(normalizedGenerationUsage)) return this._parseUsageModel(normalizedGenerationUsage, provider, model, true, rawGenerationUsage);
1171
1222
  if (isNonEmptyUsage(rawGenerationUsage)) return this._parseUsageModel(rawGenerationUsage, provider, model, false, rawGenerationUsage);