# sjzar/chatlog architecture

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

- Interactive diagram: https://gitdiagram.com/sjzar/chatlog
- Repository: https://github.com/sjzar/chatlog

## Overview

##### Overall Architecture – Identify Major Components
- Command-Line Interface (CLI) & Terminal UI: Represented by the main executable (cmd/chatlog) and internal/ui packages (footer, help, infobar, menu, style). This component handles local user interactions.
- HTTP API Service: The HTTP-related code inside internal/chatlog/http exposes REST endpoints (e.g., /api/v1/chatlog, /api/v1/contact, /api/v1/chatroom, /api/v1/session) plus the SSE endpoint for MCP.
- Core Business Logic: Organized under internal/chatlog with supporting packages such as configuration (internal/chatlog/conf), context management (internal/chatlog/ctx), and application-level orchestration (app.go, manager.go).
- Decryption & Key Extraction Modules: Encapsulated under internal/wechat (decrypt, key, process, manager) that handle reading memory/processes and decrypting WeChat data.
- Data Access Layer: Constructed through internal/wechatdb (datasource and repository subpackages) which manage reading from the local WeChat database files.
- MCP Protocol Integration: Under internal/mcp and parts of internal/chatlog/mcp that expose JSON-RPC, SSE, and other communication methods needed for interoperability with AI tools.
- Utility and Support Libraries: Provided in pkg (appver, config, util such as for compression/zstd, dat2img, OS helpers) to support common operations across modules.
- Error Handling: Modules under internal/errors coordinate robust error propagation across HTTP, OS, wechat, and MCP handling.

##### Mapping Relationships and Interactions
- The main executable (cmd/chatlog) initiates the system and routes to either CLI/Terminal UI or HTTP API interface.
- For CLI/Terminal UI commands (e.g., key extraction, decrypt) the user input flows into the core logic modules which then call on the decryption (internal/wechat/decrypt and key) and database reading (internal/wechatdb) services.
- When running as an HTTP service, incoming REST API requests are handled by the HTTP routing component (internal/chatlog/http/route.go) that delegates to service modules; these then query the local database repositories or perform decryption if needed.
- The MCP component listens for SSE connections (via internal/mcp/sse.go) and coordinates responses using JSON-RPC style endpoints.
- Utility packages in pkg support these functionalities by providing versioning, standardized configuration loading, and OS-specific operations.

##### Architectural Patterns and Principles
- Modular/Layered Design: Separate packages (cmd for CLI, internal for business logic, pkg for utilities) clearly separate concerns.
- Clear Separation between Presentation (UI/HTTP), Business Logic, and Data Access.
- Use of configuration and context packages for dependency injection and service orchestration.
- Consistent error handling across different components using centralized internal/errors modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top-Level User Input
    UserInput["User Input"]:::presentation

    %% CLI/Terminal UI Subgraph
    subgraph "CLI/Terminal UI"
        CLIExec["Command-Line Executable"]:::presentation
        Footer["Footer Component"]:::presentation
        Help["Help Component"]:::presentation
        Infobar["Infobar Component"]:::presentation
        Menu["Menu Component"]:::presentation
        Style["Style Component"]:::presentation
    end

    %% HTTP API Service Subgraph
    subgraph "HTTP API Service"
        HTTP_Router["HTTP API Router"]:::presentation
        HTTP_Static["Static Assets"]:::presentation
    end

    %% Core Business Logic Subgraph
    subgraph "Core Business Logic"
        subgraph "Application Orchestration"
            AppOrch["App Orchestration (app.go)"]:::business
            Manager["Manager (manager.go)"]:::business
        end
        subgraph "Configuration & Context"
            Config["Configuration"]:::business
            Context["Context"]:::business
        end
        DatabaseService["Database Service"]:::business
    end

    %% WeChat Decryption & Key Extraction Subgraph
    subgraph "WeChat Decryption & Key Extraction"
        Decryption["Decryption Service"]:::decryption
        KeyEx["Key Extraction Service"]:::decryption
        ProcessHandling["Process Handling"]:::decryption
        WeChatManager["WeChat Manager"]:::decryption
    end

    %% Data Access Layer Subgraph
    subgraph "Data Access Layer"
        Datasource["Datasource"]:::database
        Repository["Repository"]:::database
    end

    %% MCP Integration Subgraph
    subgraph "MCP Integration"
        MCPServices["MCP Services"]:::mcp
        ChatlogMCP["Chatlog MCP Integration"]:::mcp
    end

    %% Utility Libraries Subgraph
    subgraph "Utility Libraries"
        Utility["Utility Libraries"]:::utility
    end

    %% Error Handling Subgraph
    subgraph "Error Handling"
        ErrorHandling["Error Handling Module"]:::error
    end

    %% Connections from User Input to Presentation Layers
    UserInput -->|"TerminalInput"| CLIExec
    UserInput -->|"APIRequest"| HTTP_Router

    %% Presentation Layers to Core Business Logic
    CLIExec -->|"InitiateCommand"| AppOrch
    HTTP_Router -->|"RouteAPI"| AppOrch

    %% Within Core Business Logic
    AppOrch -->|"DelegatesTo"| Manager
    AppOrch -->|"LoadsConfig"| Config
    AppOrch -->|"LoadsContext"| Context
    Manager -->|"UsesDBService"| DatabaseService
    Manager -->|"CallsDecrypt"| Decryption
    Manager -->|"MCPCall"| MCPServices
    Manager -->|"PropagatesError"| ErrorHandling

    %% Core Business Logic to Data Access Layer
    DatabaseService -->|"Queries"| Datasource
    DatabaseService -->|"Queries"| Repository

    %% Connections within WeChat Decryption & Key Extraction
    Decryption -->|"UsesKeyExtraction"| KeyEx
    Decryption -->|"ManagesProcess"| ProcessHandling
    Decryption -->|"Coordinates"| WeChatManager

    %% HTTP API additional connection for Static Assets
    HTTP_Router -->|"ServesStatic"| HTTP_Static

    %% MCP Integration internal connection
    MCPServices ---|"SyncsWith"| ChatlogMCP

    %% Utility Libraries support arrows
    Utility -->|"ProvidesUtilities"| AppOrch
    Utility -->|"ProvidesUtilities"| Decryption
    Utility -->|"ProvidesUtilities"| HTTP_Router

    %% Additional error handling links
    HTTP_Router -->|"HandlesErrors"| ErrorHandling
    Decryption -->|"ErrorReporting"| ErrorHandling

    %% Class Definitions for Colors
    classDef presentation fill:#B3D9FF,stroke:#333,stroke-width:2px;
    classDef business fill:#C1F0C1,stroke:#333,stroke-width:2px;
    classDef decryption fill:#FFD1A4,stroke:#333,stroke-width:2px;
    classDef database fill:#FFECB3,stroke:#333,stroke-width:2px;
    classDef mcp fill:#FFB3DE,stroke:#333,stroke-width:2px;
    classDef utility fill:#D9B3FF,stroke:#333,stroke-width:2px;
    classDef error fill:#F2A0A0,stroke:#333,stroke-width:2px;

    %% Click Events for CLI & Terminal UI
    click CLIExec "https://github.com/sjzar/chatlog/tree/main/cmd/chatlog"
    click Footer "https://github.com/sjzar/chatlog/tree/main/internal/ui/footer"
    click Help "https://github.com/sjzar/chatlog/tree/main/internal/ui/help"
    click Infobar "https://github.com/sjzar/chatlog/tree/main/internal/ui/infobar"
    click Menu "https://github.com/sjzar/chatlog/tree/main/internal/ui/menu"
    click Style "https://github.com/sjzar/chatlog/tree/main/internal/ui/style"

    %% Click Events for HTTP API Service
    click HTTP_Router "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/http"
    click HTTP_Static "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/http/static"

    %% Click Events for Core Business Logic
    click AppOrch "https://github.com/sjzar/chatlog/blob/main/internal/chatlog/app.go"
    click Manager "https://github.com/sjzar/chatlog/blob/main/internal/chatlog/manager.go"
    click Config "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/conf"
    click Context "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/ctx"
    click DatabaseService "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/database"

    %% Click Events for WeChat Decryption & Key Extraction
    click Decryption "https://github.com/sjzar/chatlog/tree/main/internal/wechat/decrypt"
    click KeyEx "https://github.com/sjzar/chatlog/tree/main/internal/wechat/key"
    click ProcessHandling "https://github.com/sjzar/chatlog/tree/main/internal/wechat/process"
    click WeChatManager "https://github.com/sjzar/chatlog/blob/main/internal/wechat/manager.go"

    %% Click Events for Data Access Layer
    click Datasource "https://github.com/sjzar/chatlog/tree/main/internal/wechatdb/datasource"
    click Repository "https://github.com/sjzar/chatlog/tree/main/internal/wechatdb/repository"

    %% Click Events for MCP Integration
    click MCPServices "https://github.com/sjzar/chatlog/tree/main/internal/mcp"
    click ChatlogMCP "https://github.com/sjzar/chatlog/tree/main/internal/chatlog/mcp"

    %% Click Event for Utility Libraries
    click Utility "https://github.com/sjzar/chatlog/tree/main/pkg"

    %% Click Event for Error Handling
    click ErrorHandling "https://github.com/sjzar/chatlog/tree/main/internal/errors"
```
