@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.
@@ -22,7 +22,7 @@ function _interopNamespaceDefault(e) {
22
22
 
23
23
  var uuid__namespace = /*#__PURE__*/_interopNamespaceDefault(uuid);
24
24
 
25
- var version = "6.5.0";
25
+ var version = "6.6.0";
26
26
 
27
27
  // Type guards for safer type checking
28
28
 
@@ -1022,6 +1022,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1022
1022
  if (additionalTokenData.reasoningTokens) {
1023
1023
  eventProperties['$ai_reasoning_tokens'] = additionalTokenData.reasoningTokens;
1024
1024
  }
1025
+ if (additionalTokenData.webSearchCount !== undefined) {
1026
+ eventProperties['$ai_web_search_count'] = additionalTokenData.webSearchCount;
1027
+ }
1025
1028
 
1026
1029
  // Handle generations/completions
1027
1030
  let completions;
@@ -1192,6 +1195,50 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1192
1195
  } else if (usage.reasoningTokens != null) {
1193
1196
  additionalTokenData.reasoningTokens = usage.reasoningTokens;
1194
1197
  }
1198
+
1199
+ // Extract web search counts from various provider formats
1200
+ let webSearchCount;
1201
+
1202
+ // Priority 1: Exact Count
1203
+ // Check Anthropic format (server_tool_use.web_search_requests)
1204
+ if (usage.server_tool_use?.web_search_requests !== undefined) {
1205
+ webSearchCount = usage.server_tool_use.web_search_requests;
1206
+ }
1207
+ // Priority 2: Binary Detection (1 or 0)
1208
+ // Check for citations array (Perplexity)
1209
+ else if (usage.citations && Array.isArray(usage.citations) && usage.citations.length > 0) {
1210
+ webSearchCount = 1;
1211
+ }
1212
+ // Check for search_results array (Perplexity via OpenRouter)
1213
+ else if (usage.search_results && Array.isArray(usage.search_results) && usage.search_results.length > 0) {
1214
+ webSearchCount = 1;
1215
+ }
1216
+ // Check for search_context_size (Perplexity via OpenRouter)
1217
+ else if (usage.search_context_size) {
1218
+ webSearchCount = 1;
1219
+ }
1220
+ // Check for annotations with url_citation type
1221
+ else if (usage.annotations && Array.isArray(usage.annotations)) {
1222
+ const hasUrlCitation = usage.annotations.some(ann => {
1223
+ return ann && typeof ann === 'object' && 'type' in ann && ann.type === 'url_citation';
1224
+ });
1225
+ if (hasUrlCitation) {
1226
+ webSearchCount = 1;
1227
+ }
1228
+ }
1229
+ // Check Gemini format (grounding metadata - binary 0 or 1)
1230
+ else if (usage.grounding_metadata?.grounding_support !== undefined || usage.grounding_metadata?.web_search_queries !== undefined) {
1231
+ webSearchCount = 1;
1232
+ }
1233
+ if (webSearchCount !== undefined) {
1234
+ additionalTokenData.webSearchCount = webSearchCount;
1235
+ }
1236
+
1237
+ // In LangChain, input_tokens is the sum of input and cache read tokens.
1238
+ // Our cost calculation expects them to be separate, for Anthropic.
1239
+ if (parsedUsage.input && additionalTokenData.cacheReadInputTokens) {
1240
+ parsedUsage.input = Math.max(parsedUsage.input - additionalTokenData.cacheReadInputTokens, 0);
1241
+ }
1195
1242
  return [parsedUsage.input, parsedUsage.output, additionalTokenData];
1196
1243
  }
1197
1244
  parseUsage(response) {