@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.
@@ -198,6 +198,21 @@ const getModelParams = params => {
198
198
  const withPrivacyMode = (client, privacyMode, input) => {
199
199
  return client.privacy_mode || privacyMode ? null : input;
200
200
  };
201
+ function sanitizeValues(obj) {
202
+ if (obj === undefined || obj === null) {
203
+ return obj;
204
+ }
205
+ const jsonSafe = JSON.parse(JSON.stringify(obj));
206
+ if (typeof jsonSafe === 'string') {
207
+ // Sanitize lone surrogates by round-tripping through UTF-8
208
+ return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
209
+ } else if (Array.isArray(jsonSafe)) {
210
+ return jsonSafe.map(sanitizeValues);
211
+ } else if (jsonSafe && typeof jsonSafe === 'object') {
212
+ return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
213
+ }
214
+ return jsonSafe;
215
+ }
201
216
 
202
217
  function getDefaultExportFromCjs (x) {
203
218
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -662,7 +677,58 @@ var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
662
677
  }
663
678
  };
664
679
 
665
- var version = "7.18.12";
680
+ var version = "7.19.1";
681
+
682
+ const DEFAULT_MAX_DEPTH = 3;
683
+ const MAX_STACK_LINES = 20;
684
+ function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
685
+ if (depth < 0 || value === null || typeof value !== 'object') {
686
+ return value;
687
+ }
688
+ if (value instanceof Error) {
689
+ const out = {
690
+ name: value.name,
691
+ message: value.message,
692
+ stack: truncateStack(value.stack)
693
+ };
694
+ for (const key of Object.keys(value)) {
695
+ out[key] = serializeError(value[key], depth - 1);
696
+ }
697
+ if (value.cause !== undefined) {
698
+ out.cause = serializeError(value.cause, depth - 1);
699
+ }
700
+ return out;
701
+ }
702
+ if (Array.isArray(value)) {
703
+ return value.map(item => serializeError(item, depth - 1));
704
+ }
705
+ return value;
706
+ }
707
+ function stringifyError(error) {
708
+ try {
709
+ return JSON.stringify(sanitizeValues(serializeError(error)));
710
+ } catch {
711
+ if (error instanceof Error) {
712
+ return JSON.stringify({
713
+ name: error.name,
714
+ message: error.message
715
+ });
716
+ }
717
+ return JSON.stringify({
718
+ message: String(error)
719
+ });
720
+ }
721
+ }
722
+ function truncateStack(stack) {
723
+ if (!stack) {
724
+ return stack;
725
+ }
726
+ const lines = stack.split('\n');
727
+ if (lines.length <= MAX_STACK_LINES) {
728
+ return stack;
729
+ }
730
+ return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
731
+ }
666
732
 
667
733
  /** A run may either be a Span or a Generation */
668
734
 
@@ -914,7 +980,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
914
980
  eventProperties['$process_person_profile'] = false;
915
981
  }
916
982
  if (outputs instanceof Error) {
917
- eventProperties['$ai_error'] = outputs.toString();
983
+ eventProperties['$ai_error'] = stringifyError(outputs);
918
984
  eventProperties['$ai_is_error'] = true;
919
985
  } else if (outputs !== undefined) {
920
986
  eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
@@ -960,7 +1026,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
960
1026
  }
961
1027
  if (output instanceof Error) {
962
1028
  eventProperties['$ai_http_status'] = output.status || 500;
963
- eventProperties['$ai_error'] = output.toString();
1029
+ eventProperties['$ai_error'] = stringifyError(output);
964
1030
  eventProperties['$ai_is_error'] = true;
965
1031
  } else {
966
1032
  // Handle token usage