diff --git a/.env.example b/.env.example index 87e81e7..8983ff8 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,21 @@ # Copy to .env (chmod 600) and fill in. Never commit .env. -# API key from https://platform.claude.com (Anthropic API, billed separately from a Claude subscription). -# Without it the app still works; uploaded photos just have to be typed up by hand. +# Transcription of page photos. Set ONE of the two keys; OpenRouter wins if both are set. +# Without either the app still works; uploaded photos just have to be typed up by hand. + +# OpenRouter key from https://openrouter.ai/settings/keys (prepaid credit). +OPENROUTER_API_KEY= +# Any OpenRouter model that accepts images. Default ~$0.0015 a page; google/gemini-2.5-flash-lite +# is cheaper (~$0.0005), anthropic/claude-haiku-5.5 or google/gemini-3.5-flash read hard pages better. +OPENROUTER_MODEL=google/gemini-3.1-flash-lite + +# Or: Anthropic API key from https://platform.claude.com (billed separately from a Claude subscription). ANTHROPIC_API_KEY= # Password people need to add and edit plays. Without it uploading is switched off. UPLOAD_PASSWORD= -# Vision model used for transcription. claude-sonnet-5-5 reads hard pages better at ~20x the price. +# Claude model when using ANTHROPIC_API_KEY. claude-sonnet-5-5 reads hard pages better at ~20x the price. CLAUDE_MODEL=claude-haiku-5-5 # Safety cap on pages transcribed per day (all users together), and pages per play. diff --git a/AGENTS.md b/AGENTS.md index 78ee228..2ebe005 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,14 +1,16 @@ # AGENTS.md — theater-reader A Node 22 server (`server/`) serving a no-build front end (`public/`, plain HTML/CSS/ES modules) and a JSON API for plays -kept on a volume. Transcription of page photos goes through the Anthropic SDK (`@anthropic-ai/sdk`, the only dependency). +kept on a volume. Page photos are transcribed by a vision model through OpenRouter (plain `fetch`, chosen when `OPENROUTER_API_KEY` is set) or the +Anthropic SDK (`@anthropic-ai/sdk`, the only dependency, used with `ANTHROPIC_API_KEY`). UI language is Czech. ## Layout - `server/main.mjs`: HTTP server, routing, static files, auth (`x-upload-password` header vs `UPLOAD_PASSWORD`), daily page cap, background transcription job. - `server/store.mjs`: plays on disk, `DATA_DIR/plays//{meta.json,play.txt,img/NNN.jpg}`. Atomic writes; per-play lock for meta updates. -- `server/transcribe.mjs`: one Claude request per photo (`transcribePage`), then `splitPage` → `normalizeSpeakers` → `assemble` into one play file. `server/prompt.mjs` holds the prompt, which teaches the markup below. +- `server/providers.mjs`: `makeProvider(env)` returns `{ name, model, transcribe(jpeg) }` for OpenRouter or Anthropic. Errors become `PageError`s whose message is shown in the editor. OpenRouter retries 408/429/5xx and network errors itself. +- `server/transcribe.mjs`: `splitPage` → `normalizeSpeakers` → `assemble` turn the per-photo answers into one play file. `server/prompt.mjs` holds the prompt (shared by both providers), which teaches the markup below. - `public/`: four pages: `index.html` library (`library.js`), `read.html` reader (`reader.js`), `add.html` upload (`add.js`: shrinks photos to ≤1.15 MP JPEG in the browser), `edit.html` editor (`edit.js`). `api.js` holds the fetch and password helpers. `parse.js` is the markup parser, shared with the server tests. - `seed/`: plays copied into an empty data volume on first start. - `public/fonts/`: self-hosted woff2 + OFL licences, made by `tools/fetch_fonts.py` (keep its `FAMILIES` and `FONTS` in `reader.js` in step). @@ -23,10 +25,10 @@ edited by hand, so it can't overwrite corrections. ## Run / test - Dev port **3010** on loopback. Preview with `ssh -p 14922 -L 3010:localhost:3010 `. - `podman-compose --env-file up -d --build` (env file with `UPLOAD_PASSWORD`, and optionally `ANTHROPIC_API_KEY`, chmod 600, outside the repo). + `podman-compose --env-file up -d --build` (env file with `UPLOAD_PASSWORD`, and optionally `OPENROUTER_API_KEY` or `ANTHROPIC_API_KEY`, chmod 600, outside the repo). - Tests (parser, transcription assembly, API end to end with Claude stubbed): `podman run --rm -v "$PWD":/app:ro,Z -w /app docker.io/library/node:22-alpine npm test` (run `npm ci` in the same image first if `node_modules` is missing). -- To exercise the full UI without spending API credit, point `ANTHROPIC_BASE_URL` at a stub server that returns a Messages response. +- To exercise the full UI without spending API credit, use `ANTHROPIC_API_KEY=dummy` and point `ANTHROPIC_BASE_URL` at a stub server that returns a Messages response. ## Play markup diff --git a/README.md b/README.md index d7f1172..8811c46 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ A link like `read.html?hra=hra-1931&postava=Lída` opens a play with the charact **Adding plays** (anyone with the upload password): 1. *Přidat hru*: give the title and pick a photo of each page. -2. Each photo is read by a Claude vision model (Claude Haiku 5.5 by default) and turned into the script markup: speakers, stage directions, page numbers. Missing pages are noticed from gaps in the page numbers. +2. Each photo is read by a vision model, through OpenRouter (default `google/gemini-3.1-flash-lite`) or the Anthropic API (`claude-haiku-5-5`), and turned into the script markup: speakers, stage directions, page numbers. Missing pages are noticed from gaps in the page numbers. 3. The editor shows the text next to the photos and lists things to check (uncertain words marked `[?]`, characters who speak only once, pages that failed). Fix, tick *Zveřejněno*, save. ## Deploying @@ -29,9 +29,12 @@ The app listens on port 3010 on loopback (`PORT` and `BIND_ADDRESS` change that) | Variable | | |---|---| -| `ANTHROPIC_API_KEY` | API key from [platform.claude.com](https://platform.claude.com). **A Claude Pro/Max subscription can't be used for this.** The API is billed separately, pay as you go. Without a key the app still works, but uploaded pages have to be typed up by hand. | +| `OPENROUTER_API_KEY` | Key from [openrouter.ai](https://openrouter.ai/settings/keys) (prepaid credit). Used for transcription when set. | +| `OPENROUTER_MODEL` | Any OpenRouter model that accepts images. Default `google/gemini-3.1-flash-lite` (about $0.0015 a page). `google/gemini-2.5-flash-lite` is cheaper. `anthropic/claude-haiku-5.5` and `google/gemini-3.5-flash` are alternatives if pages come out badly. | +| `ANTHROPIC_API_KEY` | Instead of OpenRouter: an API key from [platform.claude.com](https://platform.claude.com). **A Claude Pro/Max subscription can't be used for this.** The API is billed separately. | +| | Without either key the app still works, but uploaded pages have to be typed up by hand. | | `UPLOAD_PASSWORD` | Shared password for adding, editing and deleting plays. Unset = uploading switched off. | -| `CLAUDE_MODEL` | `claude-haiku-5-5` (default, around $0.001 a page) or `claude-sonnet-5-5` (reads hard pages better, roughly 20× the price). | +| `CLAUDE_MODEL` | With the Anthropic key: `claude-haiku-5-5` (default, around $0.001 a page) or `claude-sonnet-5-5` (reads hard pages better, roughly 20× the price). | | `DAILY_PAGE_LIMIT` | Cap on pages transcribed per day, for all users together (default 300), so a leaked password can't run up a bill. | | `MAX_PAGES` | Maximum photos per play (default 80). | diff --git a/docker-compose.yml b/docker-compose.yml index b9499c8..2ce4d14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: image: theater-reader:latest restart: unless-stopped environment: + OPENROUTER_API_KEY: ${OPENROUTER_API_KEY:-} + OPENROUTER_MODEL: ${OPENROUTER_MODEL:-google/gemini-3.1-flash-lite} ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-} UPLOAD_PASSWORD: ${UPLOAD_PASSWORD:-} CLAUDE_MODEL: ${CLAUDE_MODEL:-claude-haiku-5-5} diff --git a/public/add.html b/public/add.html index 4a817cc..9f54e1e 100644 --- a/public/add.html +++ b/public/add.html @@ -20,7 +20,7 @@
Fotky stran -

Každou stranu přečte umělá inteligence (Claude) a přepíše ji do textu. Text pak zkontrolujete a opravíte v editoru, než hru zveřejníte.

+

Každou stranu přečte umělá inteligence a přepíše ji do textu. Text pak zkontrolujete a opravíte v editoru, než hru zveřejníte.