@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.d.ts CHANGED
@@ -15,17 +15,21 @@ import { AgentAction, AgentFinish } from '@langchain/core/agents';
15
15
  import { DocumentInterface } from '@langchain/core/documents';
16
16
  import { BaseMessage } from '@langchain/core/messages';
17
17
 
18
- interface MonitoringParams {
19
- posthogDistinctId?: string;
20
- posthogTraceId?: string;
21
- posthogProperties?: Record<string, any>;
22
- posthogPrivacyMode?: boolean;
23
- posthogGroups?: Record<string, any>;
24
- posthogModelOverride?: string;
25
- posthogProviderOverride?: string;
26
- posthogCostOverride?: CostOverride;
27
- posthogCaptureImmediate?: boolean;
28
- }
18
+ interface MonitoringEventPropertiesWithDefaults {
19
+ distinctId?: string;
20
+ traceId: string;
21
+ properties?: Record<string, any>;
22
+ privacyMode: boolean;
23
+ groups?: Record<string, any>;
24
+ modelOverride?: string;
25
+ providerOverride?: string;
26
+ costOverride?: CostOverride;
27
+ captureImmediate?: boolean;
28
+ }
29
+ type MonitoringEventProperties = Partial<MonitoringEventPropertiesWithDefaults>;
30
+ type MonitoringParams = {
31
+ [K in keyof MonitoringEventProperties as `posthog${Capitalize<string & K>}`]: MonitoringEventProperties[K];
32
+ };
29
33
  interface CostOverride {
30
34
  inputCost: number;
31
35
  outputCost: number;
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.2";
10
10
 
11
11
  // limit large outputs by truncating to 200kb (approx 200k bytes)
12
12
  const MAX_OUTPUT_SIZE = 200000;
@@ -584,6 +584,42 @@ const sanitizeLangChain = data => {
584
584
  return processMessages(data, sanitizeLangChainImage);
585
585
  };
586
586
 
587
+ const POSTHOG_PARAMS_MAP = {
588
+ posthogDistinctId: 'distinctId',
589
+ posthogTraceId: 'traceId',
590
+ posthogProperties: 'properties',
591
+ posthogPrivacyMode: 'privacyMode',
592
+ posthogGroups: 'groups',
593
+ posthogModelOverride: 'modelOverride',
594
+ posthogProviderOverride: 'providerOverride',
595
+ posthogCostOverride: 'costOverride',
596
+ posthogCaptureImmediate: 'captureImmediate'
597
+ };
598
+ function extractPosthogParams(body) {
599
+ const openAIParams = {};
600
+ const posthogParams = {};
601
+ for (const [key, value] of Object.entries(body)) {
602
+ if (POSTHOG_PARAMS_MAP[key]) {
603
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
604
+ } else if (key.startsWith('posthog')) {
605
+ console.warn(`Unknown Posthog parameter ${key}`);
606
+ } else {
607
+ openAIParams[key] = value;
608
+ }
609
+ }
610
+ return {
611
+ openAIParams: openAIParams,
612
+ posthogParams: addDefaults(posthogParams)
613
+ };
614
+ }
615
+ function addDefaults(params) {
616
+ return {
617
+ ...params,
618
+ privacyMode: params.privacyMode ?? false,
619
+ traceId: params.traceId ?? v4()
620
+ };
621
+ }
622
+
587
623
  const Chat = OpenAI.Chat;
588
624
  const Completions = Chat.Completions;
589
625
  const Responses = OpenAI.Responses;
@@ -616,12 +652,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
616
652
  // --- Implementation Signature
617
653
  create(body, options) {
618
654
  const {
619
- posthogDistinctId,
620
- posthogTraceId,
621
- posthogCaptureImmediate,
622
- ...openAIParams
623
- } = body;
624
- const traceId = posthogTraceId ?? v4();
655
+ openAIParams,
656
+ posthogParams
657
+ } = extractPosthogParams(body);
625
658
  const startTime = Date.now();
626
659
  const parentPromise = super.create(openAIParams, options);
627
660
  if (openAIParams.stream) {
@@ -720,8 +753,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
720
753
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
721
754
  await sendEventToPosthog({
722
755
  client: this.phClient,
723
- distinctId: posthogDistinctId,
724
- traceId,
756
+ ...posthogParams,
725
757
  model: openAIParams.model,
726
758
  provider: 'openai',
727
759
  input: sanitizeOpenAI(openAIParams.messages),
@@ -731,15 +763,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
731
763
  params: body,
732
764
  httpStatus: 200,
733
765
  usage,
734
- tools: availableTools,
735
- captureImmediate: posthogCaptureImmediate
766
+ tools: availableTools
736
767
  });
737
768
  } catch (error) {
738
769
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
739
770
  await sendEventToPosthog({
740
771
  client: this.phClient,
741
- distinctId: posthogDistinctId,
742
- traceId,
772
+ ...posthogParams,
743
773
  model: openAIParams.model,
744
774
  provider: 'openai',
745
775
  input: sanitizeOpenAI(openAIParams.messages),
@@ -753,8 +783,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
753
783
  outputTokens: 0
754
784
  },
755
785
  isError: true,
756
- error: JSON.stringify(error),
757
- captureImmediate: posthogCaptureImmediate
786
+ error: JSON.stringify(error)
758
787
  });
759
788
  }
760
789
  })();
@@ -770,8 +799,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
770
799
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
771
800
  await sendEventToPosthog({
772
801
  client: this.phClient,
773
- distinctId: posthogDistinctId,
774
- traceId,
802
+ ...posthogParams,
775
803
  model: openAIParams.model,
776
804
  provider: 'openai',
777
805
  input: sanitizeOpenAI(openAIParams.messages),
@@ -786,8 +814,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
786
814
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
787
815
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
788
816
  },
789
- tools: availableTools,
790
- captureImmediate: posthogCaptureImmediate
817
+ tools: availableTools
791
818
  });
792
819
  }
793
820
  return result;
@@ -795,8 +822,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
795
822
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
796
823
  await sendEventToPosthog({
797
824
  client: this.phClient,
798
- distinctId: posthogDistinctId,
799
- traceId,
825
+ ...posthogParams,
800
826
  model: openAIParams.model,
801
827
  provider: 'openai',
802
828
  input: sanitizeOpenAI(openAIParams.messages),
@@ -810,8 +836,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
810
836
  outputTokens: 0
811
837
  },
812
838
  isError: true,
813
- error: JSON.stringify(error),
814
- captureImmediate: posthogCaptureImmediate
839
+ error: JSON.stringify(error)
815
840
  });
816
841
  throw error;
817
842
  });
@@ -828,12 +853,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
828
853
  // --- Implementation Signature
829
854
  create(body, options) {
830
855
  const {
831
- posthogDistinctId,
832
- posthogTraceId,
833
- posthogCaptureImmediate,
834
- ...openAIParams
835
- } = body;
836
- const traceId = posthogTraceId ?? v4();
856
+ openAIParams,
857
+ posthogParams
858
+ } = extractPosthogParams(body);
837
859
  const startTime = Date.now();
838
860
  const parentPromise = super.create(openAIParams, options);
839
861
  if (openAIParams.stream) {
@@ -864,8 +886,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
864
886
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
865
887
  await sendEventToPosthog({
866
888
  client: this.phClient,
867
- distinctId: posthogDistinctId,
868
- traceId,
889
+ ...posthogParams,
869
890
  //@ts-expect-error
870
891
  model: openAIParams.model,
871
892
  provider: 'openai',
@@ -876,15 +897,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
876
897
  params: body,
877
898
  httpStatus: 200,
878
899
  usage,
879
- tools: availableTools,
880
- captureImmediate: posthogCaptureImmediate
900
+ tools: availableTools
881
901
  });
882
902
  } catch (error) {
883
903
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
884
904
  await sendEventToPosthog({
885
905
  client: this.phClient,
886
- distinctId: posthogDistinctId,
887
- traceId,
906
+ ...posthogParams,
888
907
  //@ts-expect-error
889
908
  model: openAIParams.model,
890
909
  provider: 'openai',
@@ -899,8 +918,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
899
918
  outputTokens: 0
900
919
  },
901
920
  isError: true,
902
- error: JSON.stringify(error),
903
- captureImmediate: posthogCaptureImmediate
921
+ error: JSON.stringify(error)
904
922
  });
905
923
  }
906
924
  })();
@@ -915,8 +933,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
915
933
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
916
934
  await sendEventToPosthog({
917
935
  client: this.phClient,
918
- distinctId: posthogDistinctId,
919
- traceId,
936
+ ...posthogParams,
920
937
  //@ts-expect-error
921
938
  model: openAIParams.model,
922
939
  provider: 'openai',
@@ -934,8 +951,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
934
951
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
935
952
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
936
953
  },
937
- tools: availableTools,
938
- captureImmediate: posthogCaptureImmediate
954
+ tools: availableTools
939
955
  });
940
956
  }
941
957
  return result;
@@ -943,8 +959,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
943
959
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
944
960
  await sendEventToPosthog({
945
961
  client: this.phClient,
946
- distinctId: posthogDistinctId,
947
- traceId,
962
+ ...posthogParams,
948
963
  //@ts-expect-error
949
964
  model: openAIParams.model,
950
965
  provider: 'openai',
@@ -959,8 +974,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
959
974
  outputTokens: 0
960
975
  },
961
976
  isError: true,
962
- error: JSON.stringify(error),
963
- captureImmediate: posthogCaptureImmediate
977
+ error: JSON.stringify(error)
964
978
  });
965
979
  throw error;
966
980
  });
@@ -969,12 +983,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
969
983
  }
970
984
  parse(body, options) {
971
985
  const {
972
- posthogDistinctId,
973
- posthogTraceId,
974
- posthogCaptureImmediate,
975
- ...openAIParams
976
- } = body;
977
- const traceId = posthogTraceId ?? v4();
986
+ openAIParams,
987
+ posthogParams
988
+ } = extractPosthogParams(body);
978
989
  const startTime = Date.now();
979
990
  // Create a temporary instance that bypasses our wrapped create method
980
991
  const originalCreate = super.create.bind(this);
@@ -987,8 +998,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
987
998
  const latency = (Date.now() - startTime) / 1000;
988
999
  await sendEventToPosthog({
989
1000
  client: this.phClient,
990
- distinctId: posthogDistinctId,
991
- traceId,
1001
+ ...posthogParams,
992
1002
  //@ts-expect-error
993
1003
  model: openAIParams.model,
994
1004
  provider: 'openai',
@@ -1003,16 +1013,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1003
1013
  outputTokens: result.usage?.output_tokens ?? 0,
1004
1014
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1005
1015
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1006
- },
1007
- captureImmediate: posthogCaptureImmediate
1016
+ }
1008
1017
  });
1009
1018
  return result;
1010
1019
  }, async error => {
1011
1020
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1012
1021
  await sendEventToPosthog({
1013
1022
  client: this.phClient,
1014
- distinctId: posthogDistinctId,
1015
- traceId,
1023
+ ...posthogParams,
1016
1024
  //@ts-expect-error
1017
1025
  model: openAIParams.model,
1018
1026
  provider: 'openai',
@@ -1027,8 +1035,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
1027
1035
  outputTokens: 0
1028
1036
  },
1029
1037
  isError: true,
1030
- error: JSON.stringify(error),
1031
- captureImmediate: posthogCaptureImmediate
1038
+ error: JSON.stringify(error)
1032
1039
  });
1033
1040
  throw error;
1034
1041
  });
@@ -1047,25 +1054,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1047
1054
  }
1048
1055
  create(body, options) {
1049
1056
  const {
1050
- posthogDistinctId,
1051
- posthogTraceId,
1052
- posthogPrivacyMode = false,
1053
- posthogCaptureImmediate,
1054
- ...openAIParams
1055
- } = body;
1056
- const traceId = posthogTraceId ?? v4();
1057
+ openAIParams,
1058
+ posthogParams
1059
+ } = extractPosthogParams(body);
1057
1060
  const startTime = Date.now();
1058
1061
  const parentPromise = super.create(openAIParams, options);
1059
1062
  const wrappedPromise = parentPromise.then(async result => {
1060
1063
  const latency = (Date.now() - startTime) / 1000;
1061
1064
  await sendEventToPosthog({
1062
1065
  client: this.phClient,
1066
+ ...posthogParams,
1063
1067
  eventType: AIEvent.Embedding,
1064
- distinctId: posthogDistinctId,
1065
- traceId,
1066
1068
  model: openAIParams.model,
1067
1069
  provider: 'openai',
1068
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1070
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1069
1071
  output: null,
1070
1072
  // Embeddings don't have output content
1071
1073
  latency,
@@ -1074,8 +1076,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1074
1076
  httpStatus: 200,
1075
1077
  usage: {
1076
1078
  inputTokens: result.usage?.prompt_tokens ?? 0
1077
- },
1078
- captureImmediate: posthogCaptureImmediate
1079
+ }
1079
1080
  });
1080
1081
  return result;
1081
1082
  }, async error => {
@@ -1083,11 +1084,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1083
1084
  await sendEventToPosthog({
1084
1085
  client: this.phClient,
1085
1086
  eventType: AIEvent.Embedding,
1086
- distinctId: posthogDistinctId,
1087
- traceId,
1087
+ ...posthogParams,
1088
1088
  model: openAIParams.model,
1089
1089
  provider: 'openai',
1090
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1090
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1091
1091
  output: null,
1092
1092
  // Embeddings don't have output content
1093
1093
  latency: 0,
@@ -1098,8 +1098,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
1098
1098
  inputTokens: 0
1099
1099
  },
1100
1100
  isError: true,
1101
- error: JSON.stringify(error),
1102
- captureImmediate: posthogCaptureImmediate
1101
+ error: JSON.stringify(error)
1103
1102
  });
1104
1103
  throw error;
1105
1104
  });
@@ -1134,12 +1133,9 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1134
1133
  // --- Implementation Signature
1135
1134
  create(body, options) {
1136
1135
  const {
1137
- posthogDistinctId,
1138
- posthogTraceId,
1139
- posthogCaptureImmediate,
1140
- ...openAIParams
1141
- } = body;
1142
- const traceId = posthogTraceId ?? v4();
1136
+ openAIParams,
1137
+ posthogParams
1138
+ } = extractPosthogParams(body);
1143
1139
  const startTime = Date.now();
1144
1140
  const parentPromise = super.create(openAIParams, options);
1145
1141
  if (openAIParams.stream) {
@@ -1237,8 +1233,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1237
1233
  const latency = (Date.now() - startTime) / 1000;
1238
1234
  await sendEventToPosthog({
1239
1235
  client: this.phClient,
1240
- distinctId: posthogDistinctId,
1241
- traceId,
1236
+ ...posthogParams,
1242
1237
  model: openAIParams.model,
1243
1238
  provider: 'azure',
1244
1239
  input: openAIParams.messages,
@@ -1247,15 +1242,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1247
1242
  baseURL: this.baseURL,
1248
1243
  params: body,
1249
1244
  httpStatus: 200,
1250
- usage,
1251
- captureImmediate: posthogCaptureImmediate
1245
+ usage
1252
1246
  });
1253
1247
  } catch (error) {
1254
1248
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1255
1249
  await sendEventToPosthog({
1256
1250
  client: this.phClient,
1257
- distinctId: posthogDistinctId,
1258
- traceId,
1251
+ ...posthogParams,
1259
1252
  model: openAIParams.model,
1260
1253
  provider: 'azure',
1261
1254
  input: openAIParams.messages,
@@ -1269,8 +1262,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1269
1262
  outputTokens: 0
1270
1263
  },
1271
1264
  isError: true,
1272
- error: JSON.stringify(error),
1273
- captureImmediate: posthogCaptureImmediate
1265
+ error: JSON.stringify(error)
1274
1266
  });
1275
1267
  }
1276
1268
  })();
@@ -1285,8 +1277,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1285
1277
  const latency = (Date.now() - startTime) / 1000;
1286
1278
  await sendEventToPosthog({
1287
1279
  client: this.phClient,
1288
- distinctId: posthogDistinctId,
1289
- traceId,
1280
+ ...posthogParams,
1290
1281
  model: openAIParams.model,
1291
1282
  provider: 'azure',
1292
1283
  input: openAIParams.messages,
@@ -1300,8 +1291,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1300
1291
  outputTokens: result.usage?.completion_tokens ?? 0,
1301
1292
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
1302
1293
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
1303
- },
1304
- captureImmediate: posthogCaptureImmediate
1294
+ }
1305
1295
  });
1306
1296
  }
1307
1297
  return result;
@@ -1309,8 +1299,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1309
1299
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1310
1300
  await sendEventToPosthog({
1311
1301
  client: this.phClient,
1312
- distinctId: posthogDistinctId,
1313
- traceId,
1302
+ ...posthogParams,
1314
1303
  model: openAIParams.model,
1315
1304
  provider: 'azure',
1316
1305
  input: openAIParams.messages,
@@ -1324,8 +1313,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
1324
1313
  outputTokens: 0
1325
1314
  },
1326
1315
  isError: true,
1327
- error: JSON.stringify(error),
1328
- captureImmediate: posthogCaptureImmediate
1316
+ error: JSON.stringify(error)
1329
1317
  });
1330
1318
  throw error;
1331
1319
  });
@@ -1342,12 +1330,9 @@ class WrappedResponses extends AzureOpenAI.Responses {
1342
1330
  // --- Implementation Signature
1343
1331
  create(body, options) {
1344
1332
  const {
1345
- posthogDistinctId,
1346
- posthogTraceId,
1347
- posthogCaptureImmediate,
1348
- ...openAIParams
1349
- } = body;
1350
- const traceId = posthogTraceId ?? v4();
1333
+ openAIParams,
1334
+ posthogParams
1335
+ } = extractPosthogParams(body);
1351
1336
  const startTime = Date.now();
1352
1337
  const parentPromise = super.create(openAIParams, options);
1353
1338
  if (openAIParams.stream) {
@@ -1377,8 +1362,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1377
1362
  const latency = (Date.now() - startTime) / 1000;
1378
1363
  await sendEventToPosthog({
1379
1364
  client: this.phClient,
1380
- distinctId: posthogDistinctId,
1381
- traceId,
1365
+ ...posthogParams,
1382
1366
  //@ts-expect-error
1383
1367
  model: openAIParams.model,
1384
1368
  provider: 'azure',
@@ -1388,15 +1372,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
1388
1372
  baseURL: this.baseURL,
1389
1373
  params: body,
1390
1374
  httpStatus: 200,
1391
- usage,
1392
- captureImmediate: posthogCaptureImmediate
1375
+ usage
1393
1376
  });
1394
1377
  } catch (error) {
1395
1378
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1396
1379
  await sendEventToPosthog({
1397
1380
  client: this.phClient,
1398
- distinctId: posthogDistinctId,
1399
- traceId,
1381
+ ...posthogParams,
1400
1382
  //@ts-expect-error
1401
1383
  model: openAIParams.model,
1402
1384
  provider: 'azure',
@@ -1411,8 +1393,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1411
1393
  outputTokens: 0
1412
1394
  },
1413
1395
  isError: true,
1414
- error: JSON.stringify(error),
1415
- captureImmediate: posthogCaptureImmediate
1396
+ error: JSON.stringify(error)
1416
1397
  });
1417
1398
  }
1418
1399
  })();
@@ -1426,8 +1407,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1426
1407
  const latency = (Date.now() - startTime) / 1000;
1427
1408
  await sendEventToPosthog({
1428
1409
  client: this.phClient,
1429
- distinctId: posthogDistinctId,
1430
- traceId,
1410
+ ...posthogParams,
1431
1411
  //@ts-expect-error
1432
1412
  model: openAIParams.model,
1433
1413
  provider: 'azure',
@@ -1442,8 +1422,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1442
1422
  outputTokens: result.usage?.output_tokens ?? 0,
1443
1423
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1444
1424
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1445
- },
1446
- captureImmediate: posthogCaptureImmediate
1425
+ }
1447
1426
  });
1448
1427
  }
1449
1428
  return result;
@@ -1451,8 +1430,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1451
1430
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1452
1431
  await sendEventToPosthog({
1453
1432
  client: this.phClient,
1454
- distinctId: posthogDistinctId,
1455
- traceId,
1433
+ ...posthogParams,
1456
1434
  //@ts-expect-error
1457
1435
  model: openAIParams.model,
1458
1436
  provider: 'azure',
@@ -1467,8 +1445,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1467
1445
  outputTokens: 0
1468
1446
  },
1469
1447
  isError: true,
1470
- error: JSON.stringify(error),
1471
- captureImmediate: posthogCaptureImmediate
1448
+ error: JSON.stringify(error)
1472
1449
  });
1473
1450
  throw error;
1474
1451
  });
@@ -1477,20 +1454,16 @@ class WrappedResponses extends AzureOpenAI.Responses {
1477
1454
  }
1478
1455
  parse(body, options) {
1479
1456
  const {
1480
- posthogDistinctId,
1481
- posthogTraceId,
1482
- posthogCaptureImmediate,
1483
- ...openAIParams
1484
- } = body;
1485
- const traceId = posthogTraceId ?? v4();
1457
+ openAIParams,
1458
+ posthogParams
1459
+ } = extractPosthogParams(body);
1486
1460
  const startTime = Date.now();
1487
1461
  const parentPromise = super.parse(openAIParams, options);
1488
1462
  const wrappedPromise = parentPromise.then(async result => {
1489
1463
  const latency = (Date.now() - startTime) / 1000;
1490
1464
  await sendEventToPosthog({
1491
1465
  client: this.phClient,
1492
- distinctId: posthogDistinctId,
1493
- traceId,
1466
+ ...posthogParams,
1494
1467
  //@ts-expect-error
1495
1468
  model: openAIParams.model,
1496
1469
  provider: 'azure',
@@ -1505,15 +1478,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
1505
1478
  outputTokens: result.usage?.output_tokens ?? 0,
1506
1479
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1507
1480
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
1508
- },
1509
- captureImmediate: posthogCaptureImmediate
1481
+ }
1510
1482
  });
1511
1483
  return result;
1512
1484
  }, async error => {
1513
1485
  await sendEventToPosthog({
1514
1486
  client: this.phClient,
1515
- distinctId: posthogDistinctId,
1516
- traceId,
1487
+ ...posthogParams,
1517
1488
  //@ts-expect-error
1518
1489
  model: openAIParams.model,
1519
1490
  provider: 'azure',
@@ -1528,8 +1499,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
1528
1499
  outputTokens: 0
1529
1500
  },
1530
1501
  isError: true,
1531
- error: JSON.stringify(error),
1532
- captureImmediate: posthogCaptureImmediate
1502
+ error: JSON.stringify(error)
1533
1503
  });
1534
1504
  throw error;
1535
1505
  });
@@ -1544,13 +1514,9 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1544
1514
  }
1545
1515
  create(body, options) {
1546
1516
  const {
1547
- posthogDistinctId,
1548
- posthogTraceId,
1549
- posthogPrivacyMode = false,
1550
- posthogCaptureImmediate,
1551
- ...openAIParams
1552
- } = body;
1553
- const traceId = posthogTraceId ?? v4();
1517
+ openAIParams,
1518
+ posthogParams
1519
+ } = extractPosthogParams(body);
1554
1520
  const startTime = Date.now();
1555
1521
  const parentPromise = super.create(openAIParams, options);
1556
1522
  const wrappedPromise = parentPromise.then(async result => {
@@ -1558,11 +1524,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1558
1524
  await sendEventToPosthog({
1559
1525
  client: this.phClient,
1560
1526
  eventType: AIEvent.Embedding,
1561
- distinctId: posthogDistinctId,
1562
- traceId,
1527
+ ...posthogParams,
1563
1528
  model: openAIParams.model,
1564
1529
  provider: 'azure',
1565
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1530
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1566
1531
  output: null,
1567
1532
  // Embeddings don't have output content
1568
1533
  latency,
@@ -1571,8 +1536,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1571
1536
  httpStatus: 200,
1572
1537
  usage: {
1573
1538
  inputTokens: result.usage?.prompt_tokens ?? 0
1574
- },
1575
- captureImmediate: posthogCaptureImmediate
1539
+ }
1576
1540
  });
1577
1541
  return result;
1578
1542
  }, async error => {
@@ -1580,11 +1544,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1580
1544
  await sendEventToPosthog({
1581
1545
  client: this.phClient,
1582
1546
  eventType: AIEvent.Embedding,
1583
- distinctId: posthogDistinctId,
1584
- traceId,
1547
+ ...posthogParams,
1585
1548
  model: openAIParams.model,
1586
1549
  provider: 'azure',
1587
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
1550
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
1588
1551
  output: null,
1589
1552
  latency: 0,
1590
1553
  baseURL: this.baseURL,
@@ -1594,8 +1557,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
1594
1557
  inputTokens: 0
1595
1558
  },
1596
1559
  isError: true,
1597
- error: JSON.stringify(error),
1598
- captureImmediate: posthogCaptureImmediate
1560
+ error: JSON.stringify(error)
1599
1561
  });
1600
1562
  throw error;
1601
1563
  });