@posthog/ai 7.20.11 → 7.20.13
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.mjs +1 -1
- package/dist/gemini/index.cjs +1 -1
- package/dist/gemini/index.mjs +1 -1
- package/dist/index.cjs +44 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +44 -14
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/openai/index.cjs +44 -14
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +44 -14
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +1 -1
- package/dist/openai-agents/index.mjs +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +5 -5
package/dist/openai/index.mjs
CHANGED
|
@@ -530,7 +530,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
530
530
|
return messages;
|
|
531
531
|
}
|
|
532
532
|
|
|
533
|
-
var version = "7.20.
|
|
533
|
+
var version = "7.20.13";
|
|
534
534
|
|
|
535
535
|
const DEFAULT_MAX_DEPTH = 3;
|
|
536
536
|
const MAX_STACK_LINES = 20;
|
|
@@ -761,6 +761,33 @@ const Responses = OpenAI.Responses;
|
|
|
761
761
|
const Embeddings = OpenAI.Embeddings;
|
|
762
762
|
const Audio = OpenAI.Audio;
|
|
763
763
|
const Transcriptions = OpenAI.Audio.Transcriptions;
|
|
764
|
+
function captureAiGenerationInBackground(...args) {
|
|
765
|
+
void captureAiGeneration(...args).catch(() => undefined);
|
|
766
|
+
}
|
|
767
|
+
async function captureAiGenerationAfterSuccess(...args) {
|
|
768
|
+
const [, options] = args;
|
|
769
|
+
if (options.captureImmediate) {
|
|
770
|
+
await captureAiGeneration(...args);
|
|
771
|
+
} else {
|
|
772
|
+
captureAiGenerationInBackground(...args);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
function preserveAPIPromiseHelpers(parentPromise, wrappedPromise) {
|
|
776
|
+
const apiPromise = wrappedPromise;
|
|
777
|
+
if (typeof parentPromise.asResponse === 'function') {
|
|
778
|
+
apiPromise.asResponse = () => parentPromise.asResponse();
|
|
779
|
+
}
|
|
780
|
+
if (typeof parentPromise.withResponse === 'function') {
|
|
781
|
+
apiPromise.withResponse = async () => {
|
|
782
|
+
const [response, data] = await Promise.all([parentPromise.withResponse(), wrappedPromise]);
|
|
783
|
+
return {
|
|
784
|
+
...response,
|
|
785
|
+
data
|
|
786
|
+
};
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
return apiPromise;
|
|
790
|
+
}
|
|
764
791
|
class PostHogOpenAI extends OpenAI {
|
|
765
792
|
constructor(config) {
|
|
766
793
|
const {
|
|
@@ -803,7 +830,7 @@ class WrappedCompletions extends Completions {
|
|
|
803
830
|
const startTime = Date.now();
|
|
804
831
|
const parentPromise = super.create(openAIParams, options);
|
|
805
832
|
if (openAIParams.stream) {
|
|
806
|
-
|
|
833
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
807
834
|
if ('tee' in value) {
|
|
808
835
|
const [stream1, stream2] = value.tee();
|
|
809
836
|
(async () => {
|
|
@@ -996,13 +1023,14 @@ class WrappedCompletions extends Completions {
|
|
|
996
1023
|
}
|
|
997
1024
|
return value;
|
|
998
1025
|
});
|
|
1026
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
999
1027
|
} else {
|
|
1000
1028
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1001
1029
|
if ('choices' in result) {
|
|
1002
1030
|
const latency = (Date.now() - startTime) / 1000;
|
|
1003
1031
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1004
1032
|
const formattedOutput = formatResponseOpenAI(result);
|
|
1005
|
-
await
|
|
1033
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1006
1034
|
...posthogParams,
|
|
1007
1035
|
model: openAIParams.model ?? result.model,
|
|
1008
1036
|
provider: 'openai',
|
|
@@ -1050,7 +1078,7 @@ class WrappedCompletions extends Completions {
|
|
|
1050
1078
|
});
|
|
1051
1079
|
throw error;
|
|
1052
1080
|
});
|
|
1053
|
-
return wrappedPromise;
|
|
1081
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1054
1082
|
}
|
|
1055
1083
|
}
|
|
1056
1084
|
}
|
|
@@ -1076,7 +1104,7 @@ class WrappedResponses extends Responses {
|
|
|
1076
1104
|
const startTime = Date.now();
|
|
1077
1105
|
const parentPromise = super.create(openAIParams, options);
|
|
1078
1106
|
if (openAIParams.stream) {
|
|
1079
|
-
|
|
1107
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1080
1108
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1081
1109
|
const [stream1, stream2] = value.tee();
|
|
1082
1110
|
(async () => {
|
|
@@ -1181,6 +1209,7 @@ class WrappedResponses extends Responses {
|
|
|
1181
1209
|
}
|
|
1182
1210
|
return value;
|
|
1183
1211
|
});
|
|
1212
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1184
1213
|
} else {
|
|
1185
1214
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1186
1215
|
if ('output' in result) {
|
|
@@ -1189,7 +1218,7 @@ class WrappedResponses extends Responses {
|
|
|
1189
1218
|
const formattedOutput = formatResponseOpenAI({
|
|
1190
1219
|
output: result.output
|
|
1191
1220
|
});
|
|
1192
|
-
await
|
|
1221
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1193
1222
|
...posthogParams,
|
|
1194
1223
|
model: openAIParams.model ?? result.model,
|
|
1195
1224
|
provider: 'openai',
|
|
@@ -1236,7 +1265,7 @@ class WrappedResponses extends Responses {
|
|
|
1236
1265
|
});
|
|
1237
1266
|
throw error;
|
|
1238
1267
|
});
|
|
1239
|
-
return wrappedPromise;
|
|
1268
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1240
1269
|
}
|
|
1241
1270
|
}
|
|
1242
1271
|
parse(body, options) {
|
|
@@ -1295,7 +1324,7 @@ class WrappedResponses extends Responses {
|
|
|
1295
1324
|
});
|
|
1296
1325
|
throw error;
|
|
1297
1326
|
});
|
|
1298
|
-
return wrappedPromise;
|
|
1327
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1299
1328
|
} finally {
|
|
1300
1329
|
// Restore our wrapped create method
|
|
1301
1330
|
originalSelfRecord['create'] = tempCreate;
|
|
@@ -1356,7 +1385,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1356
1385
|
});
|
|
1357
1386
|
throw error;
|
|
1358
1387
|
});
|
|
1359
|
-
return wrappedPromise;
|
|
1388
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1360
1389
|
}
|
|
1361
1390
|
}
|
|
1362
1391
|
class WrappedAudio extends Audio {
|
|
@@ -1395,7 +1424,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1395
1424
|
const startTime = Date.now();
|
|
1396
1425
|
const parentPromise = openAIParams.stream ? super.create(openAIParams, options) : super.create(openAIParams, options);
|
|
1397
1426
|
if (openAIParams.stream) {
|
|
1398
|
-
|
|
1427
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1399
1428
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1400
1429
|
const [stream1, stream2] = value.tee();
|
|
1401
1430
|
(async () => {
|
|
@@ -1463,11 +1492,12 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1463
1492
|
}
|
|
1464
1493
|
return value;
|
|
1465
1494
|
});
|
|
1495
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1466
1496
|
} else {
|
|
1467
1497
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1468
|
-
if ('text' in result) {
|
|
1498
|
+
if (result && typeof result === 'object' && 'text' in result) {
|
|
1469
1499
|
const latency = (Date.now() - startTime) / 1000;
|
|
1470
|
-
await
|
|
1500
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1471
1501
|
...posthogParams,
|
|
1472
1502
|
model: openAIParams.model,
|
|
1473
1503
|
provider: 'openai',
|
|
@@ -1483,8 +1513,8 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1483
1513
|
rawUsage: result.usage
|
|
1484
1514
|
}
|
|
1485
1515
|
});
|
|
1486
|
-
return result;
|
|
1487
1516
|
}
|
|
1517
|
+
return result;
|
|
1488
1518
|
}, async error => {
|
|
1489
1519
|
await captureAiGeneration(this.phClient, {
|
|
1490
1520
|
...posthogParams,
|
|
@@ -1503,7 +1533,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1503
1533
|
});
|
|
1504
1534
|
throw error;
|
|
1505
1535
|
});
|
|
1506
|
-
return wrappedPromise;
|
|
1536
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1507
1537
|
}
|
|
1508
1538
|
}
|
|
1509
1539
|
}
|