parththakkar106andClaude Opus 5 ae6e5af6c7 Store context_snapshot compressed
One column is 89% of the database and the free tier allows 512 MB. Reads were
already solved -- the column is deferred, so a page load never touches it and
one screen fetches one row at a time -- but nothing had costed storage, and
storage is the constraint with a cliff: 99.6 MB used, ~94 kB of disk per
action, so the ceiling arrives around 5,400 actions and 944 are stored.

Postgres already compresses it and only gets 1.7x. pglz is tuned for fast
decompression of data a query might filter on, and nothing has ever filtered
on an assembled prompt -- it is written once and read whole, rarely, by the
Insights viewer. zlib gets 3.5x on the same text for a decompress on a request
that already made an LLM call.

Done as a TypeDecorator rather than a second column, so every call site still
writes a dict and reads a dict back, and deferred/undefer/load_only keep
naming the same attribute. Only the storage format moves.

Migrations 43-45: add the bytea, convert into it, drop the original, rename.
The backfill is the one destructive step in the file -- 44 removes the only
other copy -- so it decompresses every row and compares it against what went
in, and a row that fails aborts the run. The whole loop is one transaction, so
an abort rolls the DROP back and the prompts are still there.

Verified on real Postgres, replaying 43-45 from a pre-43 schema on a throwaway
Neon database: 720,864 B of JSON became 204,293 B of bytea, 3.53x, the column
came out named context_snapshot, every snapshot compared equal and the one
NULL stayed NULL.

Postgres does not return the disk by itself: DROP COLUMN only marks the column
gone and the backfill leaves a dead tuple per row, so the table peaks near
twice its size before settling. The deploy needs one VACUUM FULL to collect
it; the migration comment says so.

The egress fixture's snapshots are prose now rather than "x" * 20_000, and the
prose generator moved to tools/fakeprose.py so the harness and the tests share
one definition. A repeated character compresses a thousandfold: against the
old fixture a compressed column looked free and the byte ceilings would have
been guarding nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Dvvqn9ZDR4ixeFPHNbww7
2026-08-17 14:08:23 +05:30
2026-08-17 14:08:23 +05:30
2026-07-06 16:58:13 +05:30

AI D&D

CI License: MIT

An AI Dungeon-style interactive storytelling app you can run entirely on your own machine — with your own AI model. Create scenarios, play open-ended adventures where an LLM narrates the world, and extend the engine with JavaScript scripts compatible with real AI Dungeon scripting.

▶️ Try it live: parththakkar106.github.io/AI-DnD

The project page loads instantly and launches the hosted demo in one tap — play a scenario as a guest, no sign-up and no API key needed. (The demo runs on a free tier that sleeps, so the first load after it's been idle takes ~30–60s to wake up.)

Want the internals? The design notes walk through the context budgeting, the world-state referee and the memory bank, and state the reasoning behind each one (Markdown version).

Built with FastAPI + SQLAlchemy on the backend and React (Vite) on the frontend, running on SQLite locally and Postgres in the cloud. Works with any OpenAI-compatible endpoint: Ollama and LM Studio locally, or OpenRouter / OpenAI / Groq / vLLM in the cloud — endpoint, key, and model are all runtime settings, and OpenRouter's free-tier models make the whole experience $0.

The play screen, with the world-state rail open

The play screen. The left rail is live world state — the AI proposes changes each turn and a Python engine decides what actually sticks. The chip under the narration reports what changed.

Features

  • The full play loop — Do / Say / Story / Continue actions, streamed AI responses (SSE), retry, undo, and edit. Reasoning models supported: "thinking" streams into a collapsible 💭 panel with its own token budget.
  • An RPG world-state engine — a scenario can declare stats, flags, milestones and a named cast; the adventure carries their live values. The design is the AI proposes deltas and a Python engine referees them: it clamps to range, enforces per-turn caps and cooldowns, keeps counters monotonic and milestones sticky, then strips the machine-readable block out of the prose (backend/app/worldstate/engine.py). Word-labelled bands (40–60: minor damage) are what make the model reliable at it. No dice, no scripting required.
  • AI Dungeon-compatible context engine — memory, author's note, and story cards (world info) triggered by keywords in recent story text, assembled under a token budget (backend/app/context/builder.py).
  • Insights: total prompt transparency — every turn stores the exact prompt sent to the model; open 🔍 on any AI action to see each context component, its token cost, and why it was included.
  • JavaScript scripting, AI Dungeon-compatible — onInput / onModelContext / onOutput modifiers with shared state and a worldEntries API, executed in an embedded quickjs sandbox (backend/app/scripting/). Real AI Dungeon scripts import and run. In-app CodeMirror editor included.
  • Auto-summarization + Memory Bank — the modern AI Dungeon memory system: AI-generated memories every few actions, a running story summary, and embedding-based retrieval that pulls old-but-relevant facts back into context, with similarity scores visible in Insights (backend/app/memorybank.py).
  • Undo and retry that actually rewind — undo and retry roll back the world state and script state to a per-action snapshot, not just the text, and prune the memories that covered the removed turns. Retries are kept as browsable variants (‹ 2/3 ›) rather than thrown away.
  • Import/export — AI Dungeon-compatible formats for scripts and scenarios; JSON for everything.
  • Optional accounts for hosted deployments — by default the app is single-user with zero auth friction; set AIDND_MULTI_USER=1 and visitors play instantly as guests (signed session cookie), can register (email + password) at any point to keep their data, and each user gets isolated data plus their own encrypted-at-rest API key. A server-funded shared demo key with a daily turn cap lets people try it without bringing a key (backend/app/auth.py).

Screenshots

Insights panel Scenario editor
Insights — the exact prompt for the next turn, broken into components with token counts and the trigger word that pulled each story card in. Authoring — stats with ranges, per-turn caps, cooldowns and word-labelled bands; NPCs the AI addresses by id.
Script editor Home
Scripting — the three AI Dungeon hooks with shared persistent state, run in a quickjs sandbox. Home — continue a story in progress or start from a scenario.

Quick start

Docker (any OS)

docker compose up --build

Open http://localhost:8000. Your data persists in a named volume across restarts.

Windows

cd backend; python -m venv .venv; .\.venv\Scripts\pip.exe install -r requirements.txt; cd ..
cd frontend; npm install; cd ..
.\start.ps1

Open http://localhost:5173 (dev servers; API docs at http://localhost:8000/docs).

macOS / Linux

./start.sh   # creates the venv and installs dependencies on first run

Open http://localhost:5173.

Connect a model

Open Settings in the app and point it at any OpenAI-compatible endpoint:

Provider Endpoint URL Notes
Ollama (local) http://localhost:11434/v1 free, private; also serves embedding models for the Memory Bank (e.g. nomic-embed-text)
LM Studio (local) http://localhost:1234/v1 free, private
OpenRouter https://openrouter.ai/api/v1 :free models cost nothing (no embeddings on the free tier)
OpenAI / Groq / vLLM / … provider's /v1 URL anything speaking /v1/chat/completions

Model name, API key, generation parameters, and (optionally) summary/embedding models for the Memory Bank are all configured there too — no config files, no rebuild.

How a turn works

player input
  → onInput script modifier
  → assemble context:  [narrator prompt] + [world state + stat guide] + [AI instructions]
                       + [plot essentials] + [story summary] + [retrieved memories]
                       + [triggered story cards] + [story history, token-budgeted]
                       + [author's note] + [player action]
  → onModelContext script modifier
  → snapshot context (Insights)
  → provider adapter → AI (streamed)
  → extract + referee the world-state delta block, strip it from the prose
  → onOutput script modifier
  → store & render

Architecture

frontend/   React + Vite SPA  ──HTTP/SSE──►  backend/  FastAPI
                                              ├─ routers/      auth, scenarios, adventures, story cards, scripts, chat, settings, debug
                                              ├─ models.py     SQLAlchemy: User, Scenario, Adventure, Action, StoryCard, Script, Settings, Memory
                                              ├─ migrations.py hand-rolled, versioned via PRAGMA user_version (37 and counting)
                                              ├─ auth.py       guest/registered users, sessions, shared demo key
                                              ├─ security.py   password hashing, cookie signing, API-key encryption
                                              ├─ context/      prompt assembly under a token budget + windowed history queries
                                              ├─ worldstate/   the stat engine: clamps, cooldowns, bands, milestones
                                              ├─ scripting/    quickjs sandbox + AI Dungeon API surface
                                              ├─ memorybank.py auto-summarization + embedding retrieval
                                              ├─ providers/    OpenAI-compatible adapter, streaming
                                              └─ data.db       SQLite (path overridable via AIDND_DB_PATH)

In production the backend serves the built SPA from one port (see Dockerfile); in development Vite proxies /api to FastAPI.

Tests

151 backend tests — unit plus full HTTP integration through the real quickjs scripting engine, with the LLM provider mocked. CI runs them on every push, alongside the frontend lint/build and a Docker image build.

cd backend && pip install -r requirements.txt -r requirements-dev.txt
python -m pytest tests/

Notes on performance

Two of the tests exist because of bugs that were measured rather than guessed at, and they're the most interesting engineering in the repo:

  • Database egress, cut ~189x. Every adventure load was pulling Action.context_snapshot — the entire assembled prompt, ~74 KB per turn — to read two small fields off it. Moving those fields into their own columns and marking the heavy ones deferred took one adventure load from 38.5 MB to 0.20 MB. The backfill runs server-side with dialect-specific SQL so the old data never crosses the wire. tests/test_egress.py hooks into SQLAlchemy's cursor events and fails if a bulk load ever names those columns again.
  • Turn cost, made flat. Assembling a turn walked the whole story, so it was O(story length) — 839 KB of reads at turn 200. backend/app/context/history.py now serves tails and slices from SQL and measures what it fetched; the same turn costs 129 KB and stops growing at around turn 50.

Deploy (Render)

The repo ships a render.yaml blueprint: one Docker web service that serves the SPA and API same-origin, backed by external Neon Postgres (the free tier has no persistent disk, so the database lives off-box).

  1. Create a Neon project and copy its pooled connection string.
  2. In Render: New → Blueprint, point it at this repo. Render reads render.yaml.
  3. Fill the secrets it prompts for (sync: false vars): AIDND_DATABASE_URL (the Neon string) and, to offer a no-signup demo, AIDND_DEMO_API_KEY / AIDND_DEMO_MODELS. AIDND_SECRET_KEY is generated automatically and kept stable across deploys.
  4. Deploy. Pushes to main auto-deploy thereafter. Health check: /api/health.

On the free tier the service sleeps after ~15 min idle; the first request then takes ~30–60s to wake. Point any keep-warm pinger at /api/health, which deliberately doesn't touch the database — waking the database around the clock costs far more than the cold start is worth.

Repo notes

  • plan/ — the phased implementation plan this was built from, kept as a build log. All twelve phases are complete; the later files (11, 12) double as design notes for the state-revert and world-state work.
  • docs/GUIDE.md — design notes: how each subsystem works and why it was built that way, with the measurements behind the decisions. Also rendered as a reading page.
  • backend/.env.example — the few environment variables the backend reads.
  • docs/self-review.md — a full-codebase self-review pass and what came out of it. All correctness findings are resolved.

License

MIT

S
Description
Create an entirely local-based interactive story generator.
Readme MIT
5.8 MiB
Languages
Python 88%
JavaScript 9.7%
CSS 2.2%