@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.
- package/dist/adk/index.cjs +40 -8
- package/dist/adk/index.cjs.map +1 -1
- package/dist/adk/index.mjs +40 -8
- package/dist/adk/index.mjs.map +1 -1
- package/dist/anthropic/index.cjs +40 -8
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +40 -8
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/claude-agent-sdk/index.cjs +40 -8
- package/dist/claude-agent-sdk/index.cjs.map +1 -1
- package/dist/claude-agent-sdk/index.mjs +40 -8
- package/dist/claude-agent-sdk/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +40 -8
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +40 -8
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/genkit/index.cjs +86 -19
- package/dist/genkit/index.cjs.map +1 -1
- package/dist/genkit/index.mjs +86 -19
- package/dist/genkit/index.mjs.map +1 -1
- package/dist/index.cjs +40 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +40 -8
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +64 -13
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +64 -13
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/langchain/middleware/index.cjs +64 -13
- package/dist/langchain/middleware/index.cjs.map +1 -1
- package/dist/langchain/middleware/index.mjs +64 -13
- package/dist/langchain/middleware/index.mjs.map +1 -1
- package/dist/openai/index.cjs +40 -8
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +40 -8
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +2 -2
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +2 -2
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/otel/index.browser.cjs +39 -7
- package/dist/otel/index.browser.cjs.map +1 -1
- package/dist/otel/index.browser.mjs +39 -7
- package/dist/otel/index.browser.mjs.map +1 -1
- package/dist/otel/index.cjs +39 -7
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.mjs +39 -7
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +40 -8
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +40 -8
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -25,20 +25,52 @@ const withoutMcpCredentials = (config) => {
|
|
|
25
25
|
for (const field of MCP_CREDENTIAL_FIELDS) if (field in redacted) redacted[field] = "[REDACTED]";
|
|
26
26
|
return redacted;
|
|
27
27
|
};
|
|
28
|
-
const normalizeKey = (key) => key.toLowerCase().replace(/[-_]/g, "");
|
|
28
|
+
const normalizeKey = (key) => key.toLowerCase().replace(/[-_\s]/g, "");
|
|
29
29
|
const isPlainObject = (value) => {
|
|
30
30
|
if (!isObject(value)) return false;
|
|
31
31
|
const prototype = Object.getPrototypeOf(value);
|
|
32
32
|
return prototype === Object.prototype || prototype === null;
|
|
33
33
|
};
|
|
34
|
+
const MAX_REDACT_DEPTH = 64;
|
|
34
35
|
/**
|
|
35
|
-
* Replaces the value of every key
|
|
36
|
-
*
|
|
36
|
+
* Replaces the value of every key that `isCredentialKey` accepts (normalized) with `[REDACTED]`, through plain
|
|
37
|
+
* 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
|
|
38
|
+
* `[Truncated]`. Class instances, such as the MCP client inside an `mcpToTool()` tool, are returned as they are.
|
|
37
39
|
*/
|
|
38
|
-
function redactKeys(value,
|
|
39
|
-
if (
|
|
40
|
+
function redactKeys(value, isCredentialKey, depth = 0) {
|
|
41
|
+
if (depth > MAX_REDACT_DEPTH) return "[Truncated]";
|
|
42
|
+
if (typeof value === "string") return redactJsonString(value, isCredentialKey, depth);
|
|
43
|
+
if (Array.isArray(value)) return value.map((item) => redactKeys(item, isCredentialKey, depth + 1));
|
|
40
44
|
if (!isPlainObject(value)) return value;
|
|
41
|
-
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key,
|
|
45
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, isCredentialKey(normalizeKey(key)) ? "[REDACTED]" : redactKeys(item, isCredentialKey, depth + 1)]));
|
|
46
|
+
}
|
|
47
|
+
const QUOTED_KEY = /["']([^"'\\\s:]{1,128})\\*["']\s*:/g;
|
|
48
|
+
const FORM_FIELD = /(?:^|[?&#;\s'"])([^&=?#;\s'"]+)=/g;
|
|
49
|
+
function namesCredential(text, isCredentialKey) {
|
|
50
|
+
for (const pattern of [QUOTED_KEY, FORM_FIELD]) for (const [, name] of text.matchAll(pattern)) if (isCredentialKey(normalizeKey(name))) return true;
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
function redactJsonString(value, isCredentialKey, depth) {
|
|
54
|
+
const namesOne = namesCredential(value, isCredentialKey);
|
|
55
|
+
const trimmed = value.trim();
|
|
56
|
+
if (/^[[{"]/.test(trimmed)) {
|
|
57
|
+
let parsed;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(trimmed);
|
|
60
|
+
} catch {
|
|
61
|
+
return namesOne ? "[REDACTED]" : value;
|
|
62
|
+
}
|
|
63
|
+
const redacted = JSON.stringify(redactKeys(parsed, isCredentialKey, depth + 1));
|
|
64
|
+
return namesOne || !serializesTo(parsed, redacted) ? redacted : value;
|
|
65
|
+
}
|
|
66
|
+
return namesOne ? "[REDACTED]" : value;
|
|
67
|
+
}
|
|
68
|
+
function serializesTo(value, json) {
|
|
69
|
+
try {
|
|
70
|
+
return JSON.stringify(value) === json;
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
42
74
|
}
|
|
43
75
|
const GEMINI_TOOL_FIELDS = new Set([
|
|
44
76
|
"codeExecution",
|
|
@@ -69,7 +101,7 @@ const isGeminiTool = (tool) => Object.keys(tool).map(normalizeKey).some((key) =>
|
|
|
69
101
|
*/
|
|
70
102
|
function redactGeminiToolDefinition(tool) {
|
|
71
103
|
if (!isPlainObject(tool)) return isObject(tool) ? { type: "callable_tool" } : tool;
|
|
72
|
-
return Object.fromEntries(Object.entries(tool).map(([key, value]) => [key, normalizeKey(key) === GEMINI_SCHEMA_FIELD ? value : redactKeys(toJsonSafeValue(value), GEMINI_CREDENTIAL_KEYS)]));
|
|
104
|
+
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))]));
|
|
73
105
|
}
|
|
74
106
|
/** Hides the credentials of a remote MCP or Gemini tool definition; everything else is kept as sent. */
|
|
75
107
|
function redactMcpToolDefinition(tool) {
|
|
@@ -439,7 +471,7 @@ function sanitizeValues(obj) {
|
|
|
439
471
|
}
|
|
440
472
|
//#endregion
|
|
441
473
|
//#region package.json
|
|
442
|
-
var version = "8.22.
|
|
474
|
+
var version = "8.22.5";
|
|
443
475
|
//#endregion
|
|
444
476
|
//#region src/serializeError.ts
|
|
445
477
|
const DEFAULT_MAX_DEPTH = 3;
|
|
@@ -633,6 +665,16 @@ function responsesStopReason(response) {
|
|
|
633
665
|
}
|
|
634
666
|
//#endregion
|
|
635
667
|
//#region src/langchain/callbacks.ts
|
|
668
|
+
const CACHE_INCLUSIVE_PROVIDERS = /* @__PURE__ */ new Set([
|
|
669
|
+
"openai",
|
|
670
|
+
"azure",
|
|
671
|
+
"openrouter"
|
|
672
|
+
]);
|
|
673
|
+
const INPUT_OR_CACHE_TOKEN_KEYS = [
|
|
674
|
+
"$ai_input_tokens",
|
|
675
|
+
"$ai_cache_read_input_tokens",
|
|
676
|
+
"$ai_cache_creation_input_tokens"
|
|
677
|
+
];
|
|
636
678
|
const isLangGraphControlFlow = (error) => error.is_bubble_up === true;
|
|
637
679
|
var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
638
680
|
constructor(options) {
|
|
@@ -927,6 +969,8 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
927
969
|
eventProperties["$ai_cache_creation_5m_input_tokens"] = additionalTokenData.cacheWrite5mInputTokens;
|
|
928
970
|
eventProperties["$ai_cache_creation_1h_input_tokens"] = additionalTokenData.cacheWrite1hInputTokens;
|
|
929
971
|
}
|
|
972
|
+
const callerReportsTokens = INPUT_OR_CACHE_TOKEN_KEYS.some((key) => Object.prototype.hasOwnProperty.call(this.properties, key));
|
|
973
|
+
if (additionalTokenData.cacheReportingExclusive !== void 0 && !callerReportsTokens) eventProperties["$ai_cache_reporting_exclusive"] = additionalTokenData.cacheReportingExclusive;
|
|
930
974
|
if (additionalTokenData.reasoningTokens) eventProperties["$ai_reasoning_tokens"] = additionalTokenData.reasoningTokens;
|
|
931
975
|
if (additionalTokenData.webSearchCount !== void 0) eventProperties["$ai_web_search_count"] = additionalTokenData.webSearchCount;
|
|
932
976
|
const stopReason = this._extractStopReason(output);
|
|
@@ -1078,6 +1122,8 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
1078
1122
|
["completionTokens", "output"],
|
|
1079
1123
|
["input_tokens", "input"],
|
|
1080
1124
|
["output_tokens", "output"],
|
|
1125
|
+
["prompt_tokens", "input"],
|
|
1126
|
+
["completion_tokens", "output"],
|
|
1081
1127
|
["prompt_token_count", "input"],
|
|
1082
1128
|
["candidates_token_count", "output"],
|
|
1083
1129
|
["inputTokenCount", "input"],
|
|
@@ -1132,10 +1178,9 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
1132
1178
|
let isAnthropic = false;
|
|
1133
1179
|
if (provider && provider.toLowerCase() === "anthropic") isAnthropic = true;
|
|
1134
1180
|
else if (model && model.toLowerCase().includes("anthropic")) isAnthropic = true;
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
}
|
|
1181
|
+
const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
|
|
1182
|
+
if (isAnthropic && inputIncludesCacheTokens && parsedUsage.input && cacheTokens > 0) parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
|
|
1183
|
+
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;
|
|
1139
1184
|
return [
|
|
1140
1185
|
parsedUsage.input,
|
|
1141
1186
|
parsedUsage.output,
|
|
@@ -1148,8 +1193,10 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
1148
1193
|
let normalizedGenerationUsage;
|
|
1149
1194
|
let rawGenerationUsage;
|
|
1150
1195
|
let fallbackGenerationUsage;
|
|
1196
|
+
let generationCount = 0;
|
|
1151
1197
|
for (const generation of response.generations ?? []) for (const genChunk of generation) {
|
|
1152
1198
|
const generationInfo = genChunk.generationInfo ?? {};
|
|
1199
|
+
generationCount++;
|
|
1153
1200
|
const message = "message" in genChunk ? genChunk.message : void 0;
|
|
1154
1201
|
const messageUsage = message && typeof message === "object" && "usage_metadata" in message ? message.usage_metadata : void 0;
|
|
1155
1202
|
normalizedGenerationUsage = firstNonEmptyUsage(normalizedGenerationUsage, messageUsage, generationInfo.usage_metadata);
|
|
@@ -1169,7 +1216,11 @@ var LangChainCallbackHandler = class extends BaseCallbackHandler {
|
|
|
1169
1216
|
if (response.llmOutput != null) for (const key of llmUsageKeys) {
|
|
1170
1217
|
const llmUsage = response.llmOutput[key];
|
|
1171
1218
|
if (!isNonEmptyUsage(llmUsage)) continue;
|
|
1172
|
-
|
|
1219
|
+
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 ? {
|
|
1220
|
+
...llmUsage,
|
|
1221
|
+
input_token_details: normalizedGenerationUsage.input_token_details
|
|
1222
|
+
} : llmUsage;
|
|
1223
|
+
return this._parseUsageModel(usageWithCacheDetails, provider, model, key !== "usage", llmUsage);
|
|
1173
1224
|
}
|
|
1174
1225
|
if (isNonEmptyUsage(normalizedGenerationUsage)) return this._parseUsageModel(normalizedGenerationUsage, provider, model, true, rawGenerationUsage);
|
|
1175
1226
|
if (isNonEmptyUsage(rawGenerationUsage)) return this._parseUsageModel(rawGenerationUsage, provider, model, false, rawGenerationUsage);
|