Skip to content

Commit 25c9a93

Browse files
committed
we should fit truncation in 20KB
1 parent fae419d commit 25c9a93

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

packages/core/src/tracing/ai/messageTruncation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ const jsonBytes = (value: unknown): number => {
6262
};
6363

6464
/**
65-
* Truncate a string to fit within maxBytes when encoded as UTF-8.
65+
* Truncate a string to fit within maxBytes (inclusive) when encoded as UTF-8.
6666
* Uses binary search for efficiency with multi-byte characters.
6767
*
6868
* @param text - The string to truncate
69-
* @param maxBytes - Maximum byte length (UTF-8 encoded)
70-
* @returns Truncated string that fits within maxBytes
69+
* @param maxBytes - Maximum byte length (inclusive, UTF-8 encoded)
70+
* @returns Truncated string whose UTF-8 byte length is at most maxBytes
7171
*/
7272
function truncateTextByBytes(text: string, maxBytes: number): string {
73-
if (utf8Bytes(text) < maxBytes) {
73+
if (utf8Bytes(text) <= maxBytes) {
7474
return text;
7575
}
7676

@@ -83,7 +83,7 @@ function truncateTextByBytes(text: string, maxBytes: number): string {
8383
const candidate = text.slice(0, mid);
8484
const byteSize = utf8Bytes(candidate);
8585

86-
if (byteSize < maxBytes) {
86+
if (byteSize <= maxBytes) {
8787
bestFit = candidate;
8888
low = mid + 1;
8989
} else {

packages/core/test/lib/tracing/ai-message-truncation.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ describe('message truncation utilities', () => {
428428
it('keeps only the last message with truncation when it does not fit the limit', () => {
429429
const messages = [{ content: `1 ${humongous}` }, { content: `2 ${humongous}` }, { content: `3 ${humongous}` }];
430430
const result = truncateGenAiMessages(messages);
431-
const truncLen = 20_000 - 2 - JSON.stringify({ content: '' }).length - 1;
431+
const truncLen = 20_000 - 2 - JSON.stringify({ content: '' }).length;
432432
expect(result).toStrictEqual([{ content: `3 ${humongous}`.substring(0, truncLen) }]);
433433
});
434434

@@ -450,7 +450,7 @@ describe('message truncation utilities', () => {
450450
it('truncates if the message content string will not fit', () => {
451451
const messages = [{ content: `2 ${humongous}` }];
452452
const result = truncateGenAiMessages(messages);
453-
const truncLen = 20_000 - 2 - JSON.stringify({ content: '' }).length - 1;
453+
const truncLen = 20_000 - 2 - JSON.stringify({ content: '' }).length;
454454
expect(result).toStrictEqual([{ content: `2 ${humongous}`.substring(0, truncLen) }]);
455455
});
456456

@@ -491,8 +491,7 @@ describe('message truncation utilities', () => {
491491
2 -
492492
JSON.stringify({
493493
parts: ['', { some_other_field: 'no text here', text: '' }],
494-
}).length -
495-
1;
494+
}).length;
496495

497496
expect(result).toStrictEqual([
498497
{
@@ -513,8 +512,7 @@ describe('message truncation utilities', () => {
513512
2 -
514513
JSON.stringify({
515514
parts: [{ text: '' }],
516-
}).length -
517-
1;
515+
}).length;
518516
expect(result).toStrictEqual([
519517
{
520518
parts: [

0 commit comments

Comments
 (0)