@posthog/ai 6.3.1 → 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.1";
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;
@@ -604,6 +604,42 @@ const sanitizeLangChain = data => {
604
604
  return processMessages(data, sanitizeLangChainImage);
605
605
  };
606
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
+
607
643
  const Chat = openai.OpenAI.Chat;
608
644
  const Completions = Chat.Completions;
609
645
  const Responses = openai.OpenAI.Responses;
@@ -636,12 +672,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
636
672
  // --- Implementation Signature
637
673
  create(body, options) {
638
674
  const {
639
- posthogDistinctId,
640
- posthogTraceId,
641
- posthogCaptureImmediate,
642
- ...openAIParams
643
- } = body;
644
- const traceId = posthogTraceId ?? uuid.v4();
675
+ openAIParams,
676
+ posthogParams
677
+ } = extractPosthogParams(body);
645
678
  const startTime = Date.now();
646
679
  const parentPromise = super.create(openAIParams, options);
647
680
  if (openAIParams.stream) {
@@ -740,8 +773,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
740
773
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
741
774
  await sendEventToPosthog({
742
775
  client: this.phClient,
743
- distinctId: posthogDistinctId,
744
- traceId,
776
+ ...posthogParams,
745
777
  model: openAIParams.model,
746
778
  provider: 'openai',
747
779
  input: sanitizeOpenAI(openAIParams.messages),
@@ -751,15 +783,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
751
783
  params: body,
752
784
  httpStatus: 200,
753
785
  usage,
754
- tools: availableTools,
755
- captureImmediate: posthogCaptureImmediate
786
+ tools: availableTools
756
787
  });
757
788
  } catch (error) {
758
789
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
759
790
  await sendEventToPosthog({
760
791
  client: this.phClient,
761
- distinctId: posthogDistinctId,
762
- traceId,
792
+ ...posthogParams,
763
793
  model: openAIParams.model,
764
794
  provider: 'openai',
765
795
  input: sanitizeOpenAI(openAIParams.messages),
@@ -773,8 +803,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
773
803
  outputTokens: 0
774
804
  },
775
805
  isError: true,
776
- error: JSON.stringify(error),
777
- captureImmediate: posthogCaptureImmediate
806
+ error: JSON.stringify(error)
778
807
  });
779
808
  }
780
809
  })();
@@ -790,8 +819,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
790
819
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
791
820
  await sendEventToPosthog({
792
821
  client: this.phClient,
793
- distinctId: posthogDistinctId,
794
- traceId,
822
+ ...posthogParams,
795
823
  model: openAIParams.model,
796
824
  provider: 'openai',
797
825
  input: sanitizeOpenAI(openAIParams.messages),
@@ -806,8 +834,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
806
834
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
807
835
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
808
836
  },
809
- tools: availableTools,
810
- captureImmediate: posthogCaptureImmediate
837
+ tools: availableTools
811
838
  });
812
839
  }
813
840
  return result;
@@ -815,8 +842,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
815
842
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
816
843
  await sendEventToPosthog({
817
844
  client: this.phClient,
818
- distinctId: posthogDistinctId,
819
- traceId,
845
+ ...posthogParams,
820
846
  model: openAIParams.model,
821
847
  provider: 'openai',
822
848
  input: sanitizeOpenAI(openAIParams.messages),
@@ -830,8 +856,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
830
856
  outputTokens: 0
831
857
  },
832
858
  isError: true,
833
- error: JSON.stringify(error),
834
- captureImmediate: posthogCaptureImmediate
859
+ error: JSON.stringify(error)
835
860
  });
836
861
  throw error;
837
862
  });
@@ -848,12 +873,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
848
873
  // --- Implementation Signature
849
874
  create(body, options) {
850
875
  const {
851
- posthogDistinctId,
852
- posthogTraceId,
853
- posthogCaptureImmediate,
854
- ...openAIParams
855
- } = body;
856
- const traceId = posthogTraceId ?? uuid.v4();
876
+ openAIParams,
877
+ posthogParams
878
+ } = extractPosthogParams(body);
857
879
  const startTime = Date.now();
858
880
  const parentPromise = super.create(openAIParams, options);
859
881
  if (openAIParams.stream) {
@@ -884,8 +906,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
884
906
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
885
907
  await sendEventToPosthog({
886
908
  client: this.phClient,
887
- distinctId: posthogDistinctId,
888
- traceId,
909
+ ...posthogParams,
889
910
  //@ts-expect-error
890
911
  model: openAIParams.model,
891
912
  provider: 'openai',
@@ -896,15 +917,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
896
917
  params: body,
897
918
  httpStatus: 200,
898
919
  usage,
899
- tools: availableTools,
900
- captureImmediate: posthogCaptureImmediate
920
+ tools: availableTools
901
921
  });
902
922
  } catch (error) {
903
923
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
904
924
  await sendEventToPosthog({
905
925
  client: this.phClient,
906
- distinctId: posthogDistinctId,
907
- traceId,
926
+ ...posthogParams,
908
927
  //@ts-expect-error
909
928
  model: openAIParams.model,
910
929
  provider: 'openai',
@@ -919,8 +938,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
919
938
  outputTokens: 0
920
939
  },
921
940
  isError: true,
922
- error: JSON.stringify(error),
923
- captureImmediate: posthogCaptureImmediate
941
+ error: JSON.stringify(error)
924
942
  });
925
943
  }
926
944
  })();
@@ -935,8 +953,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
935
953
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
936
954
  await sendEventToPosthog({
937
955
  client: this.phClient,
938
- distinctId: posthogDistinctId,
939
- traceId,
956
+ ...posthogParams,
940
957
  //@ts-expect-error
941
958
  model: openAIParams.model,
942
959
  provider: 'openai',
@@ -954,8 +971,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
954
971
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
955
972
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
956
973
  },
957
- tools: availableTools,
958
- captureImmediate: posthogCaptureImmediate
974
+ tools: availableTools
959
975
  });
960
976
  }
961
977
  return result;
@@ -963,8 +979,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
963
979
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
964
980
  await sendEventToPosthog({
965
981
  client: this.phClient,
966
- distinctId: posthogDistinctId,
967
- traceId,
982
+ ...posthogParams,
968
983
  //@ts-expect-error
969
984
  model: openAIParams.model,
970
985
  provider: 'openai',
@@ -979,8 +994,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
979
994
  outputTokens: 0
980
995
  },
981
996
  isError: true,
982
- error: JSON.stringify(error),
983
- captureImmediate: posthogCaptureImmediate
997
+ error: JSON.stringify(error)
984
998
  });
985
999
  throw error;
986
1000
  });
@@ -989,12 +1003,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
989
1003
  }
990
1004
  parse(body, options) {
991
1005
  const {
992
- posthogDistinctId,
993
- posthogTraceId,
994
- posthogCaptureImmediate,
995
- ...openAIParams
996
- } = body;
997
- const traceId = posthogTraceId ?? uuid.v4();
1006
+ openAIParams,
1007
+ posthogParams
1008
+ } = extractPosthogParams(body);
998
1009
  const startTime = Date.now();
999
1010
  // Create a temporary instance that bypasses our wrapped create method
1000
1011
  const originalCreate = super.create.bind(this);
@@ -1007,8 +1018,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1007
1018
  const latency = (Date.now() - startTime) / 1000;
1008
1019
  await sendEventToPosthog({
1009
1020
  client: this.phClient,
1010
- distinctId: posthogDistinctId,
1011
- traceId,
1021
+ ...posthogParams,
1012
1022
  //@ts-expect-error
1013
1023
  model: openAIParams.model,
1014
1024
  provider: 'openai',
@@ -1023,16 +1033,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1023
1033
  outputTokens: result.usage?.output_tokens ?? 0,
1024
1034
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1025
1035
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1026
- },
1027
- captureImmediate: posthogCaptureImmediate
1036
+ }
1028
1037
  });
1029
1038
  return result;
1030
1039
  }, async error => {
1031
1040
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1032
1041
  await sendEventToPosthog({
1033
1042
  client: this.phClient,
1034
- distinctId: posthogDistinctId,
1035
- traceId,
1043
+ ...posthogParams,
1036
1044
  //@ts-expect-error
1037
1045
  model: openAIParams.model,
1038
1046
  provider: 'openai',
@@ -1047,8 +1055,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1047
1055
  outputTokens: 0
1048
1056
  },
1049
1057
  isError: true,
1050
- error: JSON.stringify(error),
1051
- captureImmediate: posthogCaptureImmediate
1058
+ error: JSON.stringify(error)
1052
1059
  });
1053
1060
  throw error;
1054
1061
  });
@@ -1067,25 +1074,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1067
1074
  }
1068
1075
  create(body, options) {
1069
1076
  const {
1070
- posthogDistinctId,
1071
- posthogTraceId,
1072
- posthogPrivacyMode = false,
1073
- posthogCaptureImmediate,
1074
- ...openAIParams
1075
- } = body;
1076
- const traceId = posthogTraceId ?? uuid.v4();
1077
+ openAIParams,
1078
+ posthogParams
1079
+ } = extractPosthogParams(body);
1077
1080
  const startTime = Date.now();
1078
1081
  const parentPromise = super.create(openAIParams, options);
1079
1082
  const wrappedPromise = parentPromise.then(async result => {
1080
1083
  const latency = (Date.now() - startTime) / 1000;
1081
1084
  await sendEventToPosthog({
1082
1085
  client: this.phClient,
1086
+ ...posthogParams,
1083
1087
  eventType: AIEvent.Embedding,
1084
- distinctId: posthogDistinctId,
1085
- traceId,
1086
1088
  model: openAIParams.model,
1087
1089
  provider: 'openai',
1088
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1090
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1089
1091
  output: null,
1090
1092
  // Embeddings don't have output content
1091
1093
  latency,
@@ -1094,8 +1096,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1094
1096
  httpStatus: 200,
1095
1097
  usage: {
1096
1098
  inputTokens: result.usage?.prompt_tokens ?? 0
1097
- },
1098
- captureImmediate: posthogCaptureImmediate
1099
+ }
1099
1100
  });
1100
1101
  return result;
1101
1102
  }, async error => {
@@ -1103,11 +1104,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1103
1104
  await sendEventToPosthog({
1104
1105
  client: this.phClient,
1105
1106
  eventType: AIEvent.Embedding,
1106
- distinctId: posthogDistinctId,
1107
- traceId,
1107
+ ...posthogParams,
1108
1108
  model: openAIParams.model,
1109
1109
  provider: 'openai',
1110
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1110
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1111
1111
  output: null,
1112
1112
  // Embeddings don't have output content
1113
1113
  latency: 0,
@@ -1118,8 +1118,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1118
1118
  inputTokens: 0
1119
1119
  },
1120
1120
  isError: true,
1121
- error: JSON.stringify(error),
1122
- captureImmediate: posthogCaptureImmediate
1121
+ error: JSON.stringify(error)
1123
1122
  });
1124
1123
  throw error;
1125
1124
  });
@@ -1154,12 +1153,9 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1154
1153
  // --- Implementation Signature
1155
1154
  create(body, options) {
1156
1155
  const {
1157
- posthogDistinctId,
1158
- posthogTraceId,
1159
- posthogCaptureImmediate,
1160
- ...openAIParams
1161
- } = body;
1162
- const traceId = posthogTraceId ?? uuid.v4();
1156
+ openAIParams,
1157
+ posthogParams
1158
+ } = extractPosthogParams(body);
1163
1159
  const startTime = Date.now();
1164
1160
  const parentPromise = super.create(openAIParams, options);
1165
1161
  if (openAIParams.stream) {
@@ -1257,8 +1253,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1257
1253
  const latency = (Date.now() - startTime) / 1000;
1258
1254
  await sendEventToPosthog({
1259
1255
  client: this.phClient,
1260
- distinctId: posthogDistinctId,
1261
- traceId,
1256
+ ...posthogParams,
1262
1257
  model: openAIParams.model,
1263
1258
  provider: 'azure',
1264
1259
  input: openAIParams.messages,
@@ -1267,15 +1262,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1267
1262
  baseURL: this.baseURL,
1268
1263
  params: body,
1269
1264
  httpStatus: 200,
1270
- usage,
1271
- captureImmediate: posthogCaptureImmediate
1265
+ usage
1272
1266
  });
1273
1267
  } catch (error) {
1274
1268
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1275
1269
  await sendEventToPosthog({
1276
1270
  client: this.phClient,
1277
- distinctId: posthogDistinctId,
1278
- traceId,
1271
+ ...posthogParams,
1279
1272
  model: openAIParams.model,
1280
1273
  provider: 'azure',
1281
1274
  input: openAIParams.messages,
@@ -1289,8 +1282,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1289
1282
  outputTokens: 0
1290
1283
  },
1291
1284
  isError: true,
1292
- error: JSON.stringify(error),
1293
- captureImmediate: posthogCaptureImmediate
1285
+ error: JSON.stringify(error)
1294
1286
  });
1295
1287
  }
1296
1288
  })();
@@ -1305,8 +1297,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1305
1297
  const latency = (Date.now() - startTime) / 1000;
1306
1298
  await sendEventToPosthog({
1307
1299
  client: this.phClient,
1308
- distinctId: posthogDistinctId,
1309
- traceId,
1300
+ ...posthogParams,
1310
1301
  model: openAIParams.model,
1311
1302
  provider: 'azure',
1312
1303
  input: openAIParams.messages,
@@ -1320,8 +1311,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1320
1311
  outputTokens: result.usage?.completion_tokens ?? 0,
1321
1312
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
1322
1313
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
1323
- },
1324
- captureImmediate: posthogCaptureImmediate
1314
+ }
1325
1315
  });
1326
1316
  }
1327
1317
  return result;
@@ -1329,8 +1319,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1329
1319
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1330
1320
  await sendEventToPosthog({
1331
1321
  client: this.phClient,
1332
- distinctId: posthogDistinctId,
1333
- traceId,
1322
+ ...posthogParams,
1334
1323
  model: openAIParams.model,
1335
1324
  provider: 'azure',
1336
1325
  input: openAIParams.messages,
@@ -1344,8 +1333,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1344
1333
  outputTokens: 0
1345
1334
  },
1346
1335
  isError: true,
1347
- error: JSON.stringify(error),
1348
- captureImmediate: posthogCaptureImmediate
1336
+ error: JSON.stringify(error)
1349
1337
  });
1350
1338
  throw error;
1351
1339
  });
@@ -1362,12 +1350,9 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1362
1350
  // --- Implementation Signature
1363
1351
  create(body, options) {
1364
1352
  const {
1365
- posthogDistinctId,
1366
- posthogTraceId,
1367
- posthogCaptureImmediate,
1368
- ...openAIParams
1369
- } = body;
1370
- const traceId = posthogTraceId ?? uuid.v4();
1353
+ openAIParams,
1354
+ posthogParams
1355
+ } = extractPosthogParams(body);
1371
1356
  const startTime = Date.now();
1372
1357
  const parentPromise = super.create(openAIParams, options);
1373
1358
  if (openAIParams.stream) {
@@ -1397,8 +1382,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1397
1382
  const latency = (Date.now() - startTime) / 1000;
1398
1383
  await sendEventToPosthog({
1399
1384
  client: this.phClient,
1400
- distinctId: posthogDistinctId,
1401
- traceId,
1385
+ ...posthogParams,
1402
1386
  //@ts-expect-error
1403
1387
  model: openAIParams.model,
1404
1388
  provider: 'azure',
@@ -1408,15 +1392,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1408
1392
  baseURL: this.baseURL,
1409
1393
  params: body,
1410
1394
  httpStatus: 200,
1411
- usage,
1412
- captureImmediate: posthogCaptureImmediate
1395
+ usage
1413
1396
  });
1414
1397
  } catch (error) {
1415
1398
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1416
1399
  await sendEventToPosthog({
1417
1400
  client: this.phClient,
1418
- distinctId: posthogDistinctId,
1419
- traceId,
1401
+ ...posthogParams,
1420
1402
  //@ts-expect-error
1421
1403
  model: openAIParams.model,
1422
1404
  provider: 'azure',
@@ -1431,8 +1413,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1431
1413
  outputTokens: 0
1432
1414
  },
1433
1415
  isError: true,
1434
- error: JSON.stringify(error),
1435
- captureImmediate: posthogCaptureImmediate
1416
+ error: JSON.stringify(error)
1436
1417
  });
1437
1418
  }
1438
1419
  })();
@@ -1446,8 +1427,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1446
1427
  const latency = (Date.now() - startTime) / 1000;
1447
1428
  await sendEventToPosthog({
1448
1429
  client: this.phClient,
1449
- distinctId: posthogDistinctId,
1450
- traceId,
1430
+ ...posthogParams,
1451
1431
  //@ts-expect-error
1452
1432
  model: openAIParams.model,
1453
1433
  provider: 'azure',
@@ -1462,8 +1442,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1462
1442
  outputTokens: result.usage?.output_tokens ?? 0,
1463
1443
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1464
1444
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1465
- },
1466
- captureImmediate: posthogCaptureImmediate
1445
+ }
1467
1446
  });
1468
1447
  }
1469
1448
  return result;
@@ -1471,8 +1450,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1471
1450
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1472
1451
  await sendEventToPosthog({
1473
1452
  client: this.phClient,
1474
- distinctId: posthogDistinctId,
1475
- traceId,
1453
+ ...posthogParams,
1476
1454
  //@ts-expect-error
1477
1455
  model: openAIParams.model,
1478
1456
  provider: 'azure',
@@ -1487,8 +1465,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1487
1465
  outputTokens: 0
1488
1466
  },
1489
1467
  isError: true,
1490
- error: JSON.stringify(error),
1491
- captureImmediate: posthogCaptureImmediate
1468
+ error: JSON.stringify(error)
1492
1469
  });
1493
1470
  throw error;
1494
1471
  });
@@ -1497,20 +1474,16 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1497
1474
  }
1498
1475
  parse(body, options) {
1499
1476
  const {
1500
- posthogDistinctId,
1501
- posthogTraceId,
1502
- posthogCaptureImmediate,
1503
- ...openAIParams
1504
- } = body;
1505
- const traceId = posthogTraceId ?? uuid.v4();
1477
+ openAIParams,
1478
+ posthogParams
1479
+ } = extractPosthogParams(body);
1506
1480
  const startTime = Date.now();
1507
1481
  const parentPromise = super.parse(openAIParams, options);
1508
1482
  const wrappedPromise = parentPromise.then(async result => {
1509
1483
  const latency = (Date.now() - startTime) / 1000;
1510
1484
  await sendEventToPosthog({
1511
1485
  client: this.phClient,
1512
- distinctId: posthogDistinctId,
1513
- traceId,
1486
+ ...posthogParams,
1514
1487
  //@ts-expect-error
1515
1488
  model: openAIParams.model,
1516
1489
  provider: 'azure',
@@ -1525,15 +1498,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1525
1498
  outputTokens: result.usage?.output_tokens ?? 0,
1526
1499
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1527
1500
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1528
- },
1529
- captureImmediate: posthogCaptureImmediate
1501
+ }
1530
1502
  });
1531
1503
  return result;
1532
1504
  }, async error => {
1533
1505
  await sendEventToPosthog({
1534
1506
  client: this.phClient,
1535
- distinctId: posthogDistinctId,
1536
- traceId,
1507
+ ...posthogParams,
1537
1508
  //@ts-expect-error
1538
1509
  model: openAIParams.model,
1539
1510
  provider: 'azure',
@@ -1548,8 +1519,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1548
1519
  outputTokens: 0
1549
1520
  },
1550
1521
  isError: true,
1551
- error: JSON.stringify(error),
1552
- captureImmediate: posthogCaptureImmediate
1522
+ error: JSON.stringify(error)
1553
1523
  });
1554
1524
  throw error;
1555
1525
  });
@@ -1564,13 +1534,9 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1564
1534
  }
1565
1535
  create(body, options) {
1566
1536
  const {
1567
- posthogDistinctId,
1568
- posthogTraceId,
1569
- posthogPrivacyMode = false,
1570
- posthogCaptureImmediate,
1571
- ...openAIParams
1572
- } = body;
1573
- const traceId = posthogTraceId ?? uuid.v4();
1537
+ openAIParams,
1538
+ posthogParams
1539
+ } = extractPosthogParams(body);
1574
1540
  const startTime = Date.now();
1575
1541
  const parentPromise = super.create(openAIParams, options);
1576
1542
  const wrappedPromise = parentPromise.then(async result => {
@@ -1578,11 +1544,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1578
1544
  await sendEventToPosthog({
1579
1545
  client: this.phClient,
1580
1546
  eventType: AIEvent.Embedding,
1581
- distinctId: posthogDistinctId,
1582
- traceId,
1547
+ ...posthogParams,
1583
1548
  model: openAIParams.model,
1584
1549
  provider: 'azure',
1585
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1550
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1586
1551
  output: null,
1587
1552
  // Embeddings don't have output content
1588
1553
  latency,
@@ -1591,8 +1556,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1591
1556
  httpStatus: 200,
1592
1557
  usage: {
1593
1558
  inputTokens: result.usage?.prompt_tokens ?? 0
1594
- },
1595
- captureImmediate: posthogCaptureImmediate
1559
+ }
1596
1560
  });
1597
1561
  return result;
1598
1562
  }, async error => {
@@ -1600,11 +1564,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1600
1564
  await sendEventToPosthog({
1601
1565
  client: this.phClient,
1602
1566
  eventType: AIEvent.Embedding,
1603
- distinctId: posthogDistinctId,
1604
- traceId,
1567
+ ...posthogParams,
1605
1568
  model: openAIParams.model,
1606
1569
  provider: 'azure',
1607
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1570
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1608
1571
  output: null,
1609
1572
  latency: 0,
1610
1573
  baseURL: this.baseURL,
@@ -1614,8 +1577,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1614
1577
  inputTokens: 0
1615
1578
  },
1616
1579
  isError: true,
1617
- error: JSON.stringify(error),
1618
- captureImmediate: posthogCaptureImmediate
1580
+ error: JSON.stringify(error)
1619
1581
  });
1620
1582
  throw error;
1621
1583
  });