@posthog/ai 7.8.9 → 7.8.11

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
@@ -1,12 +1,11 @@
1
1
  import { OpenAI, AzureOpenAI } from 'openai';
2
- import { Buffer } from 'buffer';
3
2
  import * as uuid from 'uuid';
4
3
  import { v4 } from 'uuid';
5
4
  import { uuidv7 } from '@posthog/core';
6
5
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
6
  import { GoogleGenAI } from '@google/genai';
8
7
 
9
- var version = "7.8.9";
8
+ var version = "7.8.11";
10
9
 
11
10
  // Type guards for safer type checking
12
11
  const isString = value => {
@@ -414,9 +413,17 @@ const formatResponseGemini = response => {
414
413
  // Handle audio/media inline data
415
414
  const mimeType = part.inlineData.mimeType || 'audio/pcm';
416
415
  let data = part.inlineData.data;
417
- // Handle binary data (Buffer/Uint8Array -> base64)
418
- if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
419
- data = Buffer.from(data).toString('base64');
416
+ // Handle binary data (Uint8Array/Buffer -> base64)
417
+ if (data instanceof Uint8Array) {
418
+ if (typeof Buffer !== 'undefined') {
419
+ data = Buffer.from(data).toString('base64');
420
+ } else {
421
+ let binary = '';
422
+ for (let i = 0; i < data.length; i++) {
423
+ binary += String.fromCharCode(data[i]);
424
+ }
425
+ data = btoa(binary);
426
+ }
420
427
  }
421
428
  // Sanitize base64 data for images and other large inline data
422
429
  data = redactBase64DataUrl(data);
@@ -642,7 +649,8 @@ function sanitizeValues(obj) {
642
649
  }
643
650
  const jsonSafe = JSON.parse(JSON.stringify(obj));
644
651
  if (typeof jsonSafe === 'string') {
645
- return Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT);
652
+ // Sanitize lone surrogates by round-tripping through UTF-8
653
+ return new TextDecoder().decode(new TextEncoder().encode(jsonSafe));
646
654
  } else if (Array.isArray(jsonSafe)) {
647
655
  return jsonSafe.map(sanitizeValues);
648
656
  } else if (jsonSafe && typeof jsonSafe === 'object') {
@@ -2111,11 +2119,12 @@ const mapVercelPrompt = messages => {
2111
2119
  });
2112
2120
  try {
2113
2121
  // Trim the inputs array until its JSON size fits within MAX_OUTPUT_SIZE
2122
+ const encoder = new TextEncoder();
2114
2123
  let serialized = JSON.stringify(inputs);
2115
2124
  let removedCount = 0;
2116
2125
  // We need to keep track of the initial size of the inputs array because we're going to be mutating it
2117
2126
  const initialSize = inputs.length;
2118
- for (let i = 0; i < initialSize && Buffer.byteLength(serialized, 'utf8') > MAX_OUTPUT_SIZE; i++) {
2127
+ for (let i = 0; i < initialSize && encoder.encode(serialized).byteLength > MAX_OUTPUT_SIZE; i++) {
2119
2128
  inputs.shift();
2120
2129
  removedCount++;
2121
2130
  serialized = JSON.stringify(inputs);