@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.
@@ -1,8 +1,8 @@
1
1
  import { OpenAI } from 'openai';
2
- import { v4 } from 'uuid';
3
2
  import { Buffer } from 'buffer';
3
+ import { v4 } from 'uuid';
4
4
 
5
- var version = "6.3.1";
5
+ var version = "6.3.3";
6
6
 
7
7
  const STRING_FORMAT = 'utf8';
8
8
  const getModelParams = params => {
@@ -143,6 +143,41 @@ function sanitizeValues(obj) {
143
143
  }
144
144
  return jsonSafe;
145
145
  }
146
+ const POSTHOG_PARAMS_MAP = {
147
+ posthogDistinctId: 'distinctId',
148
+ posthogTraceId: 'traceId',
149
+ posthogProperties: 'properties',
150
+ posthogPrivacyMode: 'privacyMode',
151
+ posthogGroups: 'groups',
152
+ posthogModelOverride: 'modelOverride',
153
+ posthogProviderOverride: 'providerOverride',
154
+ posthogCostOverride: 'costOverride',
155
+ posthogCaptureImmediate: 'captureImmediate'
156
+ };
157
+ function extractPosthogParams(body) {
158
+ const providerParams = {};
159
+ const posthogParams = {};
160
+ for (const [key, value] of Object.entries(body)) {
161
+ if (POSTHOG_PARAMS_MAP[key]) {
162
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
163
+ } else if (key.startsWith('posthog')) {
164
+ console.warn(`Unknown Posthog parameter ${key}`);
165
+ } else {
166
+ providerParams[key] = value;
167
+ }
168
+ }
169
+ return {
170
+ providerParams: providerParams,
171
+ posthogParams: addDefaults(posthogParams)
172
+ };
173
+ }
174
+ function addDefaults(params) {
175
+ return {
176
+ ...params,
177
+ privacyMode: params.privacyMode ?? false,
178
+ traceId: params.traceId ?? v4()
179
+ };
180
+ }
146
181
  const sendEventToPosthog = async ({
147
182
  client,
148
183
  eventType = AIEvent.Generation,
@@ -400,12 +435,9 @@ class WrappedCompletions extends Completions {
400
435
  // --- Implementation Signature
401
436
  create(body, options) {
402
437
  const {
403
- posthogDistinctId,
404
- posthogTraceId,
405
- posthogCaptureImmediate,
406
- ...openAIParams
407
- } = body;
408
- const traceId = posthogTraceId ?? v4();
438
+ providerParams: openAIParams,
439
+ posthogParams
440
+ } = extractPosthogParams(body);
409
441
  const startTime = Date.now();
410
442
  const parentPromise = super.create(openAIParams, options);
411
443
  if (openAIParams.stream) {
@@ -511,8 +543,7 @@ class WrappedCompletions extends Completions {
511
543
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
512
544
  await sendEventToPosthog({
513
545
  client: this.phClient,
514
- distinctId: posthogDistinctId,
515
- traceId,
546
+ ...posthogParams,
516
547
  model: openAIParams.model,
517
548
  provider: 'openai',
518
549
  input: sanitizeOpenAI(openAIParams.messages),
@@ -522,15 +553,13 @@ class WrappedCompletions extends Completions {
522
553
  params: body,
523
554
  httpStatus: 200,
524
555
  usage,
525
- tools: availableTools,
526
- captureImmediate: posthogCaptureImmediate
556
+ tools: availableTools
527
557
  });
528
558
  } catch (error) {
529
559
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
530
560
  await sendEventToPosthog({
531
561
  client: this.phClient,
532
- distinctId: posthogDistinctId,
533
- traceId,
562
+ ...posthogParams,
534
563
  model: openAIParams.model,
535
564
  provider: 'openai',
536
565
  input: sanitizeOpenAI(openAIParams.messages),
@@ -544,8 +573,7 @@ class WrappedCompletions extends Completions {
544
573
  outputTokens: 0
545
574
  },
546
575
  isError: true,
547
- error: JSON.stringify(error),
548
- captureImmediate: posthogCaptureImmediate
576
+ error: JSON.stringify(error)
549
577
  });
550
578
  }
551
579
  })();
@@ -562,8 +590,7 @@ class WrappedCompletions extends Completions {
562
590
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
563
591
  await sendEventToPosthog({
564
592
  client: this.phClient,
565
- distinctId: posthogDistinctId,
566
- traceId,
593
+ ...posthogParams,
567
594
  model: openAIParams.model,
568
595
  provider: 'openai',
569
596
  input: sanitizeOpenAI(openAIParams.messages),
@@ -578,8 +605,7 @@ class WrappedCompletions extends Completions {
578
605
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
579
606
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
580
607
  },
581
- tools: availableTools,
582
- captureImmediate: posthogCaptureImmediate
608
+ tools: availableTools
583
609
  });
584
610
  }
585
611
  return result;
@@ -587,8 +613,7 @@ class WrappedCompletions extends Completions {
587
613
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
588
614
  await sendEventToPosthog({
589
615
  client: this.phClient,
590
- distinctId: posthogDistinctId,
591
- traceId,
616
+ ...posthogParams,
592
617
  model: openAIParams.model,
593
618
  provider: 'openai',
594
619
  input: sanitizeOpenAI(openAIParams.messages),
@@ -602,8 +627,7 @@ class WrappedCompletions extends Completions {
602
627
  outputTokens: 0
603
628
  },
604
629
  isError: true,
605
- error: JSON.stringify(error),
606
- captureImmediate: posthogCaptureImmediate
630
+ error: JSON.stringify(error)
607
631
  });
608
632
  throw error;
609
633
  });
@@ -627,12 +651,9 @@ class WrappedResponses extends Responses {
627
651
  // --- Implementation Signature
628
652
  create(body, options) {
629
653
  const {
630
- posthogDistinctId,
631
- posthogTraceId,
632
- posthogCaptureImmediate,
633
- ...openAIParams
634
- } = body;
635
- const traceId = posthogTraceId ?? v4();
654
+ providerParams: openAIParams,
655
+ posthogParams
656
+ } = extractPosthogParams(body);
636
657
  const startTime = Date.now();
637
658
  const parentPromise = super.create(openAIParams, options);
638
659
  if (openAIParams.stream) {
@@ -663,8 +684,7 @@ class WrappedResponses extends Responses {
663
684
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
664
685
  await sendEventToPosthog({
665
686
  client: this.phClient,
666
- distinctId: posthogDistinctId,
667
- traceId,
687
+ ...posthogParams,
668
688
  //@ts-expect-error
669
689
  model: openAIParams.model,
670
690
  provider: 'openai',
@@ -675,15 +695,13 @@ class WrappedResponses extends Responses {
675
695
  params: body,
676
696
  httpStatus: 200,
677
697
  usage,
678
- tools: availableTools,
679
- captureImmediate: posthogCaptureImmediate
698
+ tools: availableTools
680
699
  });
681
700
  } catch (error) {
682
701
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
683
702
  await sendEventToPosthog({
684
703
  client: this.phClient,
685
- distinctId: posthogDistinctId,
686
- traceId,
704
+ ...posthogParams,
687
705
  //@ts-expect-error
688
706
  model: openAIParams.model,
689
707
  provider: 'openai',
@@ -698,8 +716,7 @@ class WrappedResponses extends Responses {
698
716
  outputTokens: 0
699
717
  },
700
718
  isError: true,
701
- error: JSON.stringify(error),
702
- captureImmediate: posthogCaptureImmediate
719
+ error: JSON.stringify(error)
703
720
  });
704
721
  }
705
722
  })();
@@ -714,8 +731,7 @@ class WrappedResponses extends Responses {
714
731
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
715
732
  await sendEventToPosthog({
716
733
  client: this.phClient,
717
- distinctId: posthogDistinctId,
718
- traceId,
734
+ ...posthogParams,
719
735
  //@ts-expect-error
720
736
  model: openAIParams.model,
721
737
  provider: 'openai',
@@ -733,8 +749,7 @@ class WrappedResponses extends Responses {
733
749
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
734
750
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
735
751
  },
736
- tools: availableTools,
737
- captureImmediate: posthogCaptureImmediate
752
+ tools: availableTools
738
753
  });
739
754
  }
740
755
  return result;
@@ -742,8 +757,7 @@ class WrappedResponses extends Responses {
742
757
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
743
758
  await sendEventToPosthog({
744
759
  client: this.phClient,
745
- distinctId: posthogDistinctId,
746
- traceId,
760
+ ...posthogParams,
747
761
  //@ts-expect-error
748
762
  model: openAIParams.model,
749
763
  provider: 'openai',
@@ -758,8 +772,7 @@ class WrappedResponses extends Responses {
758
772
  outputTokens: 0
759
773
  },
760
774
  isError: true,
761
- error: JSON.stringify(error),
762
- captureImmediate: posthogCaptureImmediate
775
+ error: JSON.stringify(error)
763
776
  });
764
777
  throw error;
765
778
  });
@@ -768,12 +781,9 @@ class WrappedResponses extends Responses {
768
781
  }
769
782
  parse(body, options) {
770
783
  const {
771
- posthogDistinctId,
772
- posthogTraceId,
773
- posthogCaptureImmediate,
774
- ...openAIParams
775
- } = body;
776
- const traceId = posthogTraceId ?? v4();
784
+ providerParams: openAIParams,
785
+ posthogParams
786
+ } = extractPosthogParams(body);
777
787
  const startTime = Date.now();
778
788
 
779
789
  // Create a temporary instance that bypasses our wrapped create method
@@ -787,8 +797,7 @@ class WrappedResponses extends Responses {
787
797
  const latency = (Date.now() - startTime) / 1000;
788
798
  await sendEventToPosthog({
789
799
  client: this.phClient,
790
- distinctId: posthogDistinctId,
791
- traceId,
800
+ ...posthogParams,
792
801
  //@ts-expect-error
793
802
  model: openAIParams.model,
794
803
  provider: 'openai',
@@ -803,16 +812,14 @@ class WrappedResponses extends Responses {
803
812
  outputTokens: result.usage?.output_tokens ?? 0,
804
813
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
805
814
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
806
- },
807
- captureImmediate: posthogCaptureImmediate
815
+ }
808
816
  });
809
817
  return result;
810
818
  }, async error => {
811
819
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
812
820
  await sendEventToPosthog({
813
821
  client: this.phClient,
814
- distinctId: posthogDistinctId,
815
- traceId,
822
+ ...posthogParams,
816
823
  //@ts-expect-error
817
824
  model: openAIParams.model,
818
825
  provider: 'openai',
@@ -827,8 +834,7 @@ class WrappedResponses extends Responses {
827
834
  outputTokens: 0
828
835
  },
829
836
  isError: true,
830
- error: JSON.stringify(error),
831
- captureImmediate: posthogCaptureImmediate
837
+ error: JSON.stringify(error)
832
838
  });
833
839
  throw error;
834
840
  });
@@ -847,25 +853,20 @@ class WrappedEmbeddings extends Embeddings {
847
853
  }
848
854
  create(body, options) {
849
855
  const {
850
- posthogDistinctId,
851
- posthogTraceId,
852
- posthogPrivacyMode = false,
853
- posthogCaptureImmediate,
854
- ...openAIParams
855
- } = body;
856
- const traceId = posthogTraceId ?? v4();
856
+ providerParams: openAIParams,
857
+ posthogParams
858
+ } = extractPosthogParams(body);
857
859
  const startTime = Date.now();
858
860
  const parentPromise = super.create(openAIParams, options);
859
861
  const wrappedPromise = parentPromise.then(async result => {
860
862
  const latency = (Date.now() - startTime) / 1000;
861
863
  await sendEventToPosthog({
862
864
  client: this.phClient,
865
+ ...posthogParams,
863
866
  eventType: AIEvent.Embedding,
864
- distinctId: posthogDistinctId,
865
- traceId,
866
867
  model: openAIParams.model,
867
868
  provider: 'openai',
868
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
869
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
869
870
  output: null,
870
871
  // Embeddings don't have output content
871
872
  latency,
@@ -874,8 +875,7 @@ class WrappedEmbeddings extends Embeddings {
874
875
  httpStatus: 200,
875
876
  usage: {
876
877
  inputTokens: result.usage?.prompt_tokens ?? 0
877
- },
878
- captureImmediate: posthogCaptureImmediate
878
+ }
879
879
  });
880
880
  return result;
881
881
  }, async error => {
@@ -883,11 +883,10 @@ class WrappedEmbeddings extends Embeddings {
883
883
  await sendEventToPosthog({
884
884
  client: this.phClient,
885
885
  eventType: AIEvent.Embedding,
886
- distinctId: posthogDistinctId,
887
- traceId,
886
+ ...posthogParams,
888
887
  model: openAIParams.model,
889
888
  provider: 'openai',
890
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
889
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
891
890
  output: null,
892
891
  // Embeddings don't have output content
893
892
  latency: 0,
@@ -898,8 +897,7 @@ class WrappedEmbeddings extends Embeddings {
898
897
  inputTokens: 0
899
898
  },
900
899
  isError: true,
901
- error: JSON.stringify(error),
902
- captureImmediate: posthogCaptureImmediate
900
+ error: JSON.stringify(error)
903
901
  });
904
902
  throw error;
905
903
  });