¶ 1 · THE INSTRUMENT

Anything in. A real book out.

Epub Forge is the conversion engine behind every kaOS book. A PDF walks in; a chaptered, navigable, semantically-anchored EPUB walks out. A lecture recording, a scan, a Kindle file, a folder of Markdown — the same forge handles them all. It is a workspace package, a Cloud Run service, and a sovereign local lane. One contract, three doors.

@REPO/EPUB-GENERATOR · CLOUD RUN · SOVEREIGN LANE

FORMATS · TWELVE IN
PDF, image, audio, DOCX, TXT, CSV, Markdown, EPUB, MOBI, AZW, AZW3, KF8. One converter each, one builder at the end.
OUTPUT · EPUB 3.3
Valid package, semantic CFI anchors, full front matter, a real typographic stylesheet. The page on screen is the page in print.
RUN · THREE WAYS
Import the package. POST to Cloud Run. Run the sovereign forge lane on your own machine. Same contract, three doors.
EPUB 2 / 3 · VALIDPDF · AUDIO · IMAGE · DOCX · MOBICFI ANCHORS · SEMANTICEPUB 3.3 NAVIGATIONCLOUD RUN · SOVEREIGN LANEWORKSPACE PACKAGE
I. What walks in

Twelve source formats. One forge.

Most converters handle one format and pretend the others don't exist. The forge routes each source to its strongest extractor — Unstructured for PDF, Gemini for audio and image, LangChain for DOCX, lingo-reader for MOBI — then walks them all into the same chapter builder.

01
PDF
Maximum-quality extraction via Unstructured.io — hi_res strategy, page detection, table recovery. A 400-page academic PDF becomes a chaptered, navigable EPUB.
02
Audio
Gemini 2.5 Flash structures the recording into speakers, topics, key quotes, chapter breaks, and a transcript. The audio is embedded for EPUB 3 readers.
03
Image
A photograph, a scan, a diagram. Vision analysis produces transcribed text, a visual description, a colour palette, mood, and prompting suggestions — each its own chapter.
04
MOBI · AZW · KF8
Kindle formats parsed via @lingo-reader/mobi-parser. Chapters, TOC, and metadata preserved. Your Kindle library becomes a kaOS library.
05
DOCX · TXT · CSV · MD
LangChain loaders for DOCX. Plain text, Markdown, and CSV walked through the same chapter builder. Rich content streams become structured EPUB chapters.
06
EPUB
Already an EPUB? It walks through unchanged — re-validated, re-styled, and brought under the kaOS production stylesheet if you want it.
II. What comes out

Not a zipped HTML folder. A real EPUB.

The output is EPUB 3.3 — mimetype stored uncompressed, a valid package document, a navigation document with TOC and landmarks, and a production stylesheet that respects every typographic rule a real book deserves. It opens in Folio, in Apple Books, in Calibre, in any reader that honours the standard.

EPUB 3.3 · PACKAGE STRUCTURE
book.epub
├── mimetype                        ← stored, uncompressed
├── META-INF/
│   └── container.xml
└── OEBPS/
    ├── content.opf                 ← package document (manifest + spine)
    ├── nav.xhtml                   ← EPUB 3 navigation (TOC, landmarks, page-list)
    ├── toc.ncx                     ← fallback navigation for EPUB 2 readers
    ├── stylesheet.css              ← 2,500-line production stylesheet
    ├── cover.xhtml                 ← image or CSS-typographic cover
    ├── titlepage.xhtml             ← front matter
    ├── copyright.xhtml             ← front matter
    ├── chapter1.xhtml              ← semantic CFI anchors on every element
    ├── chapter2.xhtml
    └── images/ · audio/
EPUB 3.3 structure
Valid package document, navigation document, spine, manifest. mimetype stored uncompressed. Real EPUB, not a zipped HTML folder.
Semantic CFI anchors
Every paragraph, heading, figure, and blockquote carries data-cfi-anchor and an epub:type. Citations hold. Positions survive reflow.
Front matter, complete
Title page, copyright page, cover (image or CSS-typographic), and an EPUB 3 nav document with TOC, landmarks, and page list.
Production stylesheet
A 2,500-line CSS stylesheet — typography hierarchy, hanging punctuation, widows and orphans, page breaks, print overrides. Not a reset.
Language-aware
BCP-47 language metadata flows into xml:lang and lang on every chapter. Hyphenation, justification, and script direction all respect it.
Embedded media
Audio files embed inside the EPUB with a player bound to paragraph positions. Images carry captions, attributions, and alt text.
III. The pipeline

Four stages. One contract.

Every source walks the same path. Ingest, extract, build, ship. The extraction strategy changes per format; the contract that crosses each stage does not.

01
Ingest
The source is read from Supabase storage, a local buffer, or a stream. Format is detected from the file, not the extension.
02
Extract
The right converter runs — Unstructured for PDF, Gemini for audio and image, LangChain for DOCX, lingo-reader for MOBI. Rich content is structured into typed items.
03
Build
Chapters are assembled from rich content or plain text. The EPUB is zipped with mimetype stored, container.xml, content.opf, nav.xhtml, toc.ncx, and stylesheet.css.
04
Ship
The EPUB is uploaded to storage and ready for the Folio reader, for Pocket, for Sovereign — or for any standards-compliant EPUB reader on earth.
IV. Three ways to run it

One package. One service. One sovereign lane.

The forge is the same code three ways. Import it as a workspace package and call it from any Node process. POST to the Cloud Run service and let it run serverless. Or run the sovereign forge-processor on your own machine, where your manuscripts never leave the substrate. The lane resolver in apps/api picks the right one based on a single env var.

IMPORT IT DIRECTLY
The library package
@repo/epub-generator is a workspace package. Import generateEpub, buildEpubFromChapters, or the format-specific converters into any kaOS app, any Node service, any script.
import { generateEpub } from '@repo/epub-generator';

const result = await generateEpub({
  contentId: 'doc-0421',
  userId: 'user_123',
  storagePath: 'uploads/lecture.mp3',
  originalFormat: 'audio',
  title: 'The Face of God — Lecture',
  author: 'Fr. Kaleeg Hainsworth',
  language: 'en',
});
POST AND FORGET
The cloud-run service
kaos-epub-parser runs on Google Cloud Run. POST /process-from-storage with a storage path; it downloads, parses, embeds, and inserts. The same path serves the kaOS Library and Publisher Studio.
POST https://kaos-epub-parser-XXXX.run.app/process-from-storage
Content-Type: application/json

{
  "storagePath": "users/user_123/uploads/book.pdf",
  "userId": "user_123",
  "title": "An Altar in the Wilderness"
}
LOCAL-FIRST, SOVEREIGN
The sovereign forge lane
The sovereign forge-processor runs the same contract on your own machine. Set MALAIKA_LANE_FORGE=local and the cron caller routes to your local lane instead of Cloud Run. Your manuscripts never leave the substrate.
# Sovereign lane — local forge processor
MALAIKA_LANE_FORGE=local
FORGE_PROCESSOR_MODE=live

# The cron caller in apps/api resolves the lane:
#   local  → LOCAL_FORGE_URL
#   cloud  → Cloud Run
# Either way, the contract is identical.

The lane resolver lives in apps/api/app/api/cron/process-embeddings/route.ts. Set MALAIKA_LANE_FORGE=local and the cron caller routes to LOCAL_FORGE_URL if set, otherwise Cloud Run. The full environment reference is in the forge-processor source.

V. Observability

Every conversion leaves a paper trail.

Each conversion emits structured JSON to stdout — one object per line, consumable by any log drain. Format, strategy, page count, chapter count, word count, cost (for Unstructured), duration. The sovereign forge-processor follows the same convention so the lane-stats producer can filter on source=sovereign_forge_processor.

{"severity":"INFO","source":"kaos-epub-parser","event":"epub.completed","contentId":"doc-0421","format":"pdf","strategy":"hi_res","pages":312,"chapters":18,"wordCount":84210,"costUsd":0.42,"durationMs":18420,"epubPath":"users/user_123/library/processed/doc-0421.epub"}
{"severity":"INFO","source":"sovereign_forge_processor","lane":"forge","event":"forge.received","jobId":"job-7","canvasItemCount":4,"promptLen":820}

Bring us the format no one else will touch.

© 2026 KAOS INTELLIGENCE, INC.HOMEARCHITECTUREBOOK EVOLVEEPUB GENERATOR SOURCEEND OF DOCUMENT
¶ THE OUTPUTS

Four kinds of thinking we hand back to publishers.

Each one live in the kaOS Library. Each installable on its own — stronger together.

The Contemplative's Rediscovered Truths in 'Come on You Reds'
01· SPARKS

Literary discovery, written fresh each day from what readers love.

SEE ALL →
Alex Colville | Art Canada Institute — Deep Cut
02· BOOKCASTS

Books become listening — multi-voice audio from your catalogue.

SEE ALL →
Episode 14: The Geometry of the Unknown
03· CHRONICLE

Serialized fiction woven from real work — every deployment a chapter.

SEE ALL →
Man and His Symbols
04· FOLIO

The intelligence-first reading system — annotation, conversation, memory.

SEE ALL →
ENTER THE LIBRARYGET A FREE ACCOUNT

FREE TO START · NO CREDIT CARD REQUIRED

¶ THE LIBRARY, OPEN ALL NIGHT

Send us a document. Any document.

We'll return it thinking.

Talk to usRead today's Spark
© 2026 KAOS INTELLIGENCE, INC. · KNOWLEDGE-AWARE OPERATING SYSTEMARCHITECTUREABOUTAI AGENT? READ /LLMS.TXT · /LLMS.MD · /MCPEND OF DOCUMENT · REV 2026.07