@posthog/ai 6.5.0 → 6.6.0

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.
@@ -1,7 +1,7 @@
1
1
  import 'buffer';
2
2
  import * as uuid from 'uuid';
3
3
 
4
- var version = "6.5.0";
4
+ var version = "6.6.0";
5
5
 
6
6
  // Type guards for safer type checking
7
7
 
@@ -1001,6 +1001,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1001
1001
  if (additionalTokenData.reasoningTokens) {
1002
1002
  eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
1003
1003
  }
1004
+ if (additionalTokenData.webSearchCount !== undefined) {
1005
+ eventProperties['$ai_web_search_count'] = additionalTokenData.webSearchCount;
1006
+ }
1004
1007
 
1005
1008
  // Handle generations/completions
1006
1009
  let completions;
@@ -1171,6 +1174,50 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1171
1174
  } else if (usage.reasoningTokens != null) {
1172
1175
  additionalTokenData.reasoningTokens = usage.reasoningTokens;
1173
1176
  }
1177
+
1178
+ // Extract web search counts from various provider formats
1179
+ let webSearchCount;
1180
+
1181
+ // Priority 1: Exact Count
1182
+ // Check Anthropic format (server_tool_use.web_search_requests)
1183
+ if (usage.server_tool_use?.web_search_requests !== undefined) {
1184
+ webSearchCount = usage.server_tool_use.web_search_requests;
1185
+ }
1186
+ // Priority 2: Binary Detection (1 or 0)
1187
+ // Check for citations array (Perplexity)
1188
+ else if (usage.citations && Array.isArray(usage.citations) && usage.citations.length > 0) {
1189
+ webSearchCount = 1;
1190
+ }
1191
+ // Check for search_results array (Perplexity via OpenRouter)
1192
+ else if (usage.search_results && Array.isArray(usage.search_results) && usage.search_results.length > 0) {
1193
+ webSearchCount = 1;
1194
+ }
1195
+ // Check for search_context_size (Perplexity via OpenRouter)
1196
+ else if (usage.search_context_size) {
1197
+ webSearchCount = 1;
1198
+ }
1199
+ // Check for annotations with url_citation type
1200
+ else if (usage.annotations && Array.isArray(usage.annotations)) {
1201
+ const hasUrlCitation = usage.annotations.some(ann => {
1202
+ return ann && typeof ann === 'object' && 'type' in ann && ann.type === 'url_citation';
1203
+ });
1204
+ if (hasUrlCitation) {
1205
+ webSearchCount = 1;
1206
+ }
1207
+ }
1208
+ // Check Gemini format (grounding metadata - binary 0 or 1)
1209
+ else if (usage.grounding_metadata?.grounding_support !== undefined || usage.grounding_metadata?.web_search_queries !== undefined) {
1210
+ webSearchCount = 1;
1211
+ }
1212
+ if (webSearchCount !== undefined) {
1213
+ additionalTokenData.webSearchCount = webSearchCount;
1214
+ }
1215
+
1216
+ // In LangChain, input_tokens is the sum of input and cache read tokens.
1217
+ // Our cost calculation expects them to be separate, for Anthropic.
1218
+ if (parsedUsage.input && additionalTokenData.cacheReadInputTokens) {
1219
+ parsedUsage.input = Math.max(parsedUsage.input - additionalTokenData.cacheReadInputTokens, 0);
1220
+ }
1174
1221
  return [parsedUsage.input, parsedUsage.output, additionalTokenData];
1175
1222
  }
1176
1223
  parseUsage(response) {