# anyproto/anytype-heart architecture

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

- Interactive diagram: https://gitdiagram.com/anyproto/anytype-heart
- Repository: https://github.com/anyproto/anytype-heart

## Overview

##### Project type and purpose
- This is Anytype Heart, a Go‑based middleware library for Anytype clients. It is neither a full‑stack web app nor a pure CLI tool; rather, it exposes both a gRPC API server and a set of CLI utilities, and provides client libraries (C, JS, mobile) to apps. Its core responsibility is to implement business logic (object/block management, import/export, subscriptions, sync, storage) and expose it over gRPC.

##### Top‑level components
- **Client libraries** (`clientlibrary/…`): native C bridge, gRPC‑Web JS addon, mobile discovery APIs.
- **CLI tools** (`cmd/…`): `grpcserver`, `archiveunpacker`, `debugtree`, `logdecode`, `usecasevalidator`, etc. They invoke the core services.
- **API server** (`cmd/grpcserver` + `core/api/server`): gRPC entry point, HTTP/gRPC handlers, middleware, paginated endpoints.
- **Business logic** (`core/…` + `core/application`): object/block/domain model, import/export, subscription, sync status, invites, payments, notifications, etc.
- **Space orchestration** (`space/…` + `space/spacecore`): high‑level “space” construct, local P2P discovery, offloading, migration, storage abstraction, peer‑to‑peer coordination.
- **Persistence layer** (`spacecore/storage`): BadgerDB, SQLite, in‑memory, RPC‑backed stores.
- **Utilities** (`util/…`, `pkg/lib/…`): logging, metrics, context, protobuf helpers, image processing, network utilities, key‑value stores.
- **Protobuf definitions** (`pb/…` + `pkg/lib/cafe/pb`): service, event, snapshot, command messages.
- **Metrics** (`metrics/…`): interceptors, Anymetry client, Grafana/Prometheus provisioning.
- **Makefiles / CI / Docker**: build scripts, CI workflows, Dockerfile, docker‑compose, Nix flake.

##### Component interactions
- Clients (desktop/mobile/web) use **client libraries** to call the **gRPC API Server**.
- The API Server routes requests to **core API handlers**, which execute in‑process **business logic** modules (`core/api/internal/...`) and orchestrate use‑case flows (`core/application/...`).
- Core logic invokes **space** services for multi‑user “space” management.
- The **space** layer uses **spacecore** for low‑level P2P coordination, local discovery, and storage abstraction.
- **spacecore** persists data into BadgerDB/SQLite stores or remote RPC stores.
- Import/export subsystems use file system, zip, CSV, Markdown, Notion, PB converters.
- **Metrics & logging** are injected via gRPC interceptors in the API server.
- CLI tools instantiate the same core libraries (no server) to run offline tasks.
- The Makefiles, CI, and Docker artifacts define build/deploy flows but do not alter runtime interactions.

##### Architectural patterns and principles
- Modular monolith: clear package boundaries (API, application, domain, importers, exporters, subscription, sync).
- Dependency inversion: higher‑level modules depend on interfaces (e.g., storage, P2P) with mocks for testing.
- Layered architecture:
Presentation (CLI, gRPC handlers) → Application/service layer → Domain/core logic → Infrastructure (storage, P2P, network).
- CQRS‐style separation: import/export, read‐model pagination, write‐model application flows.
- Extensive use of interfaces and mocks for unit testing.
- gRPC services with protobuf‐generated code.

- Clients (web, mobile, desktop)
- Client Libraries (C bridge, JS addon, mobile SDK)
- gRPC API Server
- Core API Handlers & Middleware
- Application Services (use cases)
- Domain Model / Business Logic (`core/…`)
- Space Manager & Orchestrator (`space/…`)
- P2P & Local Discovery (`spacecore/localdiscovery`)
- Storage Abstraction (`spacecore/storage` sub‑modules)
- Import/Export + Converters
- Metrics & Logging
- Protobuf Definitions
- CLI Utilities
- Clients → Client Libraries → gRPC Server → Core API → App Services → Domain Logic → Space → Spacecore → Storage
- Core API ← Protobuf definitions
- CLI Utilities → App Services/Domain Logic (bypass server)
c. Highlighted technologies:
- Go, gRPC, Protocol Buffers, BadgerDB, SQLite, Prometheus/Grafana, Anymetry, CGo, Node.js addon
d. Architectural patterns: layered monolith, dependency inversion, interface mocks.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        Clients["Clients (web, mobile, desktop)"]:::presentation
        subgraph "Client Libraries"
            CBridge["C Bridge"]:::presentation
            JSAddon["JS Addon"]:::presentation
            MobileDiscovery["Mobile Discovery SDK"]:::presentation
        end
        subgraph "CLI Tools"
            GRPCServer["grpcserver"]:::presentation
            ArchiveUnpacker["archiveunpacker"]:::presentation
            DebugTree["debugtree"]:::presentation
            LogDecode["logdecode"]:::presentation
            UsecaseValidator["usecasevalidator"]:::presentation
        end
    end

    %% API & Transport Layer
    subgraph "API & Transport"
        APIServer["gRPC API Server"]:::api
        APIMiddleware["Core API Handlers & Middleware"]:::api
        ProtoDefs["Protobuf Definitions"]:::api
        Metrics["Metrics & Logging"]:::api
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        AppServices["Application Services"]:::business
        DomainLogic["Domain Model & Core Logic"]:::business
        ImportExport["Import/Export & Converters"]:::business
    end

    %% Orchestration Layer
    subgraph "Space Orchestration"
        SpaceMgr["Space Manager & Orchestrator"]:::orchestration
        P2P["P2P & Local Discovery"]:::orchestration
    end

    %% Storage / Infrastructure Layer
    subgraph "Storage / Infrastructure"
        BadgerStorage["BadgerDB (Embedded)"]:::storage
        SQLiteStorage["SQLite (Embedded)"]:::storage
        RPCStorage["RPC-backed Stores"]:::storage
    end

    %% Connections
    Clients --> CBridge
    Clients --> JSAddon
    Clients --> MobileDiscovery

    CBridge -->|"gRPC"| APIServer
    JSAddon -->|"gRPC"| APIServer
    MobileDiscovery -->|"gRPC"| APIServer

    APIServer --> APIMiddleware
    ProtoDefs --> APIMiddleware
    Metrics --> APIServer

    APIMiddleware --> AppServices
    AppServices --> DomainLogic
    DomainLogic --> ImportExport

    DomainLogic --> SpaceMgr
    SpaceMgr --> P2P

    P2P --> BadgerStorage
    P2P --> SQLiteStorage
    P2P --> RPCStorage

    ArchiveUnpacker --> AppServices
    DebugTree --> AppServices
    LogDecode --> AppServices
    UsecaseValidator --> AppServices

    %% Click Events
    click CBridge "https://github.com/anyproto/anytype-heart/blob/main/clientlibrary/clib/bridge.c"
    click JSAddon "https://github.com/anyproto/anytype-heart/blob/main/clientlibrary/jsaddon/addon.c"
    click MobileDiscovery "https://github.com/anyproto/anytype-heart/blob/main/clientlibrary/service/discovery_android.go"
    click GRPCServer "https://github.com/anyproto/anytype-heart/blob/main/cmd/grpcserver/grpc.go"
    click ArchiveUnpacker "https://github.com/anyproto/anytype-heart/blob/main/cmd/archiveunpacker/main.go"
    click DebugTree "https://github.com/anyproto/anytype-heart/blob/main/cmd/debugtree/debugtree.go"
    click LogDecode "https://github.com/anyproto/anytype-heart/blob/main/cmd/logdecode/main.go"
    click UsecaseValidator "https://github.com/anyproto/anytype-heart/blob/main/cmd/usecasevalidator/main.go"
    click APIServer "https://github.com/anyproto/anytype-heart/blob/main/core/api/server/server.go"
    click AppServices "https://github.com/anyproto/anytype-heart/blob/main/core/application/account_create.go"
    click DomainLogic "https://github.com/anyproto/anytype-heart/blob/main/core/core.go"
    click SpaceMgr "https://github.com/anyproto/anytype-heart/blob/main/space/service.go"
    click Spacecore "https://github.com/anyproto/anytype-heart/blob/main/space/spacecore/service.go"
    click BadgerStorage "https://github.com/anyproto/anytype-heart/tree/main/space/spacecore/storage/badgerstorage/"
    click SQLiteStorage "https://github.com/anyproto/anytype-heart/tree/main/space/spacecore/storage/sqlitestorage/"
    click Util "https://github.com/anyproto/anytype-heart/tree/main/util/contexthelper/"
    click CafePB "https://github.com/anyproto/anytype-heart/blob/main/pkg/lib/cafe/pb/api.pb.go"
    click Metrics "https://github.com/anyproto/anytype-heart/blob/main/metrics/interceptors.go"

    %% Styles
    classDef presentation fill:#D6EAF8,stroke:#2E86C1,color:#154360
    classDef api fill:#D5F5E3,stroke:#27AE60,color:#1E8449
    classDef business fill:#F5B041,stroke:#B9770E,color:#7E5109
    classDef orchestration fill:#D2B4DE,stroke:#884EA0,color:#512E5F
    classDef storage fill:#D5D8DC,stroke:#566573,color:#2C3E50
```
