fix(agent): enforce page range validation and refine pdftoppm error check

This commit is contained in:
yiling
2026-07-19 10:17:21 +08:00
parent f68ba70656
commit 6064a1449a
5 changed files with 85 additions and 4 deletions
@@ -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)
+39 -1
View File
@@ -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 },
@@ -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")
})
@@ -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.`
}
}
+14 -2
View File
@@ -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<string, unknown>)?.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<string, unknown>)?.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 },