@posthog/ai 7.2.1 → 7.3.0

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.
@@ -2,7 +2,7 @@ import { OpenAI } from 'openai';
2
2
  import { Buffer } from 'buffer';
3
3
  import { v4 } from 'uuid';
4
4
 
5
- var version = "7.2.1";
5
+ var version = "7.3.0";
6
6
 
7
7
  // Type guards for safer type checking
8
8
 
@@ -81,6 +81,14 @@ const formatResponseOpenAI = response => {
81
81
  });
82
82
  }
83
83
  }
84
+
85
+ // Handle audio output (gpt-4o-audio-preview)
86
+ if (choice.message.audio) {
87
+ content.push({
88
+ type: 'audio',
89
+ ...choice.message.audio
90
+ });
91
+ }
84
92
  }
85
93
  if (content.length > 0) {
86
94
  output.push({
@@ -462,6 +470,15 @@ function formatOpenAIResponsesInput(input, instructions) {
462
470
 
463
471
  const REDACTED_IMAGE_PLACEHOLDER = '[base64 image redacted]';
464
472
 
473
+ // ============================================
474
+ // Multimodal Feature Toggle
475
+ // ============================================
476
+
477
+ const isMultimodalEnabled = () => {
478
+ const val = process.env._INTERNAL_LLMA_MULTIMODAL || '';
479
+ return val.toLowerCase() === 'true' || val === '1' || val.toLowerCase() === 'yes';
480
+ };
481
+
465
482
  // ============================================
466
483
  // Base64 Detection Helpers
467
484
  // ============================================
@@ -489,6 +506,7 @@ const isRawBase64 = str => {
489
506
  return str.length > 20 && /^[A-Za-z0-9+/]+=*$/.test(str);
490
507
  };
491
508
  function redactBase64DataUrl(str) {
509
+ if (isMultimodalEnabled()) return str;
492
510
  if (!isString(str)) return str;
493
511
 
494
512
  // Check for data URL format
@@ -551,6 +569,15 @@ const sanitizeOpenAIImage = item => {
551
569
  }
552
570
  };
553
571
  }
572
+
573
+ // Handle audio format
574
+ if (item.type === 'audio' && 'data' in item) {
575
+ if (isMultimodalEnabled()) return item;
576
+ return {
577
+ ...item,
578
+ data: REDACTED_IMAGE_PLACEHOLDER
579
+ };
580
+ }
554
581
  return item;
555
582
  };
556
583
 
@@ -614,6 +641,7 @@ class WrappedCompletions extends Completions {
614
641
  try {
615
642
  const contentBlocks = [];
616
643
  let accumulatedContent = '';
644
+ let modelFromResponse;
617
645
  let usage = {
618
646
  inputTokens: 0,
619
647
  outputTokens: 0,
@@ -623,6 +651,10 @@ class WrappedCompletions extends Completions {
623
651
  // Map to track in-progress tool calls
624
652
  const toolCallsInProgress = new Map();
625
653
  for await (const chunk of stream1) {
654
+ // Extract model from chunk (Chat Completions chunks have model field)
655
+ if (!modelFromResponse && chunk.model) {
656
+ modelFromResponse = chunk.model;
657
+ }
626
658
  const choice = chunk?.choices?.[0];
627
659
  const chunkWebSearchCount = calculateWebSearchCount(chunk);
628
660
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
@@ -716,7 +748,7 @@ class WrappedCompletions extends Completions {
716
748
  await sendEventToPosthog({
717
749
  client: this.phClient,
718
750
  ...posthogParams,
719
- model: openAIParams.model,
751
+ model: openAIParams.model ?? modelFromResponse,
720
752
  provider: 'openai',
721
753
  input: sanitizeOpenAI(openAIParams.messages),
722
754
  output: formattedOutput,
@@ -770,7 +802,7 @@ class WrappedCompletions extends Completions {
770
802
  await sendEventToPosthog({
771
803
  client: this.phClient,
772
804
  ...posthogParams,
773
- model: openAIParams.model,
805
+ model: openAIParams.model ?? result.model,
774
806
  provider: 'openai',
775
807
  input: sanitizeOpenAI(openAIParams.messages),
776
808
  output: formattedOutput,
@@ -794,7 +826,7 @@ class WrappedCompletions extends Completions {
794
826
  await sendEventToPosthog({
795
827
  client: this.phClient,
796
828
  ...posthogParams,
797
- model: String(openAIParams.model ?? ''),
829
+ model: openAIParams.model,
798
830
  provider: 'openai',
799
831
  input: sanitizeOpenAI(openAIParams.messages),
800
832
  output: [],
@@ -843,6 +875,7 @@ class WrappedResponses extends Responses {
843
875
  (async () => {
844
876
  try {
845
877
  let finalContent = [];
878
+ let modelFromResponse;
846
879
  let usage = {
847
880
  inputTokens: 0,
848
881
  outputTokens: 0,
@@ -850,6 +883,10 @@ class WrappedResponses extends Responses {
850
883
  };
851
884
  for await (const chunk of stream1) {
852
885
  if ('response' in chunk && chunk.response) {
886
+ // Extract model from response object in chunk (for stored prompts)
887
+ if (!modelFromResponse && chunk.response.model) {
888
+ modelFromResponse = chunk.response.model;
889
+ }
853
890
  const chunkWebSearchCount = calculateWebSearchCount(chunk.response);
854
891
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
855
892
  usage.webSearchCount = chunkWebSearchCount;
@@ -873,8 +910,7 @@ class WrappedResponses extends Responses {
873
910
  await sendEventToPosthog({
874
911
  client: this.phClient,
875
912
  ...posthogParams,
876
- //@ts-expect-error
877
- model: openAIParams.model,
913
+ model: openAIParams.model ?? modelFromResponse,
878
914
  provider: 'openai',
879
915
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
880
916
  output: finalContent,
@@ -896,7 +932,6 @@ class WrappedResponses extends Responses {
896
932
  await sendEventToPosthog({
897
933
  client: this.phClient,
898
934
  ...posthogParams,
899
- //@ts-expect-error
900
935
  model: openAIParams.model,
901
936
  provider: 'openai',
902
937
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
@@ -929,8 +964,7 @@ class WrappedResponses extends Responses {
929
964
  await sendEventToPosthog({
930
965
  client: this.phClient,
931
966
  ...posthogParams,
932
- //@ts-expect-error
933
- model: openAIParams.model,
967
+ model: openAIParams.model ?? result.model,
934
968
  provider: 'openai',
935
969
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
936
970
  output: formattedOutput,
@@ -954,7 +988,7 @@ class WrappedResponses extends Responses {
954
988
  await sendEventToPosthog({
955
989
  client: this.phClient,
956
990
  ...posthogParams,
957
- model: String(openAIParams.model ?? ''),
991
+ model: openAIParams.model,
958
992
  provider: 'openai',
959
993
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
960
994
  output: [],
@@ -991,7 +1025,7 @@ class WrappedResponses extends Responses {
991
1025
  await sendEventToPosthog({
992
1026
  client: this.phClient,
993
1027
  ...posthogParams,
994
- model: String(openAIParams.model ?? ''),
1028
+ model: openAIParams.model ?? result.model,
995
1029
  provider: 'openai',
996
1030
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
997
1031
  output: result.output,
@@ -1012,7 +1046,7 @@ class WrappedResponses extends Responses {
1012
1046
  await sendEventToPosthog({
1013
1047
  client: this.phClient,
1014
1048
  ...posthogParams,
1015
- model: String(openAIParams.model ?? ''),
1049
+ model: openAIParams.model,
1016
1050
  provider: 'openai',
1017
1051
  input: formatOpenAIResponsesInput(openAIParams.input, openAIParams.instructions),
1018
1052
  output: [],
@@ -1202,7 +1236,7 @@ class WrappedTranscriptions extends Transcriptions {
1202
1236
  await sendEventToPosthog({
1203
1237
  client: this.phClient,
1204
1238
  ...posthogParams,
1205
- model: String(openAIParams.model ?? ''),
1239
+ model: openAIParams.model,
1206
1240
  provider: 'openai',
1207
1241
  input: openAIParams.prompt,
1208
1242
  output: result.text,
@@ -1222,7 +1256,7 @@ class WrappedTranscriptions extends Transcriptions {
1222
1256
  await sendEventToPosthog({
1223
1257
  client: this.phClient,
1224
1258
  ...posthogParams,
1225
- model: String(openAIParams.model ?? ''),
1259
+ model: openAIParams.model,
1226
1260
  provider: 'openai',
1227
1261
  input: openAIParams.prompt,
1228
1262
  output: [],