memorix icon indicating copy to clipboard operation
memorix copied to clipboard

feat: PDF ingestion via unpdf with page chunking

Open RaviTharuma opened this issue 8 months ago • 4 comments

Summary

Adds an memorix_ingest_pdf MCP tool that extracts text from PDF files using unpdf and stores each page (or chunk) as a memorix observation.

What's included

  • src/multimodal/pdf-loader.ts — PDF text extractor with:
    • Page-level chunking (one observation per page by default)
    • Configurable max pages
    • Graceful handling of empty/corrupted PDFs
    • Optional unpdf dependency (clear error if not installed)
  • src/server.ts — MCP tool registration (memorix_ingest_pdf)
  • tests/multimodal/pdf-loader.test.ts — 4 tests covering:
    • Multi-page extraction and observation storage
    • Empty PDF handling
    • Page limit configuration
    • Missing file error handling

Design decisions

  • unpdf is an optional peer dependency — tool returns clear error if not installed (npm install unpdf).
  • Each page becomes a separate observation for granular search/retrieval.
  • Stores as how-it-works observation type with PDF metadata (page number, total pages, filename) in facts.
  • No heavy native dependencies — unpdf is pure JS/WASM.

Tests

4 pass, 0 fail

RaviTharuma avatar Mar 29 '26 22:03 RaviTharuma

Thanks for working on PDF ingestion — page-level storage is a good direction, but this branch still has a few hard blockers.

  1. The test file is using bun:test, while the repo runs vitest in CI.
  2. src/multimodal/pdf-loader.ts currently fails typecheck because unpdf is dynamically imported but not represented in a way TypeScript can resolve in this repo's current dependency/type setup.
  3. In src/server.ts, the object flowing into storeObservation is inferred as type: string rather than a proper ObservationType.

So at the moment this is not just a dependency-install issue — it needs one more pass to make the optional dependency story and the repo's type/test expectations line up.

AVIDS2 avatar Mar 30 '26 13:03 AVIDS2

All three are valid — I'll fix them:

  1. bun:test → vitest: Will switch the test file to vitest imports.
  2. unpdf typecheck: Will declare unpdf as an optional peer dependency in package.json and add a @types/unpdf shim or inline type declaration so the dynamic import resolves under TypeScript without requiring the package at build time.
  3. ObservationType: Will add an explicit type assertion on the object passed to storeObservation so it satisfies ObservationType rather than widening to string.

The optional dependency story for global installs is the part that needs the most care — I'll make sure the error path when unpdf is absent gives users a clear install instruction rather than an opaque import failure.

RaviTharuma avatar Apr 08 '26 17:04 RaviTharuma

Addressed review feedback:

  • Fixed TypeScript type issue: replaced bare import('unpdf') cast with proper UnpdfModule interface shim via tryLoadUnpdf() helper
  • Fixed storeFn parameter: changed type: string to type: ObservationType with proper import
  • Fixed test imports: migrated from bun:test to vitest
  • All 4 tests pass, tsc --noEmit clean

RaviTharuma avatar Apr 08 '26 17:04 RaviTharuma

Leaving this open as a future ingestion candidate, but it still should not merge as-is.

The remaining blockers are still real: the optional unpdf dependency contract does not work cleanly for the documented global-install flow, maxPages needs strict positive-integer validation, and after 1.0.8 any new native Memorix capability needs CLI + MCP + docs parity rather than landing as an MCP-only surface.

A good next revision would define the dependency/install story first, add validation, and expose the PDF ingest path through the CLI as well.

AVIDS2 avatar Apr 21 '26 14:04 AVIDS2