# Daily PDF book extraction reference

Reusable pattern for setting up a daily digest from one or more uploaded PDFs.

## Project layout

```text
/project_dir/
  README.md
  manifest.json
  state.json
  setup_book_extract.py
  next_fragment.py
  TOOLS_AND_INSIGHTS.md
  pdf/
    part_1.pdf
    part_2.pdf
  text/
    part_1.txt
    part_2.txt
    all_parts.txt
  daily_fragments/
    fragment_001.txt
    fragment_002.txt
  daily_outputs/
    day_001.md
```

## Extraction script responsibilities

A setup script should:

1. copy uploaded files from transient cache into stable storage;
2. extract text with `fitz` / `pymupdf` for text-based PDFs;
3. preserve page markers like `===== PART 1 · PAGE 9 =====`;
4. write per-part text files and a combined text file;
5. create `manifest.json` with source paths, page counts, nonempty page counts, char counts, and daily fragment metadata;
6. split the combined text into `daily_fragments/` by chapter or approximate size;
7. initialize `state.json` with `next_fragment: 1`;
8. create an accumulating `TOOLS_AND_INSIGHTS.md` if the task is tool/practice extraction.

## Stateful `next_fragment.py` pattern

The cron script should print JSON to stdout. Keep it deterministic and simple.

Key fields:

```json
{
  "status": "ok",
  "task": "daily_book_extract",
  "book": "Book title",
  "fragment_number": 1,
  "total_fragments": 43,
  "range": {"start": {"part": 1, "page": 1}, "end": {"part": 1, "page": 9}},
  "base_dir": "/project_dir",
  "tools_file": "/project_dir/TOOLS_AND_INSIGHTS.md",
  "daily_outputs_dir": "/project_dir/daily_outputs",
  "source_fragment_path": "/project_dir/daily_fragments/fragment_001.txt",
  "instruction": "Summarize/extract...",
  "fragment_text": "..."
}
```

Include `--peek` mode:

- `--peek` prints the next fragment without changing `state.json`.
- A real cron run advances `next_fragment` after emitting.
- If all fragments are complete, emit `status: complete` and ask the agent to create an integration summary from the accumulating file.

## Cron setup pitfall

Hermes cron requires scripts to be under `~/.hermes/scripts/` and referenced by relative filename.

Correct:

```json
{
  "script": "cosmos_book_next_fragment.py"
}
```

Incorrect:

```json
{
  "script": "/home/spazio/project/next_fragment.py"
}
```

Copy the project script into `~/.hermes/scripts/`, make it executable, then test with `--peek`.

## Cron prompt checklist

The prompt should explicitly tell the future agent:

- the user-facing language and style;
- what JSON fields the script provides;
- not to quote long copyrighted passages;
- the required digest sections;
- to append structured extraction to the accumulating Markdown file;
- to save a per-day Markdown report;
- to make the final response the digest, not technical logs.

## Example digest sections

For a transformational/practical book:

```text
Book title — day N / TOTAL

Главная суть фрагмента
Инструменты / техники / практики
Вопросы из текста и очищенные вопросы для себя
Важные мысли и осознания
Что применить сегодня
Что забрать в мой проект / метод / практику
```

Adapt section labels to the user’s domain and style.
