@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.
@@ -7,7 +7,7 @@ var buffer = require('buffer');
7
7
  var uuid = require('uuid');
8
8
  var core = require('@posthog/core');
9
9
 
10
- var version = "7.4.1";
10
+ var version = "7.4.3";
11
11
 
12
12
  // Type guards for safer type checking
13
13
 
@@ -3,7 +3,7 @@ import { Buffer } from 'buffer';
3
3
  import { v4 } from 'uuid';
4
4
  import { uuidv7 } from '@posthog/core';
5
5
 
6
- var version = "7.4.1";
6
+ var version = "7.4.3";
7
7
 
8
8
  // Type guards for safer type checking
9
9
 
@@ -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.1";
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 that preserves the original type
720
- const wrappedModel = {
721
- ...model,
722
- doGenerate: async params => {
723
- const startTime = Date.now();
724
- const mergedParams = {
725
- ...mergedOptions,
726
- ...mapVercelParams(params)
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
- adjustAnthropicV3CacheTokens(model, provider, usage);
751
- await sendEventToPosthog({
752
- client: phClient,
753
- distinctId: mergedOptions.posthogDistinctId,
754
- traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
755
- model: modelId,
756
- provider: provider,
757
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
758
- output: content,
759
- latency,
760
- baseURL,
761
- params: mergedParams,
762
- httpStatus: 200,
763
- usage,
764
- tools: availableTools,
765
- captureImmediate: mergedOptions.posthogCaptureImmediate
766
- });
767
- return result;
768
- } catch (error) {
769
- const modelId = model.modelId;
770
- const enrichedError = await sendEventWithErrorToPosthog({
771
- client: phClient,
772
- distinctId: mergedOptions.posthogDistinctId,
773
- traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
774
- model: modelId,
775
- provider: model.provider,
776
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
777
- output: [],
778
- latency: 0,
779
- baseURL: '',
780
- params: mergedParams,
781
- usage: {
782
- inputTokens: 0,
783
- outputTokens: 0
784
- },
785
- error: error,
786
- tools: availableTools,
787
- captureImmediate: mergedOptions.posthogCaptureImmediate
788
- });
789
- throw enrichedError;
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: async params => {
793
- const startTime = Date.now();
794
- let generatedText = '';
795
- let reasoningText = '';
796
- let usage = {};
797
- let providerMetadata = undefined;
798
- const mergedParams = {
799
- ...mergedOptions,
800
- ...mapVercelParams(params)
801
- };
802
- const modelId = mergedOptions.posthogModelOverride ?? model.modelId;
803
- const provider = mergedOptions.posthogProviderOverride ?? extractProvider(model);
804
- const availableTools = extractAvailableToolCalls('vercel', params);
805
- const baseURL = ''; // cannot currently get baseURL from vercel
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
- // Handle tool call chunks
825
- if (chunk.type === 'tool-input-start') {
826
- // Initialize a new tool call
827
- toolCallsInProgress.set(chunk.id, {
828
- toolCallId: chunk.id,
829
- toolName: chunk.toolName,
830
- input: ''
831
- });
832
- }
833
- if (chunk.type === 'tool-input-delta') {
834
- // Accumulate tool call arguments
835
- const toolCall = toolCallsInProgress.get(chunk.id);
836
- if (toolCall) {
837
- toolCall.input += chunk.delta;
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
- // Add completed tool calls to content
883
- for (const toolCall of toolCallsInProgress.values()) {
884
- if (toolCall.toolName) {
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: 'tool-call',
887
- id: toolCall.toolCallId,
888
- function: {
889
- name: toolCall.toolName,
890
- arguments: toolCall.input
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
- // Structure output like mapVercelOutput does
897
- const output = content.length > 0 ? [{
898
- role: 'assistant',
899
- content: content.length === 1 && content[0].type === 'text' ? content[0].text : content
900
- }] : [];
901
- const webSearchCount = extractWebSearchCount(providerMetadata, usage);
902
-
903
- // Update usage with web search count
904
- const finalUsage = {
905
- ...usage,
906
- webSearchCount
907
- };
908
- adjustAnthropicV3CacheTokens(model, provider, finalUsage);
909
- await sendEventToPosthog({
910
- client: phClient,
911
- distinctId: mergedOptions.posthogDistinctId,
912
- traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
913
- model: modelId,
914
- provider: provider,
915
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
916
- output: output,
917
- latency,
918
- baseURL,
919
- params: mergedParams,
920
- httpStatus: 200,
921
- usage: finalUsage,
922
- tools: availableTools,
923
- captureImmediate: mergedOptions.posthogCaptureImmediate
924
- });
925
- }
926
- });
927
- return {
928
- stream: stream.pipeThrough(transformStream),
929
- ...rest
930
- };
931
- } catch (error) {
932
- const enrichedError = await sendEventWithErrorToPosthog({
933
- client: phClient,
934
- distinctId: mergedOptions.posthogDistinctId,
935
- traceId: mergedOptions.posthogTraceId ?? uuid.v4(),
936
- model: modelId,
937
- provider: provider,
938
- input: mergedOptions.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
939
- output: [],
940
- latency: 0,
941
- baseURL: '',
942
- params: mergedParams,
943
- usage: {
944
- inputTokens: 0,
945
- outputTokens: 0
946
- },
947
- error: error,
948
- tools: availableTools,
949
- captureImmediate: mergedOptions.posthogCaptureImmediate
950
- });
951
- throw enrichedError;
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