@posthog/ai 6.3.0 → 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.
@@ -3,10 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var openai = require('openai');
6
- var uuid = require('uuid');
7
6
  var buffer = require('buffer');
7
+ var uuid = require('uuid');
8
8
 
9
- var version = "6.3.0";
9
+ var version = "6.3.2";
10
10
 
11
11
  const STRING_FORMAT = 'utf8';
12
12
  const getModelParams = params => {
@@ -365,6 +365,42 @@ const sanitizeOpenAIResponse = data => {
365
365
  return processMessages(data, sanitizeOpenAIResponseImage);
366
366
  };
367
367
 
368
+ const POSTHOG_PARAMS_MAP = {
369
+ posthogDistinctId: 'distinctId',
370
+ posthogTraceId: 'traceId',
371
+ posthogProperties: 'properties',
372
+ posthogPrivacyMode: 'privacyMode',
373
+ posthogGroups: 'groups',
374
+ posthogModelOverride: 'modelOverride',
375
+ posthogProviderOverride: 'providerOverride',
376
+ posthogCostOverride: 'costOverride',
377
+ posthogCaptureImmediate: 'captureImmediate'
378
+ };
379
+ function extractPosthogParams(body) {
380
+ const openAIParams = {};
381
+ const posthogParams = {};
382
+ for (const [key, value] of Object.entries(body)) {
383
+ if (POSTHOG_PARAMS_MAP[key]) {
384
+ posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
385
+ } else if (key.startsWith('posthog')) {
386
+ console.warn(`Unknown Posthog parameter ${key}`);
387
+ } else {
388
+ openAIParams[key] = value;
389
+ }
390
+ }
391
+ return {
392
+ openAIParams: openAIParams,
393
+ posthogParams: addDefaults(posthogParams)
394
+ };
395
+ }
396
+ function addDefaults(params) {
397
+ return {
398
+ ...params,
399
+ privacyMode: params.privacyMode ?? false,
400
+ traceId: params.traceId ?? uuid.v4()
401
+ };
402
+ }
403
+
368
404
  const Chat = openai.OpenAI.Chat;
369
405
  const Completions = Chat.Completions;
370
406
  const Responses = openai.OpenAI.Responses;
@@ -404,12 +440,9 @@ class WrappedCompletions extends Completions {
404
440
  // --- Implementation Signature
405
441
  create(body, options) {
406
442
  const {
407
- posthogDistinctId,
408
- posthogTraceId,
409
- posthogCaptureImmediate,
410
- ...openAIParams
411
- } = body;
412
- const traceId = posthogTraceId ?? uuid.v4();
443
+ openAIParams,
444
+ posthogParams
445
+ } = extractPosthogParams(body);
413
446
  const startTime = Date.now();
414
447
  const parentPromise = super.create(openAIParams, options);
415
448
  if (openAIParams.stream) {
@@ -515,8 +548,7 @@ class WrappedCompletions extends Completions {
515
548
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
516
549
  await sendEventToPosthog({
517
550
  client: this.phClient,
518
- distinctId: posthogDistinctId,
519
- traceId,
551
+ ...posthogParams,
520
552
  model: openAIParams.model,
521
553
  provider: 'openai',
522
554
  input: sanitizeOpenAI(openAIParams.messages),
@@ -526,15 +558,13 @@ class WrappedCompletions extends Completions {
526
558
  params: body,
527
559
  httpStatus: 200,
528
560
  usage,
529
- tools: availableTools,
530
- captureImmediate: posthogCaptureImmediate
561
+ tools: availableTools
531
562
  });
532
563
  } catch (error) {
533
564
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
534
565
  await sendEventToPosthog({
535
566
  client: this.phClient,
536
- distinctId: posthogDistinctId,
537
- traceId,
567
+ ...posthogParams,
538
568
  model: openAIParams.model,
539
569
  provider: 'openai',
540
570
  input: sanitizeOpenAI(openAIParams.messages),
@@ -548,8 +578,7 @@ class WrappedCompletions extends Completions {
548
578
  outputTokens: 0
549
579
  },
550
580
  isError: true,
551
- error: JSON.stringify(error),
552
- captureImmediate: posthogCaptureImmediate
581
+ error: JSON.stringify(error)
553
582
  });
554
583
  }
555
584
  })();
@@ -566,8 +595,7 @@ class WrappedCompletions extends Completions {
566
595
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
567
596
  await sendEventToPosthog({
568
597
  client: this.phClient,
569
- distinctId: posthogDistinctId,
570
- traceId,
598
+ ...posthogParams,
571
599
  model: openAIParams.model,
572
600
  provider: 'openai',
573
601
  input: sanitizeOpenAI(openAIParams.messages),
@@ -582,8 +610,7 @@ class WrappedCompletions extends Completions {
582
610
  reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
583
611
  cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
584
612
  },
585
- tools: availableTools,
586
- captureImmediate: posthogCaptureImmediate
613
+ tools: availableTools
587
614
  });
588
615
  }
589
616
  return result;
@@ -591,8 +618,7 @@ class WrappedCompletions extends Completions {
591
618
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
592
619
  await sendEventToPosthog({
593
620
  client: this.phClient,
594
- distinctId: posthogDistinctId,
595
- traceId,
621
+ ...posthogParams,
596
622
  model: openAIParams.model,
597
623
  provider: 'openai',
598
624
  input: sanitizeOpenAI(openAIParams.messages),
@@ -606,8 +632,7 @@ class WrappedCompletions extends Completions {
606
632
  outputTokens: 0
607
633
  },
608
634
  isError: true,
609
- error: JSON.stringify(error),
610
- captureImmediate: posthogCaptureImmediate
635
+ error: JSON.stringify(error)
611
636
  });
612
637
  throw error;
613
638
  });
@@ -631,12 +656,9 @@ class WrappedResponses extends Responses {
631
656
  // --- Implementation Signature
632
657
  create(body, options) {
633
658
  const {
634
- posthogDistinctId,
635
- posthogTraceId,
636
- posthogCaptureImmediate,
637
- ...openAIParams
638
- } = body;
639
- const traceId = posthogTraceId ?? uuid.v4();
659
+ openAIParams,
660
+ posthogParams
661
+ } = extractPosthogParams(body);
640
662
  const startTime = Date.now();
641
663
  const parentPromise = super.create(openAIParams, options);
642
664
  if (openAIParams.stream) {
@@ -667,8 +689,7 @@ class WrappedResponses extends Responses {
667
689
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
668
690
  await sendEventToPosthog({
669
691
  client: this.phClient,
670
- distinctId: posthogDistinctId,
671
- traceId,
692
+ ...posthogParams,
672
693
  //@ts-expect-error
673
694
  model: openAIParams.model,
674
695
  provider: 'openai',
@@ -679,15 +700,13 @@ class WrappedResponses extends Responses {
679
700
  params: body,
680
701
  httpStatus: 200,
681
702
  usage,
682
- tools: availableTools,
683
- captureImmediate: posthogCaptureImmediate
703
+ tools: availableTools
684
704
  });
685
705
  } catch (error) {
686
706
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
687
707
  await sendEventToPosthog({
688
708
  client: this.phClient,
689
- distinctId: posthogDistinctId,
690
- traceId,
709
+ ...posthogParams,
691
710
  //@ts-expect-error
692
711
  model: openAIParams.model,
693
712
  provider: 'openai',
@@ -702,8 +721,7 @@ class WrappedResponses extends Responses {
702
721
  outputTokens: 0
703
722
  },
704
723
  isError: true,
705
- error: JSON.stringify(error),
706
- captureImmediate: posthogCaptureImmediate
724
+ error: JSON.stringify(error)
707
725
  });
708
726
  }
709
727
  })();
@@ -718,8 +736,7 @@ class WrappedResponses extends Responses {
718
736
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
719
737
  await sendEventToPosthog({
720
738
  client: this.phClient,
721
- distinctId: posthogDistinctId,
722
- traceId,
739
+ ...posthogParams,
723
740
  //@ts-expect-error
724
741
  model: openAIParams.model,
725
742
  provider: 'openai',
@@ -737,8 +754,7 @@ class WrappedResponses extends Responses {
737
754
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
738
755
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
739
756
  },
740
- tools: availableTools,
741
- captureImmediate: posthogCaptureImmediate
757
+ tools: availableTools
742
758
  });
743
759
  }
744
760
  return result;
@@ -746,8 +762,7 @@ class WrappedResponses extends Responses {
746
762
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
747
763
  await sendEventToPosthog({
748
764
  client: this.phClient,
749
- distinctId: posthogDistinctId,
750
- traceId,
765
+ ...posthogParams,
751
766
  //@ts-expect-error
752
767
  model: openAIParams.model,
753
768
  provider: 'openai',
@@ -762,8 +777,7 @@ class WrappedResponses extends Responses {
762
777
  outputTokens: 0
763
778
  },
764
779
  isError: true,
765
- error: JSON.stringify(error),
766
- captureImmediate: posthogCaptureImmediate
780
+ error: JSON.stringify(error)
767
781
  });
768
782
  throw error;
769
783
  });
@@ -772,12 +786,9 @@ class WrappedResponses extends Responses {
772
786
  }
773
787
  parse(body, options) {
774
788
  const {
775
- posthogDistinctId,
776
- posthogTraceId,
777
- posthogCaptureImmediate,
778
- ...openAIParams
779
- } = body;
780
- const traceId = posthogTraceId ?? uuid.v4();
789
+ openAIParams,
790
+ posthogParams
791
+ } = extractPosthogParams(body);
781
792
  const startTime = Date.now();
782
793
 
783
794
  // Create a temporary instance that bypasses our wrapped create method
@@ -791,8 +802,7 @@ class WrappedResponses extends Responses {
791
802
  const latency = (Date.now() - startTime) / 1000;
792
803
  await sendEventToPosthog({
793
804
  client: this.phClient,
794
- distinctId: posthogDistinctId,
795
- traceId,
805
+ ...posthogParams,
796
806
  //@ts-expect-error
797
807
  model: openAIParams.model,
798
808
  provider: 'openai',
@@ -807,16 +817,14 @@ class WrappedResponses extends Responses {
807
817
  outputTokens: result.usage?.output_tokens ?? 0,
808
818
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
809
819
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
810
- },
811
- captureImmediate: posthogCaptureImmediate
820
+ }
812
821
  });
813
822
  return result;
814
823
  }, async error => {
815
824
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
816
825
  await sendEventToPosthog({
817
826
  client: this.phClient,
818
- distinctId: posthogDistinctId,
819
- traceId,
827
+ ...posthogParams,
820
828
  //@ts-expect-error
821
829
  model: openAIParams.model,
822
830
  provider: 'openai',
@@ -831,8 +839,7 @@ class WrappedResponses extends Responses {
831
839
  outputTokens: 0
832
840
  },
833
841
  isError: true,
834
- error: JSON.stringify(error),
835
- captureImmediate: posthogCaptureImmediate
842
+ error: JSON.stringify(error)
836
843
  });
837
844
  throw error;
838
845
  });
@@ -851,25 +858,20 @@ class WrappedEmbeddings extends Embeddings {
851
858
  }
852
859
  create(body, options) {
853
860
  const {
854
- posthogDistinctId,
855
- posthogTraceId,
856
- posthogPrivacyMode = false,
857
- posthogCaptureImmediate,
858
- ...openAIParams
859
- } = body;
860
- const traceId = posthogTraceId ?? uuid.v4();
861
+ openAIParams,
862
+ posthogParams
863
+ } = extractPosthogParams(body);
861
864
  const startTime = Date.now();
862
865
  const parentPromise = super.create(openAIParams, options);
863
866
  const wrappedPromise = parentPromise.then(async result => {
864
867
  const latency = (Date.now() - startTime) / 1000;
865
868
  await sendEventToPosthog({
866
869
  client: this.phClient,
870
+ ...posthogParams,
867
871
  eventType: AIEvent.Embedding,
868
- distinctId: posthogDistinctId,
869
- traceId,
870
872
  model: openAIParams.model,
871
873
  provider: 'openai',
872
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
874
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
873
875
  output: null,
874
876
  // Embeddings don't have output content
875
877
  latency,
@@ -878,8 +880,7 @@ class WrappedEmbeddings extends Embeddings {
878
880
  httpStatus: 200,
879
881
  usage: {
880
882
  inputTokens: result.usage?.prompt_tokens ?? 0
881
- },
882
- captureImmediate: posthogCaptureImmediate
883
+ }
883
884
  });
884
885
  return result;
885
886
  }, async error => {
@@ -887,11 +888,10 @@ class WrappedEmbeddings extends Embeddings {
887
888
  await sendEventToPosthog({
888
889
  client: this.phClient,
889
890
  eventType: AIEvent.Embedding,
890
- distinctId: posthogDistinctId,
891
- traceId,
891
+ ...posthogParams,
892
892
  model: openAIParams.model,
893
893
  provider: 'openai',
894
- input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
894
+ input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
895
895
  output: null,
896
896
  // Embeddings don't have output content
897
897
  latency: 0,
@@ -902,8 +902,7 @@ class WrappedEmbeddings extends Embeddings {
902
902
  inputTokens: 0
903
903
  },
904
904
  isError: true,
905
- error: JSON.stringify(error),
906
- captureImmediate: posthogCaptureImmediate
905
+ error: JSON.stringify(error)
907
906
  });
908
907
  throw error;
909
908
  });