Metadata-Version: 2.5
Name: par-storygen
Version: 0.5.3
Summary: AI-driven TUI choose-your-own-adventure
Project-URL: Homepage, https://github.com/paulrobello/par-storygen
Project-URL: Documentation, https://github.com/paulrobello/par-storygen/blob/main/docs/ARCHITECTURE.md
Project-URL: Source, https://github.com/paulrobello/par-storygen
Project-URL: Issues, https://github.com/paulrobello/par-storygen/issues
Project-URL: Discussions, https://github.com/paulrobello/par-storygen/discussions
Author-email: Paul Robello <probello@gmail.com>
Maintainer-email: Paul Robello <probello@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Paul Robello
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: ai,choose-your-own-adventure,gemini,interactive-fiction,llm,ollama,openai,openrouter,storygen,terminal,textual,tui
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: End Users/Desktop
Classifier: Intended Audience :: Other Audience
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows :: Windows 10
Classifier: Operating System :: Microsoft :: Windows :: Windows 11
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Games/Entertainment
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Terminals
Classifier: Typing :: Typed
Requires-Python: >=3.13
Requires-Dist: google-genai<3,>=2.34.0
Requires-Dist: httpx>=0.28.1
Requires-Dist: jinja2>=3.1.6
Requires-Dist: openai<3,>=2.54.0
Requires-Dist: par-cli-tts>=0.6.1
Requires-Dist: par-textual-image>=0.1.0
Requires-Dist: pillow>=13.0.0
Requires-Dist: pydantic-ai<3,>=2.31.1
Requires-Dist: pydantic>=2.14.4
Requires-Dist: python-dotenv>=1.2.4
Requires-Dist: rich>=15.0.0
Requires-Dist: tenacity<9.2,>=9.1.4
Requires-Dist: textual-pyfiglet<1.1,>=1.0.0
Requires-Dist: textual<9,>=8.2.8
Requires-Dist: typer>=0.29.2
Requires-Dist: xdg-base-dirs>=6.1.0
Provides-Extra: api
Requires-Dist: fastapi>=0.165.0; extra == 'api'
Requires-Dist: python-multipart>=0.0.34; extra == 'api'
Requires-Dist: uvicorn[standard]>=0.54.0; extra == 'api'
Requires-Dist: websockets>=17.4; extra == 'api'
Description-Content-Type: text/markdown

# par-storygen

## Table of Contents

* [About](#about)
* [Features](#features)
   * [Core Capabilities](#core-capabilities)
   * [Advanced Features](#advanced-features)
   * [Technical Excellence](#technical-excellence)
* [Screenshots](#screenshots)
* [Prerequisites](#prerequisites)
* [Installing](#installing)
* [Command line arguments](#command-line-arguments)
* [Environment Variables](#environment-variables)
* [Data locations](#data-locations)
* [Running par-storygen](#running-par-storygen)
* [Choosing a text model](#choosing-a-text-model)
   * [OpenAI](#openai-text)
   * [OpenRouter](#openrouter)
   * [Ollama (local)](#ollama-text)
* [Choosing an image model](#choosing-an-image-model)
   * [OpenAI](#openai-image)
   * [Google Gemini](#google-gemini)
   * [Z.AI GLM-image](#zai-glm-image)
   * [Ollama (local)](#ollama-image)
   * [Fallback provider](#fallback-provider)
* [Character library](#character-library-exporting-and-importing)
* [Replay and endings](#replay-and-endings)
* [Branch prefetch](#branch-prefetch)
* [Character outfits](#character-outfits)
* [Text-to-speech](#text-to-speech)
* [Auto-play](#auto-play)
* [Export book](#export-book)
* [Keyboard shortcuts](#keyboard-shortcuts)
* [Web API and frontend (optional)](#web-api-and-frontend-optional)
* [Troubleshooting](#troubleshooting)
* [Contributing](#contributing)
* [Roadmap](#roadmap)

![CI](https://github.com/paulrobello/par-storygen/actions/workflows/ci.yml/badge.svg)
![PyPI - Version](https://img.shields.io/pypi/v/par-storygen)
![PyPI - Python Version](https://img.shields.io/badge/python-3.13-blue)
![Runs on Linux | MacOS | Windows](https://img.shields.io/badge/runs%20on-Linux%20%7C%20MacOS%20%7C%20Windows-blue)
![Arch x86-64 | ARM | AppleSilicon](https://img.shields.io/badge/arch-x86--64%20%7C%20ARM%20%7C%20AppleSilicon-blue)
![PyPI - License](https://img.shields.io/pypi/l/par-storygen)

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://buymeacoffee.com/probello3)

## About
par-storygen is a TUI (Text UI) choose-your-own-adventure powered by configurable LLMs. The application was built with [Textual](https://textual.textualize.io/) and [Rich](https://github.com/Textualize/rich).
A configurable LLM (via pydantic-ai) drives theme, characters, narration, and choices; image providers render portraits and scene illustrations using per-character reference portraits for visual consistency. Game state is a content-addressed tree persisted as JSON — walk the same choices twice and the game replays byte-for-byte. It runs on all major OS's including Windows, macOS, and Linux.

## Features

### Core Capabilities
- **Multi-Provider Text**: OpenAI, OpenRouter, and Ollama (all OpenAI-compatible) with per-save provider pinning
- **Multi-Provider Images**: OpenAI (ref-aware), Gemini (ref-aware), Z.AI (text-to-image), and Ollama (local) with automatic fallback
- **Interactive Wizard**: 8-step story setup — theme, tone, narration style, art style, length, reader level, characters, and confirmation
- **Half-Block Inline Art**: Scene illustrations rendered directly in the terminal using reference portraits for character consistency
- **Content-Addressed Tree**: Every choice is cached; replaying the same path returns byte-for-byte identical results
- **Save/Resume**: Full game state persistence with `--resume` flag to pick up where you left off

### Advanced Features
- **Branch Prefetch**: Background-generates pending choices while you read, for instant picks
- **Text-to-Speech**: Read narration aloud via OpenAI, ElevenLabs, Deepgram, Gemini, or Kokoro with provider/voice-aware audio caching
- **Auto-Play**: Press `a` to auto-advance with random choices, waiting for images and TTS before continuing
- **Character Library**: Export characters from finished stories and re-import with optional AI-powered backstory adaptation
- **Character Outfits**: Define multiple looks per character and switch between them mid-story
- **Endings Gallery**: Card-based view of every ending reached, with jump-to-node navigation
- **Branch Replay**: Read-only slideshow of any path from root to an explored node
- **Story Graph**: Full tree view with marker legend, current-node arrow, and unexplored-choice leaves
- **Reference Images**: Supply your own character images as portrait anchors for ref-aware providers
- **Reader Levels**: Vocabulary and complexity controls for ages 0-5, 6-10, 11-15, or 15+
- **Settings Persistence**: In-app Settings screen for provider defaults, art toggle, streaming, prefetch, TTS, and auto-play options
- **Export Book**: Export any story path as a standalone HTML book reader with 3D page turns, audio player, and auto-read
- **Story Templates / Presets**: Six curated presets plus your own saved presets; "Quick Start" from the main menu or "Load Preset" / "Save as Preset" in the wizard
- **Narrative Recap**: Press `Shift+R` for an on-demand "Previously on..." summary; optional auto-recap every N major beats and on resume
- **Relationship Tracking**: View pairwise character relationships (ally, rival, romantic, mentor, …) with strength bars via `i` → Relationships, extracted inline as the story progresses
- **Image Style Gallery**: Browse and apply art styles from the Settings screen
- **Graph Prune**: Press `p` in the story graph to delete a node and all its descendants (with confirmation)
- **Dynamic Pacing**: Choose Slow / Moderate / Fast pacing per story (wizard LENGTH step) to control narration length and choice frequency

### Technical Excellence
- **Async Architecture**: Non-blocking pipeline with concurrent illustration and portrait generation
- **Type Safety**: Fully typed Python 3.13 codebase with strict pyright mode
- **XDG-Compliant Paths**: Config and data stored per platform conventions
- **Atomic Persistence**: All file writes use `.tmp` + `os.replace` for crash safety
- **Cost Tracking**: Per-save image cost and token usage with per-model call counts
- **Prompt Caching**: Static system prompt content for optimal API cache hit rates

## Screenshots

**Splash & Main Menu**

![Splash screen](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_splash.png)
![Main menu](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_main_menu.png)

**Settings** — Configure text and image providers, art toggles, streaming, and prefetch options. Preferences persist across sessions.

![Settings screen](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_settings.png)

**New Story Wizard** — An 8-step guided setup for theme, tone, narration style, art style, length, reader level, and characters (with library import). See the [full wizard walkthrough](docs/NEW_STORY_WIZARD.md).

![Wizard theme step](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_wizard_1_theme.png)

**Load Story** — Browse and resume existing saves.

![Load story screen](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_load_story.png)

**Gameplay** — The main play screen shows the scene illustration (half-block inline art) on the left, narrative text in the center, and the character roster on the right. Numbered choices appear at the bottom.

![Story play screen](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_story_panel.png)
![Beat generation](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_story_generate.png)

**Character Portraits** — Press `i` → Portraits to view full portraits for every character in the current scene. High-res zoom available for ref-aware providers.

![Character roster](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_story_roster.png)


**Story Graph** — Press `i` → Graph for a full tree view with marker legend, current-node arrow, and unexplored-choice leaves. Press `r` on any node for branch replay; press `p` to prune a subtree.

![Story graph](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_story_graph.png)

**Character Catalog** — A scrollable grid of exported characters from across all your stories. Each card shows the portrait thumbnail, name, and source story. Import any character into a new story (keep as-is or adapt backstory to the new theme).

![Character catalog](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_char_catalog.png)

**High-res** versions of images are available

![High-res portrait](https://raw.githubusercontent.com/paulrobello/par-storygen/main/screenshots/sc_char_highres.png)

## Prerequisites

### For running
* Install Python 3.13 or newer
  * [https://www.python.org/downloads/](https://www.python.org/downloads/) has installers for all versions
  * On Windows the [Scoop](https://scoop.sh/) tool makes it easy to install and manage things like python
    * Install Scoop then do `scoop install python`

### For development
* Install [uv](https://docs.astral.sh/uv/)
* Install GNU Compatible Make command
  * On Windows if you have scoop installed you can install make with `scoop install make`

## Installing

### Installing uv
If you don't have uv installed you can run the following:
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

### Install from PyPI with uv
```bash
uv tool install par-storygen
storygen
```

### Install from PyPI with pip
```bash
pip install par-storygen
storygen
```

### Source install from GitHub
```bash
git clone https://github.com/paulrobello/par-storygen
cd par-storygen
make setup
```

## Command line arguments

The TUI ships as the `storygen` console script. Bare `uv run storygen` is equivalent to `storygen run` with no flags.

```
storygen --version          Print the version and exit.
storygen --help             Show top-level help.
storygen run                Launch the TUI.
storygen run --resume (-r)  Skip the menu and resume the most recently played story.
```

### `storygen-api` (optional `[api]` extra)

The FastAPI server ships as a separate console script, available after `uv sync --extra api`. Its single (default) command starts uvicorn:

```
storygen-api serve --host 127.0.0.1 --port 8101 --no-reload
```

| Flag | Default | Purpose |
|------|---------|---------|
| `--host` | `127.0.0.1` | Bind address. Pass `0.0.0.0` for LAN access (and set `STORYGEN_API_TOKEN`). |
| `--port` | `8101` | Bind port — pairs with the Next.js dev server on `:8100`. |
| `--reload` / `--no-reload` | `--no-reload` | Auto-reload on source changes (dev only). |

`make api-dev` runs uvicorn with `--reload --port 8101`; `make api-prod` pins `--workers 1` (the server is single-worker — see [Web API and frontend](#web-api-and-frontend-optional)).

## Environment Variables

### Variables are loaded in the following order, last one to set a var wins
* HOST Environment
* `.env` file in project root
* par-storygen Settings Screen

### Text provider variables
* `OPENAI_API_KEY` — OpenAI API key
* `OPENROUTER_API_KEY` — OpenRouter API key
* `STORYGEN_TEXT_PROVIDER` — `openai` (default), `openrouter`, or `ollama`
* `STORYGEN_TEXT_MODEL` — Model identifier (default: `gpt-6-luna`)
* `STORYGEN_TEXT_BASE_URL` — Override base URL for the text provider

### Image provider variables
* `GEMINI_API_KEY` — Google Gemini API key
* `ZAI_API_KEY` — Z.AI API key
* `STORYGEN_IMAGE_PROVIDER` — Scene/cover art provider: `openai` (default), `gemini`, `zai`, or `ollama`
* `STORYGEN_IMAGE_MODEL` — Scene/cover art model identifier (default: `gpt-image-2.6-flare`)
* `STORYGEN_IMAGE_BASE_URL` — Override base URL for the scene/cover art provider
* `STORYGEN_IMAGE_API_KEY` — Override API key for the scene/cover art provider
* `STORYGEN_CHARACTER_IMAGE_PROVIDER` — Character portrait provider: `openai` (default), `gemini`, `zai`, or `ollama`
* `STORYGEN_CHARACTER_IMAGE_MODEL` — Character portrait model identifier (default: `gpt-image-2.6-flare`)
* `STORYGEN_CHARACTER_IMAGE_BASE_URL` — Override base URL for the character portrait provider
* `STORYGEN_CHARACTER_IMAGE_API_KEY` — Override API key for the character portrait provider

See [`.env.example`](./.env.example) for the full list.

### FastAPI server variables (optional `[api]` extra)
* `STORYGEN_API_TOKEN` — shared bearer token. Unset → loopback trusted, off-box rejected (HTTP 503 / WS 4403); set → required of every client including loopback.
* `STORYGEN_API_ALLOWED_ORIGINS` — CORS origins (comma-separated; default `http://localhost:8100,http://127.0.0.1:8100`).
* `STORYGEN_WS_ALLOWED_ORIGINS` — WebSocket handshake origin allowlist (default mirrors `STORYGEN_API_ALLOWED_ORIGINS`).
* `STORYGEN_API_RATE_LIMIT` — per-IP sliding-window limit on cost-incurring routes, format `<count>/<period>` with period `second`/`minute`/`hour` (default `30/minute`; `0` disables).

## Data locations

par-storygen uses the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/latest/) via the `xdg-base-dirs` library. Override the defaults by setting `XDG_DATA_HOME` or `XDG_CONFIG_HOME`.

**Default data directory** (`$XDG_DATA_HOME/storygen/`):

| Platform | Default path |
|----------|-------------|
| macOS | `~/.local/share/storygen/` |
| Linux | `~/.local/share/storygen/` |
| Windows | `%APPDATA%\storygen\` |

Within the data directory:

- `games/<uuid>/` — one subdirectory per save, containing `game.json` and portrait/scene images
- `library/<uuid>/` — one subdirectory per exported character, containing `character.json` and `portrait.png`

**Config state file** (`$XDG_CONFIG_HOME/storygen/state.json`) stores provider preferences, art settings, and wizard defaults:

| Platform | Default path |
|----------|-------------|
| macOS | `~/.config/storygen/state.json` |
| Linux | `~/.config/storygen/state.json` |
| Windows | `%APPDATA%\storygen\state.json` |

## Running par-storygen

```bash
make run
```

Or directly:
```bash
uv run storygen
```

Resume last game:
```bash
make resume
# or
uv run storygen run --resume
```

## Choosing a text model

par-storygen supports three text-LLM providers, all routed through pydantic-ai's OpenAI-compatible `OpenAIChatModel`.

There are two ways to select the provider and model:

1. **Environment variables** (best for one-off runs, CI, or temporary overrides) — `STORYGEN_TEXT_PROVIDER`, `STORYGEN_TEXT_MODEL`, and optionally `STORYGEN_TEXT_BASE_URL`. See [`.env.example`](./.env.example) for the full list.
2. **Settings screen** (persisted across sessions) — open the app, hit `Settings` from the main menu, and edit the "Text provider" block. Saved prefs live in `$XDG_CONFIG_HOME/storygen/state.json`.

**Priority order:** real environment variables > `.env` file > Settings-saved prefs > hardcoded default (`openai` / `gpt-6-luna`).

### OpenAI (text)
Set `OPENAI_API_KEY`. Known-good models: **`gpt-6-luna`** (default). Billing runs through api.openai.com.

### OpenRouter
Set `OPENROUTER_API_KEY` and `STORYGEN_TEXT_PROVIDER=openrouter`. OpenRouter routes to dozens of frontier models under a single key. Known-good models: **`anthropic/claude-3.5-sonnet`** (excellent structured output), **`meta-llama/llama-3.3-70b-instruct`** (open-weight, very cheap). Full catalog at [openrouter.ai/models](https://openrouter.ai/models). Keep the `<vendor>/<model>` slash form.

### Ollama (text)

Starting in **0.5.3**, the Ollama provider uses native JSON-schema output for every structured story step: theme, cast and catalog characters, backstory adaptation, beats, illustration plans, summaries, and recaps. Plain-text blurbs remain plain text; OpenAI and OpenRouter keep their existing output-tool mode. This targets local or self-hosted Ollama instances, not Ollama Cloud.

The initial Theme fix shipped in **0.5.2** and was live-tested with `qwen2.5:32b-instruct` on Ollama **0.40.16** on macOS; the reporter also confirmed that step on Windows ([#23](https://github.com/paulrobello/par-storygen/issues/23)). The 0.5.3 extension was tested with the same model on macOS through cast generation and three saved beats. A separate no-choice dead end from that run is covered by an automated regression; a full live playthrough and the 0.5.3 extension on Windows have not been verified.

Set `STORYGEN_TEXT_PROVIDER=ollama` and run `ollama serve` locally (default: `http://localhost:11434`). No API key required. Known-good models: **`llama3.3:70b`** (good narration if you have the VRAM), **`qwen2.5:32b-instruct`** (lighter, faster). The model string must match the Ollama tag exactly. For remote Ollama, set `STORYGEN_TEXT_BASE_URL=http://<host>:11434/v1`.

**Note:** Each save pins its own `text_config`. Changing the provider in Settings only affects new stories — existing saves keep what they were created with.

## Choosing an image model

par-storygen supports four image-gen providers. Only OpenAI and Gemini support *reference images* — the pattern used to keep characters visually consistent across scenes. Z.AI and Ollama are text-to-image only, so character consistency will drift.

Scene/cover art and character portraits are configured separately:

- **Scene/cover art** uses `STORYGEN_IMAGE_PROVIDER`, `STORYGEN_IMAGE_MODEL`, `STORYGEN_IMAGE_BASE_URL`, and `STORYGEN_IMAGE_API_KEY`. The hardcoded default is OpenAI **`gpt-image-2.6-flare`**.
- **Character portraits** use `STORYGEN_CHARACTER_IMAGE_PROVIDER`, `STORYGEN_CHARACTER_IMAGE_MODEL`, `STORYGEN_CHARACTER_IMAGE_BASE_URL`, and `STORYGEN_CHARACTER_IMAGE_API_KEY`. The hardcoded default is OpenAI **`gpt-image-2.6-flare`**. Both GPT Image 2.6 models support transparent PNG portraits.

There are two ways to select image providers and models:

1. **Environment variables** — use the scene/cover `STORYGEN_IMAGE_*` variables and/or the portrait-specific `STORYGEN_CHARACTER_IMAGE_*` variables. See [`.env.example`](./.env.example).
2. **Settings screen** (persisted across sessions) — edit the "Image provider" block for scene/cover art and the "Character portrait provider" block for portraits.

**Priority order for each image config:** real environment variables > `.env` file > Settings-saved prefs > hardcoded defaults (`openai` / `gpt-image-2.6-flare` for scene/cover art, `openai` / `gpt-image-2.6-flare` for character portraits).

### OpenAI (image)
Set `OPENAI_API_KEY`. The default is **`gpt-image-2.6-flare`**, OpenAI's recommended starting point for existing GPT Image 2 workflows that already meet quality needs. Choose **`gpt-image-2.6-sunburst`** for more demanding detail or precise edits. Both support reference images through `images.edit`, transparent PNG portraits, and streaming previews. Portrait sizes/quality and scene sizes/quality are unchanged. See [OpenAI's model selection guide](https://developers.openai.com/api/docs/guides/image-prompting#choose-a-model).

Existing saves pin their own image models. To update retiring GPT Image 2 / 1.5 configurations safely, follow [the migration guide](docs/IMAGE_MODEL_MIGRATION.md). Explicit environment overrides and custom endpoints are not rewritten.

The in-app image cost counter remains a coarse legacy size/quality estimate, not a bill. GPT Image 2.6 token consumption varies by model and quality; check API usage for actual costs.

### Google Gemini
Set `GEMINI_API_KEY` and `STORYGEN_IMAGE_PROVIDER=gemini`. Supports up to 14 reference images per call. Known-good models: **`gemini-3.1-flash-image-preview`** (Nano Banana 2, $0.067/1K image tokens), **`gemini-3-pro-image-preview`** (Nano Banana Pro, $0.134/image). Leave `STORYGEN_IMAGE_BASE_URL` blank for Gemini. Docs: [ai.google.dev/gemini-api/docs/image-generation](https://ai.google.dev/gemini-api/docs/image-generation).

### Z.AI GLM-image
Set `ZAI_API_KEY` and `STORYGEN_IMAGE_PROVIDER=zai`. Text-to-image only — no reference-image support. Price: $0.015/image. Known-good model: **`glm-image`**. Docs: [docs.z.ai](https://docs.z.ai).

### Ollama (image)
Set `STORYGEN_IMAGE_PROVIDER=ollama` and run `ollama serve` locally. No API key required. macOS-only as of 2026-04, requires server **≥ 0.13.3**. No reference-image support. Known-good models: **`x/z-image-turbo`** (fastest), **`x/flux2-klein:4b`**, **`x/flux2-klein:9b`** (higher quality, more VRAM). For remote Ollama, set `STORYGEN_IMAGE_BASE_URL=http://<host>:11434/v1/`.

### Fallback provider
Settings lets you pick a secondary image provider that kicks in when the primary fails. Each fallback trip fires a toast. Useful combos: OpenAI primary + Gemini fallback (both ref-supporting). Same-provider fallbacks are ignored.

**Note:** Each save pins both `image_config` (scene/cover art) and `character_image_config` (portraits). Changing either image provider in Settings only affects new stories.

## Character library: exporting and importing

par-storygen keeps a cross-game character library at `$XDG_DATA_HOME/storygen/library/` — one subdirectory per exported character holding metadata plus its portrait. Characters from finished (or in-progress) stories can be re-used in new stories without regenerating the portrait, saving both token cost and wall time.

### Export
From a game's **Portraits** screen, press the **Export** button next to any character. The character's name, backstory, personality, physical description, portrait, and the exact portrait prompt are copied into the library. Re-exporting the same character creates a separate library entry — use the Library Browser to clean up.

### Import
During the wizard's **CHARACTERS** step, press `Ctrl+L` to open the Library Browser. Pick a character, then choose:

- **Keep as-is** — added to your new story unchanged. Portrait PNG is *copied* (no image-provider API calls).
- **Adapt to theme** — an LLM rewrites **only** the backstory to fit your new story's theme. Name, personality, and physical description are preserved so the existing portrait still matches.

### Delete + sort
The Library Browser has per-entry **Delete** buttons with confirmation. Press `s` to toggle sort between **newest-first** (default) and **alphabetical**.

## Replay and endings

Once you've played through to one or more endings, two read-only views let you revisit the journey.

### Endings gallery (`i` → Endings from PlayScreen)
Press `i` → Endings to open a card per ending you've reached. Each card shows the scene image, a narration excerpt, and the path of choices you took. Press **Jump** on any ending to set the playhead to that node.

The Endings entry is hidden when no endings have been reached yet.

### Branch replay (`r` from GraphScreen)
Open the graph (`i` → Graph from PlayScreen), highlight any explored node, and press `r` to walk through every beat from root to that node. Use `space`/`right`/`n` to advance, `left`/`p` to go back, `j` to jump to live play at the current step, and `escape` to exit.

Replay is read-only — no regeneration, no LLM calls.

## Branch prefetch

While you're reading a beat, par-storygen can background-generate the next beats for each pending choice. When you pick, the next beat appears instantly (no LLM call) — assuming the prefetch finished in time.

### Enabling
Open Settings and toggle:
- **Enable branch prefetch** — turns on background generation. Off by default (spends tokens up-front for paths you may never pick).
- **Prefetch scene images too** — also runs scene-image generation for each prefetched beat. Off by default and disabled unless prefetch + global art are both on.

### What gets generated
For each pending choice from the current beat:
- Beat text (always, when prefetch is on).
- Illustration plan (always — cheap, lets you press `i` later).
- Scene image (only when "Prefetch scene images too" is on).

### Behavior notes
- **Sibling prefetches keep running after you pick.** They populate `save.nodes`, so alternate branches are free cache hits.
- **Failures are silent.** If a provider is down, you only see an error if you pick a choice whose prefetch failed (then live generation retries).
- **Settings take effect on the next beat.** Toggle any time; in-flight prefetches finish, future ones honor the new state.

## Character outfits

Define multiple looks for a single character and switch between them at will. Each outfit is its own portrait, used as the reference image for scene generation so your character appears in the picked outfit across subsequent beats.

### Creating an outfit
From PlayScreen, press `i` → Portraits. For any character:
1. Click **Add outfit**.
2. Give it a short name (e.g. "ballroom gown", "armored", "swimwear").
3. Write a one-sentence description (e.g. "wearing a flowing red gown with gold trim").
4. Press **Generate**. The new outfit is generated using the character's existing physical description PLUS your outfit description.

Outfit thumbnails appear in a row under the character's main portrait.

### Switching and deleting
Click any outfit thumbnail for a Set as current / Delete / Cancel menu. Setting an outfit updates the character's active portrait and makes scene generation use that outfit as the reference from now on.

The main (base) portrait is preserved. Press **Revert to base** (visible only when an outfit is active) to switch back. Deleting the currently-active outfit auto-reverts to base first.

### Notes
- Outfit generation uses the character portrait image provider + model + cost, same as a regular portrait.
- Image streaming is intentionally OFF for outfit generation (portraits are 5-10s — too fast for streaming to pay off).
- Library export captures only the currently-active outfit. Imported characters start with an empty `outfits` list.

## Text-to-speech

par-storygen can read narration aloud using the [par-cli-tts](https://github.com/paulrobello/par-cli-tts) library, which supports five providers: OpenAI, ElevenLabs, Deepgram, Google Gemini, and Kokoro (local).

### Setup
1. Open **Settings** from the main menu.
2. Scroll to the **Text-to-speech** section.
3. Select a provider and enter your API key.
4. Press **Refresh voices** to populate the voice dropdown, then pick a voice.
5. Optionally toggle **Auto-read** to have narration read aloud automatically after each beat.

### Playback controls (PlayScreen)

| Key | Action |
|-----|--------|
| `t` | Read aloud / pause / resume (context-dependent) |
| `T` | Restart narration from the beginning |
| `s` | Stop playback immediately |

Audio files are cached per node, provider, and voice in the save's `audio/` directory. The cache file extension follows the active TTS provider's preferred output format, so changing provider or voice generates a separate cache entry instead of replaying stale narration.

## Auto-play

Press `a` from the play screen to auto-advance the story with random choices. Auto-play:

- **Waits for the scene image** — waits until the current scene image reaches a terminal state (`done` or `failed`), then applies a 5 second viewing delay after the image is displayed (if art is enabled).
- **Waits for TTS** — if auto-read is on, waits for narration playback to finish (including through pauses) before advancing to the next random choice.
- **Stops at endings** — auto-play halts when an ending node is reached.
- **Toggle off** — press `a` again to stop at any time.

During auto-play, only `menu`, `a` (stop auto), and TTS controls (`t`/`T`/`s`) are available.

## Export book

Press `x` from the play screen when viewing an ending to export the entire story path as a self-contained HTML book. The exported book opens in your browser and includes:

- **Chapter-by-chapter navigation** with arrow keys and on-screen buttons
- **3D page-turn animation** (CSS `rotateY`) with `prefers-reduced-motion` fallback
- **Light/dark mode toggle** persisted via `localStorage`
- **Inline audio player** for chapters with TTS narration, with seek bar and time display
- **Auto-read mode** — plays each chapter's audio and advances automatically
- **Open Graph / Twitter Card meta tags** for social sharing previews
- **Scene images** copied alongside `index.html` into `~/Desktop/<Title>_Book/`

No server required — the entire book is a single HTML file with inline CSS/JS plus local image and audio assets.

## Keyboard shortcuts

par-storygen consolidates secondary views behind an **info picker** (`i`) so the most common play actions stay on single keys. The `i` modal lists Portraits, Graph, Endings, and Relationships — each entry is hidden when the current save has nothing to show for it.

### PlayScreen

| Key | Action |
|-----|--------|
| `1`–`9` | Pick choice 1–9 |
| `j` / `↓` | Highlight next choice |
| `k` / `↑` | Highlight previous choice |
| `enter` | Pick the highlighted choice |
| `i` | Info picker — Portraits / Graph / Endings / Relationships |
| `r` | Regen picker — beat / scene image / portrait / audio |
| `b` | Back one node |
| `R` (Shift+R) | "Previously on..." recap (on demand) |
| `a` | Auto-play toggle (random choices, waits for image + TTS) |
| `t` | TTS read aloud / pause / resume (context-dependent) |
| `T` | Restart narration from the beginning |
| `s` | Stop TTS playback immediately |
| `x` | Export book (when viewing an ending) |
| `m` / `esc` | Return to main menu |

During beat generation, `_loading` blocks every action except `menu`. During auto-play, only `menu`, `a` (stop), and the TTS keys (`t`/`T`/`s`) are available.

### GraphScreen (reached via `i` → Graph)

| Key | Action |
|-----|--------|
| `r` | Branch replay from the selected node |
| `p` | Prune the selected subtree (asks for confirmation) |
| `esc` | Back to PlayScreen |

Replay advances with `space` / `right` / `n`, steps back with `left` / `p`, jumps to live play with `j`, and exits with `escape`.

## Web API and frontend (optional)

The TUI is the primary surface, but par-storygen also ships an optional HTTP/WebSocket API and a Next.js frontend that drives it. Both layers reuse the same `storage` / `llm` / `images` / `pipeline` code as the TUI — they are second composition roots, not a separate product.

### FastAPI server (`src/storygen_api/`)

Installed via the `[api]` extra; the `storygen-api` console script exposes the wizard + play loop over REST plus a WebSocket for live narration.

```bash
uv sync --extra api      # install fastapi, uvicorn[standard], websockets, python-multipart
make api-dev             # uvicorn storygen_api.main:app --reload --port 8101
make api-prod            # uvicorn ... --port 8101 --workers 1
```

- **Auto-docs:** open `http://localhost:8101/docs` for the interactive OpenAPI UI.
- **WebSocket:** connect to `/api/ws/{game_id}` to receive `narration_delta`, `beat_committed`, `image_committed`, and `image_failed` events during play.
- **Single worker only:** the server keeps in-memory session/WebSocket/TTS state per process, so always run with `--workers 1` (the Makefile target does this). A second worker silently desyncs game state.

> **Security — bind address and auth.** The server binds to `127.0.0.1` by default (loopback only). Auth is two-mode: when `STORYGEN_API_TOKEN` is unset, loopback peers are trusted (so `make api-dev` + `make web-dev` work out of the box) and off-box clients are rejected with HTTP 503 / WebSocket close 4403; when the token is set, every client — loopback included — must send the matching bearer token. To expose it on a LAN, pass `--host 0.0.0.0` **and** set `STORYGEN_API_TOKEN`. See `.env.example` for the variable and the expected `Authorization: Bearer <token>` (HTTP) and `Sec-WebSocket-Protocol: bearer.<token>` (WebSocket) usage.

### Next.js frontend (`web/`)

A separate Next.js 16 app under `web/` that drives the API from a browser. See [`web/README.md`](./web/README.md) for the full route map and data flow.

```bash
make web-install         # cd web && npm install
make web-dev             # Next.js dev server on :8100 (run alongside `make api-dev`)
make web-build           # production build
```

The dev server origin (`http://localhost:8100`) is the one the API allows for CORS by default. Point the frontend at a different API via the `NEXT_PUBLIC_API_BASE` build-time variable (see [`web/README.md`](./web/README.md)); change the API's CORS or WebSocket origin allowlist via `STORYGEN_API_ALLOWED_ORIGINS` / `STORYGEN_WS_ALLOWED_ORIGINS` (comma-separated; both default to `http://localhost:8100,http://127.0.0.1:8100`).

## Troubleshooting

Common runtime problems — missing API keys, Ollama not reachable, image-provider 4xx errors, blank image panel, TTS with no audio, and the web UI failing to reach the API — are covered in [`docs/TROUBLESHOOTING.md`](./docs/TROUBLESHOOTING.md). Each entry follows a symptom → cause → fix → verify format.

## Contributing

Contributions are welcome. The full setup, gate, pre-commit, commit-style, and branch/PR flow live in [`CONTRIBUTING.md`](./CONTRIBUTING.md). The short version:

```bash
git clone https://github.com/paulrobello/par-storygen
cd par-storygen
make build         # uv sync --extra api --dev
make web-install   # only if you touch web/
make checkall      # ruff lint + pyright + pytest (the CI gate)
```

Please ensure pull requests are formatted with ruff, pass `make checkall`, and follow the [Conventional Commits](https://www.conventionalcommits.org/) style used throughout the history (`fix(scope):`, `feat(scope):`, `docs(scope):`, …).

## Roadmap

As of v0.5.0. See [CHANGELOG.md](./CHANGELOG.md) for the full release history.

### Where we are
* **Core Gameplay** — Theme wizard, beat pipeline, choice tree, caching, save/resume
* **Multi-Provider** — OpenAI, OpenRouter, Ollama for text; OpenAI, Gemini, Z.AI, Ollama for images
* **Visual Consistency** — Reference portraits, half-block inline art, character outfits
* **Cross-Game Library** — Export/import characters with optional AI backstory adaptation
* **Navigation** — Story graph, endings gallery, branch replay
* **Branch Prefetch** — Background beat generation for instant picks
* **Reader Levels** — Vocabulary and complexity controls for different age ranges
* **Text-to-Speech** — Multi-provider narration with provider/voice-aware audio caching and auto-read
* **Auto-Play** — Random-choice auto-advance with image and TTS wait gating
* **Export Book** — Standalone HTML book reader with 3D page turns, inline audio, and auto-read
* **Story Templates & Presets** — Curated and custom presets; Quick Start from the menu, Load/Save Preset in the wizard
* **Narrative Recap** — On-demand "Previously on..." summaries (`Shift+R`), auto-recap, and resume recap
* **Relationship Tracking** — Pairwise character relationships extracted inline, viewable via `i` → Relationships
* **Image Style Gallery** — Browse and apply art styles from Settings
* **Dynamic Pacing** — Slow / Moderate / Fast pacing per story

### Where we're going
* Sound effects and music per scene
* Multiplayer (shared story tree)
* More image providers and art styles
* Export stories as PDF
