@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.
package/dist/index.mjs CHANGED
@@ -709,7 +709,58 @@ function formatOpenAIResponsesInput(input, instructions) {
709
709
  return messages;
710
710
  }
711
711
 
712
- var version = "7.18.12";
712
+ var version = "7.19.0";
713
+
714
+ const DEFAULT_MAX_DEPTH = 3;
715
+ const MAX_STACK_LINES = 20;
716
+ function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
717
+ if (depth < 0 || value === null || typeof value !== 'object') {
718
+ return value;
719
+ }
720
+ if (value instanceof Error) {
721
+ const out = {
722
+ name: value.name,
723
+ message: value.message,
724
+ stack: truncateStack(value.stack)
725
+ };
726
+ for (const key of Object.keys(value)) {
727
+ out[key] = serializeError(value[key], depth - 1);
728
+ }
729
+ if (value.cause !== undefined) {
730
+ out.cause = serializeError(value.cause, depth - 1);
731
+ }
732
+ return out;
733
+ }
734
+ if (Array.isArray(value)) {
735
+ return value.map(item => serializeError(item, depth - 1));
736
+ }
737
+ return value;
738
+ }
739
+ function stringifyError(error) {
740
+ try {
741
+ return JSON.stringify(sanitizeValues(serializeError(error)));
742
+ } catch {
743
+ if (error instanceof Error) {
744
+ return JSON.stringify({
745
+ name: error.name,
746
+ message: error.message
747
+ });
748
+ }
749
+ return JSON.stringify({
750
+ message: String(error)
751
+ });
752
+ }
753
+ }
754
+ function truncateStack(stack) {
755
+ if (!stack) {
756
+ return stack;
757
+ }
758
+ const lines = stack.split('\n');
759
+ if (lines.length <= MAX_STACK_LINES) {
760
+ return stack;
761
+ }
762
+ return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
763
+ }
713
764
 
714
765
  /**
715
766
  * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
@@ -756,7 +807,7 @@ const captureAiGeneration = async (client, options) => {
756
807
  }
757
808
  errorData = {
758
809
  $ai_is_error: true,
759
- $ai_error: sanitizeValues(JSON.stringify(options.error)),
810
+ $ai_error: stringifyError(options.error),
760
811
  $exception_event_id: exceptionId
761
812
  };
762
813
  }
@@ -4013,7 +4064,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4013
4064
  eventProperties['$process_person_profile'] = false;
4014
4065
  }
4015
4066
  if (outputs instanceof Error) {
4016
- eventProperties['$ai_error'] = outputs.toString();
4067
+ eventProperties['$ai_error'] = stringifyError(outputs);
4017
4068
  eventProperties['$ai_is_error'] = true;
4018
4069
  } else if (outputs !== undefined) {
4019
4070
  eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
@@ -4059,7 +4110,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
4059
4110
  }
4060
4111
  if (output instanceof Error) {
4061
4112
  eventProperties['$ai_http_status'] = output.status || 500;
4062
- eventProperties['$ai_error'] = output.toString();
4113
+ eventProperties['$ai_error'] = stringifyError(output);
4063
4114
  eventProperties['$ai_is_error'] = true;
4064
4115
  } else {
4065
4116
  // Handle token usage