@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.
@@ -2,7 +2,7 @@ import { OpenAI } from 'openai';
2
2
  import { v4 } from 'uuid';
3
3
  import { uuidv7 } from '@posthog/core';
4
4
 
5
- var version = "7.13.2";
5
+ var version = "7.14.0";
6
6
 
7
7
  // Type guards for safer type checking
8
8
 
@@ -520,6 +520,7 @@ const sendEventToPosthog = async ({
520
520
  usage = {},
521
521
  error,
522
522
  exceptionId,
523
+ stopReason,
523
524
  tools,
524
525
  captureImmediate = false
525
526
  }) => {
@@ -590,6 +591,9 @@ const sendEventToPosthog = async ({
590
591
  ...(distinctId ? {} : {
591
592
  $process_person_profile: false
592
593
  }),
594
+ ...(stopReason ? {
595
+ $ai_stop_reason: stopReason
596
+ } : {}),
593
597
  ...(tools ? {
594
598
  $ai_tools: tools
595
599
  } : {}),
@@ -721,6 +725,7 @@ class WrappedCompletions extends Completions {
721
725
  let accumulatedContent = '';
722
726
  let modelFromResponse;
723
727
  let firstTokenTime;
728
+ let stopReason;
724
729
  let usage = {
725
730
  inputTokens: 0,
726
731
  outputTokens: 0,
@@ -736,6 +741,9 @@ class WrappedCompletions extends Completions {
736
741
  modelFromResponse = chunk.model;
737
742
  }
738
743
  const choice = chunk?.choices?.[0];
744
+ if (choice?.finish_reason) {
745
+ stopReason = choice.finish_reason;
746
+ }
739
747
  const chunkWebSearchCount = calculateWebSearchCount(chunk);
740
748
  if (chunkWebSearchCount > 0 && chunkWebSearchCount > (usage.webSearchCount ?? 0)) {
741
749
  usage.webSearchCount = chunkWebSearchCount;
@@ -853,6 +861,7 @@ class WrappedCompletions extends Completions {
853
861
  webSearchCount: usage.webSearchCount,
854
862
  rawUsage: rawUsageData
855
863
  },
864
+ stopReason,
856
865
  tools: availableTools
857
866
  });
858
867
  } catch (error) {
@@ -906,6 +915,7 @@ class WrappedCompletions extends Completions {
906
915
  webSearchCount: calculateWebSearchCount(result),
907
916
  rawUsage: result.usage
908
917
  },
918
+ stopReason: result.choices[0]?.finish_reason ?? undefined,
909
919
  tools: availableTools
910
920
  });
911
921
  }
@@ -965,6 +975,7 @@ class WrappedResponses extends Responses {
965
975
  let finalContent = [];
966
976
  let modelFromResponse;
967
977
  let firstTokenTime;
978
+ let stopReason;
968
979
  let usage = {
969
980
  inputTokens: 0,
970
981
  outputTokens: 0,
@@ -988,6 +999,9 @@ class WrappedResponses extends Responses {
988
999
  }
989
1000
  if (chunk.type === 'response.completed' && 'response' in chunk && chunk.response?.output && chunk.response.output.length > 0) {
990
1001
  finalContent = chunk.response.output;
1002
+ if (chunk.response.status) {
1003
+ stopReason = chunk.response.status;
1004
+ }
991
1005
  }
992
1006
  if ('response' in chunk && chunk.response?.usage) {
993
1007
  rawUsageData = chunk.response.usage;
@@ -1023,6 +1037,7 @@ class WrappedResponses extends Responses {
1023
1037
  webSearchCount: usage.webSearchCount,
1024
1038
  rawUsage: rawUsageData
1025
1039
  },
1040
+ stopReason,
1026
1041
  tools: availableTools
1027
1042
  });
1028
1043
  } catch (error) {
@@ -1076,6 +1091,7 @@ class WrappedResponses extends Responses {
1076
1091
  webSearchCount: calculateWebSearchCount(result),
1077
1092
  rawUsage: result.usage
1078
1093
  },
1094
+ stopReason: result.status ?? undefined,
1079
1095
  tools: availableTools
1080
1096
  });
1081
1097
  }
@@ -1135,7 +1151,8 @@ class WrappedResponses extends Responses {
1135
1151
  reasoningTokens: result.usage?.output_tokens_details?.reasoning_tokens ?? 0,
1136
1152
  cacheReadInputTokens: result.usage?.input_tokens_details?.cached_tokens ?? 0,
1137
1153
  rawUsage: result.usage
1138
- }
1154
+ },
1155
+ stopReason: result.status ?? undefined
1139
1156
  });
1140
1157
  return result;
1141
1158
  }, async error => {