@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/anthropic/index.cjs
CHANGED
package/dist/anthropic/index.mjs
CHANGED
package/dist/gemini/index.cjs
CHANGED
package/dist/gemini/index.mjs
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -733,7 +733,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
733
733
|
return messages;
|
|
734
734
|
}
|
|
735
735
|
|
|
736
|
-
var version = "7.20.
|
|
736
|
+
var version = "7.20.13";
|
|
737
737
|
|
|
738
738
|
const DEFAULT_MAX_DEPTH = 3;
|
|
739
739
|
const MAX_STACK_LINES = 20;
|
|
@@ -956,6 +956,33 @@ const Responses = openai.OpenAI.Responses;
|
|
|
956
956
|
const Embeddings = openai.OpenAI.Embeddings;
|
|
957
957
|
const Audio = openai.OpenAI.Audio;
|
|
958
958
|
const Transcriptions = openai.OpenAI.Audio.Transcriptions;
|
|
959
|
+
function captureAiGenerationInBackground(...args) {
|
|
960
|
+
void captureAiGeneration(...args).catch(() => undefined);
|
|
961
|
+
}
|
|
962
|
+
async function captureAiGenerationAfterSuccess(...args) {
|
|
963
|
+
const [, options] = args;
|
|
964
|
+
if (options.captureImmediate) {
|
|
965
|
+
await captureAiGeneration(...args);
|
|
966
|
+
} else {
|
|
967
|
+
captureAiGenerationInBackground(...args);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
function preserveAPIPromiseHelpers(parentPromise, wrappedPromise) {
|
|
971
|
+
const apiPromise = wrappedPromise;
|
|
972
|
+
if (typeof parentPromise.asResponse === 'function') {
|
|
973
|
+
apiPromise.asResponse = () => parentPromise.asResponse();
|
|
974
|
+
}
|
|
975
|
+
if (typeof parentPromise.withResponse === 'function') {
|
|
976
|
+
apiPromise.withResponse = async () => {
|
|
977
|
+
const [response, data] = await Promise.all([parentPromise.withResponse(), wrappedPromise]);
|
|
978
|
+
return {
|
|
979
|
+
...response,
|
|
980
|
+
data
|
|
981
|
+
};
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
return apiPromise;
|
|
985
|
+
}
|
|
959
986
|
class PostHogOpenAI extends openai.OpenAI {
|
|
960
987
|
constructor(config) {
|
|
961
988
|
const {
|
|
@@ -991,7 +1018,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
991
1018
|
const startTime = Date.now();
|
|
992
1019
|
const parentPromise = super.create(openAIParams, options);
|
|
993
1020
|
if (openAIParams.stream) {
|
|
994
|
-
|
|
1021
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
995
1022
|
if ('tee' in value) {
|
|
996
1023
|
const [stream1, stream2] = value.tee();
|
|
997
1024
|
(async () => {
|
|
@@ -1176,13 +1203,14 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
1176
1203
|
}
|
|
1177
1204
|
return value;
|
|
1178
1205
|
});
|
|
1206
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1179
1207
|
} else {
|
|
1180
1208
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1181
1209
|
if ('choices' in result) {
|
|
1182
1210
|
const latency = (Date.now() - startTime) / 1000;
|
|
1183
1211
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1184
1212
|
const formattedOutput = formatResponseOpenAI(result);
|
|
1185
|
-
await
|
|
1213
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1186
1214
|
...posthogParams,
|
|
1187
1215
|
model: openAIParams.model ?? result.model,
|
|
1188
1216
|
provider: 'openai',
|
|
@@ -1230,7 +1258,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
1230
1258
|
});
|
|
1231
1259
|
throw error;
|
|
1232
1260
|
});
|
|
1233
|
-
return wrappedPromise;
|
|
1261
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1234
1262
|
}
|
|
1235
1263
|
}
|
|
1236
1264
|
};
|
|
@@ -1249,7 +1277,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1249
1277
|
const startTime = Date.now();
|
|
1250
1278
|
const parentPromise = super.create(openAIParams, options);
|
|
1251
1279
|
if (openAIParams.stream) {
|
|
1252
|
-
|
|
1280
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1253
1281
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1254
1282
|
const [stream1, stream2] = value.tee();
|
|
1255
1283
|
(async () => {
|
|
@@ -1354,6 +1382,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1354
1382
|
}
|
|
1355
1383
|
return value;
|
|
1356
1384
|
});
|
|
1385
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1357
1386
|
} else {
|
|
1358
1387
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1359
1388
|
if ('output' in result) {
|
|
@@ -1362,7 +1391,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1362
1391
|
const formattedOutput = formatResponseOpenAI({
|
|
1363
1392
|
output: result.output
|
|
1364
1393
|
});
|
|
1365
|
-
await
|
|
1394
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1366
1395
|
...posthogParams,
|
|
1367
1396
|
model: openAIParams.model ?? result.model,
|
|
1368
1397
|
provider: 'openai',
|
|
@@ -1409,7 +1438,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1409
1438
|
});
|
|
1410
1439
|
throw error;
|
|
1411
1440
|
});
|
|
1412
|
-
return wrappedPromise;
|
|
1441
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1413
1442
|
}
|
|
1414
1443
|
}
|
|
1415
1444
|
parse(body, options) {
|
|
@@ -1468,7 +1497,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1468
1497
|
});
|
|
1469
1498
|
throw error;
|
|
1470
1499
|
});
|
|
1471
|
-
return wrappedPromise;
|
|
1500
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1472
1501
|
} finally {
|
|
1473
1502
|
// Restore our wrapped create method
|
|
1474
1503
|
originalSelfRecord['create'] = tempCreate;
|
|
@@ -1529,7 +1558,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1529
1558
|
});
|
|
1530
1559
|
throw error;
|
|
1531
1560
|
});
|
|
1532
|
-
return wrappedPromise;
|
|
1561
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1533
1562
|
}
|
|
1534
1563
|
};
|
|
1535
1564
|
class WrappedAudio extends Audio {
|
|
@@ -1553,7 +1582,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1553
1582
|
const startTime = Date.now();
|
|
1554
1583
|
const parentPromise = openAIParams.stream ? super.create(openAIParams, options) : super.create(openAIParams, options);
|
|
1555
1584
|
if (openAIParams.stream) {
|
|
1556
|
-
|
|
1585
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1557
1586
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1558
1587
|
const [stream1, stream2] = value.tee();
|
|
1559
1588
|
(async () => {
|
|
@@ -1621,11 +1650,12 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1621
1650
|
}
|
|
1622
1651
|
return value;
|
|
1623
1652
|
});
|
|
1653
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1624
1654
|
} else {
|
|
1625
1655
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1626
|
-
if ('text' in result) {
|
|
1656
|
+
if (result && typeof result === 'object' && 'text' in result) {
|
|
1627
1657
|
const latency = (Date.now() - startTime) / 1000;
|
|
1628
|
-
await
|
|
1658
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1629
1659
|
...posthogParams,
|
|
1630
1660
|
model: openAIParams.model,
|
|
1631
1661
|
provider: 'openai',
|
|
@@ -1641,8 +1671,8 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1641
1671
|
rawUsage: result.usage
|
|
1642
1672
|
}
|
|
1643
1673
|
});
|
|
1644
|
-
return result;
|
|
1645
1674
|
}
|
|
1675
|
+
return result;
|
|
1646
1676
|
}, async error => {
|
|
1647
1677
|
await captureAiGeneration(this.phClient, {
|
|
1648
1678
|
...posthogParams,
|
|
@@ -1661,7 +1691,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1661
1691
|
});
|
|
1662
1692
|
throw error;
|
|
1663
1693
|
});
|
|
1664
|
-
return wrappedPromise;
|
|
1694
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1665
1695
|
}
|
|
1666
1696
|
}
|
|
1667
1697
|
}
|