# cassiemarie0728/the-fork architecture

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

- Interactive diagram: https://gitdiagram.com/cassiemarie0728/the-fork
- Repository: https://github.com/cassiemarie0728/the-fork

## Overview

1. **Overall shape**
   - This repository is a two-tier chat application: a **React frontend** in `frontend/` and a **FastAPI backend** in `backend/`.
   - The app’s core runtime dependency is an **external LLM service** accessed through `emergentintegrations.llm.chat`.
   - A **MongoDB service is present in the container setup**, but the product is intentionally **stateless**; conversation history is not treated as durable application data.

2. **Frontend subsystem**
   - The frontend is a Create React App–based UI, customized with **craco**, **Tailwind**, and several local plugins under `frontend/plugins/`.
   - Main UI pieces live in `frontend/src/components/` and include the chat window, fork setup, intensity toggle, message rendering, reset modal, and summary bar.
   - `frontend/src/hooks/useSessionId.js` indicates the client generates or maintains a session identifier locally and sends it with each request.
   - The frontend reads `REACT_APP_BACKEND_URL` and posts chat payloads to the backend; it is the primary user interaction boundary.

3. **Backend subsystem**
   - The backend entrypoint is `backend/server.py`, exposed via **Uvicorn + FastAPI**.
   - It provides at least two public routes: a lightweight health check at `/api/` and the main chat endpoint at `/api/chat`.
   - Request handling is centered on a structured payload containing `forkStatement`, `intensity`, `messages`, and `sessionId`.
   - The backend composes the LLM prompt, applies safety rules, and returns a single reply object to the frontend.

4. **LLM request path**
   - The main data flow is: **user input in the browser → frontend state → backend chat endpoint → prompt composition → Emergent LLM → backend response → chat UI**.
   - The session identifier is passed through the request, but the repository’s design is still **ephemeral**: the backend is not a durable conversation store.
   - The LLM model selection is configured in code, while authentication depends on `EMERGENT_LLM_KEY`.

5. **Persona and safety boundary**
   - The product is intentionally opinionated: the backend enforces the “Other You” persona and uses the user’s fork statement plus intensity level to shape tone.
   - Safety logic is part of the backend boundary, not the frontend: self-harm cues and hateful/dehumanizing language are detected server-side.
   - When triggered, the backend returns a refusal or crisis-oriented response instead of forwarding the request to the LLM.
   - Intensity changes style, not permissions; profanity may be allowed, but slurs, threats, and identity attacks are explicitly blocked.

6. **Persistence and state**
   - The app is designed to feel conversational without becoming a long-term data store.
   - Messages are sent with each request, but the architecture treats conversation state as **session-scoped and ephemeral**.
   - MongoDB is bundled as an optional service and appears to support the template/runtime environment rather than being essential to the chat experience.

7. **Container and local runtime topology**
   - `docker-compose.yml` orchestrates the full stack: **frontend on 3000**, **backend on 8000**, and **MongoDB on 27017**.
   - The backend has its own Dockerfile, and the frontend has both a development Dockerfile and a production Dockerfile.
   - The compose setup is optimized for local development with hot reload and service health checks rather than a single immutable production deployment image.

8. **Documentation subsystem**
   - The repo carries a substantial documentation layer separate from the app runtime.
   - Documentation is split across Markdown/RST sources under `docs/` and additional repo-level guides like `QUICKSTART.md`, `DOCKER_GUIDE.md`, and `backend/API_DOCUMENTATION.md`.
   - There is also a Docusaurus-based documentation site structure, indicating the docs are treated as a first-class deliverable, not just inline notes.

9. **Testing and verification**
   - Backend verification uses **Pytest**, with tests in both `backend/tests/` and top-level `tests/` for unit and integration coverage.
   - Frontend verification uses **Playwright** for end-to-end tests in `frontend/e2e/`.
   - CI is split by concern in GitHub Actions: backend tests, frontend tests, code quality, Docker build, and docs build/publish workflows.

10. **Architectural boundaries to preserve**
   - The key boundary is the **chat API contract** between browser and backend; most product behavior depends on the shape and semantics of that request.
   - The second boundary is the **backend-to-LLM integration**, where persona, safety, and tone are enforced before any external model call.
   - A third boundary is the **deployment/runtime split** between local dev, Docker Compose, and docs/CI workflows.
   - If you are drawing the architecture, treat the frontend, backend, LLM provider, and optional Mongo service as the primary runtime nodes, with docs/CI as supporting systems around them.

Main entry points: [`frontend/src/App.js`](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/App.js) (Chat UI), [`backend/server.py`](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py) (Chat Endpoint), [`docker-compose.yml`](https://github.com/cassiemarie0728/the-fork/blob/main/docker-compose.yml) (Compose Stack), [`.github/workflows/backend-tests.yml`](https://github.com/cassiemarie0728/the-fork/blob/main/.github/workflows/backend-tests.yml) (GitHub Actions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Chat UI<br/>React app<br/>[App.js]"]
  node_frontend_chat["Chat Window<br/>UI component<br/>[ChatWindow.js]"]
  node_frontend_fork["Fork Setup<br/>UI component<br/>[ForkSetup.js]"]
  node_frontend_intensity(("Intensity<br/>UI control<br/>[IntensityToggle.js]"))
  node_frontend_session["Session ID<br/>client state<br/>[useSessionId.js]"]
end

subgraph group_backend["Backend"]
  node_backend_server["API Server<br/>FastAPI app<br/>[server.py]"]
  node_backend_chat["Chat Endpoint<br/>HTTP route<br/>[server.py]"]
  node_backend_prompt["Prompt Builder<br/>composition<br/>[server.py]"]
  node_backend_safety{{"Safety Rules<br/>policy layer<br/>[server.py]"}}
  node_backend_llm["LLM Client<br/>external integration<br/>[server.py]"]
end

subgraph group_docs["Docs"]
  node_docs_site["Docs Site<br/>Docusaurus"]
  node_repo_docs["Repo Guides<br/>markdown docs<br/>[README.md]"]
end

subgraph group_ops["Runtime & CI"]
  node_mongo[("MongoDB<br/>optional service<br/>[docker-compose.yml]")]
  node_compose["Compose Stack<br/>docker orchestration<br/>[docker-compose.yml]"]
  node_frontend_docker["Frontend Docker<br/>container build"]
  node_backend_docker["Backend Docker<br/>container build"]
  node_backend_tests["Backend Tests<br/>pytest<br/>[test_server.py]"]
  node_frontend_tests["E2E Tests<br/>playwright<br/>[fork.spec.js]"]
  node_ci["GitHub Actions<br/>ci workflows<br/>[backend-tests.yml]"]
end

node_frontend_app -->|"renders"| node_frontend_chat
node_frontend_app -->|"collects"| node_frontend_fork
node_frontend_app -->|"controls"| node_frontend_intensity
node_frontend_app -->|"uses"| node_frontend_session
node_frontend_app -->|"POST /api/chat"| node_backend_chat
node_backend_server -->|"exposes"| node_backend_chat
node_backend_chat -->|"checks"| node_backend_safety
node_backend_chat -->|"builds"| node_backend_prompt
node_backend_prompt -->|"invokes"| node_backend_llm
node_backend_safety -.->|"bypasses"| node_backend_llm
node_backend_llm -->|"returns reply"| node_frontend_chat
node_backend_chat -.->|"tracks"| node_frontend_session
node_compose -->|"starts"| node_frontend_docker
node_compose -->|"starts"| node_backend_docker
node_compose -.->|"includes"| node_mongo
node_frontend_tests -->|"verifies"| node_frontend_app
node_backend_tests -->|"verifies"| node_backend_server
node_ci -->|"runs"| node_backend_tests
node_ci -->|"runs"| node_frontend_tests
node_ci -->|"builds"| node_docs_site
node_docs_site -.->|"sources"| node_repo_docs

click node_frontend_app "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/App.js"
click node_frontend_chat "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/ChatWindow.js"
click node_frontend_fork "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/ForkSetup.js"
click node_frontend_intensity "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/IntensityToggle.js"
click node_frontend_session "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/hooks/useSessionId.js"
click node_backend_server "https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py"
click node_backend_chat "https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py"
click node_backend_prompt "https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py"
click node_backend_safety "https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py"
click node_backend_llm "https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py"
click node_mongo "https://github.com/cassiemarie0728/the-fork/blob/main/docker-compose.yml"
click node_compose "https://github.com/cassiemarie0728/the-fork/blob/main/docker-compose.yml"
click node_frontend_docker "https://github.com/cassiemarie0728/the-fork/tree/main/frontend/Dockerfile"
click node_backend_docker "https://github.com/cassiemarie0728/the-fork/tree/main/backend/Dockerfile"
click node_backend_tests "https://github.com/cassiemarie0728/the-fork/blob/main/backend/tests/test_server.py"
click node_frontend_tests "https://github.com/cassiemarie0728/the-fork/blob/main/frontend/e2e/fork.spec.js"
click node_ci "https://github.com/cassiemarie0728/the-fork/blob/main/.github/workflows/backend-tests.yml"
click node_docs_site "https://github.com/cassiemarie0728/the-fork/blob/main/docs/docusaurus.config.js"
click node_repo_docs "https://github.com/cassiemarie0728/the-fork/blob/main/README.md"

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_fork,node_frontend_intensity,node_frontend_session toneBlue
class node_backend_server,node_backend_chat,node_backend_prompt,node_backend_safety,node_backend_llm toneAmber
class node_docs_site,node_repo_docs toneMint
class node_mongo,node_compose,node_frontend_docker,node_backend_docker,node_backend_tests,node_frontend_tests,node_ci toneRose
```

## Components

### Frontend

React UI and client-side request flow

- [Chat UI](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/App.js): React app · Main browser application and interaction shell · `frontend/src/App.js`
- [Chat Window](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/ChatWindow.js): UI component · Conversation rendering and message flow · `frontend/src/components/ChatWindow.js`
- [Fork Setup](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/ForkSetup.js): UI component · Collects the fork statement that shapes the persona · `frontend/src/components/ForkSetup.js`
- [Intensity](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/components/IntensityToggle.js): UI control · Adjusts tone and style level · `frontend/src/components/IntensityToggle.js`
- [Session ID](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/src/hooks/useSessionId.js): client state · Locally maintained session identifier · `frontend/src/hooks/useSessionId.js`

### Backend

FastAPI chat API and safety boundary

- [API Server](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py): FastAPI app · Uvicorn-hosted entrypoint exposing chat routes · `backend/server.py`
- [Chat Endpoint](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py): HTTP route · Accepts messages, fork statement, intensity, and session id · `backend/server.py`
- [Prompt Builder](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py): composition · Assembles persona and request context for the model · `backend/server.py`
- [Safety Rules](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py): policy layer · Server-side refusal and crisis detection boundary · `backend/server.py`
- [LLM Client](https://github.com/cassiemarie0728/the-fork/blob/main/backend/server.py): external integration · Calls Emergent chat service using configured model and key · `backend/server.py`

### Docs

Documentation site and repo guides

- [Docs Site](https://github.com/cassiemarie0728/the-fork/blob/main/docs/docusaurus.config.js): Docusaurus · Published documentation site and content structure · `docs/docusaurus.config.js`
- [Repo Guides](https://github.com/cassiemarie0728/the-fork/blob/main/README.md): markdown docs · Top-level guides and architecture notes · `README.md`

### Runtime & CI

Container, deployment, and verification workflows

- [MongoDB](https://github.com/cassiemarie0728/the-fork/blob/main/docker-compose.yml): optional service · Bundled container service, not the primary app datastore · `docker-compose.yml`
- [Compose Stack](https://github.com/cassiemarie0728/the-fork/blob/main/docker-compose.yml): docker orchestration · Runs frontend, backend, and MongoDB for local development · `docker-compose.yml`
- [Frontend Docker](https://github.com/cassiemarie0728/the-fork/tree/main/frontend/Dockerfile): container build · Development and production images for the client · `frontend/Dockerfile`
- [Backend Docker](https://github.com/cassiemarie0728/the-fork/tree/main/backend/Dockerfile): container build · Container image for the FastAPI service · `backend/Dockerfile`
- [Backend Tests](https://github.com/cassiemarie0728/the-fork/blob/main/backend/tests/test_server.py): pytest · Server and API verification coverage · `backend/tests/test_server.py`
- [E2E Tests](https://github.com/cassiemarie0728/the-fork/blob/main/frontend/e2e/fork.spec.js): playwright · Browser-level verification of fork and chat flows · `frontend/e2e/fork.spec.js`
- [GitHub Actions](https://github.com/cassiemarie0728/the-fork/blob/main/.github/workflows/backend-tests.yml): ci workflows · Backend, frontend, quality, Docker, and docs automation · `.github/workflows/backend-tests.yml`

## Connections

- Chat UI → Chat Window: renders · UI composes the chat experience
- Chat UI → Fork Setup: collects · User defines the fork statement
- Chat UI → Intensity: controls · User tunes response style
- Chat UI → Session ID: uses · Client attaches session identifier to requests
- Chat UI → Chat Endpoint: POST /api/chat · Browser sends chat payloads to the backend
- API Server → Chat Endpoint: exposes · FastAPI route surface
- Chat Endpoint → Safety Rules: checks · Safety screening happens before model use
- Chat Endpoint → Prompt Builder: builds · Request context becomes a structured prompt
- Prompt Builder → LLM Client: invokes · Prompt is sent to Emergent LLM service
- Safety Rules → LLM Client: bypasses · Refusals short-circuit external model calls
- LLM Client → Chat Window: returns reply · Backend response drives the chat UI
- Chat Endpoint → Session ID: tracks · Session id is passed through but remains ephemeral
- Compose Stack → Frontend Docker: starts · Compose launches the client container
- Compose Stack → Backend Docker: starts · Compose launches the API container
- Compose Stack → MongoDB: includes · MongoDB is part of the local stack
- E2E Tests → Chat UI: verifies · Playwright covers user-facing flows
- Backend Tests → API Server: verifies · Pytest covers API behavior
- GitHub Actions → Backend Tests: runs · Backend workflow executes server tests
- GitHub Actions → E2E Tests: runs · Frontend workflow executes e2e tests
- GitHub Actions → Docs Site: builds · Docs workflow publishes documentation
- Docs Site → Repo Guides: sources · Docs are backed by repo-level guides and site content
