@posthog/ai 7.3.1 → 7.4.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.
- package/dist/anthropic/index.cjs +43 -12
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +37 -10
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +40 -14
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +40 -14
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +160 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +152 -78
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +89 -8
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +86 -6
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +48 -33
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +48 -33
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +40 -14
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +40 -14
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +12 -11
package/dist/langchain/index.cjs
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
require('buffer');
|
|
4
4
|
var uuid = require('uuid');
|
|
5
|
+
require('@posthog/core');
|
|
5
6
|
|
|
6
|
-
function
|
|
7
|
+
function _interopNamespace(e) {
|
|
8
|
+
if (e && e.__esModule) return e;
|
|
7
9
|
var n = Object.create(null);
|
|
8
10
|
if (e) {
|
|
9
11
|
Object.keys(e).forEach(function (k) {
|
|
@@ -20,9 +22,9 @@ function _interopNamespaceDefault(e) {
|
|
|
20
22
|
return Object.freeze(n);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
var uuid__namespace = /*#__PURE__*/
|
|
25
|
+
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
24
26
|
|
|
25
|
-
var version = "7.
|
|
27
|
+
var version = "7.4.0";
|
|
26
28
|
|
|
27
29
|
// Type guards for safer type checking
|
|
28
30
|
|
|
@@ -246,6 +248,64 @@ function mapKeys(fields, mapper, map) {
|
|
|
246
248
|
return mapped;
|
|
247
249
|
}
|
|
248
250
|
|
|
251
|
+
//#region src/load/validation.ts
|
|
252
|
+
/**
|
|
253
|
+
* Sentinel key used to mark escaped user objects during serialization.
|
|
254
|
+
*
|
|
255
|
+
* When a plain object contains 'lc' key (which could be confused with LC objects),
|
|
256
|
+
* we wrap it as `{"__lc_escaped__": {...original...}}`.
|
|
257
|
+
*/
|
|
258
|
+
const LC_ESCAPED_KEY = "__lc_escaped__";
|
|
259
|
+
/**
|
|
260
|
+
* Check if an object needs escaping to prevent confusion with LC objects.
|
|
261
|
+
*
|
|
262
|
+
* An object needs escaping if:
|
|
263
|
+
* 1. It has an `'lc'` key (could be confused with LC serialization format)
|
|
264
|
+
* 2. It has only the escape key (would be mistaken for an escaped object)
|
|
265
|
+
*/
|
|
266
|
+
function needsEscaping(obj) {
|
|
267
|
+
return "lc" in obj || Object.keys(obj).length === 1 && LC_ESCAPED_KEY in obj;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Wrap an object in the escape marker.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* {"key": "value"} // becomes {"__lc_escaped__": {"key": "value"}}
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
function escapeObject(obj) {
|
|
278
|
+
return { [LC_ESCAPED_KEY]: obj };
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Check if an object looks like a Serializable instance (duck typing).
|
|
282
|
+
*/
|
|
283
|
+
function isSerializableLike(obj) {
|
|
284
|
+
return obj !== null && typeof obj === "object" && "lc_serializable" in obj && typeof obj.toJSON === "function";
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Escape a value if it needs escaping (contains `lc` key).
|
|
288
|
+
*
|
|
289
|
+
* This is a simpler version of `serializeValue` that doesn't handle Serializable
|
|
290
|
+
* objects - it's meant to be called on kwargs values that have already been
|
|
291
|
+
* processed by `toJSON()`.
|
|
292
|
+
*
|
|
293
|
+
* @param value - The value to potentially escape.
|
|
294
|
+
* @returns The value with any `lc`-containing objects wrapped in escape markers.
|
|
295
|
+
*/
|
|
296
|
+
function escapeIfNeeded(value) {
|
|
297
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
298
|
+
if (isSerializableLike(value)) return value;
|
|
299
|
+
const record = value;
|
|
300
|
+
if (needsEscaping(record)) return escapeObject(record);
|
|
301
|
+
const result = {};
|
|
302
|
+
for (const [key, val] of Object.entries(record)) result[key] = escapeIfNeeded(val);
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
if (Array.isArray(value)) return value.map((item) => escapeIfNeeded(item));
|
|
306
|
+
return value;
|
|
307
|
+
}
|
|
308
|
+
|
|
249
309
|
//#region src/load/serializable.ts
|
|
250
310
|
var serializable_exports = {};
|
|
251
311
|
__export(serializable_exports, {
|
|
@@ -367,11 +427,15 @@ var Serializable = class Serializable {
|
|
|
367
427
|
}
|
|
368
428
|
if (last in read && read[last] !== void 0) write[last] = write[last] || read[last];
|
|
369
429
|
});
|
|
430
|
+
const escapedKwargs = {};
|
|
431
|
+
for (const [key, value] of Object.entries(kwargs)) escapedKwargs[key] = escapeIfNeeded(value);
|
|
432
|
+
const kwargsWithSecrets = Object.keys(secrets).length ? replaceSecrets(escapedKwargs, secrets) : escapedKwargs;
|
|
433
|
+
const processedKwargs = mapKeys(kwargsWithSecrets, keyToJson, aliases);
|
|
370
434
|
return {
|
|
371
435
|
lc: 1,
|
|
372
436
|
type: "constructor",
|
|
373
437
|
id: this.lc_id,
|
|
374
|
-
kwargs:
|
|
438
|
+
kwargs: processedKwargs
|
|
375
439
|
};
|
|
376
440
|
}
|
|
377
441
|
toJSONNotImplemented() {
|
|
@@ -971,7 +1035,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
971
1035
|
|
|
972
1036
|
// Add additional token data to properties
|
|
973
1037
|
if (additionalTokenData.cacheReadInputTokens) {
|
|
974
|
-
eventProperties['$
|
|
1038
|
+
eventProperties['$ai_cache_read_input_tokens'] = additionalTokenData.cacheReadInputTokens;
|
|
1039
|
+
}
|
|
1040
|
+
if (additionalTokenData.cacheWriteInputTokens) {
|
|
1041
|
+
eventProperties['$ai_cache_creation_input_tokens'] = additionalTokenData.cacheWriteInputTokens;
|
|
975
1042
|
}
|
|
976
1043
|
if (additionalTokenData.reasoningTokens) {
|
|
977
1044
|
eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
|
|
@@ -1139,6 +1206,15 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1139
1206
|
additionalTokenData.cacheReadInputTokens = usage.input_token_details.cache_read;
|
|
1140
1207
|
} else if (usage.cachedPromptTokens != null) {
|
|
1141
1208
|
additionalTokenData.cacheReadInputTokens = usage.cachedPromptTokens;
|
|
1209
|
+
} else if (usage.cache_read_input_tokens != null) {
|
|
1210
|
+
additionalTokenData.cacheReadInputTokens = usage.cache_read_input_tokens;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
// Check for cache write/creation tokens in various formats
|
|
1214
|
+
if (usage.cache_creation_input_tokens != null) {
|
|
1215
|
+
additionalTokenData.cacheWriteInputTokens = usage.cache_creation_input_tokens;
|
|
1216
|
+
} else if (usage.input_token_details?.cache_creation != null) {
|
|
1217
|
+
additionalTokenData.cacheWriteInputTokens = usage.input_token_details.cache_creation;
|
|
1142
1218
|
}
|
|
1143
1219
|
|
|
1144
1220
|
// Check for reasoning tokens in various formats
|
|
@@ -1188,8 +1264,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1188
1264
|
additionalTokenData.webSearchCount = webSearchCount;
|
|
1189
1265
|
}
|
|
1190
1266
|
|
|
1191
|
-
// For Anthropic providers, LangChain reports input_tokens as the sum of input
|
|
1267
|
+
// For Anthropic providers, LangChain reports input_tokens as the sum of all input tokens.
|
|
1192
1268
|
// Our cost calculation expects them to be separate for Anthropic, so we subtract cache tokens.
|
|
1269
|
+
// Both cache_read and cache_write tokens should be subtracted since Anthropic's raw API
|
|
1270
|
+
// reports input_tokens as tokens NOT read from or used to create a cache.
|
|
1193
1271
|
// For other providers (OpenAI, etc.), input_tokens already excludes cache tokens as expected.
|
|
1194
1272
|
// Match logic consistent with plugin-server: exact match on provider OR substring match on model
|
|
1195
1273
|
let isAnthropic = false;
|
|
@@ -1198,8 +1276,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1198
1276
|
} else if (model && model.toLowerCase().includes('anthropic')) {
|
|
1199
1277
|
isAnthropic = true;
|
|
1200
1278
|
}
|
|
1201
|
-
if (isAnthropic && parsedUsage.input
|
|
1202
|
-
|
|
1279
|
+
if (isAnthropic && parsedUsage.input) {
|
|
1280
|
+
const cacheTokens = (additionalTokenData.cacheReadInputTokens || 0) + (additionalTokenData.cacheWriteInputTokens || 0);
|
|
1281
|
+
if (cacheTokens > 0) {
|
|
1282
|
+
parsedUsage.input = Math.max(parsedUsage.input - cacheTokens, 0);
|
|
1283
|
+
}
|
|
1203
1284
|
}
|
|
1204
1285
|
return [parsedUsage.input, parsedUsage.output, additionalTokenData];
|
|
1205
1286
|
}
|