@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.
@@ -534,7 +534,58 @@ function formatOpenAIResponsesInput(input, instructions) {
534
534
  return messages;
535
535
  }
536
536
 
537
- var version = "7.18.12";
537
+ var version = "7.19.0";
538
+
539
+ const DEFAULT_MAX_DEPTH = 3;
540
+ const MAX_STACK_LINES = 20;
541
+ function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
542
+ if (depth < 0 || value === null || typeof value !== 'object') {
543
+ return value;
544
+ }
545
+ if (value instanceof Error) {
546
+ const out = {
547
+ name: value.name,
548
+ message: value.message,
549
+ stack: truncateStack(value.stack)
550
+ };
551
+ for (const key of Object.keys(value)) {
552
+ out[key] = serializeError(value[key], depth - 1);
553
+ }
554
+ if (value.cause !== undefined) {
555
+ out.cause = serializeError(value.cause, depth - 1);
556
+ }
557
+ return out;
558
+ }
559
+ if (Array.isArray(value)) {
560
+ return value.map(item => serializeError(item, depth - 1));
561
+ }
562
+ return value;
563
+ }
564
+ function stringifyError(error) {
565
+ try {
566
+ return JSON.stringify(sanitizeValues(serializeError(error)));
567
+ } catch {
568
+ if (error instanceof Error) {
569
+ return JSON.stringify({
570
+ name: error.name,
571
+ message: error.message
572
+ });
573
+ }
574
+ return JSON.stringify({
575
+ message: String(error)
576
+ });
577
+ }
578
+ }
579
+ function truncateStack(stack) {
580
+ if (!stack) {
581
+ return stack;
582
+ }
583
+ const lines = stack.split('\n');
584
+ if (lines.length <= MAX_STACK_LINES) {
585
+ return stack;
586
+ }
587
+ return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
588
+ }
538
589
 
539
590
  /**
540
591
  * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
@@ -587,7 +638,7 @@ const captureAiGeneration = async (client, options) => {
587
638
  }
588
639
  errorData = {
589
640
  $ai_is_error: true,
590
- $ai_error: sanitizeValues(JSON.stringify(options.error)),
641
+ $ai_error: stringifyError(options.error),
591
642
  $exception_event_id: exceptionId
592
643
  };
593
644
  }