@posthog/ai 7.2.2 → 7.3.1

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.
@@ -6,7 +6,7 @@ var openai = require('openai');
6
6
  var buffer = require('buffer');
7
7
  var uuid = require('uuid');
8
8
 
9
- var version = "7.2.2";
9
+ var version = "7.3.1";
10
10
 
11
11
  // Type guards for safer type checking
12
12
 
@@ -645,6 +645,7 @@ class WrappedCompletions extends Completions {
645
645
  try {
646
646
  const contentBlocks = [];
647
647
  let accumulatedContent = '';
648
+ let modelFromResponse;
648
649
  let usage = {
649
650
  inputTokens: 0,
650
651
  outputTokens: 0,
@@ -654,6 +655,10 @@ class WrappedCompletions extends Completions {
654
655
  // Map to track in-progress tool calls
655
656
  const toolCallsInProgress = new Map();
656
657
  for await (const chunk of stream1) {
658
+ // Extract model from chunk (Chat Completions chunks have model field)
659
+ if (!modelFromResponse && chunk.model) {
660
+ modelFromResponse = chunk.model;
661
+ }
657
662
  const choice = chunk?.choices?.[0];
658
663
  const chunkWebSearchCount = calculateWebSearchCount(chunk);
659
664
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
@@ -747,7 +752,7 @@ class WrappedCompletions extends Completions {
747
752
  await sendEventToPosthog({
748
753
  client: this.phClient,
749
754
  ...posthogParams,
750
- model: openAIParams.model,
755
+ model: openAIParams.model ?? modelFromResponse,
751
756
  provider: 'openai',
752
757
  input: sanitizeOpenAI(openAIParams.messages),
753
758
  output: formattedOutput,
@@ -801,7 +806,7 @@ class WrappedCompletions extends Completions {
801
806
  await sendEventToPosthog({
802
807
  client: this.phClient,
803
808
  ...posthogParams,
804
- model: openAIParams.model,
809
+ model: openAIParams.model ?? result.model,
805
810
  provider: 'openai',
806
811
  input: sanitizeOpenAI(openAIParams.messages),
807
812
  output: formattedOutput,
@@ -825,7 +830,7 @@ class WrappedCompletions extends Completions {
825
830
  await sendEventToPosthog({
826
831
  client: this.phClient,
827
832
  ...posthogParams,
828
- model: String(openAIParams.model ?? ''),
833
+ model: openAIParams.model,
829
834
  provider: 'openai',
830
835
  input: sanitizeOpenAI(openAIParams.messages),
831
836
  output: [],
@@ -874,6 +879,7 @@ class WrappedResponses extends Responses {
874
879
  (async () => {
875
880
  try {
876
881
  let finalContent = [];
882
+ let modelFromResponse;
877
883
  let usage = {
878
884
  inputTokens: 0,
879
885
  outputTokens: 0,
@@ -881,6 +887,10 @@ class WrappedResponses extends Responses {
881
887
  };
882
888
  for await (const chunk of stream1) {
883
889
  if ('response' in chunk && chunk.response) {
890
+ // Extract model from response object in chunk (for stored prompts)
891
+ if (!modelFromResponse && chunk.response.model) {
892
+ modelFromResponse = chunk.response.model;
893
+ }
884
894
  const chunkWebSearchCount = calculateWebSearchCount(chunk.response);
885
895
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
886
896
  usage.webSearchCount = chunkWebSearchCount;
@@ -904,8 +914,7 @@ class WrappedResponses extends Responses {
904
914
  await sendEventToPosthog({
905
915
  client: this.phClient,
906
916
  ...posthogParams,
907
- //@ts-expect-error
908
- model: openAIParams.model,
917
+ model: openAIParams.model ?? modelFromResponse,
909
918
  provider: 'openai',
910
919
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
911
920
  output: finalContent,
@@ -927,7 +936,6 @@ class WrappedResponses extends Responses {
927
936
  await sendEventToPosthog({
928
937
  client: this.phClient,
929
938
  ...posthogParams,
930
- //@ts-expect-error
931
939
  model: openAIParams.model,
932
940
  provider: 'openai',
933
941
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
@@ -960,8 +968,7 @@ class WrappedResponses extends Responses {
960
968
  await sendEventToPosthog({
961
969
  client: this.phClient,
962
970
  ...posthogParams,
963
- //@ts-expect-error
964
- model: openAIParams.model,
971
+ model: openAIParams.model ?? result.model,
965
972
  provider: 'openai',
966
973
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
967
974
  output: formattedOutput,
@@ -985,7 +992,7 @@ class WrappedResponses extends Responses {
985
992
  await sendEventToPosthog({
986
993
  client: this.phClient,
987
994
  ...posthogParams,
988
- model: String(openAIParams.model ?? ''),
995
+ model: openAIParams.model,
989
996
  provider: 'openai',
990
997
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
991
998
  output: [],
@@ -1022,7 +1029,7 @@ class WrappedResponses extends Responses {
1022
1029
  await sendEventToPosthog({
1023
1030
  client: this.phClient,
1024
1031
  ...posthogParams,
1025
- model: String(openAIParams.model ?? ''),
1032
+ model: openAIParams.model ?? result.model,
1026
1033
  provider: 'openai',
1027
1034
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
1028
1035
  output: result.output,
@@ -1043,7 +1050,7 @@ class WrappedResponses extends Responses {
1043
1050
  await sendEventToPosthog({
1044
1051
  client: this.phClient,
1045
1052
  ...posthogParams,
1046
- model: String(openAIParams.model ?? ''),
1053
+ model: openAIParams.model,
1047
1054
  provider: 'openai',
1048
1055
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
1049
1056
  output: [],
@@ -1233,7 +1240,7 @@ class WrappedTranscriptions extends Transcriptions {
1233
1240
  await sendEventToPosthog({
1234
1241
  client: this.phClient,
1235
1242
  ...posthogParams,
1236
- model: String(openAIParams.model ?? ''),
1243
+ model: openAIParams.model,
1237
1244
  provider: 'openai',
1238
1245
  input: openAIParams.prompt,
1239
1246
  output: result.text,
@@ -1253,7 +1260,7 @@ class WrappedTranscriptions extends Transcriptions {
1253
1260
  await sendEventToPosthog({
1254
1261
  client: this.phClient,
1255
1262
  ...posthogParams,
1256
- model: String(openAIParams.model ?? ''),
1263
+ model: openAIParams.model,
1257
1264
  provider: 'openai',
1258
1265
  input: openAIParams.prompt,
1259
1266
  output: [],