@posthog/ai 6.4.2 → 6.4.4

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.4.2";
9
+ var version = "6.4.4";
10
10
 
11
11
  // Type guards for safer type checking
12
12
  const isString = value => {
@@ -19,6 +19,31 @@ const isObject = value => {
19
19
  // limit large outputs by truncating to 200kb (approx 200k bytes)
20
20
  const MAX_OUTPUT_SIZE = 200000;
21
21
  const STRING_FORMAT = 'utf8';
22
+ /**
23
+ * Safely converts content to a string, preserving structure for objects/arrays.
24
+ * - If content is already a string, returns it as-is
25
+ * - If content is an object or array, stringifies it with JSON.stringify to preserve structure
26
+ * - Otherwise, converts to string with String()
27
+ *
28
+ * This prevents the "[object Object]" bug when objects are naively converted to strings.
29
+ *
30
+ * @param content - The content to convert to a string
31
+ * @returns A string representation that preserves structure for complex types
32
+ */
33
+ function toContentString(content) {
34
+ if (typeof content === 'string') {
35
+ return content;
36
+ }
37
+ if (content !== undefined && content !== null && typeof content === 'object') {
38
+ try {
39
+ return JSON.stringify(content);
40
+ } catch {
41
+ // Fallback for circular refs, BigInt, or objects with throwing toJSON
42
+ return String(content);
43
+ }
44
+ }
45
+ return String(content);
46
+ }
22
47
  const getModelParams = params => {
23
48
  if (!params) {
24
49
  return {};
@@ -446,15 +471,16 @@ function formatOpenAIResponsesInput(input, instructions) {
446
471
  } else if (item && typeof item === 'object') {
447
472
  const obj = item;
448
473
  const role = isString(obj.role) ? obj.role : 'user';
449
- const content = obj.content || obj.text || String(item);
474
+ // Handle content properly - preserve structure for objects/arrays
475
+ const content = obj.content ?? obj.text ?? item;
450
476
  messages.push({
451
477
  role,
452
- content: String(content)
478
+ content: toContentString(content)
453
479
  });
454
480
  } else {
455
481
  messages.push({
456
482
  role: 'user',
457
- content: String(item)
483
+ content: toContentString(item)
458
484
  });
459
485
  }
460
486
  }
@@ -466,7 +492,7 @@ function formatOpenAIResponsesInput(input, instructions) {
466
492
  } else if (input) {
467
493
  messages.push({
468
494
  role: 'user',
469
- content: String(input)
495
+ content: toContentString(input)
470
496
  });
471
497
  }
472
498
  return messages;
@@ -1606,7 +1632,7 @@ const mapVercelPrompt = messages => {
1606
1632
  if (message.role === 'system') {
1607
1633
  content = [{
1608
1634
  type: 'text',
1609
- text: truncate(String(message.content))
1635
+ text: truncate(toContentString(message.content))
1610
1636
  }];
1611
1637
  } else {
1612
1638
  // Handle other roles which have array content
@@ -1664,7 +1690,7 @@ const mapVercelPrompt = messages => {
1664
1690
  // Fallback for non-array content
1665
1691
  content = [{
1666
1692
  type: 'text',
1667
- text: truncate(String(message.content))
1693
+ text: truncate(toContentString(message.content))
1668
1694
  }];
1669
1695
  }
1670
1696
  }
@@ -2479,7 +2505,7 @@ class WrappedModels {
2479
2505
  }
2480
2506
  return {
2481
2507
  role: 'user',
2482
- content: String(item)
2508
+ content: toContentString(item)
2483
2509
  };
2484
2510
  });
2485
2511
  }
@@ -2500,7 +2526,7 @@ class WrappedModels {
2500
2526
  }
2501
2527
  return [{
2502
2528
  role: 'user',
2503
- content: String(contents)
2529
+ content: toContentString(contents)
2504
2530
  }];
2505
2531
  }
2506
2532
  extractSystemInstruction(params) {
@@ -3492,7 +3518,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
3492
3518
  default:
3493
3519
  messageDict = {
3494
3520
  role: messageType,
3495
- content: String(message.content)
3521
+ content: toContentString(message.content)
3496
3522
  };
3497
3523
  break;
3498
3524
  }