| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- import { describe, expect, it, vi } from "vitest";
- import {
- CoreErrorType,
- READ_PDF_EXTRACT_MAX_INPUT_BYTES,
- READ_PDF_NATIVE_MAX_INPUT_BYTES,
- createSessionId,
- createToolCallId,
- createTurnId,
- PdfDocumentPortError,
- ReadErrorCode,
- type FileSystemPort,
- type ImageProcessorPort,
- type PdfDocumentPort,
- } from "@zcode/contracts";
- import { readHandler, readToolEntry } from "../src/tool/handlers/read.js";
- import type { ToolExecutionContext } from "../src/tool/types.js";
- import { createTestRuntimeModel } from "./test-runtime-model.js";
- const PDF_PATH = "/tmp/report.pdf";
- describe("Read PDF handling", () => {
- it("returns the page summary followed by ordered prepared page images", async () => {
- const renderPages = vi.fn(async () => [
- {
- pageNumber: 2,
- data: new Uint8Array(Buffer.from("page-two")),
- mediaType: "image/jpeg" as const,
- },
- {
- pageNumber: 3,
- data: new Uint8Array(Buffer.from("page-three")),
- mediaType: "image/jpeg" as const,
- },
- ]);
- const result = await executeReadPdf(
- { file_path: PDF_PATH, pages: "2-3" },
- { supportsPdf: true, supportsImages: true },
- { renderPages },
- );
- expect(result.output).toMatchObject({
- type: "parts",
- filePath: PDF_PATH,
- numParts: 2,
- originalSize: 1_536,
- pages: [{ pageNumber: 2 }, { pageNumber: 3 }],
- });
- expect(result.modelContent).toEqual([
- { type: "text", text: `PDF pages extracted: 2 page(s) from ${PDF_PATH} (1.5KB)` },
- expect.objectContaining({
- type: "image",
- mediaType: "image/jpeg",
- dataUrl: `data:image/jpeg;base64,${Buffer.from("page-two").toString("base64")}`,
- }),
- expect.objectContaining({
- type: "image",
- mediaType: "image/jpeg",
- dataUrl: `data:image/jpeg;base64,${Buffer.from("page-three").toString("base64")}`,
- }),
- ]);
- expect(JSON.stringify(result.modelContent)).not.toMatch(
- /Page_\d+_(?:image|parse_result)|text layer/u,
- );
- });
- it("returns a unified tool error before Poppler when page images are unsupported", async () => {
- const renderPages = vi.fn();
- const result = await executeReadPdf(
- { file_path: PDF_PATH, pages: "1-2" },
- { supportsPdf: true, supportsImages: false },
- { renderPages },
- );
- expect(result.success).toBe(false);
- expect(result.output).toEqual({
- result: false,
- errorCode: ReadErrorCode.PDF_PAGES_IMAGES_UNSUPPORTED,
- message:
- "The current model supports PDF input but does not support image input; remove the pages parameter.",
- });
- expect(result.modelContent).toBe(
- "<tool_use_error>The current model supports PDF input but does not support image input; remove the pages parameter.</tool_use_error>",
- );
- expect(renderPages).not.toHaveBeenCalled();
- });
- it.each([
- ["unavailable", ReadErrorCode.PDF_CONFIGURATION_ERROR],
- ["corrupted", ReadErrorCode.PDF_INVALID],
- ["timeout", ReadErrorCode.PDF_TIMEOUT],
- ["password_protected", ReadErrorCode.PDF_PASSWORD_PROTECTED],
- ["page_out_of_range", ReadErrorCode.PDF_PAGE_OUT_OF_RANGE],
- ["permission_denied", ReadErrorCode.PDF_PERMISSION_DENIED],
- ["io_error", ReadErrorCode.PDF_IO_ERROR],
- ["process_failed", ReadErrorCode.PDF_PROCESS_FAILED],
- ] as const)("preserves the %s Poppler failure category", async (portCode, readCode) => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH, pages: "1" },
- { supportsPdf: true, supportsImages: true },
- {
- renderPages: async () => {
- throw new PdfDocumentPortError(portCode, `PDF failure: ${portCode}`);
- },
- },
- );
- expect(result.output).toEqual({
- result: false,
- errorCode: readCode,
- message: `PDF failure: ${portCode}`,
- });
- });
- it("normalizes paged Poppler cancellation for the executor", async () => {
- await expect(
- executeReadPdf(
- { file_path: PDF_PATH, pages: "1" },
- { supportsPdf: true, supportsImages: true },
- {
- renderPages: async () => {
- throw new PdfDocumentPortError("cancelled", "PDF extraction cancelled");
- },
- },
- ),
- ).rejects.toMatchObject({
- message: "PDF extraction cancelled",
- type: CoreErrorType.ToolCancelled,
- });
- });
- it("reads a small PDF natively without invoking Poppler rendering", async () => {
- const getPageCount = vi.fn(async () => 2);
- const renderPages = vi.fn();
- const result = await executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- { getPageCount, renderPages },
- );
- expect(result.success).toBe(true);
- expect(result.modelContent).toEqual([
- { type: "text", text: `PDF file read: ${PDF_PATH} (1.5KB)` },
- expect.objectContaining({
- type: "file",
- mediaType: "application/pdf",
- dataUrl: expect.stringMatching(/^data:application\/pdf;base64,/u),
- }),
- ]);
- expect(getPageCount).toHaveBeenCalledTimes(1);
- expect(renderPages).not.toHaveBeenCalled();
- });
- it("uses the exact error for a non-regular PDF path", async () => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH, pages: "1" },
- { supportsPdf: true, supportsImages: true },
- {},
- fileSystemPortWith({
- async stat(request) {
- return { path: request.path, kind: "directory", sizeBytes: 0 };
- },
- }),
- );
- expect(result.modelContent).toBe(
- `<tool_use_error>Path is not a regular file: ${PDF_PATH}</tool_use_error>`,
- );
- });
- it("uses the exact native PDF size-limit error", async () => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- {},
- fileSystemPortWith({
- async stat(request) {
- return {
- path: request.path,
- kind: "file",
- sizeBytes: READ_PDF_NATIVE_MAX_INPUT_BYTES + 1,
- };
- },
- }),
- );
- expect(result.modelContent).toBe(
- "<tool_use_error>PDF file exceeds maximum allowed size of 20MB.</tool_use_error>",
- );
- });
- it("uses the exact page-extraction size-limit error", async () => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH, pages: "1" },
- { supportsPdf: true, supportsImages: true },
- {},
- fileSystemPortWith({
- async stat(request) {
- return {
- path: request.path,
- kind: "file",
- sizeBytes: READ_PDF_EXTRACT_MAX_INPUT_BYTES + 1,
- };
- },
- }),
- );
- expect(result.modelContent).toBe(
- "<tool_use_error>PDF file exceeds maximum allowed size for text extraction (100MB).</tool_use_error>",
- );
- });
- it("uses the exact missing-PDF-header error", async () => {
- const invalidPdf = new Uint8Array(Buffer.from("not-a-pdf"));
- const result = await executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- { getPageCount: async () => undefined },
- fileSystemPortWith({
- async readBinaryFile(request) {
- return {
- path: request.path,
- content: invalidPdf,
- bytesRead: invalidPdf.byteLength,
- sizeBytes: invalidPdf.byteLength,
- truncated: false,
- };
- },
- async stat(request) {
- return { path: request.path, kind: "file", sizeBytes: invalidPdf.byteLength };
- },
- }),
- );
- expect(result.modelContent).toBe(
- `<tool_use_error>File is not a valid PDF (missing %PDF- header): ${PDF_PATH}</tool_use_error>`,
- );
- });
- it("uses the fixed punctuation when requiring pages for a long PDF", async () => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- { getPageCount: async () => 88 },
- );
- expect(result.modelContent).toBe(
- '<tool_use_error>This PDF has 88 pages, which is too many to read at once. Use the pages parameter to read specific page ranges (e.g., pages: "1-5"). Maximum 20 pages per request.</tool_use_error>',
- );
- });
- it("continues native PDF reading when page-count inspection is unavailable", async () => {
- const result = await executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- { getPageCount: async () => undefined },
- );
- expect(result.success).toBe(true);
- expect(result.output).toMatchObject({ type: "pdf", filePath: PDF_PATH });
- });
- it("propagates native page-count cancellation while the outer signal is still live", async () => {
- await expect(
- executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- {
- getPageCount: async () => {
- throw new PdfDocumentPortError("cancelled", "PDF inspection cancelled");
- },
- },
- ),
- ).rejects.toMatchObject({
- message: "PDF inspection cancelled",
- type: CoreErrorType.ToolCancelled,
- });
- });
- it("propagates unexpected native page-count inspection failures", async () => {
- const inspectionError = new Error("unexpected pdfinfo adapter failure");
- await expect(
- executeReadPdf(
- { file_path: PDF_PATH },
- { supportsPdf: true, supportsImages: false },
- {
- getPageCount: async () => {
- throw inspectionError;
- },
- },
- ),
- ).rejects.toBe(inspectionError);
- });
- it("reads PDF-disabled current turn input as text", async () => {
- const context = contextWith({});
- context.model = createTestRuntimeModel({
- generateText: async () => ({ finishReason: "stop", text: "", usage: {} }),
- inputFormat: { supportsPdf: false },
- modelId: "turn-unknown-pdf",
- });
- const output = await readHandler({ file_path: PDF_PATH, pages: "1" }, context);
- expect(output).toMatchObject({ type: "text", filePath: PDF_PATH });
- });
- });
- async function executeReadPdf(
- input: unknown,
- capabilities: { supportsPdf?: boolean; supportsImages?: boolean },
- pdfPort: Partial<PdfDocumentPort>,
- fileSystem?: FileSystemPort,
- ) {
- const context = contextWith(pdfPort, fileSystem);
- context.model = createTestRuntimeModel({
- generateText: async () => ({ finishReason: "stop", text: "", usage: {} }),
- inputFormat: {
- supportsPdf: capabilities.supportsPdf ?? false,
- supportsImage: capabilities.supportsImages ?? false,
- },
- modelId: "pdf-model",
- });
- const output = await readHandler(input, context);
- if (isHandlerFailure(output)) {
- return {
- success: false,
- output,
- modelContent: `<tool_use_error>${output.message}</tool_use_error>`,
- };
- }
- return {
- success: true,
- output,
- modelContent: readToolEntry.formatModelContent?.(output),
- };
- }
- function isHandlerFailure(
- value: unknown,
- ): value is { result: false; errorCode: number; message: string } {
- return typeof value === "object" && value !== null && "result" in value && value.result === false;
- }
- function contextWith(
- pdfPort: Partial<PdfDocumentPort>,
- fileSystem: FileSystemPort = fileSystemPort(),
- ): ToolExecutionContext {
- return {
- abortSignal: new AbortController().signal,
- fileSystemPort: fileSystem,
- imageProcessorPort: imageProcessorPort(),
- pdfDocumentPort: pdfPort as PdfDocumentPort,
- sessionId: createSessionId("read-pdf-context"),
- toolCallId: createToolCallId("read-pdf-context"),
- traceId: "trace-read-pdf" as never,
- turnId: createTurnId("read-pdf-context"),
- workingDirectory: "/tmp",
- workspaceRoot: "/tmp",
- };
- }
- function fileSystemPortWith(overrides: Partial<FileSystemPort>): FileSystemPort {
- return { ...fileSystemPort(), ...overrides } as FileSystemPort;
- }
- function fileSystemPort(): FileSystemPort {
- const data = Buffer.concat([Buffer.from("%PDF-1.7\n"), Buffer.alloc(1_527)]);
- return {
- async stat(request) {
- return { path: request.path, kind: "file", sizeBytes: data.byteLength };
- },
- async readBinaryFile(request) {
- return {
- path: request.path,
- content: new Uint8Array(data),
- bytesRead: data.byteLength,
- sizeBytes: data.byteLength,
- truncated: false,
- };
- },
- async readTextFileRange(request) {
- return {
- path: request.path,
- content: data.toString("utf8"),
- encoding: "utf8" as const,
- bytesRead: data.byteLength,
- sizeBytes: data.byteLength,
- truncated: false,
- startLine: 1,
- lineCount: 1,
- totalLines: 1,
- };
- },
- } as FileSystemPort;
- }
- function imageProcessorPort(): ImageProcessorPort {
- return {
- async prepareForModel(request) {
- return {
- data: request.data,
- mediaType: request.mediaType,
- originalSizeBytes: request.data.byteLength,
- transformedSizeBytes: request.data.byteLength,
- resized: false,
- compressed: false,
- strategy: "original" as const,
- };
- },
- };
- }
|