A modern, full-stack web application that uses AI to explain code in simple, understandable terms. Built with React Router, Cloudflare Workers, and powered by GPT-OSS-120B, this application provides an interactive ChatGPT-like interface for code explanation.
AI Code Explainer is a production-ready application that helps developers understand code by providing AI-powered explanations. Users can paste code snippets in various programming languages and receive detailed, markdown-formatted explanations with a smooth typing animation effect.
- AI-Powered Explanations: Leverages Cloudflare's GPT-OSS-120B model for accurate code explanations
- ChatGPT-like Interface: Modern, conversational UI with typing animations and markdown rendering
- Edge Computing: Deployed on Cloudflare Workers for global low-latency performance
- Production-Ready: Includes rate limiting, CORS, security headers, and body size limits
- Type-Safe: Built entirely with TypeScript for reliability and maintainability
- β Multi-Language Support: Supports 16+ programming languages (JavaScript, TypeScript, Python, Java, C++, Go, Rust, and more)
- β Real-Time Typing Animation: ChatGPT-style character-by-character text animation (3ms per character)
- β Markdown Rendering: Beautifully formatted explanations with code blocks, headings, lists, and links
- β Interactive Chat Interface: Message history with user and assistant messages
- β Message Actions: Stop, Copy, and Edit buttons for enhanced UX
- β Rate Limiting: 100 requests per 15 minutes per IP address
- β Body Size Limits: 10MB request size limit with proper error handling
- β CORS Protection: Configurable origin-based CORS with preflight support
- β Security Headers: Helmet-like security headers (XSS protection, frame options, CSP, etc.)
- β Error Handling: Comprehensive error handling with user-friendly messages
- β Form Validation: React Hook Form integration for robust form handling
- β Loading States: Visual feedback during API requests
- β Responsive Design: Mobile-friendly interface with Tailwind CSS
- β Dark Mode Support: Built-in dark theme support
- β Accessibility: Semantic HTML and ARIA-compliant components
- React 19.1.1 - Latest React with modern hooks and patterns
- React Router 7.9.2 - Full-stack framework with SSR support
- TypeScript 5.9.2 - Type-safe development
- Tailwind CSS 4.1.13 - Utility-first styling
- React Hook Form 7.66.0 - Performant form management
- React Markdown 10.1.0 - Markdown rendering
- Lucide React - Modern icon library
- Radix UI - Accessible component primitives
- Cloudflare Workers - Edge computing platform
- Cloudflare AI - GPT-OSS-120B model integration
- React Router SSR - Server-side rendering
- Wrangler - Cloudflare Workers deployment tool
- Vite 7.1.7 - Fast build tool and dev server
- TypeScript - Type checking and compilation
- ESLint - Code linting (if configured)
ai-code-explainer/
βββ app/
β βββ components/
β β βββ layout.tsx # Main layout component
β β βββ markdown-renderer.tsx # Client-side markdown renderer
β β βββ typing-animation.tsx # ChatGPT-style typing animation
β β βββ ui/
β β βββ button.tsx # Reusable button component
β βββ lib/
β β βββ middleware.ts # Security, rate limiting, CORS middleware
β β βββ utils.ts # Utility functions
β βββ routes/
β β βββ home.tsx # Main chat interface
β β βββ explain-code.ts # API endpoint for code explanation
β βββ root.tsx # Root layout with error boundaries
β βββ app.css # Global styles and Tailwind config
βββ workers/
β βββ app.ts # Cloudflare Worker entry point
βββ wrangler.jsonc # Cloudflare Workers configuration
βββ vite.config.ts # Vite build configuration
βββ package.json # Dependencies and scripts
- Node.js 18+ and npm/bun
- Cloudflare Account (for deployment)
- Cloudflare AI Access (for AI model usage)
-
Clone the repository
git clone <repository-url> cd ai-code-explainer
-
Install dependencies
npm install # or bun install -
Generate Cloudflare types
npm run cf-typegen
Start the development server with hot module replacement:
npm run devThe application will be available at http://localhost:5173.
Create a production build:
npm run buildPreview the production build locally:
npm run previewRun TypeScript type checking:
npm run typecheckDeploy directly to Cloudflare Workers:
npm run deployThis will:
- Build the application
- Deploy to Cloudflare Workers
- Make it available at your configured Workers URL
Configure your deployment in wrangler.jsonc:
Adjust rate limits in workers/app.ts:
rateLimit: {
maxRequests: 100, // Number of requests
windowMs: 15 * 60 * 1000, // Time window (15 minutes)
}Modify body size limit in workers/app.ts:
bodySizeLimit: "10mb" // Change to "5mb", "20mb", etc.Set allowed CORS origin in wrangler.jsonc:
{
"vars": {
"FRONTEND_URL": "https://your-allowed-origin.com"
}
}- Client Request β User submits code via form
- Middleware Layer β Rate limiting, body size check, CORS validation
- API Route β
/explain-codeendpoint processes request - AI Integration β Cloudflare AI model generates explanation
- Response β Markdown-formatted explanation returned
- UI Update β Typing animation displays explanation
- Rate Limiting: In-memory rate limiter (can be upgraded to Cloudflare KV/Durable Objects)
- CORS: Origin-based access control with preflight support
- Security Headers: XSS protection, frame options, CSP, HSTS
- Input Validation: Form validation and sanitization
- Error Handling: Graceful error handling with user feedback
Explains code using AI.
Request Body:
{
"code": "function hello() { console.log('Hello'); }",
"language": "JavaScript"
}Response:
{
"explanation": "This function...",
"language": "JavaScript",
"usage": {
"input_tokens": 50,
"output_tokens": 200,
"total_tokens": 250
}
}- ChatGPT-like Interface: Familiar conversational UI
- Typing Animation: Smooth character-by-character reveal (3ms speed)
- Markdown Support: Rich text formatting with code blocks
- Message Actions: Stop, Copy, and Edit functionality
- Responsive Design: Works on all device sizes
- Dark Mode: Built-in dark theme support
- Rate limiting prevents abuse
- CORS restricts cross-origin requests
- Security headers protect against common attacks
- Input validation prevents malicious payloads
- Error messages don't expose sensitive information
This project is licensed under the MIT License - see the LICENSE file for details.
Bhaskar Guthula
Built with modern web technologies and best practices for production deployment.
Note: This application requires Cloudflare Workers and AI access. Ensure you have the necessary permissions and quotas configured in your Cloudflare account.

{ "name": "ai-code-explainer", "vars": { "FRONTEND_URL": "https://your-domain.com" }, "ai": { "binding": "AI" } }