@posthog/ai 7.3.1 → 7.4.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,8 +1,9 @@
1
1
  import { OpenAI } from 'openai';
2
2
  import { Buffer } from 'buffer';
3
3
  import { v4 } from 'uuid';
4
+ import { uuidv7 } from '@posthog/core';
4
5
 
5
- var version = "7.3.1";
6
+ var version = "7.4.0";
6
7
 
7
8
  // Type guards for safer type checking
8
9
 
@@ -325,6 +326,33 @@ function addDefaults(params) {
325
326
  traceId: params.traceId ?? v4()
326
327
  };
327
328
  }
329
+ const sendEventWithErrorToPosthog = async ({
330
+ client,
331
+ traceId,
332
+ error,
333
+ ...args
334
+ }) => {
335
+ const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
336
+ const properties = {
337
+ client,
338
+ traceId,
339
+ httpStatus,
340
+ error: JSON.stringify(error),
341
+ ...args
342
+ };
343
+ const enrichedError = error;
344
+ if (client.options?.enableExceptionAutocapture) {
345
+ // assign a uuid that can be used to link the trace and exception events
346
+ const exceptionId = uuidv7();
347
+ client.captureException(error, undefined, {
348
+ $ai_trace_id: traceId
349
+ }, exceptionId);
350
+ enrichedError.__posthog_previously_captured_error = true;
351
+ properties.exceptionId = exceptionId;
352
+ }
353
+ await sendEventToPosthog(properties);
354
+ return enrichedError;
355
+ };
328
356
  const sendEventToPosthog = async ({
329
357
  client,
330
358
  eventType = AIEvent.Generation,
@@ -339,8 +367,8 @@ const sendEventToPosthog = async ({
339
367
  params,
340
368
  httpStatus = 200,
341
369
  usage = {},
342
- isError = false,
343
370
  error,
371
+ exceptionId,
344
372
  tools,
345
373
  captureImmediate = false
346
374
  }) => {
@@ -352,10 +380,11 @@ const sendEventToPosthog = async ({
352
380
  const safeOutput = sanitizeValues(output);
353
381
  const safeError = sanitizeValues(error);
354
382
  let errorData = {};
355
- if (isError) {
383
+ if (error) {
356
384
  errorData = {
357
385
  $ai_is_error: true,
358
- $ai_error: safeError
386
+ $ai_error: safeError,
387
+ $exception_event_id: exceptionId
359
388
  };
360
389
  }
361
390
  let costOverrideData = {};
@@ -421,6 +450,7 @@ const sendEventToPosthog = async ({
421
450
  } else {
422
451
  client.capture(event);
423
452
  }
453
+ return Promise.resolve();
424
454
  };
425
455
  function formatOpenAIResponsesInput(input, instructions) {
426
456
  const messages = [];
@@ -766,8 +796,7 @@ class WrappedCompletions extends Completions {
766
796
  tools: availableTools
767
797
  });
768
798
  } catch (error) {
769
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
770
- await sendEventToPosthog({
799
+ const enrichedError = await sendEventWithErrorToPosthog({
771
800
  client: this.phClient,
772
801
  ...posthogParams,
773
802
  model: openAIParams.model,
@@ -777,14 +806,13 @@ class WrappedCompletions extends Completions {
777
806
  latency: 0,
778
807
  baseURL: this.baseURL,
779
808
  params: body,
780
- httpStatus,
781
809
  usage: {
782
810
  inputTokens: 0,
783
811
  outputTokens: 0
784
812
  },
785
- isError: true,
786
- error: JSON.stringify(error)
813
+ error
787
814
  });
815
+ throw enrichedError;
788
816
  }
789
817
  })();
790
818
 
@@ -838,7 +866,6 @@ class WrappedCompletions extends Completions {
838
866
  inputTokens: 0,
839
867
  outputTokens: 0
840
868
  },
841
- isError: true,
842
869
  error: JSON.stringify(error)
843
870
  });
844
871
  throw error;
@@ -928,8 +955,7 @@ class WrappedResponses extends Responses {
928
955
  tools: availableTools
929
956
  });
930
957
  } catch (error) {
931
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
932
- await sendEventToPosthog({
958
+ const enrichedError = await sendEventWithErrorToPosthog({
933
959
  client: this.phClient,
934
960
  ...posthogParams,
935
961
  model: openAIParams.model,
@@ -939,14 +965,13 @@ class WrappedResponses extends Responses {
939
965
  latency: 0,
940
966
  baseURL: this.baseURL,
941
967
  params: body,
942
- httpStatus,
943
968
  usage: {
944
969
  inputTokens: 0,
945
970
  outputTokens: 0
946
971
  },
947
- isError: true,
948
- error: JSON.stringify(error)
972
+ error: error
949
973
  });
974
+ throw enrichedError;
950
975
  }
951
976
  })();
952
977
  return stream2;
@@ -1000,7 +1025,6 @@ class WrappedResponses extends Responses {
1000
1025
  inputTokens: 0,
1001
1026
  outputTokens: 0
1002
1027
  },
1003
- isError: true,
1004
1028
  error: JSON.stringify(error)
1005
1029
  });
1006
1030
  throw error;
@@ -1042,8 +1066,7 @@ class WrappedResponses extends Responses {
1042
1066
  });
1043
1067
  return result;
1044
1068
  }, async error => {
1045
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1046
- await sendEventToPosthog({
1069
+ const enrichedError = await sendEventWithErrorToPosthog({
1047
1070
  client: this.phClient,
1048
1071
  ...posthogParams,
1049
1072
  model: openAIParams.model,
@@ -1053,15 +1076,13 @@ class WrappedResponses extends Responses {
1053
1076
  latency: 0,
1054
1077
  baseURL: this.baseURL,
1055
1078
  params: body,
1056
- httpStatus,
1057
1079
  usage: {
1058
1080
  inputTokens: 0,
1059
1081
  outputTokens: 0
1060
1082
  },
1061
- isError: true,
1062
1083
  error: JSON.stringify(error)
1063
1084
  });
1064
- throw error;
1085
+ throw enrichedError;
1065
1086
  });
1066
1087
  return wrappedPromise;
1067
1088
  } finally {
@@ -1121,7 +1142,6 @@ class WrappedEmbeddings extends Embeddings {
1121
1142
  usage: {
1122
1143
  inputTokens: 0
1123
1144
  },
1124
- isError: true,
1125
1145
  error: JSON.stringify(error)
1126
1146
  });
1127
1147
  throw error;
@@ -1204,8 +1224,7 @@ class WrappedTranscriptions extends Transcriptions {
1204
1224
  tools: availableTools
1205
1225
  });
1206
1226
  } catch (error) {
1207
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1208
- await sendEventToPosthog({
1227
+ const enrichedError = await sendEventWithErrorToPosthog({
1209
1228
  client: this.phClient,
1210
1229
  ...posthogParams,
1211
1230
  model: openAIParams.model,
@@ -1215,14 +1234,13 @@ class WrappedTranscriptions extends Transcriptions {
1215
1234
  latency: 0,
1216
1235
  baseURL: this.baseURL,
1217
1236
  params: body,
1218
- httpStatus,
1219
1237
  usage: {
1220
1238
  inputTokens: 0,
1221
1239
  outputTokens: 0
1222
1240
  },
1223
- isError: true,
1224
- error: JSON.stringify(error)
1241
+ error: error
1225
1242
  });
1243
+ throw enrichedError;
1226
1244
  }
1227
1245
  })();
1228
1246
  return stream2;
@@ -1252,8 +1270,7 @@ class WrappedTranscriptions extends Transcriptions {
1252
1270
  return result;
1253
1271
  }
1254
1272
  }, async error => {
1255
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1256
- await sendEventToPosthog({
1273
+ const enrichedError = await sendEventWithErrorToPosthog({
1257
1274
  client: this.phClient,
1258
1275
  ...posthogParams,
1259
1276
  model: openAIParams.model,
@@ -1263,15 +1280,13 @@ class WrappedTranscriptions extends Transcriptions {
1263
1280
  latency: 0,
1264
1281
  baseURL: this.baseURL,
1265
1282
  params: body,
1266
- httpStatus,
1267
1283
  usage: {
1268
1284
  inputTokens: 0,
1269
1285
  outputTokens: 0
1270
1286
  },
1271
- isError: true,
1272
- error: JSON.stringify(error)
1287
+ error: error
1273
1288
  });
1274
- throw error;
1289
+ throw enrichedError;
1275
1290
  });
1276
1291
  return wrappedPromise;
1277
1292
  }