@posthog/ai 7.13.2 → 7.14.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.
@@ -63,6 +63,7 @@ declare class LangChainCallbackHandler extends BaseCallbackHandler {
63
63
  private _convertLcToolCallsToOai;
64
64
  private _extractRawResponse;
65
65
  private _convertMessageToDict;
66
+ private _extractStopReason;
66
67
  private _parseUsageModel;
67
68
  private parseUsage;
68
69
  }
@@ -1,7 +1,7 @@
1
1
  import * as uuid from 'uuid';
2
2
  import '@posthog/core';
3
3
 
4
- var version = "7.13.2";
4
+ var version = "7.14.0";
5
5
 
6
6
  // Type guards for safer type checking
7
7
 
@@ -965,6 +965,12 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
965
965
  eventProperties['$ai_web_search_count'] = additionalTokenData.webSearchCount;
966
966
  }
967
967
 
968
+ // Extract stop reason from generation info
969
+ const stopReason = this._extractStopReason(output);
970
+ if (stopReason) {
971
+ eventProperties['$ai_stop_reason'] = stopReason;
972
+ }
973
+
968
974
  // Handle generations/completions
969
975
  let completions;
970
976
  if (output.generations && Array.isArray(output.generations)) {
@@ -1100,6 +1106,37 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
1100
1106
  // Sanitize the message content to redact base64 images
1101
1107
  return sanitizeLangChain(messageDict);
1102
1108
  }
1109
+ _extractStopReason(output) {
1110
+ if (!output.generations || !Array.isArray(output.generations)) {
1111
+ return undefined;
1112
+ }
1113
+ const lastGeneration = output.generations[output.generations.length - 1];
1114
+ if (!Array.isArray(lastGeneration) || lastGeneration.length === 0) {
1115
+ return undefined;
1116
+ }
1117
+ const gen = lastGeneration[0];
1118
+
1119
+ // Check generationInfo for finish_reason (OpenAI format)
1120
+ if (gen.generationInfo?.finish_reason) {
1121
+ return String(gen.generationInfo.finish_reason);
1122
+ }
1123
+
1124
+ // Check generationInfo for response_metadata.stop_reason (Anthropic format)
1125
+ if (gen.generationInfo?.response_metadata?.stop_reason) {
1126
+ return String(gen.generationInfo.response_metadata.stop_reason);
1127
+ }
1128
+
1129
+ // Check message response_metadata for finish_reason (common LangChain format)
1130
+ if (gen.generationInfo?.response_metadata?.finish_reason) {
1131
+ return String(gen.generationInfo.response_metadata.finish_reason);
1132
+ }
1133
+
1134
+ // Check for stop_reason directly in generationInfo
1135
+ if (gen.generationInfo?.stop_reason) {
1136
+ return String(gen.generationInfo.stop_reason);
1137
+ }
1138
+ return undefined;
1139
+ }
1103
1140
  _parseUsageModel(usage, provider, model) {
1104
1141
  const conversionList = [['promptTokens', 'input'], ['completionTokens', 'output'], ['input_tokens', 'input'], ['output_tokens', 'output'], ['prompt_token_count', 'input'], ['candidates_token_count', 'output'], ['inputTokenCount', 'input'], ['outputTokenCount', 'output'], ['input_token_count', 'input'], ['generated_token_count', 'output']];
1105
1142
  const parsedUsage = conversionList.reduce((acc, [modelKey, typeKey]) => {