@posthog/ai 6.3.0 → 6.3.2

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/index.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var openai = require('openai');
4
- var uuid = require('uuid');
5
4
  var buffer = require('buffer');
5
+ var uuid = require('uuid');
6
6
  var ai = require('ai');
7
7
  var AnthropicOriginal = require('@anthropic-ai/sdk');
8
8
  var genai = require('@google/genai');
@@ -26,7 +26,7 @@ function _interopNamespaceDefault(e) {
26
26
 
27
27
  var uuid__namespace = /*#__PURE__*/_interopNamespaceDefault(uuid);
28
28
 
29
- var version = "6.3.0";
29
+ var version = "6.3.2";
30
30
 
31
31
  // limit large outputs by truncating to 200kb (approx 200k bytes)
32
32
  const MAX_OUTPUT_SIZE = 200000;
@@ -226,18 +226,43 @@ const mergeSystemPrompt = (params, provider) => {
226
226
  const withPrivacyMode = (client, privacyMode, input) => {
227
227
  return client.privacy_mode || privacyMode ? null : input;
228
228
  };
229
- const truncate = str => {
229
+ function toSafeString(input) {
230
+ if (input === undefined || input === null) {
231
+ return '';
232
+ }
233
+ if (typeof input === 'string') {
234
+ return input;
235
+ }
230
236
  try {
231
- const buffer$1 = buffer.Buffer.from(str, STRING_FORMAT);
232
- if (buffer$1.length <= MAX_OUTPUT_SIZE) {
233
- return str;
234
- }
235
- const truncatedBuffer = buffer$1.slice(0, MAX_OUTPUT_SIZE);
236
- return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
237
+ return JSON.stringify(input);
237
238
  } catch {
238
- console.error('Error truncating, likely not a string');
239
- return str;
239
+ console.warn('Failed to stringify input', input);
240
+ return '';
240
241
  }
242
+ }
243
+ const truncate = input => {
244
+ const str = toSafeString(input);
245
+ if (str === '') {
246
+ return '';
247
+ }
248
+ // Check if we need to truncate and ensure STRING_FORMAT is respected
249
+ const encoder = new TextEncoder();
250
+ const buffer = encoder.encode(str);
251
+ if (buffer.length <= MAX_OUTPUT_SIZE) {
252
+ // Ensure STRING_FORMAT is respected
253
+ return new TextDecoder(STRING_FORMAT).decode(buffer);
254
+ }
255
+ // Truncate the buffer and ensure a valid string is returned
256
+ const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
257
+ // fatal: false means we get U+FFFD at the end if truncation broke the encoding
258
+ const decoder = new TextDecoder(STRING_FORMAT, {
259
+ fatal: false
260
+ });
261
+ let truncatedStr = decoder.decode(truncatedBuffer);
262
+ if (truncatedStr.endsWith('\uFFFD')) {
263
+ truncatedStr = truncatedStr.slice(0, -1);
264
+ }
265
+ return `${truncatedStr}... [truncated]`;
241
266
  };
242
267
  /**
243
268
  * Extract available tool calls from the request parameters.
@@ -579,6 +604,42 @@ const sanitizeLangChain = data => {
579
604
  return processMessages(data, sanitizeLangChainImage);
580
605
  };
581
606
 
607
+ const POSTHOG_PARAMS_MAP = {
608
+ posthogDistinctId: 'distinctId',
609
+ posthogTraceId: 'traceId',
610
+ posthogProperties: 'properties',
611
+ posthogPrivacyMode: 'privacyMode',
612
+ posthogGroups: 'groups',
613
+ posthogModelOverride: 'modelOverride',
614
+ posthogProviderOverride: 'providerOverride',
615
+ posthogCostOverride: 'costOverride',
616
+ posthogCaptureImmediate: 'captureImmediate'
617
+ };
618
+ function extractPosthogParams(body) {
619
+ const openAIParams = {};
620
+ const posthogParams = {};
621
+ for (const [key, value] of Object.entries(body)) {
622
+ if (POSTHOG_PARAMS_MAP[key]) {
623
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
624
+ } else if (key.startsWith('posthog')) {
625
+ console.warn(`Unknown Posthog parameter ${key}`);
626
+ } else {
627
+ openAIParams[key] = value;
628
+ }
629
+ }
630
+ return {
631
+ openAIParams: openAIParams,
632
+ posthogParams: addDefaults(posthogParams)
633
+ };
634
+ }
635
+ function addDefaults(params) {
636
+ return {
637
+ ...params,
638
+ privacyMode: params.privacyMode ?? false,
639
+ traceId: params.traceId ?? uuid.v4()
640
+ };
641
+ }
642
+
582
643
  const Chat = openai.OpenAI.Chat;
583
644
  const Completions = Chat.Completions;
584
645
  const Responses = openai.OpenAI.Responses;
@@ -611,12 +672,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
611
672
  // --- Implementation Signature
612
673
  create(body, options) {
613
674
  const {
614
- posthogDistinctId,
615
- posthogTraceId,
616
- posthogCaptureImmediate,
617
- ...openAIParams
618
- } = body;
619
- const traceId = posthogTraceId ?? uuid.v4();
675
+ openAIParams,
676
+ posthogParams
677
+ } = extractPosthogParams(body);
620
678
  const startTime = Date.now();
621
679
  const parentPromise = super.create(openAIParams, options);
622
680
  if (openAIParams.stream) {
@@ -715,8 +773,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
715
773
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
716
774
  await sendEventToPosthog({
717
775
  client: this.phClient,
718
- distinctId: posthogDistinctId,
719
- traceId,
776
+ ...posthogParams,
720
777
  model: openAIParams.model,
721
778
  provider: 'openai',
722
779
  input: sanitizeOpenAI(openAIParams.messages),
@@ -726,15 +783,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
726
783
  params: body,
727
784
  httpStatus: 200,
728
785
  usage,
729
- tools: availableTools,
730
- captureImmediate: posthogCaptureImmediate
786
+ tools: availableTools
731
787
  });
732
788
  } catch (error) {
733
789
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
734
790
  await sendEventToPosthog({
735
791
  client: this.phClient,
736
- distinctId: posthogDistinctId,
737
- traceId,
792
+ ...posthogParams,
738
793
  model: openAIParams.model,
739
794
  provider: 'openai',
740
795
  input: sanitizeOpenAI(openAIParams.messages),
@@ -748,8 +803,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
748
803
  outputTokens: 0
749
804
  },
750
805
  isError: true,
751
- error: JSON.stringify(error),
752
- captureImmediate: posthogCaptureImmediate
806
+ error: JSON.stringify(error)
753
807
  });
754
808
  }
755
809
  })();
@@ -765,8 +819,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
765
819
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
766
820
  await sendEventToPosthog({
767
821
  client: this.phClient,
768
- distinctId: posthogDistinctId,
769
- traceId,
822
+ ...posthogParams,
770
823
  model: openAIParams.model,
771
824
  provider: 'openai',
772
825
  input: sanitizeOpenAI(openAIParams.messages),
@@ -781,8 +834,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
781
834
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
782
835
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
783
836
  },
784
- tools: availableTools,
785
- captureImmediate: posthogCaptureImmediate
837
+ tools: availableTools
786
838
  });
787
839
  }
788
840
  return result;
@@ -790,8 +842,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
790
842
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
791
843
  await sendEventToPosthog({
792
844
  client: this.phClient,
793
- distinctId: posthogDistinctId,
794
- traceId,
845
+ ...posthogParams,
795
846
  model: openAIParams.model,
796
847
  provider: 'openai',
797
848
  input: sanitizeOpenAI(openAIParams.messages),
@@ -805,8 +856,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
805
856
  outputTokens: 0
806
857
  },
807
858
  isError: true,
808
- error: JSON.stringify(error),
809
- captureImmediate: posthogCaptureImmediate
859
+ error: JSON.stringify(error)
810
860
  });
811
861
  throw error;
812
862
  });
@@ -823,12 +873,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
823
873
  // --- Implementation Signature
824
874
  create(body, options) {
825
875
  const {
826
- posthogDistinctId,
827
- posthogTraceId,
828
- posthogCaptureImmediate,
829
- ...openAIParams
830
- } = body;
831
- const traceId = posthogTraceId ?? uuid.v4();
876
+ openAIParams,
877
+ posthogParams
878
+ } = extractPosthogParams(body);
832
879
  const startTime = Date.now();
833
880
  const parentPromise = super.create(openAIParams, options);
834
881
  if (openAIParams.stream) {
@@ -859,8 +906,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
859
906
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
860
907
  await sendEventToPosthog({
861
908
  client: this.phClient,
862
- distinctId: posthogDistinctId,
863
- traceId,
909
+ ...posthogParams,
864
910
  //@ts-expect-error
865
911
  model: openAIParams.model,
866
912
  provider: 'openai',
@@ -871,15 +917,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
871
917
  params: body,
872
918
  httpStatus: 200,
873
919
  usage,
874
- tools: availableTools,
875
- captureImmediate: posthogCaptureImmediate
920
+ tools: availableTools
876
921
  });
877
922
  } catch (error) {
878
923
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
879
924
  await sendEventToPosthog({
880
925
  client: this.phClient,
881
- distinctId: posthogDistinctId,
882
- traceId,
926
+ ...posthogParams,
883
927
  //@ts-expect-error
884
928
  model: openAIParams.model,
885
929
  provider: 'openai',
@@ -894,8 +938,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
894
938
  outputTokens: 0
895
939
  },
896
940
  isError: true,
897
- error: JSON.stringify(error),
898
- captureImmediate: posthogCaptureImmediate
941
+ error: JSON.stringify(error)
899
942
  });
900
943
  }
901
944
  })();
@@ -910,8 +953,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
910
953
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
911
954
  await sendEventToPosthog({
912
955
  client: this.phClient,
913
- distinctId: posthogDistinctId,
914
- traceId,
956
+ ...posthogParams,
915
957
  //@ts-expect-error
916
958
  model: openAIParams.model,
917
959
  provider: 'openai',
@@ -929,8 +971,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
929
971
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
930
972
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
931
973
  },
932
- tools: availableTools,
933
- captureImmediate: posthogCaptureImmediate
974
+ tools: availableTools
934
975
  });
935
976
  }
936
977
  return result;
@@ -938,8 +979,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
938
979
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
939
980
  await sendEventToPosthog({
940
981
  client: this.phClient,
941
- distinctId: posthogDistinctId,
942
- traceId,
982
+ ...posthogParams,
943
983
  //@ts-expect-error
944
984
  model: openAIParams.model,
945
985
  provider: 'openai',
@@ -954,8 +994,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
954
994
  outputTokens: 0
955
995
  },
956
996
  isError: true,
957
- error: JSON.stringify(error),
958
- captureImmediate: posthogCaptureImmediate
997
+ error: JSON.stringify(error)
959
998
  });
960
999
  throw error;
961
1000
  });
@@ -964,12 +1003,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
964
1003
  }
965
1004
  parse(body, options) {
966
1005
  const {
967
- posthogDistinctId,
968
- posthogTraceId,
969
- posthogCaptureImmediate,
970
- ...openAIParams
971
- } = body;
972
- const traceId = posthogTraceId ?? uuid.v4();
1006
+ openAIParams,
1007
+ posthogParams
1008
+ } = extractPosthogParams(body);
973
1009
  const startTime = Date.now();
974
1010
  // Create a temporary instance that bypasses our wrapped create method
975
1011
  const originalCreate = super.create.bind(this);
@@ -982,8 +1018,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
982
1018
  const latency = (Date.now() - startTime) / 1000;
983
1019
  await sendEventToPosthog({
984
1020
  client: this.phClient,
985
- distinctId: posthogDistinctId,
986
- traceId,
1021
+ ...posthogParams,
987
1022
  //@ts-expect-error
988
1023
  model: openAIParams.model,
989
1024
  provider: 'openai',
@@ -998,16 +1033,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
998
1033
  outputTokens: result.usage?.output_tokens ?? 0,
999
1034
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1000
1035
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1001
- },
1002
- captureImmediate: posthogCaptureImmediate
1036
+ }
1003
1037
  });
1004
1038
  return result;
1005
1039
  }, async error => {
1006
1040
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1007
1041
  await sendEventToPosthog({
1008
1042
  client: this.phClient,
1009
- distinctId: posthogDistinctId,
1010
- traceId,
1043
+ ...posthogParams,
1011
1044
  //@ts-expect-error
1012
1045
  model: openAIParams.model,
1013
1046
  provider: 'openai',
@@ -1022,8 +1055,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1022
1055
  outputTokens: 0
1023
1056
  },
1024
1057
  isError: true,
1025
- error: JSON.stringify(error),
1026
- captureImmediate: posthogCaptureImmediate
1058
+ error: JSON.stringify(error)
1027
1059
  });
1028
1060
  throw error;
1029
1061
  });
@@ -1042,25 +1074,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1042
1074
  }
1043
1075
  create(body, options) {
1044
1076
  const {
1045
- posthogDistinctId,
1046
- posthogTraceId,
1047
- posthogPrivacyMode = false,
1048
- posthogCaptureImmediate,
1049
- ...openAIParams
1050
- } = body;
1051
- const traceId = posthogTraceId ?? uuid.v4();
1077
+ openAIParams,
1078
+ posthogParams
1079
+ } = extractPosthogParams(body);
1052
1080
  const startTime = Date.now();
1053
1081
  const parentPromise = super.create(openAIParams, options);
1054
1082
  const wrappedPromise = parentPromise.then(async result => {
1055
1083
  const latency = (Date.now() - startTime) / 1000;
1056
1084
  await sendEventToPosthog({
1057
1085
  client: this.phClient,
1086
+ ...posthogParams,
1058
1087
  eventType: AIEvent.Embedding,
1059
- distinctId: posthogDistinctId,
1060
- traceId,
1061
1088
  model: openAIParams.model,
1062
1089
  provider: 'openai',
1063
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1090
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1064
1091
  output: null,
1065
1092
  // Embeddings don't have output content
1066
1093
  latency,
@@ -1069,8 +1096,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1069
1096
  httpStatus: 200,
1070
1097
  usage: {
1071
1098
  inputTokens: result.usage?.prompt_tokens ?? 0
1072
- },
1073
- captureImmediate: posthogCaptureImmediate
1099
+ }
1074
1100
  });
1075
1101
  return result;
1076
1102
  }, async error => {
@@ -1078,11 +1104,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1078
1104
  await sendEventToPosthog({
1079
1105
  client: this.phClient,
1080
1106
  eventType: AIEvent.Embedding,
1081
- distinctId: posthogDistinctId,
1082
- traceId,
1107
+ ...posthogParams,
1083
1108
  model: openAIParams.model,
1084
1109
  provider: 'openai',
1085
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1110
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1086
1111
  output: null,
1087
1112
  // Embeddings don't have output content
1088
1113
  latency: 0,
@@ -1093,8 +1118,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1093
1118
  inputTokens: 0
1094
1119
  },
1095
1120
  isError: true,
1096
- error: JSON.stringify(error),
1097
- captureImmediate: posthogCaptureImmediate
1121
+ error: JSON.stringify(error)
1098
1122
  });
1099
1123
  throw error;
1100
1124
  });
@@ -1129,12 +1153,9 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1129
1153
  // --- Implementation Signature
1130
1154
  create(body, options) {
1131
1155
  const {
1132
- posthogDistinctId,
1133
- posthogTraceId,
1134
- posthogCaptureImmediate,
1135
- ...openAIParams
1136
- } = body;
1137
- const traceId = posthogTraceId ?? uuid.v4();
1156
+ openAIParams,
1157
+ posthogParams
1158
+ } = extractPosthogParams(body);
1138
1159
  const startTime = Date.now();
1139
1160
  const parentPromise = super.create(openAIParams, options);
1140
1161
  if (openAIParams.stream) {
@@ -1232,8 +1253,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1232
1253
  const latency = (Date.now() - startTime) / 1000;
1233
1254
  await sendEventToPosthog({
1234
1255
  client: this.phClient,
1235
- distinctId: posthogDistinctId,
1236
- traceId,
1256
+ ...posthogParams,
1237
1257
  model: openAIParams.model,
1238
1258
  provider: 'azure',
1239
1259
  input: openAIParams.messages,
@@ -1242,15 +1262,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1242
1262
  baseURL: this.baseURL,
1243
1263
  params: body,
1244
1264
  httpStatus: 200,
1245
- usage,
1246
- captureImmediate: posthogCaptureImmediate
1265
+ usage
1247
1266
  });
1248
1267
  } catch (error) {
1249
1268
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1250
1269
  await sendEventToPosthog({
1251
1270
  client: this.phClient,
1252
- distinctId: posthogDistinctId,
1253
- traceId,
1271
+ ...posthogParams,
1254
1272
  model: openAIParams.model,
1255
1273
  provider: 'azure',
1256
1274
  input: openAIParams.messages,
@@ -1264,8 +1282,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1264
1282
  outputTokens: 0
1265
1283
  },
1266
1284
  isError: true,
1267
- error: JSON.stringify(error),
1268
- captureImmediate: posthogCaptureImmediate
1285
+ error: JSON.stringify(error)
1269
1286
  });
1270
1287
  }
1271
1288
  })();
@@ -1280,8 +1297,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1280
1297
  const latency = (Date.now() - startTime) / 1000;
1281
1298
  await sendEventToPosthog({
1282
1299
  client: this.phClient,
1283
- distinctId: posthogDistinctId,
1284
- traceId,
1300
+ ...posthogParams,
1285
1301
  model: openAIParams.model,
1286
1302
  provider: 'azure',
1287
1303
  input: openAIParams.messages,
@@ -1295,8 +1311,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1295
1311
  outputTokens: result.usage?.completion_tokens ?? 0,
1296
1312
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
1297
1313
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
1298
- },
1299
- captureImmediate: posthogCaptureImmediate
1314
+ }
1300
1315
  });
1301
1316
  }
1302
1317
  return result;
@@ -1304,8 +1319,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1304
1319
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1305
1320
  await sendEventToPosthog({
1306
1321
  client: this.phClient,
1307
- distinctId: posthogDistinctId,
1308
- traceId,
1322
+ ...posthogParams,
1309
1323
  model: openAIParams.model,
1310
1324
  provider: 'azure',
1311
1325
  input: openAIParams.messages,
@@ -1319,8 +1333,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1319
1333
  outputTokens: 0
1320
1334
  },
1321
1335
  isError: true,
1322
- error: JSON.stringify(error),
1323
- captureImmediate: posthogCaptureImmediate
1336
+ error: JSON.stringify(error)
1324
1337
  });
1325
1338
  throw error;
1326
1339
  });
@@ -1337,12 +1350,9 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1337
1350
  // --- Implementation Signature
1338
1351
  create(body, options) {
1339
1352
  const {
1340
- posthogDistinctId,
1341
- posthogTraceId,
1342
- posthogCaptureImmediate,
1343
- ...openAIParams
1344
- } = body;
1345
- const traceId = posthogTraceId ?? uuid.v4();
1353
+ openAIParams,
1354
+ posthogParams
1355
+ } = extractPosthogParams(body);
1346
1356
  const startTime = Date.now();
1347
1357
  const parentPromise = super.create(openAIParams, options);
1348
1358
  if (openAIParams.stream) {
@@ -1372,8 +1382,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1372
1382
  const latency = (Date.now() - startTime) / 1000;
1373
1383
  await sendEventToPosthog({
1374
1384
  client: this.phClient,
1375
- distinctId: posthogDistinctId,
1376
- traceId,
1385
+ ...posthogParams,
1377
1386
  //@ts-expect-error
1378
1387
  model: openAIParams.model,
1379
1388
  provider: 'azure',
@@ -1383,15 +1392,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1383
1392
  baseURL: this.baseURL,
1384
1393
  params: body,
1385
1394
  httpStatus: 200,
1386
- usage,
1387
- captureImmediate: posthogCaptureImmediate
1395
+ usage
1388
1396
  });
1389
1397
  } catch (error) {
1390
1398
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1391
1399
  await sendEventToPosthog({
1392
1400
  client: this.phClient,
1393
- distinctId: posthogDistinctId,
1394
- traceId,
1401
+ ...posthogParams,
1395
1402
  //@ts-expect-error
1396
1403
  model: openAIParams.model,
1397
1404
  provider: 'azure',
@@ -1406,8 +1413,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1406
1413
  outputTokens: 0
1407
1414
  },
1408
1415
  isError: true,
1409
- error: JSON.stringify(error),
1410
- captureImmediate: posthogCaptureImmediate
1416
+ error: JSON.stringify(error)
1411
1417
  });
1412
1418
  }
1413
1419
  })();
@@ -1421,8 +1427,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1421
1427
  const latency = (Date.now() - startTime) / 1000;
1422
1428
  await sendEventToPosthog({
1423
1429
  client: this.phClient,
1424
- distinctId: posthogDistinctId,
1425
- traceId,
1430
+ ...posthogParams,
1426
1431
  //@ts-expect-error
1427
1432
  model: openAIParams.model,
1428
1433
  provider: 'azure',
@@ -1437,8 +1442,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1437
1442
  outputTokens: result.usage?.output_tokens ?? 0,
1438
1443
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1439
1444
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1440
- },
1441
- captureImmediate: posthogCaptureImmediate
1445
+ }
1442
1446
  });
1443
1447
  }
1444
1448
  return result;
@@ -1446,8 +1450,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1446
1450
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1447
1451
  await sendEventToPosthog({
1448
1452
  client: this.phClient,
1449
- distinctId: posthogDistinctId,
1450
- traceId,
1453
+ ...posthogParams,
1451
1454
  //@ts-expect-error
1452
1455
  model: openAIParams.model,
1453
1456
  provider: 'azure',
@@ -1462,8 +1465,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1462
1465
  outputTokens: 0
1463
1466
  },
1464
1467
  isError: true,
1465
- error: JSON.stringify(error),
1466
- captureImmediate: posthogCaptureImmediate
1468
+ error: JSON.stringify(error)
1467
1469
  });
1468
1470
  throw error;
1469
1471
  });
@@ -1472,20 +1474,16 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1472
1474
  }
1473
1475
  parse(body, options) {
1474
1476
  const {
1475
- posthogDistinctId,
1476
- posthogTraceId,
1477
- posthogCaptureImmediate,
1478
- ...openAIParams
1479
- } = body;
1480
- const traceId = posthogTraceId ?? uuid.v4();
1477
+ openAIParams,
1478
+ posthogParams
1479
+ } = extractPosthogParams(body);
1481
1480
  const startTime = Date.now();
1482
1481
  const parentPromise = super.parse(openAIParams, options);
1483
1482
  const wrappedPromise = parentPromise.then(async result => {
1484
1483
  const latency = (Date.now() - startTime) / 1000;
1485
1484
  await sendEventToPosthog({
1486
1485
  client: this.phClient,
1487
- distinctId: posthogDistinctId,
1488
- traceId,
1486
+ ...posthogParams,
1489
1487
  //@ts-expect-error
1490
1488
  model: openAIParams.model,
1491
1489
  provider: 'azure',
@@ -1500,15 +1498,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1500
1498
  outputTokens: result.usage?.output_tokens ?? 0,
1501
1499
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1502
1500
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1503
- },
1504
- captureImmediate: posthogCaptureImmediate
1501
+ }
1505
1502
  });
1506
1503
  return result;
1507
1504
  }, async error => {
1508
1505
  await sendEventToPosthog({
1509
1506
  client: this.phClient,
1510
- distinctId: posthogDistinctId,
1511
- traceId,
1507
+ ...posthogParams,
1512
1508
  //@ts-expect-error
1513
1509
  model: openAIParams.model,
1514
1510
  provider: 'azure',
@@ -1523,8 +1519,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1523
1519
  outputTokens: 0
1524
1520
  },
1525
1521
  isError: true,
1526
- error: JSON.stringify(error),
1527
- captureImmediate: posthogCaptureImmediate
1522
+ error: JSON.stringify(error)
1528
1523
  });
1529
1524
  throw error;
1530
1525
  });
@@ -1539,13 +1534,9 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1539
1534
  }
1540
1535
  create(body, options) {
1541
1536
  const {
1542
- posthogDistinctId,
1543
- posthogTraceId,
1544
- posthogPrivacyMode = false,
1545
- posthogCaptureImmediate,
1546
- ...openAIParams
1547
- } = body;
1548
- const traceId = posthogTraceId ?? uuid.v4();
1537
+ openAIParams,
1538
+ posthogParams
1539
+ } = extractPosthogParams(body);
1549
1540
  const startTime = Date.now();
1550
1541
  const parentPromise = super.create(openAIParams, options);
1551
1542
  const wrappedPromise = parentPromise.then(async result => {
@@ -1553,11 +1544,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1553
1544
  await sendEventToPosthog({
1554
1545
  client: this.phClient,
1555
1546
  eventType: AIEvent.Embedding,
1556
- distinctId: posthogDistinctId,
1557
- traceId,
1547
+ ...posthogParams,
1558
1548
  model: openAIParams.model,
1559
1549
  provider: 'azure',
1560
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1550
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1561
1551
  output: null,
1562
1552
  // Embeddings don't have output content
1563
1553
  latency,
@@ -1566,8 +1556,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1566
1556
  httpStatus: 200,
1567
1557
  usage: {
1568
1558
  inputTokens: result.usage?.prompt_tokens ?? 0
1569
- },
1570
- captureImmediate: posthogCaptureImmediate
1559
+ }
1571
1560
  });
1572
1561
  return result;
1573
1562
  }, async error => {
@@ -1575,11 +1564,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1575
1564
  await sendEventToPosthog({
1576
1565
  client: this.phClient,
1577
1566
  eventType: AIEvent.Embedding,
1578
- distinctId: posthogDistinctId,
1579
- traceId,
1567
+ ...posthogParams,
1580
1568
  model: openAIParams.model,
1581
1569
  provider: 'azure',
1582
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1570
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1583
1571
  output: null,
1584
1572
  latency: 0,
1585
1573
  baseURL: this.baseURL,
@@ -1589,8 +1577,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1589
1577
  inputTokens: 0
1590
1578
  },
1591
1579
  isError: true,
1592
- error: JSON.stringify(error),
1593
- captureImmediate: posthogCaptureImmediate
1580
+ error: JSON.stringify(error)
1594
1581
  });
1595
1582
  throw error;
1596
1583
  });