# cloudflare/vibesdk architecture

> Architecture diagram of the GitHub repository cloudflare/vibesdk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cloudflare/vibesdk
- Repository: https://github.com/cloudflare/vibesdk

## Overview

##### Project Type & Purpose
- This is an open-source, full-stack AI webapp generator (“Cloudflare VibeSDK”).
- Frontend: React + Vite + TypeScript + Tailwind SPA.
- Backend: Cloudflare Workers + Durable Objects + REST/WebSocket API.
- Database: D1 (SQLite via Drizzle ORM), KV, R2.
- AI: Multi-model calls via AI Gateway.
- Sandboxed containers: live app previews & builds.
- CI/CD: bun scripts, GitHub Actions, Wrangler deploy.

##### File Structure & Architectural Indicators
- Top-level:
- src/ → Frontend code (App.tsx, routes, components, hooks, contexts)
- public/, index.html → Static assets
- worker/ → Serverless backend (api controllers, routes, services, middleware, durable objects, database)
- container/ → Scripts & types for sandboxed containers (CLI, storage, monitoring)
- shared/types → Cross-cutting TS definitions
- migrations/ & drizzle.config.*.ts → DB schema & migrations
- scripts/ → deploy.ts, setup.ts, undeploy.ts
- wrangler.jsonc → Cloudflare Workers configuration
- Backend layering: controllers → routes → services → database → durable objects.
- Use of middleware for auth, rate-limiting, security.
- Debug-tools/ for analytics and testing.

##### Key README Insights
- Durable Objects manage stateful AI agents.
- Workers for Platforms dispatch namespace serves generated apps.

##### Components
A. Frontend SPA
- Browser
- React/Vite app (src/)
- API client & WebSocket client
B. Cloudflare Edge Layer
- Workers HTTP/API layer (worker/app.ts)
- Durable Objects cluster (CodeGeneratorAgent, etc.)
- Middleware (auth, security, rate-limit)
C. Services & Data Stores
- D1 via Drizzle ORM (migrations + services)
- KV (sessions, rate-limit store)
- R2 (templates, assets)
- AI Gateway (external LLM providers)
D. Container Sandbox Cluster
- Container service (container/*)
- Build & runtime environments for live previews
- Monitor & storage service
E. Worker for Platforms Dispatcher
- Dispatch namespace routing to user-deployed apps
F. External Integrations
- GitHub OAuth & Export
- Google/GitHub OAuth for login
- Cloudflare APIs (Workers, Durable Objects, R2, KV)

##### Relationships & Data Flows
- User → Frontend (HTTP, WS) → Workers API
- Workers API → Durable Objects (WebSocket)
- Durable Objects → AI Gateway (LLM calls)
- Controllers → Services → D1/KV/R2
- API triggers sandbox service → spin up container → R2 asset store
- Live preview served via Workers for Platforms / Dispatch
- User feedback loops back through WebSocket to agents
- Deploy action → Deployer service → Cloudflare Workers for Platforms or GitHub

##### Architectural Patterns & Principles
- Layered MVC-style backend
- Stateful agents via Durable Objects
- Micro-service style “services” directory
- Domain-driven agent workflows (planning, codegen, QA phases)
- Event-driven & streaming (WebSocket, NDJSON)
- Infrastructure as code (wrangler.jsonc, scripts)
- CI/CD automation via GitHub Actions & bun scripts

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Frontend Layer
  subgraph "Frontend Layer"
    direction TB
    User(["User"]):::external
    Browser(["Browser"]):::frontend
    ReactApp(["React/Vite App"]):::frontend
    APIClient(["API Client"]):::frontend
    WSClient(["WebSocket Client"]):::frontend
  end

  %% Cloudflare Edge Layer
  subgraph "Cloudflare Edge Layer"
    direction TB
    WorkersAPI(["Workers HTTP/API"]):::edge
    Controllers(["Controllers"]):::edge
    Routes(["API Routes"]):::edge
    Middleware(["Middleware (Auth, Security, Rate-limit)"]):::edge
    ServicesBE(["Backend Services"]):::edge
    DurableObjects(["Durable Object Agents"]):::edge
  end

  %% Data Stores & External Services
  subgraph "Data Stores & External Services"
    direction TB
    D1(["D1 (SQLite via Drizzle ORM)"]):::storage
    KV(["KV Store"]):::storage
    R2(["R2 Asset Store"]):::storage
    AIGateway(["AI Gateway"]):::external
    GitHubOAuth(["GitHub OAuth"]):::external
    GoogleOAuth(["Google OAuth"]):::external
  end

  %% Sandbox Container Cluster
  subgraph "Sandbox Container Cluster"
    direction TB
    ContainerSvc(["Container Service & SDK"]):::sandbox
    LivePreview(["Live Preview Environment"]):::sandbox
  end

  %% Dispatcher & Deploy Target
  subgraph "Dispatcher & Deploy Target"
    direction TB
    Dispatcher(["Workers for Platforms Dispatcher"]):::edge
    UserApps(["User-Deployed Apps"]):::external
    Deployer(["Deployer Service"]):::edge
  end

  %% Flows
  User -->|opens| Browser
  Browser -->|loads SPA| ReactApp
  ReactApp -->|HTTP| APIClient
  ReactApp -->|WS| WSClient
  APIClient -->|requests| WorkersAPI
  WSClient -->|streams| DurableObjects
  WorkersAPI -->|routes| Routes
  Routes -->|invoke| Controllers
  Controllers -->|use| Middleware
  Controllers -->|call| ServicesBE
  ServicesBE -->|ORM| D1
  ServicesBE -->|cache| KV
  ServicesBE -->|assets| R2
  DurableObjects -->|LLM calls| AIGateway
  AIGateway -->|responses| DurableObjects
  Controllers -->|trigger| ContainerSvc
  ContainerSvc -->|provision| LivePreview
  LivePreview -->|store assets| R2
  LivePreview -->|serve| Dispatcher
  Dispatcher -->|route traffic| UserApps
  Controllers -->|deploy| Deployer
  Deployer -->|deploy to| Dispatcher
  ReactApp -->|OAuth login| GoogleOAuth
  ReactApp -->|OAuth login| GitHubOAuth

  %% Click Events
  click ReactApp "https://github.com/cloudflare/vibesdk/tree/main/src/"
  click Browser "https://github.com/cloudflare/vibesdk/tree/main/public/"
  click APIClient "https://github.com/cloudflare/vibesdk/blob/main/src/lib/api-client.ts"
  click WSClient "https://github.com/cloudflare/vibesdk/blob/main/src/lib/app-events.ts"
  click WorkersAPI "https://github.com/cloudflare/vibesdk/blob/main/worker/index.ts"
  click Controllers "https://github.com/cloudflare/vibesdk/tree/main/worker/api/controllers/"
  click Routes "https://github.com/cloudflare/vibesdk/blob/main/worker/api/routes/index.ts"
  click Middleware "https://github.com/cloudflare/vibesdk/tree/main/worker/middleware/auth/"
  click DurableObjects "https://github.com/cloudflare/vibesdk/tree/main/worker/agents/"
  click D1 "https://github.com/cloudflare/vibesdk/blob/main/drizzle.config.ts"
  click KV "https://github.com/cloudflare/vibesdk/blob/main/worker/services/cache/KVCache.ts"
  click R2 "https://github.com/cloudflare/vibesdk/blob/main/container/storage.ts"
  click AIGateway "https://github.com/cloudflare/vibesdk/blob/main/worker/services/analytics/AiGatewayAnalyticsService.ts"
  click ContainerSvc "https://github.com/cloudflare/vibesdk/tree/main/container/"
  click Deployer "https://github.com/cloudflare/vibesdk/blob/main/worker/services/deployer/deployer.ts"
  click Dispatcher "https://github.com/cloudflare/vibesdk/blob/main/worker/utils/dispatcherUtils.ts"
  click GitHubOAuth "https://github.com/cloudflare/vibesdk/blob/main/worker/services/oauth/github.ts"
  click UserApps "https://github.com/cloudflare/vibesdk/blob/main/scripts/deploy.ts"
  click Browser "https://github.com/cloudflare/vibesdk/blob/main/index.html"
  click Deployer "https://github.com/cloudflare/vibesdk/blob/main/scripts/deploy.ts"
  click Deployer "https://github.com/cloudflare/vibesdk/blob/main/wrangler.jsonc"

  %% Styles
  classDef frontend fill:#cfe2ff,stroke:#339af0,color:#1c7ed6;
  classDef edge fill:#e6ffed,stroke:#2f9e44,color:#2f9e44;
  classDef storage fill:#fff4e6,stroke:#f08c00,color:#d9480f;
  classDef external fill:#f3f0ff,stroke:#7755cc,color:#5f3dc4;
  classDef sandbox fill:#fff9db,stroke:#fab005,color:#d9480f;
```
