@posthog/ai 8.6.4 → 8.6.6

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.
@@ -35,14 +35,16 @@ const FILE_FAMILY_TYPES = new Set(['file', 'input_file', 'document', 'media', 'f
35
35
  const KNOWN_AUDIO_FORMATS = new Set(['wav', 'mp3', 'ogg', 'flac', 'm4a', 'aac', 'webm']);
36
36
  class MediaTypeContext {
37
37
  static EMPTY = new MediaTypeContext(undefined, undefined);
38
- constructor(parent, key) {
38
+ constructor(parent, key, explicitMediaType) {
39
39
  this.parent = parent;
40
40
  this.key = key;
41
+ this.explicitMediaType = explicitMediaType;
41
42
  }
42
43
  inferMediaType() {
43
44
  return this.inferFromSiblingMime() ?? this.inferFromSiblingFormat() ?? this.inferFromParentType() ?? this.inferFromKey();
44
45
  }
45
46
  inferFromSiblingMime() {
47
+ if (this.explicitMediaType) return this.explicitMediaType;
46
48
  if (!this.parent) return undefined;
47
49
  for (const hint of MIME_HINT_KEYS) {
48
50
  const v = this.parent[hint];
@@ -77,7 +79,13 @@ class MediaTypeContext {
77
79
  if (key.includes('file') || key.includes('document')) return 'application/octet-stream';
78
80
  return undefined;
79
81
  }
82
+ hasExplicitBinaryMediaType() {
83
+ if (!this.explicitMediaType && (!this.parent || !this.key || !STRONG_CONTEXT_KEYS.has(this.key))) return false;
84
+ const mediaType = this.inferFromSiblingMime();
85
+ return mediaType !== undefined && !mediaType.toLowerCase().startsWith('text/');
86
+ }
80
87
  signalsBinary() {
88
+ if (this.explicitMediaType) return true;
81
89
  if (this.parent) {
82
90
  for (const hint of MIME_HINT_KEYS) {
83
91
  if (typeof this.parent[hint] === 'string') return true;
@@ -99,10 +107,10 @@ class BinaryContentRedactor {
99
107
  constructor(recognizer = new Base64Recognizer()) {
100
108
  this.recognizer = recognizer;
101
109
  }
102
- redact(value) {
110
+ redact(value, mediaType) {
103
111
  if (this.isMultimodalEnabled()) return value;
104
112
  this.visited = new WeakSet();
105
- return this.walk(value, MediaTypeContext.EMPTY);
113
+ return this.walk(value, mediaType ? new MediaTypeContext(undefined, undefined, mediaType) : MediaTypeContext.EMPTY);
106
114
  }
107
115
  walk(value, ctx) {
108
116
  if (value === null || value === undefined) return value;
@@ -126,8 +134,10 @@ class BinaryContentRedactor {
126
134
  return out;
127
135
  }
128
136
  redactString(value, ctx) {
129
- const minLength = ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
130
- const recognition = this.recognizer.recognize(value, minLength);
137
+ const hasExplicitBinaryMediaType = ctx.hasExplicitBinaryMediaType();
138
+ const recognitionValue = hasExplicitBinaryMediaType ? value.replace(/[\r\n]/g, '') : value;
139
+ const minLength = hasExplicitBinaryMediaType ? Math.min(recognitionValue.length, STRONG_CONTEXT_MIN_LENGTH) : ctx.signalsBinary() ? STRONG_CONTEXT_MIN_LENGTH : WEAK_CONTEXT_MIN_LENGTH;
140
+ const recognition = this.recognizer.recognize(recognitionValue, minLength);
131
141
  switch (recognition.kind) {
132
142
  case 'data-url':
133
143
  return this.placeholderFor(recognition.mediaType);
@@ -292,7 +302,7 @@ function addDefaults(params) {
292
302
  };
293
303
  }
294
304
 
295
- var version = "8.6.4";
305
+ var version = "8.6.6";
296
306
 
297
307
  const DEFAULT_MAX_DEPTH = 3;
298
308
  const MAX_STACK_LINES = 20;
@@ -490,7 +500,9 @@ const captureAiGeneration = async (client, options) => {
490
500
  $ai_output_tokens: usage.outputTokens
491
501
  } : {}),
492
502
  ...additionalTokenValues,
493
- $ai_latency: options.latency ?? 0,
503
+ ...(options.latency !== undefined ? {
504
+ $ai_latency: options.latency
505
+ } : {}),
494
506
  ...(options.timeToFirstToken !== undefined ? {
495
507
  $ai_time_to_first_token: options.timeToFirstToken
496
508
  } : {}),