@posthog/ai 8.9.1 → 8.9.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 +28 -24
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +28 -24
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +58 -51
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +58 -51
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +22 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +22 -20
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +1 -1
- package/dist/langchain/index.mjs +1 -1
- package/dist/langchain/middleware/index.cjs +1 -1
- package/dist/langchain/middleware/index.mjs +1 -1
- package/dist/openai/index.cjs +70 -58
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +70 -58
- 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 +23 -20
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +23 -20
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/openai/index.cjs
CHANGED
|
@@ -581,7 +581,7 @@ function formatOpenAIResponsesInput(input, instructions) {
|
|
|
581
581
|
return messages;
|
|
582
582
|
}
|
|
583
583
|
|
|
584
|
-
var version = "8.9.
|
|
584
|
+
var version = "8.9.2";
|
|
585
585
|
|
|
586
586
|
const DEFAULT_MAX_DEPTH = 3;
|
|
587
587
|
const MAX_STACK_LINES = 20;
|
|
@@ -738,15 +738,22 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
738
738
|
};
|
|
739
739
|
}
|
|
740
740
|
httpStatus = httpStatus ?? 200;
|
|
741
|
-
|
|
741
|
+
|
|
742
|
+
// A configured price applies only to a count the provider reported, so a call with no
|
|
743
|
+
// reported usage sends no cost instead of asserting $0. $ai_total_cost_usd sums the sides
|
|
744
|
+
// that were priced, which makes it the cost of the known side alone when the other side
|
|
745
|
+
// went unreported: a lower bound on the true total, not an assertion of it.
|
|
746
|
+
const costOverrideData = {};
|
|
742
747
|
if (options.costOverride) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
748
|
+
if (usage.inputTokens !== undefined) {
|
|
749
|
+
costOverrideData.$ai_input_cost_usd = (options.costOverride.inputCost ?? 0) * usage.inputTokens;
|
|
750
|
+
}
|
|
751
|
+
if (usage.outputTokens !== undefined) {
|
|
752
|
+
costOverrideData.$ai_output_cost_usd = (options.costOverride.outputCost ?? 0) * usage.outputTokens;
|
|
753
|
+
}
|
|
754
|
+
if (Object.keys(costOverrideData).length > 0) {
|
|
755
|
+
costOverrideData.$ai_total_cost_usd = (costOverrideData.$ai_input_cost_usd ?? 0) + (costOverrideData.$ai_output_cost_usd ?? 0);
|
|
756
|
+
}
|
|
750
757
|
}
|
|
751
758
|
|
|
752
759
|
// The caller's own token counts override the SDK-derived ones further down, via the
|
|
@@ -790,7 +797,9 @@ const captureAiGeneration$1 = async (client, options) => {
|
|
|
790
797
|
$ai_input: safeInput,
|
|
791
798
|
$ai_output_choices: safeOutput,
|
|
792
799
|
$ai_http_status: httpStatus,
|
|
793
|
-
|
|
800
|
+
...(usage.inputTokens !== undefined ? {
|
|
801
|
+
$ai_input_tokens: usage.inputTokens
|
|
802
|
+
} : {}),
|
|
794
803
|
...(usage.outputTokens !== undefined ? {
|
|
795
804
|
$ai_output_tokens: usage.outputTokens
|
|
796
805
|
} : {}),
|
|
@@ -1343,8 +1352,6 @@ function monitoredStreamTee(source, createStream) {
|
|
|
1343
1352
|
class OpenAIChatStreamAccumulator {
|
|
1344
1353
|
accumulatedContent = '';
|
|
1345
1354
|
usage = {
|
|
1346
|
-
inputTokens: 0,
|
|
1347
|
-
outputTokens: 0,
|
|
1348
1355
|
webSearchCount: 0
|
|
1349
1356
|
};
|
|
1350
1357
|
toolCalls = new Map();
|
|
@@ -1446,8 +1453,6 @@ class OpenAIChatStreamAccumulator {
|
|
|
1446
1453
|
class OpenAIResponsesStreamAccumulator {
|
|
1447
1454
|
output = [];
|
|
1448
1455
|
usage = {
|
|
1449
|
-
inputTokens: 0,
|
|
1450
|
-
outputTokens: 0,
|
|
1451
1456
|
webSearchCount: 0
|
|
1452
1457
|
};
|
|
1453
1458
|
consume(event, receivedAt = Date.now()) {
|
|
@@ -1556,20 +1561,17 @@ function buildChatSuccessOptions(context, result) {
|
|
|
1556
1561
|
})
|
|
1557
1562
|
};
|
|
1558
1563
|
}
|
|
1559
|
-
function buildChatErrorOptions(context, error, metadata
|
|
1564
|
+
function buildChatErrorOptions(context, error, metadata) {
|
|
1560
1565
|
return {
|
|
1561
1566
|
...context.monitoring,
|
|
1562
1567
|
model: context.params.model,
|
|
1563
1568
|
provider: context.provider,
|
|
1564
1569
|
input: sanitizeOpenAI(context.params.messages, context.client),
|
|
1565
1570
|
output: [],
|
|
1566
|
-
latency:
|
|
1571
|
+
latency: metadata.latency,
|
|
1567
1572
|
baseURL: context.baseURL,
|
|
1568
1573
|
modelParameters: getModelParams(context.modelParametersSource),
|
|
1569
|
-
usage: {
|
|
1570
|
-
inputTokens: 0,
|
|
1571
|
-
outputTokens: 0
|
|
1572
|
-
},
|
|
1574
|
+
usage: metadata.usage ?? {},
|
|
1573
1575
|
completionId: metadata.completionId,
|
|
1574
1576
|
providerMetadata: buildProviderMetadata({
|
|
1575
1577
|
systemFingerprint: metadata.systemFingerprint
|
|
@@ -1619,21 +1621,18 @@ function buildBackgroundResponseOptions(context, response) {
|
|
|
1619
1621
|
includeRequestId: true
|
|
1620
1622
|
});
|
|
1621
1623
|
}
|
|
1622
|
-
function buildResponsesErrorOptions(context, error,
|
|
1624
|
+
function buildResponsesErrorOptions(context, error, metadata) {
|
|
1623
1625
|
return {
|
|
1624
1626
|
...context.monitoring,
|
|
1625
1627
|
model: context.params.model,
|
|
1626
1628
|
provider: context.provider,
|
|
1627
1629
|
input: buildSanitizedResponsesInput(context),
|
|
1628
1630
|
output: [],
|
|
1629
|
-
latency:
|
|
1631
|
+
latency: metadata.latency,
|
|
1630
1632
|
baseURL: context.baseURL,
|
|
1631
1633
|
modelParameters: getModelParams(context.modelParametersSource),
|
|
1632
|
-
usage: {
|
|
1633
|
-
|
|
1634
|
-
outputTokens: 0
|
|
1635
|
-
},
|
|
1636
|
-
completionId,
|
|
1634
|
+
usage: metadata.usage ?? {},
|
|
1635
|
+
completionId: metadata.completionId,
|
|
1637
1636
|
error
|
|
1638
1637
|
};
|
|
1639
1638
|
}
|
|
@@ -1655,7 +1654,7 @@ function buildEmbeddingSuccessOptions(context, usage, latency) {
|
|
|
1655
1654
|
}
|
|
1656
1655
|
};
|
|
1657
1656
|
}
|
|
1658
|
-
function buildEmbeddingErrorOptions(context, error) {
|
|
1657
|
+
function buildEmbeddingErrorOptions(context, error, latency) {
|
|
1659
1658
|
return {
|
|
1660
1659
|
eventType: AIEvent.Embedding,
|
|
1661
1660
|
...context.monitoring,
|
|
@@ -1663,12 +1662,10 @@ function buildEmbeddingErrorOptions(context, error) {
|
|
|
1663
1662
|
provider: context.provider,
|
|
1664
1663
|
input: withPrivacyMode(context.client, context.monitoring.privacyMode, context.params.input),
|
|
1665
1664
|
output: null,
|
|
1666
|
-
latency
|
|
1665
|
+
latency,
|
|
1667
1666
|
baseURL: context.baseURL,
|
|
1668
1667
|
modelParameters: getModelParams(context.modelParametersSource),
|
|
1669
|
-
usage: {
|
|
1670
|
-
inputTokens: 0
|
|
1671
|
-
},
|
|
1668
|
+
usage: {},
|
|
1672
1669
|
error
|
|
1673
1670
|
};
|
|
1674
1671
|
}
|
|
@@ -1747,7 +1744,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
|
|
|
1747
1744
|
modelParametersSource: body
|
|
1748
1745
|
}, error, {
|
|
1749
1746
|
completionId: accumulated.completionId,
|
|
1750
|
-
systemFingerprint: accumulated.systemFingerprint
|
|
1747
|
+
systemFingerprint: accumulated.systemFingerprint,
|
|
1748
|
+
usage: accumulated.usage,
|
|
1749
|
+
latency: (Date.now() - startTime) / 1000
|
|
1751
1750
|
}));
|
|
1752
1751
|
throw error;
|
|
1753
1752
|
}
|
|
@@ -1793,7 +1792,9 @@ let WrappedCompletions$1 = class WrappedCompletions extends openai.AzureOpenAI.C
|
|
|
1793
1792
|
params: openAIParams,
|
|
1794
1793
|
monitoring: posthogParams,
|
|
1795
1794
|
modelParametersSource: body
|
|
1796
|
-
}, error
|
|
1795
|
+
}, error, {
|
|
1796
|
+
latency: (Date.now() - startTime) / 1000
|
|
1797
|
+
}));
|
|
1797
1798
|
throw error;
|
|
1798
1799
|
});
|
|
1799
1800
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -1895,7 +1896,11 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
|
|
|
1895
1896
|
params: openAIParams,
|
|
1896
1897
|
monitoring: posthogParams,
|
|
1897
1898
|
modelParametersSource: body
|
|
1898
|
-
}, error,
|
|
1899
|
+
}, error, {
|
|
1900
|
+
completionId: accumulated.completionId,
|
|
1901
|
+
usage: accumulated.usage,
|
|
1902
|
+
latency: (Date.now() - startTime) / 1000
|
|
1903
|
+
}));
|
|
1899
1904
|
throw error;
|
|
1900
1905
|
}
|
|
1901
1906
|
})().catch(() => {
|
|
@@ -1943,7 +1948,9 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
|
|
|
1943
1948
|
params: openAIParams,
|
|
1944
1949
|
monitoring: posthogParams,
|
|
1945
1950
|
modelParametersSource: body
|
|
1946
|
-
}, error
|
|
1951
|
+
}, error, {
|
|
1952
|
+
latency: (Date.now() - startTime) / 1000
|
|
1953
|
+
}));
|
|
1947
1954
|
throw error;
|
|
1948
1955
|
});
|
|
1949
1956
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2035,7 +2042,9 @@ let WrappedResponses$1 = class WrappedResponses extends openai.AzureOpenAI.Respo
|
|
|
2035
2042
|
params: openAIParams,
|
|
2036
2043
|
monitoring: posthogParams,
|
|
2037
2044
|
modelParametersSource: body
|
|
2038
|
-
}, error
|
|
2045
|
+
}, error, {
|
|
2046
|
+
latency: (Date.now() - startTime) / 1000
|
|
2047
|
+
}));
|
|
2039
2048
|
throw error;
|
|
2040
2049
|
});
|
|
2041
2050
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2072,7 +2081,7 @@ let WrappedEmbeddings$1 = class WrappedEmbeddings extends openai.AzureOpenAI.Emb
|
|
|
2072
2081
|
params: openAIParams,
|
|
2073
2082
|
monitoring: posthogParams,
|
|
2074
2083
|
modelParametersSource: body
|
|
2075
|
-
}, error));
|
|
2084
|
+
}, error, (Date.now() - startTime) / 1000));
|
|
2076
2085
|
throw error;
|
|
2077
2086
|
});
|
|
2078
2087
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2160,7 +2169,9 @@ class WrappedCompletions extends Completions {
|
|
|
2160
2169
|
modelParametersSource: body
|
|
2161
2170
|
}, error, {
|
|
2162
2171
|
completionId: accumulated.completionId,
|
|
2163
|
-
systemFingerprint: accumulated.systemFingerprint
|
|
2172
|
+
systemFingerprint: accumulated.systemFingerprint,
|
|
2173
|
+
usage: accumulated.usage,
|
|
2174
|
+
latency: (Date.now() - startTime) / 1000
|
|
2164
2175
|
}));
|
|
2165
2176
|
throw error;
|
|
2166
2177
|
}
|
|
@@ -2206,7 +2217,9 @@ class WrappedCompletions extends Completions {
|
|
|
2206
2217
|
params: openAIParams,
|
|
2207
2218
|
monitoring: posthogParams,
|
|
2208
2219
|
modelParametersSource: body
|
|
2209
|
-
}, error
|
|
2220
|
+
}, error, {
|
|
2221
|
+
latency: (Date.now() - startTime) / 1000
|
|
2222
|
+
}));
|
|
2210
2223
|
throw error;
|
|
2211
2224
|
});
|
|
2212
2225
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2308,7 +2321,11 @@ class WrappedResponses extends Responses {
|
|
|
2308
2321
|
params: openAIParams,
|
|
2309
2322
|
monitoring: posthogParams,
|
|
2310
2323
|
modelParametersSource: body
|
|
2311
|
-
}, error,
|
|
2324
|
+
}, error, {
|
|
2325
|
+
completionId: accumulated.completionId,
|
|
2326
|
+
usage: accumulated.usage,
|
|
2327
|
+
latency: (Date.now() - startTime) / 1000
|
|
2328
|
+
}));
|
|
2312
2329
|
throw error;
|
|
2313
2330
|
}
|
|
2314
2331
|
})().catch(() => {
|
|
@@ -2356,7 +2373,9 @@ class WrappedResponses extends Responses {
|
|
|
2356
2373
|
params: openAIParams,
|
|
2357
2374
|
monitoring: posthogParams,
|
|
2358
2375
|
modelParametersSource: body
|
|
2359
|
-
}, error
|
|
2376
|
+
}, error, {
|
|
2377
|
+
latency: (Date.now() - startTime) / 1000
|
|
2378
|
+
}));
|
|
2360
2379
|
throw error;
|
|
2361
2380
|
});
|
|
2362
2381
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2448,7 +2467,9 @@ class WrappedResponses extends Responses {
|
|
|
2448
2467
|
params: openAIParams,
|
|
2449
2468
|
monitoring: posthogParams,
|
|
2450
2469
|
modelParametersSource: body
|
|
2451
|
-
}, error
|
|
2470
|
+
}, error, {
|
|
2471
|
+
latency: (Date.now() - startTime) / 1000
|
|
2472
|
+
}));
|
|
2452
2473
|
throw error;
|
|
2453
2474
|
});
|
|
2454
2475
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2485,7 +2506,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
2485
2506
|
params: openAIParams,
|
|
2486
2507
|
monitoring: posthogParams,
|
|
2487
2508
|
modelParametersSource: body
|
|
2488
|
-
}, error));
|
|
2509
|
+
}, error, (Date.now() - startTime) / 1000));
|
|
2489
2510
|
throw error;
|
|
2490
2511
|
});
|
|
2491
2512
|
return preserveProviderPromise(parentPromise, wrappedPromise);
|
|
@@ -2531,13 +2552,10 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
2531
2552
|
if (Symbol.asyncIterator in value) {
|
|
2532
2553
|
const [stream1, stream2] = monitoredStreamTee(value, (iterator, controller) => new streaming.Stream(iterator, controller));
|
|
2533
2554
|
(async () => {
|
|
2555
|
+
let usage = {};
|
|
2534
2556
|
try {
|
|
2535
2557
|
let finalContent = '';
|
|
2536
2558
|
let firstTokenTime;
|
|
2537
|
-
let usage = {
|
|
2538
|
-
inputTokens: 0,
|
|
2539
|
-
outputTokens: 0
|
|
2540
|
-
};
|
|
2541
2559
|
const doneEvent = 'transcript.text.done';
|
|
2542
2560
|
for await (const chunk of stream1) {
|
|
2543
2561
|
// Track first token on text delta events
|
|
@@ -2579,13 +2597,10 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
2579
2597
|
provider: 'openai',
|
|
2580
2598
|
input: openAIParams.prompt,
|
|
2581
2599
|
output: [],
|
|
2582
|
-
latency:
|
|
2600
|
+
latency: (Date.now() - startTime) / 1000,
|
|
2583
2601
|
baseURL: this.baseURL,
|
|
2584
2602
|
modelParameters: getModelParams(body),
|
|
2585
|
-
usage
|
|
2586
|
-
inputTokens: 0,
|
|
2587
|
-
outputTokens: 0
|
|
2588
|
-
},
|
|
2603
|
+
usage,
|
|
2589
2604
|
error
|
|
2590
2605
|
});
|
|
2591
2606
|
throw error;
|
|
@@ -2628,13 +2643,10 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
2628
2643
|
provider: 'openai',
|
|
2629
2644
|
input: openAIParams.prompt,
|
|
2630
2645
|
output: [],
|
|
2631
|
-
latency:
|
|
2646
|
+
latency: (Date.now() - startTime) / 1000,
|
|
2632
2647
|
baseURL: this.baseURL,
|
|
2633
2648
|
modelParameters: getModelParams(body),
|
|
2634
|
-
usage: {
|
|
2635
|
-
inputTokens: 0,
|
|
2636
|
-
outputTokens: 0
|
|
2637
|
-
},
|
|
2649
|
+
usage: {},
|
|
2638
2650
|
error
|
|
2639
2651
|
});
|
|
2640
2652
|
throw error;
|