@posthog/ai 4.0.1 → 4.1.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.
- package/CHANGELOG.md +4 -0
- package/lib/anthropic/index.cjs.js +2 -1
- package/lib/anthropic/index.cjs.js.map +1 -1
- package/lib/anthropic/index.esm.js +2 -1
- package/lib/anthropic/index.esm.js.map +1 -1
- package/lib/index.cjs.js +73 -29
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.esm.js +73 -29
- package/lib/index.esm.js.map +1 -1
- package/lib/langchain/index.cjs.js.map +1 -1
- package/lib/langchain/index.esm.js.map +1 -1
- package/lib/openai/index.cjs.js +2 -1
- package/lib/openai/index.cjs.js.map +1 -1
- package/lib/openai/index.esm.js +2 -1
- package/lib/openai/index.esm.js.map +1 -1
- package/lib/posthog-ai/src/utils.d.ts +1 -0
- package/lib/vercel/index.cjs.js +73 -29
- package/lib/vercel/index.cjs.js.map +1 -1
- package/lib/vercel/index.esm.js +73 -29
- package/lib/vercel/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/utils.ts +19 -1
- package/src/vercel/middleware.ts +44 -19
package/lib/index.esm.js
CHANGED
|
@@ -5,6 +5,9 @@ import { Buffer } from 'buffer';
|
|
|
5
5
|
import { experimental_wrapLanguageModel } from 'ai';
|
|
6
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
7
|
|
|
8
|
+
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
9
|
+
const MAX_OUTPUT_SIZE = 200000;
|
|
10
|
+
const STRING_FORMAT = 'utf8';
|
|
8
11
|
const getModelParams = params => {
|
|
9
12
|
if (!params) {
|
|
10
13
|
return {};
|
|
@@ -60,13 +63,26 @@ const mergeSystemPrompt = (params, provider) => {
|
|
|
60
63
|
const withPrivacyMode = (client, privacyMode, input) => {
|
|
61
64
|
return client.privacy_mode || privacyMode ? null : input;
|
|
62
65
|
};
|
|
66
|
+
const truncate = str => {
|
|
67
|
+
try {
|
|
68
|
+
const buffer = Buffer.from(str, STRING_FORMAT);
|
|
69
|
+
if (buffer.length <= MAX_OUTPUT_SIZE) {
|
|
70
|
+
return str;
|
|
71
|
+
}
|
|
72
|
+
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
73
|
+
return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('Error truncating, likely not a string');
|
|
76
|
+
return str;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
63
79
|
function sanitizeValues(obj) {
|
|
64
80
|
if (obj === undefined || obj === null) {
|
|
65
81
|
return obj;
|
|
66
82
|
}
|
|
67
83
|
const jsonSafe = JSON.parse(JSON.stringify(obj));
|
|
68
84
|
if (typeof jsonSafe === 'string') {
|
|
69
|
-
return Buffer.from(jsonSafe,
|
|
85
|
+
return Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT);
|
|
70
86
|
} else if (Array.isArray(jsonSafe)) {
|
|
71
87
|
return jsonSafe.map(sanitizeValues);
|
|
72
88
|
} else if (jsonSafe && typeof jsonSafe === 'object') {
|
|
@@ -488,14 +504,26 @@ const mapVercelParams = params => {
|
|
|
488
504
|
};
|
|
489
505
|
};
|
|
490
506
|
const mapVercelPrompt = prompt => {
|
|
491
|
-
|
|
507
|
+
// normalize single inputs into an array of messages
|
|
508
|
+
let promptsArray;
|
|
509
|
+
if (typeof prompt === 'string') {
|
|
510
|
+
promptsArray = [{
|
|
511
|
+
role: 'user',
|
|
512
|
+
content: prompt
|
|
513
|
+
}];
|
|
514
|
+
} else if (!Array.isArray(prompt)) {
|
|
515
|
+
promptsArray = [prompt];
|
|
516
|
+
} else {
|
|
517
|
+
promptsArray = prompt;
|
|
518
|
+
}
|
|
519
|
+
return promptsArray.map(p => {
|
|
492
520
|
let content = {};
|
|
493
521
|
if (Array.isArray(p.content)) {
|
|
494
522
|
content = p.content.map(c => {
|
|
495
523
|
if (c.type === 'text') {
|
|
496
524
|
return {
|
|
497
525
|
type: 'text',
|
|
498
|
-
content: c.text
|
|
526
|
+
content: truncate(c.text)
|
|
499
527
|
};
|
|
500
528
|
} else if (c.type === 'image') {
|
|
501
529
|
return {
|
|
@@ -541,7 +569,7 @@ const mapVercelPrompt = prompt => {
|
|
|
541
569
|
} else {
|
|
542
570
|
content = {
|
|
543
571
|
type: 'text',
|
|
544
|
-
text: p.content
|
|
572
|
+
text: truncate(p.content)
|
|
545
573
|
};
|
|
546
574
|
}
|
|
547
575
|
return {
|
|
@@ -551,46 +579,62 @@ const mapVercelPrompt = prompt => {
|
|
|
551
579
|
});
|
|
552
580
|
};
|
|
553
581
|
const mapVercelOutput = result => {
|
|
582
|
+
// normalize string results to object
|
|
583
|
+
const normalizedResult = typeof result === 'string' ? {
|
|
584
|
+
text: result
|
|
585
|
+
} : result;
|
|
554
586
|
const output = {
|
|
555
|
-
...(
|
|
556
|
-
text:
|
|
587
|
+
...(normalizedResult.text ? {
|
|
588
|
+
text: normalizedResult.text
|
|
589
|
+
} : {}),
|
|
590
|
+
...(normalizedResult.object ? {
|
|
591
|
+
object: normalizedResult.object
|
|
557
592
|
} : {}),
|
|
558
|
-
...(
|
|
559
|
-
|
|
593
|
+
...(normalizedResult.reasoning ? {
|
|
594
|
+
reasoning: normalizedResult.reasoning
|
|
560
595
|
} : {}),
|
|
561
|
-
...(
|
|
562
|
-
|
|
596
|
+
...(normalizedResult.response ? {
|
|
597
|
+
response: normalizedResult.response
|
|
563
598
|
} : {}),
|
|
564
|
-
...(
|
|
565
|
-
|
|
599
|
+
...(normalizedResult.finishReason ? {
|
|
600
|
+
finishReason: normalizedResult.finishReason
|
|
566
601
|
} : {}),
|
|
567
|
-
...(
|
|
568
|
-
|
|
602
|
+
...(normalizedResult.usage ? {
|
|
603
|
+
usage: normalizedResult.usage
|
|
569
604
|
} : {}),
|
|
570
|
-
...(
|
|
571
|
-
|
|
605
|
+
...(normalizedResult.warnings ? {
|
|
606
|
+
warnings: normalizedResult.warnings
|
|
572
607
|
} : {}),
|
|
573
|
-
...(
|
|
574
|
-
|
|
608
|
+
...(normalizedResult.providerMetadata ? {
|
|
609
|
+
toolCalls: normalizedResult.providerMetadata
|
|
575
610
|
} : {}),
|
|
576
|
-
...(
|
|
577
|
-
|
|
611
|
+
...(normalizedResult.files ? {
|
|
612
|
+
files: normalizedResult.files.map(file => ({
|
|
613
|
+
name: file.name,
|
|
614
|
+
size: file.size,
|
|
615
|
+
type: file.type
|
|
616
|
+
}))
|
|
578
617
|
} : {})
|
|
579
618
|
};
|
|
580
|
-
// if text and no object or reasoning, return text
|
|
581
619
|
if (output.text && !output.object && !output.reasoning) {
|
|
582
620
|
return [{
|
|
583
|
-
content: output.text,
|
|
621
|
+
content: truncate(output.text),
|
|
584
622
|
role: 'assistant'
|
|
585
623
|
}];
|
|
586
624
|
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
625
|
+
// otherwise stringify and truncate
|
|
626
|
+
try {
|
|
627
|
+
const jsonOutput = JSON.stringify(output);
|
|
628
|
+
return [{
|
|
629
|
+
content: truncate(jsonOutput),
|
|
630
|
+
role: 'assistant'
|
|
631
|
+
}];
|
|
632
|
+
} catch (error) {
|
|
633
|
+
console.error('Error stringifying output');
|
|
634
|
+
return [];
|
|
635
|
+
}
|
|
591
636
|
};
|
|
592
637
|
const extractProvider = model => {
|
|
593
|
-
// vercel provider is in the format of provider.endpoint
|
|
594
638
|
const provider = model.provider.toLowerCase();
|
|
595
639
|
const providerName = provider.split('.')[0];
|
|
596
640
|
return providerName;
|
|
@@ -668,7 +712,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
|
|
|
668
712
|
outputTokens: 0
|
|
669
713
|
},
|
|
670
714
|
isError: true,
|
|
671
|
-
error: JSON.stringify(error)
|
|
715
|
+
error: truncate(JSON.stringify(error))
|
|
672
716
|
});
|
|
673
717
|
throw error;
|
|
674
718
|
}
|
|
@@ -760,7 +804,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
|
|
|
760
804
|
outputTokens: 0
|
|
761
805
|
},
|
|
762
806
|
isError: true,
|
|
763
|
-
error: JSON.stringify(error)
|
|
807
|
+
error: truncate(JSON.stringify(error))
|
|
764
808
|
});
|
|
765
809
|
throw error;
|
|
766
810
|
}
|