How mendableai/open-lovable works
Project Type & Purpose
• This is a full-stack Next.js (React) application (“Open Lovable”) that lets users chat with AI to scaffold, edit, and preview React apps in real time.
• The App Router’s “app” directory contains both the UI pages and a suite of serverless API routes under app/api/*. These handlers orchestrate sandboxes, invoke AI services, scrape data, manage Vite builds, and return streaming code or logs back to the frontend.
Key Architectural Components
a. Frontend (Next.js App Router & React components)
• app/page.tsx, app/layout.tsx – pages and global layout
• app/components/ui & components/ui – shared UI primitives (buttons, inputs, toggles)
• components/CodeApplicationProgress, SandboxPreview, HMRErrorDetector – visual feedback & previews
• Tailwind CSS (globals.css, tailwind.config.ts) for styling
b. Backend / Orchestrator (Next.js Serverless API routes)
• app/api/*/route.ts – handlers like analyze-edit-intent, generate-ai-code-stream, run-command, sandbox-status, etc.
• config/app.config.ts – central configuration for keys, timeouts, sandbox parameters
• lib/*.ts – shared utilities (file parsing, intent analysis, context selection, file search execution)
c. External Services
• E2B API (Sandboxes) – containerized environments for running user code & Vite dev server
• Firecrawl API – web scraping / screenshotting for “scrape-url-enhanced” and “scrape-screenshot”
• AI Providers – OpenAI, Anthropic, Groq for code generation, intent analysis, streaming responses
d. Local Dev & Build Tools
• Vite (monitored via sandbox logs & vite-error endpoints)
• Next.js build & dev commands (npm run dev)
• ESLint, PostCSS, pnpm/ npm lockfiles, TypeScript config
Relationships & Data Flows
• User interacts with the React UI in the browser → UI triggers API calls (e.g., “Generate Code,” “Analyze Intent”).
• API Route receives request → uses lib utilities to:
1) Call AI provider(s) to generate or analyze code
2) Spin up or manipulate an E2B sandbox via E2B_API_KEY (create, run, kill, install packages)
3) Run Vite inside sandbox, capture logs, detect errors, apply HMR fixes
4) Optionally call Firecrawl for scraping tasks
• Streaming responses (SSE / fetch streams) traverse back to the UI to update code editor, preview pane, or error detector in real time.
Architectural Patterns & Principles
• Serverless micro-endpoints: each API function is a single-responsibility route.
• Streaming-first UX: endpoints named *-stream deliver chunked updates.
• “Sandbox-as-a-Service”: ephemeral containers via E2B while isolating user code executions.
• Clear separation: UI layer vs. orchestration layer vs. sandbox execution vs. third-party AI/web services.
Diagramming Recommendations
• Blue for frontend
• Green for serverless/API
• Orange for sandbox runtime
• Purple for external 3rd-party services
• Data formats: JSON payloads, code chunks, log streams.