# bhamid7112/aws-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/bhamid7112/aws-chatbot
- Repository: https://github.com/bhamid7112/aws-chatbot

## Overview

#### Purpose and topology
A single-screen streaming chatbot: React/Vite UI sends chat requests to a FastAPI API and renders SSE reply fragments. It deploys as two containers on one AWS EC2 instance; Caddy is the sole public ingress, serves the compiled UI, terminates HTTPS, and proxies `/api/*` internally. Primary entry points: `frontend/src/main.tsx`, `backend/app/main.py`, `deploy/docker-compose.yml`.

#### Backend layers and request boundary
FastAPI is assembled in `backend/app/main.py`. HTTP routes validate DTO shape and expose `POST /api/chat` in `backend/app/interfaces/routes.py`; dependencies select concrete implementations in `backend/app/interfaces/dependencies.py`. Interface code translates HTTP/Pydantic data into domain requests, keeping framework concerns outside the use case.

#### Chat use case and domain port
`backend/app/application/chat_service.py` synchronously applies prompt rules before streaming, allowing invalid requests to return HTTP 422 before headers are committed. It depends on domain entities, errors, and the `ReplyGenerator` port rather than infrastructure: `backend/app/domain/entities.py`, `backend/app/domain/ports.py`, `backend/app/domain/errors.py`. This is the backend swap boundary for a real LLM provider.

#### Streaming generator and SSE protocol
The current adapter, `backend/app/infrastructure/canned_reply_generator.py`, emits a fixed reply word-by-word. `backend/app/interfaces/sse.py` frames generator chunks as SSE `delta`, in-stream `error`, and final `[DONE]` events. Mid-stream failures cannot change HTTP status, so they are encoded in-band and always followed by `[DONE]`; pre-stream failures remain normal HTTP errors.

#### Frontend composition and stream consumption
`frontend/src/main.tsx` wires the concrete gateway, while `frontend/src/App.tsx` hosts the single chat screen. `frontend/src/application/useChat.ts` owns transcript and in-flight assistant-message state: append user message, invoke the gateway, and append incoming deltas. Presentation components under `frontend/src/presentation/` render the transcript, input, typing state, and wire readout without transport knowledge.

#### Frontend transport boundary
The domain gateway contract is `frontend/src/domain/chatGateway.ts`; HTTP/SSE parsing is isolated in `frontend/src/infrastructure/sseChatGateway.ts`. It POSTs message plus history to relative `/api/chat`, manually reads `fetch` response bytes, splits SSE frames, and yields chunks. Relative routing preserves one-origin behavior: Vite proxies locally and Caddy proxies in production, avoiding a production CORS dependency.

#### Runtime, edge, and deployment infrastructure
Compose builds an API image and a Caddy/UI image via `deploy/api.Dockerfile`, `deploy/web.Dockerfile`, and `deploy/docker-compose.yml`. `deploy/caddy/Caddyfile` disables proxy buffering for streaming and manages ACME IP-address certificates with persisted Caddy data. Terraform provisions a dedicated AWS VPC, public EC2 host, Elastic IP, security/IAM, and SSM-only administration: `infra/compute.tf`, `infra/network.tf`, `infra/security.tf`. Bootstrap/release behavior is installed through `infra/templates/user_data.sh.tftpl`; the host clones the public repository, rebuilds containers, and redeploys idempotently.

Main entry points: [`deploy/caddy/Caddyfile`](https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/caddy/Caddyfile) (Caddy ingress), [`deploy/docker-compose.yml`](https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/docker-compose.yml) (Container deployment), [`frontend/src/infrastructure/sseChatGateway.ts`](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/infrastructure/sseChatGateway.ts) (SSE chat gateway), [`frontend/src/application/useChat.ts`](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/application/useChat.ts) (Chat state use case)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_entry["Frontend bootstrap<br/>React entry<br/>[main.tsx]"]
  node_chat_screen["Chat screen<br/>React composition<br/>[App.tsx]"]
  node_chat_hook["Chat state use case<br/>frontend application<br/>[useChat.ts]"]
  node_gateway_port{{"Chat gateway port<br/>domain contract<br/>[chatGateway.ts]"}}
  node_sse_gateway["SSE chat gateway<br/>HTTP/SSE adapter<br/>[sseChatGateway.ts]"]
  node_presentation["Chat presentation<br/>React components"]
end

subgraph group_backend["Backend"]
  node_api_main["FastAPI assembly<br/>API entry<br/>[main.py]"]
  node_chat_route["POST /api/chat<br/>HTTP route<br/>[routes.py]"]
  node_dependencies["Dependency selection<br/>composition root<br/>[dependencies.py]"]
  node_chat_service["Chat service<br/>application use case<br/>[chat_service.py]"]
  node_chat_domain{{"Chat domain<br/>entities, errors &amp; port<br/>[ports.py]"}}
  node_canned_generator["Canned reply generator<br/>ReplyGenerator adapter"]
  node_sse_framing["SSE framing<br/>interface protocol<br/>[sse.py]"]
end

subgraph group_edge["Edge &amp; containers"]
  node_caddy["Caddy ingress<br/>TLS reverse proxy"]
  node_compose["Container deployment<br/>Docker Compose<br/>[docker-compose.yml]"]
  node_container_images["API and web images<br/>container builds<br/>[api.Dockerfile]"]
end

subgraph group_cloud["AWS infrastructure"]
  node_ec2["EC2 chat host<br/>AWS compute<br/>[compute.tf]"]
  node_network_security["VPC, security &amp; IAM<br/>AWS network/security<br/>[network.tf]"]
  node_bootstrap["Host bootstrap &amp; release<br/>user-data deployment<br/>[user_data.sh.tftpl]"]
end

node_browser(("Chat user<br/>browser client"))

node_browser -->|"HTTPS"| node_caddy
node_caddy -->|"serves compiled UI"| node_frontend_entry
node_frontend_entry -->|"mounts"| node_chat_screen
node_chat_screen -->|"uses"| node_chat_hook
node_chat_hook -->|"supplies state"| node_presentation
node_chat_hook -->|"depends on"| node_gateway_port
node_sse_gateway -.->|"implements"| node_gateway_port
node_gateway_port -->|"chat stream"| node_sse_gateway
node_sse_gateway -->|"POST /api/chat"| node_caddy
node_caddy -->|"proxies /api/*"| node_api_main
node_api_main -->|"registers"| node_chat_route
node_chat_route -->|"resolves"| node_dependencies
node_chat_route -->|"invokes"| node_chat_service
node_chat_service -->|"uses port and rules"| node_chat_domain
node_dependencies -->|"selects"| node_canned_generator
node_canned_generator -.->|"implements ReplyGenerator"| node_chat_domain
node_chat_service -->|"streams chunks"| node_sse_framing
node_sse_framing -->|"SSE delta/error/&#91;DONE&#93;"| node_sse_gateway
node_compose -->|"builds"| node_container_images
node_compose -->|"runs web container"| node_caddy
node_compose -->|"runs API container"| node_api_main
node_ec2 -->|"hosts"| node_compose
node_network_security -->|"provisions access"| node_ec2
node_bootstrap -->|"installs release flow"| node_ec2

click node_frontend_entry "https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/main.tsx"
click node_chat_screen "https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/App.tsx"
click node_chat_hook "https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/application/useChat.ts"
click node_gateway_port "https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/domain/chatGateway.ts"
click node_sse_gateway "https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/infrastructure/sseChatGateway.ts"
click node_api_main "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/main.py"
click node_chat_route "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/routes.py"
click node_dependencies "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/dependencies.py"
click node_chat_service "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/application/chat_service.py"
click node_chat_domain "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/domain/ports.py"
click node_canned_generator "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/infrastructure/canned_reply_generator.py"
click node_sse_framing "https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/sse.py"
click node_caddy "https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/caddy/Caddyfile"
click node_compose "https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/docker-compose.yml"
click node_container_images "https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/api.Dockerfile"
click node_ec2 "https://github.com/bhamid7112/aws-chatbot/blob/main/infra/compute.tf"
click node_network_security "https://github.com/bhamid7112/aws-chatbot/blob/main/infra/network.tf"
click node_bootstrap "https://github.com/bhamid7112/aws-chatbot/blob/main/infra/templates/user_data.sh.tftpl"

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_entry,node_chat_screen,node_chat_hook,node_gateway_port,node_sse_gateway,node_presentation toneBlue
class node_api_main,node_chat_route,node_dependencies,node_chat_service,node_chat_domain,node_canned_generator,node_sse_framing toneAmber
class node_caddy,node_compose,node_container_images toneMint
class node_ec2,node_network_security,node_bootstrap toneRose
class node_browser toneNeutral
```

## Components

### Frontend

- [Frontend bootstrap](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/main.tsx): React entry · `frontend/src/main.tsx`
- [Chat screen](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/App.tsx): React composition · `frontend/src/App.tsx`
- [Chat state use case](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/application/useChat.ts): frontend application · Owns transcript and in-flight assistant state. · `frontend/src/application/useChat.ts`
- [Chat gateway port](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/domain/chatGateway.ts): domain contract · `frontend/src/domain/chatGateway.ts`
- [SSE chat gateway](https://github.com/bhamid7112/aws-chatbot/blob/main/frontend/src/infrastructure/sseChatGateway.ts): HTTP/SSE adapter · Posts to relative /api/chat and yields parsed chunks. · `frontend/src/infrastructure/sseChatGateway.ts`
- **Chat presentation**: React components · Renders transcript, input, typing state, and wire readout.

### Backend

- [FastAPI assembly](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/main.py): API entry · `backend/app/main.py`
- [POST /api/chat](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/routes.py): HTTP route · Validates DTOs and translates HTTP data into domain requests. · `backend/app/interfaces/routes.py`
- [Dependency selection](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/dependencies.py): composition root · `backend/app/interfaces/dependencies.py`
- [Chat service](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/application/chat_service.py): application use case · Applies prompt rules before streaming starts. · `backend/app/application/chat_service.py`
- [Chat domain](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/domain/ports.py): entities, errors & port · Defines ReplyGenerator provider boundary. · `backend/app/domain/ports.py`
- [Canned reply generator](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/infrastructure/canned_reply_generator.py): ReplyGenerator adapter · Emits a fixed reply word-by-word. · `backend/app/infrastructure/canned_reply_generator.py`
- [SSE framing](https://github.com/bhamid7112/aws-chatbot/blob/main/backend/app/interfaces/sse.py): interface protocol · Frames delta, in-band error, and final [DONE] events. · `backend/app/interfaces/sse.py`

### Edge & containers

- [Caddy ingress](https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/caddy/Caddyfile): TLS reverse proxy · Serves UI, terminates HTTPS, and disables proxy buffering. · `deploy/caddy/Caddyfile`
- [Container deployment](https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/docker-compose.yml): Docker Compose · Runs API and Caddy/UI containers on one host. · `deploy/docker-compose.yml`
- [API and web images](https://github.com/bhamid7112/aws-chatbot/blob/main/deploy/api.Dockerfile): container builds · `deploy/api.Dockerfile`

### AWS infrastructure

- [EC2 chat host](https://github.com/bhamid7112/aws-chatbot/blob/main/infra/compute.tf): AWS compute · Single public host with Elastic IP and SSM-only administration. · `infra/compute.tf`
- [VPC, security & IAM](https://github.com/bhamid7112/aws-chatbot/blob/main/infra/network.tf): AWS network/security · `infra/network.tf`
- [Host bootstrap & release](https://github.com/bhamid7112/aws-chatbot/blob/main/infra/templates/user_data.sh.tftpl): user-data deployment · Clones the public repository and idempotently rebuilds and redeploys. · `infra/templates/user_data.sh.tftpl`

### Other components

- **Chat user**: browser client

## Connections

- Chat user → Caddy ingress: HTTPS
- Caddy ingress → Frontend bootstrap: serves compiled UI
- Frontend bootstrap → Chat screen: mounts
- Chat screen → Chat state use case: uses
- Chat state use case → Chat presentation: supplies state
- Chat state use case → Chat gateway port: depends on
- SSE chat gateway → Chat gateway port: implements
- Chat gateway port → SSE chat gateway: chat stream
- SSE chat gateway → Caddy ingress: POST /api/chat
- Caddy ingress → FastAPI assembly: proxies /api/*
- FastAPI assembly → POST /api/chat: registers
- POST /api/chat → Dependency selection: resolves
- POST /api/chat → Chat service: invokes
- Chat service → Chat domain: uses port and rules
- Dependency selection → Canned reply generator: selects
- Canned reply generator → Chat domain: implements ReplyGenerator
- Chat service → SSE framing: streams chunks
- SSE framing → SSE chat gateway: SSE delta/error/[DONE]
- Container deployment → API and web images: builds
- Container deployment → Caddy ingress: runs web container
- Container deployment → FastAPI assembly: runs API container
- EC2 chat host → Container deployment: hosts
- VPC, security & IAM → EC2 chat host: provisions access
- Host bootstrap & release → EC2 chat host: installs release flow
