fix(pdf): reject empty parsed page ranges (#97698)

This commit is contained in:
xingzhou
2026-07-01 08:46:45 +08:00
committed by GitHub
parent e2dd5a0309
commit aadd57c31c
3 changed files with 29 additions and 1 deletions
@@ -62,6 +62,10 @@ describe("parsePageRange", () => {
expect(parsePageRange("1-100", 5)).toEqual([1, 2, 3, 4, 5]);
});
it("throws when no requested pages are within maxPages", () => {
expect(() => parsePageRange("999", 20)).toThrow('No PDF pages matched requested range "999"');
});
it("deduplicates and sorts", () => {
expect(parsePageRange("5,3,1,3,5", 20)).toEqual([1, 3, 5]);
});
+5 -1
View File
@@ -74,7 +74,11 @@ export function parsePageRange(range: string, maxPages: number): number[] {
}
}
}
return Array.from(pages).toSorted((a, b) => a - b);
const parsedPages = Array.from(pages).toSorted((a, b) => a - b);
if (parsedPages.length === 0) {
throw new Error(`No PDF pages matched requested range "${range}"`);
}
return parsedPages;
}
/** Converts a provider assistant message into PDF text or throws a model-labelled failure. */
+20
View File
@@ -548,6 +548,26 @@ describe("createPdfTool", () => {
});
});
it("rejects explicit page ranges that resolve to no pages before native PDF analysis", async () => {
await withTempPdfAgentDir(async (agentDir) => {
await stubPdfToolInfra(agentDir, { provider: "anthropic", input: ["text", "document"] });
const nativeSpy = vi
.spyOn(pdfNativeProviders, "anthropicAnalyzePdf")
.mockResolvedValue("native summary");
const cfg = withPdfModel(ANTHROPIC_PDF_MODEL);
const tool = requirePdfTool((await loadCreatePdfTool())({ config: cfg, agentDir }));
await expect(
tool.execute("t1", {
prompt: "summarize",
pdf: "/tmp/doc.pdf",
pages: "999",
}),
).rejects.toThrow('No PDF pages matched requested range "999"');
expect(nativeSpy).not.toHaveBeenCalled();
});
});
it("rejects password parameter for native PDF providers", async () => {
await withTempPdfAgentDir(async (agentDir) => {
await stubPdfToolInfra(agentDir, { provider: "anthropic", input: ["text", "document"] });