@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.mjs CHANGED
@@ -1,12 +1,12 @@
1
1
  import { OpenAI, AzureOpenAI } from 'openai';
2
+ import { Buffer } from 'buffer';
2
3
  import * as uuid from 'uuid';
3
4
  import { v4 } from 'uuid';
4
- import { Buffer } from 'buffer';
5
5
  import { wrapLanguageModel } from 'ai';
6
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
7
  import { GoogleGenAI } from '@google/genai';
8
8
 
9
- var version = "6.3.1";
9
+ var version = "6.3.3";
10
10
 
11
11
  // limit large outputs by truncating to 200kb (approx 200k bytes)
12
12
  const MAX_OUTPUT_SIZE = 200000;
@@ -291,6 +291,41 @@ function sanitizeValues(obj) {
291
291
  }
292
292
  return jsonSafe;
293
293
  }
294
+ const POSTHOG_PARAMS_MAP = {
295
+ posthogDistinctId: 'distinctId',
296
+ posthogTraceId: 'traceId',
297
+ posthogProperties: 'properties',
298
+ posthogPrivacyMode: 'privacyMode',
299
+ posthogGroups: 'groups',
300
+ posthogModelOverride: 'modelOverride',
301
+ posthogProviderOverride: 'providerOverride',
302
+ posthogCostOverride: 'costOverride',
303
+ posthogCaptureImmediate: 'captureImmediate'
304
+ };
305
+ function extractPosthogParams(body) {
306
+ const providerParams = {};
307
+ const posthogParams = {};
308
+ for (const [key, value] of Object.entries(body)) {
309
+ if (POSTHOG_PARAMS_MAP[key]) {
310
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
311
+ } else if (key.startsWith('posthog')) {
312
+ console.warn(`Unknown Posthog parameter ${key}`);
313
+ } else {
314
+ providerParams[key] = value;
315
+ }
316
+ }
317
+ return {
318
+ providerParams: providerParams,
319
+ posthogParams: addDefaults(posthogParams)
320
+ };
321
+ }
322
+ function addDefaults(params) {
323
+ return {
324
+ ...params,
325
+ privacyMode: params.privacyMode ?? false,
326
+ traceId: params.traceId ?? v4()
327
+ };
328
+ }
294
329
  const sendEventToPosthog = async ({
295
330
  client,
296
331
  eventType = AIEvent.Generation,
@@ -616,12 +651,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
616
651
  // --- Implementation Signature
617
652
  create(body, options) {
618
653
  const {
619
- posthogDistinctId,
620
- posthogTraceId,
621
- posthogCaptureImmediate,
622
- ...openAIParams
623
- } = body;
624
- const traceId = posthogTraceId ?? v4();
654
+ providerParams: openAIParams,
655
+ posthogParams
656
+ } = extractPosthogParams(body);
625
657
  const startTime = Date.now();
626
658
  const parentPromise = super.create(openAIParams, options);
627
659
  if (openAIParams.stream) {
@@ -720,8 +752,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
720
752
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
721
753
  await sendEventToPosthog({
722
754
  client: this.phClient,
723
- distinctId: posthogDistinctId,
724
- traceId,
755
+ ...posthogParams,
725
756
  model: openAIParams.model,
726
757
  provider: 'openai',
727
758
  input: sanitizeOpenAI(openAIParams.messages),
@@ -731,15 +762,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
731
762
  params: body,
732
763
  httpStatus: 200,
733
764
  usage,
734
- tools: availableTools,
735
- captureImmediate: posthogCaptureImmediate
765
+ tools: availableTools
736
766
  });
737
767
  } catch (error) {
738
768
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
739
769
  await sendEventToPosthog({
740
770
  client: this.phClient,
741
- distinctId: posthogDistinctId,
742
- traceId,
771
+ ...posthogParams,
743
772
  model: openAIParams.model,
744
773
  provider: 'openai',
745
774
  input: sanitizeOpenAI(openAIParams.messages),
@@ -753,8 +782,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
753
782
  outputTokens: 0
754
783
  },
755
784
  isError: true,
756
- error: JSON.stringify(error),
757
- captureImmediate: posthogCaptureImmediate
785
+ error: JSON.stringify(error)
758
786
  });
759
787
  }
760
788
  })();
@@ -770,8 +798,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
770
798
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
771
799
  await sendEventToPosthog({
772
800
  client: this.phClient,
773
- distinctId: posthogDistinctId,
774
- traceId,
801
+ ...posthogParams,
775
802
  model: openAIParams.model,
776
803
  provider: 'openai',
777
804
  input: sanitizeOpenAI(openAIParams.messages),
@@ -786,8 +813,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
786
813
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
787
814
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
788
815
  },
789
- tools: availableTools,
790
- captureImmediate: posthogCaptureImmediate
816
+ tools: availableTools
791
817
  });
792
818
  }
793
819
  return result;
@@ -795,8 +821,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
795
821
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
796
822
  await sendEventToPosthog({
797
823
  client: this.phClient,
798
- distinctId: posthogDistinctId,
799
- traceId,
824
+ ...posthogParams,
800
825
  model: openAIParams.model,
801
826
  provider: 'openai',
802
827
  input: sanitizeOpenAI(openAIParams.messages),
@@ -810,8 +835,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
810
835
  outputTokens: 0
811
836
  },
812
837
  isError: true,
813
- error: JSON.stringify(error),
814
- captureImmediate: posthogCaptureImmediate
838
+ error: JSON.stringify(error)
815
839
  });
816
840
  throw error;
817
841
  });
@@ -828,12 +852,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
828
852
  // --- Implementation Signature
829
853
  create(body, options) {
830
854
  const {
831
- posthogDistinctId,
832
- posthogTraceId,
833
- posthogCaptureImmediate,
834
- ...openAIParams
835
- } = body;
836
- const traceId = posthogTraceId ?? v4();
855
+ providerParams: openAIParams,
856
+ posthogParams
857
+ } = extractPosthogParams(body);
837
858
  const startTime = Date.now();
838
859
  const parentPromise = super.create(openAIParams, options);
839
860
  if (openAIParams.stream) {
@@ -864,8 +885,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
864
885
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
865
886
  await sendEventToPosthog({
866
887
  client: this.phClient,
867
- distinctId: posthogDistinctId,
868
- traceId,
888
+ ...posthogParams,
869
889
  //@ts-expect-error
870
890
  model: openAIParams.model,
871
891
  provider: 'openai',
@@ -876,15 +896,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
876
896
  params: body,
877
897
  httpStatus: 200,
878
898
  usage,
879
- tools: availableTools,
880
- captureImmediate: posthogCaptureImmediate
899
+ tools: availableTools
881
900
  });
882
901
  } catch (error) {
883
902
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
884
903
  await sendEventToPosthog({
885
904
  client: this.phClient,
886
- distinctId: posthogDistinctId,
887
- traceId,
905
+ ...posthogParams,
888
906
  //@ts-expect-error
889
907
  model: openAIParams.model,
890
908
  provider: 'openai',
@@ -899,8 +917,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
899
917
  outputTokens: 0
900
918
  },
901
919
  isError: true,
902
- error: JSON.stringify(error),
903
- captureImmediate: posthogCaptureImmediate
920
+ error: JSON.stringify(error)
904
921
  });
905
922
  }
906
923
  })();
@@ -915,8 +932,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
915
932
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
916
933
  await sendEventToPosthog({
917
934
  client: this.phClient,
918
- distinctId: posthogDistinctId,
919
- traceId,
935
+ ...posthogParams,
920
936
  //@ts-expect-error
921
937
  model: openAIParams.model,
922
938
  provider: 'openai',
@@ -934,8 +950,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
934
950
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
935
951
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
936
952
  },
937
- tools: availableTools,
938
- captureImmediate: posthogCaptureImmediate
953
+ tools: availableTools
939
954
  });
940
955
  }
941
956
  return result;
@@ -943,8 +958,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
943
958
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
944
959
  await sendEventToPosthog({
945
960
  client: this.phClient,
946
- distinctId: posthogDistinctId,
947
- traceId,
961
+ ...posthogParams,
948
962
  //@ts-expect-error
949
963
  model: openAIParams.model,
950
964
  provider: 'openai',
@@ -959,8 +973,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
959
973
  outputTokens: 0
960
974
  },
961
975
  isError: true,
962
- error: JSON.stringify(error),
963
- captureImmediate: posthogCaptureImmediate
976
+ error: JSON.stringify(error)
964
977
  });
965
978
  throw error;
966
979
  });
@@ -969,12 +982,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
969
982
  }
970
983
  parse(body, options) {
971
984
  const {
972
- posthogDistinctId,
973
- posthogTraceId,
974
- posthogCaptureImmediate,
975
- ...openAIParams
976
- } = body;
977
- const traceId = posthogTraceId ?? v4();
985
+ providerParams: openAIParams,
986
+ posthogParams
987
+ } = extractPosthogParams(body);
978
988
  const startTime = Date.now();
979
989
  // Create a temporary instance that bypasses our wrapped create method
980
990
  const originalCreate = super.create.bind(this);
@@ -987,8 +997,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
987
997
  const latency = (Date.now() - startTime) / 1000;
988
998
  await sendEventToPosthog({
989
999
  client: this.phClient,
990
- distinctId: posthogDistinctId,
991
- traceId,
1000
+ ...posthogParams,
992
1001
  //@ts-expect-error
993
1002
  model: openAIParams.model,
994
1003
  provider: 'openai',
@@ -1003,16 +1012,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1003
1012
  outputTokens: result.usage?.output_tokens ?? 0,
1004
1013
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1005
1014
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1006
- },
1007
- captureImmediate: posthogCaptureImmediate
1015
+ }
1008
1016
  });
1009
1017
  return result;
1010
1018
  }, async error => {
1011
1019
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1012
1020
  await sendEventToPosthog({
1013
1021
  client: this.phClient,
1014
- distinctId: posthogDistinctId,
1015
- traceId,
1022
+ ...posthogParams,
1016
1023
  //@ts-expect-error
1017
1024
  model: openAIParams.model,
1018
1025
  provider: 'openai',
@@ -1027,8 +1034,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1027
1034
  outputTokens: 0
1028
1035
  },
1029
1036
  isError: true,
1030
- error: JSON.stringify(error),
1031
- captureImmediate: posthogCaptureImmediate
1037
+ error: JSON.stringify(error)
1032
1038
  });
1033
1039
  throw error;
1034
1040
  });
@@ -1047,25 +1053,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1047
1053
  }
1048
1054
  create(body, options) {
1049
1055
  const {
1050
- posthogDistinctId,
1051
- posthogTraceId,
1052
- posthogPrivacyMode = false,
1053
- posthogCaptureImmediate,
1054
- ...openAIParams
1055
- } = body;
1056
- const traceId = posthogTraceId ?? v4();
1056
+ providerParams: openAIParams,
1057
+ posthogParams
1058
+ } = extractPosthogParams(body);
1057
1059
  const startTime = Date.now();
1058
1060
  const parentPromise = super.create(openAIParams, options);
1059
1061
  const wrappedPromise = parentPromise.then(async result => {
1060
1062
  const latency = (Date.now() - startTime) / 1000;
1061
1063
  await sendEventToPosthog({
1062
1064
  client: this.phClient,
1065
+ ...posthogParams,
1063
1066
  eventType: AIEvent.Embedding,
1064
- distinctId: posthogDistinctId,
1065
- traceId,
1066
1067
  model: openAIParams.model,
1067
1068
  provider: 'openai',
1068
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1069
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1069
1070
  output: null,
1070
1071
  // Embeddings don't have output content
1071
1072
  latency,
@@ -1074,8 +1075,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1074
1075
  httpStatus: 200,
1075
1076
  usage: {
1076
1077
  inputTokens: result.usage?.prompt_tokens ?? 0
1077
- },
1078
- captureImmediate: posthogCaptureImmediate
1078
+ }
1079
1079
  });
1080
1080
  return result;
1081
1081
  }, async error => {
@@ -1083,11 +1083,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1083
1083
  await sendEventToPosthog({
1084
1084
  client: this.phClient,
1085
1085
  eventType: AIEvent.Embedding,
1086
- distinctId: posthogDistinctId,
1087
- traceId,
1086
+ ...posthogParams,
1088
1087
  model: openAIParams.model,
1089
1088
  provider: 'openai',
1090
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1089
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1091
1090
  output: null,
1092
1091
  // Embeddings don't have output content
1093
1092
  latency: 0,
@@ -1098,8 +1097,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1098
1097
  inputTokens: 0
1099
1098
  },
1100
1099
  isError: true,
1101
- error: JSON.stringify(error),
1102
- captureImmediate: posthogCaptureImmediate
1100
+ error: JSON.stringify(error)
1103
1101
  });
1104
1102
  throw error;
1105
1103
  });
@@ -1134,12 +1132,9 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1134
1132
  // --- Implementation Signature
1135
1133
  create(body, options) {
1136
1134
  const {
1137
- posthogDistinctId,
1138
- posthogTraceId,
1139
- posthogCaptureImmediate,
1140
- ...openAIParams
1141
- } = body;
1142
- const traceId = posthogTraceId ?? v4();
1135
+ providerParams: openAIParams,
1136
+ posthogParams
1137
+ } = extractPosthogParams(body);
1143
1138
  const startTime = Date.now();
1144
1139
  const parentPromise = super.create(openAIParams, options);
1145
1140
  if (openAIParams.stream) {
@@ -1237,8 +1232,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1237
1232
  const latency = (Date.now() - startTime) / 1000;
1238
1233
  await sendEventToPosthog({
1239
1234
  client: this.phClient,
1240
- distinctId: posthogDistinctId,
1241
- traceId,
1235
+ ...posthogParams,
1242
1236
  model: openAIParams.model,
1243
1237
  provider: 'azure',
1244
1238
  input: openAIParams.messages,
@@ -1247,15 +1241,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1247
1241
  baseURL: this.baseURL,
1248
1242
  params: body,
1249
1243
  httpStatus: 200,
1250
- usage,
1251
- captureImmediate: posthogCaptureImmediate
1244
+ usage
1252
1245
  });
1253
1246
  } catch (error) {
1254
1247
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1255
1248
  await sendEventToPosthog({
1256
1249
  client: this.phClient,
1257
- distinctId: posthogDistinctId,
1258
- traceId,
1250
+ ...posthogParams,
1259
1251
  model: openAIParams.model,
1260
1252
  provider: 'azure',
1261
1253
  input: openAIParams.messages,
@@ -1269,8 +1261,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1269
1261
  outputTokens: 0
1270
1262
  },
1271
1263
  isError: true,
1272
- error: JSON.stringify(error),
1273
- captureImmediate: posthogCaptureImmediate
1264
+ error: JSON.stringify(error)
1274
1265
  });
1275
1266
  }
1276
1267
  })();
@@ -1285,8 +1276,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1285
1276
  const latency = (Date.now() - startTime) / 1000;
1286
1277
  await sendEventToPosthog({
1287
1278
  client: this.phClient,
1288
- distinctId: posthogDistinctId,
1289
- traceId,
1279
+ ...posthogParams,
1290
1280
  model: openAIParams.model,
1291
1281
  provider: 'azure',
1292
1282
  input: openAIParams.messages,
@@ -1300,8 +1290,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1300
1290
  outputTokens: result.usage?.completion_tokens ?? 0,
1301
1291
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
1302
1292
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
1303
- },
1304
- captureImmediate: posthogCaptureImmediate
1293
+ }
1305
1294
  });
1306
1295
  }
1307
1296
  return result;
@@ -1309,8 +1298,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1309
1298
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1310
1299
  await sendEventToPosthog({
1311
1300
  client: this.phClient,
1312
- distinctId: posthogDistinctId,
1313
- traceId,
1301
+ ...posthogParams,
1314
1302
  model: openAIParams.model,
1315
1303
  provider: 'azure',
1316
1304
  input: openAIParams.messages,
@@ -1324,8 +1312,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1324
1312
  outputTokens: 0
1325
1313
  },
1326
1314
  isError: true,
1327
- error: JSON.stringify(error),
1328
- captureImmediate: posthogCaptureImmediate
1315
+ error: JSON.stringify(error)
1329
1316
  });
1330
1317
  throw error;
1331
1318
  });
@@ -1342,12 +1329,9 @@ class WrappedResponses extends AzureOpenAI.Responses {
1342
1329
  // --- Implementation Signature
1343
1330
  create(body, options) {
1344
1331
  const {
1345
- posthogDistinctId,
1346
- posthogTraceId,
1347
- posthogCaptureImmediate,
1348
- ...openAIParams
1349
- } = body;
1350
- const traceId = posthogTraceId ?? v4();
1332
+ providerParams: openAIParams,
1333
+ posthogParams
1334
+ } = extractPosthogParams(body);
1351
1335
  const startTime = Date.now();
1352
1336
  const parentPromise = super.create(openAIParams, options);
1353
1337
  if (openAIParams.stream) {
@@ -1377,8 +1361,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1377
1361
  const latency = (Date.now() - startTime) / 1000;
1378
1362
  await sendEventToPosthog({
1379
1363
  client: this.phClient,
1380
- distinctId: posthogDistinctId,
1381
- traceId,
1364
+ ...posthogParams,
1382
1365
  //@ts-expect-error
1383
1366
  model: openAIParams.model,
1384
1367
  provider: 'azure',
@@ -1388,15 +1371,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
1388
1371
  baseURL: this.baseURL,
1389
1372
  params: body,
1390
1373
  httpStatus: 200,
1391
- usage,
1392
- captureImmediate: posthogCaptureImmediate
1374
+ usage
1393
1375
  });
1394
1376
  } catch (error) {
1395
1377
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1396
1378
  await sendEventToPosthog({
1397
1379
  client: this.phClient,
1398
- distinctId: posthogDistinctId,
1399
- traceId,
1380
+ ...posthogParams,
1400
1381
  //@ts-expect-error
1401
1382
  model: openAIParams.model,
1402
1383
  provider: 'azure',
@@ -1411,8 +1392,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1411
1392
  outputTokens: 0
1412
1393
  },
1413
1394
  isError: true,
1414
- error: JSON.stringify(error),
1415
- captureImmediate: posthogCaptureImmediate
1395
+ error: JSON.stringify(error)
1416
1396
  });
1417
1397
  }
1418
1398
  })();
@@ -1426,8 +1406,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1426
1406
  const latency = (Date.now() - startTime) / 1000;
1427
1407
  await sendEventToPosthog({
1428
1408
  client: this.phClient,
1429
- distinctId: posthogDistinctId,
1430
- traceId,
1409
+ ...posthogParams,
1431
1410
  //@ts-expect-error
1432
1411
  model: openAIParams.model,
1433
1412
  provider: 'azure',
@@ -1442,8 +1421,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1442
1421
  outputTokens: result.usage?.output_tokens ?? 0,
1443
1422
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1444
1423
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1445
- },
1446
- captureImmediate: posthogCaptureImmediate
1424
+ }
1447
1425
  });
1448
1426
  }
1449
1427
  return result;
@@ -1451,8 +1429,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1451
1429
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1452
1430
  await sendEventToPosthog({
1453
1431
  client: this.phClient,
1454
- distinctId: posthogDistinctId,
1455
- traceId,
1432
+ ...posthogParams,
1456
1433
  //@ts-expect-error
1457
1434
  model: openAIParams.model,
1458
1435
  provider: 'azure',
@@ -1467,8 +1444,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1467
1444
  outputTokens: 0
1468
1445
  },
1469
1446
  isError: true,
1470
- error: JSON.stringify(error),
1471
- captureImmediate: posthogCaptureImmediate
1447
+ error: JSON.stringify(error)
1472
1448
  });
1473
1449
  throw error;
1474
1450
  });
@@ -1477,20 +1453,16 @@ class WrappedResponses extends AzureOpenAI.Responses {
1477
1453
  }
1478
1454
  parse(body, options) {
1479
1455
  const {
1480
- posthogDistinctId,
1481
- posthogTraceId,
1482
- posthogCaptureImmediate,
1483
- ...openAIParams
1484
- } = body;
1485
- const traceId = posthogTraceId ?? v4();
1456
+ providerParams: openAIParams,
1457
+ posthogParams
1458
+ } = extractPosthogParams(body);
1486
1459
  const startTime = Date.now();
1487
1460
  const parentPromise = super.parse(openAIParams, options);
1488
1461
  const wrappedPromise = parentPromise.then(async result => {
1489
1462
  const latency = (Date.now() - startTime) / 1000;
1490
1463
  await sendEventToPosthog({
1491
1464
  client: this.phClient,
1492
- distinctId: posthogDistinctId,
1493
- traceId,
1465
+ ...posthogParams,
1494
1466
  //@ts-expect-error
1495
1467
  model: openAIParams.model,
1496
1468
  provider: 'azure',
@@ -1505,15 +1477,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
1505
1477
  outputTokens: result.usage?.output_tokens ?? 0,
1506
1478
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1507
1479
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1508
- },
1509
- captureImmediate: posthogCaptureImmediate
1480
+ }
1510
1481
  });
1511
1482
  return result;
1512
1483
  }, async error => {
1513
1484
  await sendEventToPosthog({
1514
1485
  client: this.phClient,
1515
- distinctId: posthogDistinctId,
1516
- traceId,
1486
+ ...posthogParams,
1517
1487
  //@ts-expect-error
1518
1488
  model: openAIParams.model,
1519
1489
  provider: 'azure',
@@ -1528,8 +1498,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1528
1498
  outputTokens: 0
1529
1499
  },
1530
1500
  isError: true,
1531
- error: JSON.stringify(error),
1532
- captureImmediate: posthogCaptureImmediate
1501
+ error: JSON.stringify(error)
1533
1502
  });
1534
1503
  throw error;
1535
1504
  });
@@ -1544,13 +1513,9 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1544
1513
  }
1545
1514
  create(body, options) {
1546
1515
  const {
1547
- posthogDistinctId,
1548
- posthogTraceId,
1549
- posthogPrivacyMode = false,
1550
- posthogCaptureImmediate,
1551
- ...openAIParams
1552
- } = body;
1553
- const traceId = posthogTraceId ?? v4();
1516
+ providerParams: openAIParams,
1517
+ posthogParams
1518
+ } = extractPosthogParams(body);
1554
1519
  const startTime = Date.now();
1555
1520
  const parentPromise = super.create(openAIParams, options);
1556
1521
  const wrappedPromise = parentPromise.then(async result => {
@@ -1558,11 +1523,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1558
1523
  await sendEventToPosthog({
1559
1524
  client: this.phClient,
1560
1525
  eventType: AIEvent.Embedding,
1561
- distinctId: posthogDistinctId,
1562
- traceId,
1526
+ ...posthogParams,
1563
1527
  model: openAIParams.model,
1564
1528
  provider: 'azure',
1565
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1529
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1566
1530
  output: null,
1567
1531
  // Embeddings don't have output content
1568
1532
  latency,
@@ -1571,8 +1535,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1571
1535
  httpStatus: 200,
1572
1536
  usage: {
1573
1537
  inputTokens: result.usage?.prompt_tokens ?? 0
1574
- },
1575
- captureImmediate: posthogCaptureImmediate
1538
+ }
1576
1539
  });
1577
1540
  return result;
1578
1541
  }, async error => {
@@ -1580,11 +1543,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1580
1543
  await sendEventToPosthog({
1581
1544
  client: this.phClient,
1582
1545
  eventType: AIEvent.Embedding,
1583
- distinctId: posthogDistinctId,
1584
- traceId,
1546
+ ...posthogParams,
1585
1547
  model: openAIParams.model,
1586
1548
  provider: 'azure',
1587
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1549
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1588
1550
  output: null,
1589
1551
  latency: 0,
1590
1552
  baseURL: this.baseURL,
@@ -1594,8 +1556,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1594
1556
  inputTokens: 0
1595
1557
  },
1596
1558
  isError: true,
1597
- error: JSON.stringify(error),
1598
- captureImmediate: posthogCaptureImmediate
1559
+ error: JSON.stringify(error)
1599
1560
  });
1600
1561
  throw error;
1601
1562
  });
@@ -2068,12 +2029,9 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2068
2029
  }
2069
2030
  create(body, options) {
2070
2031
  const {
2071
- posthogDistinctId,
2072
- posthogTraceId,
2073
- posthogCaptureImmediate,
2074
- ...anthropicParams
2075
- } = body;
2076
- const traceId = posthogTraceId ?? v4();
2032
+ providerParams: anthropicParams,
2033
+ posthogParams
2034
+ } = extractPosthogParams(body);
2077
2035
  const startTime = Date.now();
2078
2036
  const parentPromise = super.create(anthropicParams, options);
2079
2037
  if (anthropicParams.stream) {
@@ -2183,8 +2141,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2183
2141
  }];
2184
2142
  await sendEventToPosthog({
2185
2143
  client: this.phClient,
2186
- distinctId: posthogDistinctId,
2187
- traceId,
2144
+ ...posthogParams,
2188
2145
  model: anthropicParams.model,
2189
2146
  provider: 'anthropic',
2190
2147
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
@@ -2194,15 +2151,13 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2194
2151
  params: body,
2195
2152
  httpStatus: 200,
2196
2153
  usage,
2197
- tools: availableTools,
2198
- captureImmediate: posthogCaptureImmediate
2154
+ tools: availableTools
2199
2155
  });
2200
2156
  } catch (error) {
2201
2157
  // error handling
2202
2158
  await sendEventToPosthog({
2203
2159
  client: this.phClient,
2204
- distinctId: posthogDistinctId,
2205
- traceId,
2160
+ ...posthogParams,
2206
2161
  model: anthropicParams.model,
2207
2162
  provider: 'anthropic',
2208
2163
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2216,8 +2171,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2216
2171
  outputTokens: 0
2217
2172
  },
2218
2173
  isError: true,
2219
- error: JSON.stringify(error),
2220
- captureImmediate: posthogCaptureImmediate
2174
+ error: JSON.stringify(error)
2221
2175
  });
2222
2176
  }
2223
2177
  })();
@@ -2233,8 +2187,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2233
2187
  const availableTools = extractAvailableToolCalls('anthropic', anthropicParams);
2234
2188
  await sendEventToPosthog({
2235
2189
  client: this.phClient,
2236
- distinctId: posthogDistinctId,
2237
- traceId,
2190
+ ...posthogParams,
2238
2191
  model: anthropicParams.model,
2239
2192
  provider: 'anthropic',
2240
2193
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2249,16 +2202,14 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2249
2202
  cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0,
2250
2203
  cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0
2251
2204
  },
2252
- tools: availableTools,
2253
- captureImmediate: posthogCaptureImmediate
2205
+ tools: availableTools
2254
2206
  });
2255
2207
  }
2256
2208
  return result;
2257
2209
  }, async error => {
2258
2210
  await sendEventToPosthog({
2259
2211
  client: this.phClient,
2260
- distinctId: posthogDistinctId,
2261
- traceId,
2212
+ ...posthogParams,
2262
2213
  model: anthropicParams.model,
2263
2214
  provider: 'anthropic',
2264
2215
  input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
@@ -2272,8 +2223,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
2272
2223
  outputTokens: 0
2273
2224
  },
2274
2225
  isError: true,
2275
- error: JSON.stringify(error),
2276
- captureImmediate: posthogCaptureImmediate
2226
+ error: JSON.stringify(error)
2277
2227
  });
2278
2228
  throw error;
2279
2229
  });
@@ -2300,12 +2250,9 @@ class WrappedModels {
2300
2250
  }
2301
2251
  async generateContent(params) {
2302
2252
  const {
2303
- posthogDistinctId,
2304
- posthogTraceId,
2305
- posthogCaptureImmediate,
2306
- ...geminiParams
2307
- } = params;
2308
- const traceId = posthogTraceId ?? v4();
2253
+ providerParams: geminiParams,
2254
+ posthogParams
2255
+ } = extractPosthogParams(params);
2309
2256
  const startTime = Date.now();
2310
2257
  try {
2311
2258
  const response = await this.client.models.generateContent(geminiParams);
@@ -2314,8 +2261,7 @@ class WrappedModels {
2314
2261
  const metadata = response.usageMetadata;
2315
2262
  await sendEventToPosthog({
2316
2263
  client: this.phClient,
2317
- distinctId: posthogDistinctId,
2318
- traceId,
2264
+ ...posthogParams,
2319
2265
  model: geminiParams.model,
2320
2266
  provider: 'gemini',
2321
2267
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2330,16 +2276,14 @@ class WrappedModels {
2330
2276
  reasoningTokens: metadata?.thoughtsTokenCount ?? 0,
2331
2277
  cacheReadInputTokens: metadata?.cachedContentTokenCount ?? 0
2332
2278
  },
2333
- tools: availableTools,
2334
- captureImmediate: posthogCaptureImmediate
2279
+ tools: availableTools
2335
2280
  });
2336
2281
  return response;
2337
2282
  } catch (error) {
2338
2283
  const latency = (Date.now() - startTime) / 1000;
2339
2284
  await sendEventToPosthog({
2340
2285
  client: this.phClient,
2341
- distinctId: posthogDistinctId,
2342
- traceId,
2286
+ ...posthogParams,
2343
2287
  model: geminiParams.model,
2344
2288
  provider: 'gemini',
2345
2289
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2353,20 +2297,16 @@ class WrappedModels {
2353
2297
  outputTokens: 0
2354
2298
  },
2355
2299
  isError: true,
2356
- error: JSON.stringify(error),
2357
- captureImmediate: posthogCaptureImmediate
2300
+ error: JSON.stringify(error)
2358
2301
  });
2359
2302
  throw error;
2360
2303
  }
2361
2304
  }
2362
2305
  async *generateContentStream(params) {
2363
2306
  const {
2364
- posthogDistinctId,
2365
- posthogTraceId,
2366
- posthogCaptureImmediate,
2367
- ...geminiParams
2368
- } = params;
2369
- const traceId = posthogTraceId ?? v4();
2307
+ providerParams: geminiParams,
2308
+ posthogParams
2309
+ } = extractPosthogParams(params);
2370
2310
  const startTime = Date.now();
2371
2311
  const accumulatedContent = [];
2372
2312
  let usage = {
@@ -2438,8 +2378,7 @@ class WrappedModels {
2438
2378
  }] : [];
2439
2379
  await sendEventToPosthog({
2440
2380
  client: this.phClient,
2441
- distinctId: posthogDistinctId,
2442
- traceId,
2381
+ ...posthogParams,
2443
2382
  model: geminiParams.model,
2444
2383
  provider: 'gemini',
2445
2384
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2449,15 +2388,13 @@ class WrappedModels {
2449
2388
  params: params,
2450
2389
  httpStatus: 200,
2451
2390
  usage,
2452
- tools: availableTools,
2453
- captureImmediate: posthogCaptureImmediate
2391
+ tools: availableTools
2454
2392
  });
2455
2393
  } catch (error) {
2456
2394
  const latency = (Date.now() - startTime) / 1000;
2457
2395
  await sendEventToPosthog({
2458
2396
  client: this.phClient,
2459
- distinctId: posthogDistinctId,
2460
- traceId,
2397
+ ...posthogParams,
2461
2398
  model: geminiParams.model,
2462
2399
  provider: 'gemini',
2463
2400
  input: this.formatInputForPostHog(geminiParams.contents),
@@ -2471,8 +2408,7 @@ class WrappedModels {
2471
2408
  outputTokens: 0
2472
2409
  },
2473
2410
  isError: true,
2474
- error: JSON.stringify(error),
2475
- captureImmediate: posthogCaptureImmediate
2411
+ error: JSON.stringify(error)
2476
2412
  });
2477
2413
  throw error;
2478
2414
  }