@posthog/ai 7.11.2 → 7.12.1
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/README.md +16 -33
- 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 +29 -841
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +29 -62
- package/dist/index.mjs +29 -839
- 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 +1 -1
- package/dist/openai/index.mjs +1 -1
- package/dist/otel/index.cjs +30 -1128
- package/dist/otel/index.cjs.map +1 -1
- package/dist/otel/index.d.ts +29 -72
- package/dist/otel/index.mjs +30 -1125
- package/dist/otel/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +1 -1
- package/dist/vercel/index.mjs +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -2,10 +2,11 @@ import { OpenAI, AzureOpenAI } from 'openai';
|
|
|
2
2
|
import * as uuid from 'uuid';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
import { uuidv7 } from '@posthog/core';
|
|
5
|
+
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
|
|
5
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
6
7
|
import { GoogleGenAI } from '@google/genai';
|
|
7
8
|
|
|
8
|
-
var version = "7.
|
|
9
|
+
var version = "7.12.1";
|
|
9
10
|
|
|
10
11
|
// Type guards for safer type checking
|
|
11
12
|
const isString = value => {
|
|
@@ -2611,847 +2612,36 @@ const wrapVercelLanguageModel = (model, phClient, options) => {
|
|
|
2611
2612
|
return wrappedModel;
|
|
2612
2613
|
};
|
|
2613
2614
|
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
const parsed = Number(value);
|
|
2635
|
-
if (Number.isFinite(parsed)) {
|
|
2636
|
-
return parsed;
|
|
2637
|
-
}
|
|
2638
|
-
}
|
|
2639
|
-
return undefined;
|
|
2640
|
-
}
|
|
2641
|
-
function toStringValue(value) {
|
|
2642
|
-
return typeof value === 'string' ? value : undefined;
|
|
2643
|
-
}
|
|
2644
|
-
function toStringArray(value) {
|
|
2645
|
-
if (!Array.isArray(value)) {
|
|
2646
|
-
return [];
|
|
2647
|
-
}
|
|
2648
|
-
return value.filter(item => typeof item === 'string');
|
|
2649
|
-
}
|
|
2650
|
-
function toSafeBinaryData(value) {
|
|
2651
|
-
const asString = typeof value === 'string' ? value : JSON.stringify(value ?? '');
|
|
2652
|
-
return truncate(redactBase64DataUrl(asString));
|
|
2653
|
-
}
|
|
2654
|
-
function toMimeType(value) {
|
|
2655
|
-
return typeof value === 'string' && value.length > 0 ? value : 'application/octet-stream';
|
|
2656
|
-
}
|
|
2657
|
-
function getSpanLatencySeconds(span) {
|
|
2658
|
-
const duration = span.duration;
|
|
2659
|
-
if (!duration || !Array.isArray(duration) || duration.length !== 2) {
|
|
2660
|
-
return 0;
|
|
2661
|
-
}
|
|
2662
|
-
const seconds = Number(duration[0]) || 0;
|
|
2663
|
-
const nanos = Number(duration[1]) || 0;
|
|
2664
|
-
return seconds + nanos / 1000000000;
|
|
2665
|
-
}
|
|
2666
|
-
function getOperationId(span) {
|
|
2667
|
-
const attributes = span.attributes || {};
|
|
2668
|
-
const operationId = toStringValue(attributes['ai.operationId']);
|
|
2669
|
-
if (operationId) {
|
|
2670
|
-
return operationId;
|
|
2671
|
-
}
|
|
2672
|
-
return span.name || '';
|
|
2673
|
-
}
|
|
2674
|
-
function isDoGenerateSpan(operationId) {
|
|
2675
|
-
return operationId.endsWith('.doGenerate');
|
|
2676
|
-
}
|
|
2677
|
-
function isDoStreamSpan(operationId) {
|
|
2678
|
-
return operationId.endsWith('.doStream');
|
|
2679
|
-
}
|
|
2680
|
-
function isDoEmbedSpan(operationId) {
|
|
2681
|
-
return operationId.endsWith('.doEmbed');
|
|
2682
|
-
}
|
|
2683
|
-
function shouldMapAiSdkSpan(span) {
|
|
2684
|
-
const operationId = getOperationId(span);
|
|
2685
|
-
return isDoGenerateSpan(operationId) || isDoStreamSpan(operationId) || isDoEmbedSpan(operationId);
|
|
2686
|
-
}
|
|
2687
|
-
function extractAiSdkTelemetryMetadata(attributes) {
|
|
2688
|
-
const metadata = {};
|
|
2689
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
2690
|
-
if (key.startsWith(AI_TELEMETRY_METADATA_PREFIX)) {
|
|
2691
|
-
metadata[key.slice(AI_TELEMETRY_METADATA_PREFIX.length)] = value;
|
|
2692
|
-
}
|
|
2693
|
-
}
|
|
2694
|
-
if (metadata.traceId && typeof metadata.traceId === 'string') {
|
|
2695
|
-
metadata.trace_id = metadata.traceId;
|
|
2696
|
-
}
|
|
2697
|
-
return metadata;
|
|
2698
|
-
}
|
|
2699
|
-
function mapPromptMessagesInput(attributes) {
|
|
2700
|
-
const promptMessages = parseJsonValue(attributes['ai.prompt.messages']) || [];
|
|
2701
|
-
if (!Array.isArray(promptMessages)) {
|
|
2702
|
-
return [];
|
|
2703
|
-
}
|
|
2704
|
-
return promptMessages.map(message => {
|
|
2705
|
-
const role = typeof message?.role === 'string' ? message.role : 'user';
|
|
2706
|
-
const content = message?.content;
|
|
2707
|
-
if (typeof content === 'string') {
|
|
2708
|
-
return {
|
|
2709
|
-
role,
|
|
2710
|
-
content: [{
|
|
2711
|
-
type: 'text',
|
|
2712
|
-
text: truncate(content)
|
|
2713
|
-
}]
|
|
2714
|
-
};
|
|
2715
|
-
}
|
|
2716
|
-
if (Array.isArray(content)) {
|
|
2717
|
-
return {
|
|
2718
|
-
role,
|
|
2719
|
-
content: content.map(part => {
|
|
2720
|
-
if (part && typeof part === 'object' && 'type' in part) {
|
|
2721
|
-
const typedPart = part;
|
|
2722
|
-
if (typedPart.type === 'text' && typeof typedPart.text === 'string') {
|
|
2723
|
-
return {
|
|
2724
|
-
type: 'text',
|
|
2725
|
-
text: truncate(typedPart.text)
|
|
2726
|
-
};
|
|
2727
|
-
}
|
|
2728
|
-
return typedPart;
|
|
2729
|
-
}
|
|
2730
|
-
return {
|
|
2731
|
-
type: 'text',
|
|
2732
|
-
text: truncate(String(part))
|
|
2733
|
-
};
|
|
2734
|
-
})
|
|
2735
|
-
};
|
|
2736
|
-
}
|
|
2737
|
-
return {
|
|
2738
|
-
role,
|
|
2739
|
-
content: [{
|
|
2740
|
-
type: 'text',
|
|
2741
|
-
text: truncate(content)
|
|
2742
|
-
}]
|
|
2743
|
-
};
|
|
2744
|
-
});
|
|
2745
|
-
}
|
|
2746
|
-
function mapPromptInput(attributes, operationId) {
|
|
2747
|
-
if (isDoEmbedSpan(operationId)) {
|
|
2748
|
-
if (attributes['ai.values'] !== undefined) {
|
|
2749
|
-
return attributes['ai.values'];
|
|
2750
|
-
}
|
|
2751
|
-
return attributes['ai.value'] ?? null;
|
|
2752
|
-
}
|
|
2753
|
-
const promptMessages = mapPromptMessagesInput(attributes);
|
|
2754
|
-
if (promptMessages.length > 0) {
|
|
2755
|
-
return promptMessages;
|
|
2756
|
-
}
|
|
2757
|
-
if (attributes['ai.prompt'] !== undefined) {
|
|
2758
|
-
return [{
|
|
2759
|
-
role: 'user',
|
|
2760
|
-
content: [{
|
|
2761
|
-
type: 'text',
|
|
2762
|
-
text: truncate(attributes['ai.prompt'])
|
|
2763
|
-
}]
|
|
2764
|
-
}];
|
|
2765
|
-
}
|
|
2766
|
-
return [];
|
|
2767
|
-
}
|
|
2768
|
-
function mapOutputPart(part) {
|
|
2769
|
-
const partType = toStringValue(part.type);
|
|
2770
|
-
if (partType === 'text' && typeof part.text === 'string') {
|
|
2771
|
-
return {
|
|
2772
|
-
type: 'text',
|
|
2773
|
-
text: truncate(part.text)
|
|
2774
|
-
};
|
|
2775
|
-
}
|
|
2776
|
-
if (partType === 'tool-call') {
|
|
2777
|
-
const toolName = toStringValue(part.toolName) || toStringValue(part.function?.name) || '';
|
|
2778
|
-
const toolCallId = toStringValue(part.toolCallId) || toStringValue(part.id) || '';
|
|
2779
|
-
const input = 'input' in part ? part.input : part.function?.arguments;
|
|
2780
|
-
if (toolName) {
|
|
2781
|
-
return {
|
|
2782
|
-
type: 'tool-call',
|
|
2783
|
-
id: toolCallId,
|
|
2784
|
-
function: {
|
|
2785
|
-
name: toolName,
|
|
2786
|
-
arguments: typeof input === 'string' ? input : JSON.stringify(input ?? {})
|
|
2787
|
-
}
|
|
2788
|
-
};
|
|
2789
|
-
}
|
|
2790
|
-
}
|
|
2791
|
-
if (partType === 'file') {
|
|
2792
|
-
const mediaType = toMimeType(part.mediaType ?? part.mimeType ?? part.contentType);
|
|
2793
|
-
const data = part.data ?? part.base64 ?? part.bytes ?? part.url ?? part.uri;
|
|
2794
|
-
if (data !== undefined) {
|
|
2795
|
-
return {
|
|
2796
|
-
type: 'file',
|
|
2797
|
-
name: 'generated_file',
|
|
2798
|
-
mediaType,
|
|
2799
|
-
data: toSafeBinaryData(data)
|
|
2800
|
-
};
|
|
2801
|
-
}
|
|
2802
|
-
}
|
|
2803
|
-
if (partType === 'image') {
|
|
2804
|
-
const mediaType = toMimeType(part.mediaType ?? part.mimeType ?? part.contentType ?? 'image/unknown');
|
|
2805
|
-
const data = part.data ?? part.base64 ?? part.bytes ?? part.url ?? part.uri ?? part.image ?? part.image_url;
|
|
2806
|
-
if (data !== undefined) {
|
|
2807
|
-
return {
|
|
2808
|
-
type: 'file',
|
|
2809
|
-
name: 'generated_file',
|
|
2810
|
-
mediaType,
|
|
2811
|
-
data: toSafeBinaryData(data)
|
|
2812
|
-
};
|
|
2813
|
-
}
|
|
2814
|
-
}
|
|
2815
|
-
const inlineData = part.inlineData ?? part.inline_data;
|
|
2816
|
-
if (inlineData && typeof inlineData === 'object' && inlineData.data !== undefined) {
|
|
2817
|
-
const mediaType = toMimeType(inlineData.mimeType ?? inlineData.mime_type);
|
|
2818
|
-
return {
|
|
2819
|
-
type: 'file',
|
|
2820
|
-
name: 'generated_file',
|
|
2821
|
-
mediaType,
|
|
2822
|
-
data: toSafeBinaryData(inlineData.data)
|
|
2823
|
-
};
|
|
2824
|
-
}
|
|
2825
|
-
if (partType === 'object' && part.object !== undefined) {
|
|
2826
|
-
return {
|
|
2827
|
-
type: 'object',
|
|
2828
|
-
object: part.object
|
|
2829
|
-
};
|
|
2830
|
-
}
|
|
2831
|
-
return null;
|
|
2832
|
-
}
|
|
2833
|
-
function mapResponseMessagesOutput(attributes) {
|
|
2834
|
-
const messagesRaw = parseJsonValue(attributes['ai.response.messages']) ?? parseJsonValue(attributes['ai.response.message']);
|
|
2835
|
-
if (!messagesRaw) {
|
|
2836
|
-
return [];
|
|
2837
|
-
}
|
|
2838
|
-
const messages = Array.isArray(messagesRaw) ? messagesRaw : [messagesRaw];
|
|
2839
|
-
const mappedMessages = [];
|
|
2840
|
-
for (const message of messages) {
|
|
2841
|
-
if (!message || typeof message !== 'object') {
|
|
2842
|
-
continue;
|
|
2843
|
-
}
|
|
2844
|
-
const role = toStringValue(message.role) || 'assistant';
|
|
2845
|
-
const content = message.content;
|
|
2846
|
-
if (typeof content === 'string') {
|
|
2847
|
-
mappedMessages.push({
|
|
2848
|
-
role,
|
|
2849
|
-
content: [{
|
|
2850
|
-
type: 'text',
|
|
2851
|
-
text: truncate(content)
|
|
2852
|
-
}]
|
|
2853
|
-
});
|
|
2854
|
-
continue;
|
|
2855
|
-
}
|
|
2856
|
-
if (Array.isArray(content)) {
|
|
2857
|
-
const parts = content.map(part => part && typeof part === 'object' ? mapOutputPart(part) : null).filter(part => part !== null);
|
|
2858
|
-
if (parts.length > 0) {
|
|
2859
|
-
mappedMessages.push({
|
|
2860
|
-
role,
|
|
2861
|
-
content: parts
|
|
2862
|
-
});
|
|
2863
|
-
}
|
|
2864
|
-
continue;
|
|
2865
|
-
}
|
|
2866
|
-
}
|
|
2867
|
-
return mappedMessages;
|
|
2868
|
-
}
|
|
2869
|
-
function mapTextToolObjectOutputParts(attributes) {
|
|
2870
|
-
const responseText = toStringValue(attributes['ai.response.text']) || '';
|
|
2871
|
-
const toolCalls = parseJsonValue(attributes['ai.response.toolCalls']) || [];
|
|
2872
|
-
const responseObjectRaw = attributes['ai.response.object'];
|
|
2873
|
-
const responseObject = parseJsonValue(responseObjectRaw);
|
|
2874
|
-
const contentParts = [];
|
|
2875
|
-
if (responseText) {
|
|
2876
|
-
contentParts.push({
|
|
2877
|
-
type: 'text',
|
|
2878
|
-
text: truncate(responseText)
|
|
2879
|
-
});
|
|
2880
|
-
}
|
|
2881
|
-
if (responseObjectRaw !== undefined) {
|
|
2882
|
-
contentParts.push({
|
|
2883
|
-
type: 'object',
|
|
2884
|
-
object: responseObject ?? responseObjectRaw
|
|
2885
|
-
});
|
|
2886
|
-
}
|
|
2887
|
-
if (Array.isArray(toolCalls)) {
|
|
2888
|
-
for (const toolCall of toolCalls) {
|
|
2889
|
-
if (!toolCall || typeof toolCall !== 'object') {
|
|
2890
|
-
continue;
|
|
2891
|
-
}
|
|
2892
|
-
const toolName = typeof toolCall.toolName === 'string' ? toolCall.toolName : '';
|
|
2893
|
-
const toolCallId = typeof toolCall.toolCallId === 'string' ? toolCall.toolCallId : '';
|
|
2894
|
-
if (!toolName) {
|
|
2895
|
-
continue;
|
|
2896
|
-
}
|
|
2897
|
-
const input = 'input' in toolCall ? toolCall.input : {};
|
|
2898
|
-
contentParts.push({
|
|
2899
|
-
type: 'tool-call',
|
|
2900
|
-
id: toolCallId,
|
|
2901
|
-
function: {
|
|
2902
|
-
name: toolName,
|
|
2903
|
-
arguments: typeof input === 'string' ? input : JSON.stringify(input)
|
|
2904
|
-
}
|
|
2905
|
-
});
|
|
2906
|
-
}
|
|
2907
|
-
}
|
|
2908
|
-
return contentParts;
|
|
2909
|
-
}
|
|
2910
|
-
function mapResponseFilesOutput(attributes) {
|
|
2911
|
-
const responseFiles = parseJsonValue(attributes['ai.response.files']) || [];
|
|
2912
|
-
if (!Array.isArray(responseFiles)) {
|
|
2913
|
-
return [];
|
|
2914
|
-
}
|
|
2915
|
-
const mapped = [];
|
|
2916
|
-
for (const file of responseFiles) {
|
|
2917
|
-
if (!file || typeof file !== 'object') {
|
|
2918
|
-
continue;
|
|
2919
|
-
}
|
|
2920
|
-
const mimeType = toMimeType(file.mimeType ?? file.mediaType ?? file.contentType);
|
|
2921
|
-
const data = file.data ?? file.base64 ?? file.bytes;
|
|
2922
|
-
const url = typeof file.url === 'string' ? file.url : typeof file.uri === 'string' ? file.uri : undefined;
|
|
2923
|
-
if (data !== undefined) {
|
|
2924
|
-
mapped.push({
|
|
2925
|
-
type: 'file',
|
|
2926
|
-
name: 'generated_file',
|
|
2927
|
-
mediaType: mimeType,
|
|
2928
|
-
data: toSafeBinaryData(data)
|
|
2929
|
-
});
|
|
2930
|
-
continue;
|
|
2931
|
-
}
|
|
2932
|
-
if (url) {
|
|
2933
|
-
mapped.push({
|
|
2934
|
-
type: 'file',
|
|
2935
|
-
name: 'generated_file',
|
|
2936
|
-
mediaType: mimeType,
|
|
2937
|
-
data: truncate(url)
|
|
2938
|
-
});
|
|
2939
|
-
}
|
|
2940
|
-
}
|
|
2941
|
-
return mapped;
|
|
2942
|
-
}
|
|
2943
|
-
function extractGeminiParts(providerMetadata) {
|
|
2944
|
-
const parts = [];
|
|
2945
|
-
const visit = node => {
|
|
2946
|
-
if (!node || typeof node !== 'object') {
|
|
2947
|
-
return;
|
|
2948
|
-
}
|
|
2949
|
-
if (Array.isArray(node)) {
|
|
2950
|
-
for (const item of node) {
|
|
2951
|
-
visit(item);
|
|
2952
|
-
}
|
|
2953
|
-
return;
|
|
2954
|
-
}
|
|
2955
|
-
const objectNode = node;
|
|
2956
|
-
const maybeParts = objectNode.parts;
|
|
2957
|
-
if (Array.isArray(maybeParts)) {
|
|
2958
|
-
for (const part of maybeParts) {
|
|
2959
|
-
if (part && typeof part === 'object') {
|
|
2960
|
-
parts.push(part);
|
|
2961
|
-
}
|
|
2962
|
-
}
|
|
2963
|
-
}
|
|
2964
|
-
for (const value of Object.values(objectNode)) {
|
|
2965
|
-
visit(value);
|
|
2966
|
-
}
|
|
2967
|
-
};
|
|
2968
|
-
visit(providerMetadata);
|
|
2969
|
-
return parts;
|
|
2970
|
-
}
|
|
2971
|
-
function mapProviderMetadataInlineDataOutput(providerMetadata) {
|
|
2972
|
-
const parts = extractGeminiParts(providerMetadata);
|
|
2973
|
-
const mapped = [];
|
|
2974
|
-
for (const part of parts) {
|
|
2975
|
-
const inlineData = part.inlineData ?? part.inline_data;
|
|
2976
|
-
if (!inlineData || typeof inlineData !== 'object') {
|
|
2977
|
-
continue;
|
|
2978
|
-
}
|
|
2979
|
-
const mimeType = toMimeType(inlineData.mimeType ?? inlineData.mime_type);
|
|
2980
|
-
if (inlineData.data === undefined) {
|
|
2981
|
-
continue;
|
|
2982
|
-
}
|
|
2983
|
-
mapped.push({
|
|
2984
|
-
type: 'file',
|
|
2985
|
-
name: 'generated_file',
|
|
2986
|
-
mediaType: mimeType,
|
|
2987
|
-
data: toSafeBinaryData(inlineData.data)
|
|
2988
|
-
});
|
|
2989
|
-
}
|
|
2990
|
-
return mapped;
|
|
2991
|
-
}
|
|
2992
|
-
function mapProviderMetadataTextOutput(providerMetadata) {
|
|
2993
|
-
const parts = extractGeminiParts(providerMetadata);
|
|
2994
|
-
const mapped = [];
|
|
2995
|
-
for (const part of parts) {
|
|
2996
|
-
if (typeof part.text === 'string' && part.text.length > 0) {
|
|
2997
|
-
mapped.push({
|
|
2998
|
-
type: 'text',
|
|
2999
|
-
text: truncate(part.text)
|
|
3000
|
-
});
|
|
3001
|
-
}
|
|
3002
|
-
}
|
|
3003
|
-
return mapped;
|
|
3004
|
-
}
|
|
3005
|
-
function extractMediaBlocksFromUnknownNode(node) {
|
|
3006
|
-
const mapped = [];
|
|
3007
|
-
const visit = value => {
|
|
3008
|
-
if (!value || typeof value !== 'object') {
|
|
3009
|
-
return;
|
|
3010
|
-
}
|
|
3011
|
-
if (Array.isArray(value)) {
|
|
3012
|
-
for (const item of value) {
|
|
3013
|
-
visit(item);
|
|
3014
|
-
}
|
|
3015
|
-
return;
|
|
3016
|
-
}
|
|
3017
|
-
const objectValue = value;
|
|
3018
|
-
const inlineData = objectValue.inlineData ?? objectValue.inline_data;
|
|
3019
|
-
if (inlineData && typeof inlineData === 'object' && inlineData.data !== undefined) {
|
|
3020
|
-
const mediaType = toMimeType(inlineData.mimeType ?? inlineData.mime_type);
|
|
3021
|
-
mapped.push({
|
|
3022
|
-
type: 'file',
|
|
3023
|
-
name: 'generated_file',
|
|
3024
|
-
mediaType,
|
|
3025
|
-
data: toSafeBinaryData(inlineData.data)
|
|
3026
|
-
});
|
|
3027
|
-
}
|
|
3028
|
-
if ((objectValue.type === 'file' || 'mediaType' in objectValue || 'mimeType' in objectValue) && objectValue.data) {
|
|
3029
|
-
const mediaType = toMimeType(objectValue.mediaType ?? objectValue.mimeType);
|
|
3030
|
-
mapped.push({
|
|
3031
|
-
type: 'file',
|
|
3032
|
-
name: 'generated_file',
|
|
3033
|
-
mediaType,
|
|
3034
|
-
data: toSafeBinaryData(objectValue.data)
|
|
3035
|
-
});
|
|
3036
|
-
}
|
|
3037
|
-
for (const child of Object.values(objectValue)) {
|
|
3038
|
-
visit(child);
|
|
3039
|
-
}
|
|
3040
|
-
};
|
|
3041
|
-
visit(node);
|
|
3042
|
-
return mapped;
|
|
3043
|
-
}
|
|
3044
|
-
function mapUnknownResponseAttributeMediaOutput(attributes) {
|
|
3045
|
-
const mapped = [];
|
|
3046
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
3047
|
-
if (!key.startsWith('ai.response.')) {
|
|
3048
|
-
continue;
|
|
3049
|
-
}
|
|
3050
|
-
if (key === 'ai.response.text' || key === 'ai.response.toolCalls' || key === 'ai.response.object' || key === 'ai.response.files' || key === 'ai.response.message' || key === 'ai.response.messages' || key === 'ai.response.providerMetadata') {
|
|
3051
|
-
continue;
|
|
3052
|
-
}
|
|
3053
|
-
const parsed = typeof value === 'string' ? parseJsonValue(value) ?? value : value;
|
|
3054
|
-
mapped.push(...extractMediaBlocksFromUnknownNode(parsed));
|
|
3055
|
-
}
|
|
3056
|
-
return mapped;
|
|
3057
|
-
}
|
|
3058
|
-
function mapGenericResponseAttributeMediaOutput(attributes) {
|
|
3059
|
-
const mapped = [];
|
|
3060
|
-
for (const [key, value] of Object.entries(attributes)) {
|
|
3061
|
-
if (!key.includes('response') || key.startsWith('ai.response.') || key === 'ai.response.providerMetadata' || key.startsWith('ai.prompt.') || key.startsWith('gen_ai.request.')) {
|
|
3062
|
-
continue;
|
|
3063
|
-
}
|
|
3064
|
-
const parsed = typeof value === 'string' ? parseJsonValue(value) : value;
|
|
3065
|
-
if (parsed === null || parsed === undefined) {
|
|
3066
|
-
continue;
|
|
3067
|
-
}
|
|
3068
|
-
mapped.push(...extractMediaBlocksFromUnknownNode(parsed));
|
|
3069
|
-
}
|
|
3070
|
-
return mapped;
|
|
3071
|
-
}
|
|
3072
|
-
function dedupeContentParts(parts) {
|
|
3073
|
-
const seen = new Set();
|
|
3074
|
-
const deduped = [];
|
|
3075
|
-
for (const part of parts) {
|
|
3076
|
-
const key = JSON.stringify(part);
|
|
3077
|
-
if (seen.has(key)) {
|
|
3078
|
-
continue;
|
|
3079
|
-
}
|
|
3080
|
-
seen.add(key);
|
|
3081
|
-
deduped.push(part);
|
|
3082
|
-
}
|
|
3083
|
-
return deduped;
|
|
3084
|
-
}
|
|
3085
|
-
function mapOutput(attributes, operationId, providerMetadata) {
|
|
3086
|
-
if (isDoEmbedSpan(operationId)) {
|
|
3087
|
-
// Keep embedding behavior aligned with existing provider wrappers.
|
|
3088
|
-
return null;
|
|
3089
|
-
}
|
|
3090
|
-
const responseMessages = mapResponseMessagesOutput(attributes);
|
|
3091
|
-
if (responseMessages.length > 0) {
|
|
3092
|
-
return responseMessages;
|
|
3093
|
-
}
|
|
3094
|
-
const textToolObjectParts = mapTextToolObjectOutputParts(attributes);
|
|
3095
|
-
const responseFileParts = mapResponseFilesOutput(attributes);
|
|
3096
|
-
const unknownMediaParts = mapUnknownResponseAttributeMediaOutput(attributes);
|
|
3097
|
-
const genericResponseMediaParts = mapGenericResponseAttributeMediaOutput(attributes);
|
|
3098
|
-
const providerMetadataTextParts = mapProviderMetadataTextOutput(providerMetadata);
|
|
3099
|
-
const providerMetadataInlineParts = mapProviderMetadataInlineDataOutput(providerMetadata);
|
|
3100
|
-
const mergedContentParts = dedupeContentParts([...textToolObjectParts, ...responseFileParts, ...unknownMediaParts, ...genericResponseMediaParts, ...providerMetadataTextParts, ...providerMetadataInlineParts]);
|
|
3101
|
-
const contentParts = mergedContentParts;
|
|
3102
|
-
if (contentParts.length === 0) {
|
|
3103
|
-
return [];
|
|
3104
|
-
}
|
|
3105
|
-
return [{
|
|
3106
|
-
role: 'assistant',
|
|
3107
|
-
content: contentParts
|
|
3108
|
-
}];
|
|
3109
|
-
}
|
|
3110
|
-
function mapModelSettings(attributes, operationId) {
|
|
3111
|
-
const temperature = toNumber(attributes['ai.settings.temperature']) ?? toNumber(attributes['gen_ai.request.temperature']);
|
|
3112
|
-
const maxTokens = toNumber(attributes['ai.settings.maxTokens']) ?? toNumber(attributes['gen_ai.request.max_tokens']);
|
|
3113
|
-
const maxOutputTokens = toNumber(attributes['ai.settings.maxOutputTokens']);
|
|
3114
|
-
const topP = toNumber(attributes['ai.settings.topP']) ?? toNumber(attributes['gen_ai.request.top_p']);
|
|
3115
|
-
const frequencyPenalty = toNumber(attributes['ai.settings.frequencyPenalty']) ?? toNumber(attributes['gen_ai.request.frequency_penalty']);
|
|
3116
|
-
const presencePenalty = toNumber(attributes['ai.settings.presencePenalty']) ?? toNumber(attributes['gen_ai.request.presence_penalty']);
|
|
3117
|
-
const stopSequences = parseJsonValue(attributes['ai.settings.stopSequences']) ?? parseJsonValue(attributes['gen_ai.request.stop_sequences']);
|
|
3118
|
-
const stream = isDoStreamSpan(operationId);
|
|
3119
|
-
return {
|
|
3120
|
-
...(temperature !== undefined ? {
|
|
3121
|
-
temperature
|
|
3122
|
-
} : {}),
|
|
3123
|
-
...(maxTokens !== undefined ? {
|
|
3124
|
-
max_tokens: maxTokens
|
|
3125
|
-
} : {}),
|
|
3126
|
-
...(maxOutputTokens !== undefined ? {
|
|
3127
|
-
max_completion_tokens: maxOutputTokens
|
|
3128
|
-
} : {}),
|
|
3129
|
-
...(topP !== undefined ? {
|
|
3130
|
-
top_p: topP
|
|
3131
|
-
} : {}),
|
|
3132
|
-
...(frequencyPenalty !== undefined ? {
|
|
3133
|
-
frequency_penalty: frequencyPenalty
|
|
3134
|
-
} : {}),
|
|
3135
|
-
...(presencePenalty !== undefined ? {
|
|
3136
|
-
presence_penalty: presencePenalty
|
|
3137
|
-
} : {}),
|
|
3138
|
-
...(stopSequences !== null ? {
|
|
3139
|
-
stop: stopSequences
|
|
3140
|
-
} : {}),
|
|
3141
|
-
...(stream ? {
|
|
3142
|
-
stream: true
|
|
3143
|
-
} : {})
|
|
3144
|
-
};
|
|
3145
|
-
}
|
|
3146
|
-
function mapUsage(attributes, providerMetadata, operationId) {
|
|
3147
|
-
if (isDoEmbedSpan(operationId)) {
|
|
3148
|
-
const tokens = toNumber(attributes['ai.usage.tokens']) ?? toNumber(attributes['gen_ai.usage.input_tokens']) ?? 0;
|
|
3149
|
-
return {
|
|
3150
|
-
inputTokens: tokens,
|
|
3151
|
-
rawUsage: {
|
|
3152
|
-
usage: {
|
|
3153
|
-
tokens
|
|
3154
|
-
},
|
|
3155
|
-
providerMetadata
|
|
3156
|
-
}
|
|
3157
|
-
};
|
|
3158
|
-
}
|
|
3159
|
-
const inputTokens = toNumber(attributes['ai.usage.promptTokens']) ?? toNumber(attributes['gen_ai.usage.input_tokens']) ?? 0;
|
|
3160
|
-
const outputTokens = toNumber(attributes['ai.usage.completionTokens']) ?? toNumber(attributes['gen_ai.usage.output_tokens']) ?? 0;
|
|
3161
|
-
const totalTokens = toNumber(attributes['ai.usage.totalTokens']);
|
|
3162
|
-
const reasoningTokens = toNumber(attributes['ai.usage.reasoningTokens']);
|
|
3163
|
-
const cachedInputTokens = toNumber(attributes['ai.usage.cachedInputTokens']);
|
|
3164
|
-
return {
|
|
3165
|
-
inputTokens,
|
|
3166
|
-
outputTokens,
|
|
3167
|
-
...(reasoningTokens !== undefined ? {
|
|
3168
|
-
reasoningTokens
|
|
3169
|
-
} : {}),
|
|
3170
|
-
...(cachedInputTokens !== undefined ? {
|
|
3171
|
-
cacheReadInputTokens: cachedInputTokens
|
|
3172
|
-
} : {}),
|
|
3173
|
-
rawUsage: {
|
|
3174
|
-
usage: {
|
|
3175
|
-
promptTokens: inputTokens,
|
|
3176
|
-
completionTokens: outputTokens,
|
|
3177
|
-
...(totalTokens !== undefined ? {
|
|
3178
|
-
totalTokens
|
|
3179
|
-
} : {})
|
|
3180
|
-
},
|
|
3181
|
-
providerMetadata
|
|
2615
|
+
/**
|
|
2616
|
+
* An OpenTelemetry SpanExporter that sends traces to PostHog's OTLP
|
|
2617
|
+
* ingestion endpoint. PostHog converts `gen_ai.*` spans into
|
|
2618
|
+
* `$ai_generation` events server-side.
|
|
2619
|
+
*
|
|
2620
|
+
* @example
|
|
2621
|
+
* ```ts
|
|
2622
|
+
* import { PostHogTraceExporter } from '@posthog/ai/otel'
|
|
2623
|
+
* import { NodeSDK } from '@opentelemetry/sdk-node'
|
|
2624
|
+
*
|
|
2625
|
+
* const sdk = new NodeSDK({
|
|
2626
|
+
* traceExporter: new PostHogTraceExporter({ apiKey: 'phc_...' }),
|
|
2627
|
+
* })
|
|
2628
|
+
* sdk.start()
|
|
2629
|
+
* ```
|
|
2630
|
+
*/
|
|
2631
|
+
class PostHogTraceExporter extends OTLPTraceExporter {
|
|
2632
|
+
constructor(options) {
|
|
2633
|
+
if (!options.apiKey) {
|
|
2634
|
+
throw new Error('PostHogTraceExporter requires an apiKey');
|
|
3182
2635
|
}
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
}
|
|
3190
|
-
const parsedTools = [];
|
|
3191
|
-
for (const rawTool of rawTools) {
|
|
3192
|
-
if (typeof rawTool === 'string') {
|
|
3193
|
-
const parsed = parseJsonValue(rawTool);
|
|
3194
|
-
if (parsed !== null) {
|
|
3195
|
-
parsedTools.push(parsed);
|
|
2636
|
+
const host = new URL(options.host || 'https://us.i.posthog.com').origin;
|
|
2637
|
+
super({
|
|
2638
|
+
url: `${host}/i/v0/ai/otel`,
|
|
2639
|
+
headers: {
|
|
2640
|
+
// The OTLP ingestion endpoint authenticates using the project API key as a Bearer token
|
|
2641
|
+
Authorization: `Bearer ${options.apiKey}`
|
|
3196
2642
|
}
|
|
3197
|
-
continue;
|
|
3198
|
-
}
|
|
3199
|
-
if (rawTool && typeof rawTool === 'object') {
|
|
3200
|
-
parsedTools.push(rawTool);
|
|
3201
|
-
}
|
|
3202
|
-
}
|
|
3203
|
-
return parsedTools.length > 0 ? parsedTools : null;
|
|
3204
|
-
}
|
|
3205
|
-
function extractProviderMetadata(attributes) {
|
|
3206
|
-
const rawProviderMetadata = attributes['ai.response.providerMetadata'];
|
|
3207
|
-
return parseJsonValue(rawProviderMetadata) || {};
|
|
3208
|
-
}
|
|
3209
|
-
function getAiSdkFrameworkVersion(span) {
|
|
3210
|
-
const instrumentedSpan = span;
|
|
3211
|
-
const attributes = span.attributes || {};
|
|
3212
|
-
const instrumentationScopeVersion = toStringValue(instrumentedSpan.instrumentationScope?.version) || toStringValue(instrumentedSpan.instrumentationLibrary?.version);
|
|
3213
|
-
const aiUserAgent = toStringValue(attributes['ai.request.headers.user-agent']);
|
|
3214
|
-
const userAgentVersionMatch = aiUserAgent?.match(/\bai\/(\d+(?:\.\d+)*)\b/i);
|
|
3215
|
-
const userAgentVersion = userAgentVersionMatch?.[1];
|
|
3216
|
-
const rawVersion = instrumentationScopeVersion || userAgentVersion;
|
|
3217
|
-
if (!rawVersion) {
|
|
3218
|
-
return undefined;
|
|
3219
|
-
}
|
|
3220
|
-
const majorVersionMatch = rawVersion.match(/^v?(\d+)/i);
|
|
3221
|
-
return majorVersionMatch ? majorVersionMatch[1] : rawVersion;
|
|
3222
|
-
}
|
|
3223
|
-
function buildPosthogProperties(attributes, operationId) {
|
|
3224
|
-
const telemetryMetadata = extractAiSdkTelemetryMetadata(attributes);
|
|
3225
|
-
const finishReasons = toStringArray(parseJsonValue(attributes['gen_ai.response.finish_reasons']));
|
|
3226
|
-
const finishReason = toStringValue(attributes['ai.response.finishReason']) || finishReasons[0];
|
|
3227
|
-
const toolChoice = parseJsonValue(attributes['ai.prompt.toolChoice']) ?? attributes['ai.prompt.toolChoice'];
|
|
3228
|
-
return {
|
|
3229
|
-
...telemetryMetadata,
|
|
3230
|
-
$ai_framework: 'vercel',
|
|
3231
|
-
ai_operation_id: operationId,
|
|
3232
|
-
...(finishReason ? {
|
|
3233
|
-
ai_finish_reason: finishReason
|
|
3234
|
-
} : {}),
|
|
3235
|
-
...(toStringValue(attributes['ai.response.model']) ? {
|
|
3236
|
-
ai_response_model: attributes['ai.response.model']
|
|
3237
|
-
} : {}),
|
|
3238
|
-
...(toStringValue(attributes['gen_ai.response.model']) ? {
|
|
3239
|
-
ai_response_model: attributes['gen_ai.response.model']
|
|
3240
|
-
} : {}),
|
|
3241
|
-
...(toStringValue(attributes['ai.response.id']) ? {
|
|
3242
|
-
ai_response_id: attributes['ai.response.id']
|
|
3243
|
-
} : {}),
|
|
3244
|
-
...(toStringValue(attributes['gen_ai.response.id']) ? {
|
|
3245
|
-
ai_response_id: attributes['gen_ai.response.id']
|
|
3246
|
-
} : {}),
|
|
3247
|
-
...(toStringValue(attributes['ai.response.timestamp']) ? {
|
|
3248
|
-
ai_response_timestamp: attributes['ai.response.timestamp']
|
|
3249
|
-
} : {}),
|
|
3250
|
-
...(toNumber(attributes['ai.response.msToFinish']) !== undefined ? {
|
|
3251
|
-
ai_response_ms_to_finish: toNumber(attributes['ai.response.msToFinish'])
|
|
3252
|
-
} : {}),
|
|
3253
|
-
...(toNumber(attributes['ai.response.avgCompletionTokensPerSecond']) !== undefined ? {
|
|
3254
|
-
ai_response_avg_completion_tokens_per_second: toNumber(attributes['ai.response.avgCompletionTokensPerSecond'])
|
|
3255
|
-
} : {}),
|
|
3256
|
-
...(toStringValue(attributes['ai.telemetry.functionId']) ? {
|
|
3257
|
-
ai_telemetry_function_id: attributes['ai.telemetry.functionId']
|
|
3258
|
-
} : {}),
|
|
3259
|
-
...(toNumber(attributes['ai.settings.maxRetries']) !== undefined ? {
|
|
3260
|
-
ai_settings_max_retries: toNumber(attributes['ai.settings.maxRetries'])
|
|
3261
|
-
} : {}),
|
|
3262
|
-
...(toNumber(attributes['gen_ai.request.top_k']) !== undefined ? {
|
|
3263
|
-
ai_request_top_k: toNumber(attributes['gen_ai.request.top_k'])
|
|
3264
|
-
} : {}),
|
|
3265
|
-
...(attributes['ai.schema.name'] !== undefined ? {
|
|
3266
|
-
ai_schema_name: attributes['ai.schema.name']
|
|
3267
|
-
} : {}),
|
|
3268
|
-
...(attributes['ai.schema.description'] !== undefined ? {
|
|
3269
|
-
ai_schema_description: attributes['ai.schema.description']
|
|
3270
|
-
} : {}),
|
|
3271
|
-
...(attributes['ai.settings.output'] !== undefined ? {
|
|
3272
|
-
ai_settings_output: attributes['ai.settings.output']
|
|
3273
|
-
} : {}),
|
|
3274
|
-
...(toolChoice ? {
|
|
3275
|
-
ai_prompt_tool_choice: toolChoice
|
|
3276
|
-
} : {})
|
|
3277
|
-
};
|
|
3278
|
-
}
|
|
3279
|
-
function buildAiSdkMapperResult(span) {
|
|
3280
|
-
const attributes = span.attributes || {};
|
|
3281
|
-
const operationId = getOperationId(span);
|
|
3282
|
-
const providerMetadata = extractProviderMetadata(attributes);
|
|
3283
|
-
const model = toStringValue(attributes['ai.model.id']) || toStringValue(attributes['gen_ai.request.model']) || 'unknown';
|
|
3284
|
-
const provider = (toStringValue(attributes['ai.model.provider']) || toStringValue(attributes['gen_ai.system']) || 'unknown').toLowerCase();
|
|
3285
|
-
const latency = getSpanLatencySeconds(span);
|
|
3286
|
-
const timeToFirstTokenMs = toNumber(attributes['ai.response.msToFirstChunk']);
|
|
3287
|
-
const timeToFirstToken = timeToFirstTokenMs !== undefined ? timeToFirstTokenMs / 1000 : undefined;
|
|
3288
|
-
const input = mapPromptInput(attributes, operationId);
|
|
3289
|
-
const output = mapOutput(attributes, operationId, providerMetadata);
|
|
3290
|
-
const usage = mapUsage(attributes, providerMetadata, operationId);
|
|
3291
|
-
const modelParams = mapModelSettings(attributes, operationId);
|
|
3292
|
-
const tools = parsePromptTools(attributes);
|
|
3293
|
-
const httpStatus = toNumber(attributes['http.response.status_code']) || 200;
|
|
3294
|
-
const eventType = isDoEmbedSpan(operationId) ? AIEvent.Embedding : AIEvent.Generation;
|
|
3295
|
-
const frameworkVersion = getAiSdkFrameworkVersion(span);
|
|
3296
|
-
const error = span.status?.code === OTEL_STATUS_ERROR ? span.status.message || 'AI SDK span recorded error status' : undefined;
|
|
3297
|
-
return {
|
|
3298
|
-
model,
|
|
3299
|
-
provider,
|
|
3300
|
-
input,
|
|
3301
|
-
output,
|
|
3302
|
-
latency,
|
|
3303
|
-
timeToFirstToken,
|
|
3304
|
-
httpStatus,
|
|
3305
|
-
eventType,
|
|
3306
|
-
usage,
|
|
3307
|
-
tools,
|
|
3308
|
-
modelParams,
|
|
3309
|
-
posthogProperties: {
|
|
3310
|
-
...buildPosthogProperties(attributes, operationId),
|
|
3311
|
-
...(frameworkVersion ? {
|
|
3312
|
-
$ai_framework_version: frameworkVersion
|
|
3313
|
-
} : {})
|
|
3314
|
-
},
|
|
3315
|
-
error
|
|
3316
|
-
};
|
|
3317
|
-
}
|
|
3318
|
-
const aiSdkSpanMapper = {
|
|
3319
|
-
name: 'ai-sdk',
|
|
3320
|
-
canMap: shouldMapAiSdkSpan,
|
|
3321
|
-
map: span => {
|
|
3322
|
-
return buildAiSdkMapperResult(span);
|
|
3323
|
-
}
|
|
3324
|
-
};
|
|
3325
|
-
|
|
3326
|
-
const defaultSpanMappers = [aiSdkSpanMapper];
|
|
3327
|
-
|
|
3328
|
-
function pickMapper(span, mappers) {
|
|
3329
|
-
return mappers.find(mapper => {
|
|
3330
|
-
try {
|
|
3331
|
-
return mapper.canMap(span);
|
|
3332
|
-
} catch {
|
|
3333
|
-
return false;
|
|
3334
|
-
}
|
|
3335
|
-
});
|
|
3336
|
-
}
|
|
3337
|
-
function getTraceId(span, options, mapperTraceId) {
|
|
3338
|
-
if (mapperTraceId) {
|
|
3339
|
-
return mapperTraceId;
|
|
3340
|
-
}
|
|
3341
|
-
if (options.posthogTraceId) {
|
|
3342
|
-
return options.posthogTraceId;
|
|
3343
|
-
}
|
|
3344
|
-
const spanTraceId = span.spanContext?.().traceId;
|
|
3345
|
-
return spanTraceId || v4();
|
|
3346
|
-
}
|
|
3347
|
-
function buildPosthogParams(options, traceId, distinctId, modelParams, posthogProperties) {
|
|
3348
|
-
return {
|
|
3349
|
-
...modelParams,
|
|
3350
|
-
posthogDistinctId: distinctId,
|
|
3351
|
-
posthogTraceId: traceId,
|
|
3352
|
-
posthogProperties,
|
|
3353
|
-
posthogPrivacyMode: options.posthogPrivacyMode,
|
|
3354
|
-
posthogGroups: options.posthogGroups,
|
|
3355
|
-
posthogModelOverride: options.posthogModelOverride,
|
|
3356
|
-
posthogProviderOverride: options.posthogProviderOverride,
|
|
3357
|
-
posthogCostOverride: options.posthogCostOverride,
|
|
3358
|
-
posthogCaptureImmediate: options.posthogCaptureImmediate
|
|
3359
|
-
};
|
|
3360
|
-
}
|
|
3361
|
-
async function captureSpan(span, phClient, options = {}) {
|
|
3362
|
-
if (options.shouldExportSpan && options.shouldExportSpan({
|
|
3363
|
-
otelSpan: span
|
|
3364
|
-
}) === false) {
|
|
3365
|
-
return;
|
|
3366
|
-
}
|
|
3367
|
-
const mappers = options.mappers ?? defaultSpanMappers;
|
|
3368
|
-
const mapper = pickMapper(span, mappers);
|
|
3369
|
-
if (!mapper) {
|
|
3370
|
-
return;
|
|
3371
|
-
}
|
|
3372
|
-
const mapped = mapper.map(span, {
|
|
3373
|
-
options
|
|
3374
|
-
});
|
|
3375
|
-
if (!mapped) {
|
|
3376
|
-
return;
|
|
3377
|
-
}
|
|
3378
|
-
const traceId = getTraceId(span, options, mapped.traceId);
|
|
3379
|
-
const distinctId = mapped.distinctId ?? options.posthogDistinctId;
|
|
3380
|
-
const posthogProperties = {
|
|
3381
|
-
...options.posthogProperties,
|
|
3382
|
-
...mapped.posthogProperties
|
|
3383
|
-
};
|
|
3384
|
-
const params = buildPosthogParams(options, traceId, distinctId, mapped.modelParams ?? {}, posthogProperties);
|
|
3385
|
-
const baseURL = mapped.baseURL ?? '';
|
|
3386
|
-
const usage = mapped.usage ?? {};
|
|
3387
|
-
if (mapped.error !== undefined) {
|
|
3388
|
-
await sendEventWithErrorToPosthog({
|
|
3389
|
-
eventType: mapped.eventType,
|
|
3390
|
-
client: phClient,
|
|
3391
|
-
distinctId,
|
|
3392
|
-
traceId,
|
|
3393
|
-
model: mapped.model,
|
|
3394
|
-
provider: mapped.provider,
|
|
3395
|
-
input: mapped.input,
|
|
3396
|
-
output: mapped.output,
|
|
3397
|
-
latency: mapped.latency,
|
|
3398
|
-
baseURL,
|
|
3399
|
-
params: params,
|
|
3400
|
-
usage,
|
|
3401
|
-
tools: mapped.tools,
|
|
3402
|
-
error: mapped.error,
|
|
3403
|
-
captureImmediate: options.posthogCaptureImmediate
|
|
3404
|
-
});
|
|
3405
|
-
return;
|
|
3406
|
-
}
|
|
3407
|
-
await sendEventToPosthog({
|
|
3408
|
-
eventType: mapped.eventType,
|
|
3409
|
-
client: phClient,
|
|
3410
|
-
distinctId,
|
|
3411
|
-
traceId,
|
|
3412
|
-
model: mapped.model,
|
|
3413
|
-
provider: mapped.provider,
|
|
3414
|
-
input: mapped.input,
|
|
3415
|
-
output: mapped.output,
|
|
3416
|
-
latency: mapped.latency,
|
|
3417
|
-
timeToFirstToken: mapped.timeToFirstToken,
|
|
3418
|
-
baseURL,
|
|
3419
|
-
params: params,
|
|
3420
|
-
httpStatus: mapped.httpStatus ?? 200,
|
|
3421
|
-
usage,
|
|
3422
|
-
tools: mapped.tools,
|
|
3423
|
-
captureImmediate: options.posthogCaptureImmediate
|
|
3424
|
-
});
|
|
3425
|
-
}
|
|
3426
|
-
|
|
3427
|
-
class PostHogSpanProcessor {
|
|
3428
|
-
constructor(phClient, options = {}) {
|
|
3429
|
-
this.phClient = phClient;
|
|
3430
|
-
this.options = options;
|
|
3431
|
-
this.pendingCaptures = new Set();
|
|
3432
|
-
}
|
|
3433
|
-
onStart(_span, _parentContext) {
|
|
3434
|
-
// no-op
|
|
3435
|
-
}
|
|
3436
|
-
onEnd(span) {
|
|
3437
|
-
const capturePromise = captureSpan(span, this.phClient, this.options).catch(error => {
|
|
3438
|
-
console.error('Failed to capture telemetry span', error);
|
|
3439
|
-
}).finally(() => {
|
|
3440
|
-
this.pendingCaptures.delete(capturePromise);
|
|
3441
2643
|
});
|
|
3442
|
-
this.pendingCaptures.add(capturePromise);
|
|
3443
2644
|
}
|
|
3444
|
-
async shutdown() {
|
|
3445
|
-
await this.forceFlush();
|
|
3446
|
-
}
|
|
3447
|
-
async forceFlush() {
|
|
3448
|
-
while (this.pendingCaptures.size > 0) {
|
|
3449
|
-
await Promise.allSettled([...this.pendingCaptures]);
|
|
3450
|
-
}
|
|
3451
|
-
}
|
|
3452
|
-
}
|
|
3453
|
-
function createPostHogSpanProcessor(phClient, options = {}) {
|
|
3454
|
-
return new PostHogSpanProcessor(phClient, options);
|
|
3455
2645
|
}
|
|
3456
2646
|
|
|
3457
2647
|
class PostHogAnthropic extends AnthropicOriginal {
|
|
@@ -5329,5 +4519,5 @@ class Prompts {
|
|
|
5329
4519
|
}
|
|
5330
4520
|
}
|
|
5331
4521
|
|
|
5332
|
-
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI,
|
|
4522
|
+
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, PostHogTraceExporter, Prompts, wrapVercelLanguageModel as withTracing };
|
|
5333
4523
|
//# sourceMappingURL=index.mjs.map
|