System Architecture
ZapDev is an AI-powered web application development platform that enables users to create complete web applications through conversational AI. The system leverages a modern tech stack designed for real-time collaboration, isolated code execution, and scalable background processing.Architectural Overview
ZapDev follows a hybrid architecture combining:- Next.js 15 App Router for the frontend and API layer
- Convex as a real-time database with reactive queries and mutations
- E2B sandboxes for isolated code execution environments
- Inngest + Agent Kit for background job orchestration
- Convex hooks for type-safe client-server communication
Core Components
1. Frontend Layer (Next.js 15)
Location:src/app/
The frontend uses Next.js 15’s App Router with React Server Components for optimal performance:
- App Router: File-based routing with layouts and nested routes
- Server Components: Default rendering strategy for improved performance
- Client Components: Interactive UI elements with
'use client'directive - Streaming: Progressive UI rendering with Suspense boundaries
/- Landing page and marketing content/projects/[id]- Project workspace with split-pane interface/api/*- API routes for server-side operations
2. Real-Time Database (Convex)
Location:convex/
Convex provides a real-time, reactive database with automatic subscriptions:
Core Tables (see convex/schema.ts:92):
projects- User projects with framework selectionmessages- Conversation history (USER/ASSISTANT)fragments- Generated code snapshots with sandbox URLsfragmentDrafts- In-progress code modificationsusage- Credit tracking (free: 5/day, pro: 100/day)subscriptions- Polar billing integrationagentRuns- Background job tracking
- Reactive Queries: UI automatically updates when data changes
- Indexed Access: All queries use indexes (no
.filter()allowed) - Authentication: Clerk JWT integration via
requireAuth(ctx) - Type Safety: Generated TypeScript types for all tables
3. AI Agent Orchestration
Location:src/agents/ and src/inngest/functions/
ZapDev uses a hybrid agent execution model:
Primary Path: Custom SSE Agents
Location:src/agents/code-agent.ts:1
- Streams real-time updates via Server-Sent Events (SSE)
- Handles framework detection and model selection
- Manages E2B sandbox lifecycle
- Implements auto-fix retry logic (up to 2 attempts)
/api/agent/run/route.ts:1
Background Path: Inngest + Agent Kit
Location:src/inngest/functions/
- code-agent.ts - Long-running code generation workflows
- figma-import.ts - Figma design file processing
- webcontainer-run.ts - WebContainer-based execution
/api/inngest/route.ts:1
Event Types (see src/inngest/client.ts:4):
agent/code-agent-kit.run- Full project generationagent/fix-errors.run- Build/lint error correctionagent/figma-import.run- Design-to-code conversionagent/code-webcontainer.run- Browser-based execution
4. Isolated Code Execution (E2B)
Location:sandbox-templates/
E2B provides secure, ephemeral sandboxes for code generation:
Supported Frameworks:
- Next.js 15 (default)
- Angular 19 with Material Design
- React 18 with Vite + Chakra UI
- Vue 3 with Vuetify
- SvelteKit with DaisyUI
- Pre-warmed Templates: Custom
compile_page.shstarts dev servers before AI generation - Python Optimizations: Batch file operations to reduce API latency
- Port Mappings: Framework-specific (Next.js=3000, Vite=5173)
- File Operations:
writeFilesBatch,readFilesBatchfor efficiency
5. Feature-Based Modules
Location:src/modules/
Modules are organized by feature domain with UI and server concerns:
Module Structure:
The
server/procedures.ts files in each module contain empty tRPC routers. All API operations use Convex directly via convex/ functions.6. Authentication & Authorization (Clerk)
Integration Points:- Frontend:
@clerk/nextjsfor sign-in/sign-up flows - Backend:
@clerk/backendfor JWT verification - Convex: Token-based auth via
convex/auth.config.ts:1
- User authenticates with Clerk
- Clerk issues JWT token
- Token passed to Convex via
ConvexProviderWithClerk - Server validates token using
requireAuth(ctx)
7. Subscription Management (Polar)
Location:convex/polar.ts, convex/subscriptions.ts
Features:
- Webhook handling for subscription events
- Customer portal integration
- Usage tier management (free/pro/unlimited)
convex/usage.ts:1):
- Free tier: 5 generations/day
- Pro tier: 100 generations/day
- 24-hour rolling window
Data Flow Patterns
Code Generation Flow
Real-Time Updates Flow
Design Patterns
Feature-Based Modules
Each feature is organized in a self-contained module:- Co-location of related code
- Clear boundaries between features
- Easy to understand and modify
Optimistic Updates
Convex enables optimistic UI updates for instant feedback:Python-Optimized Sandbox Operations
Batch file operations reduce API latency:Framework Detection
Automatic framework selection via AI: Location:src/prompts/framework-selector.ts
- User describes project in natural language
- Gemini analyzes requirements
- Selects optimal framework (default: Next.js)
- Loads framework-specific prompts and templates
Security Considerations
Input Validation
- Zod Schemas: All API inputs validated with Zod
- Path Sanitization: File paths sanitized to prevent traversal
- NULL Byte Removal:
sanitizeAnyForDatabase()prevents database errors
Sandbox Isolation
- E2B Sandboxes: Complete isolation from host system
- Resource Limits: CPU, memory, and network constraints
- Ephemeral: Destroyed after generation completes
Authentication
- JWT Tokens: Clerk-issued tokens verified on every request
- Rate Limiting: Per-user rate limits in Convex
- CORS: Strict origin policies on API routes
Performance Optimizations
Database Queries
- Indexes: All Convex queries use indexes (see
convex/schema.ts:101) - Pagination: Messages and projects use cursor-based pagination
- Selective Fields: Only fetch required fields
Frontend
- Code Splitting: Automatic route-based splitting
- Server Components: Reduce client-side JavaScript
- Streaming: Progressive rendering with Suspense
- Image Optimization: Next.js Image component with lazy loading
Build Pipeline
- Turbopack: Next.js 15’s faster bundler (
--turbopackflag) - Bun: Fast package manager and runtime
- Type Checking: Incremental builds with TypeScript
Monitoring & Observability
Error Tracking
Sentry Integration (seepackage.json:55):
- Frontend error boundaries
- API route error capture
- Source maps for stack traces
Performance Monitoring
Vercel Speed Insights (seepackage.json:63):
- Web Vitals tracking
- Real user monitoring
- Performance regression alerts
Development Tools
- Inngest Dev UI: http://localhost:8288 (when running
inngest-cli dev) - Convex Dashboard: Real-time database inspection
- E2B Logs: Sandbox execution logs
Deployment Architecture
Vercel (Production)
- Frontend: Next.js app deployed to Vercel Edge Network
- API Routes: Serverless functions in Vercel
- Environment: Production environment variables
External Services
- Convex Cloud: Hosted database and backend functions
- E2B Cloud: Sandbox execution infrastructure
- Inngest Cloud: Background job orchestration
- Clerk: Authentication service
- Polar: Subscription billing
Database Migrations
Convex uses automatic schema migrations:Scalability Considerations
Horizontal Scaling
- Serverless Functions: Auto-scale with traffic
- Convex: Automatically handles concurrent queries
- E2B Sandboxes: On-demand provisioning
Resource Management
- Credit System: Prevents abuse via usage limits
- Sandbox Cleanup: Automatic destruction after use
- Rate Limiting: Per-user request throttling
Caching Strategy
- React Query: Client-side cache (1-minute stale time)
- Convex: Automatic query result caching
- CDN: Static assets via Vercel Edge Network
Migration History
Prisma → Convex Migration
Status: Complete (seeMIGRATION_STATUS.md)
Reasons:
- Real-time reactivity without polling
- Simplified deployment (no database provisioning)
- Built-in authentication integration
- Better TypeScript experience
- Replaced Prisma Client with Convex API
- Converted SQL migrations to Convex schema
- Updated all database queries to use indexes
- Migrated file storage to Convex file storage