Transcribe through OpenRouter when OPENROUTER_API_KEY is set

A Claude subscription can't be used by the app, so let it use any cheap
vision model on OpenRouter instead (default google/gemini-3.1-flash-lite,
OPENROUTER_MODEL to change). The Anthropic API stays available as the
second choice. Both providers share the prompt and live behind one
{ name, model, transcribe } interface in server/providers.mjs; OpenRouter
retries 408/429/5xx and network errors and turns 401/402/404 into
messages the editor can show.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
agent 2026-11-04 15:38:24 +00:00
parent 46aa1236ed
commit f353c14619
11 changed files with 258 additions and 91 deletions

View file

@ -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/<id>/{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 <host>`.
`podman-compose --env-file <file> up -d --build` (env file with `UPLOAD_PASSWORD`, and optionally `ANTHROPIC_API_KEY`, chmod 600, outside the repo).
`podman-compose --env-file <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