# davidlms/aphra architecture

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

- Interactive diagram: https://gitdiagram.com/davidlms/aphra
- Repository: https://github.com/davidlms/aphra

## Overview

##### Main Components

a. Core Components:
- Input/Output Interface (CLI and Python API)
- Configuration Manager (handling config.toml)
- LLM Client (llm_client.py)
- Translation Orchestrator (translate.py)
- Prompt Manager (prompts.py and prompts directory)
- Parser System (parsers.py)

b. External Components:
- OpenRouter API
- LLM Models (Writer, Searcher, Critic)

##### Component Relationships

Top Layer:
- Interface Layer: Show both CLI (aphra_runner.py) and Python API entry points
- Configuration Layer: Display config.toml integration

Middle Layer:
- Core Processing Layer: Show the main translation orchestrator
- LLM Client Layer: Demonstrate API communication
- Prompt Management Layer: Show prompt template system

Bottom Layer:
- External Services Layer: Display OpenRouter and LLM models

##### Specific Elements

- Input handlers for both file-based and direct text input
- Configuration flow showing how API keys and model settings are managed
- Clear separation between the different LLM roles (Writer, Searcher, Critic)
- Parallel processing paths for search and initial translation
- Error handling and feedback loops
- Output formatting and file writing capabilities

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Interface
        CLI["CLI Interface"]:::interface
        API["Python API"]:::interface
        WEB["Web Interface"]:::interface
    end

    subgraph Configuration
        CONFIG["Configuration Manager"]:::config
        PROMPTS["Prompt Manager"]:::config
        PARSER["Parser System"]:::config
    end

    subgraph Core
        TRANS["Translation Orchestrator"]:::core
        LLM_CLIENT["LLM Client"]:::core
    end

    subgraph "LLM Processing Stages"
        ANALYSIS["1. Analysis (Writer)"]:::stage
        SEARCH["2. Search (Searcher)"]:::stage
        INITIAL["3. Initial Translation (Writer)"]:::stage
        CRITIQUE["4. Critique (Critic)"]:::stage
        FINAL["5. Final Translation (Writer)"]:::stage
    end

    subgraph "External Services"
        OPENROUTER["OpenRouter API"]:::external
        LLM["LLM Models"]:::external
    end

    CLI --> CONFIG
    API --> CONFIG
    WEB --> CONFIG
    
    CONFIG --> TRANS
    PROMPTS --> TRANS
    PARSER --> TRANS
    
    TRANS --> LLM_CLIENT
    LLM_CLIENT --> OPENROUTER
    OPENROUTER --> LLM
    
    TRANS --> ANALYSIS
    ANALYSIS --> SEARCH
    SEARCH --> INITIAL
    INITIAL --> CRITIQUE
    CRITIQUE --> FINAL
    
    %% Component Mappings
    click CLI "https://github.com/DavidLMS/aphra/blob/main/aphra_runner.py"
    click CONFIG "https://github.com/DavidLMS/aphra/blob/main/config.example.toml"
    click LLM_CLIENT "https://github.com/DavidLMS/aphra/blob/main/aphra/llm_client.py"
    click TRANS "https://github.com/DavidLMS/aphra/blob/main/aphra/translate.py"
    click PROMPTS "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts.py"
    click PARSER "https://github.com/DavidLMS/aphra/blob/main/aphra/parsers.py"
    click WEB "https://github.com/DavidLMS/aphra/blob/main/gradio-demo.py"
    
    %% Stage Prompt Mappings
    click ANALYSIS "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts/step1_system.txt"
    click SEARCH "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts/step2_system.txt"
    click INITIAL "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts/step3_system.txt"
    click CRITIQUE "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts/step4_system.txt"
    click FINAL "https://github.com/DavidLMS/aphra/blob/main/aphra/prompts/step5_system.txt"

    classDef interface fill:#gray,stroke:#333,stroke-width:2px;
    classDef config fill:#purple,stroke:#333,stroke-width:2px;
    classDef core fill:#blue,stroke:#333,stroke-width:2px;
    classDef stage fill:#orange,stroke:#333,stroke-width:2px;
    classDef external fill:#green,stroke:#333,stroke-width:2px;

    %% Legend
    subgraph Legend
        L1["Interface Components"]:::interface
        L2["Configuration Components"]:::config
        L3["Core Components"]:::core
        L4["Processing Stages"]:::stage
        L5["External Services"]:::external
    end
```
