@posthog/ai 7.16.1 → 7.16.3
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 +1 -1
- package/dist/anthropic/index.mjs +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.mjs +1 -1
- package/dist/index.cjs +61 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +61 -28
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/openai/index.cjs +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/vercel/index.cjs +65 -30
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +65 -30
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/anthropic/index.cjs
CHANGED
|
@@ -10,7 +10,7 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
10
10
|
|
|
11
11
|
var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
|
|
12
12
|
|
|
13
|
-
var version = "7.16.
|
|
13
|
+
var version = "7.16.3";
|
|
14
14
|
|
|
15
15
|
// Type guards for safer type checking
|
|
16
16
|
|
package/dist/anthropic/index.mjs
CHANGED
package/dist/gemini/index.cjs
CHANGED
package/dist/gemini/index.mjs
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -29,7 +29,7 @@ function _interopNamespace(e) {
|
|
|
29
29
|
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
30
30
|
var AnthropicOriginal__default = /*#__PURE__*/_interopDefault(AnthropicOriginal);
|
|
31
31
|
|
|
32
|
-
var version = "7.16.
|
|
32
|
+
var version = "7.16.3";
|
|
33
33
|
|
|
34
34
|
// Type guards for safer type checking
|
|
35
35
|
const isString = value => {
|
|
@@ -2213,12 +2213,14 @@ const mapVercelOutput = result => {
|
|
|
2213
2213
|
};
|
|
2214
2214
|
}
|
|
2215
2215
|
if (item.type === 'tool-call') {
|
|
2216
|
+
const toolCall = item;
|
|
2217
|
+
const rawArgs = toolCall.input ?? toolCall.args ?? toolCall.arguments ?? {};
|
|
2216
2218
|
return {
|
|
2217
2219
|
type: 'tool-call',
|
|
2218
2220
|
id: item.toolCallId,
|
|
2219
2221
|
function: {
|
|
2220
2222
|
name: item.toolName,
|
|
2221
|
-
arguments:
|
|
2223
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs)
|
|
2222
2224
|
}
|
|
2223
2225
|
};
|
|
2224
2226
|
}
|
|
@@ -2302,28 +2304,6 @@ const extractWebSearchCount = (providerMetadata, usage) => {
|
|
|
2302
2304
|
providerMetadata
|
|
2303
2305
|
});
|
|
2304
2306
|
};
|
|
2305
|
-
// Extract additional token values from provider metadata
|
|
2306
|
-
const extractAdditionalTokenValues = providerMetadata => {
|
|
2307
|
-
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
2308
|
-
return {
|
|
2309
|
-
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
2310
|
-
};
|
|
2311
|
-
}
|
|
2312
|
-
return {};
|
|
2313
|
-
};
|
|
2314
|
-
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
2315
|
-
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
2316
|
-
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
2317
|
-
const adjustAnthropicV3CacheTokens = (model, provider, usage) => {
|
|
2318
|
-
if (isV3Model(model) && provider.toLowerCase().includes('anthropic')) {
|
|
2319
|
-
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
2320
|
-
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
2321
|
-
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
2322
|
-
if (usage.inputTokens && cacheTokens > 0) {
|
|
2323
|
-
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
2324
|
-
}
|
|
2325
|
-
}
|
|
2326
|
-
};
|
|
2327
2307
|
// Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
|
|
2328
2308
|
const extractTokenCount = value => {
|
|
2329
2309
|
if (typeof value === 'number') {
|
|
@@ -2358,6 +2338,59 @@ const extractCacheReadTokens = usage => {
|
|
|
2358
2338
|
}
|
|
2359
2339
|
return undefined;
|
|
2360
2340
|
};
|
|
2341
|
+
// Helper to extract cache write tokens from V3 (usage.inputTokens.cacheWrite). Providers like
|
|
2342
|
+
// Amazon Bedrock populate this standardized field instead of providerMetadata.anthropic.
|
|
2343
|
+
const extractCacheWriteTokens = usage => {
|
|
2344
|
+
if ('inputTokens' in usage && usage.inputTokens && typeof usage.inputTokens === 'object' && 'cacheWrite' in usage.inputTokens) {
|
|
2345
|
+
return usage.inputTokens.cacheWrite;
|
|
2346
|
+
}
|
|
2347
|
+
return undefined;
|
|
2348
|
+
};
|
|
2349
|
+
// Extract additional token values from provider metadata, with a V3 standardized fallback
|
|
2350
|
+
// (e.g. Amazon Bedrock exposes cache write tokens via usage.inputTokens.cacheWrite rather
|
|
2351
|
+
// than providerMetadata.anthropic.cacheCreationInputTokens). A cacheWrite of 0 is treated
|
|
2352
|
+
// as absent so we preserve the pre-fallback event shape on providers that simply omit the
|
|
2353
|
+
// field — consumers downstream saw `$ai_cache_creation_input_tokens` missing, not 0.
|
|
2354
|
+
const extractAdditionalTokenValues = (providerMetadata, usage) => {
|
|
2355
|
+
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
2356
|
+
return {
|
|
2357
|
+
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
2358
|
+
};
|
|
2359
|
+
}
|
|
2360
|
+
if (usage && typeof usage === 'object') {
|
|
2361
|
+
const cacheWrite = extractCacheWriteTokens(usage);
|
|
2362
|
+
if (typeof cacheWrite === 'number' && cacheWrite > 0) {
|
|
2363
|
+
return {
|
|
2364
|
+
cacheCreationInputTokens: cacheWrite
|
|
2365
|
+
};
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
return {};
|
|
2369
|
+
};
|
|
2370
|
+
// Detects Anthropic Claude regardless of host (direct Anthropic, Amazon Bedrock, Google Vertex, etc.).
|
|
2371
|
+
// The server applies exclusive cache token accounting based on the model name, so any Claude model
|
|
2372
|
+
// needs its V3 input tokens adjusted to exclude cache tokens — not just those routed through a
|
|
2373
|
+
// provider whose name contains "anthropic". Accepts the resolved modelId string (not the raw model)
|
|
2374
|
+
// so it sees the same id the server does after posthogModelOverride / response.modelId fallbacks.
|
|
2375
|
+
const isAnthropicClaudeModel = (modelId, provider) => {
|
|
2376
|
+
if (provider.toLowerCase().includes('anthropic')) {
|
|
2377
|
+
return true;
|
|
2378
|
+
}
|
|
2379
|
+
return /claude|anthropic/i.test(modelId);
|
|
2380
|
+
};
|
|
2381
|
+
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
2382
|
+
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
2383
|
+
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
2384
|
+
const adjustAnthropicV3CacheTokens = (model, modelId, provider, usage) => {
|
|
2385
|
+
if (isV3Model(model) && isAnthropicClaudeModel(modelId, provider)) {
|
|
2386
|
+
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
2387
|
+
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
2388
|
+
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
2389
|
+
if (usage.inputTokens && cacheTokens > 0) {
|
|
2390
|
+
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
};
|
|
2361
2394
|
/**
|
|
2362
2395
|
* Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
|
|
2363
2396
|
* Automatically detects the model version and applies appropriate instrumentation.
|
|
@@ -2394,7 +2427,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2394
2427
|
const content = mapVercelOutput(result.content ?? []);
|
|
2395
2428
|
const latency = (Date.now() - startTime) / 1000;
|
|
2396
2429
|
const providerMetadata = result.providerMetadata;
|
|
2397
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
2430
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
|
|
2398
2431
|
const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
|
|
2399
2432
|
// V2 usage has simple numbers, V3 has objects with .total - normalize both
|
|
2400
2433
|
const usageObj = result.usage;
|
|
@@ -2422,7 +2455,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2422
2455
|
...additionalTokenValues,
|
|
2423
2456
|
rawUsage: rawUsageData
|
|
2424
2457
|
};
|
|
2425
|
-
adjustAnthropicV3CacheTokens(model, provider, usage);
|
|
2458
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, usage);
|
|
2426
2459
|
// Extract finish reason - V2 returns a string, V3 returns an object with .unified
|
|
2427
2460
|
const rawFinishReason = result.finishReason;
|
|
2428
2461
|
const finishReasonStr = typeof rawFinishReason === 'string' ? rawFinishReason : rawFinishReason && typeof rawFinishReason === 'object' && 'unified' in rawFinishReason ? String(rawFinishReason.unified) : undefined;
|
|
@@ -2546,8 +2579,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2546
2579
|
}
|
|
2547
2580
|
if (chunk.type === 'finish') {
|
|
2548
2581
|
providerMetadata = chunk.providerMetadata;
|
|
2549
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
2550
2582
|
const chunkUsage = chunk.usage || {};
|
|
2583
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, chunkUsage);
|
|
2551
2584
|
usage = {
|
|
2552
2585
|
inputTokens: extractTokenCount(chunk.usage?.inputTokens),
|
|
2553
2586
|
outputTokens: extractTokenCount(chunk.usage?.outputTokens),
|
|
@@ -2610,7 +2643,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2610
2643
|
providerMetadata
|
|
2611
2644
|
}
|
|
2612
2645
|
};
|
|
2613
|
-
adjustAnthropicV3CacheTokens(model, provider, finalUsage);
|
|
2646
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, finalUsage);
|
|
2614
2647
|
await sendEventToPosthog({
|
|
2615
2648
|
client: phClient,
|
|
2616
2649
|
distinctId: mergedOptions.posthogDistinctId,
|