@posthog/ai 6.3.0 → 6.3.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.
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ import { wrapLanguageModel } from 'ai';
6
6
  import AnthropicOriginal from '@anthropic-ai/sdk';
7
7
  import { GoogleGenAI } from '@google/genai';
8
8
 
9
- var version = "6.3.0";
9
+ var version = "6.3.1";
10
10
 
11
11
  // limit large outputs by truncating to 200kb (approx 200k bytes)
12
12
  const MAX_OUTPUT_SIZE = 200000;
@@ -206,18 +206,43 @@ const mergeSystemPrompt = (params, provider) => {
206
206
  const withPrivacyMode = (client, privacyMode, input) => {
207
207
  return client.privacy_mode || privacyMode ? null : input;
208
208
  };
209
- const truncate = str => {
209
+ function toSafeString(input) {
210
+ if (input === undefined || input === null) {
211
+ return '';
212
+ }
213
+ if (typeof input === 'string') {
214
+ return input;
215
+ }
210
216
  try {
211
- const buffer = Buffer.from(str, STRING_FORMAT);
212
- if (buffer.length <= MAX_OUTPUT_SIZE) {
213
- return str;
214
- }
215
- const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
216
- return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
217
+ return JSON.stringify(input);
217
218
  } catch {
218
- console.error('Error truncating, likely not a string');
219
- return str;
219
+ console.warn('Failed to stringify input', input);
220
+ return '';
221
+ }
222
+ }
223
+ const truncate = input => {
224
+ const str = toSafeString(input);
225
+ if (str === '') {
226
+ return '';
227
+ }
228
+ // Check if we need to truncate and ensure STRING_FORMAT is respected
229
+ const encoder = new TextEncoder();
230
+ const buffer = encoder.encode(str);
231
+ if (buffer.length <= MAX_OUTPUT_SIZE) {
232
+ // Ensure STRING_FORMAT is respected
233
+ return new TextDecoder(STRING_FORMAT).decode(buffer);
234
+ }
235
+ // Truncate the buffer and ensure a valid string is returned
236
+ const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
237
+ // fatal: false means we get U+FFFD at the end if truncation broke the encoding
238
+ const decoder = new TextDecoder(STRING_FORMAT, {
239
+ fatal: false
240
+ });
241
+ let truncatedStr = decoder.decode(truncatedBuffer);
242
+ if (truncatedStr.endsWith('\uFFFD')) {
243
+ truncatedStr = truncatedStr.slice(0, -1);
220
244
  }
245
+ return `${truncatedStr}... [truncated]`;
221
246
  };
222
247
  /**
223
248
  * Extract available tool calls from the request parameters.