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