# t-majumder/agentgo architecture

> Architecture diagram of the GitHub repository t-majumder/agentgo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/t-majumder/agentgo
- Repository: https://github.com/t-majumder/agentgo

## Overview

This repo is a **single-service Python application** that exposes an **AI agent** via:
- **Web UI + REST API** (Flask in `app.py`, UI in `index.html`)
- **CLI** (`main.py`)

Core purpose: an **LLM-driven tool-using agent**. The LLM (via **Groq API**) decides which **tools** to call (web search, Wikipedia, scraper, calculator, time), the app executes those tool calls, then the LLM synthesizes a final response.

##### File-structure-driven decomposition (map files → components)
Use the file tree as the canonical “module boundary” guide:

- **Entry points / Interfaces**
- `app.py` → Flask server, routes (notably `POST /chat`), serves `index.html`
- `index.html` → Browser UI (calls backend `/chat`)
- `main.py` → CLI loop (calls agent directly; no HTTP)

- **Core orchestration**
- `agent.py` → the “brain”: prompt + tool schemas + iteration loop + tool dispatch
- `tools.py` → tool implementations (wikipedia_search, web_search, web_scraper, calculator, current_time)
- `llm_call.py` → LLM client wrappers for various models (gpt-oss-120b, qwen-32b, etc.), calling Groq

- **Packaging/Runtime**
- `requirements.txt` → deps
- `Dockerfile` → container runtime
- `LICENSE`, `README.md`

This naturally becomes: **Client(s) → Flask/CLI → Agent Orchestrator → (LLM + Tools) → External Services**.

##### Flask App / API Layer (`app.py`)
- Route: `POST /chat`
- Also static serving of `index.html`

##### Agent Orchestrator (`agent.py`)
- Conversation state/messages
- Tool schemas (function-calling spec)
- Iteration loop (max iterations)
- Tool selection + tool execution + final synthesis

##### LLM Adapter (`llm_call.py`)
- Model selection
- Groq API call
- Returns function-call requests or final text

##### Tooling Layer (`tools.py`)
- Individual tool functions
- Any parsing/normalization/summarization done inside tools

##### Flow 1: Web UI → API
- **Browser UI (`index.html`)** → HTTP `POST /chat` → **Flask (`app.py`)**
- Flask passes the message to **Agent Orchestrator (`agent.py`)**
- Agent uses **LLM Adapter (`llm_call.py`)** → **Groq API**
- Groq response may contain:
- **Tool calls** → Agent dispatches to **tools.py** → external services (Wikipedia/DDG/Websites) → returns tool results
- Or a **final answer**
- Agent returns final answer → Flask returns JSON → UI renders response

##### Flow 2: CLI → Agent
- **CLI (`main.py`)** → directly calls **Agent Orchestrator (`agent.py`)**
- Then same loop: Agent ↔ LLM ↔ Tools ↔ External services

##### LLM returns either
- `function_call(s)` with arguments, or
- final answer
##### If tool calls
- execute tool(s) in `tools.py`
- append tool outputs to conversation
- repeat until done or max iterations

##### Architectural patterns/design principles present
Call these out visually (labels/notes) because they guide correct diagramming:

- **Function-calling / Tool-augmented LLM (ReAct-like)**: LLM chooses tools; system executes them; LLM synthesizes.
- **Single-service modular monolith**: everything runs in one Python process; separation is by module, not microservices.
- **Ports & adapters** style (informally):
- Ports: agent interface invoked by Flask/CLI
- Adapters: `llm_call.py` for Groq; `tools.py` for external info sources
- **Pluggable tools**: add function in `tools.py`, add schema + registry mapping in `agent.py`.

##### What to label explicitly (tech + interfaces)

- **Protocols**
- Browser → Flask: HTTP (JSON)
- Flask → Agent: Python function call
- LLM Adapter → Groq: HTTPS API
- Tools → Wikipedia/DDG/Websites: HTTPS requests

- **Key API endpoint**
- `POST /chat` (request `{message}` → response `{response}`)

- **Configuration**
- `GROQ_API_KEY` (env var)
- Optional: `FLASK_DEBUG`, `FLASK_PORT`

- **Runtime options**
- “Run as CLI” vs “Run as Web server”
- “Dockerized deployment” boundary

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% ======================
    %% Clients / UX
    %% ======================
    subgraph "Clients / UX"
        direction TB
        User(("User")):::user

        Browser["Browser UI\n(index.html)"]:::client
        CLI["CLI\n(main.py)"]:::client

        User -->|"typesPrompt"| Browser
        User -->|"typesPrompt"| CLI
    end

    %% ======================
    %% Deployment Wrapper
    %% ======================
    subgraph "Runtime / Deployment"
        direction TB
        Docker["Docker Container\n(Dockerfile)"]:::deploy

        subgraph "Single Python Service (Monolith)"
            direction TB

            Flask["Flask App / REST API\n(app.py)\n- serves index.html\n- POST /chat {message}->{response}"]:::backend

            Agent["Agent Orchestrator\n(agent.py)\n- conversationState\n- toolSchemas\n- maxIterations=15\n- toolDispatch\n- finalSynthesis"]:::backend

            LLM["LLM Adapter\n(llm_call.py)\n- modelSelection\n- Groq API client\n- returns toolCalls or finalText"]:::backend

            subgraph "Tooling Layer (tools.py)"
                direction TB
                Tools["Tools Module\n(tools.py)"]:::backend
                T1["wikipedia_search()"]:::tool
                T2["web_search() (DuckDuckGo)"]:::tool
                T3["web_scraper()"]:::tool
                T4["calculator()"]:::tool
                T5["current_time()"]:::tool

                Tools --> T1
                Tools --> T2
                Tools --> T3
                Tools --> T4
                Tools --> T5
            end
        end

        Requirements["Python Dependencies\n(requirements.txt)"]:::meta
        Docs["Project Docs\n(README.md)"]:::meta
        License["License\n(LICENSE)"]:::meta

        Docker -.->|"packages"| Requirements
        Docker -.->|"docs"| Docs
        Docker -.->|"license"| License
    end

    %% ======================
    %% External Dependencies
    %% ======================
    subgraph "External Systems"
        direction TB
        Groq["Groq LLM Service\n(HTTPS API)\nEnv: GROQ_API_KEY"]:::external
        Wikipedia["Wikipedia\n(HTTPS)"]:::external
        DDG["DuckDuckGo Search\n(HTTPS)"]:::external
        Websites["Target Websites\n(HTTPS scrape targets)"]:::external
        Clock["System Clock\n(local)"]:::externalLocal
    end

    %% ======================
    %% Primary Ingress Flows
    %% ======================
    Browser -->|"HTTP POST /chat (JSON)"| Flask
    Flask -.->|"JSON {response}"| Browser

    CLI -->|"pythonCall: runAgent(prompt)"| Agent
    Flask -->|"pythonCall: handleChat(message)"| Agent

    %% ======================
    %% Agent Tool-Use Loop (explicit)
    %% ======================
    Agent -->|"1 sendMessages+toolSchemas"| LLM
    LLM -->|"2a toolCalls(args)"| Agent
    LLM -->|"2b finalAnswer"| Agent

    Agent -->|"3 executeToolCalls"| Tools
    Tools -->|"4 toolResult(s)"| Agent
    Agent -->|"5 repeatUntilDone/maxIterations"| LLM

    Agent -->|"returnFinalResponse"| Flask
    Agent -->|"printFinalResponse"| CLI

    %% ======================
    %% External Calls
    %% ======================
    LLM -->|"HTTPS request"| Groq
    Groq -->|"LLM output: toolCalls/finalText"| LLM

    T1 -->|"HTTPS query"| Wikipedia
    Wikipedia -->|"page extracts"| T1

    T2 -->|"HTTPS query"| DDG
    DDG -->|"search results"| T2

    T3 -->|"HTTPS GET"| Websites
    Websites -->|"HTML content"| T3

    T5 -->|"readTime"| Clock
    Clock -->|"timestamp"| T5

    %% ======================
    %% Notes / Annotations
    %% ======================
    NoteLoop["Iterative function-calling loop:\nLLM selects tools -> app executes -> LLM synthesizes\n(ReAct-like; tool-augmented)"]:::note
    NoteLoop -.-> Agent

    NotePorts["Ports & Adapters:\nPorts: Flask/CLI -> Agent\nAdapters: llm_call.py (Groq), tools.py (web sources)"]:::note
    NotePorts -.-> Agent

    %% ======================
    %% Click Events (component mapping)
    %% ======================
    click Browser "https://github.com/t-majumder/agentgo/blob/main/index.html"
    click CLI "https://github.com/t-majumder/agentgo/blob/main/main.py"
    click Flask "https://github.com/t-majumder/agentgo/blob/main/app.py"
    click Agent "https://github.com/t-majumder/agentgo/blob/main/agent.py"
    click LLM "https://github.com/t-majumder/agentgo/blob/main/llm_call.py"
    click Tools "https://github.com/t-majumder/agentgo/blob/main/tools.py"
    click Docker "https://github.com/t-majumder/agentgo/tree/main/Dockerfile"
    click Requirements "https://github.com/t-majumder/agentgo/blob/main/requirements.txt"
    click Docs "https://github.com/t-majumder/agentgo/blob/main/README.md"
    click License "https://github.com/t-majumder/agentgo/tree/main/LICENSE"

    %% ======================
    %% Styles
    %% ======================
    classDef user fill:#ffffff,stroke:#111827,stroke-width:1.5px,color:#111827
    classDef client fill:#dbeafe,stroke:#1d4ed8,stroke-width:1.5px,color:#0f172a
    classDef backend fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#052e16
    classDef tool fill:#bbf7d0,stroke:#15803d,stroke-width:1.2px,color:#052e16
    classDef external fill:#ffedd5,stroke:#f97316,stroke-width:1.5px,color:#7c2d12
    classDef externalLocal fill:#fde68a,stroke:#b45309,stroke-width:1.5px,color:#78350f
    classDef deploy fill:#f3f4f6,stroke:#6b7280,stroke-width:2px,color:#111827
    classDef meta fill:#e5e7eb,stroke:#6b7280,stroke-width:1.2px,color:#111827
    classDef note fill:#fef9c3,stroke:#ca8a04,stroke-width:1.2px,color:#713f12
```
