# kharvd/gpt-cli architecture

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

- Interactive diagram: https://gitdiagram.com/kharvd/gpt-cli
- Repository: https://github.com/kharvd/gpt-cli

## Overview

##### Main Components

a) Core Components:
- CLI Interface Layer
- Provider Integration Layer
- Session Management
- Configuration Management
- Logging System

b) External Services:
- OpenAI API
- Anthropic API
- Google Gemini API
- Cohere API
- Custom OpenAI-compatible APIs

**Top Layer: User Interface**
**Middle Layer: Core Logic**
**Bottom Layer: Provider Integration**
**External Layer: API Services**

##### Component Relationships

a) User Interface Components:
- cli.py → Main entry point
- shell.py → Interactive shell handling
- completion.py → Command completion

b) Core Logic:
- session.py → Chat session management
- config.py → Configuration handling
- assistant.py → Assistant behavior
- cost.py → Usage tracking
- logging_utils.py → Logging functionality

c) Provider Integration:
- providers/* → Individual provider implementations
- gpt.py → Common interface for providers

##### Specific Implementation Details

a) Data Flow:
- User input → CLI → Session Manager → Provider → External API
- Configuration file (gpt.yml) → Config Manager → Component Configuration
- Usage data → Cost Tracker → Logging System

b) Key Interfaces:
- Provider abstract interface in providers/__init__.py
- Individual provider implementations
- Session management interface

##### Below it, create a layer for core components
- Session Manager (session.py)
- Assistant Manager (assistant.py)
- Configuration Manager (config.py)
- Abstract Provider Interface
- Individual Provider Implementations
##### At the bottom, show external API services

Important Architectural Patterns to Highlight:
- Provider Pattern: Show how different LLM providers are abstracted
- Command Pattern: CLI command processing
- Strategy Pattern: Different assistant configurations
- Factory Pattern: Provider creation

- Component types
- Configuration flow

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef ui fill:#2196F3,stroke:#1565C0,color:white
    classDef core fill:#4CAF50,stroke:#2E7D32,color:white
    classDef provider fill:#FF9800,stroke:#EF6C00,color:white
    classDef external fill:#9E9E9E,stroke:#616161,color:white
    classDef config fill:#FFEB3B,stroke:#FBC02D,color:black

    %% UI Layer
    subgraph UI
        CLI["CLI Interface"]
        Shell["Interactive Shell"]
        Completion["Command Completion"]
    end

    %% Core Layer
    subgraph Core
        Session["Session Manager"]
        Config["Configuration Manager"]
        Assistant["Assistant Manager"]
        Cost["Cost Tracker"]
        Logging["Logging System"]
        GPTInterface["Core GPT Interface"]
    end

    %% Provider Layer
    subgraph Providers
        ProviderInterface["Provider Interface"]
        OpenAI["OpenAI Provider"]
        Anthropic["Anthropic Provider"]
        Google["Google Provider"]
        Cohere["Cohere Provider"]
        Azure["Azure OpenAI Provider"]
        Llama["Llama Provider"]
    end

    %% External Services
    subgraph External
        OpenAIAPI["OpenAI API"]
        AnthropicAPI["Anthropic API"]
        GoogleAPI["Google Gemini API"]
        CohereAPI["Cohere API"]
        AzureAPI["Azure OpenAI API"]
        LlamaAPI["Llama API"]
    end

    %% Relationships
    CLI --> Shell
    CLI --> Completion
    CLI --> Session
    Shell --> Session
    Session --> GPTInterface
    Config --> Session
    Config --> GPTInterface
    Assistant --> Session
    Cost --> Logging
    GPTInterface --> ProviderInterface
    
    ProviderInterface --> OpenAI
    ProviderInterface --> Anthropic
    ProviderInterface --> Google
    ProviderInterface --> Cohere
    ProviderInterface --> Azure
    ProviderInterface --> Llama
    
    OpenAI --> OpenAIAPI
    Anthropic --> AnthropicAPI
    Google --> GoogleAPI
    Cohere --> CohereAPI
    Azure --> AzureAPI
    Llama --> LlamaAPI

    %% Click Events
    click CLI "https://github.com/kharvd/gpt-cli/blob/main/gptcli/cli.py"
    click Shell "https://github.com/kharvd/gpt-cli/blob/main/gptcli/shell.py"
    click Completion "https://github.com/kharvd/gpt-cli/blob/main/gptcli/completion.py"
    click Session "https://github.com/kharvd/gpt-cli/blob/main/gptcli/session.py"
    click Config "https://github.com/kharvd/gpt-cli/blob/main/gptcli/config.py"
    click Assistant "https://github.com/kharvd/gpt-cli/blob/main/gptcli/assistant.py"
    click Cost "https://github.com/kharvd/gpt-cli/blob/main/gptcli/cost.py"
    click Logging "https://github.com/kharvd/gpt-cli/blob/main/gptcli/logging_utils.py"
    click ProviderInterface "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/__init__.py"
    click OpenAI "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/openai.py"
    click Anthropic "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/anthropic.py"
    click Google "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/google.py"
    click Cohere "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/cohere.py"
    click Azure "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/azure_openai.py"
    click Llama "https://github.com/kharvd/gpt-cli/blob/main/gptcli/providers/llama.py"
    click GPTInterface "https://github.com/kharvd/gpt-cli/blob/main/gptcli/gpt.py"

    %% Style Applications
    class CLI,Shell,Completion ui
    class Session,Config,Assistant,Cost,Logging,GPTInterface core
    class ProviderInterface,OpenAI,Anthropic,Google,Cohere,Azure,Llama provider
    class OpenAIAPI,AnthropicAPI,GoogleAPI,CohereAPI,AzureAPI,LlamaAPI external
```
