@posthog/ai 7.18.12 → 7.19.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.
@@ -392,7 +392,58 @@ function sanitizeValues(obj) {
392
392
  return jsonSafe;
393
393
  }
394
394
 
395
- var version = "7.18.12";
395
+ var version = "7.19.0";
396
+
397
+ const DEFAULT_MAX_DEPTH = 3;
398
+ const MAX_STACK_LINES = 20;
399
+ function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
400
+ if (depth < 0 || value === null || typeof value !== 'object') {
401
+ return value;
402
+ }
403
+ if (value instanceof Error) {
404
+ const out = {
405
+ name: value.name,
406
+ message: value.message,
407
+ stack: truncateStack(value.stack)
408
+ };
409
+ for (const key of Object.keys(value)) {
410
+ out[key] = serializeError(value[key], depth - 1);
411
+ }
412
+ if (value.cause !== undefined) {
413
+ out.cause = serializeError(value.cause, depth - 1);
414
+ }
415
+ return out;
416
+ }
417
+ if (Array.isArray(value)) {
418
+ return value.map(item => serializeError(item, depth - 1));
419
+ }
420
+ return value;
421
+ }
422
+ function stringifyError(error) {
423
+ try {
424
+ return JSON.stringify(sanitizeValues(serializeError(error)));
425
+ } catch {
426
+ if (error instanceof Error) {
427
+ return JSON.stringify({
428
+ name: error.name,
429
+ message: error.message
430
+ });
431
+ }
432
+ return JSON.stringify({
433
+ message: String(error)
434
+ });
435
+ }
436
+ }
437
+ function truncateStack(stack) {
438
+ if (!stack) {
439
+ return stack;
440
+ }
441
+ const lines = stack.split('\n');
442
+ if (lines.length <= MAX_STACK_LINES) {
443
+ return stack;
444
+ }
445
+ return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
446
+ }
396
447
 
397
448
  /**
398
449
  * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
@@ -445,7 +496,7 @@ const captureAiGeneration = async (client, options) => {
445
496
  }
446
497
  errorData = {
447
498
  $ai_is_error: true,
448
- $ai_error: sanitizeValues(JSON.stringify(options.error)),
499
+ $ai_error: stringifyError(options.error),
449
500
  $exception_event_id: exceptionId
450
501
  };
451
502
  }