@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/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.
|
|
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;
|
|
@@ -206,18 +206,43 @@ const mergeSystemPrompt = (params, provider) => {
|
|
|
206
206
|
const withPrivacyMode = (client, privacyMode, input) => {
|
|
207
207
|
return client.privacy_mode || privacyMode ? null : input;
|
|
208
208
|
};
|
|
209
|
-
|
|
209
|
+
function toSafeString(input) {
|
|
210
|
+
if (input === undefined || input === null) {
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
if (typeof input === 'string') {
|
|
214
|
+
return input;
|
|
215
|
+
}
|
|
210
216
|
try {
|
|
211
|
-
|
|
212
|
-
if (buffer.length <= MAX_OUTPUT_SIZE) {
|
|
213
|
-
return str;
|
|
214
|
-
}
|
|
215
|
-
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
216
|
-
return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
|
|
217
|
+
return JSON.stringify(input);
|
|
217
218
|
} catch {
|
|
218
|
-
console.
|
|
219
|
-
return
|
|
219
|
+
console.warn('Failed to stringify input', input);
|
|
220
|
+
return '';
|
|
220
221
|
}
|
|
222
|
+
}
|
|
223
|
+
const truncate = input => {
|
|
224
|
+
const str = toSafeString(input);
|
|
225
|
+
if (str === '') {
|
|
226
|
+
return '';
|
|
227
|
+
}
|
|
228
|
+
// Check if we need to truncate and ensure STRING_FORMAT is respected
|
|
229
|
+
const encoder = new TextEncoder();
|
|
230
|
+
const buffer = encoder.encode(str);
|
|
231
|
+
if (buffer.length <= MAX_OUTPUT_SIZE) {
|
|
232
|
+
// Ensure STRING_FORMAT is respected
|
|
233
|
+
return new TextDecoder(STRING_FORMAT).decode(buffer);
|
|
234
|
+
}
|
|
235
|
+
// Truncate the buffer and ensure a valid string is returned
|
|
236
|
+
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
237
|
+
// fatal: false means we get U+FFFD at the end if truncation broke the encoding
|
|
238
|
+
const decoder = new TextDecoder(STRING_FORMAT, {
|
|
239
|
+
fatal: false
|
|
240
|
+
});
|
|
241
|
+
let truncatedStr = decoder.decode(truncatedBuffer);
|
|
242
|
+
if (truncatedStr.endsWith('\uFFFD')) {
|
|
243
|
+
truncatedStr = truncatedStr.slice(0, -1);
|
|
244
|
+
}
|
|
245
|
+
return `${truncatedStr}... [truncated]`;
|
|
221
246
|
};
|
|
222
247
|
/**
|
|
223
248
|
* Extract available tool calls from the request parameters.
|
|
@@ -559,6 +584,42 @@ const sanitizeLangChain = data => {
|
|
|
559
584
|
return processMessages(data, sanitizeLangChainImage);
|
|
560
585
|
};
|
|
561
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
|
+
|
|
562
623
|
const Chat = OpenAI.Chat;
|
|
563
624
|
const Completions = Chat.Completions;
|
|
564
625
|
const Responses = OpenAI.Responses;
|
|
@@ -591,12 +652,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
591
652
|
// --- Implementation Signature
|
|
592
653
|
create(body, options) {
|
|
593
654
|
const {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
...openAIParams
|
|
598
|
-
} = body;
|
|
599
|
-
const traceId = posthogTraceId ?? v4();
|
|
655
|
+
openAIParams,
|
|
656
|
+
posthogParams
|
|
657
|
+
} = extractPosthogParams(body);
|
|
600
658
|
const startTime = Date.now();
|
|
601
659
|
const parentPromise = super.create(openAIParams, options);
|
|
602
660
|
if (openAIParams.stream) {
|
|
@@ -695,8 +753,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
695
753
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
696
754
|
await sendEventToPosthog({
|
|
697
755
|
client: this.phClient,
|
|
698
|
-
|
|
699
|
-
traceId,
|
|
756
|
+
...posthogParams,
|
|
700
757
|
model: openAIParams.model,
|
|
701
758
|
provider: 'openai',
|
|
702
759
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -706,15 +763,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
706
763
|
params: body,
|
|
707
764
|
httpStatus: 200,
|
|
708
765
|
usage,
|
|
709
|
-
tools: availableTools
|
|
710
|
-
captureImmediate: posthogCaptureImmediate
|
|
766
|
+
tools: availableTools
|
|
711
767
|
});
|
|
712
768
|
} catch (error) {
|
|
713
769
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
714
770
|
await sendEventToPosthog({
|
|
715
771
|
client: this.phClient,
|
|
716
|
-
|
|
717
|
-
traceId,
|
|
772
|
+
...posthogParams,
|
|
718
773
|
model: openAIParams.model,
|
|
719
774
|
provider: 'openai',
|
|
720
775
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -728,8 +783,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
728
783
|
outputTokens: 0
|
|
729
784
|
},
|
|
730
785
|
isError: true,
|
|
731
|
-
error: JSON.stringify(error)
|
|
732
|
-
captureImmediate: posthogCaptureImmediate
|
|
786
|
+
error: JSON.stringify(error)
|
|
733
787
|
});
|
|
734
788
|
}
|
|
735
789
|
})();
|
|
@@ -745,8 +799,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
745
799
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
746
800
|
await sendEventToPosthog({
|
|
747
801
|
client: this.phClient,
|
|
748
|
-
|
|
749
|
-
traceId,
|
|
802
|
+
...posthogParams,
|
|
750
803
|
model: openAIParams.model,
|
|
751
804
|
provider: 'openai',
|
|
752
805
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -761,8 +814,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
761
814
|
reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
762
815
|
cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
763
816
|
},
|
|
764
|
-
tools: availableTools
|
|
765
|
-
captureImmediate: posthogCaptureImmediate
|
|
817
|
+
tools: availableTools
|
|
766
818
|
});
|
|
767
819
|
}
|
|
768
820
|
return result;
|
|
@@ -770,8 +822,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
770
822
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
771
823
|
await sendEventToPosthog({
|
|
772
824
|
client: this.phClient,
|
|
773
|
-
|
|
774
|
-
traceId,
|
|
825
|
+
...posthogParams,
|
|
775
826
|
model: openAIParams.model,
|
|
776
827
|
provider: 'openai',
|
|
777
828
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
@@ -785,8 +836,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
785
836
|
outputTokens: 0
|
|
786
837
|
},
|
|
787
838
|
isError: true,
|
|
788
|
-
error: JSON.stringify(error)
|
|
789
|
-
captureImmediate: posthogCaptureImmediate
|
|
839
|
+
error: JSON.stringify(error)
|
|
790
840
|
});
|
|
791
841
|
throw error;
|
|
792
842
|
});
|
|
@@ -803,12 +853,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
803
853
|
// --- Implementation Signature
|
|
804
854
|
create(body, options) {
|
|
805
855
|
const {
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
...openAIParams
|
|
810
|
-
} = body;
|
|
811
|
-
const traceId = posthogTraceId ?? v4();
|
|
856
|
+
openAIParams,
|
|
857
|
+
posthogParams
|
|
858
|
+
} = extractPosthogParams(body);
|
|
812
859
|
const startTime = Date.now();
|
|
813
860
|
const parentPromise = super.create(openAIParams, options);
|
|
814
861
|
if (openAIParams.stream) {
|
|
@@ -839,8 +886,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
839
886
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
840
887
|
await sendEventToPosthog({
|
|
841
888
|
client: this.phClient,
|
|
842
|
-
|
|
843
|
-
traceId,
|
|
889
|
+
...posthogParams,
|
|
844
890
|
//@ts-expect-error
|
|
845
891
|
model: openAIParams.model,
|
|
846
892
|
provider: 'openai',
|
|
@@ -851,15 +897,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
851
897
|
params: body,
|
|
852
898
|
httpStatus: 200,
|
|
853
899
|
usage,
|
|
854
|
-
tools: availableTools
|
|
855
|
-
captureImmediate: posthogCaptureImmediate
|
|
900
|
+
tools: availableTools
|
|
856
901
|
});
|
|
857
902
|
} catch (error) {
|
|
858
903
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
859
904
|
await sendEventToPosthog({
|
|
860
905
|
client: this.phClient,
|
|
861
|
-
|
|
862
|
-
traceId,
|
|
906
|
+
...posthogParams,
|
|
863
907
|
//@ts-expect-error
|
|
864
908
|
model: openAIParams.model,
|
|
865
909
|
provider: 'openai',
|
|
@@ -874,8 +918,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
874
918
|
outputTokens: 0
|
|
875
919
|
},
|
|
876
920
|
isError: true,
|
|
877
|
-
error: JSON.stringify(error)
|
|
878
|
-
captureImmediate: posthogCaptureImmediate
|
|
921
|
+
error: JSON.stringify(error)
|
|
879
922
|
});
|
|
880
923
|
}
|
|
881
924
|
})();
|
|
@@ -890,8 +933,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
890
933
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
891
934
|
await sendEventToPosthog({
|
|
892
935
|
client: this.phClient,
|
|
893
|
-
|
|
894
|
-
traceId,
|
|
936
|
+
...posthogParams,
|
|
895
937
|
//@ts-expect-error
|
|
896
938
|
model: openAIParams.model,
|
|
897
939
|
provider: 'openai',
|
|
@@ -909,8 +951,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
909
951
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
910
952
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
911
953
|
},
|
|
912
|
-
tools: availableTools
|
|
913
|
-
captureImmediate: posthogCaptureImmediate
|
|
954
|
+
tools: availableTools
|
|
914
955
|
});
|
|
915
956
|
}
|
|
916
957
|
return result;
|
|
@@ -918,8 +959,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
918
959
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
919
960
|
await sendEventToPosthog({
|
|
920
961
|
client: this.phClient,
|
|
921
|
-
|
|
922
|
-
traceId,
|
|
962
|
+
...posthogParams,
|
|
923
963
|
//@ts-expect-error
|
|
924
964
|
model: openAIParams.model,
|
|
925
965
|
provider: 'openai',
|
|
@@ -934,8 +974,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
934
974
|
outputTokens: 0
|
|
935
975
|
},
|
|
936
976
|
isError: true,
|
|
937
|
-
error: JSON.stringify(error)
|
|
938
|
-
captureImmediate: posthogCaptureImmediate
|
|
977
|
+
error: JSON.stringify(error)
|
|
939
978
|
});
|
|
940
979
|
throw error;
|
|
941
980
|
});
|
|
@@ -944,12 +983,9 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
944
983
|
}
|
|
945
984
|
parse(body, options) {
|
|
946
985
|
const {
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
...openAIParams
|
|
951
|
-
} = body;
|
|
952
|
-
const traceId = posthogTraceId ?? v4();
|
|
986
|
+
openAIParams,
|
|
987
|
+
posthogParams
|
|
988
|
+
} = extractPosthogParams(body);
|
|
953
989
|
const startTime = Date.now();
|
|
954
990
|
// Create a temporary instance that bypasses our wrapped create method
|
|
955
991
|
const originalCreate = super.create.bind(this);
|
|
@@ -962,8 +998,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
962
998
|
const latency = (Date.now() - startTime) / 1000;
|
|
963
999
|
await sendEventToPosthog({
|
|
964
1000
|
client: this.phClient,
|
|
965
|
-
|
|
966
|
-
traceId,
|
|
1001
|
+
...posthogParams,
|
|
967
1002
|
//@ts-expect-error
|
|
968
1003
|
model: openAIParams.model,
|
|
969
1004
|
provider: 'openai',
|
|
@@ -978,16 +1013,14 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
978
1013
|
outputTokens: result.usage?.output_tokens ?? 0,
|
|
979
1014
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
980
1015
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
981
|
-
}
|
|
982
|
-
captureImmediate: posthogCaptureImmediate
|
|
1016
|
+
}
|
|
983
1017
|
});
|
|
984
1018
|
return result;
|
|
985
1019
|
}, async error => {
|
|
986
1020
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
987
1021
|
await sendEventToPosthog({
|
|
988
1022
|
client: this.phClient,
|
|
989
|
-
|
|
990
|
-
traceId,
|
|
1023
|
+
...posthogParams,
|
|
991
1024
|
//@ts-expect-error
|
|
992
1025
|
model: openAIParams.model,
|
|
993
1026
|
provider: 'openai',
|
|
@@ -1002,8 +1035,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1002
1035
|
outputTokens: 0
|
|
1003
1036
|
},
|
|
1004
1037
|
isError: true,
|
|
1005
|
-
error: JSON.stringify(error)
|
|
1006
|
-
captureImmediate: posthogCaptureImmediate
|
|
1038
|
+
error: JSON.stringify(error)
|
|
1007
1039
|
});
|
|
1008
1040
|
throw error;
|
|
1009
1041
|
});
|
|
@@ -1022,25 +1054,20 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1022
1054
|
}
|
|
1023
1055
|
create(body, options) {
|
|
1024
1056
|
const {
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
posthogCaptureImmediate,
|
|
1029
|
-
...openAIParams
|
|
1030
|
-
} = body;
|
|
1031
|
-
const traceId = posthogTraceId ?? v4();
|
|
1057
|
+
openAIParams,
|
|
1058
|
+
posthogParams
|
|
1059
|
+
} = extractPosthogParams(body);
|
|
1032
1060
|
const startTime = Date.now();
|
|
1033
1061
|
const parentPromise = super.create(openAIParams, options);
|
|
1034
1062
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1035
1063
|
const latency = (Date.now() - startTime) / 1000;
|
|
1036
1064
|
await sendEventToPosthog({
|
|
1037
1065
|
client: this.phClient,
|
|
1066
|
+
...posthogParams,
|
|
1038
1067
|
eventType: AIEvent.Embedding,
|
|
1039
|
-
distinctId: posthogDistinctId,
|
|
1040
|
-
traceId,
|
|
1041
1068
|
model: openAIParams.model,
|
|
1042
1069
|
provider: 'openai',
|
|
1043
|
-
input: withPrivacyMode(this.phClient,
|
|
1070
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
1044
1071
|
output: null,
|
|
1045
1072
|
// Embeddings don't have output content
|
|
1046
1073
|
latency,
|
|
@@ -1049,8 +1076,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1049
1076
|
httpStatus: 200,
|
|
1050
1077
|
usage: {
|
|
1051
1078
|
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1052
|
-
}
|
|
1053
|
-
captureImmediate: posthogCaptureImmediate
|
|
1079
|
+
}
|
|
1054
1080
|
});
|
|
1055
1081
|
return result;
|
|
1056
1082
|
}, async error => {
|
|
@@ -1058,11 +1084,10 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1058
1084
|
await sendEventToPosthog({
|
|
1059
1085
|
client: this.phClient,
|
|
1060
1086
|
eventType: AIEvent.Embedding,
|
|
1061
|
-
|
|
1062
|
-
traceId,
|
|
1087
|
+
...posthogParams,
|
|
1063
1088
|
model: openAIParams.model,
|
|
1064
1089
|
provider: 'openai',
|
|
1065
|
-
input: withPrivacyMode(this.phClient,
|
|
1090
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
1066
1091
|
output: null,
|
|
1067
1092
|
// Embeddings don't have output content
|
|
1068
1093
|
latency: 0,
|
|
@@ -1073,8 +1098,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1073
1098
|
inputTokens: 0
|
|
1074
1099
|
},
|
|
1075
1100
|
isError: true,
|
|
1076
|
-
error: JSON.stringify(error)
|
|
1077
|
-
captureImmediate: posthogCaptureImmediate
|
|
1101
|
+
error: JSON.stringify(error)
|
|
1078
1102
|
});
|
|
1079
1103
|
throw error;
|
|
1080
1104
|
});
|
|
@@ -1109,12 +1133,9 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1109
1133
|
// --- Implementation Signature
|
|
1110
1134
|
create(body, options) {
|
|
1111
1135
|
const {
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
...openAIParams
|
|
1116
|
-
} = body;
|
|
1117
|
-
const traceId = posthogTraceId ?? v4();
|
|
1136
|
+
openAIParams,
|
|
1137
|
+
posthogParams
|
|
1138
|
+
} = extractPosthogParams(body);
|
|
1118
1139
|
const startTime = Date.now();
|
|
1119
1140
|
const parentPromise = super.create(openAIParams, options);
|
|
1120
1141
|
if (openAIParams.stream) {
|
|
@@ -1212,8 +1233,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1212
1233
|
const latency = (Date.now() - startTime) / 1000;
|
|
1213
1234
|
await sendEventToPosthog({
|
|
1214
1235
|
client: this.phClient,
|
|
1215
|
-
|
|
1216
|
-
traceId,
|
|
1236
|
+
...posthogParams,
|
|
1217
1237
|
model: openAIParams.model,
|
|
1218
1238
|
provider: 'azure',
|
|
1219
1239
|
input: openAIParams.messages,
|
|
@@ -1222,15 +1242,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1222
1242
|
baseURL: this.baseURL,
|
|
1223
1243
|
params: body,
|
|
1224
1244
|
httpStatus: 200,
|
|
1225
|
-
usage
|
|
1226
|
-
captureImmediate: posthogCaptureImmediate
|
|
1245
|
+
usage
|
|
1227
1246
|
});
|
|
1228
1247
|
} catch (error) {
|
|
1229
1248
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1230
1249
|
await sendEventToPosthog({
|
|
1231
1250
|
client: this.phClient,
|
|
1232
|
-
|
|
1233
|
-
traceId,
|
|
1251
|
+
...posthogParams,
|
|
1234
1252
|
model: openAIParams.model,
|
|
1235
1253
|
provider: 'azure',
|
|
1236
1254
|
input: openAIParams.messages,
|
|
@@ -1244,8 +1262,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1244
1262
|
outputTokens: 0
|
|
1245
1263
|
},
|
|
1246
1264
|
isError: true,
|
|
1247
|
-
error: JSON.stringify(error)
|
|
1248
|
-
captureImmediate: posthogCaptureImmediate
|
|
1265
|
+
error: JSON.stringify(error)
|
|
1249
1266
|
});
|
|
1250
1267
|
}
|
|
1251
1268
|
})();
|
|
@@ -1260,8 +1277,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1260
1277
|
const latency = (Date.now() - startTime) / 1000;
|
|
1261
1278
|
await sendEventToPosthog({
|
|
1262
1279
|
client: this.phClient,
|
|
1263
|
-
|
|
1264
|
-
traceId,
|
|
1280
|
+
...posthogParams,
|
|
1265
1281
|
model: openAIParams.model,
|
|
1266
1282
|
provider: 'azure',
|
|
1267
1283
|
input: openAIParams.messages,
|
|
@@ -1275,8 +1291,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1275
1291
|
outputTokens: result.usage?.completion_tokens ?? 0,
|
|
1276
1292
|
reasoningTokens: result.usage?.completion_tokens_details?.reasoning_tokens ?? 0,
|
|
1277
1293
|
cacheReadInputTokens: result.usage?.prompt_tokens_details?.cached_tokens ?? 0
|
|
1278
|
-
}
|
|
1279
|
-
captureImmediate: posthogCaptureImmediate
|
|
1294
|
+
}
|
|
1280
1295
|
});
|
|
1281
1296
|
}
|
|
1282
1297
|
return result;
|
|
@@ -1284,8 +1299,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1284
1299
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1285
1300
|
await sendEventToPosthog({
|
|
1286
1301
|
client: this.phClient,
|
|
1287
|
-
|
|
1288
|
-
traceId,
|
|
1302
|
+
...posthogParams,
|
|
1289
1303
|
model: openAIParams.model,
|
|
1290
1304
|
provider: 'azure',
|
|
1291
1305
|
input: openAIParams.messages,
|
|
@@ -1299,8 +1313,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1299
1313
|
outputTokens: 0
|
|
1300
1314
|
},
|
|
1301
1315
|
isError: true,
|
|
1302
|
-
error: JSON.stringify(error)
|
|
1303
|
-
captureImmediate: posthogCaptureImmediate
|
|
1316
|
+
error: JSON.stringify(error)
|
|
1304
1317
|
});
|
|
1305
1318
|
throw error;
|
|
1306
1319
|
});
|
|
@@ -1317,12 +1330,9 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1317
1330
|
// --- Implementation Signature
|
|
1318
1331
|
create(body, options) {
|
|
1319
1332
|
const {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
...openAIParams
|
|
1324
|
-
} = body;
|
|
1325
|
-
const traceId = posthogTraceId ?? v4();
|
|
1333
|
+
openAIParams,
|
|
1334
|
+
posthogParams
|
|
1335
|
+
} = extractPosthogParams(body);
|
|
1326
1336
|
const startTime = Date.now();
|
|
1327
1337
|
const parentPromise = super.create(openAIParams, options);
|
|
1328
1338
|
if (openAIParams.stream) {
|
|
@@ -1352,8 +1362,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1352
1362
|
const latency = (Date.now() - startTime) / 1000;
|
|
1353
1363
|
await sendEventToPosthog({
|
|
1354
1364
|
client: this.phClient,
|
|
1355
|
-
|
|
1356
|
-
traceId,
|
|
1365
|
+
...posthogParams,
|
|
1357
1366
|
//@ts-expect-error
|
|
1358
1367
|
model: openAIParams.model,
|
|
1359
1368
|
provider: 'azure',
|
|
@@ -1363,15 +1372,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1363
1372
|
baseURL: this.baseURL,
|
|
1364
1373
|
params: body,
|
|
1365
1374
|
httpStatus: 200,
|
|
1366
|
-
usage
|
|
1367
|
-
captureImmediate: posthogCaptureImmediate
|
|
1375
|
+
usage
|
|
1368
1376
|
});
|
|
1369
1377
|
} catch (error) {
|
|
1370
1378
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1371
1379
|
await sendEventToPosthog({
|
|
1372
1380
|
client: this.phClient,
|
|
1373
|
-
|
|
1374
|
-
traceId,
|
|
1381
|
+
...posthogParams,
|
|
1375
1382
|
//@ts-expect-error
|
|
1376
1383
|
model: openAIParams.model,
|
|
1377
1384
|
provider: 'azure',
|
|
@@ -1386,8 +1393,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1386
1393
|
outputTokens: 0
|
|
1387
1394
|
},
|
|
1388
1395
|
isError: true,
|
|
1389
|
-
error: JSON.stringify(error)
|
|
1390
|
-
captureImmediate: posthogCaptureImmediate
|
|
1396
|
+
error: JSON.stringify(error)
|
|
1391
1397
|
});
|
|
1392
1398
|
}
|
|
1393
1399
|
})();
|
|
@@ -1401,8 +1407,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1401
1407
|
const latency = (Date.now() - startTime) / 1000;
|
|
1402
1408
|
await sendEventToPosthog({
|
|
1403
1409
|
client: this.phClient,
|
|
1404
|
-
|
|
1405
|
-
traceId,
|
|
1410
|
+
...posthogParams,
|
|
1406
1411
|
//@ts-expect-error
|
|
1407
1412
|
model: openAIParams.model,
|
|
1408
1413
|
provider: 'azure',
|
|
@@ -1417,8 +1422,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1417
1422
|
outputTokens: result.usage?.output_tokens ?? 0,
|
|
1418
1423
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
1419
1424
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
1420
|
-
}
|
|
1421
|
-
captureImmediate: posthogCaptureImmediate
|
|
1425
|
+
}
|
|
1422
1426
|
});
|
|
1423
1427
|
}
|
|
1424
1428
|
return result;
|
|
@@ -1426,8 +1430,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1426
1430
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1427
1431
|
await sendEventToPosthog({
|
|
1428
1432
|
client: this.phClient,
|
|
1429
|
-
|
|
1430
|
-
traceId,
|
|
1433
|
+
...posthogParams,
|
|
1431
1434
|
//@ts-expect-error
|
|
1432
1435
|
model: openAIParams.model,
|
|
1433
1436
|
provider: 'azure',
|
|
@@ -1442,8 +1445,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1442
1445
|
outputTokens: 0
|
|
1443
1446
|
},
|
|
1444
1447
|
isError: true,
|
|
1445
|
-
error: JSON.stringify(error)
|
|
1446
|
-
captureImmediate: posthogCaptureImmediate
|
|
1448
|
+
error: JSON.stringify(error)
|
|
1447
1449
|
});
|
|
1448
1450
|
throw error;
|
|
1449
1451
|
});
|
|
@@ -1452,20 +1454,16 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1452
1454
|
}
|
|
1453
1455
|
parse(body, options) {
|
|
1454
1456
|
const {
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
...openAIParams
|
|
1459
|
-
} = body;
|
|
1460
|
-
const traceId = posthogTraceId ?? v4();
|
|
1457
|
+
openAIParams,
|
|
1458
|
+
posthogParams
|
|
1459
|
+
} = extractPosthogParams(body);
|
|
1461
1460
|
const startTime = Date.now();
|
|
1462
1461
|
const parentPromise = super.parse(openAIParams, options);
|
|
1463
1462
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1464
1463
|
const latency = (Date.now() - startTime) / 1000;
|
|
1465
1464
|
await sendEventToPosthog({
|
|
1466
1465
|
client: this.phClient,
|
|
1467
|
-
|
|
1468
|
-
traceId,
|
|
1466
|
+
...posthogParams,
|
|
1469
1467
|
//@ts-expect-error
|
|
1470
1468
|
model: openAIParams.model,
|
|
1471
1469
|
provider: 'azure',
|
|
@@ -1480,15 +1478,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1480
1478
|
outputTokens: result.usage?.output_tokens ?? 0,
|
|
1481
1479
|
reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
|
|
1482
1480
|
cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0
|
|
1483
|
-
}
|
|
1484
|
-
captureImmediate: posthogCaptureImmediate
|
|
1481
|
+
}
|
|
1485
1482
|
});
|
|
1486
1483
|
return result;
|
|
1487
1484
|
}, async error => {
|
|
1488
1485
|
await sendEventToPosthog({
|
|
1489
1486
|
client: this.phClient,
|
|
1490
|
-
|
|
1491
|
-
traceId,
|
|
1487
|
+
...posthogParams,
|
|
1492
1488
|
//@ts-expect-error
|
|
1493
1489
|
model: openAIParams.model,
|
|
1494
1490
|
provider: 'azure',
|
|
@@ -1503,8 +1499,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1503
1499
|
outputTokens: 0
|
|
1504
1500
|
},
|
|
1505
1501
|
isError: true,
|
|
1506
|
-
error: JSON.stringify(error)
|
|
1507
|
-
captureImmediate: posthogCaptureImmediate
|
|
1502
|
+
error: JSON.stringify(error)
|
|
1508
1503
|
});
|
|
1509
1504
|
throw error;
|
|
1510
1505
|
});
|
|
@@ -1519,13 +1514,9 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1519
1514
|
}
|
|
1520
1515
|
create(body, options) {
|
|
1521
1516
|
const {
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
posthogCaptureImmediate,
|
|
1526
|
-
...openAIParams
|
|
1527
|
-
} = body;
|
|
1528
|
-
const traceId = posthogTraceId ?? v4();
|
|
1517
|
+
openAIParams,
|
|
1518
|
+
posthogParams
|
|
1519
|
+
} = extractPosthogParams(body);
|
|
1529
1520
|
const startTime = Date.now();
|
|
1530
1521
|
const parentPromise = super.create(openAIParams, options);
|
|
1531
1522
|
const wrappedPromise = parentPromise.then(async result => {
|
|
@@ -1533,11 +1524,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1533
1524
|
await sendEventToPosthog({
|
|
1534
1525
|
client: this.phClient,
|
|
1535
1526
|
eventType: AIEvent.Embedding,
|
|
1536
|
-
|
|
1537
|
-
traceId,
|
|
1527
|
+
...posthogParams,
|
|
1538
1528
|
model: openAIParams.model,
|
|
1539
1529
|
provider: 'azure',
|
|
1540
|
-
input: withPrivacyMode(this.phClient,
|
|
1530
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
1541
1531
|
output: null,
|
|
1542
1532
|
// Embeddings don't have output content
|
|
1543
1533
|
latency,
|
|
@@ -1546,8 +1536,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1546
1536
|
httpStatus: 200,
|
|
1547
1537
|
usage: {
|
|
1548
1538
|
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1549
|
-
}
|
|
1550
|
-
captureImmediate: posthogCaptureImmediate
|
|
1539
|
+
}
|
|
1551
1540
|
});
|
|
1552
1541
|
return result;
|
|
1553
1542
|
}, async error => {
|
|
@@ -1555,11 +1544,10 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1555
1544
|
await sendEventToPosthog({
|
|
1556
1545
|
client: this.phClient,
|
|
1557
1546
|
eventType: AIEvent.Embedding,
|
|
1558
|
-
|
|
1559
|
-
traceId,
|
|
1547
|
+
...posthogParams,
|
|
1560
1548
|
model: openAIParams.model,
|
|
1561
1549
|
provider: 'azure',
|
|
1562
|
-
input: withPrivacyMode(this.phClient,
|
|
1550
|
+
input: withPrivacyMode(this.phClient, posthogParams.privacyMode, openAIParams.input),
|
|
1563
1551
|
output: null,
|
|
1564
1552
|
latency: 0,
|
|
1565
1553
|
baseURL: this.baseURL,
|
|
@@ -1569,8 +1557,7 @@ class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
|
1569
1557
|
inputTokens: 0
|
|
1570
1558
|
},
|
|
1571
1559
|
isError: true,
|
|
1572
|
-
error: JSON.stringify(error)
|
|
1573
|
-
captureImmediate: posthogCaptureImmediate
|
|
1560
|
+
error: JSON.stringify(error)
|
|
1574
1561
|
});
|
|
1575
1562
|
throw error;
|
|
1576
1563
|
});
|