@posthog/ai 6.3.1 → 6.3.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.
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.3";
30
30
 
31
31
  // limit large outputs by truncating to 200kb (approx 200k bytes)
32
32
  const MAX_OUTPUT_SIZE = 200000;
@@ -311,6 +311,41 @@ function sanitizeValues(obj) {
311
311
  }
312
312
  return jsonSafe;
313
313
  }
314
+ const POSTHOG_PARAMS_MAP = {
315
+ posthogDistinctId: 'distinctId',
316
+ posthogTraceId: 'traceId',
317
+ posthogProperties: 'properties',
318
+ posthogPrivacyMode: 'privacyMode',
319
+ posthogGroups: 'groups',
320
+ posthogModelOverride: 'modelOverride',
321
+ posthogProviderOverride: 'providerOverride',
322
+ posthogCostOverride: 'costOverride',
323
+ posthogCaptureImmediate: 'captureImmediate'
324
+ };
325
+ function extractPosthogParams(body) {
326
+ const providerParams = {};
327
+ const posthogParams = {};
328
+ for (const [key, value] of Object.entries(body)) {
329
+ if (POSTHOG_PARAMS_MAP[key]) {
330
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
331
+ } else if (key.startsWith('posthog')) {
332
+ console.warn(`Unknown Posthog parameter ${key}`);
333
+ } else {
334
+ providerParams[key] = value;
335
+ }
336
+ }
337
+ return {
338
+ providerParams: providerParams,
339
+ posthogParams: addDefaults(posthogParams)
340
+ };
341
+ }
342
+ function addDefaults(params) {
343
+ return {
344
+ ...params,
345
+ privacyMode: params.privacyMode ?? false,
346
+ traceId: params.traceId ?? uuid.v4()
347
+ };
348
+ }
314
349
  const sendEventToPosthog = async ({
315
350
  client,
316
351
  eventType = AIEvent.Generation,
@@ -636,12 +671,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
636
671
  // --- Implementation Signature
637
672
  create(body, options) {
638
673
  const {
639
- posthogDistinctId,
640
- posthogTraceId,
641
- posthogCaptureImmediate,
642
- ...openAIParams
643
- } = body;
644
- const traceId = posthogTraceId ?? uuid.v4();
674
+ providerParams: openAIParams,
675
+ posthogParams
676
+ } = extractPosthogParams(body);
645
677
  const startTime = Date.now();
646
678
  const parentPromise = super.create(openAIParams, options);
647
679
  if (openAIParams.stream) {
@@ -740,8 +772,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
740
772
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
741
773
  await sendEventToPosthog({
742
774
  client: this.phClient,
743
- distinctId: posthogDistinctId,
744
- traceId,
775
+ ...posthogParams,
745
776
  model: openAIParams.model,
746
777
  provider: 'openai',
747
778
  input: sanitizeOpenAI(openAIParams.messages),
@@ -751,15 +782,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
751
782
  params: body,
752
783
  httpStatus: 200,
753
784
  usage,
754
- tools: availableTools,
755
- captureImmediate: posthogCaptureImmediate
785
+ tools: availableTools
756
786
  });
757
787
  } catch (error) {
758
788
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
759
789
  await sendEventToPosthog({
760
790
  client: this.phClient,
761
- distinctId: posthogDistinctId,
762
- traceId,
791
+ ...posthogParams,
763
792
  model: openAIParams.model,
764
793
  provider: 'openai',
765
794
  input: sanitizeOpenAI(openAIParams.messages),
@@ -773,8 +802,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
773
802
  outputTokens: 0
774
803
  },
775
804
  isError: true,
776
- error: JSON.stringify(error),
777
- captureImmediate: posthogCaptureImmediate
805
+ error: JSON.stringify(error)
778
806
  });
779
807
  }
780
808
  })();
@@ -790,8 +818,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
790
818
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
791
819
  await sendEventToPosthog({
792
820
  client: this.phClient,
793
- distinctId: posthogDistinctId,
794
- traceId,
821
+ ...posthogParams,
795
822
  model: openAIParams.model,
796
823
  provider: 'openai',
797
824
  input: sanitizeOpenAI(openAIParams.messages),
@@ -806,8 +833,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
806
833
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
807
834
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
808
835
  },
809
- tools: availableTools,
810
- captureImmediate: posthogCaptureImmediate
836
+ tools: availableTools
811
837
  });
812
838
  }
813
839
  return result;
@@ -815,8 +841,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
815
841
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
816
842
  await sendEventToPosthog({
817
843
  client: this.phClient,
818
- distinctId: posthogDistinctId,
819
- traceId,
844
+ ...posthogParams,
820
845
  model: openAIParams.model,
821
846
  provider: 'openai',
822
847
  input: sanitizeOpenAI(openAIParams.messages),
@@ -830,8 +855,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
830
855
  outputTokens: 0
831
856
  },
832
857
  isError: true,
833
- error: JSON.stringify(error),
834
- captureImmediate: posthogCaptureImmediate
858
+ error: JSON.stringify(error)
835
859
  });
836
860
  throw error;
837
861
  });
@@ -848,12 +872,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
848
872
  // --- Implementation Signature
849
873
  create(body, options) {
850
874
  const {
851
- posthogDistinctId,
852
- posthogTraceId,
853
- posthogCaptureImmediate,
854
- ...openAIParams
855
- } = body;
856
- const traceId = posthogTraceId ?? uuid.v4();
875
+ providerParams: openAIParams,
876
+ posthogParams
877
+ } = extractPosthogParams(body);
857
878
  const startTime = Date.now();
858
879
  const parentPromise = super.create(openAIParams, options);
859
880
  if (openAIParams.stream) {
@@ -884,8 +905,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
884
905
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
885
906
  await sendEventToPosthog({
886
907
  client: this.phClient,
887
- distinctId: posthogDistinctId,
888
- traceId,
908
+ ...posthogParams,
889
909
  //@ts-expect-error
890
910
  model: openAIParams.model,
891
911
  provider: 'openai',
@@ -896,15 +916,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
896
916
  params: body,
897
917
  httpStatus: 200,
898
918
  usage,
899
- tools: availableTools,
900
- captureImmediate: posthogCaptureImmediate
919
+ tools: availableTools
901
920
  });
902
921
  } catch (error) {
903
922
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
904
923
  await sendEventToPosthog({
905
924
  client: this.phClient,
906
- distinctId: posthogDistinctId,
907
- traceId,
925
+ ...posthogParams,
908
926
  //@ts-expect-error
909
927
  model: openAIParams.model,
910
928
  provider: 'openai',
@@ -919,8 +937,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
919
937
  outputTokens: 0
920
938
  },
921
939
  isError: true,
922
- error: JSON.stringify(error),
923
- captureImmediate: posthogCaptureImmediate
940
+ error: JSON.stringify(error)
924
941
  });
925
942
  }
926
943
  })();
@@ -935,8 +952,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
935
952
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
936
953
  await sendEventToPosthog({
937
954
  client: this.phClient,
938
- distinctId: posthogDistinctId,
939
- traceId,
955
+ ...posthogParams,
940
956
  //@ts-expect-error
941
957
  model: openAIParams.model,
942
958
  provider: 'openai',
@@ -954,8 +970,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
954
970
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
955
971
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
956
972
  },
957
- tools: availableTools,
958
- captureImmediate: posthogCaptureImmediate
973
+ tools: availableTools
959
974
  });
960
975
  }
961
976
  return result;
@@ -963,8 +978,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
963
978
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
964
979
  await sendEventToPosthog({
965
980
  client: this.phClient,
966
- distinctId: posthogDistinctId,
967
- traceId,
981
+ ...posthogParams,
968
982
  //@ts-expect-error
969
983
  model: openAIParams.model,
970
984
  provider: 'openai',
@@ -979,8 +993,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
979
993
  outputTokens: 0
980
994
  },
981
995
  isError: true,
982
- error: JSON.stringify(error),
983
- captureImmediate: posthogCaptureImmediate
996
+ error: JSON.stringify(error)
984
997
  });
985
998
  throw error;
986
999
  });
@@ -989,12 +1002,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
989
1002
  }
990
1003
  parse(body, options) {
991
1004
  const {
992
- posthogDistinctId,
993
- posthogTraceId,
994
- posthogCaptureImmediate,
995
- ...openAIParams
996
- } = body;
997
- const traceId = posthogTraceId ?? uuid.v4();
1005
+ providerParams: openAIParams,
1006
+ posthogParams
1007
+ } = extractPosthogParams(body);
998
1008
  const startTime = Date.now();
999
1009
  // Create a temporary instance that bypasses our wrapped create method
1000
1010
  const originalCreate = super.create.bind(this);
@@ -1007,8 +1017,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1007
1017
  const latency = (Date.now() - startTime) / 1000;
1008
1018
  await sendEventToPosthog({
1009
1019
  client: this.phClient,
1010
- distinctId: posthogDistinctId,
1011
- traceId,
1020
+ ...posthogParams,
1012
1021
  //@ts-expect-error
1013
1022
  model: openAIParams.model,
1014
1023
  provider: 'openai',
@@ -1023,16 +1032,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1023
1032
  outputTokens: result.usage?.output_tokens ?? 0,
1024
1033
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1025
1034
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1026
- },
1027
- captureImmediate: posthogCaptureImmediate
1035
+ }
1028
1036
  });
1029
1037
  return result;
1030
1038
  }, async error => {
1031
1039
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1032
1040
  await sendEventToPosthog({
1033
1041
  client: this.phClient,
1034
- distinctId: posthogDistinctId,
1035
- traceId,
1042
+ ...posthogParams,
1036
1043
  //@ts-expect-error
1037
1044
  model: openAIParams.model,
1038
1045
  provider: 'openai',
@@ -1047,8 +1054,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1047
1054
  outputTokens: 0
1048
1055
  },
1049
1056
  isError: true,
1050
- error: JSON.stringify(error),
1051
- captureImmediate: posthogCaptureImmediate
1057
+ error: JSON.stringify(error)
1052
1058
  });
1053
1059
  throw error;
1054
1060
  });
@@ -1067,25 +1073,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1067
1073
  }
1068
1074
  create(body, options) {
1069
1075
  const {
1070
- posthogDistinctId,
1071
- posthogTraceId,
1072
- posthogPrivacyMode = false,
1073
- posthogCaptureImmediate,
1074
- ...openAIParams
1075
- } = body;
1076
- const traceId = posthogTraceId ?? uuid.v4();
1076
+ providerParams: openAIParams,
1077
+ posthogParams
1078
+ } = extractPosthogParams(body);
1077
1079
  const startTime = Date.now();
1078
1080
  const parentPromise = super.create(openAIParams, options);
1079
1081
  const wrappedPromise = parentPromise.then(async result => {
1080
1082
  const latency = (Date.now() - startTime) / 1000;
1081
1083
  await sendEventToPosthog({
1082
1084
  client: this.phClient,
1085
+ ...posthogParams,
1083
1086
  eventType: AIEvent.Embedding,
1084
- distinctId: posthogDistinctId,
1085
- traceId,
1086
1087
  model: openAIParams.model,
1087
1088
  provider: 'openai',
1088
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1089
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1089
1090
  output: null,
1090
1091
  // Embeddings don't have output content
1091
1092
  latency,
@@ -1094,8 +1095,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1094
1095
  httpStatus: 200,
1095
1096
  usage: {
1096
1097
  inputTokens: result.usage?.prompt_tokens ?? 0
1097
- },
1098
- captureImmediate: posthogCaptureImmediate
1098
+ }
1099
1099
  });
1100
1100
  return result;
1101
1101
  }, async error => {
@@ -1103,11 +1103,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1103
1103
  await sendEventToPosthog({
1104
1104
  client: this.phClient,
1105
1105
  eventType: AIEvent.Embedding,
1106
- distinctId: posthogDistinctId,
1107
- traceId,
1106
+ ...posthogParams,
1108
1107
  model: openAIParams.model,
1109
1108
  provider: 'openai',
1110
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1109
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1111
1110
  output: null,
1112
1111
  // Embeddings don't have output content
1113
1112
  latency: 0,
@@ -1118,8 +1117,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1118
1117
  inputTokens: 0
1119
1118
  },
1120
1119
  isError: true,
1121
- error: JSON.stringify(error),
1122
- captureImmediate: posthogCaptureImmediate
1120
+ error: JSON.stringify(error)
1123
1121
  });
1124
1122
  throw error;
1125
1123
  });
@@ -1154,12 +1152,9 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1154
1152
  // --- Implementation Signature
1155
1153
  create(body, options) {
1156
1154
  const {
1157
- posthogDistinctId,
1158
- posthogTraceId,
1159
- posthogCaptureImmediate,
1160
- ...openAIParams
1161
- } = body;
1162
- const traceId = posthogTraceId ?? uuid.v4();
1155
+ providerParams: openAIParams,
1156
+ posthogParams
1157
+ } = extractPosthogParams(body);
1163
1158
  const startTime = Date.now();
1164
1159
  const parentPromise = super.create(openAIParams, options);
1165
1160
  if (openAIParams.stream) {
@@ -1257,8 +1252,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1257
1252
  const latency = (Date.now() - startTime) / 1000;
1258
1253
  await sendEventToPosthog({
1259
1254
  client: this.phClient,
1260
- distinctId: posthogDistinctId,
1261
- traceId,
1255
+ ...posthogParams,
1262
1256
  model: openAIParams.model,
1263
1257
  provider: 'azure',
1264
1258
  input: openAIParams.messages,
@@ -1267,15 +1261,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1267
1261
  baseURL: this.baseURL,
1268
1262
  params: body,
1269
1263
  httpStatus: 200,
1270
- usage,
1271
- captureImmediate: posthogCaptureImmediate
1264
+ usage
1272
1265
  });
1273
1266
  } catch (error) {
1274
1267
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1275
1268
  await sendEventToPosthog({
1276
1269
  client: this.phClient,
1277
- distinctId: posthogDistinctId,
1278
- traceId,
1270
+ ...posthogParams,
1279
1271
  model: openAIParams.model,
1280
1272
  provider: 'azure',
1281
1273
  input: openAIParams.messages,
@@ -1289,8 +1281,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1289
1281
  outputTokens: 0
1290
1282
  },
1291
1283
  isError: true,
1292
- error: JSON.stringify(error),
1293
- captureImmediate: posthogCaptureImmediate
1284
+ error: JSON.stringify(error)
1294
1285
  });
1295
1286
  }
1296
1287
  })();
@@ -1305,8 +1296,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1305
1296
  const latency = (Date.now() - startTime) / 1000;
1306
1297
  await sendEventToPosthog({
1307
1298
  client: this.phClient,
1308
- distinctId: posthogDistinctId,
1309
- traceId,
1299
+ ...posthogParams,
1310
1300
  model: openAIParams.model,
1311
1301
  provider: 'azure',
1312
1302
  input: openAIParams.messages,
@@ -1320,8 +1310,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1320
1310
  outputTokens: result.usage?.completion_tokens ?? 0,
1321
1311
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
1322
1312
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
1323
- },
1324
- captureImmediate: posthogCaptureImmediate
1313
+ }
1325
1314
  });
1326
1315
  }
1327
1316
  return result;
@@ -1329,8 +1318,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1329
1318
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1330
1319
  await sendEventToPosthog({
1331
1320
  client: this.phClient,
1332
- distinctId: posthogDistinctId,
1333
- traceId,
1321
+ ...posthogParams,
1334
1322
  model: openAIParams.model,
1335
1323
  provider: 'azure',
1336
1324
  input: openAIParams.messages,
@@ -1344,8 +1332,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
1344
1332
  outputTokens: 0
1345
1333
  },
1346
1334
  isError: true,
1347
- error: JSON.stringify(error),
1348
- captureImmediate: posthogCaptureImmediate
1335
+ error: JSON.stringify(error)
1349
1336
  });
1350
1337
  throw error;
1351
1338
  });
@@ -1362,12 +1349,9 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1362
1349
  // --- Implementation Signature
1363
1350
  create(body, options) {
1364
1351
  const {
1365
- posthogDistinctId,
1366
- posthogTraceId,
1367
- posthogCaptureImmediate,
1368
- ...openAIParams
1369
- } = body;
1370
- const traceId = posthogTraceId ?? uuid.v4();
1352
+ providerParams: openAIParams,
1353
+ posthogParams
1354
+ } = extractPosthogParams(body);
1371
1355
  const startTime = Date.now();
1372
1356
  const parentPromise = super.create(openAIParams, options);
1373
1357
  if (openAIParams.stream) {
@@ -1397,8 +1381,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1397
1381
  const latency = (Date.now() - startTime) / 1000;
1398
1382
  await sendEventToPosthog({
1399
1383
  client: this.phClient,
1400
- distinctId: posthogDistinctId,
1401
- traceId,
1384
+ ...posthogParams,
1402
1385
  //@ts-expect-error
1403
1386
  model: openAIParams.model,
1404
1387
  provider: 'azure',
@@ -1408,15 +1391,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1408
1391
  baseURL: this.baseURL,
1409
1392
  params: body,
1410
1393
  httpStatus: 200,
1411
- usage,
1412
- captureImmediate: posthogCaptureImmediate
1394
+ usage
1413
1395
  });
1414
1396
  } catch (error) {
1415
1397
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1416
1398
  await sendEventToPosthog({
1417
1399
  client: this.phClient,
1418
- distinctId: posthogDistinctId,
1419
- traceId,
1400
+ ...posthogParams,
1420
1401
  //@ts-expect-error
1421
1402
  model: openAIParams.model,
1422
1403
  provider: 'azure',
@@ -1431,8 +1412,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1431
1412
  outputTokens: 0
1432
1413
  },
1433
1414
  isError: true,
1434
- error: JSON.stringify(error),
1435
- captureImmediate: posthogCaptureImmediate
1415
+ error: JSON.stringify(error)
1436
1416
  });
1437
1417
  }
1438
1418
  })();
@@ -1446,8 +1426,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1446
1426
  const latency = (Date.now() - startTime) / 1000;
1447
1427
  await sendEventToPosthog({
1448
1428
  client: this.phClient,
1449
- distinctId: posthogDistinctId,
1450
- traceId,
1429
+ ...posthogParams,
1451
1430
  //@ts-expect-error
1452
1431
  model: openAIParams.model,
1453
1432
  provider: 'azure',
@@ -1462,8 +1441,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1462
1441
  outputTokens: result.usage?.output_tokens ?? 0,
1463
1442
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1464
1443
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1465
- },
1466
- captureImmediate: posthogCaptureImmediate
1444
+ }
1467
1445
  });
1468
1446
  }
1469
1447
  return result;
@@ -1471,8 +1449,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1471
1449
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1472
1450
  await sendEventToPosthog({
1473
1451
  client: this.phClient,
1474
- distinctId: posthogDistinctId,
1475
- traceId,
1452
+ ...posthogParams,
1476
1453
  //@ts-expect-error
1477
1454
  model: openAIParams.model,
1478
1455
  provider: 'azure',
@@ -1487,8 +1464,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1487
1464
  outputTokens: 0
1488
1465
  },
1489
1466
  isError: true,
1490
- error: JSON.stringify(error),
1491
- captureImmediate: posthogCaptureImmediate
1467
+ error: JSON.stringify(error)
1492
1468
  });
1493
1469
  throw error;
1494
1470
  });
@@ -1497,20 +1473,16 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1497
1473
  }
1498
1474
  parse(body, options) {
1499
1475
  const {
1500
- posthogDistinctId,
1501
- posthogTraceId,
1502
- posthogCaptureImmediate,
1503
- ...openAIParams
1504
- } = body;
1505
- const traceId = posthogTraceId ?? uuid.v4();
1476
+ providerParams: openAIParams,
1477
+ posthogParams
1478
+ } = extractPosthogParams(body);
1506
1479
  const startTime = Date.now();
1507
1480
  const parentPromise = super.parse(openAIParams, options);
1508
1481
  const wrappedPromise = parentPromise.then(async result => {
1509
1482
  const latency = (Date.now() - startTime) / 1000;
1510
1483
  await sendEventToPosthog({
1511
1484
  client: this.phClient,
1512
- distinctId: posthogDistinctId,
1513
- traceId,
1485
+ ...posthogParams,
1514
1486
  //@ts-expect-error
1515
1487
  model: openAIParams.model,
1516
1488
  provider: 'azure',
@@ -1525,15 +1497,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1525
1497
  outputTokens: result.usage?.output_tokens ?? 0,
1526
1498
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1527
1499
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1528
- },
1529
- captureImmediate: posthogCaptureImmediate
1500
+ }
1530
1501
  });
1531
1502
  return result;
1532
1503
  }, async error => {
1533
1504
  await sendEventToPosthog({
1534
1505
  client: this.phClient,
1535
- distinctId: posthogDistinctId,
1536
- traceId,
1506
+ ...posthogParams,
1537
1507
  //@ts-expect-error
1538
1508
  model: openAIParams.model,
1539
1509
  provider: 'azure',
@@ -1548,8 +1518,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
1548
1518
  outputTokens: 0
1549
1519
  },
1550
1520
  isError: true,
1551
- error: JSON.stringify(error),
1552
- captureImmediate: posthogCaptureImmediate
1521
+ error: JSON.stringify(error)
1553
1522
  });
1554
1523
  throw error;
1555
1524
  });
@@ -1564,13 +1533,9 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1564
1533
  }
1565
1534
  create(body, options) {
1566
1535
  const {
1567
- posthogDistinctId,
1568
- posthogTraceId,
1569
- posthogPrivacyMode = false,
1570
- posthogCaptureImmediate,
1571
- ...openAIParams
1572
- } = body;
1573
- const traceId = posthogTraceId ?? uuid.v4();
1536
+ providerParams: openAIParams,
1537
+ posthogParams
1538
+ } = extractPosthogParams(body);
1574
1539
  const startTime = Date.now();
1575
1540
  const parentPromise = super.create(openAIParams, options);
1576
1541
  const wrappedPromise = parentPromise.then(async result => {
@@ -1578,11 +1543,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1578
1543
  await sendEventToPosthog({
1579
1544
  client: this.phClient,
1580
1545
  eventType: AIEvent.Embedding,
1581
- distinctId: posthogDistinctId,
1582
- traceId,
1546
+ ...posthogParams,
1583
1547
  model: openAIParams.model,
1584
1548
  provider: 'azure',
1585
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1549
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1586
1550
  output: null,
1587
1551
  // Embeddings don't have output content
1588
1552
  latency,
@@ -1591,8 +1555,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1591
1555
  httpStatus: 200,
1592
1556
  usage: {
1593
1557
  inputTokens: result.usage?.prompt_tokens ?? 0
1594
- },
1595
- captureImmediate: posthogCaptureImmediate
1558
+ }
1596
1559
  });
1597
1560
  return result;
1598
1561
  }, async error => {
@@ -1600,11 +1563,10 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1600
1563
  await sendEventToPosthog({
1601
1564
  client: this.phClient,
1602
1565
  eventType: AIEvent.Embedding,
1603
- distinctId: posthogDistinctId,
1604
- traceId,
1566
+ ...posthogParams,
1605
1567
  model: openAIParams.model,
1606
1568
  provider: 'azure',
1607
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1569
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1608
1570
  output: null,
1609
1571
  latency: 0,
1610
1572
  baseURL: this.baseURL,
@@ -1614,8 +1576,7 @@ class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
1614
1576
  inputTokens: 0
1615
1577
  },
1616
1578
  isError: true,
1617
- error: JSON.stringify(error),
1618
- captureImmediate: posthogCaptureImmediate
1579
+ error: JSON.stringify(error)
1619
1580
  });
1620
1581
  throw error;
1621
1582
  });
@@ -2088,12 +2049,9 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2088
2049
  }
2089
2050
  create(body, options) {
2090
2051
  const {
2091
- posthogDistinctId,
2092
- posthogTraceId,
2093
- posthogCaptureImmediate,
2094
- ...anthropicParams
2095
- } = body;
2096
- const traceId = posthogTraceId ?? uuid.v4();
2052
+ providerParams: anthropicParams,
2053
+ posthogParams
2054
+ } = extractPosthogParams(body);
2097
2055
  const startTime = Date.now();
2098
2056
  const parentPromise = super.create(anthropicParams, options);
2099
2057
  if (anthropicParams.stream) {
@@ -2203,8 +2161,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2203
2161
  }];
2204
2162
  await sendEventToPosthog({
2205
2163
  client: this.phClient,
2206
- distinctId: posthogDistinctId,
2207
- traceId,
2164
+ ...posthogParams,
2208
2165
  model: anthropicParams.model,
2209
2166
  provider: 'anthropic',
2210
2167
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
@@ -2214,15 +2171,13 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2214
2171
  params: body,
2215
2172
  httpStatus: 200,
2216
2173
  usage,
2217
- tools: availableTools,
2218
- captureImmediate: posthogCaptureImmediate
2174
+ tools: availableTools
2219
2175
  });
2220
2176
  } catch (error) {
2221
2177
  // error handling
2222
2178
  await sendEventToPosthog({
2223
2179
  client: this.phClient,
2224
- distinctId: posthogDistinctId,
2225
- traceId,
2180
+ ...posthogParams,
2226
2181
  model: anthropicParams.model,
2227
2182
  provider: 'anthropic',
2228
2183
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2236,8 +2191,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2236
2191
  outputTokens: 0
2237
2192
  },
2238
2193
  isError: true,
2239
- error: JSON.stringify(error),
2240
- captureImmediate: posthogCaptureImmediate
2194
+ error: JSON.stringify(error)
2241
2195
  });
2242
2196
  }
2243
2197
  })();
@@ -2253,8 +2207,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2253
2207
  const availableTools = extractAvailableToolCalls('anthropic', anthropicParams);
2254
2208
  await sendEventToPosthog({
2255
2209
  client: this.phClient,
2256
- distinctId: posthogDistinctId,
2257
- traceId,
2210
+ ...posthogParams,
2258
2211
  model: anthropicParams.model,
2259
2212
  provider: 'anthropic',
2260
2213
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2269,16 +2222,14 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2269
2222
  cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0,
2270
2223
  cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0
2271
2224
  },
2272
- tools: availableTools,
2273
- captureImmediate: posthogCaptureImmediate
2225
+ tools: availableTools
2274
2226
  });
2275
2227
  }
2276
2228
  return result;
2277
2229
  }, async error => {
2278
2230
  await sendEventToPosthog({
2279
2231
  client: this.phClient,
2280
- distinctId: posthogDistinctId,
2281
- traceId,
2232
+ ...posthogParams,
2282
2233
  model: anthropicParams.model,
2283
2234
  provider: 'anthropic',
2284
2235
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2292,8 +2243,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2292
2243
  outputTokens: 0
2293
2244
  },
2294
2245
  isError: true,
2295
- error: JSON.stringify(error),
2296
- captureImmediate: posthogCaptureImmediate
2246
+ error: JSON.stringify(error)
2297
2247
  });
2298
2248
  throw error;
2299
2249
  });
@@ -2320,12 +2270,9 @@ class WrappedModels {
2320
2270
  }
2321
2271
  async generateContent(params) {
2322
2272
  const {
2323
- posthogDistinctId,
2324
- posthogTraceId,
2325
- posthogCaptureImmediate,
2326
- ...geminiParams
2327
- } = params;
2328
- const traceId = posthogTraceId ?? uuid.v4();
2273
+ providerParams: geminiParams,
2274
+ posthogParams
2275
+ } = extractPosthogParams(params);
2329
2276
  const startTime = Date.now();
2330
2277
  try {
2331
2278
  const response = await this.client.models.generateContent(geminiParams);
@@ -2334,8 +2281,7 @@ class WrappedModels {
2334
2281
  const metadata = response.usageMetadata;
2335
2282
  await sendEventToPosthog({
2336
2283
  client: this.phClient,
2337
- distinctId: posthogDistinctId,
2338
- traceId,
2284
+ ...posthogParams,
2339
2285
  model: geminiParams.model,
2340
2286
  provider: 'gemini',
2341
2287
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2350,16 +2296,14 @@ class WrappedModels {
2350
2296
  reasoningTokens: metadata?.thoughtsTokenCount ?? 0,
2351
2297
  cacheReadInputTokens: metadata?.cachedContentTokenCount ?? 0
2352
2298
  },
2353
- tools: availableTools,
2354
- captureImmediate: posthogCaptureImmediate
2299
+ tools: availableTools
2355
2300
  });
2356
2301
  return response;
2357
2302
  } catch (error) {
2358
2303
  const latency = (Date.now() - startTime) / 1000;
2359
2304
  await sendEventToPosthog({
2360
2305
  client: this.phClient,
2361
- distinctId: posthogDistinctId,
2362
- traceId,
2306
+ ...posthogParams,
2363
2307
  model: geminiParams.model,
2364
2308
  provider: 'gemini',
2365
2309
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2373,20 +2317,16 @@ class WrappedModels {
2373
2317
  outputTokens: 0
2374
2318
  },
2375
2319
  isError: true,
2376
- error: JSON.stringify(error),
2377
- captureImmediate: posthogCaptureImmediate
2320
+ error: JSON.stringify(error)
2378
2321
  });
2379
2322
  throw error;
2380
2323
  }
2381
2324
  }
2382
2325
  async *generateContentStream(params) {
2383
2326
  const {
2384
- posthogDistinctId,
2385
- posthogTraceId,
2386
- posthogCaptureImmediate,
2387
- ...geminiParams
2388
- } = params;
2389
- const traceId = posthogTraceId ?? uuid.v4();
2327
+ providerParams: geminiParams,
2328
+ posthogParams
2329
+ } = extractPosthogParams(params);
2390
2330
  const startTime = Date.now();
2391
2331
  const accumulatedContent = [];
2392
2332
  let usage = {
@@ -2458,8 +2398,7 @@ class WrappedModels {
2458
2398
  }] : [];
2459
2399
  await sendEventToPosthog({
2460
2400
  client: this.phClient,
2461
- distinctId: posthogDistinctId,
2462
- traceId,
2401
+ ...posthogParams,
2463
2402
  model: geminiParams.model,
2464
2403
  provider: 'gemini',
2465
2404
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2469,15 +2408,13 @@ class WrappedModels {
2469
2408
  params: params,
2470
2409
  httpStatus: 200,
2471
2410
  usage,
2472
- tools: availableTools,
2473
- captureImmediate: posthogCaptureImmediate
2411
+ tools: availableTools
2474
2412
  });
2475
2413
  } catch (error) {
2476
2414
  const latency = (Date.now() - startTime) / 1000;
2477
2415
  await sendEventToPosthog({
2478
2416
  client: this.phClient,
2479
- distinctId: posthogDistinctId,
2480
- traceId,
2417
+ ...posthogParams,
2481
2418
  model: geminiParams.model,
2482
2419
  provider: 'gemini',
2483
2420
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2491,8 +2428,7 @@ class WrappedModels {
2491
2428
  outputTokens: 0
2492
2429
  },
2493
2430
  isError: true,
2494
- error: JSON.stringify(error),
2495
- captureImmediate: posthogCaptureImmediate
2431
+ error: JSON.stringify(error)
2496
2432
  });
2497
2433
  throw error;
2498
2434
  }