@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/vercel/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { v4 } from 'uuid';
|
|
2
2
|
import { uuidv7 } from '@posthog/core';
|
|
3
3
|
|
|
4
|
-
var version = "7.16.
|
|
4
|
+
var version = "7.16.3";
|
|
5
5
|
|
|
6
6
|
// Type guards for safer type checking
|
|
7
7
|
|
|
@@ -564,12 +564,14 @@ const mapVercelOutput = result => {
|
|
|
564
564
|
};
|
|
565
565
|
}
|
|
566
566
|
if (item.type === 'tool-call') {
|
|
567
|
+
const toolCall = item;
|
|
568
|
+
const rawArgs = toolCall.input ?? toolCall.args ?? toolCall.arguments ?? {};
|
|
567
569
|
return {
|
|
568
570
|
type: 'tool-call',
|
|
569
571
|
id: item.toolCallId,
|
|
570
572
|
function: {
|
|
571
573
|
name: item.toolName,
|
|
572
|
-
arguments:
|
|
574
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs)
|
|
573
575
|
}
|
|
574
576
|
};
|
|
575
577
|
}
|
|
@@ -657,30 +659,6 @@ const extractWebSearchCount = (providerMetadata, usage) => {
|
|
|
657
659
|
});
|
|
658
660
|
};
|
|
659
661
|
|
|
660
|
-
// Extract additional token values from provider metadata
|
|
661
|
-
const extractAdditionalTokenValues = providerMetadata => {
|
|
662
|
-
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
663
|
-
return {
|
|
664
|
-
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
return {};
|
|
668
|
-
};
|
|
669
|
-
|
|
670
|
-
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
671
|
-
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
672
|
-
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
673
|
-
const adjustAnthropicV3CacheTokens = (model, provider, usage) => {
|
|
674
|
-
if (isV3Model(model) && provider.toLowerCase().includes('anthropic')) {
|
|
675
|
-
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
676
|
-
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
677
|
-
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
678
|
-
if (usage.inputTokens && cacheTokens > 0) {
|
|
679
|
-
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
};
|
|
683
|
-
|
|
684
662
|
// Helper to extract numeric token value from V2 (number) or V3 (object with .total) usage formats
|
|
685
663
|
const extractTokenCount = value => {
|
|
686
664
|
if (typeof value === 'number') {
|
|
@@ -718,6 +696,63 @@ const extractCacheReadTokens = usage => {
|
|
|
718
696
|
return undefined;
|
|
719
697
|
};
|
|
720
698
|
|
|
699
|
+
// Helper to extract cache write tokens from V3 (usage.inputTokens.cacheWrite). Providers like
|
|
700
|
+
// Amazon Bedrock populate this standardized field instead of providerMetadata.anthropic.
|
|
701
|
+
const extractCacheWriteTokens = usage => {
|
|
702
|
+
if ('inputTokens' in usage && usage.inputTokens && typeof usage.inputTokens === 'object' && 'cacheWrite' in usage.inputTokens) {
|
|
703
|
+
return usage.inputTokens.cacheWrite;
|
|
704
|
+
}
|
|
705
|
+
return undefined;
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
// Extract additional token values from provider metadata, with a V3 standardized fallback
|
|
709
|
+
// (e.g. Amazon Bedrock exposes cache write tokens via usage.inputTokens.cacheWrite rather
|
|
710
|
+
// than providerMetadata.anthropic.cacheCreationInputTokens). A cacheWrite of 0 is treated
|
|
711
|
+
// as absent so we preserve the pre-fallback event shape on providers that simply omit the
|
|
712
|
+
// field — consumers downstream saw `$ai_cache_creation_input_tokens` missing, not 0.
|
|
713
|
+
const extractAdditionalTokenValues = (providerMetadata, usage) => {
|
|
714
|
+
if (providerMetadata && typeof providerMetadata === 'object' && 'anthropic' in providerMetadata && providerMetadata.anthropic && typeof providerMetadata.anthropic === 'object' && 'cacheCreationInputTokens' in providerMetadata.anthropic) {
|
|
715
|
+
return {
|
|
716
|
+
cacheCreationInputTokens: providerMetadata.anthropic.cacheCreationInputTokens
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
if (usage && typeof usage === 'object') {
|
|
720
|
+
const cacheWrite = extractCacheWriteTokens(usage);
|
|
721
|
+
if (typeof cacheWrite === 'number' && cacheWrite > 0) {
|
|
722
|
+
return {
|
|
723
|
+
cacheCreationInputTokens: cacheWrite
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return {};
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
// Detects Anthropic Claude regardless of host (direct Anthropic, Amazon Bedrock, Google Vertex, etc.).
|
|
731
|
+
// The server applies exclusive cache token accounting based on the model name, so any Claude model
|
|
732
|
+
// needs its V3 input tokens adjusted to exclude cache tokens — not just those routed through a
|
|
733
|
+
// provider whose name contains "anthropic". Accepts the resolved modelId string (not the raw model)
|
|
734
|
+
// so it sees the same id the server does after posthogModelOverride / response.modelId fallbacks.
|
|
735
|
+
const isAnthropicClaudeModel = (modelId, provider) => {
|
|
736
|
+
if (provider.toLowerCase().includes('anthropic')) {
|
|
737
|
+
return true;
|
|
738
|
+
}
|
|
739
|
+
return /claude|anthropic/i.test(modelId);
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// For Anthropic providers in V3, inputTokens.total is the sum of all tokens (uncached + cache read + cache write).
|
|
743
|
+
// Our cost calculation expects inputTokens to be only the uncached portion for Anthropic.
|
|
744
|
+
// This helper subtracts cache tokens from inputTokens for Anthropic V3 models.
|
|
745
|
+
const adjustAnthropicV3CacheTokens = (model, modelId, provider, usage) => {
|
|
746
|
+
if (isV3Model(model) && isAnthropicClaudeModel(modelId, provider)) {
|
|
747
|
+
const cacheReadTokens = usage.cacheReadInputTokens || 0;
|
|
748
|
+
const cacheWriteTokens = usage.cacheCreationInputTokens || 0;
|
|
749
|
+
const cacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
750
|
+
if (usage.inputTokens && cacheTokens > 0) {
|
|
751
|
+
usage.inputTokens = Math.max(usage.inputTokens - cacheTokens, 0);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
|
|
721
756
|
/**
|
|
722
757
|
* Wraps a Vercel AI SDK language model (V2 or V3) with PostHog tracing.
|
|
723
758
|
* Automatically detects the model version and applies appropriate instrumentation.
|
|
@@ -755,7 +790,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
755
790
|
const content = mapVercelOutput(result.content ?? []);
|
|
756
791
|
const latency = (Date.now() - startTime) / 1000;
|
|
757
792
|
const providerMetadata = result.providerMetadata;
|
|
758
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
793
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, result.usage);
|
|
759
794
|
const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
|
|
760
795
|
|
|
761
796
|
// V2 usage has simple numbers, V3 has objects with .total - normalize both
|
|
@@ -786,7 +821,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
786
821
|
...additionalTokenValues,
|
|
787
822
|
rawUsage: rawUsageData
|
|
788
823
|
};
|
|
789
|
-
adjustAnthropicV3CacheTokens(model, provider, usage);
|
|
824
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, usage);
|
|
790
825
|
|
|
791
826
|
// Extract finish reason - V2 returns a string, V3 returns an object with .unified
|
|
792
827
|
const rawFinishReason = result.finishReason;
|
|
@@ -913,8 +948,8 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
913
948
|
}
|
|
914
949
|
if (chunk.type === 'finish') {
|
|
915
950
|
providerMetadata = chunk.providerMetadata;
|
|
916
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
917
951
|
const chunkUsage = chunk.usage || {};
|
|
952
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata, chunkUsage);
|
|
918
953
|
usage = {
|
|
919
954
|
inputTokens: extractTokenCount(chunk.usage?.inputTokens),
|
|
920
955
|
outputTokens: extractTokenCount(chunk.usage?.outputTokens),
|
|
@@ -981,7 +1016,7 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
981
1016
|
providerMetadata
|
|
982
1017
|
}
|
|
983
1018
|
};
|
|
984
|
-
adjustAnthropicV3CacheTokens(model, provider, finalUsage);
|
|
1019
|
+
adjustAnthropicV3CacheTokens(model, modelId, provider, finalUsage);
|
|
985
1020
|
await sendEventToPosthog({
|
|
986
1021
|
client: phClient,
|
|
987
1022
|
distinctId: mergedOptions.posthogDistinctId,
|