Skip to content

Latest commit

ย 

History

73 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽจ Story AI - Interactive Children's Storytelling

An AI-powered interactive storytelling app for kids that creates personalized adventures using GPT-4o-mini and DALL-E 2. Children follow the Hero's Journey through 12 magical story beats with beautiful watercolor illustrations, making choices that shape their unique adventure.

๐ŸŒ Live Demo - Try it now!

Next.js React TypeScript Tailwind CSS Vitest

โœจ Features

Core Experience

  • ๐Ÿค– AI-Powered Stories: Uses GPT-4o-mini to generate unique, age-appropriate stories
  • ๐ŸŽจ Watercolor Illustrations: DALL-E 2 creates dreamy, hand-painted style images for each story beat
  • ๐Ÿ“– Hero's Journey Structure: 12-step narrative framework for engaging storytelling
  • ๐ŸŽฎ Interactive Choices: Kids make decisions that shape the story's direction with animated transitions
  • ๐ŸŽ‰ Celebration Screen: Special ending with congratulations message and restart option

Character & Theme Customization

  • ๐Ÿ‘ค Character Wizard: Full character customization with:
    • Hair color (brown, black, blonde, red, blue, pink)
    • Hair style (short, long, curly, braids, ponytail)
    • Outfit style (adventurer, princess, superhero, wizard, explorer)
    • Favorite color influence on outfit
  • ๐Ÿฐ 5 Story Themes: Enchanted Forest, Space Adventure, Underwater Kingdom, Dinosaur Land, Fairy Tale Castle
  • ๐ŸŒ Trilingual Support: English, Thai (เธ เธฒเธฉเธฒเน„เธ—เธข), and Singlish (Singapore English)

Export & Accessibility

  • ๐Ÿ“„ PDF Export: Download completed stories as beautifully formatted PDF books
  • ๐Ÿ”Š Text-to-Speech: Listen to story narration (SpeakButton component)
  • ๐Ÿ“ฑ Responsive Design: Works beautifully on mobile, tablet, and desktop

Technical Features

  • ๐Ÿ’พ Progress Persistence: LocalStorage saves story history
  • ๐Ÿ”— Shareable Links: Preload name, gender, language, theme via URL parameters
  • โš™๏ธ Configurable: Toggle image generation on/off to manage costs
  • ๐Ÿงช Test Coverage: Vitest + React Testing Library for component testing

๐Ÿš€ Quick Start

Prerequisites

  • Node.js 20+
  • OpenAI API key with access to GPT-4o-mini and DALL-E 2

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/story-ai.git
cd story-ai
  1. Install dependencies:
pnpm install
  1. Create a .env file in the root directory:
OPENAI_API_KEY=sk-your-api-key-here
ENABLE_IMAGE_GENERATION=true  # Set to false to disable image generation
  1. Run the development server:
pnpm run dev
  1. Open http://localhost:3000 in your browser

๐Ÿ—๏ธ Tech Stack

  • Framework: Next.js 16.1.1 (App Router)
  • UI Library: React 19.2.3
  • Language: TypeScript 5.x
  • Styling: Tailwind CSS 4.x
  • AI Models:
    • GPT-4o-mini for story generation
    • DALL-E 2 for image generation
  • Validation: Zod for runtime type checking
  • Testing: Vitest + React Testing Library
  • PDF Generation: jsPDF
  • Font: Geist Sans & Geist Mono

๐Ÿ“ Project Structure

story-ai/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”œโ”€โ”€ common/               # Reusable UI components
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Button.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Card.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Input.tsx
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ LoadingSpinner.tsx
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ SpeakButton.tsx   # Text-to-speech button
โ”‚   โ”‚   โ””โ”€โ”€ story/                # Story-specific components
โ”‚   โ”‚       โ”œโ”€โ”€ CharacterWizard.tsx    # Character customization UI
โ”‚   โ”‚       โ”œโ”€โ”€ ChoiceButtons.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ ExportPdfButton.tsx    # PDF export functionality
โ”‚   โ”‚       โ”œโ”€โ”€ StartScreen.tsx        # Name, gender, language, theme selection
โ”‚   โ”‚       โ”œโ”€โ”€ StoryCarousel.tsx      # Image carousel for story review
โ”‚   โ”‚       โ”œโ”€โ”€ StoryImage.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ StoryScreen.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ StoryText.tsx
โ”‚   โ”‚       โ””โ”€โ”€ TransitionScreen.tsx   # Animated choice transitions
โ”‚   โ”œโ”€โ”€ hooks/
โ”‚   โ”‚   โ”œโ”€โ”€ useStoryGame.ts
โ”‚   โ”‚   โ””โ”€โ”€ useLocalStorage.ts
โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ””โ”€โ”€ story/
โ”‚   โ”‚       โ””โ”€โ”€ route.ts          # Story generation API endpoint
โ”‚   โ”œโ”€โ”€ layout.tsx
โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ””โ”€โ”€ globals.css
โ”œโ”€โ”€ ai/
โ”‚   โ”œโ”€โ”€ llm/
โ”‚   โ”‚   โ”œโ”€โ”€ index.ts              # LLM client
โ”‚   โ”‚   โ””โ”€โ”€ systemPrompt.ts       # Hero's Journey prompt with character support
โ”‚   โ”œโ”€โ”€ generateImage/
โ”‚   โ”‚   โ””โ”€โ”€ index.ts              # DALL-E 2 client
โ”‚   โ”œโ”€โ”€ story.ts                  # Story orchestration
โ”‚   โ”œโ”€โ”€ story.test.ts             # Story logic tests
โ”‚   โ”œโ”€โ”€ types.ts                  # AI message types
โ”‚   โ””โ”€โ”€ ai.ts                     # OpenAI client
โ”œโ”€โ”€ lib/
โ”‚   โ””โ”€โ”€ i18n/                     # Internationalization system
โ”‚       โ”œโ”€โ”€ index.ts              # t() translation function
โ”‚       โ”œโ”€โ”€ types.ts              # TranslationKey type definitions
โ”‚       โ”œโ”€โ”€ locales/              # Translation files (en, th, singlish)
โ”‚       โ””โ”€โ”€ prompts/              # AI prompt translations per language
โ”œโ”€โ”€ services/
โ”‚   โ”œโ”€โ”€ storyClient.ts            # Story API client
โ”‚   โ””โ”€โ”€ ttsClient.ts              # Text-to-speech API client
โ”œโ”€โ”€ docs/
โ”‚   โ””โ”€โ”€ internal/                 # Internal documentation and task tracking
โ”œโ”€โ”€ types.ts                      # Core types (StoryBeat, CharacterCustomization, etc.)
โ”œโ”€โ”€ vitest.config.ts              # Vitest configuration
โ”œโ”€โ”€ vitest.setup.ts               # Test setup
โ””โ”€โ”€ vitest.smoke.test.ts          # Smoke tests

๐ŸŽฎ How It Works

Story Generation Flow

  1. Character Setup: Child enters name, selects gender, language, theme, and customizes character
  2. LLM Call: GPT-4o-mini generates story text + choices + transitions + image prompt
  3. Image Generation: DALL-E 2 creates watercolor illustration with consistent character design
  4. Transition Animation: Selected choice triggers animated transition screen
  5. User Choice: Child selects from 3 story choices
  6. Repeat: Steps 2-5 for 12 beats (Hero's Journey)
  7. Celebration: Special ending screen with PDF export option

Hero's Journey Structure (12 Beats)

  1. Ordinary World (The Dream)
  2. Call to Adventure
  3. Refusal of the Call
  4. Meeting the Mentor
  5. Crossing the Threshold
  6. Tests, Allies, Enemies
  7. Approach to the Inmost Cave
  8. The Ordeal
  9. Reward (Seizing the Sword)
  10. The Road Back
  11. Resurrection
  12. Return with the Elixir (ending - no choices)

Story Themes

Theme English Setting Thai Setting
๐ŸŒฒ Enchanted Forest Magical forest with talking animals, fairies เธ›เนˆเธฒเธงเธดเน€เธจเธฉเธ—เธตเนˆเธกเธตเธชเธฑเธ•เธงเนŒเธžเธนเธ”เน„เธ”เน‰ เธ™เธฒเธ‡เธŸเน‰เธฒเนƒเธˆเธ”เธต
๐Ÿš€ Space Adventure Outer space with planets, aliens, rockets เธญเธงเธเธฒเธจเธเธงเน‰เธฒเธ‡เนƒเธซเธเนˆ เธกเธตเธ”เธฒเธงเน€เธ„เธฃเธฒเธฐเธซเนŒเธซเธฅเธฒเธเธชเธต
๐Ÿ  Underwater Kingdom Ocean with mermaids, dolphins, treasures เนƒเธ•เน‰เธ—เน‰เธญเธ‡เธกเธซเธฒเธชเธกเธธเธ—เธฃ เธกเธตเธ™เธฒเธ‡เน€เธ‡เธทเธญเธ เธ›เธฐเธเธฒเธฃเธฑเธ‡
๐Ÿฆ• Dinosaur Land Prehistoric world with gentle dinosaurs เน‚เธฅเธเธขเธธเธ„เธเนˆเธญเธ™เธ›เธฃเธฐเธงเธฑเธ•เธดเธจเธฒเธชเธ•เธฃเนŒ เธกเธตเน„เธ”เน‚เธ™เน€เธชเธฒเธฃเนŒ
๐Ÿฐ Fairy Tale Castle Magical kingdom with castles, dragons เธญเธฒเธ“เธฒเธˆเธฑเธเธฃเธกเธซเธฑเธจเธˆเธฃเธฃเธขเนŒ เธกเธตเธ›เธฃเธฒเธชเธฒเธ— เธกเธฑเธ‡เธเธฃ

Note: Singlish stories use predominantly English with subtle Singapore English expressions ("lah", "leh", "shiok", etc.).

โš™๏ธ Configuration

Environment Variables

# Required
OPENAI_API_KEY=sk-...

# Optional (defaults shown)
ENABLE_IMAGE_GENERATION=true  # Set to false to disable images and reduce cost

Cost Optimization

With Images Enabled (default):

  • LLM: ~$0.03 per story (12 beats)
  • Images: ~$0.24 per story (12 images ร— $0.02)
  • Total: ~$0.27 per story

With Images Disabled:

  • LLM: ~$0.03 per story
  • Images: $0.00
  • Total: ~$0.03 per story (89% savings)

To disable images during development:

ENABLE_IMAGE_GENERATION=false pnpm run dev

URL Parameters (Shareable Links)

Preload the start screen with custom settings using URL query parameters:

https://your-app.com/?name=Alex&gender=girl&language=singlish&theme=space_adventure
Parameter Values Default
name Any string (max 50 chars, HTML sanitized) Empty
gender boy, girl boy
language en, th, singlish en
theme enchanted_forest, space_adventure, underwater_kingdom, dinosaur_land, fairy_tale_castle enchanted_forest

All parameters are optional. Invalid values are silently ignored and fall back to defaults.

๐Ÿ› ๏ธ Development

Scripts

pnpm run dev           # Start development server
pnpm run build         # Production build
pnpm run start         # Start production server
pnpm run lint          # Run ESLint
pnpm run test          # Run Vitest in watch mode
pnpm run test:run      # Run tests once
pnpm run test:coverage # Run tests with coverage report

Testing

The project uses Vitest with React Testing Library for component testing.

# Run all tests
pnpm run test

# Run tests once (CI mode)
pnpm run test:run

# Run with coverage
pnpm run test:coverage

Test Files:

  • ai/story.test.ts - Story orchestration logic tests
  • app/components/story/StoryScreen.test.tsx - StoryScreen component tests
  • app/components/story/StoryCarousel.test.tsx - StoryCarousel component tests
  • app/components/story/ExportPdfButton.test.tsx - PDF export tests
  • vitest.smoke.test.ts - Smoke tests

Performance Logging

The app includes performance metrics in the console:

[PERF] Starting LLM call...
[PERF] LLM completed in 2500ms
[PERF] Starting image generation...
[PERF] Image generation completed in 15000ms
[PERF] Total runStory duration: 17500ms (LLM: 2500ms, Image: 15000ms)

๐Ÿ“ API Routes

POST /api/story

Generate next story beat based on history.

Request:

{
  "name": "Alice",
  "gender": "girl",
  "language": "en",
  "theme": "enchanted_forest",
  "character": {
    "hairColor": "blonde",
    "hairStyle": "braids",
    "outfitStyle": "princess",
    "favoriteColor": "pink"
  },
  "history": [
    {
      "storyText": "Alice found herself in a magical garden...",
      "choices": ["Explore the forest", "Follow the river", "Climb the mountain"],
      "choicesWithTransition": [
        { "text": "Explore the forest", "transition": ["She walked into the woods...", "The trees sparkled..."] }
      ],
      "imagePrompt": "A young girl with braided blonde hair...",
      "imageUrl": "https://...",
      "selected": "Explore the forest"
    }
  ]
}

Response:

{
  "storyText": "Alice ventured into the enchanted forest...",
  "choices": ["Talk to the owl", "Pick the glowing flower", "Cross the bridge"],
  "choicesWithTransition": [
    { "text": "Talk to the owl", "transition": ["She approached the wise owl...", "Its eyes sparkled..."] }
  ],
  "imagePrompt": "A young girl with braided blonde hair wearing a pink princess dress talking to a wise owl...",
  "imageUrl": "https://..."
}

๐Ÿšข Deployment

Vercel (Recommended)

  1. Push to GitHub
  2. Import to Vercel
  3. Add environment variables:
    • OPENAI_API_KEY
    • ENABLE_IMAGE_GENERATION
  4. Deploy

Other Platforms

Build the production bundle:

pnpm run build
pnpm run start

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License.

๐Ÿ™ Acknowledgments

  • OpenAI for GPT-4o-mini and DALL-E 2
  • Vercel for Next.js framework
  • Joseph Campbell for the Hero's Journey framework

๐Ÿ“ง Contact

Pariyawit J - @yourtwitter

Project Link: https://github.com/yourusername/story-ai


Built with โค๏ธ using Next.js, React, and OpenAI

About

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages