@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/index.mjs
CHANGED
|
@@ -709,7 +709,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
709
709
|
return messages;
|
|
710
710
|
}
|
|
711
711
|
|
|
712
|
-
var version = "7.20.
|
|
712
|
+
var version = "7.20.13";
|
|
713
713
|
|
|
714
714
|
const DEFAULT_MAX_DEPTH = 3;
|
|
715
715
|
const MAX_STACK_LINES = 20;
|
|
@@ -932,6 +932,33 @@ const Responses = OpenAI.Responses;
|
|
|
932
932
|
const Embeddings = OpenAI.Embeddings;
|
|
933
933
|
const Audio = OpenAI.Audio;
|
|
934
934
|
const Transcriptions = OpenAI.Audio.Transcriptions;
|
|
935
|
+
function captureAiGenerationInBackground(...args) {
|
|
936
|
+
void captureAiGeneration(...args).catch(() => undefined);
|
|
937
|
+
}
|
|
938
|
+
async function captureAiGenerationAfterSuccess(...args) {
|
|
939
|
+
const [, options] = args;
|
|
940
|
+
if (options.captureImmediate) {
|
|
941
|
+
await captureAiGeneration(...args);
|
|
942
|
+
} else {
|
|
943
|
+
captureAiGenerationInBackground(...args);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function preserveAPIPromiseHelpers(parentPromise, wrappedPromise) {
|
|
947
|
+
const apiPromise = wrappedPromise;
|
|
948
|
+
if (typeof parentPromise.asResponse === 'function') {
|
|
949
|
+
apiPromise.asResponse = () => parentPromise.asResponse();
|
|
950
|
+
}
|
|
951
|
+
if (typeof parentPromise.withResponse === 'function') {
|
|
952
|
+
apiPromise.withResponse = async () => {
|
|
953
|
+
const [response, data] = await Promise.all([parentPromise.withResponse(), wrappedPromise]);
|
|
954
|
+
return {
|
|
955
|
+
...response,
|
|
956
|
+
data
|
|
957
|
+
};
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
return apiPromise;
|
|
961
|
+
}
|
|
935
962
|
class PostHogOpenAI extends OpenAI {
|
|
936
963
|
constructor(config) {
|
|
937
964
|
const {
|
|
@@ -967,7 +994,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
967
994
|
const startTime = Date.now();
|
|
968
995
|
const parentPromise = super.create(openAIParams, options);
|
|
969
996
|
if (openAIParams.stream) {
|
|
970
|
-
|
|
997
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
971
998
|
if ('tee' in value) {
|
|
972
999
|
const [stream1, stream2] = value.tee();
|
|
973
1000
|
(async () => {
|
|
@@ -1152,13 +1179,14 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
1152
1179
|
}
|
|
1153
1180
|
return value;
|
|
1154
1181
|
});
|
|
1182
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1155
1183
|
} else {
|
|
1156
1184
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1157
1185
|
if ('choices' in result) {
|
|
1158
1186
|
const latency = (Date.now() - startTime) / 1000;
|
|
1159
1187
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1160
1188
|
const formattedOutput = formatResponseOpenAI(result);
|
|
1161
|
-
await
|
|
1189
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1162
1190
|
...posthogParams,
|
|
1163
1191
|
model: openAIParams.model ?? result.model,
|
|
1164
1192
|
provider: 'openai',
|
|
@@ -1206,7 +1234,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
1206
1234
|
});
|
|
1207
1235
|
throw error;
|
|
1208
1236
|
});
|
|
1209
|
-
return wrappedPromise;
|
|
1237
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1210
1238
|
}
|
|
1211
1239
|
}
|
|
1212
1240
|
};
|
|
@@ -1225,7 +1253,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1225
1253
|
const startTime = Date.now();
|
|
1226
1254
|
const parentPromise = super.create(openAIParams, options);
|
|
1227
1255
|
if (openAIParams.stream) {
|
|
1228
|
-
|
|
1256
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1229
1257
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1230
1258
|
const [stream1, stream2] = value.tee();
|
|
1231
1259
|
(async () => {
|
|
@@ -1330,6 +1358,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1330
1358
|
}
|
|
1331
1359
|
return value;
|
|
1332
1360
|
});
|
|
1361
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1333
1362
|
} else {
|
|
1334
1363
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1335
1364
|
if ('output' in result) {
|
|
@@ -1338,7 +1367,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1338
1367
|
const formattedOutput = formatResponseOpenAI({
|
|
1339
1368
|
output: result.output
|
|
1340
1369
|
});
|
|
1341
|
-
await
|
|
1370
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1342
1371
|
...posthogParams,
|
|
1343
1372
|
model: openAIParams.model ?? result.model,
|
|
1344
1373
|
provider: 'openai',
|
|
@@ -1385,7 +1414,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1385
1414
|
});
|
|
1386
1415
|
throw error;
|
|
1387
1416
|
});
|
|
1388
|
-
return wrappedPromise;
|
|
1417
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1389
1418
|
}
|
|
1390
1419
|
}
|
|
1391
1420
|
parse(body, options) {
|
|
@@ -1444,7 +1473,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1444
1473
|
});
|
|
1445
1474
|
throw error;
|
|
1446
1475
|
});
|
|
1447
|
-
return wrappedPromise;
|
|
1476
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1448
1477
|
} finally {
|
|
1449
1478
|
// Restore our wrapped create method
|
|
1450
1479
|
originalSelfRecord['create'] = tempCreate;
|
|
@@ -1505,7 +1534,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
|
1505
1534
|
});
|
|
1506
1535
|
throw error;
|
|
1507
1536
|
});
|
|
1508
|
-
return wrappedPromise;
|
|
1537
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1509
1538
|
}
|
|
1510
1539
|
};
|
|
1511
1540
|
class WrappedAudio extends Audio {
|
|
@@ -1529,7 +1558,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1529
1558
|
const startTime = Date.now();
|
|
1530
1559
|
const parentPromise = openAIParams.stream ? super.create(openAIParams, options) : super.create(openAIParams, options);
|
|
1531
1560
|
if (openAIParams.stream) {
|
|
1532
|
-
|
|
1561
|
+
const wrappedPromise = parentPromise.then(value => {
|
|
1533
1562
|
if ('tee' in value && typeof value.tee === 'function') {
|
|
1534
1563
|
const [stream1, stream2] = value.tee();
|
|
1535
1564
|
(async () => {
|
|
@@ -1597,11 +1626,12 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1597
1626
|
}
|
|
1598
1627
|
return value;
|
|
1599
1628
|
});
|
|
1629
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1600
1630
|
} else {
|
|
1601
1631
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1602
|
-
if ('text' in result) {
|
|
1632
|
+
if (result && typeof result === 'object' && 'text' in result) {
|
|
1603
1633
|
const latency = (Date.now() - startTime) / 1000;
|
|
1604
|
-
await
|
|
1634
|
+
await captureAiGenerationAfterSuccess(this.phClient, {
|
|
1605
1635
|
...posthogParams,
|
|
1606
1636
|
model: openAIParams.model,
|
|
1607
1637
|
provider: 'openai',
|
|
@@ -1617,8 +1647,8 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1617
1647
|
rawUsage: result.usage
|
|
1618
1648
|
}
|
|
1619
1649
|
});
|
|
1620
|
-
return result;
|
|
1621
1650
|
}
|
|
1651
|
+
return result;
|
|
1622
1652
|
}, async error => {
|
|
1623
1653
|
await captureAiGeneration(this.phClient, {
|
|
1624
1654
|
...posthogParams,
|
|
@@ -1637,7 +1667,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1637
1667
|
});
|
|
1638
1668
|
throw error;
|
|
1639
1669
|
});
|
|
1640
|
-
return wrappedPromise;
|
|
1670
|
+
return preserveAPIPromiseHelpers(parentPromise, wrappedPromise);
|
|
1641
1671
|
}
|
|
1642
1672
|
}
|
|
1643
1673
|
}
|