# prateekjannu/zen architecture

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

- Interactive diagram: https://gitdiagram.com/prateekjannu/zen
- Repository: https://github.com/prateekjannu/zen

## Overview

##### Main Components
- Frontend UI (Svelte/Vite)
- Backend Server (Python, uv)
- Agent Orchestrator & Sub‑agents
- LLM Clients (Gemini, optional OpenAI)
- Browser Automation (Playwright)
- Memory & Persistence (SQLite `zen.db`, project files)
- Services Layer (Git, GitHub, Netlify, Search APIs)
- Logging & Monitoring

##### Relationships and Data Flows
- UI ↔ Backend: bi‑directional WebSocket/HTTP (via `ui/src/lib/sockets.js` and `src/socket_instance.py`)
- Backend ↔ LLM APIs: request/response over HTTPS (Gemini API via `src/llm/gemini_client.py`)
- Agent Orchestrator ⟶ Sub‑agents: in‑process calls (each agent module imports `src/agents/...`)
- Agents ⟶ Browser: control Playwright (`src/browser/browser.py`) for web searches and scraping
- Agents ⟶ Memory DB: read/write to SQLite (`data/db/zen.db` via `src/memory/knowledge_base.py`)
- Agents ⟶ External Services: Git/GitHub pulls/pushes (`src/services/git.py`, `github.py`), Netlify deploys (`netlify.py`), search API calls (`src/services/utils.py`)
- Logging: all components emit logs to `data/logs/zen_agent.log` via `src/logger.py`

##### Architectural Patterns
- Layered Architecture:
- Presentation (UI)
- API/WebSocket Gateway
- Application Logic (Agent Orchestrator → Sub‑agents)
- Integration Layer (LLM client, Browser, Services)
- Persistence (SQLite, project filesystem)
- Plugin‑like Agent Modules: each agent (`action`, `planner`, `researcher`, etc.) implements a common interface (`src/agents/agent.py`) and Jinja2 prompts.

##### Incorporate Key Technologies
- Frontend: Svelte, Vite, Bun, Tailwind CSS
- Backend: Python ≥3.10, `uv` server, Playwright, SQLite
- APIs: Gemini, Bing/Google Search, GitHub
- Inter‑process comms: WebSockets (socket.io style)

##### Essential Details to Capture
- Frontend ↔ Backend communication channel (sockets.js ↔ socket_instance.py)
- Agent orchestration flow: high‑level task → planner → researcher/builder → coder → patcher → reporter
- Integration modules: browser automation, API calls, file I/O
- Persistence: how projects and logs are stored under `data/`

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers
    subgraph "Presentation Layer" 
        direction TB
        UI["Frontend UI (Svelte/Vite)"]:::frontend
        WSClient["WebSocket Client"]:::frontend
        APIClient["HTTP API Client"]:::frontend
    end

    subgraph "API/WebSocket Gateway"
        direction TB
        GatewayWS["WebSocket / HTTP Gateway"]:::api
        Init["Backend Init & Routing"]:::api
    end

    subgraph "Application Logic"
        direction TB
        OrchestratorCore["Agent Orchestrator Core Interface"]:::logic
        Runner["Agent Orchestrator Runner"]:::logic
        subgraph "Sub‑agents"
            direction TB
            Planner["Planner Agent"]:::logic
            Researcher["Researcher Agent"]:::logic
            Coder["Coder Agent"]:::logic
            Patcher["Patcher Agent"]:::logic
            Reporter["Reporter Agent"]:::logic
            Action["Action Agent"]:::logic
            Decision["Decision Agent"]:::logic
            Feature["Feature Agent"]:::logic
            Formatter["Formatter Agent"]:::logic
            Monologue["Internal Monologue Agent"]:::logic
            Answer["Answer Agent"]:::logic
        end
    end

    subgraph "Integration Layer"
        direction TB
        subgraph "LLM Clients"
            direction TB
            GeminiClient["Gemini API Client"]:::integration
            LLMAbstraction["LLM Abstraction Layer"]:::integration
        end
        subgraph "Browser Automation"
            direction TB
            BrowserCore["Browser Orchestration"]:::integration
            BrowserHelpers["Browser Interaction Helpers"]:::integration
            SearchAdapters["Search Adapters"]:::integration
        end
        subgraph "Services"
            direction TB
            GitOps["Git Operations"]:::integration
            GitHubAPI["GitHub API Integration"]:::integration
            NetlifySvc["Netlify Deployment"]:::integration
            Utils["Service Utilities"]:::integration
        end
        subgraph "Project & Filesystem API"
            direction TB
            ProjectAPI["Project API"]:::integration
            FileSystem["Filesystem Reader"]:::integration
        end
    end

    subgraph "Persistence Layer"
        direction TB
        SQLiteDB["SQLite Database"]:::persistence
        KnowledgeBase["Knowledge Base Interface"]:::persistence
        RAG["Retrieval‑Augmented Gen Helper"]:::persistence
    end

    subgraph "Logging & Monitoring"
        direction TB
        LoggerMod["Central Logger Module"]:::logging
        LogFile["Agent Log File"]:::logging
    end

    subgraph "Sandbox Environment"
        direction TB
        CodeRunner["Code Runner Sandbox"]:::sandbox
        Firejail["Firejail Security Wrapper"]:::sandbox
    end

    CLIConfig["CLI & Configuration"]:::config
    StateMgmt["Process State Management"]:::config
    Documentation["Documentation & Architecture Specs"]:::config

    %% External Services
    GeminiAPI["Gemini API"]:::external
    OpenAI["OpenAI API"]:::external
    SearchEngines["Search Engines (Bing/Google)"]:::external
    GitHubExt["GitHub"]:::external
    NetlifyExt["Netlify"]:::external

    %% Connections
    UI -->|HTTP/WebSocket| GatewayWS
    WSClient -->|WebSocket| GatewayWS
    APIClient -->|HTTP| GatewayWS
    GatewayWS -->|routes to| Init
    Init -->|invokes| Runner
    Runner -->|uses| OrchestratorCore
    OrchestratorCore -->|spawns| Planner
    OrchestratorCore -->|spawns| Researcher
    OrchestratorCore -->|spawns| Coder
    OrchestratorCore -->|spawns| Patcher
    OrchestratorCore -->|spawns| Reporter
    OrchestratorCore -->|spawns| Action
    OrchestratorCore -->|spawns| Decision
    OrchestratorCore -->|spawns| Feature
    OrchestratorCore -->|spawns| Formatter
    OrchestratorCore -->|spawns| Monologue
    OrchestratorCore -->|spawns| Answer

    Planner -->|calls| GeminiClient
    Researcher -->|uses| SearchAdapters
    Researcher -->|automates| BrowserCore
    Coder -->|writes| FileSystem
    AgentsAll(Agents) -.->|read/write| SQLiteDB
    AgentsAll -.->|use| KnowledgeBase
    AgentsAll -.->|call| RAG
    AgentsAll -.->|execute| CodeRunner
    AgentsAll -.->|sandboxes| Firejail
    AgentsAll -.->|perform| GitOps
    AgentsAll -.->|perform| GitHubAPI
    AgentsAll -.->|deploy| NetlifySvc
    AgentsAll -.->|fetch via| Utils
    AgentsAll -.->|manage state| StateMgmt
    LoggerMod -->|logs to| LogFile
    AgentsAll -->|emit logs| LoggerMod

    GeminiClient -->|HTTPS| GeminiAPI
    LLMAbstraction -->|routes| GeminiClient
    LLMAbstraction -->|could call| OpenAI
    SearchAdapters -->|HTTP| SearchEngines
    GitHubAPI -->|HTTPS| GitHubExt
    NetlifySvc -->|HTTPS| NetlifyExt

    %% Click Events
    click UI "https://github.com/prateekjannu/zen/tree/main/ui/src"
    click WSClient "https://github.com/prateekjannu/zen/blob/main/ui/src/lib/sockets.js"
    click APIClient "https://github.com/prateekjannu/zen/blob/main/ui/src/lib/api.js"
    click GatewayWS "https://github.com/prateekjannu/zen/blob/main/src/socket_instance.py"
    click Init "https://github.com/prateekjannu/zen/blob/main/src/init.py"
    click OrchestratorCore "https://github.com/prateekjannu/zen/blob/main/src/agents/agent.py"
    click Runner "https://github.com/prateekjannu/zen/blob/main/src/agents/runner/runner.py"
    click Planner "https://github.com/prateekjannu/zen/blob/main/src/agents/planner/planner.py"
    click Researcher "https://github.com/prateekjannu/zen/blob/main/src/agents/researcher/researcher.py"
    click Coder "https://github.com/prateekjannu/zen/blob/main/src/agents/coder/coder.py"
    click Patcher "https://github.com/prateekjannu/zen/blob/main/src/agents/patcher/patcher.py"
    click Reporter "https://github.com/prateekjannu/zen/blob/main/src/agents/reporter/reporter.py"
    click Action "https://github.com/prateekjannu/zen/blob/main/src/agents/action/action.py"
    click Decision "https://github.com/prateekjannu/zen/blob/main/src/agents/decision/decision.py"
    click Feature "https://github.com/prateekjannu/zen/blob/main/src/agents/feature/feature.py"
    click Formatter "https://github.com/prateekjannu/zen/blob/main/src/agents/formatter/formatter.py"
    click Monologue "https://github.com/prateekjannu/zen/blob/main/src/agents/internal_monologue/internal_monologue.py"
    click Answer "https://github.com/prateekjannu/zen/blob/main/src/agents/answer/answer.py"
    click GeminiClient "https://github.com/prateekjannu/zen/blob/main/src/llm/gemini_client.py"
    click LLMAbstraction "https://github.com/prateekjannu/zen/blob/main/src/llm/llm.py"
    click BrowserCore "https://github.com/prateekjannu/zen/blob/main/src/browser/browser.py"
    click BrowserHelpers "https://github.com/prateekjannu/zen/blob/main/src/browser/interaction.py"
    click SearchAdapters "https://github.com/prateekjannu/zen/blob/main/src/browser/search.py"
    click KnowledgeBase "https://github.com/prateekjannu/zen/blob/main/src/memory/knowledge_base.py"
    click RAG "https://github.com/prateekjannu/zen/blob/main/src/memory/rag.py"
    click SQLiteDB "https://github.com/prateekjannu/zen/blob/main/data/db/zen.db"
    click GitOps "https://github.com/prateekjannu/zen/blob/main/src/services/git.py"
    click GitHubAPI "https://github.com/prateekjannu/zen/blob/main/src/services/github.py"
    click NetlifySvc "https://github.com/prateekjannu/zen/blob/main/src/services/netlify.py"
    click Utils "https://github.com/prateekjannu/zen/blob/main/src/services/utils.py"
    click LoggerMod "https://github.com/prateekjannu/zen/blob/main/src/logger.py"
    click LogFile "https://github.com/prateekjannu/zen/blob/main/data/logs/zen_agent.log"
    click ProjectAPI "https://github.com/prateekjannu/zen/blob/main/src/apis/project.py"
    click FileSystem "https://github.com/prateekjannu/zen/blob/main/src/filesystem/read_code.py"
    click CodeRunner "https://github.com/prateekjannu/zen/blob/main/src/sandbox/code_runner.py"
    click Firejail "https://github.com/prateekjannu/zen/blob/main/src/sandbox/firejail.py"
    click StateMgmt "https://github.com/prateekjannu/zen/blob/main/src/state.py"
    click CLIConfig "https://github.com/prateekjannu/zen/blob/main/config.toml"
    click Documentation "https://github.com/prateekjannu/zen/blob/main/docs/architecture/ARCHITECTURE.md"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,color:#004085;
    classDef api fill:#d1ecf1,stroke:#0c5460,color:#0c5460;
    classDef logic fill:#d4edda,stroke:#155724,color:#155724;
    classDef integration fill:#fff3cd,stroke:#856404,color:#856404;
    classDef persistence fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
    classDef logging fill:#f8d7da,stroke:#721c24,color:#721c24;
    classDef sandbox fill:#f5c6cb,stroke:#721c24,color:#721c24;
    classDef config fill:#e2f0d9,stroke:#2d572c,color:#2d572c;
    classDef external fill:#f0f0f0,stroke:#888888,color:#333333,stroke-dasharray: 5 5;
```
