# gnostr-org/gnostr architecture

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

- Interactive diagram: https://gitdiagram.com/gnostr-org/gnostr
- Repository: https://github.com/gnostr-org/gnostr

## Overview

##### Project Type & Purpose
- This is a Rust-based, CLI/TUI “git+nostr” workflow utility called **gnostr**, organized as a Cargo workspace with multiple crates.
- Its core deliverable is a terminal application combining Git operations with the Nostr protocol (P2P messaging over relays), plus a number of companion tools (asyncgit library, crawlers, testing crates, QR generator, etc.).

##### File-Structure & Architectural Patterns
- Top-level Cargo.toml defines a workspace:
- **Root crate** in `/src` for the main `gnostr` binary (and sub-bins like `git_remote_nostr`).
- **Library modules** under `src/lib` (business logic: chat, git, Nostr, UI components, subcommands).
- **Sub-crates** at root level (asyncgit, crawler, filetreelist, git2-hooks, qr, scopetime, etc.) that encapsulate reusable functionality.
- Notice:
- A modular, crate-per-capability pattern (separation by domain: Git sync, Nostr p2p, UI, subcommands).
- A Tokio-based, event-driven runtime (global runtime in `lib/global_rt.rs`).
- MVC-like separation in the main crate:
– Controllers / sub_commands dispatch → Git/Nostr services → update Model → View (TUI) renders.

##### Key Components
A. CLI & TUI Frontend
- **Entry point**: `src/main.rs` & `src/bin/...`
- **Argument parser**: `src/lib/args.rs` → builds command objects
- **Subcommands layer**: `src/lib/sub_commands/*` (e.g., fetch, push, chat, tui)
- **UI layer**: TUI framework modules (`components/`, `popups/`, `tabs/`, `ui/`, `tui.rs`)

B. Core Application Logic
- **Command dispatcher**: maps CLI input to business logic
- **State manager**: `repo_state.rs`, `queue.rs`, `watcher.rs`
- **Async event loop**: Tokio runtime + channels in `global_rt.rs`

C. Git Integration (asyncgit)
- **asyncgit crate**: sits as a dependency—handles all Git operations asynchronously
- **Sync engine**: `src/lib/git_events.rs`, `src/lib/git/utils.rs`, `asyncgit/src/sync/*`
- **Local FS**: repository files, default config (`default_config.conf`)

D. Nostr Integration
- **Core Nostr crate**: modules in `src/lib/gnostr/*`
- **P2P & Relay management**: `src/lib/chat/p2p.rs`, `relay_manager.rs` in crawler crate
- **Network layer**: WebSockets / HTTP (external relays)

E. Data Stores & Config
- **Local config**: `key_config.rs`, `login` (user, key encryption)
- **On-disk**: local Git repos (FS), possibly SQLite (`nostr-sqlite.rs` example)
- **External services**: Nostr relays, Git remotes

##### Relationships & Data Flow
- **User** → TUI/CLI → **Command Dispatcher**
- **Dispatcher** calls either:
- **Git Service** (asyncgit) → FS & Git remote via network → returns results
- **Nostr Service** → opens WebSocket to relays → publishes/subscribes events
- Results → **Model / State Manager** → **View** (UI) updates
- **Background tasks** (watcher, event subscriptions) push updates into the same event loop

##### Architectural Patterns & Principles
- **Modular crates** for reusability: asyncgit, crawler, filetreelist, etc.
- **Event-driven** with Tokio: decouples UI from I/O
- **Command pattern** for subcommands
- **MVU (Model-View-Update)** style in TUI layer: state + view + update cycle

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External User
    User["User"]:::external

    %% Frontend Layer
    subgraph "CLI/TUI Frontend"
        style CLIEntryLine stroke:#1f77b4,stroke-width:2px
        CLIEntryPointMain["src/main.rs"]:::frontend
        CLIEntryPointBin["src/bin/git_remote_nostr/*"]:::frontend
        ArgParser["src/lib/args.rs"]:::frontend
        SubCommands["src/lib/sub_commands/*"]:::frontend
        UIComponents["src/lib/components/*"]:::frontend
        UILayer["src/lib/ui/*"]:::frontend
        TUICore["src/lib/tui.rs"]:::frontend
    end

    %% Core Application Logic
    subgraph "Core Logic"
        Dispatcher["src/lib/cli.rs"]:::core
        Interactor["src/lib/cli_interactor.rs"]:::core
        StateManager["State Manager"]:::core
        RepoState["src/lib/repo_state.rs"]:::core
        Queue["src/lib/queue.rs"]:::core
        Watcher["src/lib/watcher.rs"]:::core
        EventLoop["src/lib/global_rt.rs"]:::core
    end

    %% Git Engine
    subgraph "Git Integration (asyncgit)" 
        AsyncGitCrate["asyncgit/"]:::git
        SyncEngine["asyncgit/src/sync/*"]:::git
        GitEvents["src/lib/git_events.rs"]:::git
        GitUtils["src/lib/git/utils.rs"]:::git
    end

    %% Nostr Engine
    subgraph "Nostr Integration"
        NostrCore["src/lib/gnostr/*"]:::nostr
        ChatP2P["src/lib/chat/p2p.rs"]:::nostr
        RelayMgr["crawler/src/relay_manager.rs"]:::nostr
    end

    %% External Services
    subgraph "External Services"
        GitRemote["Git Remotes"]:::external
        NostrRelays["Nostr Relays"]:::external
        LocalFS["Local FS & Config"]:::external
    end

    %% User flow
    User -->|"runs"| CLIEntryPointMain
    User -->|"runs sub-bins"| CLIEntryPointBin

    CLIEntryPointMain --> ArgParser
    CLIEntryPointBin --> ArgParser
    ArgParser --> SubCommands
    SubCommands --> Dispatcher
    Dispatcher --> Interactor
    Interactor --> EventLoop
    EventLoop --> RepoState
    EventLoop --> Queue
    EventLoop --> Watcher
    RepoState --> StateManager
    Queue --> StateManager
    Watcher --> StateManager
    StateManager --> TUICore
    TUICore --> UIComponents
    TUICore --> UILayer

    %% Git calls
    Dispatcher -->|"calls Git Service"| GitEvents
    GitEvents --> GitUtils
    GitUtils --> AsyncGitCrate
    AsyncGitCrate --> SyncEngine
    SyncEngine --> LocalFS
    SyncEngine --> GitRemote
    SyncEngine --> AsyncGitCrate

    %% Nostr calls
    Dispatcher -->|"calls Nostr Service"| NostrCore
    NostrCore --> ChatP2P
    ChatP2P --> RelayMgr
    RelayMgr --> NostrRelays

    %% Background tasks
    Watcher --> EventLoop
    RelayMgr --> EventLoop

    %% Click Events
    click CLIEntryPointMain "https://github.com/gnostr-org/gnostr/blob/main/src/main.rs"
    click CLIEntryPointBin "https://github.com/gnostr-org/gnostr/tree/main/src/bin/git_remote_nostr/"
    click ArgParser "https://github.com/gnostr-org/gnostr/blob/main/src/lib/args.rs"
    click SubCommands "https://github.com/gnostr-org/gnostr/tree/main/src/lib/sub_commands/"
    click UIComponents "https://github.com/gnostr-org/gnostr/tree/main/src/lib/components/"
    click UILayer "https://github.com/gnostr-org/gnostr/tree/main/src/lib/ui/"
    click TUICore "https://github.com/gnostr-org/gnostr/blob/main/src/lib/tui.rs"
    click Dispatcher "https://github.com/gnostr-org/gnostr/blob/main/src/lib/cli.rs"
    click Interactor "https://github.com/gnostr-org/gnostr/blob/main/src/lib/cli_interactor.rs"
    click RepoState "https://github.com/gnostr-org/gnostr/blob/main/src/lib/repo_state.rs"
    click Queue "https://github.com/gnostr-org/gnostr/blob/main/src/lib/queue.rs"
    click Watcher "https://github.com/gnostr-org/gnostr/blob/main/src/lib/watcher.rs"
    click EventLoop "https://github.com/gnostr-org/gnostr/blob/main/src/lib/global_rt.rs"
    click AsyncGitCrate "https://github.com/gnostr-org/gnostr/tree/main/asyncgit/"
    click SyncEngine "https://github.com/gnostr-org/gnostr/tree/main/asyncgit/src/sync/"
    click GitEvents "https://github.com/gnostr-org/gnostr/blob/main/src/lib/git_events.rs"
    click GitUtils "https://github.com/gnostr-org/gnostr/blob/main/src/lib/git/utils.rs"
    click NostrCore "https://github.com/gnostr-org/gnostr/tree/main/src/lib/gnostr/"
    click ChatP2P "https://github.com/gnostr-org/gnostr/blob/main/src/lib/chat/p2p.rs"
    click RelayMgr "https://github.com/gnostr-org/gnostr/blob/main/crawler/src/relay_manager.rs"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#1f77b4,color:#000;
    classDef core fill:#d4edda,stroke:#28a745,color:#000;
    classDef git fill:#ffe5cc,stroke:#ff9900,color:#000;
    classDef nostr fill:#fff3cd,stroke:#ffcc00,color:#000;
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#000;
```
