What if a single flutter changed the course of history?
Pick a moment. Change it. Watch 1,000 years unfold.
"Does the flap of a butterfly's wings in Brazil set off a tornado in Texas?" — Edward Lorenz, 1972
The Butterfly Effect is an interactive web application that merges Chaos Theory, Alternative History, and Generative AI into a cinematic experience. Users choose a historical turning point, and the AI engine simulates how that tiny change ripples across centuries — generating unique timelines, world states, and concept art along the way.
|
Simple, guided flow:
|
|
|
|
git clone https://github.com/modarresi1913/the-butterfly-effect.git
cd the-butterfly-effect
npm install
cp .env.example .env.local
npm run devOpen http://localhost:3000 — or try the live demo at butterfly0.space-z.ai
⚙️ Production Build
npm run build
npm start📦 Tech Stack Details
┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ │ │ │ │ │ │ │
│ THE RIPPLE │──────▶│ THE MAGNITUDE│──────▶│ THE TIME LEAP│──────▶│ TIMELINE │
│ │ │ │ │ │ │ RESULTS │
│ Era │ │ Secret │ │ AI generates│ │ │
│ Location │ │ Limited │ │ 4 epochs │ │ Checkpoint 1│
│ Change │ │ Public │ │ + art │ │ Checkpoint 2│
│ │ │ │ │ │ │ Checkpoint 3│
└──────────────┘ └──────────────┘ └──────────────┘ │ Checkpoint 4│
│ + AI Art │
└──────────────┘
Each AI-generated checkpoint contains:
interface Checkpoint {
year: string; // "500 BC", "+200 years"
era_label: string; // "The Printing Dawn"
achievements: string[]; // ["Mass book production", "..."]
crises: string[]; // ["Resistance from priests"]
world_state: string; // "Persia became the knowledge center..."
geography: string; // "The Achaemenid Empire expanded..."
image_prompt: string; // Visual description for AI art generation
}the-butterfly-effect/
├── src/
│ ├── app/
│ │ ├── api/simulate/
│ │ │ └── route.ts # 🧠 AI simulation endpoint
│ │ ├── globals.css # 🎨 Dark sci-fi theme + animations
│ │ ├── layout.tsx # 📐 Root layout (LTR, metadata)
│ │ └── page.tsx # 🦋 Main app (wizard + timeline)
│ └── components/
│ └── ui/ # shadcn/ui primitives
├── .github/ # CI, issue templates, PR template
├── .env.example # Environment variables template
├── CONTRIBUTING.md # Contribution guidelines
├── LICENSE # MIT
└── README.md # You are here 📍
🛡️ Resilient JSON Parsing
AI models frequently truncate their JSON output. This project implements a multi-layer repair strategy:
- Strip markdown fences — Removes
json ...wrappers - Fix trailing commas — Cleans
{a: 1,}→{a: 1} - Detect unclosed strings — Finds the last unclosed quote and removes the incomplete key-value pair
- Auto-close brackets — Counts open/close braces and appends missing closers
- Validate structure — Ensures all required fields exist before rendering
This reduces LLM response failures from ~40% to near-zero.
💧 Hydration-Safe Particle Field
Next.js SSR requires server and client markup to match. Using Math.random() in components causes hydration mismatches because the server generates different values than the client.
Solution: Deterministic positioning via modular arithmetic:
// Instead of Math.random()
const x = ((i * 37 + 13) % 97) * 1.03;
const y = ((i * 53 + 7) % 89) * 1.14;This produces visually random-looking distributions that are identical on server and client.
🔄 Smart Retry + Fallback
Attempt 1: LLM call → parse → validate ✅/❌
Attempt 2: LLM call → parse → validate ✅/❌
Fallback: Hand-crafted checkpoints ✅
The system makes 2 attempts with the LLM. If both fail (truncation, invalid format, timeout), it falls back to pre-written thematic checkpoints so the user always gets a result.
- Shareable timeline links
- Multiple branching paths (user chooses at each checkpoint)
- Historical comparison mode (real vs. alternate)
- Export as PDF / image
- Community timeline gallery
- Mobile app (React Native)
Contributions, issues, and feature requests are welcome!
- Read CONTRIBUTING.md
- Fork the repository
- Create a feature branch
- Open a Pull Request