Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/utils/toolContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ import {
type ToolContent = BaseMessage['content'];

describe('toolContent', () => {
it('counts bounded inline images without exhausting structured character work', () => {
const content: ToolContent = [
{
type: 'image_url',
image_url: {
url: `data:image/png;base64,${Buffer.alloc(2_477_915).toString('base64')}`,
},
},
];
expect(getToolContentCharLength(content)).toBe(
JSON.stringify(content).length
);
expect(compactToolContent(content, 4_000_000).changed).toBe(false);
expect(compactToolContent(content, 400_000).changed).toBe(true);
});

it('retains character traversal protection for escaped and oversized strings', () => {
for (const url of [
`data:image/png;"base64,${'A'.repeat(1_100_000)}`,
`data:image/png;base64,${'A'.repeat(5_000_004)}`,
'A'.repeat(1_100_000),
]) {
const content: ToolContent = [{ type: 'image_url', image_url: { url } }];
expect(compactToolContent(content, 8_000_000).changed).toBe(true);
}
});

it('normalizes a small opaque array to provider-neutral text', () => {
const content = [{ type: 'json', rows: [{ id: 1 }] }] as ToolContent;

Expand Down
10 changes: 10 additions & 0 deletions src/utils/toolContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ function jsonStringLength(
limit: number,
work: StructuredWorkState
): number {
/** Bounded inline images contain no JSON escapes; count their characters directly. */
if (
value.length <= HARD_MAX_TOTAL_TOOL_OUTPUT_SIZE &&
value.startsWith('data:image/') &&
/^data:image\/[a-z0-9.+-]+;base64,[a-z0-9+/]+={0,2}$/i.test(value)
) {
return value.length + 2 > limit
? conservativeStructuredLength(limit)
: value.length + 2;
}
let length = 2;
for (let i = 0; i < value.length; i++) {
if (!consumeStructuredCharacterWork(work)) {
Expand Down