@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.
@@ -6,7 +6,7 @@ var openai = require('openai');
6
6
  var uuid = require('uuid');
7
7
  var core = require('@posthog/core');
8
8
 
9
- var version = "7.13.2";
9
+ var version = "7.14.0";
10
10
 
11
11
  // Type guards for safer type checking
12
12
 
@@ -524,6 +524,7 @@ const sendEventToPosthog = async ({
524
524
  usage = {},
525
525
  error,
526
526
  exceptionId,
527
+ stopReason,
527
528
  tools,
528
529
  captureImmediate = false
529
530
  }) => {
@@ -594,6 +595,9 @@ const sendEventToPosthog = async ({
594
595
  ...(distinctId ? {} : {
595
596
  $process_person_profile: false
596
597
  }),
598
+ ...(stopReason ? {
599
+ $ai_stop_reason: stopReason
600
+ } : {}),
597
601
  ...(tools ? {
598
602
  $ai_tools: tools
599
603
  } : {}),
@@ -725,6 +729,7 @@ class WrappedCompletions extends Completions {
725
729
  let accumulatedContent = '';
726
730
  let modelFromResponse;
727
731
  let firstTokenTime;
732
+ let stopReason;
728
733
  let usage = {
729
734
  inputTokens: 0,
730
735
  outputTokens: 0,
@@ -740,6 +745,9 @@ class WrappedCompletions extends Completions {
740
745
  modelFromResponse = chunk.model;
741
746
  }
742
747
  const choice = chunk?.choices?.[0];
748
+ if (choice?.finish_reason) {
749
+ stopReason = choice.finish_reason;
750
+ }
743
751
  const chunkWebSearchCount = calculateWebSearchCount(chunk);
744
752
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
745
753
  usage.webSearchCount = chunkWebSearchCount;
@@ -857,6 +865,7 @@ class WrappedCompletions extends Completions {
857
865
  webSearchCount: usage.webSearchCount,
858
866
  rawUsage: rawUsageData
859
867
  },
868
+ stopReason,
860
869
  tools: availableTools
861
870
  });
862
871
  } catch (error) {
@@ -910,6 +919,7 @@ class WrappedCompletions extends Completions {
910
919
  webSearchCount: calculateWebSearchCount(result),
911
920
  rawUsage: result.usage
912
921
  },
922
+ stopReason: result.choices[0]?.finish_reason ?? undefined,
913
923
  tools: availableTools
914
924
  });
915
925
  }
@@ -969,6 +979,7 @@ class WrappedResponses extends Responses {
969
979
  let finalContent = [];
970
980
  let modelFromResponse;
971
981
  let firstTokenTime;
982
+ let stopReason;
972
983
  let usage = {
973
984
  inputTokens: 0,
974
985
  outputTokens: 0,
@@ -992,6 +1003,9 @@ class WrappedResponses extends Responses {
992
1003
  }
993
1004
  if (chunk.type === 'response.completed' && 'response' in chunk && chunk.response?.output && chunk.response.output.length > 0) {
994
1005
  finalContent = chunk.response.output;
1006
+ if (chunk.response.status) {
1007
+ stopReason = chunk.response.status;
1008
+ }
995
1009
  }
996
1010
  if ('response' in chunk && chunk.response?.usage) {
997
1011
  rawUsageData = chunk.response.usage;
@@ -1027,6 +1041,7 @@ class WrappedResponses extends Responses {
1027
1041
  webSearchCount: usage.webSearchCount,
1028
1042
  rawUsage: rawUsageData
1029
1043
  },
1044
+ stopReason,
1030
1045
  tools: availableTools
1031
1046
  });
1032
1047
  } catch (error) {
@@ -1080,6 +1095,7 @@ class WrappedResponses extends Responses {
1080
1095
  webSearchCount: calculateWebSearchCount(result),
1081
1096
  rawUsage: result.usage
1082
1097
  },
1098
+ stopReason: result.status ?? undefined,
1083
1099
  tools: availableTools
1084
1100
  });
1085
1101
  }
@@ -1139,7 +1155,8 @@ class WrappedResponses extends Responses {
1139
1155
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1140
1156
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0,
1141
1157
  rawUsage: result.usage
1142
- }
1158
+ },
1159
+ stopReason: result.status ?? undefined
1143
1160
  });
1144
1161
  return result;
1145
1162
  }, async error => {