# joshpxyne/gpt-migrate architecture

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

- Interactive diagram: https://gitdiagram.com/joshpxyne/gpt-migrate
- Repository: https://github.com/joshpxyne/gpt-migrate

## Overview

##### Project Type and Purpose
- Project Type: An open-source code migration utility that uses GPT (LLM) to transform a codebase from a source language/framework to a target one.
- Purpose: To automate the tedious and error-prone process of modernizing or migrating existing codebases, including setting up Docker environments, debugging, and verifying code via unit tests.
- Key indicators include the README’s detailed description of migrating apps (e.g., migrating a Python-based Flask app to Node.js) and the dedicated benchmark directories containing sample repos.

##### Analyzing the File Structure
- The top-level files (LICENSE, README.md, CODE_OF_CONDUCT.md) indicate a well-documented OSS project.
- The “gpt_migrate” directory contains the core modules:
- ai.py – Likely responsible for interfacing with the LLM.
- config.py – Manages configuration parameters.
- main.py – The entry point that parses CLI arguments and kicks off the migration process.
- parser.py, utils.py – Utility functionality.
- “steps” subdirectory – Contains modules like setup.py, migrate.py, debug.py, and test.py corresponding to the different phases of migration.
- “prompts” subdirectory – Organized into categories (HIERARCHY, p1, p2, p3, p4) representing different levels of prompt specificity. This is key to understanding how the tool constructs and composes the GPT prompts during migration.
- “memory/external_dependencies” – Likely manages caching of external dependency information.
- The “benchmarks” directory includes several example repositories (for flask with C++, FastAPI, Node.js, and Rust) that serve as real-world inputs to the migration process. Each example is structured with its own “source” directory containing typical files like app.py, db.py, requirements.txt, and sample JSON data for persistence/storage.

##### System Components and Their Relationships
a. Main Components:
- Command-Line Interface (CLI): The user interacts via CLI with main.py and passes various options (sourcedir, targetlang, port configurations, etc.).
- Migration Engine:
- Core modules in gpt_migrate (ai.py, parser.py, utils.py) that handle communication with the GPT model, prompt construction, and result interpretation.
- “steps” submodules to perform discrete tasks: setup (configuring Docker, preparing environments), migrate (converting code), debug (handling errors and iteratively refining code), and test (running unit tests).
- Prompt Manager: The “prompts” subdirectory holds various prompt templates organized by preference levels (p1, p2, p3, and p4) along with a hierarchy that ensures the most critical prompt aspects are prioritized.
- Docker Environment Manager: GPT-Migrate automatically creates and manages Docker environments designed for the target language/framework, ensures the migrated code is working, and exposes it on designated ports (e.g., targetport 8080).
- Benchmark/Test Harness: The “benchmarks” directory offers sample codebases, illustrating how the migration tool works on actual small-scale REST API apps.

b. Relationships and Data Flow:
- The user initiates the migration process via main.py, specifying source and target directories, languages, ports, and desired test files.
- main.py orchestrates the sequence by calling into modules under “steps” based on the specified step (setup, migrate, test, etc.).
- AI-related modules (located in ai.py and also via prompt_constructor) construct detailed prompts using subprompts from the “prompts” directory, integrating configuration from config.py.
- The migration engine communicates with external LLM APIs (using provided API keys like OPENAI_API_KEY or OPENROUTER_API_KEY).
- The Docker environment is dynamically created to run and test the migrated code; unit tests are generated and executed against this environment.
- Feedback from testing and debugging is used to iteratively refine the code migration.

##### Architectural Patterns and Design Principles
- Modular and Layered Architecture: Separate modules handle distinct phases of migration (e.g., prompting, Docker setup, testing), which aids troubleshooting and extension.
- Command-Driven Design: The main.py CLI encapsulates argument parsing and orchestrates various “steps” of the migration process.
- Prompt Composability: A distinctive feature where prompt templates are combined hierarchically to provide detailed instructions to the LLM.
- Continuous Feedback Loop: The system iterates through debug and test phases, refining the migration iteratively based on error logs and unit test results.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface Layer"
        CLI["CLI Entry (main.py)"]:::ui_class
    end

    subgraph "Migration Layer"
        ENGINE["Migration Engine"]:::engine_class
        PROMPT["Prompt Manager"]:::prompt_class
        subgraph "Steps Processor"
            SETUP["Setup"]:::steps_class
            MIGRATE["Migrate"]:::steps_class
            DEBUG["Debug"]:::steps_class
            TEST["Test"]:::steps_class
        end
        subgraph "Docker Environment Manager"
            DOCKER_CREATE["Create Target Docker"]:::docker_class
            DOCKER_DEBUG["Debug Target Docker"]:::docker_class
        end
        LLM["External LLM API"]:::external_class
        DEP["External Dependencies Management"]:::external_class
    end

    subgraph "Test Source"
        BENCH["Benchmark/Test Harness"]:::benchmark_class
    end

    BENCH -->|"source_input"| CLI
    CLI -->|"initiate_migration"| ENGINE
    ENGINE -->|"construct_prompts"| PROMPT
    PROMPT -->|"send_prompt"| LLM
    ENGINE -->|"execute_setup"| SETUP
    ENGINE -->|"execute_migrate"| MIGRATE
    ENGINE -->|"execute_debug"| DEBUG
    ENGINE -->|"execute_test"| TEST
    SETUP -->|"setup_container"| DOCKER_CREATE
    DEBUG -->|"update_container"| DOCKER_DEBUG
    TEST -->|"feedback_cycle"| ENGINE
    DEBUG -->|"feedback_cycle"| ENGINE
    ENGINE -->|"load_dependencies"| DEP

    click CLI "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/main.py"
    click ENGINE "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/ai.py"
    click ENGINE "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/parser.py"
    click ENGINE "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/utils.py"
    click ENGINE "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/config.py"
    click SETUP "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/steps/setup.py"
    click MIGRATE "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/steps/migrate.py"
    click DEBUG "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/steps/debug.py"
    click TEST "https://github.com/joshpxyne/gpt-migrate/blob/main/gpt_migrate/steps/test.py"
    click PROMPT "https://github.com/joshpxyne/gpt-migrate/tree/main/gpt_migrate/prompts"
    click DOCKER_CREATE "https://github.com/joshpxyne/gpt-migrate/tree/main/gpt_migrate/prompts/p3_setup/create_target_docker"
    click DOCKER_DEBUG "https://github.com/joshpxyne/gpt-migrate/tree/main/gpt_migrate/prompts/p3_debug/debug_target_docker"
    click BENCH "https://github.com/joshpxyne/gpt-migrate/tree/main/benchmarks"
    click DEP "https://github.com/joshpxyne/gpt-migrate/tree/main/gpt_migrate/memory/external_dependencies"

    classDef ui_class fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef engine_class fill:#F9E79F,stroke:#7D6608,stroke-width:2px;
    classDef prompt_class fill:#ABEBC6,stroke:#145A32,stroke-width:2px;
    classDef steps_class fill:#F5CBA7,stroke:#9C640C,stroke-width:2px;
    classDef docker_class fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef external_class fill:#FAD7A0,stroke:#A04000,stroke-width:2px;
    classDef benchmark_class fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
```
