@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.
@@ -220,6 +220,21 @@ const getModelParams = params => {
220
220
  const withPrivacyMode = (client, privacyMode, input) => {
221
221
  return client.privacy_mode || privacyMode ? null : input;
222
222
  };
223
+ function sanitizeValues(obj) {
224
+ if (obj === undefined || obj === null) {
225
+ return obj;
226
+ }
227
+ const jsonSafe = JSON.parse(JSON.stringify(obj));
228
+ if (typeof jsonSafe === 'string') {
229
+ // Sanitize lone surrogates by round-tripping through UTF-8
230
+ return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
231
+ } else if (Array.isArray(jsonSafe)) {
232
+ return jsonSafe.map(sanitizeValues);
233
+ } else if (jsonSafe && typeof jsonSafe === 'object') {
234
+ return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
235
+ }
236
+ return jsonSafe;
237
+ }
223
238
 
224
239
  function getDefaultExportFromCjs (x) {
225
240
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -684,7 +699,58 @@ var BaseCallbackHandler = class extends BaseCallbackHandlerMethodsClass {
684
699
  }
685
700
  };
686
701
 
687
- var version = "7.18.12";
702
+ var version = "7.19.0";
703
+
704
+ const DEFAULT_MAX_DEPTH = 3;
705
+ const MAX_STACK_LINES = 20;
706
+ function serializeError(value, depth = DEFAULT_MAX_DEPTH) {
707
+ if (depth < 0 || value === null || typeof value !== 'object') {
708
+ return value;
709
+ }
710
+ if (value instanceof Error) {
711
+ const out = {
712
+ name: value.name,
713
+ message: value.message,
714
+ stack: truncateStack(value.stack)
715
+ };
716
+ for (const key of Object.keys(value)) {
717
+ out[key] = serializeError(value[key], depth - 1);
718
+ }
719
+ if (value.cause !== undefined) {
720
+ out.cause = serializeError(value.cause, depth - 1);
721
+ }
722
+ return out;
723
+ }
724
+ if (Array.isArray(value)) {
725
+ return value.map(item => serializeError(item, depth - 1));
726
+ }
727
+ return value;
728
+ }
729
+ function stringifyError(error) {
730
+ try {
731
+ return JSON.stringify(sanitizeValues(serializeError(error)));
732
+ } catch {
733
+ if (error instanceof Error) {
734
+ return JSON.stringify({
735
+ name: error.name,
736
+ message: error.message
737
+ });
738
+ }
739
+ return JSON.stringify({
740
+ message: String(error)
741
+ });
742
+ }
743
+ }
744
+ function truncateStack(stack) {
745
+ if (!stack) {
746
+ return stack;
747
+ }
748
+ const lines = stack.split('\n');
749
+ if (lines.length <= MAX_STACK_LINES) {
750
+ return stack;
751
+ }
752
+ return [...lines.slice(0, MAX_STACK_LINES), '... (truncated)'].join('\n');
753
+ }
688
754
 
689
755
  /** A run may either be a Span or a Generation */
690
756
 
@@ -936,7 +1002,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
936
1002
  eventProperties['$process_person_profile'] = false;
937
1003
  }
938
1004
  if (outputs instanceof Error) {
939
- eventProperties['$ai_error'] = outputs.toString();
1005
+ eventProperties['$ai_error'] = stringifyError(outputs);
940
1006
  eventProperties['$ai_is_error'] = true;
941
1007
  } else if (outputs !== undefined) {
942
1008
  eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, outputs);
@@ -982,7 +1048,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
982
1048
  }
983
1049
  if (output instanceof Error) {
984
1050
  eventProperties['$ai_http_status'] = output.status || 500;
985
- eventProperties['$ai_error'] = output.toString();
1051
+ eventProperties['$ai_error'] = stringifyError(output);
986
1052
  eventProperties['$ai_is_error'] = true;
987
1053
  } else {
988
1054
  // Handle token usage