From 6064a1449a303d0ec0b8d3a436db563a18e900ba Mon Sep 17 00:00:00 2001 From: yiling Date: Sun, 19 Jul 2026 10:17:21 +0800 Subject: [PATCH] fix(agent): enforce page range validation and refine pdftoppm error check --- .../agent/src/tools/read-pdf-pages.test.ts | 30 ++++++++++++++ packages/agent/src/tools/read-pdf-pages.ts | 40 ++++++++++++++++++- .../agent-chat-prompt-builder.test.ts | 1 + .../workflows/agent-chat-prompt-builder.ts | 2 +- packages/core/src/transcode/transcode.ts | 16 +++++++- 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/packages/agent/src/tools/read-pdf-pages.test.ts b/packages/agent/src/tools/read-pdf-pages.test.ts index 97476b02..eb714cdf 100644 --- a/packages/agent/src/tools/read-pdf-pages.test.ts +++ b/packages/agent/src/tools/read-pdf-pages.test.ts @@ -126,6 +126,36 @@ describe('readPdfPagesTool', () => { expect(result.details.sourceKeys).toEqual(['pdf_pages/page_1.webp', 'pdf_pages/page_2.webp']) }) + it('should return error text if start page is less than 1', async () => { + const tool = createReadPdfPagesTool('asset-1') + const result = await tool.execute('call-1', { start: 0, end: 5 }) + + expect(result.content[0]).toEqual({ + type: 'text', + text: 'Invalid page range: start page (0) must be at least 1.', + }) + }) + + it('should return error text if start page is greater than end page', async () => { + const tool = createReadPdfPagesTool('asset-1') + const result = await tool.execute('call-1', { start: 5, end: 2 }) + + expect(result.content[0]).toEqual({ + type: 'text', + text: 'Invalid page range: start page (5) must be less than or equal to end page (2).', + }) + }) + + it('should return error text if page range exceeds maximum limit of 20', async () => { + const tool = createReadPdfPagesTool('asset-1') + const result = await tool.execute('call-1', { start: 1, end: 25 }) + + expect(result.content[0]).toEqual({ + type: 'text', + text: 'Page range (25 pages requested) exceeds the maximum limit of 20 pages per request.', + }) + }) + it('should return error text if asset not found', async () => { vi.mocked(prisma.asset.findUnique).mockResolvedValue(null) diff --git a/packages/agent/src/tools/read-pdf-pages.ts b/packages/agent/src/tools/read-pdf-pages.ts index d164b616..158f77d5 100644 --- a/packages/agent/src/tools/read-pdf-pages.ts +++ b/packages/agent/src/tools/read-pdf-pages.ts @@ -23,10 +23,48 @@ export function createReadPdfPagesTool( name: 'read_pdf_pages', label: 'Read PDF Pages', description: - 'Converts specific page range of a PDF document into 1080p WebP images for visual analysis.', + 'Converts specific page range of a PDF document into 1080p WebP images for visual analysis. Maximum 20 pages allowed per call.', parameters: readPdfPagesSchema, execute: async (_toolCallId, params) => { try { + if (params.start < 1) { + return { + content: [ + { + type: 'text', + text: `Invalid page range: start page (${params.start}) must be at least 1.`, + }, + ], + details: {}, + } + } + + if (params.start > params.end) { + return { + content: [ + { + type: 'text', + text: `Invalid page range: start page (${params.start}) must be less than or equal to end page (${params.end}).`, + }, + ], + details: {}, + } + } + + const MAX_PAGE_RANGE = 20 + const requestedPages = params.end - params.start + 1 + if (requestedPages > MAX_PAGE_RANGE) { + return { + content: [ + { + type: 'text', + text: `Page range (${requestedPages} pages requested) exceeds the maximum limit of ${MAX_PAGE_RANGE} pages per request.`, + }, + ], + details: {}, + } + } + const targetAssetId = params.assetId || assetId const asset = await prisma.asset.findUnique({ where: { id: targetAssetId }, diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts index 745706fb..11730672 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.test.ts @@ -62,6 +62,7 @@ describe('AgentChatPromptBuilder', () => { expect(result).toContain('File Type: pdf') expect(result).toContain('Total Pages: 15') expect(result).toContain("call the 'read_pdf_pages' tool") + expect(result).toContain('Maximum 20 pages allowed per call.') expect(result).not.toContain("call the 'screenshot' tool") expect(result).not.toContain("call the 'analyze_image' tool") }) diff --git a/packages/agent/src/workflows/agent-chat-prompt-builder.ts b/packages/agent/src/workflows/agent-chat-prompt-builder.ts index 7634d771..1bf5dc5a 100644 --- a/packages/agent/src/workflows/agent-chat-prompt-builder.ts +++ b/packages/agent/src/workflows/agent-chat-prompt-builder.ts @@ -149,7 +149,7 @@ export class AgentChatPromptBuilder { } else if (type === 'video') { instruction += `\n\nIf you need to view visual frames or take screenshots of the video, call the 'screenshot' tool. You must specify the 'start' (seconds), 'end' (seconds), and 'count' (number of screenshots) parameters.` } else if (type === 'pdf') { - instruction += `\n\nIf you need to view pages of the PDF document, call the 'read_pdf_pages' tool. You must specify the 'start' (page number) and 'end' (page number) parameters.` + instruction += `\n\nIf you need to view pages of the PDF document, call the 'read_pdf_pages' tool. You must specify the 'start' (page number) and 'end' (page number) parameters. Maximum 20 pages allowed per call.` } } diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 9bd11fc9..9b34fb17 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -299,8 +299,14 @@ export class TranscodeService { try { await execFileAsync('pdftoppm', ['-png', '-f', '1', '-l', '100', inputFile, pagePrefix]) } catch (err) { + const errCode = (err as Record)?.code const msg = err instanceof Error ? err.message : String(err) - if (msg.includes('pdftoppm') || msg.includes('ENOENT') || msg.includes('not found')) { + const lower = msg.toLowerCase() + if ( + errCode === 'ENOENT' || + lower.includes('enoent') || + (lower.includes('not found') && lower.includes('pdftoppm')) + ) { throw new Error( `pdftoppm executable not found in $PATH. Please install poppler-utils / poppler. (${msg})`, { cause: err }, @@ -694,8 +700,14 @@ export class TranscodeService { pagePrefix, ]) } catch (err) { + const errCode = (err as Record)?.code const msg = err instanceof Error ? err.message : String(err) - if (msg.includes('pdftoppm') || msg.includes('ENOENT') || msg.includes('not found')) { + const lower = msg.toLowerCase() + if ( + errCode === 'ENOENT' || + lower.includes('enoent') || + (lower.includes('not found') && lower.includes('pdftoppm')) + ) { throw new Error( `pdftoppm executable not found in $PATH. Please install poppler-utils / poppler. (${msg})`, { cause: err },