# pyspur-dev/pyspur architecture

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

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

## Overview

##### Main Components
- Frontend Application:
- Built with Next.js (React-based) from the “frontend” directory.
- Contains pages, components, state management (store slices), and routing.
- Interacts with the backend via RESTful API endpoints.
- Backend Application:
- Written in Python under the “backend” directory (pyspur package).
- Provides a set of API endpoints (under “backend/pyspur/api”) for agent management, file management, dataset and evaluation management, workflow operations, etc.
- Contains specialized modules for node handling, integrations (Slack, GitHub, Google, etc.), RAG (including vector datastore interactions and document processing), evaluations, and execution of workflows.
- Utilizes Alembic for database migrations, and supports both SQLite (development) and Postgres (production) as noted in the README and .env configuration.
- Database:
- A PostgreSQL database (or SQLite for testing) that persists user data, agent workflows, document collections, and evaluation runs.
- External Services and Integrations:
- Various third-party services such as OpenAI, Anthropic, Slack, Google Sheets, GitHub, YouTube, etc.
- Also includes optional vector database providers for RAG (Chroma, pgvector, Pinecone, etc.).
- Container and Deployment Infrastructure:
- Dockerfiles (Dockerfile.backend, Dockerfile.frontend) and docker-compose files are provided for development and production deployment.
- The devcontainer configuration supports a containerized development environment.
- Documentation & Dev Tools:
- The “docs” directory provides API references, walkthroughs, and essential guides.
- Scripts (like generate_migrations.sh, start_pyspur_docker.sh) help with automation and deployment.

- Render the Frontend and Backend as separate, clearly labeled nodes. Show how the frontend (via HTTP/HTTPS requests) calls the backend API.
- Backend’s internal modular structure by grouping related sub-components:
- API Layer: Routes and endpoints managing agent workflows (from “api” folder).
- Execution & Node Management: Nodes, execution contexts, CLI integration, and looping constructs (from “nodes”, “execution” directories).
- Integrations: Connectors for external services and providers (e.g., Slack, GitHub, Google, etc.).
- RAG Module: Components for document chunking, vector index creation, and datastore providers.
- Database Interaction: The module with Alembic migration scripts and SQL operations.
- Backend to the Database, indicating bi-directional data flow (CRUD operations).
- External Services as separate entities that the Backend communicates with, usually through dedicated integration modules.
- Docker/Container orchestration as the underlying infrastructure layer that houses both the Frontend and Backend components.

##### Architectural Patterns and Principles
- Separation of Concerns: Distinct separation between frontend UI, backend services/APIs, and external integrations.
- Modularity: The backend is divided into clear modules (e.g., API endpoints, node management, RAG, integrations) which can be independently maintained or extended.
- Containerization: Use of Docker ensures consistent environments across development and production.
- Extensibility: Python-based node additions and integrations that allow developers to plug in new functionalities with minimal friction.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Frontend["Frontend (Next.js)"]:::frontend
    Backend["Backend (Python - PySpur)"]:::backend

    subgraph "Backend Modules"
        AP["API Endpoints"]:::module
        NE["Node Execution & Management"]:::module
        EI["External Integrations"]:::module
        RAG["RAG Module"]:::module
        DBM["Database Interaction"]:::module
    end

    Database["Database (PostgreSQL/SQLite)"]:::database
    ExtServices["External Services"]:::external
    Deploy["Deployment Infrastructure"]:::container
    Docs["Documentation & Dev Tools"]:::documentation

    Frontend -->|"HTTP_calls"| Backend
    Backend --> AP
    Backend --> NE
    Backend --> EI
    Backend --> RAG
    Backend --> DBM
    DBM -->|"CRUD"| Database
    EI -->|"API_calls"| ExtServices

    Deploy --- Frontend
    Deploy --- Backend

    Docs --- Frontend
    Docs --- Backend

    click Frontend "https://github.com/pyspur-dev/pyspur/tree/main/frontend/"
    click Backend "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/"
    click AP "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/api/"
    click NE "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/execution/"
    click NE "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/nodes/"
    click EI "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/integrations/"
    click EI "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/nodes/integrations/"
    click RAG "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/rag/"
    click DBM "https://github.com/pyspur-dev/pyspur/blob/main/backend/pyspur/database.py"
    click DBM "https://github.com/pyspur-dev/pyspur/tree/main/backend/pyspur/models/management/alembic/"
    click Deploy "https://github.com/pyspur-dev/pyspur/blob/main/Dockerfile.backend"
    click Deploy "https://github.com/pyspur-dev/pyspur/blob/main/Dockerfile.frontend"
    click Deploy "https://github.com/pyspur-dev/pyspur/blob/main/docker-compose.yml"
    click Deploy "https://github.com/pyspur-dev/pyspur/blob/main/docker-compose.dev.yml"
    click Deploy "https://github.com/pyspur-dev/pyspur/blob/main/docker-compose.staging.yml"
    click Deploy "https://github.com/pyspur-dev/pyspur/tree/main/.devcontainer/"
    click Docs "https://github.com/pyspur-dev/pyspur/tree/main/docs/"
    click Docs "https://github.com/pyspur-dev/pyspur/tree/main/scripts/"

    classDef frontend fill:#ffe6b3,stroke:#cc6600,stroke-width:2px;
    classDef backend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef module fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef container fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef documentation fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
