A Next.js application that uses AI to generate interactive educational lessons with TypeScript validation and transpilation. Perfect for creating dynamic, type-safe educational content.
- π€ AI-Powered Lesson Generation: Uses OpenAI GPT-4o-mini to generate engaging lesson content
- π¨ AI Image Generation: Uses DALL-E 3 to generate custom images for lesson content
- π TypeScript Validation: Validates and transpiles TypeScript lesson code to JavaScript using the TypeScript compiler API
- π Multiple Content Types: Supports explanation blocks, interactive quizzes, code examples, and images
- π― Interactive Quizzes: Built-in quiz component with scoring, explanations, and progress tracking
- π Real-time Updates: Supabase real-time subscriptions for live lesson status updates
- πΎ Database Storage: Stores TypeScript and JavaScript code in Supabase with visual assets
- π¨ Modern UI: Built with Tailwind CSS and shadcn/ui components
- βοΈ Serverless Ready: Optimized for Vercel deployment with proper file tracing
- Framework: Next.js 15 (App Router)
- Database: Supabase
- AI: OpenAI GPT-4o-mini via Vercel AI SDK
- Type Checking: TypeScript Compiler API
- Styling: Tailwind CSS
- UI Components: shadcn/ui (Radix UI)
- Authentication: Supabase Auth with SSR
βββ app/
β βββ api/
β β βββ generate-lesson/ # AI lesson generation endpoint
β β βββ generate-visual/ # AI image generation endpoint
β β βββ lessons/ # CRUD operations for lessons
β βββ lessons/
β β βββ [id]/ # Individual lesson viewer
β βββ page.tsx # Home page with lesson generation
βββ components/
β βββ InteractiveQuiz.tsx # Interactive quiz component
β βββ LessonsTable.tsx # Lessons listing table
βββ lib/
β βββ supabase/ # Supabase client configuration
β βββ types/ # TypeScript types
β βββ validateAndTranspile.ts # TypeScript validation & transpilation
βββ supabase/
β βββ migrations/ # Database migrations
βββ components/ui/ # shadcn/ui components
- Lesson Creation: Users enter a lesson outline on the homepage
- Database Insert: A lesson record is created with "generating" status
- AI Generation: OpenAI generates TypeScript code following the
GeneratedLessonContentinterface - Validation: The TypeScript code is validated and transpiled to JavaScript
- Retry Logic: If validation fails, the AI retries up to 5 times with feedback
- Visual Generation: DALL-E 3 generates images based on visual descriptions in the lesson content
- Storage: TypeScript code, JavaScript code, and visual assets are stored in Supabase
- Display: Lessons are rendered with interactive components and generated images
Lessons can contain the following block types:
- ExplanationBlock: Text content with optional heading and AI-generated SVG diagrams
- QuizBlock: Interactive quizzes with multiple choice questions
- CodeBlock: Code examples in TypeScript, JavaScript, or Python
- ImageBlock: AI-generated images with generation prompts
Each visual is stored with its block_id and base64 image data for efficient retrieval and display.
- Node.js 20+
- A Supabase account and project
- An OpenAI API key
-
Clone the repository:
git clone cd astral-digital-lessons
2. Install dependencies:
```bash
npm install
- Set up environment variables:
Create a .env.local file:
NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
OPENAI_API_KEY=your_openai_api_key- Set up the database:
Run the migrations in your Supabase SQL editor (in order):
# 001_create_lessons_table.sql - Creates the lessons table
# 002_add_js_code_column.sql - Adds js_code column
# 003_add_visuals_column.sql - Adds visuals array column
# 004_add_generated_content_status.sql - Adds new status values-
Run the development server:
npm run dev
Open http://localhost:3000 in your browser.
The app uses the TypeScript Compiler API for validation. The configuration is in lib/validateAndTranspile.ts and includes:
- ES2017 target
- Strict type checking
- ESNext modules
- Bundler module resolution
The next.config.ts includes output file tracing for TypeScript library files to ensure proper serverless function operation on Vercel:
outputFileTracingIncludes: {
'/api/generate-lesson': [
'node_modules/typescript/lib/lib.d.ts',
'node_modules/typescript/lib/lib.es5.d.ts',
'node_modules/typescript/lib/lib.esnext.d.ts',
],
}CREATE TABLE lessons (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
outline TEXT,
status TEXT DEFAULT 'generating' CHECK (status IN ('generating', 'generated_content', 'generated', 'failed')),
ts_code TEXT,
js_code TEXT,
visuals JSONB DEFAULT '[]'::jsonb,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);The visuals column stores an array of visual objects:
type Visual = {
block_id: number;
imageData: string; // Base64 encoded image data
type?: string; // Media type (e.g., "image/png")
}generating: Lesson content is being generatedgenerated_content: Content generated, awaiting visual generationgenerated: Fully complete with all visualsfailed: Generation failed
Creates a new lesson with "generating" status and triggers async generation.
Request:
{
"outline": "Introduction to JavaScript functions"
}Response:
{
"id": "uuid",
"title": "Introduction...",
"outline": "...",
"status": "generating",
"created_at": "..."
}Generates lesson content using AI (called asynchronously).
Request:
{
"lesson_id": "uuid",
"outline": "Lesson outline"
}Uses retry logic to ensure valid TypeScript code generation.
Generates AI images for lesson content using DALL-E 3.
Request:
{
"lesson_id": "uuid",
"js_code": "const lesson = {...};"
}Response:
{
"message": "Visuals generated",
"count": 3,
"failedCount": 0
}Generates images based on visual generation prompts in the lesson blocks. Handles partial failures gracefully by returning successful generations and marking lessons as failed only if all images fail.
The app enforces strict TypeScript validation on generated content:
- Uses the
GeneratedLessonContentinterface for type checking - Validates TypeScript syntax before transpilation
- Transpiles to ES2017 compatible JavaScript
Uses Supabase real-time subscriptions to update lesson status without page refresh:
supabase
.channel('public:lessons')
.on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'lessons' }, callback)
.subscribe();npm run dev- Start development server with Turbopacknpm run build- Build for productionnpm run start- Start production servernpm run lint- Run ESLint
- Fork the repository
- Create a feature branch
- Make your changes
- Ensure TypeScript validation passes
- Submit a pull request
This project is private and proprietary.
- Built with Next.js
- Database powered by Supabase
- AI integration via Vercel AI SDK
- UI components from shadcn/ui