@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.
@@ -5,8 +5,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var openai = require('openai');
6
6
  var buffer = require('buffer');
7
7
  var uuid = require('uuid');
8
+ var core = require('@posthog/core');
8
9
 
9
- var version = "7.3.1";
10
+ var version = "7.4.0";
10
11
 
11
12
  // Type guards for safer type checking
12
13
 
@@ -329,6 +330,33 @@ function addDefaults(params) {
329
330
  traceId: params.traceId ?? uuid.v4()
330
331
  };
331
332
  }
333
+ const sendEventWithErrorToPosthog = async ({
334
+ client,
335
+ traceId,
336
+ error,
337
+ ...args
338
+ }) => {
339
+ const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
340
+ const properties = {
341
+ client,
342
+ traceId,
343
+ httpStatus,
344
+ error: JSON.stringify(error),
345
+ ...args
346
+ };
347
+ const enrichedError = error;
348
+ if (client.options?.enableExceptionAutocapture) {
349
+ // assign a uuid that can be used to link the trace and exception events
350
+ const exceptionId = core.uuidv7();
351
+ client.captureException(error, undefined, {
352
+ $ai_trace_id: traceId
353
+ }, exceptionId);
354
+ enrichedError.__posthog_previously_captured_error = true;
355
+ properties.exceptionId = exceptionId;
356
+ }
357
+ await sendEventToPosthog(properties);
358
+ return enrichedError;
359
+ };
332
360
  const sendEventToPosthog = async ({
333
361
  client,
334
362
  eventType = AIEvent.Generation,
@@ -343,8 +371,8 @@ const sendEventToPosthog = async ({
343
371
  params,
344
372
  httpStatus = 200,
345
373
  usage = {},
346
- isError = false,
347
374
  error,
375
+ exceptionId,
348
376
  tools,
349
377
  captureImmediate = false
350
378
  }) => {
@@ -356,10 +384,11 @@ const sendEventToPosthog = async ({
356
384
  const safeOutput = sanitizeValues(output);
357
385
  const safeError = sanitizeValues(error);
358
386
  let errorData = {};
359
- if (isError) {
387
+ if (error) {
360
388
  errorData = {
361
389
  $ai_is_error: true,
362
- $ai_error: safeError
390
+ $ai_error: safeError,
391
+ $exception_event_id: exceptionId
363
392
  };
364
393
  }
365
394
  let costOverrideData = {};
@@ -425,6 +454,7 @@ const sendEventToPosthog = async ({
425
454
  } else {
426
455
  client.capture(event);
427
456
  }
457
+ return Promise.resolve();
428
458
  };
429
459
  function formatOpenAIResponsesInput(input, instructions) {
430
460
  const messages = [];
@@ -770,8 +800,7 @@ class WrappedCompletions extends Completions {
770
800
  tools: availableTools
771
801
  });
772
802
  } catch (error) {
773
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
774
- await sendEventToPosthog({
803
+ const enrichedError = await sendEventWithErrorToPosthog({
775
804
  client: this.phClient,
776
805
  ...posthogParams,
777
806
  model: openAIParams.model,
@@ -781,14 +810,13 @@ class WrappedCompletions extends Completions {
781
810
  latency: 0,
782
811
  baseURL: this.baseURL,
783
812
  params: body,
784
- httpStatus,
785
813
  usage: {
786
814
  inputTokens: 0,
787
815
  outputTokens: 0
788
816
  },
789
- isError: true,
790
- error: JSON.stringify(error)
817
+ error
791
818
  });
819
+ throw enrichedError;
792
820
  }
793
821
  })();
794
822
 
@@ -842,7 +870,6 @@ class WrappedCompletions extends Completions {
842
870
  inputTokens: 0,
843
871
  outputTokens: 0
844
872
  },
845
- isError: true,
846
873
  error: JSON.stringify(error)
847
874
  });
848
875
  throw error;
@@ -932,8 +959,7 @@ class WrappedResponses extends Responses {
932
959
  tools: availableTools
933
960
  });
934
961
  } catch (error) {
935
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
936
- await sendEventToPosthog({
962
+ const enrichedError = await sendEventWithErrorToPosthog({
937
963
  client: this.phClient,
938
964
  ...posthogParams,
939
965
  model: openAIParams.model,
@@ -943,14 +969,13 @@ class WrappedResponses extends Responses {
943
969
  latency: 0,
944
970
  baseURL: this.baseURL,
945
971
  params: body,
946
- httpStatus,
947
972
  usage: {
948
973
  inputTokens: 0,
949
974
  outputTokens: 0
950
975
  },
951
- isError: true,
952
- error: JSON.stringify(error)
976
+ error: error
953
977
  });
978
+ throw enrichedError;
954
979
  }
955
980
  })();
956
981
  return stream2;
@@ -1004,7 +1029,6 @@ class WrappedResponses extends Responses {
1004
1029
  inputTokens: 0,
1005
1030
  outputTokens: 0
1006
1031
  },
1007
- isError: true,
1008
1032
  error: JSON.stringify(error)
1009
1033
  });
1010
1034
  throw error;
@@ -1046,8 +1070,7 @@ class WrappedResponses extends Responses {
1046
1070
  });
1047
1071
  return result;
1048
1072
  }, async error => {
1049
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1050
- await sendEventToPosthog({
1073
+ const enrichedError = await sendEventWithErrorToPosthog({
1051
1074
  client: this.phClient,
1052
1075
  ...posthogParams,
1053
1076
  model: openAIParams.model,
@@ -1057,15 +1080,13 @@ class WrappedResponses extends Responses {
1057
1080
  latency: 0,
1058
1081
  baseURL: this.baseURL,
1059
1082
  params: body,
1060
- httpStatus,
1061
1083
  usage: {
1062
1084
  inputTokens: 0,
1063
1085
  outputTokens: 0
1064
1086
  },
1065
- isError: true,
1066
1087
  error: JSON.stringify(error)
1067
1088
  });
1068
- throw error;
1089
+ throw enrichedError;
1069
1090
  });
1070
1091
  return wrappedPromise;
1071
1092
  } finally {
@@ -1125,7 +1146,6 @@ class WrappedEmbeddings extends Embeddings {
1125
1146
  usage: {
1126
1147
  inputTokens: 0
1127
1148
  },
1128
- isError: true,
1129
1149
  error: JSON.stringify(error)
1130
1150
  });
1131
1151
  throw error;
@@ -1208,8 +1228,7 @@ class WrappedTranscriptions extends Transcriptions {
1208
1228
  tools: availableTools
1209
1229
  });
1210
1230
  } catch (error) {
1211
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1212
- await sendEventToPosthog({
1231
+ const enrichedError = await sendEventWithErrorToPosthog({
1213
1232
  client: this.phClient,
1214
1233
  ...posthogParams,
1215
1234
  model: openAIParams.model,
@@ -1219,14 +1238,13 @@ class WrappedTranscriptions extends Transcriptions {
1219
1238
  latency: 0,
1220
1239
  baseURL: this.baseURL,
1221
1240
  params: body,
1222
- httpStatus,
1223
1241
  usage: {
1224
1242
  inputTokens: 0,
1225
1243
  outputTokens: 0
1226
1244
  },
1227
- isError: true,
1228
- error: JSON.stringify(error)
1245
+ error: error
1229
1246
  });
1247
+ throw enrichedError;
1230
1248
  }
1231
1249
  })();
1232
1250
  return stream2;
@@ -1256,8 +1274,7 @@ class WrappedTranscriptions extends Transcriptions {
1256
1274
  return result;
1257
1275
  }
1258
1276
  }, async error => {
1259
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1260
- await sendEventToPosthog({
1277
+ const enrichedError = await sendEventWithErrorToPosthog({
1261
1278
  client: this.phClient,
1262
1279
  ...posthogParams,
1263
1280
  model: openAIParams.model,
@@ -1267,15 +1284,13 @@ class WrappedTranscriptions extends Transcriptions {
1267
1284
  latency: 0,
1268
1285
  baseURL: this.baseURL,
1269
1286
  params: body,
1270
- httpStatus,
1271
1287
  usage: {
1272
1288
  inputTokens: 0,
1273
1289
  outputTokens: 0
1274
1290
  },
1275
- isError: true,
1276
- error: JSON.stringify(error)
1291
+ error: error
1277
1292
  });
1278
- throw error;
1293
+ throw enrichedError;
1279
1294
  });
1280
1295
  return wrappedPromise;
1281
1296
  }