# mathewgeejo/cartgenie architecture

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

- Interactive diagram: https://gitdiagram.com/mathewgeejo/cartgenie
- Repository: https://github.com/mathewgeejo/cartgenie

## Overview

1. **Overall shape**
   - This is a split frontend/backend system centered on an autonomous shopping workflow. The Python backend owns all orchestration, retrieval, memory, and tool execution; the Next.js frontend is a thin interactive client.

2. **Frontend boundary**
   - `frontend/` is a Next.js 15 app with a chat-centric UI and recommendation surfaces (`chat-ui`, `deal-banner`, `product-card`, `price-sparkline`).
   - It talks to the backend through `frontend/lib/api.ts` and renders streamed responses rather than waiting for a single synchronous completion.

3. **API gateway**
   - `api/main.py` is the FastAPI entrypoint. It exposes the public contract: JWT auth, `/chat` streaming, product/deal/history/alert endpoints, WebSocket streaming, and `/metrics`.
   - This layer is the security and transport boundary: request auth, rate limiting, streaming protocol handling, and response shaping happen here, not in the agent code.

4. **Core orchestration**
   - `graphs/shopping_graph.py` is the main decision engine. It uses LangGraph-style orchestration with conditional routing, subgraph fan-out, checkpoint/resume, and a human-in-the-loop checkpoint before final decisioning.
   - The graph is the central control plane that decides which agent or tool runs next and when to stop.

5. **Specialized agent layer**
   - `agents/` contains focused ReAct-style agents: product search, price comparison, review analysis, negotiation, reflection, decisioning, plus a supervisor.
   - These agents are not the data sources themselves; they coordinate reasoning over tool outputs and feed results back into the graph.

6. **Tool and provider boundary**
   - `tools/` wraps the concrete capabilities used by agents: product search, price comparison, price history, deal finding, spec matching, stock alerts, review analysis, and RAG retrieval.
   - `tools/providers/` and `tools/scrapers/` isolate external commerce sources and browser automation. The repo supports a mock catalog mode, so external API/site dependencies can be swapped out without changing agent contracts.

7. **RAG subsystem**
   - `rag/` provides hybrid retrieval and conversational QA support. The structure shows ingestion, retrievers, and a conversational chain, with hybrid dense/sparse retrieval and reranking described in the README.
   - This subsystem is a separate knowledge path used when the agent needs product/spec/review context beyond live tool calls.

8. **Memory and personalization**
   - `memory/` stores user profiles plus short-term, long-term, and entity memory. This is the personalization/state layer that lets the assistant remember preferences, constraints, and prior interactions across turns.
   - It sits alongside the graph rather than inside the frontend, so state can influence routing and decisions server-side.

9. **Background processing**
   - `workers/` defines a Celery app and tasks for asynchronous work. This is the non-interactive execution path for jobs that should not block chat turns, such as scraping, ingestion, alerts, or maintenance tasks.
   - Redis is part of the runtime surface here, both as shared infrastructure and likely as the task broker/cache boundary.

10. **Shared runtime and observability**
   - `common/` holds cross-cutting infrastructure: LLM client setup, logging, metrics, observability hooks, retries, settings, text utilities, Redis client, and shared schemas.
   - This is the glue that keeps API, agents, RAG, and workers consistent in configuration and telemetry.

11. **Infrastructure and deployment**
   - `infra/docker/`, `docker-compose.yml`, and `infra/terraform/` define the deployment boundaries: local multi-service composition, containerization, Prometheus metrics scraping, and AWS-oriented infrastructure stubs.
   - The README also calls out ChromaDB, Redis, Playwright browser runtime, LangSmith/OpenTelemetry hooks, and Flower/Celery support as operational dependencies.

12. **End-to-end flow**
   - A typical request is: frontend sends a chat message → FastAPI authenticates and streams the request into the shopping graph → the graph consults memory/RAG/tools/agents → results are optionally augmented by background workers or external providers → streamed recommendations and deal summaries are returned to the UI.
   - The important architectural distinction is that the UI is presentation-only, the API is the transport/security layer, and the graph/agents/tools/memory/RAG stack contains the business logic.

13. **Testing boundaries**
   - The repo is organized for layered verification: unit tests for tools, integration tests for agents, graph tests for routing, RAG retrieval tests, end-to-end shopping-flow tests, and load tests.
   - That test split mirrors the runtime boundaries and is a good guide for drawing the architecture diagram.

Main entry points: [`tools/product_search.py`](https://github.com/mathewgeejo/cartgenie/blob/main/tools/product_search.py) (Commerce tools), [`graphs/shopping_graph.py`](https://github.com/mathewgeejo/cartgenie/blob/main/graphs/shopping_graph.py) (Shopping graph), [`agents/supervisor.py`](https://github.com/mathewgeejo/cartgenie/blob/main/agents/supervisor.py) (Supervisor), [`api/main.py`](https://github.com/mathewgeejo/cartgenie/blob/main/api/main.py) (API entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Next app<br/>Next.js UI<br/>[page.tsx]"]
  node_frontend_chat["Chat UI<br/>client component<br/>[chat-ui.tsx]"]
  node_frontend_cards["Deal views<br/>product widgets<br/>[product-card.tsx]"]
  node_frontend_api["API client<br/>HTTP client<br/>[api.ts]"]
end

subgraph group_api["API"]
  node_api_main["API entry<br/>FastAPI app<br/>[main.py]"]
  node_api_auth["JWT auth<br/>security<br/>[jwt.py]"]
  node_api_state["API state<br/>session state<br/>[state.py]"]
end

subgraph group_core["Core backend"]
  node_graph_main{{"Shopping graph<br/>orchestrator<br/>[shopping_graph.py]"}}
  node_agent_supervisor["Supervisor<br/>[supervisor.py]"]
  node_agent_search["Search agent<br/>ReAct agent"]
  node_agent_compare["Compare agent<br/>ReAct agent"]
  node_agent_review["Review agent<br/>ReAct agent"]
  node_agent_decision["Decision agent<br/>ReAct agent<br/>[decision_agent.py]"]
  node_toolset["Commerce tools<br/>tool suite<br/>[product_search.py]"]
  node_providers[("Providers<br/>source adapters")]
  node_scraper["Scraper<br/>browser automation"]
  node_rag_stack["RAG stack<br/>retrieval pipeline<br/>[hybrid.py]"]
  node_memory_store[("Memory<br/>personalization store<br/>[short_term.py]")]
  node_common_runtime["Shared runtime<br/>shared infra<br/>[llm.py]"]
end

subgraph group_ops["Ops"]
  node_workers["Workers<br/>Celery tasks<br/>[tasks.py]"]
  node_infra["Deployment<br/>docker/terraform<br/>[docker-compose.yml]"]
end

node_frontend_app -->|"renders"| node_frontend_chat
node_frontend_app -->|"renders"| node_frontend_cards
node_frontend_chat -->|"sends chat"| node_frontend_api
node_frontend_cards -->|"loads data"| node_frontend_api
node_frontend_api -->|"HTTP/stream"| node_api_main
node_api_main -->|"authenticates"| node_api_auth
node_api_main -->|"hydrates state"| node_api_state
node_api_main -->|"dispatches chat"| node_graph_main
node_graph_main -->|"routes"| node_agent_supervisor
node_graph_main -->|"reads/writes"| node_memory_store
node_graph_main -->|"retrieves context"| node_rag_stack
node_agent_supervisor -->|"delegates"| node_agent_search
node_agent_supervisor -->|"delegates"| node_agent_compare
node_agent_supervisor -->|"delegates"| node_agent_review
node_agent_supervisor -->|"delegates"| node_agent_decision
node_agent_search -->|"uses"| node_toolset
node_agent_compare -->|"uses"| node_toolset
node_agent_review -->|"uses"| node_toolset
node_agent_decision -->|"uses"| node_toolset
node_toolset -->|"queries"| node_providers
node_toolset -->|"scrapes"| node_scraper
node_rag_stack -->|"shares runtime"| node_common_runtime
node_graph_main -->|"shares runtime"| node_common_runtime
node_workers -->|"executes async"| node_toolset
node_workers -->|"shares runtime"| node_common_runtime
node_infra -->|"deploys"| node_workers
node_infra -->|"deploys"| node_api_main

click node_frontend_app "https://github.com/mathewgeejo/cartgenie/blob/main/frontend/app/page.tsx"
click node_frontend_chat "https://github.com/mathewgeejo/cartgenie/blob/main/frontend/components/chat-ui.tsx"
click node_frontend_cards "https://github.com/mathewgeejo/cartgenie/blob/main/frontend/components/product-card.tsx"
click node_frontend_api "https://github.com/mathewgeejo/cartgenie/blob/main/frontend/lib/api.ts"
click node_api_main "https://github.com/mathewgeejo/cartgenie/blob/main/api/main.py"
click node_api_auth "https://github.com/mathewgeejo/cartgenie/blob/main/api/auth/jwt.py"
click node_api_state "https://github.com/mathewgeejo/cartgenie/blob/main/api/state.py"
click node_graph_main "https://github.com/mathewgeejo/cartgenie/blob/main/graphs/shopping_graph.py"
click node_agent_supervisor "https://github.com/mathewgeejo/cartgenie/blob/main/agents/supervisor.py"
click node_agent_search "https://github.com/mathewgeejo/cartgenie/blob/main/agents/product_search_agent.py"
click node_agent_compare "https://github.com/mathewgeejo/cartgenie/blob/main/agents/price_comparison_agent.py"
click node_agent_review "https://github.com/mathewgeejo/cartgenie/blob/main/agents/review_analyzer_agent.py"
click node_agent_decision "https://github.com/mathewgeejo/cartgenie/blob/main/agents/decision_agent.py"
click node_toolset "https://github.com/mathewgeejo/cartgenie/blob/main/tools/product_search.py"
click node_providers "https://github.com/mathewgeejo/cartgenie/blob/main/tools/providers/ecommerce_sources.py"
click node_scraper "https://github.com/mathewgeejo/cartgenie/blob/main/tools/scrapers/playwright_scraper.py"
click node_rag_stack "https://github.com/mathewgeejo/cartgenie/blob/main/rag/retrievers/hybrid.py"
click node_memory_store "https://github.com/mathewgeejo/cartgenie/blob/main/memory/stores/short_term.py"
click node_common_runtime "https://github.com/mathewgeejo/cartgenie/blob/main/common/llm.py"
click node_workers "https://github.com/mathewgeejo/cartgenie/blob/main/workers/tasks.py"
click node_infra "https://github.com/mathewgeejo/cartgenie/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_chat,node_frontend_cards,node_frontend_api toneBlue
class node_api_main,node_api_auth,node_api_state toneAmber
class node_graph_main,node_agent_supervisor,node_agent_search,node_agent_compare,node_agent_review,node_agent_decision,node_toolset,node_providers,node_scraper,node_rag_stack,node_memory_store,node_common_runtime toneMint
class node_workers,node_infra toneRose
```

## Components

### Frontend

Next.js client and presentation components

- [Next app](https://github.com/mathewgeejo/cartgenie/blob/main/frontend/app/page.tsx): Next.js UI · Chat-centric frontend shell and page layout · `frontend/app/page.tsx`
- [Chat UI](https://github.com/mathewgeejo/cartgenie/blob/main/frontend/components/chat-ui.tsx): client component · Streaming chat interaction surface · `frontend/components/chat-ui.tsx`
- [Deal views](https://github.com/mathewgeejo/cartgenie/blob/main/frontend/components/product-card.tsx): product widgets · Recommendation and comparison presentation components · `frontend/components/product-card.tsx`
- [API client](https://github.com/mathewgeejo/cartgenie/blob/main/frontend/lib/api.ts): HTTP client · Frontend transport wrapper for backend calls · `frontend/lib/api.ts`

### API

FastAPI transport and auth boundary

- [API entry](https://github.com/mathewgeejo/cartgenie/blob/main/api/main.py): FastAPI app · Public HTTP, streaming, websocket, and metrics boundary · `api/main.py`
- [JWT auth](https://github.com/mathewgeejo/cartgenie/blob/main/api/auth/jwt.py): security · Request authentication and authorization layer · `api/auth/jwt.py`
- [API state](https://github.com/mathewgeejo/cartgenie/blob/main/api/state.py): session state · Request and conversation state wiring for API handlers · `api/state.py`

### Core backend

Orchestration, agents, tools, memory, and RAG

- [Shopping graph](https://github.com/mathewgeejo/cartgenie/blob/main/graphs/shopping_graph.py): orchestrator · Conditional routing, checkpoints, and final decision control plane · `graphs/shopping_graph.py`
- [Supervisor](https://github.com/mathewgeejo/cartgenie/blob/main/agents/supervisor.py): agent supervisor · Coordinates specialized agents and task delegation · `agents/supervisor.py`
- [Search agent](https://github.com/mathewgeejo/cartgenie/blob/main/agents/product_search_agent.py): ReAct agent · Product discovery and query refinement · `agents/product_search_agent.py`
- [Compare agent](https://github.com/mathewgeejo/cartgenie/blob/main/agents/price_comparison_agent.py): ReAct agent · Price and offer comparison reasoning · `agents/price_comparison_agent.py`
- [Review agent](https://github.com/mathewgeejo/cartgenie/blob/main/agents/review_analyzer_agent.py): ReAct agent · Review synthesis and sentiment analysis · `agents/review_analyzer_agent.py`
- [Decision agent](https://github.com/mathewgeejo/cartgenie/blob/main/agents/decision_agent.py): ReAct agent · Recommendation and final choice synthesis · `agents/decision_agent.py`
- [Commerce tools](https://github.com/mathewgeejo/cartgenie/blob/main/tools/product_search.py): tool suite · Search, pricing, deals, specs, alerts, and review utilities · `tools/product_search.py`
- [Providers](https://github.com/mathewgeejo/cartgenie/blob/main/tools/providers/ecommerce_sources.py): source adapters · External commerce sources and mock catalog abstraction · `tools/providers/ecommerce_sources.py`
- [Scraper](https://github.com/mathewgeejo/cartgenie/blob/main/tools/scrapers/playwright_scraper.py): browser automation · Playwright-based site automation for external data access · `tools/scrapers/playwright_scraper.py`
- [RAG stack](https://github.com/mathewgeejo/cartgenie/blob/main/rag/retrievers/hybrid.py): retrieval pipeline · Hybrid retrieval, ingestion, and conversational QA · `rag/retrievers/hybrid.py`
- [Memory](https://github.com/mathewgeejo/cartgenie/blob/main/memory/stores/short_term.py): personalization store · Profiles plus short-term, long-term, and entity memory · `memory/stores/short_term.py`
- [Shared runtime](https://github.com/mathewgeejo/cartgenie/blob/main/common/llm.py): shared infra · LLM clients, logging, metrics, retries, settings, Redis, schemas · `common/llm.py`

### Ops

Workers and deployment infrastructure

- [Workers](https://github.com/mathewgeejo/cartgenie/blob/main/workers/tasks.py): Celery tasks · Async background jobs for scraping, ingestion, alerts, and maintenance · `workers/tasks.py`
- [Deployment](https://github.com/mathewgeejo/cartgenie/blob/main/docker-compose.yml): docker/terraform · Container, compose, Prometheus, and infrastructure definitions · `docker-compose.yml`

## Connections

- Next app → Chat UI: renders · Page composes the chat experience
- Next app → Deal views: renders · Page shows recommendation surfaces
- Chat UI → API client: sends chat · UI calls backend for streaming responses
- Deal views → API client: loads data · Presentation components consume API-fed product data
- API client → API entry: HTTP/stream · Frontend client talks to the FastAPI boundary
- API entry → JWT auth: authenticates · Requests pass through JWT security checks
- API entry → API state: hydrates state · API handlers load request and conversation context
- API entry → Shopping graph: dispatches chat · Validated requests enter the orchestration graph
- Shopping graph → Supervisor: routes · Graph delegates control to agent supervision
- Shopping graph → Memory: reads/writes · Graph consults personalization and conversation state
- Shopping graph → RAG stack: retrieves context · Graph can invoke retrieval for product/spec knowledge
- Supervisor → Search agent: delegates · Supervisor fans out discovery work
- Supervisor → Compare agent: delegates · Supervisor fans out comparison work
- Supervisor → Review agent: delegates · Supervisor fans out review analysis work
- Supervisor → Decision agent: delegates · Supervisor gathers final recommendation reasoning
- Search agent → Commerce tools: uses · Search agent calls product and deal tools
- Compare agent → Commerce tools: uses · Comparison agent calls pricing and spec tools
- Review agent → Commerce tools: uses · Review agent calls review analysis tools
- Decision agent → Commerce tools: uses · Decisioning can consult commerce tools
- Commerce tools → Providers: queries · Tools resolve data through provider adapters
- Commerce tools → Scraper: scrapes · Tools can rely on browser automation for live sources
- RAG stack → Shared runtime: shares runtime · Retrieval uses common settings, logging, and LLM clients
- Shopping graph → Shared runtime: shares runtime · Orchestration depends on shared infrastructure helpers
- Workers → Commerce tools: executes async · Background tasks reuse the same commerce capabilities
- Workers → Shared runtime: shares runtime · Workers use common Redis, logging, and retry plumbing
- Deployment → Workers: deploys · Infrastructure defines worker runtime and service wiring
- Deployment → API entry: deploys · Infrastructure defines API container and runtime
