The Audience Is The Game.
CrowdQuest AI is a Twitch-connected collaborative quest platform. A streamer composes an adventure, viewers choose the next path through real Twitch chat commands, and the community works toward a shared objective. The streamer remains in control, the AI proposes story content, and a deterministic backend owns votes, progression, and rewards.
This repository contains the complete application source, backend services, Twitch EventSub worker, persistent data model, frontend, OBS browser overlay, migrations, tests, infrastructure configuration, and operational documentation.
This repository does not ship with a demo mode or simulated Twitch audience. Real Twitch credentials and a registered Twitch developer application are required for live channel integration.
- Twitch OAuth authorization-code flow and encrypted token storage.
- Twitch EventSub WebSocket client for
channel.chat.message. - Twitch Helix chat messaging when the broadcaster grants
user:write:chat. - Real chat commands:
!quest,!vote 1,!vote 2,!vote 3,!help,!stats, and!leaderboard. - Deterministic state machine, first-vote-wins enforcement, tie policy, one contribution per viewer per round, configurable voting/objective deadlines, pause/resume clock preservation, and transactional XP rewards.
- AI Quest Composer with Ollama and configurable OpenAI-compatible provider support.
- Strict Pydantic schema validation and deterministic content fallback when a provider is unavailable.
- PostgreSQL persistence and Redis-based rate limiting/realtime fan-out.
- Streamer dashboard, Quest Studio, live control center, community leaderboard, analytics, and settings.
- Secure OBS browser overlay with a revocable private token and live WebSocket updates.
- Alembic migration, Docker Compose deployment, CI, automated tests, and operational docs.
flowchart TD
T[Twitch chat] --> E[EventSub WebSocket]
E --> W[EventSub worker]
W --> C[Command dispatcher]
C --> G[Deterministic game engine]
G --> P[(PostgreSQL)]
G --> R[(Redis events)]
R --> A[FastAPI WebSocket]
A --> D[Streamer dashboard]
A --> O[OBS browser overlay]
F[Next.js dashboard] --> API[FastAPI REST API]
API --> P
API --> Q[AI Quest Composer]
Q --> L[Ollama / OpenAI-compatible provider]
See Architecture, Gameplay, Twitch setup, Deployment, and the build/verification status.
- Docker Desktop with Docker Compose v2 (recommended local workflow).
- Twitch Developer application credentials.
- Optional AI runtime: Ollama reachable from the API container, or an OpenAI-compatible endpoint.
- For production: a domain, HTTPS reverse proxy, durable PostgreSQL/Redis storage, backups, and secret management.
- Install and start Docker Desktop.
- From the repository root, create a local environment file:
python scripts/bootstrap_env.py- Open
.envand set:TWITCH_CLIENT_IDTWITCH_CLIENT_SECRETTWITCH_REDIRECT_URI(must exactly match the Twitch Developer Console callback URL)- Optionally
OLLAMA_MODELand AI endpoint settings.
- Register the same redirect URL in the Twitch Developer Console:
http://localhost:8000/api/v1/auth/twitch/callback
- Start the application:
docker compose up --build -d-
Open:
- Web application: http://localhost:3000
- API health: http://localhost:8000/health/ready
- OpenAPI docs: http://localhost:8000/api/docs
-
Select Connect with Twitch. Approve the requested scopes. Create a quest, open its live control center, and generate an OBS overlay URL.
The default provider is Ollama. The API container uses host.docker.internal:11434 to reach an Ollama process running on the host. On Windows, start Ollama and pull the model configured in .env before generating AI quests.
Example:
ollama pull qwen2.5:7b-instructFor a compatible hosted model endpoint, set:
AI_PROVIDER=openai_compatible
OPENAI_COMPATIBLE_BASE_URL=https://your-provider.example/v1
OPENAI_COMPATIBLE_API_KEY=your-secret
OPENAI_COMPATIBLE_MODEL=your-modelThe provider is accessed only by the backend. Do not put provider keys in NEXT_PUBLIC_* variables.
| Command | Behaviour |
|---|---|
!quest |
Shows the current quest state in chat when chat-send permission is available. |
!vote 1 |
Votes for mission path 1 during an open voting round. |
!vote 2 |
Votes for mission path 2 during an open voting round. |
!vote 3 |
Votes for mission path 3 during an open voting round. |
!help |
Adds one eligible contribution per viewer per round while an objective is active. |
!stats |
Returns the current quest status when chat-send permission is available. |
!leaderboard |
Directs viewers to the community leaderboard. |
Voting and contribution identity is based on Twitch chatter user IDs. Repeated EventSub delivery is deduplicated. The first accepted vote per viewer per round is final.
- Open a session in the dashboard.
- In OBS broadcast, create a private overlay URL.
- In OBS, add a Browser Source.
- Paste the private overlay URL.
- Set a suitable source size (1920 × 1080 is recommended); crop or position it within the scene.
- Keep the URL private. Rotate it from the session control center if it is exposed.
The overlay URL is a bearer secret: anyone who possesses it can view that session's public overlay.
Backend:
cd apps/api
python -m pip install -e ".[dev]"
ruff check app tests
pytest -qFrontend:
cd apps/web
npm install
npm run typecheck
npm run lint
npm run buildA real Twitch end-to-end test requires a configured developer application, a broadcaster who authorizes the app, and a live chat session. Unit tests do not claim that external Twitch connectivity has been exercised.
.envis generated locally and ignored by Git.APP_SECRET_KEYsigns application sessions.TOKEN_ENCRYPTION_KEYencrypts Twitch access and refresh tokens at rest.TWITCH_CHAT_RESPONSE_COOLDOWN_SECONDSgates outgoing chat replies per broadcaster so busy chat activity does not trigger avoidable send-rate bursts.- PostgreSQL and Redis passwords are generated by the bootstrap script.
- Keep
COOKIE_SECURE=falseonly for local HTTP development. Production must use HTTPS andCOOKIE_SECURE=true. - Rotate secrets if they are exposed. Changing
TOKEN_ENCRYPTION_KEYwithout re-encrypting stored credentials makes existing Twitch tokens unreadable.
The included Docker Compose file is a production-oriented single-host starting point. API and web ports bind to loopback by default so a host-based Caddy reverse proxy can serve them without directly exposing application ports. Before public deployment:
- Set
APP_ENV=production. - Set
COOKIE_SECURE=true. - Set
FRONTEND_URL,BACKEND_URL,CORS_ORIGINS,TWITCH_REDIRECT_URI, andNEXT_PUBLIC_API_URLto your HTTPS domain. - Configure the exact HTTPS Twitch callback in the Twitch Developer Console.
- Put Caddy, Nginx, or a managed ingress in front of the web and API containers.
- Remove public direct exposure of PostgreSQL and Redis; they are not published by Compose.
- Configure automated encrypted backups and test restore procedures.
- Keep one active
twitch-workerreplica per deployment until distributed EventSub ownership/leases are implemented and verified. - Monitor API/worker logs, database availability, Redis availability, Twitch token status, and reconnect health.
- Pin container image digests and dependency lockfiles before a regulated or high-availability production rollout.
See Deployment for the operational checklist.
- The EventSub supervisor is designed as a single active worker process. Do not horizontally scale it without adding and validating distributed per-broadcaster leases.
- Twitch may revoke access or EventSub subscriptions. The worker marks accounts that require reauthorization; the broadcaster must reconnect.
- Model output is untrusted. Schema validation is enforced, but a production deployment should add provider-specific safety evaluation and moderation review appropriate to its community.
- This codebase is production-oriented, not a claim of completed third-party security certification, load testing, or a verified live Twitch deployment. Those require deployment-specific credentials and operational testing.
MIT. See LICENSE.