@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/anthropic/index.cjs +48 -24
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +14 -10
- package/dist/anthropic/index.mjs +48 -24
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +51 -30
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.d.ts +14 -10
- package/dist/gemini/index.mjs +51 -30
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +138 -202
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +15 -11
- package/dist/index.mjs +138 -202
- 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 +75 -77
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +14 -10
- package/dist/openai/index.mjs +75 -77
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/openai/index.cjs
CHANGED
|
@@ -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.
|
|
9
|
+
var version = "6.3.3";
|
|
10
10
|
|
|
11
11
|
const STRING_FORMAT = 'utf8';
|
|
12
12
|
const getModelParams = params => {
|
|
@@ -147,6 +147,41 @@ function sanitizeValues(obj) {
|
|
|
147
147
|
}
|
|
148
148
|
return jsonSafe;
|
|
149
149
|
}
|
|
150
|
+
const POSTHOG_PARAMS_MAP = {
|
|
151
|
+
posthogDistinctId: 'distinctId',
|
|
152
|
+
posthogTraceId: 'traceId',
|
|
153
|
+
posthogProperties: 'properties',
|
|
154
|
+
posthogPrivacyMode: 'privacyMode',
|
|
155
|
+
posthogGroups: 'groups',
|
|
156
|
+
posthogModelOverride: 'modelOverride',
|
|
157
|
+
posthogProviderOverride: 'providerOverride',
|
|
158
|
+
posthogCostOverride: 'costOverride',
|
|
159
|
+
posthogCaptureImmediate: 'captureImmediate'
|
|
160
|
+
};
|
|
161
|
+
function extractPosthogParams(body) {
|
|
162
|
+
const providerParams = {};
|
|
163
|
+
const posthogParams = {};
|
|
164
|
+
for (const [key, value] of Object.entries(body)) {
|
|
165
|
+
if (POSTHOG_PARAMS_MAP[key]) {
|
|
166
|
+
posthogParams[POSTHOG_PARAMS_MAP[key]] = value;
|
|
167
|
+
} else if (key.startsWith('posthog')) {
|
|
168
|
+
console.warn(`Unknown Posthog parameter ${key}`);
|
|
169
|
+
} else {
|
|
170
|
+
providerParams[key] = value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
providerParams: providerParams,
|
|
175
|
+
posthogParams: addDefaults(posthogParams)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function addDefaults(params) {
|
|
179
|
+
return {
|
|
180
|
+
...params,
|
|
181
|
+
privacyMode: params.privacyMode ?? false,
|
|
182
|
+
traceId: params.traceId ?? uuid.v4()
|
|
183
|
+
};
|
|
184
|
+
}
|
|
150
185
|
const sendEventToPosthog = async ({
|
|
151
186
|
client,
|
|
152
187
|
eventType = AIEvent.Generation,
|
|
@@ -404,12 +439,9 @@ class WrappedCompletions extends Completions {
|
|
|
404
439
|
// --- Implementation Signature
|
|
405
440
|
create(body, options) {
|
|
406
441
|
const {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
...openAIParams
|
|
411
|
-
} = body;
|
|
412
|
-
const traceId = posthogTraceId ?? uuid.v4();
|
|
442
|
+
providerParams: openAIParams,
|
|
443
|
+
posthogParams
|
|
444
|
+
} = extractPosthogParams(body);
|
|
413
445
|
const startTime = Date.now();
|
|
414
446
|
const parentPromise = super.create(openAIParams, options);
|
|
415
447
|
if (openAIParams.stream) {
|
|
@@ -515,8 +547,7 @@ class WrappedCompletions extends Completions {
|
|
|
515
547
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
516
548
|
await sendEventToPosthog({
|
|
517
549
|
client: this.phClient,
|
|
518
|
-
|
|
519
|
-
traceId,
|
|
550
|
+
...posthogParams,
|
|
520
551
|
model: openAIParams.model,
|
|
521
552
|
provider: 'openai',
|
|
522
553
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -526,15 +557,13 @@ class WrappedCompletions extends Completions {
|
|
|
526
557
|
params: body,
|
|
527
558
|
httpStatus: 200,
|
|
528
559
|
usage,
|
|
529
|
-
tools: availableTools
|
|
530
|
-
captureImmediate: posthogCaptureImmediate
|
|
560
|
+
tools: availableTools
|
|
531
561
|
});
|
|
532
562
|
} catch (error) {
|
|
533
563
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
534
564
|
await sendEventToPosthog({
|
|
535
565
|
client: this.phClient,
|
|
536
|
-
|
|
537
|
-
traceId,
|
|
566
|
+
...posthogParams,
|
|
538
567
|
model: openAIParams.model,
|
|
539
568
|
provider: 'openai',
|
|
540
569
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -548,8 +577,7 @@ class WrappedCompletions extends Completions {
|
|
|
548
577
|
outputTokens: 0
|
|
549
578
|
},
|
|
550
579
|
isError: true,
|
|
551
|
-
error: JSON.stringify(error)
|
|
552
|
-
captureImmediate: posthogCaptureImmediate
|
|
580
|
+
error: JSON.stringify(error)
|
|
553
581
|
});
|
|
554
582
|
}
|
|
555
583
|
})();
|
|
@@ -566,8 +594,7 @@ class WrappedCompletions extends Completions {
|
|
|
566
594
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
567
595
|
await sendEventToPosthog({
|
|
568
596
|
client: this.phClient,
|
|
569
|
-
|
|
570
|
-
traceId,
|
|
597
|
+
...posthogParams,
|
|
571
598
|
model: openAIParams.model,
|
|
572
599
|
provider: 'openai',
|
|
573
600
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -582,8 +609,7 @@ class WrappedCompletions extends Completions {
|
|
|
582
609
|
reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
583
610
|
cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
584
611
|
},
|
|
585
|
-
tools: availableTools
|
|
586
|
-
captureImmediate: posthogCaptureImmediate
|
|
612
|
+
tools: availableTools
|
|
587
613
|
});
|
|
588
614
|
}
|
|
589
615
|
return result;
|
|
@@ -591,8 +617,7 @@ class WrappedCompletions extends Completions {
|
|
|
591
617
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
592
618
|
await sendEventToPosthog({
|
|
593
619
|
client: this.phClient,
|
|
594
|
-
|
|
595
|
-
traceId,
|
|
620
|
+
...posthogParams,
|
|
596
621
|
model: openAIParams.model,
|
|
597
622
|
provider: 'openai',
|
|
598
623
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -606,8 +631,7 @@ class WrappedCompletions extends Completions {
|
|
|
606
631
|
outputTokens: 0
|
|
607
632
|
},
|
|
608
633
|
isError: true,
|
|
609
|
-
error: JSON.stringify(error)
|
|
610
|
-
captureImmediate: posthogCaptureImmediate
|
|
634
|
+
error: JSON.stringify(error)
|
|
611
635
|
});
|
|
612
636
|
throw error;
|
|
613
637
|
});
|
|
@@ -631,12 +655,9 @@ class WrappedResponses extends Responses {
|
|
|
631
655
|
// --- Implementation Signature
|
|
632
656
|
create(body, options) {
|
|
633
657
|
const {
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
...openAIParams
|
|
638
|
-
} = body;
|
|
639
|
-
const traceId = posthogTraceId ?? uuid.v4();
|
|
658
|
+
providerParams: openAIParams,
|
|
659
|
+
posthogParams
|
|
660
|
+
} = extractPosthogParams(body);
|
|
640
661
|
const startTime = Date.now();
|
|
641
662
|
const parentPromise = super.create(openAIParams, options);
|
|
642
663
|
if (openAIParams.stream) {
|
|
@@ -667,8 +688,7 @@ class WrappedResponses extends Responses {
|
|
|
667
688
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
668
689
|
await sendEventToPosthog({
|
|
669
690
|
client: this.phClient,
|
|
670
|
-
|
|
671
|
-
traceId,
|
|
691
|
+
...posthogParams,
|
|
672
692
|
//@ts-expect-error
|
|
673
693
|
model: openAIParams.model,
|
|
674
694
|
provider: 'openai',
|
|
@@ -679,15 +699,13 @@ class WrappedResponses extends Responses {
|
|
|
679
699
|
params: body,
|
|
680
700
|
httpStatus: 200,
|
|
681
701
|
usage,
|
|
682
|
-
tools: availableTools
|
|
683
|
-
captureImmediate: posthogCaptureImmediate
|
|
702
|
+
tools: availableTools
|
|
684
703
|
});
|
|
685
704
|
} catch (error) {
|
|
686
705
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
687
706
|
await sendEventToPosthog({
|
|
688
707
|
client: this.phClient,
|
|
689
|
-
|
|
690
|
-
traceId,
|
|
708
|
+
...posthogParams,
|
|
691
709
|
//@ts-expect-error
|
|
692
710
|
model: openAIParams.model,
|
|
693
711
|
provider: 'openai',
|
|
@@ -702,8 +720,7 @@ class WrappedResponses extends Responses {
|
|
|
702
720
|
outputTokens: 0
|
|
703
721
|
},
|
|
704
722
|
isError: true,
|
|
705
|
-
error: JSON.stringify(error)
|
|
706
|
-
captureImmediate: posthogCaptureImmediate
|
|
723
|
+
error: JSON.stringify(error)
|
|
707
724
|
});
|
|
708
725
|
}
|
|
709
726
|
})();
|
|
@@ -718,8 +735,7 @@ class WrappedResponses extends Responses {
|
|
|
718
735
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
719
736
|
await sendEventToPosthog({
|
|
720
737
|
client: this.phClient,
|
|
721
|
-
|
|
722
|
-
traceId,
|
|
738
|
+
...posthogParams,
|
|
723
739
|
//@ts-expect-error
|
|
724
740
|
model: openAIParams.model,
|
|
725
741
|
provider: 'openai',
|
|
@@ -737,8 +753,7 @@ class WrappedResponses extends Responses {
|
|
|
737
753
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
738
754
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
739
755
|
},
|
|
740
|
-
tools: availableTools
|
|
741
|
-
captureImmediate: posthogCaptureImmediate
|
|
756
|
+
tools: availableTools
|
|
742
757
|
});
|
|
743
758
|
}
|
|
744
759
|
return result;
|
|
@@ -746,8 +761,7 @@ class WrappedResponses extends Responses {
|
|
|
746
761
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
747
762
|
await sendEventToPosthog({
|
|
748
763
|
client: this.phClient,
|
|
749
|
-
|
|
750
|
-
traceId,
|
|
764
|
+
...posthogParams,
|
|
751
765
|
//@ts-expect-error
|
|
752
766
|
model: openAIParams.model,
|
|
753
767
|
provider: 'openai',
|
|
@@ -762,8 +776,7 @@ class WrappedResponses extends Responses {
|
|
|
762
776
|
outputTokens: 0
|
|
763
777
|
},
|
|
764
778
|
isError: true,
|
|
765
|
-
error: JSON.stringify(error)
|
|
766
|
-
captureImmediate: posthogCaptureImmediate
|
|
779
|
+
error: JSON.stringify(error)
|
|
767
780
|
});
|
|
768
781
|
throw error;
|
|
769
782
|
});
|
|
@@ -772,12 +785,9 @@ class WrappedResponses extends Responses {
|
|
|
772
785
|
}
|
|
773
786
|
parse(body, options) {
|
|
774
787
|
const {
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
...openAIParams
|
|
779
|
-
} = body;
|
|
780
|
-
const traceId = posthogTraceId ?? uuid.v4();
|
|
788
|
+
providerParams: openAIParams,
|
|
789
|
+
posthogParams
|
|
790
|
+
} = extractPosthogParams(body);
|
|
781
791
|
const startTime = Date.now();
|
|
782
792
|
|
|
783
793
|
// Create a temporary instance that bypasses our wrapped create method
|
|
@@ -791,8 +801,7 @@ class WrappedResponses extends Responses {
|
|
|
791
801
|
const latency = (Date.now() - startTime) / 1000;
|
|
792
802
|
await sendEventToPosthog({
|
|
793
803
|
client: this.phClient,
|
|
794
|
-
|
|
795
|
-
traceId,
|
|
804
|
+
...posthogParams,
|
|
796
805
|
//@ts-expect-error
|
|
797
806
|
model: openAIParams.model,
|
|
798
807
|
provider: 'openai',
|
|
@@ -807,16 +816,14 @@ class WrappedResponses extends Responses {
|
|
|
807
816
|
outputTokens: result.usage?.output_tokens ?? 0,
|
|
808
817
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
809
818
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
810
|
-
}
|
|
811
|
-
captureImmediate: posthogCaptureImmediate
|
|
819
|
+
}
|
|
812
820
|
});
|
|
813
821
|
return result;
|
|
814
822
|
}, async error => {
|
|
815
823
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
816
824
|
await sendEventToPosthog({
|
|
817
825
|
client: this.phClient,
|
|
818
|
-
|
|
819
|
-
traceId,
|
|
826
|
+
...posthogParams,
|
|
820
827
|
//@ts-expect-error
|
|
821
828
|
model: openAIParams.model,
|
|
822
829
|
provider: 'openai',
|
|
@@ -831,8 +838,7 @@ class WrappedResponses extends Responses {
|
|
|
831
838
|
outputTokens: 0
|
|
832
839
|
},
|
|
833
840
|
isError: true,
|
|
834
|
-
error: JSON.stringify(error)
|
|
835
|
-
captureImmediate: posthogCaptureImmediate
|
|
841
|
+
error: JSON.stringify(error)
|
|
836
842
|
});
|
|
837
843
|
throw error;
|
|
838
844
|
});
|
|
@@ -851,25 +857,20 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
851
857
|
}
|
|
852
858
|
create(body, options) {
|
|
853
859
|
const {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
posthogCaptureImmediate,
|
|
858
|
-
...openAIParams
|
|
859
|
-
} = body;
|
|
860
|
-
const traceId = posthogTraceId ?? uuid.v4();
|
|
860
|
+
providerParams: openAIParams,
|
|
861
|
+
posthogParams
|
|
862
|
+
} = extractPosthogParams(body);
|
|
861
863
|
const startTime = Date.now();
|
|
862
864
|
const parentPromise = super.create(openAIParams, options);
|
|
863
865
|
const wrappedPromise = parentPromise.then(async result => {
|
|
864
866
|
const latency = (Date.now() - startTime) / 1000;
|
|
865
867
|
await sendEventToPosthog({
|
|
866
868
|
client: this.phClient,
|
|
869
|
+
...posthogParams,
|
|
867
870
|
eventType: AIEvent.Embedding,
|
|
868
|
-
distinctId: posthogDistinctId,
|
|
869
|
-
traceId,
|
|
870
871
|
model: openAIParams.model,
|
|
871
872
|
provider: 'openai',
|
|
872
|
-
input: withPrivacyMode(this.phClient,
|
|
873
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
873
874
|
output: null,
|
|
874
875
|
// Embeddings don't have output content
|
|
875
876
|
latency,
|
|
@@ -878,8 +879,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
878
879
|
httpStatus: 200,
|
|
879
880
|
usage: {
|
|
880
881
|
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
881
|
-
}
|
|
882
|
-
captureImmediate: posthogCaptureImmediate
|
|
882
|
+
}
|
|
883
883
|
});
|
|
884
884
|
return result;
|
|
885
885
|
}, async error => {
|
|
@@ -887,11 +887,10 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
887
887
|
await sendEventToPosthog({
|
|
888
888
|
client: this.phClient,
|
|
889
889
|
eventType: AIEvent.Embedding,
|
|
890
|
-
|
|
891
|
-
traceId,
|
|
890
|
+
...posthogParams,
|
|
892
891
|
model: openAIParams.model,
|
|
893
892
|
provider: 'openai',
|
|
894
|
-
input: withPrivacyMode(this.phClient,
|
|
893
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
895
894
|
output: null,
|
|
896
895
|
// Embeddings don't have output content
|
|
897
896
|
latency: 0,
|
|
@@ -902,8 +901,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
902
901
|
inputTokens: 0
|
|
903
902
|
},
|
|
904
903
|
isError: true,
|
|
905
|
-
error: JSON.stringify(error)
|
|
906
|
-
captureImmediate: posthogCaptureImmediate
|
|
904
|
+
error: JSON.stringify(error)
|
|
907
905
|
});
|
|
908
906
|
throw error;
|
|
909
907
|
});
|