# matthewzmd/emigo architecture

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

- Interactive diagram: https://gitdiagram.com/matthewzmd/emigo
- Repository: https://github.com/matthewzmd/emigo

## Overview

##### Project Type and Purpose
- Emigo is designed to integrate an AI-powered tool directly into Emacs. Its primary function is to let developers leverage large language models (LLMs) to interact with their project context, read/write files, execute commands, and ultimately assist with code development in an "agentic" fashion.
- It achieves this by using an agent (implemented in Python) that orchestrates LLM interactions and acts upon user commands relayed via Emacs.

##### Analyzing the File Structure
- Key Python Files: Files such as agents.py, config.py, llm.py, system_prompt.py, repomapper.py, and utils.py collectively comprise the core backend logic. They handle tasks like managing LLM interactions, executing and approving tool actions, and processing commands.
- Emacs Lisp Files: Files like emigo.el and emigo-epc.el are the front-end integration components that allow Emacs to communicate with the Python backend, start sessions, and manage the context.
- Queries Directory: The “queries” folder (including its tree-sitter-language-pack and tree-sitter-languages subdirectories) indicates that the project supports language-specific parsing or tagging — likely used to assist in accurate code reading or refactoring by the LLM.
- Additional Files: Standard files like requirements.txt and README.md support dependency management and documentation, while todo.md suggests ongoing feature development.

##### Architectural Components and Their Relationships
- Emacs Frontend (User Interface):
- Emigo.el & Emigo-epc.el: These files integrate with Emacs and provide the interface for end-users to invoke commands (`M-x emigo`), interact with prompts, and approve tool actions.
- Python Agent Backend:
- Core Modules: The Python files (agents.py, config.py, llm.py, system_prompt.py, repomapper.py, and utils.py) form the brain of the system. They handle session management, the iterative LLM tool-use cycle, and file system interactions.
- File Interaction: The agent monitors and manipulates files (e.g., reading, refactoring, executing commands). Mention in Emacs with an “@” symbol triggers addition of files to the context by the backend.
- Language Query Support:
- The queries directory holds language-specific tree-sitter configurations that likely assist in understanding or tagging code in various languages. This module informs the agent with extra context regarding the specific codebase language structure.
- External Services:
- LLM Providers: External LLM services (integrated via the LiteLLM library) are used for generating responses and decision making within the agent.
- Emacs and System Integration: The backend may use system commands or file system calls based on the agents’ actions.

##### Architectural Patterns and Design Principles
- Layered Architecture: The system clearly separates the presentation layer (Emacs Lisp integration) from the business logic layer (Python agent) and further abstracts external dependencies (LLM services, language parsing).
- Tool-Use Pattern: The agent decouples LLM-generated suggestions from actual tool execution. The iterative cycle is central: the LLM analyzes user requests, picks a tool (such as read, write, or execute commands), and the result is fed back as context.
- Extensibility & Configurability: From the tree-sitter queries to dynamic LLM support via LiteLLM, the system is designed to be flexible and accommodate various coding languages and LLM providers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Emacs Frontend
    subgraph "Emacs Frontend"
        UI1["emigo.el"]:::ui
        UI2["emigo-epc.el"]:::ui
    end

    %% Python Agent Core
    subgraph "Python Agent Core"
        P1["agents.py"]:::backend
        P2["config.py"]:::backend
        P3["llm.py"]:::backend
        P4["system_prompt.py"]:::backend
        P5["repomapper.py"]:::backend
        P6["utils.py"]:::backend
    end

    %% Language Query System
    subgraph "Language Query System"
        Q1["queries"]:::language
        Q2["tree-sitter-language-pack"]:::language
        Q3["tree-sitter-languages"]:::language
        Q1 -->|"contains"| Q2
        Q1 -->|"contains"| Q3
    end

    %% External Services
    E1["LLM Service"]:::external
    E2["File System/External Commands"]:::external

    %% Relationships
    UI1 -->|"UserCommands&ContextRequests"| P1
    UI2 -->|"UserCommands&ContextRequests"| P1
    P3 -->|"RequestLLMDecisions"| E1
    P1 -->|"FetchContext"| Q1
    P1 -->|"ExecuteCommands"| E2
    P1 -->|"ReturnResults"| UI1

    %% Styles
    classDef ui fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef backend fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef language fill:#ffff99,stroke:#333,stroke-width:2px;
    classDef external fill:#ffcc99,stroke:#333,stroke-width:2px;

    %% Click Events for Emacs Frontend components
    click UI1 "https://github.com/matthewzmd/emigo/blob/main/emigo.el"
    click UI2 "https://github.com/matthewzmd/emigo/blob/main/emigo-epc.el"

    %% Click Events for Python Agent Core components
    click P1 "https://github.com/matthewzmd/emigo/blob/main/agents.py"
    click P2 "https://github.com/matthewzmd/emigo/blob/main/config.py"
    click P3 "https://github.com/matthewzmd/emigo/blob/main/llm.py"
    click P4 "https://github.com/matthewzmd/emigo/blob/main/system_prompt.py"
    click P5 "https://github.com/matthewzmd/emigo/blob/main/repomapper.py"
    click P6 "https://github.com/matthewzmd/emigo/blob/main/utils.py"

    %% Click Events for Language Query System components
    click Q1 "https://github.com/matthewzmd/emigo/tree/main/queries/"
    click Q2 "https://github.com/matthewzmd/emigo/tree/main/queries/tree-sitter-language-pack/"
    click Q3 "https://github.com/matthewzmd/emigo/tree/main/queries/tree-sitter-languages/"
```
