@posthog/ai 7.18.12 → 7.19.1

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