@posthog/ai 8.22.4 → 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 +40 -8
  26. package/dist/langchain/index.cjs.map +1 -1
  27. package/dist/langchain/index.mjs +40 -8
  28. package/dist/langchain/index.mjs.map +1 -1
  29. package/dist/langchain/middleware/index.cjs +40 -8
  30. package/dist/langchain/middleware/index.cjs.map +1 -1
  31. package/dist/langchain/middleware/index.mjs +40 -8
  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
@@ -1,7 +1,7 @@
1
1
  import { acceptsEventOptions, eventOptionsAsLegacyProperties, toJsonSafeValue, uuidv7 } from "@posthog/core";
2
2
  import { v4 } from "uuid";
3
3
  //#region package.json
4
- var version = "8.22.4";
4
+ var version = "8.22.5";
5
5
  //#endregion
6
6
  //#region src/captureAiEvent.ts
7
7
  /** @internal */
@@ -112,20 +112,52 @@ const withoutMcpCredentials = (config) => {
112
112
  for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
113
113
  return redacted;
114
114
  };
115
- const normalizeKey = (key) => key.toLowerCase().replace(/[-_]/g, "");
115
+ const normalizeKey = (key) => key.toLowerCase().replace(/[-_\s]/g, "");
116
116
  const isPlainObject = (value) => {
117
117
  if (!isObject(value)) return false;
118
118
  const prototype = Object.getPrototypeOf(value);
119
119
  return prototype === Object.prototype || prototype === null;
120
120
  };
121
+ const MAX_REDACT_DEPTH = 64;
121
122
  /**
122
- * Replaces the value of every key in `keys` (normalized) with `[REDACTED]`, through plain objects and arrays.
123
- * Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
123
+ * Replaces the value of every key that `isCredentialKey` accepts (normalized) with `[REDACTED]`, through plain
124
+ * 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
125
+ * `[Truncated]`. Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
124
126
  */
125
- function redactKeys(value, keys) {
126
- if (Array.isArray(value)) return value.map((item) => redactKeys(item, keys));
127
+ function redactKeys(value, isCredentialKey, depth = 0) {
128
+ if (depth > MAX_REDACT_DEPTH) return "[Truncated]";
129
+ if (typeof value === "string") return redactJsonString(value, isCredentialKey, depth);
130
+ if (Array.isArray(value)) return value.map((item) => redactKeys(item, isCredentialKey, depth + 1));
127
131
  if (!isPlainObject(value)) return value;
128
- return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, keys.has(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, keys)]));
132
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, isCredentialKey(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, isCredentialKey, depth + 1)]));
133
+ }
134
+ const QUOTED_KEY = /["']([^"'\\\s:]{1,128})\\*["']\s*:/g;
135
+ const FORM_FIELD = /(?:^|[?&#;\s'"])([^&=?#;\s'"]+)=/g;
136
+ function namesCredential(text, isCredentialKey) {
137
+ for (const pattern of [QUOTED_KEY, FORM_FIELD]) for (const [, name] of text.matchAll(pattern)) if (isCredentialKey(normalizeKey(name))) return true;
138
+ return false;
139
+ }
140
+ function redactJsonString(value, isCredentialKey, depth) {
141
+ const namesOne = namesCredential(value, isCredentialKey);
142
+ const trimmed = value.trim();
143
+ if (/^[[{"]/.test(trimmed)) {
144
+ let parsed;
145
+ try {
146
+ parsed = JSON.parse(trimmed);
147
+ } catch {
148
+ return namesOne ? "[REDACTED]" : value;
149
+ }
150
+ const redacted = JSON.stringify(redactKeys(parsed, isCredentialKey, depth + 1));
151
+ return namesOne || !serializesTo(parsed, redacted) ? redacted : value;
152
+ }
153
+ return namesOne ? "[REDACTED]" : value;
154
+ }
155
+ function serializesTo(value, json) {
156
+ try {
157
+ return JSON.stringify(value) === json;
158
+ } catch {
159
+ return false;
160
+ }
129
161
  }
130
162
  const GEMINI_TOOL_FIELDS = new Set([
131
163
  "codeExecution",
@@ -156,7 +188,7 @@ const isGeminiTool = (tool) => Object.keys(tool).map(normalizeKey).some((key) =>
156
188
  */
157
189
  function redactGeminiToolDefinition(tool) {
158
190
  if (!isPlainObject(tool)) return isObject(tool) ? { type: "callable_tool" } : tool;
159
- return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys(toJsonSafeValue(value), GEMINI_CREDENTIAL_KEYS)]));
191
+ return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys(toJsonSafeValue(value), (key) => GEMINI_CREDENTIAL_KEYS.has(key))]));
160
192
  }
161
193
  /** Hides the credentials of a remote MCP or Gemini tool definition; everything else is kept as sent. */
162
194
  function redactMcpToolDefinition(tool) {
@@ -846,16 +878,51 @@ const MODEL_PARAMETER_KEYS = [
846
878
  "topP",
847
879
  "stopSequences"
848
880
  ];
849
- const CREDENTIAL_KEYS = new Set([
850
- "apiKey",
851
- "accessToken",
852
- "authorization",
853
- "clientSecret",
854
- "headers",
855
- "password",
881
+ const CREDENTIAL_KEY_SUFFIXES = [
882
+ "token",
883
+ "tokens",
856
884
  "secret",
857
- "token"
858
- ].map(normalizeKey));
885
+ "secrets",
886
+ "secretkey",
887
+ "password",
888
+ "passwords",
889
+ "passwd",
890
+ "apikey",
891
+ "apikeys",
892
+ "privatekey",
893
+ "accesskey",
894
+ "authorization",
895
+ "cookie",
896
+ "cookies",
897
+ "credential",
898
+ "credentials",
899
+ "headers"
900
+ ];
901
+ const TOKEN_COUNT_KEYS = /* @__PURE__ */ new Set([
902
+ "maxtokens",
903
+ "maxoutputtokens",
904
+ "maxcompletiontokens",
905
+ "inputtokens",
906
+ "outputtokens",
907
+ "totaltokens",
908
+ "prompttokens",
909
+ "completiontokens",
910
+ "reasoningtokens",
911
+ "thoughtstokens",
912
+ "cachedtokens",
913
+ "cachedcontenttokens",
914
+ "cachecreationinputtokens",
915
+ "cachereadinputtokens",
916
+ "budgettokens",
917
+ "thinkingtokens",
918
+ "audiotokens",
919
+ "acceptedpredictiontokens",
920
+ "rejectedpredictiontokens"
921
+ ]);
922
+ const isCredentialKey = (key) => {
923
+ const name = key.replace(/header$/, "");
924
+ return !TOKEN_COUNT_KEYS.has(name) && CREDENTIAL_KEY_SUFFIXES.some((suffix) => name.endsWith(suffix));
925
+ };
859
926
  const PROVIDER_NAMES = /* @__PURE__ */ new Map([["googleai", "gemini"], ["vertexai", "vertex"]]);
860
927
  const STACK_ERROR_NAME = /^([A-Za-z_$][\w$]*)(?=:|\n|$)/;
861
928
  /**
@@ -1030,13 +1097,13 @@ var PostHogGenkitSpanProcessor = class {
1030
1097
  }
1031
1098
  _formatState(span, key) {
1032
1099
  if (isPrivacyModeEnabled(this._client, this._privacyMode)) return null;
1033
- return formatToolResult(redactKeys(parseAttribute(span, key), CREDENTIAL_KEYS), this._client, MAX_OUTPUT_SIZE);
1100
+ return formatToolResult(redactKeys(parseAttribute(span, key), isCredentialKey), this._client, MAX_OUTPUT_SIZE);
1034
1101
  }
1035
1102
  _formatMessages(messages) {
1036
1103
  return formatToolResult(redactKeys(messages.map((message) => ({
1037
1104
  role: message.role === "model" ? "assistant" : message.role,
1038
1105
  content: (message.content ?? []).map((part) => formatPart(part, this._client))
1039
- })), CREDENTIAL_KEYS), this._client, MAX_OUTPUT_SIZE);
1106
+ })), isCredentialKey), this._client, MAX_OUTPUT_SIZE);
1040
1107
  }
1041
1108
  };
1042
1109
  function formatPart(part, client) {