@posthog/ai 7.20.12 → 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 +3 -3
package/dist/langchain/index.cjs
CHANGED
package/dist/langchain/index.mjs
CHANGED
package/dist/openai/index.cjs
CHANGED
|
@@ -534,7 +534,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
534
534
|
return messages;
|
|
535
535
|
}
|
|
536
536
|
|
|
537
|
-
var version = "7.20.
|
|
537
|
+
var version = "7.20.13";
|
|
538
538
|
|
|
539
539
|
const DEFAULT_MAX_DEPTH = 3;
|
|
540
540
|
const MAX_STACK_LINES = 20;
|
|
@@ -765,6 +765,33 @@ const Responses = openai.OpenAI.Responses;
|
|
|
765
765
|
const Embeddings = openai.OpenAI.Embeddings;
|
|
766
766
|
const Audio = openai.OpenAI.Audio;
|
|
767
767
|
const Transcriptions = openai.OpenAI.Audio.Transcriptions;
|
|
768
|
+
function captureAiGenerationInBackground(...args) {
|
|
769
|
+
void captureAiGeneration(...args).catch(() => undefined);
|
|
770
|
+
}
|
|
771
|
+
async function captureAiGenerationAfterSuccess(...args) {
|
|
772
|
+
const [, options] = args;
|
|
773
|
+
if (options.captureImmediate) {
|
|
774
|
+
await captureAiGeneration(...args);
|
|
775
|
+
} else {
|
|
776
|
+
captureAiGenerationInBackground(...args);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function preserveAPIPromiseHelpers(parentPromise, wrappedPromise) {
|
|
780
|
+
const apiPromise = wrappedPromise;
|
|
781
|
+
if (typeof parentPromise.asResponse === 'function') {
|
|
782
|
+
apiPromise.asResponse = () => parentPromise.asResponse();
|
|
783
|
+
}
|
|
784
|
+
if (typeof parentPromise.withResponse === 'function') {
|
|
785
|
+
apiPromise.withResponse = async () => {
|
|
786
|
+
const [response, data] = await Promise.all([parentPromise.withResponse(), wrappedPromise]);
|
|
787
|
+
return {
|
|
788
|
+
...response,
|
|
789
|
+
data
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
return apiPromise;
|
|
794
|
+
}
|
|
768
795
|
class PostHogOpenAI extends openai.OpenAI {
|
|
769
796
|
constructor(config) {
|
|
770
797
|
const {
|
|
@@ -807,7 +834,7 @@ class WrappedCompletions extends Completions {
|
|
|
807
834
|
const startTime = Date.now();
|
|
808
835
|
const parentPromise = super.create(openAIParams, options);
|
|
809
836
|
if (openAIParams.stream) {
|
|
810
|
-
|
|
837
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
811
838
|
if ('tee' in value) {
|
|
812
839
|
const [stream1, stream2] = value.tee();
|
|
813
840
|
(async () => {
|
|
@@ -1000,13 +1027,14 @@ class WrappedCompletions extends Completions {
|
|
|
1000
1027
|
}
|
|
1001
1028
|
return value;
|
|
1002
1029
|
});
|
|
1030
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1003
1031
|
} else {
|
|
1004
1032
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1005
1033
|
if ('choices' in result) {
|
|
1006
1034
|
const latency = (Date.now() - startTime) / 1000;
|
|
1007
1035
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1008
1036
|
const formattedOutput = formatResponseOpenAI(result);
|
|
1009
|
-
await
|
|
1037
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1010
1038
|
...posthogParams,
|
|
1011
1039
|
model: openAIParams.model ?? result.model,
|
|
1012
1040
|
provider: 'openai',
|
|
@@ -1054,7 +1082,7 @@ class WrappedCompletions extends Completions {
|
|
|
1054
1082
|
});
|
|
1055
1083
|
throw error;
|
|
1056
1084
|
});
|
|
1057
|
-
return wrappedPromise;
|
|
1085
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1058
1086
|
}
|
|
1059
1087
|
}
|
|
1060
1088
|
}
|
|
@@ -1080,7 +1108,7 @@ class WrappedResponses extends Responses {
|
|
|
1080
1108
|
const startTime = Date.now();
|
|
1081
1109
|
const parentPromise = super.create(openAIParams, options);
|
|
1082
1110
|
if (openAIParams.stream) {
|
|
1083
|
-
|
|
1111
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1084
1112
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1085
1113
|
const [stream1, stream2] = value.tee();
|
|
1086
1114
|
(async () => {
|
|
@@ -1185,6 +1213,7 @@ class WrappedResponses extends Responses {
|
|
|
1185
1213
|
}
|
|
1186
1214
|
return value;
|
|
1187
1215
|
});
|
|
1216
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1188
1217
|
} else {
|
|
1189
1218
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1190
1219
|
if ('output' in result) {
|
|
@@ -1193,7 +1222,7 @@ class WrappedResponses extends Responses {
|
|
|
1193
1222
|
const formattedOutput = formatResponseOpenAI({
|
|
1194
1223
|
output: result.output
|
|
1195
1224
|
});
|
|
1196
|
-
await
|
|
1225
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1197
1226
|
...posthogParams,
|
|
1198
1227
|
model: openAIParams.model ?? result.model,
|
|
1199
1228
|
provider: 'openai',
|
|
@@ -1240,7 +1269,7 @@ class WrappedResponses extends Responses {
|
|
|
1240
1269
|
});
|
|
1241
1270
|
throw error;
|
|
1242
1271
|
});
|
|
1243
|
-
return wrappedPromise;
|
|
1272
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1244
1273
|
}
|
|
1245
1274
|
}
|
|
1246
1275
|
parse(body, options) {
|
|
@@ -1299,7 +1328,7 @@ class WrappedResponses extends Responses {
|
|
|
1299
1328
|
});
|
|
1300
1329
|
throw error;
|
|
1301
1330
|
});
|
|
1302
|
-
return wrappedPromise;
|
|
1331
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1303
1332
|
} finally {
|
|
1304
1333
|
// Restore our wrapped create method
|
|
1305
1334
|
originalSelfRecord['create'] = tempCreate;
|
|
@@ -1360,7 +1389,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1360
1389
|
});
|
|
1361
1390
|
throw error;
|
|
1362
1391
|
});
|
|
1363
|
-
return wrappedPromise;
|
|
1392
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1364
1393
|
}
|
|
1365
1394
|
}
|
|
1366
1395
|
class WrappedAudio extends Audio {
|
|
@@ -1399,7 +1428,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1399
1428
|
const startTime = Date.now();
|
|
1400
1429
|
const parentPromise = openAIParams.stream ? super.create(openAIParams, options) : super.create(openAIParams, options);
|
|
1401
1430
|
if (openAIParams.stream) {
|
|
1402
|
-
|
|
1431
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1403
1432
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1404
1433
|
const [stream1, stream2] = value.tee();
|
|
1405
1434
|
(async () => {
|
|
@@ -1467,11 +1496,12 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1467
1496
|
}
|
|
1468
1497
|
return value;
|
|
1469
1498
|
});
|
|
1499
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1470
1500
|
} else {
|
|
1471
1501
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1472
|
-
if ('text' in result) {
|
|
1502
|
+
if (result && typeof result === 'object' && 'text' in result) {
|
|
1473
1503
|
const latency = (Date.now() - startTime) / 1000;
|
|
1474
|
-
await
|
|
1504
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1475
1505
|
...posthogParams,
|
|
1476
1506
|
model: openAIParams.model,
|
|
1477
1507
|
provider: 'openai',
|
|
@@ -1487,8 +1517,8 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1487
1517
|
rawUsage: result.usage
|
|
1488
1518
|
}
|
|
1489
1519
|
});
|
|
1490
|
-
return result;
|
|
1491
1520
|
}
|
|
1521
|
+
return result;
|
|
1492
1522
|
}, async error => {
|
|
1493
1523
|
await captureAiGeneration(this.phClient, {
|
|
1494
1524
|
...posthogParams,
|
|
@@ -1507,7 +1537,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1507
1537
|
});
|
|
1508
1538
|
throw error;
|
|
1509
1539
|
});
|
|
1510
|
-
return wrappedPromise;
|
|
1540
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1511
1541
|
}
|
|
1512
1542
|
}
|
|
1513
1543
|
}
|