@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.
- package/dist/anthropic/index.cjs +1 -1
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +14 -10
- package/dist/anthropic/index.mjs +1 -1
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +14 -10
- package/dist/gemini/index.mjs +1 -1
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +148 -161
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +15 -11
- package/dist/index.mjs +148 -161
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +76 -77
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +14 -10
- package/dist/openai/index.mjs +76 -77
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +37 -10
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +37 -10
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/openai/index.mjs
CHANGED
|
@@ -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.
|
|
5
|
+
var version = "6.3.2";
|
|
6
6
|
|
|
7
7
|
const STRING_FORMAT = 'utf8';
|
|
8
8
|
const getModelParams = params => {
|
|
@@ -361,6 +361,42 @@ const sanitizeOpenAIResponse = data => {
|
|
|
361
361
|
return processMessages(data, sanitizeOpenAIResponseImage);
|
|
362
362
|
};
|
|
363
363
|
|
|
364
|
+
const POSTHOG_PARAMS_MAP = {
|
|
365
|
+
posthogDistinctId: 'distinctId',
|
|
366
|
+
posthogTraceId: 'traceId',
|
|
367
|
+
posthogProperties: 'properties',
|
|
368
|
+
posthogPrivacyMode: 'privacyMode',
|
|
369
|
+
posthogGroups: 'groups',
|
|
370
|
+
posthogModelOverride: 'modelOverride',
|
|
371
|
+
posthogProviderOverride: 'providerOverride',
|
|
372
|
+
posthogCostOverride: 'costOverride',
|
|
373
|
+
posthogCaptureImmediate: 'captureImmediate'
|
|
374
|
+
};
|
|
375
|
+
function extractPosthogParams(body) {
|
|
376
|
+
const openAIParams = {};
|
|
377
|
+
const posthogParams = {};
|
|
378
|
+
for (const [key, value] of Object.entries(body)) {
|
|
379
|
+
if (POSTHOG_PARAMS_MAP[key]) {
|
|
380
|
+
posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
|
|
381
|
+
} else if (key.startsWith('posthog')) {
|
|
382
|
+
console.warn(`Unknown Posthog parameter ${key}`);
|
|
383
|
+
} else {
|
|
384
|
+
openAIParams[key] = value;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
openAIParams: openAIParams,
|
|
389
|
+
posthogParams: addDefaults(posthogParams)
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
function addDefaults(params) {
|
|
393
|
+
return {
|
|
394
|
+
...params,
|
|
395
|
+
privacyMode: params.privacyMode ?? false,
|
|
396
|
+
traceId: params.traceId ?? v4()
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
364
400
|
const Chat = OpenAI.Chat;
|
|
365
401
|
const Completions = Chat.Completions;
|
|
366
402
|
const Responses = OpenAI.Responses;
|
|
@@ -400,12 +436,9 @@ class WrappedCompletions extends Completions {
|
|
|
400
436
|
// --- Implementation Signature
|
|
401
437
|
create(body, options) {
|
|
402
438
|
const {
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
...openAIParams
|
|
407
|
-
} = body;
|
|
408
|
-
const traceId = posthogTraceId ?? v4();
|
|
439
|
+
openAIParams,
|
|
440
|
+
posthogParams
|
|
441
|
+
} = extractPosthogParams(body);
|
|
409
442
|
const startTime = Date.now();
|
|
410
443
|
const parentPromise = super.create(openAIParams, options);
|
|
411
444
|
if (openAIParams.stream) {
|
|
@@ -511,8 +544,7 @@ class WrappedCompletions extends Completions {
|
|
|
511
544
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
512
545
|
await sendEventToPosthog({
|
|
513
546
|
client: this.phClient,
|
|
514
|
-
|
|
515
|
-
traceId,
|
|
547
|
+
...posthogParams,
|
|
516
548
|
model: openAIParams.model,
|
|
517
549
|
provider: 'openai',
|
|
518
550
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -522,15 +554,13 @@ class WrappedCompletions extends Completions {
|
|
|
522
554
|
params: body,
|
|
523
555
|
httpStatus: 200,
|
|
524
556
|
usage,
|
|
525
|
-
tools: availableTools
|
|
526
|
-
captureImmediate: posthogCaptureImmediate
|
|
557
|
+
tools: availableTools
|
|
527
558
|
});
|
|
528
559
|
} catch (error) {
|
|
529
560
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
530
561
|
await sendEventToPosthog({
|
|
531
562
|
client: this.phClient,
|
|
532
|
-
|
|
533
|
-
traceId,
|
|
563
|
+
...posthogParams,
|
|
534
564
|
model: openAIParams.model,
|
|
535
565
|
provider: 'openai',
|
|
536
566
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -544,8 +574,7 @@ class WrappedCompletions extends Completions {
|
|
|
544
574
|
outputTokens: 0
|
|
545
575
|
},
|
|
546
576
|
isError: true,
|
|
547
|
-
error: JSON.stringify(error)
|
|
548
|
-
captureImmediate: posthogCaptureImmediate
|
|
577
|
+
error: JSON.stringify(error)
|
|
549
578
|
});
|
|
550
579
|
}
|
|
551
580
|
})();
|
|
@@ -562,8 +591,7 @@ class WrappedCompletions extends Completions {
|
|
|
562
591
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
563
592
|
await sendEventToPosthog({
|
|
564
593
|
client: this.phClient,
|
|
565
|
-
|
|
566
|
-
traceId,
|
|
594
|
+
...posthogParams,
|
|
567
595
|
model: openAIParams.model,
|
|
568
596
|
provider: 'openai',
|
|
569
597
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -578,8 +606,7 @@ class WrappedCompletions extends Completions {
|
|
|
578
606
|
reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
579
607
|
cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
580
608
|
},
|
|
581
|
-
tools: availableTools
|
|
582
|
-
captureImmediate: posthogCaptureImmediate
|
|
609
|
+
tools: availableTools
|
|
583
610
|
});
|
|
584
611
|
}
|
|
585
612
|
return result;
|
|
@@ -587,8 +614,7 @@ class WrappedCompletions extends Completions {
|
|
|
587
614
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
588
615
|
await sendEventToPosthog({
|
|
589
616
|
client: this.phClient,
|
|
590
|
-
|
|
591
|
-
traceId,
|
|
617
|
+
...posthogParams,
|
|
592
618
|
model: openAIParams.model,
|
|
593
619
|
provider: 'openai',
|
|
594
620
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -602,8 +628,7 @@ class WrappedCompletions extends Completions {
|
|
|
602
628
|
outputTokens: 0
|
|
603
629
|
},
|
|
604
630
|
isError: true,
|
|
605
|
-
error: JSON.stringify(error)
|
|
606
|
-
captureImmediate: posthogCaptureImmediate
|
|
631
|
+
error: JSON.stringify(error)
|
|
607
632
|
});
|
|
608
633
|
throw error;
|
|
609
634
|
});
|
|
@@ -627,12 +652,9 @@ class WrappedResponses extends Responses {
|
|
|
627
652
|
// --- Implementation Signature
|
|
628
653
|
create(body, options) {
|
|
629
654
|
const {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
...openAIParams
|
|
634
|
-
} = body;
|
|
635
|
-
const traceId = posthogTraceId ?? v4();
|
|
655
|
+
openAIParams,
|
|
656
|
+
posthogParams
|
|
657
|
+
} = extractPosthogParams(body);
|
|
636
658
|
const startTime = Date.now();
|
|
637
659
|
const parentPromise = super.create(openAIParams, options);
|
|
638
660
|
if (openAIParams.stream) {
|
|
@@ -663,8 +685,7 @@ class WrappedResponses extends Responses {
|
|
|
663
685
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
664
686
|
await sendEventToPosthog({
|
|
665
687
|
client: this.phClient,
|
|
666
|
-
|
|
667
|
-
traceId,
|
|
688
|
+
...posthogParams,
|
|
668
689
|
//@ts-expect-error
|
|
669
690
|
model: openAIParams.model,
|
|
670
691
|
provider: 'openai',
|
|
@@ -675,15 +696,13 @@ class WrappedResponses extends Responses {
|
|
|
675
696
|
params: body,
|
|
676
697
|
httpStatus: 200,
|
|
677
698
|
usage,
|
|
678
|
-
tools: availableTools
|
|
679
|
-
captureImmediate: posthogCaptureImmediate
|
|
699
|
+
tools: availableTools
|
|
680
700
|
});
|
|
681
701
|
} catch (error) {
|
|
682
702
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
683
703
|
await sendEventToPosthog({
|
|
684
704
|
client: this.phClient,
|
|
685
|
-
|
|
686
|
-
traceId,
|
|
705
|
+
...posthogParams,
|
|
687
706
|
//@ts-expect-error
|
|
688
707
|
model: openAIParams.model,
|
|
689
708
|
provider: 'openai',
|
|
@@ -698,8 +717,7 @@ class WrappedResponses extends Responses {
|
|
|
698
717
|
outputTokens: 0
|
|
699
718
|
},
|
|
700
719
|
isError: true,
|
|
701
|
-
error: JSON.stringify(error)
|
|
702
|
-
captureImmediate: posthogCaptureImmediate
|
|
720
|
+
error: JSON.stringify(error)
|
|
703
721
|
});
|
|
704
722
|
}
|
|
705
723
|
})();
|
|
@@ -714,8 +732,7 @@ class WrappedResponses extends Responses {
|
|
|
714
732
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
715
733
|
await sendEventToPosthog({
|
|
716
734
|
client: this.phClient,
|
|
717
|
-
|
|
718
|
-
traceId,
|
|
735
|
+
...posthogParams,
|
|
719
736
|
//@ts-expect-error
|
|
720
737
|
model: openAIParams.model,
|
|
721
738
|
provider: 'openai',
|
|
@@ -733,8 +750,7 @@ class WrappedResponses extends Responses {
|
|
|
733
750
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
734
751
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
735
752
|
},
|
|
736
|
-
tools: availableTools
|
|
737
|
-
captureImmediate: posthogCaptureImmediate
|
|
753
|
+
tools: availableTools
|
|
738
754
|
});
|
|
739
755
|
}
|
|
740
756
|
return result;
|
|
@@ -742,8 +758,7 @@ class WrappedResponses extends Responses {
|
|
|
742
758
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
743
759
|
await sendEventToPosthog({
|
|
744
760
|
client: this.phClient,
|
|
745
|
-
|
|
746
|
-
traceId,
|
|
761
|
+
...posthogParams,
|
|
747
762
|
//@ts-expect-error
|
|
748
763
|
model: openAIParams.model,
|
|
749
764
|
provider: 'openai',
|
|
@@ -758,8 +773,7 @@ class WrappedResponses extends Responses {
|
|
|
758
773
|
outputTokens: 0
|
|
759
774
|
},
|
|
760
775
|
isError: true,
|
|
761
|
-
error: JSON.stringify(error)
|
|
762
|
-
captureImmediate: posthogCaptureImmediate
|
|
776
|
+
error: JSON.stringify(error)
|
|
763
777
|
});
|
|
764
778
|
throw error;
|
|
765
779
|
});
|
|
@@ -768,12 +782,9 @@ class WrappedResponses extends Responses {
|
|
|
768
782
|
}
|
|
769
783
|
parse(body, options) {
|
|
770
784
|
const {
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
...openAIParams
|
|
775
|
-
} = body;
|
|
776
|
-
const traceId = posthogTraceId ?? v4();
|
|
785
|
+
openAIParams,
|
|
786
|
+
posthogParams
|
|
787
|
+
} = extractPosthogParams(body);
|
|
777
788
|
const startTime = Date.now();
|
|
778
789
|
|
|
779
790
|
// Create a temporary instance that bypasses our wrapped create method
|
|
@@ -787,8 +798,7 @@ class WrappedResponses extends Responses {
|
|
|
787
798
|
const latency = (Date.now() - startTime) / 1000;
|
|
788
799
|
await sendEventToPosthog({
|
|
789
800
|
client: this.phClient,
|
|
790
|
-
|
|
791
|
-
traceId,
|
|
801
|
+
...posthogParams,
|
|
792
802
|
//@ts-expect-error
|
|
793
803
|
model: openAIParams.model,
|
|
794
804
|
provider: 'openai',
|
|
@@ -803,16 +813,14 @@ class WrappedResponses extends Responses {
|
|
|
803
813
|
outputTokens: result.usage?.output_tokens ?? 0,
|
|
804
814
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
805
815
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
806
|
-
}
|
|
807
|
-
captureImmediate: posthogCaptureImmediate
|
|
816
|
+
}
|
|
808
817
|
});
|
|
809
818
|
return result;
|
|
810
819
|
}, async error => {
|
|
811
820
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
812
821
|
await sendEventToPosthog({
|
|
813
822
|
client: this.phClient,
|
|
814
|
-
|
|
815
|
-
traceId,
|
|
823
|
+
...posthogParams,
|
|
816
824
|
//@ts-expect-error
|
|
817
825
|
model: openAIParams.model,
|
|
818
826
|
provider: 'openai',
|
|
@@ -827,8 +835,7 @@ class WrappedResponses extends Responses {
|
|
|
827
835
|
outputTokens: 0
|
|
828
836
|
},
|
|
829
837
|
isError: true,
|
|
830
|
-
error: JSON.stringify(error)
|
|
831
|
-
captureImmediate: posthogCaptureImmediate
|
|
838
|
+
error: JSON.stringify(error)
|
|
832
839
|
});
|
|
833
840
|
throw error;
|
|
834
841
|
});
|
|
@@ -847,25 +854,20 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
847
854
|
}
|
|
848
855
|
create(body, options) {
|
|
849
856
|
const {
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
posthogCaptureImmediate,
|
|
854
|
-
...openAIParams
|
|
855
|
-
} = body;
|
|
856
|
-
const traceId = posthogTraceId ?? v4();
|
|
857
|
+
openAIParams,
|
|
858
|
+
posthogParams
|
|
859
|
+
} = extractPosthogParams(body);
|
|
857
860
|
const startTime = Date.now();
|
|
858
861
|
const parentPromise = super.create(openAIParams, options);
|
|
859
862
|
const wrappedPromise = parentPromise.then(async result => {
|
|
860
863
|
const latency = (Date.now() - startTime) / 1000;
|
|
861
864
|
await sendEventToPosthog({
|
|
862
865
|
client: this.phClient,
|
|
866
|
+
...posthogParams,
|
|
863
867
|
eventType: AIEvent.Embedding,
|
|
864
|
-
distinctId: posthogDistinctId,
|
|
865
|
-
traceId,
|
|
866
868
|
model: openAIParams.model,
|
|
867
869
|
provider: 'openai',
|
|
868
|
-
input: withPrivacyMode(this.phClient,
|
|
870
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
869
871
|
output: null,
|
|
870
872
|
// Embeddings don't have output content
|
|
871
873
|
latency,
|
|
@@ -874,8 +876,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
874
876
|
httpStatus: 200,
|
|
875
877
|
usage: {
|
|
876
878
|
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
877
|
-
}
|
|
878
|
-
captureImmediate: posthogCaptureImmediate
|
|
879
|
+
}
|
|
879
880
|
});
|
|
880
881
|
return result;
|
|
881
882
|
}, async error => {
|
|
@@ -883,11 +884,10 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
883
884
|
await sendEventToPosthog({
|
|
884
885
|
client: this.phClient,
|
|
885
886
|
eventType: AIEvent.Embedding,
|
|
886
|
-
|
|
887
|
-
traceId,
|
|
887
|
+
...posthogParams,
|
|
888
888
|
model: openAIParams.model,
|
|
889
889
|
provider: 'openai',
|
|
890
|
-
input: withPrivacyMode(this.phClient,
|
|
890
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
891
891
|
output: null,
|
|
892
892
|
// Embeddings don't have output content
|
|
893
893
|
latency: 0,
|
|
@@ -898,8 +898,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
898
898
|
inputTokens: 0
|
|
899
899
|
},
|
|
900
900
|
isError: true,
|
|
901
|
-
error: JSON.stringify(error)
|
|
902
|
-
captureImmediate: posthogCaptureImmediate
|
|
901
|
+
error: JSON.stringify(error)
|
|
903
902
|
});
|
|
904
903
|
throw error;
|
|
905
904
|
});
|