Files
CatoandClaude Opus 5.5 58cb5c691f feat(pdf): read a PDF someone attaches, as text or as pictures of its pages (0.20.0) (#46)
* feat(pdf): read a PDF someone attaches, as text or as pictures of its pages

No model on a free chain reads a PDF file, so a PDF never leaves the device
as one. @bitbaum/chatkit/pdf reads it in the browser with pdf.js:

- a PDF with a text layer comes back as text, page markers kept;
- one without (slides exported as pictures, a scan) comes back as its pages,
  four to a sheet at 784px a page, for a model that can see.

chatkit-copy-pdf-assets copies pdf.js's worker and decoders into the app's
static folder; readPdf takes their URL. Without the decoders, JPEG 2000
pages (macOS's "reduce file size" export) draw blank. The legacy build is
used so older iPhones read PDFs too; pdf.js loads only on the first PDF.

Found on OrangeCat 2026-10-27: a 17-slide investor deck (no text layer,
JPEG 2000 images) could not be shared with the Cat at all. Checked in
Chromium against that deck: 5 sheets, every slide legible.

Release 0.20.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt7KKvAA6LzcgFqXA3

* docs(pdf): describe the app's static folder without naming it as a chatkit path; chain the copy into build (pnpm skips prebuild)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt7KKvAA6LzcgFqXA3

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-27 17:00:54 +01:00

84 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// A PDF a person attaches must reach the model as something it can read.
// Found 2026-10-27: an investor deck of slide images could not be shared at all.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
isPdfFile,
isTextRich,
joinPageTexts,
planSheets,
readPdf,
sheetName,
PDF_PAGES_PER_SHEET,
} from "../dist/pdf.js";
// The files an app copies with `chatkit-copy-pdf-assets`, copied the same way.
const assets = mkdtempSync(join(tmpdir(), "chatkit-pdf-"));
execFileSync(process.execPath, [
fileURLToPath(new URL("../bin/chatkit-copy-pdf-assets.mjs", import.meta.url)),
assets,
]);
const assetsUrl = pathToFileURL(assets).href + "/";
const fixture = async (name) =>
new Uint8Array(await readFile(new URL(`./fixtures/${name}`, import.meta.url)));
test("a PDF is recognised by type or by name", () => {
assert.equal(isPdfFile({ name: "deck.PDF", type: "" }), true);
assert.equal(isPdfFile({ name: "x", type: "application/pdf" }), true);
assert.equal(isPdfFile({ name: "notes.txt", type: "text/plain" }), false);
});
test("page numbers alone are not a text layer", () => {
assert.equal(isTextRich(["1", "2", "3"]), false);
assert.equal(isTextRich([]), false);
assert.equal(
isTextRich([
"A paragraph long enough to be the document itself, not a page number or a stamp.",
]),
true,
);
});
test("page markers survive so the model can cite a page", () => {
assert.equal(joinPageTexts(["one ", " two"]), "--- page 1 ---\none\n\n--- page 2 ---\ntwo");
});
test("sheets hold four pages and stop where the message runs out of pictures", () => {
assert.equal(PDF_PAGES_PER_SHEET, 4);
assert.deepEqual(planSheets(6, 5), [
[1, 2, 3, 4],
[5, 6],
]);
// A 17-page deck with room for five pictures is read whole.
assert.equal(planSheets(17, 5).flat().length, 17);
// With room for two, eight pages go and the caller says so.
assert.deepEqual(planSheets(17, 2).flat(), [1, 2, 3, 4, 5, 6, 7, 8]);
assert.deepEqual(planSheets(3, 0), []);
});
test("a sheet is named for the pages on it", () => {
assert.equal(sheetName("deck.pdf", 5, 8), "deck.pdf · pages 5–8");
assert.equal(sheetName("deck.pdf", 9, 9), "deck.pdf · page 9");
});
test("a PDF with a text layer comes back as text, page by page", async () => {
const reading = await readPdf(await fixture("text.pdf"), { maxSheets: 5, assetsUrl });
assert.equal(reading.kind, "text");
assert.equal(reading.pageCount, 2);
assert.match(reading.text, /--- page 1 ---\nQuarterly report, page 1\./);
assert.match(reading.text, /--- page 2 ---/);
});
test("a PDF of slide images is treated as pages to look at, not as empty text", async () => {
const reading = await readPdf(await fixture("slides.pdf"), { maxSheets: 5, assetsUrl });
assert.equal(reading.kind, "pages");
assert.equal(reading.pageCount, 3);
// Drawing needs a browser canvas; test/browser-pdf.mjs checks the pictures.
});