# kmesiab/cadre architecture

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

- Interactive diagram: https://gitdiagram.com/kmesiab/cadre
- Repository: https://github.com/kmesiab/cadre

## Overview

##### Project type and purpose
- This is a language-agnostic, AI-powered CLI tool (written in Go) named “Cadre CLI” that automates code reviews of GitHub pull requests by fetching diffs, sending them to OpenAI’s ChatGPT API, and rendering the result as Markdown.

##### File structure
- Top-level Go files:
- main.go / main_test.go: application entrypoint and unit tests
- github_diff_client.go / github_diff_client_mock.go: abstraction & mock for GitHub API
- openai.go: wrapper for OpenAI ChatGPT API calls
- prompt.go: logic to build prompts for ChatGPT
- Makefile: build, test, lint targets
- go.mod / go.sum: dependency management
- assets/: screenshots, documentation only
- .github/workflows/: CI pipelines (build, lint, test)

##### README for additional insights
- Describes CLI switches (--url, --key, --model)
- Explains review categories (Technical Accuracy, Best Practices, etc.)
- Shows that the core workflow is: fetch diff → build prompt → call ChatGPT → parse response → output Markdown report.

##### Main components & interactions
a. Components:
1. CLI Frontend (main.go)
2. GitHub Diff Client
3. Prompt Builder
4. OpenAI Client
5. Markdown Renderer / Output Writer
6. External Services:
– GitHub REST API
– OpenAI ChatGPT API
b. Relationships / Data flow:
1. User invokes `cadre --url=… --key=…` → CLI Frontend
2. CLI calls GitHub Diff Client to fetch pull-request diffs from GitHub REST API
3. Diffs passed into Prompt Builder to assemble language-specific review prompts
4. Prompt sent to OpenAI Client → communicates with OpenAI ChatGPT API → receives review text
5. CLI aggregates responses, passes to Markdown Renderer
6. Markdown report printed to stdout or file
c. Architectural patterns:
- Modular layers (ports & adapters):
- Adapters: GitHubDiffClient, OpenAIClient
- Core business logic: Prompt Builder, Output Renderer
- Dependency injection for testability (github_diff_client_mock.go)
d. Key technologies:
- Go modules, standard library (net/http), CLI flags parsing
- OpenAI API (gpt-3.5-turbo, gpt-4)
- GitHub REST API v3

##### Additional details to capture
- The environment/configuration node (env var OPENAI_API_KEY, CADRE_COMPLETION_MODEL) feeding into CLI Frontend
- Interfaces vs implementations: mark the GitHub client interface and the concrete/mock implementations

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Environment Configuration
    ENV["Environment Variables\n(OPENAI_API_KEY, CADRE_MODEL)"]:::data

    %% Main CLI Flow
    ENV -->|"configuration"| CLI["CLI Frontend"]:::internal
    click CLI "https://github.com/kmesiab/cadre/blob/main/main.go"
    click CLI "https://github.com/kmesiab/cadre/blob/main/main_test.go"

    CLI -->|"invoke FetchDiff()"| GHCInterface["GitHubDiffClient Interface"]:::internal
    GHCInterface -->|"concrete impl"| GHCImpl["GitHub Diff Client"]:::internal
    GHCInterface -->|"mock impl"| GHCMock["GitHub Diff Client Mock"]:::internal
    click GHCImpl "https://github.com/kmesiab/cadre/blob/main/github_diff_client.go"
    click GHCMock "https://github.com/kmesiab/cadre/blob/main/github_diff_client_mock.go"

    GHCImpl -->|"GET /repos/.../pulls/.../diffs\n(Diff JSON)"| GHAPI["GitHub REST API v3"]:::external
    GHAPI -->|"Diff JSON"| GHCImpl
    GHCImpl -->|"Diff JSON"| CLI

    CLI -->|"Diff JSON"| Prompt["Prompt Builder"]:::internal
    click Prompt "https://github.com/kmesiab/cadre/blob/main/prompt.go"
    Prompt -->|"ChatGPT Prompt"| OpenAIClient["OpenAI Client"]:::internal
    click OpenAIClient "https://github.com/kmesiab/cadre/blob/main/openai.go"

    OpenAIClient -->|"POST /v1/chat/completions\n(Request Payload)"| OAAPI["OpenAI ChatGPT API"]:::external
    OAAPI -->|"Response (Review Text)"| OpenAIClient
    OpenAIClient -->|"Review Text"| CLI

    CLI -->|"Review Text"| Renderer["Markdown Renderer / Output Writer"]:::internal
    click Renderer "https://github.com/kmesiab/cadre/blob/main/main.go"
    Renderer -->|"Markdown Report"| STDOUT["stdout or file"]:::external

    %% CI Workflows
    subgraph "CI Workflows"
        direction TB
        Build["go-build.yml"]:::external
        Lint["go-lint.yml"]:::external
        Test["go-test.yml"]:::external
        click Build "https://github.com/kmesiab/cadre/blob/main/.github/workflows/go-build.yml"
        click Lint "https://github.com/kmesiab/cadre/blob/main/.github/workflows/go-lint.yml"
        click Test "https://github.com/kmesiab/cadre/blob/main/.github/workflows/go-test.yml"
    end

    %% Styles
    classDef internal fill:#AED6F1,stroke:#1B4F72,color:#0B2348
    classDef external fill:#ABEBC6,stroke:#145A32,color:#0B5345
    classDef data fill:#F9E79F,stroke:#B7950B,color:#7D6608
```
