@posthog/ai 7.4.1 → 7.4.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 +233 -223
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +233 -223
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/vercel/index.cjs +237 -227
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +237 -227
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/openai/index.cjs
CHANGED
package/dist/openai/index.mjs
CHANGED
package/dist/vercel/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@ var uuid = require('uuid');
|
|
|
4
4
|
var buffer = require('buffer');
|
|
5
5
|
var core = require('@posthog/core');
|
|
6
6
|
|
|
7
|
-
var version = "7.4.
|
|
7
|
+
var version = "7.4.3";
|
|
8
8
|
|
|
9
9
|
// Type guards for safer type checking
|
|
10
10
|
|
|
@@ -716,242 +716,252 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
716
716
|
}
|
|
717
717
|
};
|
|
718
718
|
|
|
719
|
-
// Create wrapped model
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
doGenerate:
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
const availableTools = extractAvailableToolCalls('vercel', params);
|
|
729
|
-
try {
|
|
730
|
-
const result = await model.doGenerate(params);
|
|
731
|
-
const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
|
|
732
|
-
const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
|
|
733
|
-
const baseURL = ''; // cannot currently get baseURL from vercel
|
|
734
|
-
const content = mapVercelOutput(result.content);
|
|
735
|
-
const latency = (Date.now() - startTime) / 1000;
|
|
736
|
-
const providerMetadata = result.providerMetadata;
|
|
737
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
738
|
-
const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
|
|
739
|
-
|
|
740
|
-
// V2 usage has simple numbers, V3 has objects with .total - normalize both
|
|
741
|
-
const usageObj = result.usage;
|
|
742
|
-
const usage = {
|
|
743
|
-
inputTokens: extractTokenCount(result.usage.inputTokens),
|
|
744
|
-
outputTokens: extractTokenCount(result.usage.outputTokens),
|
|
745
|
-
reasoningTokens: extractReasoningTokens(usageObj),
|
|
746
|
-
cacheReadInputTokens: extractCacheReadTokens(usageObj),
|
|
747
|
-
webSearchCount,
|
|
748
|
-
...additionalTokenValues
|
|
719
|
+
// Create wrapped model using Object.create to preserve the prototype chain
|
|
720
|
+
// This automatically inherits all properties (including getters) from the model
|
|
721
|
+
const wrappedModel = Object.create(model, {
|
|
722
|
+
doGenerate: {
|
|
723
|
+
value: async params => {
|
|
724
|
+
const startTime = Date.now();
|
|
725
|
+
const mergedParams = {
|
|
726
|
+
...mergedOptions,
|
|
727
|
+
...mapVercelParams(params)
|
|
749
728
|
};
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
usage
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
729
|
+
const availableTools = extractAvailableToolCalls('vercel', params);
|
|
730
|
+
try {
|
|
731
|
+
const result = await model.doGenerate(params);
|
|
732
|
+
const modelId = mergedOptions.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
|
|
733
|
+
const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
|
|
734
|
+
const baseURL = ''; // cannot currently get baseURL from vercel
|
|
735
|
+
const content = mapVercelOutput(result.content);
|
|
736
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
737
|
+
const providerMetadata = result.providerMetadata;
|
|
738
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
739
|
+
const webSearchCount = extractWebSearchCount(providerMetadata, result.usage);
|
|
740
|
+
|
|
741
|
+
// V2 usage has simple numbers, V3 has objects with .total - normalize both
|
|
742
|
+
const usageObj = result.usage;
|
|
743
|
+
const usage = {
|
|
744
|
+
inputTokens: extractTokenCount(result.usage.inputTokens),
|
|
745
|
+
outputTokens: extractTokenCount(result.usage.outputTokens),
|
|
746
|
+
reasoningTokens: extractReasoningTokens(usageObj),
|
|
747
|
+
cacheReadInputTokens: extractCacheReadTokens(usageObj),
|
|
748
|
+
webSearchCount,
|
|
749
|
+
...additionalTokenValues
|
|
750
|
+
};
|
|
751
|
+
adjustAnthropicV3CacheTokens(model, provider, usage);
|
|
752
|
+
await sendEventToPosthog({
|
|
753
|
+
client: phClient,
|
|
754
|
+
distinctId: mergedOptions.posthogDistinctId,
|
|
755
|
+
traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
|
|
756
|
+
model: modelId,
|
|
757
|
+
provider: provider,
|
|
758
|
+
input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
|
|
759
|
+
output: content,
|
|
760
|
+
latency,
|
|
761
|
+
baseURL,
|
|
762
|
+
params: mergedParams,
|
|
763
|
+
httpStatus: 200,
|
|
764
|
+
usage,
|
|
765
|
+
tools: availableTools,
|
|
766
|
+
captureImmediate: mergedOptions.posthogCaptureImmediate
|
|
767
|
+
});
|
|
768
|
+
return result;
|
|
769
|
+
} catch (error) {
|
|
770
|
+
const modelId = model.modelId;
|
|
771
|
+
const enrichedError = await sendEventWithErrorToPosthog({
|
|
772
|
+
client: phClient,
|
|
773
|
+
distinctId: mergedOptions.posthogDistinctId,
|
|
774
|
+
traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
|
|
775
|
+
model: modelId,
|
|
776
|
+
provider: model.provider,
|
|
777
|
+
input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
|
|
778
|
+
output: [],
|
|
779
|
+
latency: 0,
|
|
780
|
+
baseURL: '',
|
|
781
|
+
params: mergedParams,
|
|
782
|
+
usage: {
|
|
783
|
+
inputTokens: 0,
|
|
784
|
+
outputTokens: 0
|
|
785
|
+
},
|
|
786
|
+
error: error,
|
|
787
|
+
tools: availableTools,
|
|
788
|
+
captureImmediate: mergedOptions.posthogCaptureImmediate
|
|
789
|
+
});
|
|
790
|
+
throw enrichedError;
|
|
791
|
+
}
|
|
792
|
+
},
|
|
793
|
+
writable: true,
|
|
794
|
+
configurable: true,
|
|
795
|
+
enumerable: false
|
|
791
796
|
},
|
|
792
|
-
doStream:
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
// Map to track in-progress tool calls
|
|
808
|
-
const toolCallsInProgress = new Map();
|
|
809
|
-
try {
|
|
810
|
-
const {
|
|
811
|
-
stream,
|
|
812
|
-
...rest
|
|
813
|
-
} = await model.doStream(params);
|
|
814
|
-
const transformStream = new TransformStream({
|
|
815
|
-
transform(chunk, controller) {
|
|
816
|
-
// Handle streaming patterns - compatible with both V2 and V3
|
|
817
|
-
if (chunk.type === 'text-delta') {
|
|
818
|
-
generatedText += chunk.delta;
|
|
819
|
-
}
|
|
820
|
-
if (chunk.type === 'reasoning-delta') {
|
|
821
|
-
reasoningText += chunk.delta;
|
|
822
|
-
}
|
|
797
|
+
doStream: {
|
|
798
|
+
value: async params => {
|
|
799
|
+
const startTime = Date.now();
|
|
800
|
+
let generatedText = '';
|
|
801
|
+
let reasoningText = '';
|
|
802
|
+
let usage = {};
|
|
803
|
+
let providerMetadata = undefined;
|
|
804
|
+
const mergedParams = {
|
|
805
|
+
...mergedOptions,
|
|
806
|
+
...mapVercelParams(params)
|
|
807
|
+
};
|
|
808
|
+
const modelId = mergedOptions.posthogModelOverride ?? model.modelId;
|
|
809
|
+
const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
|
|
810
|
+
const availableTools = extractAvailableToolCalls('vercel', params);
|
|
811
|
+
const baseURL = ''; // cannot currently get baseURL from vercel
|
|
823
812
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
813
|
+
// Map to track in-progress tool calls
|
|
814
|
+
const toolCallsInProgress = new Map();
|
|
815
|
+
try {
|
|
816
|
+
const {
|
|
817
|
+
stream,
|
|
818
|
+
...rest
|
|
819
|
+
} = await model.doStream(params);
|
|
820
|
+
const transformStream = new TransformStream({
|
|
821
|
+
transform(chunk, controller) {
|
|
822
|
+
// Handle streaming patterns - compatible with both V2 and V3
|
|
823
|
+
if (chunk.type === 'text-delta') {
|
|
824
|
+
generatedText += chunk.delta;
|
|
825
|
+
}
|
|
826
|
+
if (chunk.type === 'reasoning-delta') {
|
|
827
|
+
reasoningText += chunk.delta;
|
|
838
828
|
}
|
|
839
|
-
}
|
|
840
|
-
if (chunk.type === 'tool-input-end') {
|
|
841
|
-
// Tool call is complete, keep it in the map for final processing
|
|
842
|
-
}
|
|
843
|
-
if (chunk.type === 'tool-call') {
|
|
844
|
-
// Direct tool call chunk (complete tool call)
|
|
845
|
-
toolCallsInProgress.set(chunk.toolCallId, {
|
|
846
|
-
toolCallId: chunk.toolCallId,
|
|
847
|
-
toolName: chunk.toolName,
|
|
848
|
-
input: chunk.input
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
if (chunk.type === 'finish') {
|
|
852
|
-
providerMetadata = chunk.providerMetadata;
|
|
853
|
-
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
854
|
-
const chunkUsage = chunk.usage || {};
|
|
855
|
-
usage = {
|
|
856
|
-
inputTokens: extractTokenCount(chunk.usage?.inputTokens),
|
|
857
|
-
outputTokens: extractTokenCount(chunk.usage?.outputTokens),
|
|
858
|
-
reasoningTokens: extractReasoningTokens(chunkUsage),
|
|
859
|
-
cacheReadInputTokens: extractCacheReadTokens(chunkUsage),
|
|
860
|
-
...additionalTokenValues
|
|
861
|
-
};
|
|
862
|
-
}
|
|
863
|
-
controller.enqueue(chunk);
|
|
864
|
-
},
|
|
865
|
-
flush: async () => {
|
|
866
|
-
const latency = (Date.now() - startTime) / 1000;
|
|
867
|
-
// Build content array similar to mapVercelOutput structure
|
|
868
|
-
const content = [];
|
|
869
|
-
if (reasoningText) {
|
|
870
|
-
content.push({
|
|
871
|
-
type: 'reasoning',
|
|
872
|
-
text: truncate(reasoningText)
|
|
873
|
-
});
|
|
874
|
-
}
|
|
875
|
-
if (generatedText) {
|
|
876
|
-
content.push({
|
|
877
|
-
type: 'text',
|
|
878
|
-
text: truncate(generatedText)
|
|
879
|
-
});
|
|
880
|
-
}
|
|
881
829
|
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
830
|
+
// Handle tool call chunks
|
|
831
|
+
if (chunk.type === 'tool-input-start') {
|
|
832
|
+
// Initialize a new tool call
|
|
833
|
+
toolCallsInProgress.set(chunk.id, {
|
|
834
|
+
toolCallId: chunk.id,
|
|
835
|
+
toolName: chunk.toolName,
|
|
836
|
+
input: ''
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
if (chunk.type === 'tool-input-delta') {
|
|
840
|
+
// Accumulate tool call arguments
|
|
841
|
+
const toolCall = toolCallsInProgress.get(chunk.id);
|
|
842
|
+
if (toolCall) {
|
|
843
|
+
toolCall.input += chunk.delta;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (chunk.type === 'tool-input-end') {
|
|
847
|
+
// Tool call is complete, keep it in the map for final processing
|
|
848
|
+
}
|
|
849
|
+
if (chunk.type === 'tool-call') {
|
|
850
|
+
// Direct tool call chunk (complete tool call)
|
|
851
|
+
toolCallsInProgress.set(chunk.toolCallId, {
|
|
852
|
+
toolCallId: chunk.toolCallId,
|
|
853
|
+
toolName: chunk.toolName,
|
|
854
|
+
input: chunk.input
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
if (chunk.type === 'finish') {
|
|
858
|
+
providerMetadata = chunk.providerMetadata;
|
|
859
|
+
const additionalTokenValues = extractAdditionalTokenValues(providerMetadata);
|
|
860
|
+
const chunkUsage = chunk.usage || {};
|
|
861
|
+
usage = {
|
|
862
|
+
inputTokens: extractTokenCount(chunk.usage?.inputTokens),
|
|
863
|
+
outputTokens: extractTokenCount(chunk.usage?.outputTokens),
|
|
864
|
+
reasoningTokens: extractReasoningTokens(chunkUsage),
|
|
865
|
+
cacheReadInputTokens: extractCacheReadTokens(chunkUsage),
|
|
866
|
+
...additionalTokenValues
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
controller.enqueue(chunk);
|
|
870
|
+
},
|
|
871
|
+
flush: async () => {
|
|
872
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
873
|
+
// Build content array similar to mapVercelOutput structure
|
|
874
|
+
const content = [];
|
|
875
|
+
if (reasoningText) {
|
|
885
876
|
content.push({
|
|
886
|
-
type: '
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
877
|
+
type: 'reasoning',
|
|
878
|
+
text: truncate(reasoningText)
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
if (generatedText) {
|
|
882
|
+
content.push({
|
|
883
|
+
type: 'text',
|
|
884
|
+
text: truncate(generatedText)
|
|
892
885
|
});
|
|
893
886
|
}
|
|
894
|
-
}
|
|
895
887
|
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
888
|
+
// Add completed tool calls to content
|
|
889
|
+
for (const toolCall of toolCallsInProgress.values()) {
|
|
890
|
+
if (toolCall.toolName) {
|
|
891
|
+
content.push({
|
|
892
|
+
type: 'tool-call',
|
|
893
|
+
id: toolCall.toolCallId,
|
|
894
|
+
function: {
|
|
895
|
+
name: toolCall.toolName,
|
|
896
|
+
arguments: toolCall.input
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// Structure output like mapVercelOutput does
|
|
903
|
+
const output = content.length > 0 ? [{
|
|
904
|
+
role: 'assistant',
|
|
905
|
+
content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
|
|
906
|
+
}] : [];
|
|
907
|
+
const webSearchCount = extractWebSearchCount(providerMetadata, usage);
|
|
908
|
+
|
|
909
|
+
// Update usage with web search count
|
|
910
|
+
const finalUsage = {
|
|
911
|
+
...usage,
|
|
912
|
+
webSearchCount
|
|
913
|
+
};
|
|
914
|
+
adjustAnthropicV3CacheTokens(model, provider, finalUsage);
|
|
915
|
+
await sendEventToPosthog({
|
|
916
|
+
client: phClient,
|
|
917
|
+
distinctId: mergedOptions.posthogDistinctId,
|
|
918
|
+
traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
|
|
919
|
+
model: modelId,
|
|
920
|
+
provider: provider,
|
|
921
|
+
input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
|
|
922
|
+
output: output,
|
|
923
|
+
latency,
|
|
924
|
+
baseURL,
|
|
925
|
+
params: mergedParams,
|
|
926
|
+
httpStatus: 200,
|
|
927
|
+
usage: finalUsage,
|
|
928
|
+
tools: availableTools,
|
|
929
|
+
captureImmediate: mergedOptions.posthogCaptureImmediate
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
});
|
|
933
|
+
return {
|
|
934
|
+
stream: stream.pipeThrough(transformStream),
|
|
935
|
+
...rest
|
|
936
|
+
};
|
|
937
|
+
} catch (error) {
|
|
938
|
+
const enrichedError = await sendEventWithErrorToPosthog({
|
|
939
|
+
client: phClient,
|
|
940
|
+
distinctId: mergedOptions.posthogDistinctId,
|
|
941
|
+
traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
|
|
942
|
+
model: modelId,
|
|
943
|
+
provider: provider,
|
|
944
|
+
input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
|
|
945
|
+
output: [],
|
|
946
|
+
latency: 0,
|
|
947
|
+
baseURL: '',
|
|
948
|
+
params: mergedParams,
|
|
949
|
+
usage: {
|
|
950
|
+
inputTokens: 0,
|
|
951
|
+
outputTokens: 0
|
|
952
|
+
},
|
|
953
|
+
error: error,
|
|
954
|
+
tools: availableTools,
|
|
955
|
+
captureImmediate: mergedOptions.posthogCaptureImmediate
|
|
956
|
+
});
|
|
957
|
+
throw enrichedError;
|
|
958
|
+
}
|
|
959
|
+
},
|
|
960
|
+
writable: true,
|
|
961
|
+
configurable: true,
|
|
962
|
+
enumerable: false
|
|
953
963
|
}
|
|
954
|
-
};
|
|
964
|
+
});
|
|
955
965
|
return wrappedModel;
|
|
956
966
|
};
|
|
957
967
|
|