# midorin-linux/opencoder architecture

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

- Interactive diagram: https://gitdiagram.com/midorin-linux/opencoder
- Repository: https://github.com/midorin-linux/opencoder

## Overview

##### Project type and purpose
- This is a Rust-based command-line “coding agent” framework (“OpenCoder”) that speaks the OpenAI V1 chat API.
- It provides a CLI interface, parses commands, runs them through an application runner, dispatches to domain logic (chat sessions, messages, models), persists state, and calls out to an LLM service.

##### File structure
- Top-level: .env, Cargo.toml, README.md
- src/main.rs: entry point
- src/cli: handles user input, prompts, output
- src/app: core application runner and configuration
- src/commands: command parser, registry, and handler modules (exit, help, models, set, etc.)
- src/domain: business logic layer
- chat (conversation, message, session)
- model (provider, model_info)
- src/infrastructure: external integrations
- lm (client abstraction over HTTP calls to API_URL)
- storage (history_store, session_store)
- src/utils: logging helpers

##### README for additional insights
- Configured via .env: API_URL, API_KEY, RUST_LOG, TIMEOUT_SECS
- Described as an “OpenAI-API-compatible coding agent”

##### Detailed capturing of essential elements
- Don’t lump everything into “backend”; separate the command parsing, runners, domain services, and each store or client
- Trait definitions in domain (model/provider) and their concrete bindings in infrastructure
- Configuration as a shared module influencing App Runner and infra clients

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI Layer
    subgraph "CLI Layer"
        direction TB
        CLIMod["CLI Module\n(src/cli/mod.rs)"]:::cli
        Prompt["Prompt\n(src/cli/prompt.rs)"]:::cli
        Output["Output\n(src/cli/output.rs)"]:::cli
    end

    %% Commands Layer
    subgraph "Commands Layer"
        direction TB
        Parser["Command Parser\n(src/commands/parser.rs)"]:::cli
        Registry["Command Registry\n(src/commands/registry.rs)"]:::cli
        subgraph "Command Handlers"
            direction TB
            HandlersMod["Handlers Module\n(src/commands/handlers/mod.rs)"]:::cli
            ExitHandler["Exit Handler\n(src/commands/handlers/exit.rs)"]:::cli
            HelpHandler["Help Handler\n(src/commands/handlers/help.rs)"]:::cli
            ModelsHandler["Models Handler\n(src/commands/handlers/models.rs)"]:::cli
            SetHandler["Set Handler\n(src/commands/handlers/set.rs)"]:::cli
        end
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        ConfigLoader["Config Loader\n(src/app/config.rs)"]:::app
        Runner["App Runner\n(src/app/runner.rs)"]:::app
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        subgraph "Chat Domain"
            direction TB
            ChatMod["Chat Module\n(src/domain/chat/mod.rs)"]:::domain
            SessionMgr["Session Manager\n(src/domain/chat/session.rs)"]:::domain
            Conversation["Conversation\n(src/domain/chat/conversation.rs)"]:::domain
            Message["Message\n(src/domain/chat/message.rs)"]:::domain
        end
        subgraph "Model Domain"
            direction TB
            ModelMod["Model Module\n(src/domain/model/mod.rs)"]:::domain
            ModelProvider["ModelProvider Trait\n(src/domain/model/provider.rs)"]:::domain
            ModelInfo["ModelInfo\n(src/domain/model/model_info.rs)"]:::domain
        end
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        subgraph "LLM Integration"
            direction TB
            LMMod["LM Module\n(src/infrastructure/lm/mod.rs)"]:::infra
            LLMClient["LLM HTTP Client\n(src/infrastructure/lm/client.rs)"]:::infra
            LLMError["LM Error\n(src/infrastructure/lm/error.rs)"]:::infra
        end
        subgraph "Storage Adapters"
            direction TB
            StorageMod["Storage Module\n(src/infrastructure/storage/mod.rs)"]:::infra
            HistoryStore["History Store\n(src/infrastructure/storage/history_store.rs)"]:::infra
            SessionStore["Session Store\n(src/infrastructure/storage/session_store.rs)"]:::infra
        end
    end

    %% Utils and Entrypoint
    Entrypoint["Main Entry (main.rs)"]:::app
    subgraph "Utilities"
        direction TB
        UtilsMod["Utils Module\n(src/utils/mod.rs)"]:::infra
        Logging["Logging Helpers\n(src/utils/logging.rs)"]:::infra
    end

    %% External Systems
    ExternalAPI["OpenAI-compatible API"]:::external
    FileSystem["File System / DB"]:::external

    %% Flow
    Entrypoint --> CLIMod
    CLIMod --> Prompt
    Prompt --> Parser
    Parser --> Registry
    Registry --> HandlersMod
    HandlersMod --> ExitHandler
    HandlersMod --> HelpHandler
    HandlersMod --> ModelsHandler
    HandlersMod --> SetHandler
    ExitHandler --> Runner
    HelpHandler --> Runner
    ModelsHandler --> Runner
    SetHandler --> Runner

    Runner --> ConfigLoader
    Runner --> SessionMgr
    Runner --> ModelProvider
    Runner --> Logging

    SessionMgr --> HistoryStore
    SessionMgr --> SessionStore

    ModelProvider -.-> LLMClient
    LLMClient --> ExternalAPI
    LLMClient --> Logging

    HistoryStore --> FileSystem
    SessionStore --> FileSystem

    ConfigLoader --> Logging
    Entrypoint --> ConfigLoader

    %% Click Events
    click CLIMod "https://github.com/midorin-linux/opencoder/blob/master/src/cli/mod.rs"
    click Prompt "https://github.com/midorin-linux/opencoder/blob/master/src/cli/prompt.rs"
    click Output "https://github.com/midorin-linux/opencoder/blob/master/src/cli/output.rs"
    click Parser "https://github.com/midorin-linux/opencoder/blob/master/src/commands/parser.rs"
    click Registry "https://github.com/midorin-linux/opencoder/blob/master/src/commands/registry.rs"
    click HandlersMod "https://github.com/midorin-linux/opencoder/blob/master/src/commands/handlers/mod.rs"
    click ExitHandler "https://github.com/midorin-linux/opencoder/blob/master/src/commands/handlers/exit.rs"
    click HelpHandler "https://github.com/midorin-linux/opencoder/blob/master/src/commands/handlers/help.rs"
    click ModelsHandler "https://github.com/midorin-linux/opencoder/blob/master/src/commands/handlers/models.rs"
    click SetHandler "https://github.com/midorin-linux/opencoder/blob/master/src/commands/handlers/set.rs"
    click Runner "https://github.com/midorin-linux/opencoder/blob/master/src/app/runner.rs"
    click ConfigLoader "https://github.com/midorin-linux/opencoder/blob/master/src/app/config.rs"
    click Entrypoint "https://github.com/midorin-linux/opencoder/blob/master/src/main.rs"
    click ChatMod "https://github.com/midorin-linux/opencoder/blob/master/src/domain/chat/mod.rs"
    click SessionMgr "https://github.com/midorin-linux/opencoder/blob/master/src/domain/chat/session.rs"
    click Conversation "https://github.com/midorin-linux/opencoder/blob/master/src/domain/chat/conversation.rs"
    click Message "https://github.com/midorin-linux/opencoder/blob/master/src/domain/chat/message.rs"
    click ModelProvider "https://github.com/midorin-linux/opencoder/blob/master/src/domain/model/provider.rs"
    click ModelInfo "https://github.com/midorin-linux/opencoder/blob/master/src/domain/model/model_info.rs"
    click ModelMod "https://github.com/midorin-linux/opencoder/blob/master/src/domain/model/mod.rs"
    click LMMod "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/lm/mod.rs"
    click LLMClient "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/lm/client.rs"
    click LLMError "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/lm/error.rs"
    click StorageMod "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/storage/mod.rs"
    click HistoryStore "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/storage/history_store.rs"
    click SessionStore "https://github.com/midorin-linux/opencoder/blob/master/src/infrastructure/storage/session_store.rs"
    click UtilsMod "https://github.com/midorin-linux/opencoder/blob/master/src/utils/mod.rs"
    click Logging "https://github.com/midorin-linux/opencoder/blob/master/src/utils/logging.rs"

    %% Styles
    classDef cli fill:#B3D4FC,stroke:#0366d6,color:#000
    classDef app fill:#C6F6D5,stroke:#2F855A,color:#000
    classDef domain fill:#DFFCF0,stroke:#2C7A7B,color:#000
    classDef infra fill:#FCEBCF,stroke:#DD6B20,color:#000
    classDef external fill:#E2E8F0,stroke:#718096,color:#000
```
