# we0-dev/we0 architecture

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

- Interactive diagram: https://gitdiagram.com/we0-dev/we0
- Repository: https://github.com/we0-dev/we0

## Overview

##### Project type and purpose
- This is a monorepo full-stack application that provides:
- A cross-platform desktop client (Electron + React) with built-in IDE, chat, D2C (design-to-code), WeChat mini-program preview, and webcontainer/nodecontainer debugging
- A Next.js server (we-dev-next) offering REST/stream endpoints for chat, code generation, deployment, enhanced prompting, and model configuration
- A static/docs site under the same Next.js codebase

##### File structure
- Top level: monorepo with `apps/we-dev-client` (Electron + React) and `apps/we-dev-next` (Next.js server + docs)
- Client
- Electron main processes (`electron/*.ts`)
- React renderer (`src/…`): UI components (AiChat, WeIde, WeAPI, Settings, etc.), WebContainer/NodeContainer services, hooks, stores
- Build/config files: `package.json`, `electron-builder.*`, `vite.config.ts`, tailwind, .env.example
- Server (we-dev-next)
- Next.js App Router API routes: `src/app/api/chat`, `/deploy`, `/enhancedPrompt`, `/model`
- Utilities: file processors, diff generators, token handlers, streaming helpers
- i18n, middleware, Tailwind, docs under `content/docs`
- Shared/devops: Dockerfile, scripts/wedev-build.sh, pnpm, .npmrc

##### README for additional insights
- Monorepo with pnpm workspaces
- Electron desktop + webcontainer debugging, design restoration, existing project import, WeChat Tool integration
- Next.js backend calls third-party LLM API (OpenAI or others) via environment-driven URL & key
- Monolithic serverless functions pattern (multiple handlers under `/api`)

1) Desktop Client
- Electron Main
- React Renderer
- WebContainer Service
- NodeContainer Service
- Local MCP Server (Electron-spawned TCP)
- Local File System Access
2) Web Client
- React UI served by Vite or static CDN
3) Next.js Server (we-dev-next)
- API Routes: Chat, Deploy, EnhancedPrompt, Model
- Middleware & Cache
4) External Services
- Third-party LLM API (OpenAI, etc.)
- WeChat Mini-Program Developer Tools
5) Persistence (if any)
- In-memory or file-based cache (no explicit DB in code)

b. Relationships and interactions:
- Electron Renderer → Electron Main via IPC (startup, configuration)
- Renderer → Local WebContainer/NodeContainer (embedded terminal APIs)
- Renderer → MCP Server (TCP) for plugin/tool orchestration
- Renderer & Web Client → Next.js Server via HTTP/Fetch (API calls: chat, deploy, etc.)
- Next.js Server → Third-party LLM API (streaming or JSON)
- Next.js Server → internal cache (token counts, conversation history)
- Electron Main → spawn WeChat Developer Tools (CLI)

c. Architectural patterns & design principles:
- Monorepo with pnpm workspaces
- Electron multi-process architecture (main/renderer)
- Micro-handlers in Next.js App Router (serverless-style)
- Service abstraction for WebContainer/NodeContainer
- Stream-based chat response (Switchable-stream)
- i18n and feature flags via environment

d. Key technologies/frameworks:
- Electron, Vite, React, Zustand/Redux slices
- webcontainer & @webcontainer/api, nodecontainer
- Next.js App Router (Edge/Node), TypeScript
- Tailwind CSS, PostCSS, Zod for input validation
- pnpm, Docker

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Desktop Client
    subgraph "Desktop Client"
        direction TB
        EM["Electron Main\n(TypeScript)"]:::desktop
        EP["Electron Preload\n(TypeScript)"]:::desktop
        ER["React Renderer Entry\n(tsx)"]:::desktop
        RC["Root Component\n(App.tsx)"]:::desktop
        WC["WebContainer Service\n(index.ts)"]:::desktop
        WF["WebContainer FS\n(filesystem.ts)"]:::desktop
        NC["NodeContainer Service\n(index.ts)"]:::desktop
        NF["NodeContainer FS\n(filesystem.ts)"]:::desktop
        MCP["Local MCP Server\n(TCP)"]:::desktop
        LS["Login Server\n(CLI)"]:::desktop
    end

    %% Web Client
    subgraph "Web Client"
        direction TB
        WCli["React UI\n(Vite)"]:::web
    end

    %% Next.js Backend
    subgraph "Next.js Backend"
        direction TB
        Mid["Middleware & Caching\n(middleware.ts)"]:::backend
        ChatAPI["/api/chat Route\n(route.ts)"]:::backend
        ChatH["chatHandler\n(chatHandler.ts)"]:::backend
        Stream["switchable-stream\n(switchable-stream.ts)"]:::backend
        ChatUtils["Chat Utils\n(utils)"]:::backend
        ChatCache["Chat Cache\n(cache.ts)"]:::backend
        ChatDB["Chat DB (History)\n(database.ts)"]:::backend
        Deploy["/api/deploy Route\n(route.ts)"]:::backend
        EnPrompt["/api/enhancedPrompt\n(route.ts)"]:::backend
        Model["/api/model Route\n(route.ts)"]:::backend
        ModelCfg["Model Config\n(config.ts)"]:::backend
        Docs["Static Docs\n(content/docs)"]:::backend
        i18n["i18n Setup\n(i18n.ts)"]:::backend
    end

    %% External Services
    LLM["3rd-party LLM API\n(OpenAI, etc.)"]:::external
    WeChat["WeChat Dev Tools\n(CLI)"]:::external

    %% Relationships
    ER -->|"Initializes"| EM
    ER -->|"IPC"| EP
    ER -->|"Renders UI"| RC
    ER -->|"Uses"| WC
    ER -->|"Uses"| NC
    WC -->|"Filesystem ops"| WF
    NC -->|"Filesystem ops"| NF
    ER -->|"TCP"| MCP
    EM -->|"Spawn CLI"| LS
    EM -->|"Spawn"| WeChat

    ER -->|"HTTP/Fetch"| ChatAPI
    WCli -->|"HTTP/Fetch"| ChatAPI

    Mid -->|"Interceptors"| ChatAPI
    ChatAPI -->|"Handler"| ChatH
    ChatH -->|"Streams"| Stream
    ChatH -->|"Utils"| ChatUtils
    ChatH -->|"Cache ops"| ChatCache
    ChatCache -->|"Storage"| ChatDB

    ChatAPI -->|"Calls"| LLM
    Deploy -->|"Calls"| LLM
    EnPrompt -->|"Calls"| LLM
    Model -->|"Loads"| ModelCfg

    Mid -->|"Interceptors"| Deploy
    Mid -->|"Interceptors"| EnPrompt
    Mid -->|"Interceptors"| Model

    Docs -.->|"Served static"| Mid
    i18n -->|"Load locales"| Mid

    %% Click Events
    click EM "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/electron/main.ts"
    click EP "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/electron/preload.ts"
    click MCP "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/electron/MCPServer.ts"
    click LS "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/electron/loginServer.ts"
    click ER "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/main.tsx"
    click RC "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/App.tsx"
    click WC "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/components/WeIde/services/webcontainer/index.ts"
    click WF "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/components/WeIde/services/webcontainer/filesystem.ts"
    click NC "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/components/WeIde/services/nodecontainer/index.ts"
    click NF "https://github.com/we0-dev/we0/blob/main/apps/we-dev-client/src/components/WeIde/services/nodecontainer/filesystem.ts"
    click ChatAPI "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/chat/route.ts"
    click ChatH "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/chat/handlers/chatHandler.ts"
    click Stream "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/chat/switchable-stream.ts"
    click ChatUtils "https://github.com/we0-dev/we0/tree/main/apps/we-dev-next/src/app/api/chat/utils"
    click ChatCache "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/chat/cache.ts"
    click ChatDB "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/chat/database.ts"
    click Deploy "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/deploy/route.ts"
    click EnPrompt "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/enhancedPrompt/route.ts"
    click Model "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/model/route.ts"
    click ModelCfg "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/app/api/model/config.ts"
    click Mid "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/src/middleware.ts"
    click Docs "https://github.com/we0-dev/we0/tree/main/apps/we-dev-next/content/docs"
    click i18n "https://github.com/we0-dev/we0/blob/main/apps/we-dev-next/i18n.ts"

    %% Styles
    classDef desktop fill:#AED6F1,stroke:#1B4F72,color:#000
    classDef web fill:#ABEBC6,stroke:#1D8348,color:#000
    classDef backend fill:#F5CBA7,stroke:#B9770E,color:#000
    classDef external fill:#D5D8DC,stroke:#566573,color:#000
```
