# beastly713/dad-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/beastly713/dad-chatbot
- Repository: https://github.com/beastly713/dad-chatbot

## Overview

1. **Repository shape**
   - This is a TypeScript monorepo split into two main runtime boundaries: `backend/` and `frontend/`.
   - The backend is a LangGraph-powered Node service; the frontend is a Next.js app that drives file upload and chat UI.
   - Root-level tooling (`turbo.json`, Yarn config, CI workflow, path-check script) coordinates build/test consistency across both sides.

2. **Frontend boundary: UI + thin API layer**
   - The frontend is a Next.js App Router app (`frontend/app/...`) with a browser UI and two server routes: `app/api/ingest/route.ts` and `app/api/chat/route.ts`.
   - Those routes are the main bridge to the backend LangGraph service, so the frontend does not own the agent logic.
   - UI components are split into reusable chat/file/subjective-check-in widgets plus a large local UI kit under `components/ui/`.

3. **Backend boundary: LangGraph agent service**
   - The backend is organized around multiple LangGraph workflows rather than a single monolithic API.
   - The main graphs are:
     - `src/ingestion_graph/` for document processing and indexing
     - `src/retrieval_graph/` for question answering over ingested documents
     - `src/subjective/` for the optional Phase 2 subjective check-in layer
     - `src/safety/` for policy and guardrail checks
   - Shared backend concerns live under `src/shared/`.

4. **Document ingestion flow**
   - Ingestion starts from uploaded PDFs in the frontend and is handled by the backend ingestion graph.
   - The graph converts documents into LangChain `Document` objects, extracts/normalizes content, and then stores embeddings in a vector store.
   - The README and code structure indicate Supabase is the default vector database, with document storage and similarity search support.

5. **Retrieval / chat flow**
   - User questions go through the chat route to the retrieval graph, which decides whether to retrieve context or answer directly.
   - Retrieved chunks are used to generate a final response, with sources exposed back to the UI.
   - The retrieval graph is split into state, prompt templates, configuration, graph wiring, and summary utilities, showing a deliberate separation between orchestration and prompt logic.

6. **Vector store and knowledge-base layer**
   - `src/shared/retrieval.ts` and `src/shared/configuration.ts` suggest the vector store and retriever are pluggable rather than hardcoded.
   - `src/kb/` contains retrieval filters and seed data for an alcohol-related knowledge base, implying the repo includes domain-specific sample content and filter logic.
   - Seed scripts such as `scripts/seedAlcoholKb.ts` and `src/kb/seed/*` are part of the data provisioning path, not the user-facing app.

7. **Safety subsystem**
   - `src/safety/` is a distinct policy layer with triage, subflags, templates, and a final guard.
   - This is especially important because the Phase 2 docs indicate the project supports safe alcohol-support use cases.
   - The safety layer appears to sit between user intent and final assistant output, enforcing guardrails rather than generating core answers itself.

8. **Phase 2 subjective check-in layer**
   - `src/subjective/` and the corresponding frontend components add an optional non-diagnostic check-in flow.
   - It includes planning, extraction, freshness handling, reducer logic, and a controller, suggesting a mini state machine or orchestration layer that collects user-reported context before or alongside retrieval.
   - This layer is separate from the retrieval graph and safety logic, but likely informs them with higher-level support context.

9. **State and configuration as first-class boundaries**
   - Each major subsystem has its own `state.ts` and often `configuration.ts`, which is a LangGraph-style architecture pattern.
   - This means graph behavior is parameterized through typed state/config objects instead of hidden global state.
   - The design makes it easy to diagram message flow: input state enters a graph, nodes mutate/augment it, and the final state is rendered or streamed back.

10. **Shared utilities and cross-cutting concerns**
   - `src/shared/` contains reusable helpers for configuration, retrieval, state, and utilities used by multiple graphs.
   - On the frontend, `lib/langgraph-*` and `types/graphTypes.ts` encapsulate API/client integration so the UI can talk to LangGraph consistently.
   - `frontend/lib/pdf.ts` and `frontend/lib/subjective-checkin.ts` support file handling and check-in UI behavior.

11. **Runtime and integration model**
   - Backend runtime is Node.js/TypeScript with LangGraph orchestration and LangChain integrations.
   - The frontend is Next.js/React with server routes that proxy or prepare requests to the backend.
   - External services matter to the architecture: OpenAI (or another LLM provider) provides generation, Supabase stores embeddings and enables similarity search, and LangSmith tracing is optional for observability.

12. **Testing and operational guardrails**
   - The repository is test-heavy around graph behavior: ingestion, retrieval, safety, and subjective flows each have dedicated test suites.
   - The docs include phase-specific runbooks and acceptance cases, which suggests the architecture is intended to be validated as a workflow system, not just as a UI.
   - CI and lint/build scripts reinforce the split between backend graph correctness and frontend integration correctness.

13. **How to draw the main diagram**
   - Draw two top-level boxes: Next.js frontend and LangGraph backend.
   - Inside the backend, show separate graph clusters for ingestion, retrieval, safety, and subjective check-ins, all sharing configuration and vector-store access.
   - Put Supabase as the persistent document/embedding store, OpenAI/LLM provider as the generation service, and LangSmith as optional tracing.
   - Show two primary user paths: upload PDF → ingestion graph → Supabase, and ask question → chat route → retrieval/safety/subjective layers → streamed answer back to UI.

Main entry points: [`backend/src/retrieval_graph/graph.ts`](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/retrieval_graph/graph.ts) (Retrieval graph), [`frontend/app/page.tsx`](https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/page.tsx) (Next.js app), [`backend/src/shared/state.ts`](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/shared/state.ts) (LangGraph backend), [`backend/src/ingestion_graph/graph.ts`](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/ingestion_graph/graph.ts) (Ingestion graph)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_tooling["Tooling"]
  node_monorepo["Monorepo<br/>workspace"]
  node_ci_checks["CI checks<br/>build guardrails"]
end

subgraph group_frontend["Frontend"]
  node_next_app["Next.js app<br/>web frontend<br/>[page.tsx]"]
  node_chat_route["Chat route<br/>server api<br/>[route.ts]"]
  node_ingest_route["Ingest route<br/>server api<br/>[route.ts]"]
  node_chat_ui["Chat UI<br/>react components<br/>[chat-message.tsx]"]
  node_file_ui["File UI<br/>react components<br/>[file-preview.tsx]"]
  node_subjective_ui["Check-in UI<br/>react components"]
  node_langgraph_client["Graph client<br/>integration layer"]
  node_pdf_helpers["PDF helpers<br/>file utils<br/>[pdf.ts]"]
end

subgraph group_backend["Backend"]
  node_backend_core["LangGraph backend<br/>agent service<br/>[state.ts]"]
  node_ingestion_graph["Ingestion graph<br/>workflow<br/>[graph.ts]"]
  node_retrieval_graph["Retrieval graph<br/>workflow<br/>[graph.ts]"]
  node_subjective_layer["Subjective layer<br/>workflow<br/>[index.ts]"]
  node_safety_layer["Safety layer<br/>policy workflow<br/>[triage.ts]"]
  node_shared_backend["Shared backend<br/>shared modules<br/>[configuration.ts]"]
  node_kb_layer[("Knowledge base<br/>seed and filters<br/>[filters.ts]")]
end

subgraph group_external["External services"]
  node_supabase[("Supabase<br/>vector store")]
  node_llm_provider(("LLM provider<br/>generation service"))
  node_langsmith(("LangSmith<br/>tracing"))
end

node_monorepo -->|"contains"| node_next_app
node_monorepo -->|"contains"| node_backend_core
node_monorepo -->|"verifies"| node_ci_checks
node_next_app -->|"serves"| node_chat_route
node_next_app -->|"serves"| node_ingest_route
node_next_app -->|"renders"| node_chat_ui
node_next_app -->|"renders"| node_file_ui
node_next_app -->|"renders"| node_subjective_ui
node_chat_route -->|"calls"| node_langgraph_client
node_ingest_route -->|"prepares"| node_pdf_helpers
node_ingest_route -->|"calls"| node_langgraph_client
node_chat_route -->|"invokes"| node_retrieval_graph
node_ingest_route -->|"invokes"| node_ingestion_graph
node_backend_core -->|"hosts"| node_ingestion_graph
node_backend_core -->|"hosts"| node_retrieval_graph
node_backend_core -->|"hosts"| node_subjective_layer
node_backend_core -->|"hosts"| node_safety_layer
node_ingestion_graph -->|"uses"| node_shared_backend
node_retrieval_graph -->|"uses"| node_shared_backend
node_subjective_layer -->|"uses"| node_shared_backend
node_safety_layer -->|"uses"| node_shared_backend
node_ingestion_graph -->|"seeds"| node_kb_layer
node_retrieval_graph -->|"guards"| node_safety_layer
node_retrieval_graph -->|"augments"| node_subjective_layer
node_retrieval_graph -->|"retrieves from"| node_supabase
node_ingestion_graph -->|"indexes to"| node_supabase
node_subjective_layer -->|"analyzes with"| node_llm_provider
node_retrieval_graph -->|"generates with"| node_llm_provider
node_safety_layer -->|"evaluates with"| node_llm_provider
node_backend_core -.->|"traces"| node_langsmith

click node_ci_checks "https://github.com/beastly713/dad-chatbot/blob/main/scripts/checkLanggraphPaths.js"
click node_next_app "https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/page.tsx"
click node_chat_route "https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/api/chat/route.ts"
click node_ingest_route "https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/api/ingest/route.ts"
click node_chat_ui "https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/chat-message.tsx"
click node_file_ui "https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/file-preview.tsx"
click node_subjective_ui "https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/subjective-checkin-card.tsx"
click node_langgraph_client "https://github.com/beastly713/dad-chatbot/blob/main/frontend/lib/langgraph-client.ts"
click node_pdf_helpers "https://github.com/beastly713/dad-chatbot/blob/main/frontend/lib/pdf.ts"
click node_backend_core "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/shared/state.ts"
click node_ingestion_graph "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/ingestion_graph/graph.ts"
click node_retrieval_graph "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/retrieval_graph/graph.ts"
click node_subjective_layer "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/subjective/index.ts"
click node_safety_layer "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/safety/triage.ts"
click node_shared_backend "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/shared/configuration.ts"
click node_kb_layer "https://github.com/beastly713/dad-chatbot/blob/main/backend/src/kb/filters.ts"

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_monorepo,node_ci_checks toneBlue
class node_next_app,node_chat_route,node_ingest_route,node_chat_ui,node_file_ui,node_subjective_ui,node_langgraph_client,node_pdf_helpers toneAmber
class node_backend_core,node_ingestion_graph,node_retrieval_graph,node_subjective_layer,node_safety_layer,node_shared_backend,node_kb_layer toneMint
class node_supabase,node_llm_provider,node_langsmith toneRose
```

## Components

### Tooling

Root-level orchestration and repository checks

- **Monorepo**: workspace · Yarn/Turbo-managed TypeScript repository coordinating frontend and backend
- [CI checks](https://github.com/beastly713/dad-chatbot/blob/main/scripts/checkLanggraphPaths.js): build guardrails · Workspace consistency and path validation in CI and scripts · `scripts/checkLanggraphPaths.js`

### Frontend

Next.js app and browser-facing UI

- [Next.js app](https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/page.tsx): web frontend · App Router UI, layouts, and page shell · `frontend/app/page.tsx`
- [Chat route](https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/api/chat/route.ts): server api · Server route that forwards chat requests into backend graph execution · `frontend/app/api/chat/route.ts`
- [Ingest route](https://github.com/beastly713/dad-chatbot/blob/main/frontend/app/api/ingest/route.ts): server api · Server route for file upload and ingestion handoff · `frontend/app/api/ingest/route.ts`
- [Chat UI](https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/chat-message.tsx): react components · Message display, prompts, and chat interaction widgets · `frontend/components/chat-message.tsx`
- [File UI](https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/file-preview.tsx): react components · File preview and upload-facing components · `frontend/components/file-preview.tsx`
- [Check-in UI](https://github.com/beastly713/dad-chatbot/blob/main/frontend/components/subjective-checkin-card.tsx): react components · Optional subjective check-in card and option components · `frontend/components/subjective-checkin-card.tsx`
- [Graph client](https://github.com/beastly713/dad-chatbot/blob/main/frontend/lib/langgraph-client.ts): integration layer · Shared frontend LangGraph client/server wrappers and graph typing · `frontend/lib/langgraph-client.ts`
- [PDF helpers](https://github.com/beastly713/dad-chatbot/blob/main/frontend/lib/pdf.ts): file utils · Frontend-side PDF handling and upload preparation · `frontend/lib/pdf.ts`

### Backend

LangGraph service and shared backend logic

- [LangGraph backend](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/shared/state.ts): agent service · Node/TypeScript backend organizing multiple graph workflows · `backend/src/shared/state.ts`
- [Ingestion graph](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/ingestion_graph/graph.ts): workflow · Document processing, normalization, and indexing pipeline · `backend/src/ingestion_graph/graph.ts`
- [Retrieval graph](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/retrieval_graph/graph.ts): workflow · Question answering over ingested content with summarization support · `backend/src/retrieval_graph/graph.ts`
- [Subjective layer](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/subjective/index.ts): workflow · Optional phase 2 check-in planner, extractor, reducer, and controller · `backend/src/subjective/index.ts`
- [Safety layer](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/safety/triage.ts): policy workflow · Triage, subflags, templates, and final guardrail enforcement · `backend/src/safety/triage.ts`
- [Shared backend](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/shared/configuration.ts): shared modules · Common configuration, retrieval, state, and utilities used across graphs · `backend/src/shared/configuration.ts`
- [Knowledge base](https://github.com/beastly713/dad-chatbot/blob/main/backend/src/kb/filters.ts): seed and filters · Alcohol-focused filters, seed content, and provisioning scripts · `backend/src/kb/filters.ts`

### External services

LLM, storage, and tracing dependencies

- **Supabase**: vector store · Persistent document storage and embedding-backed similarity search
- **LLM provider**: generation service · Model-backed generation for retrieval, safety, and subjective flows
- **LangSmith**: tracing · Optional observability for graph execution and debugging

## Connections

- Monorepo → Next.js app: contains · Workspace includes the frontend app
- Monorepo → LangGraph backend: contains · Workspace includes the backend service
- Monorepo → CI checks: verifies · Shared scripts and CI keep path and build assumptions aligned
- Next.js app → Chat route: serves · UI sends chat requests through the server route
- Next.js app → Ingest route: serves · UI uploads documents through the ingest route
- Next.js app → Chat UI: renders · Chat page composes reusable chat widgets
- Next.js app → File UI: renders · Page includes file preview and upload widgets
- Next.js app → Check-in UI: renders · Phase 2 UI includes subjective check-in components
- Chat route → Graph client: calls · Server route uses shared graph integration wrappers
- Ingest route → PDF helpers: prepares · File handling and PDF utilities support upload flow
- Ingest route → Graph client: calls · Ingest route delegates work to backend graph execution
- Chat route → Retrieval graph: invokes · Chat requests are handled by the retrieval workflow
- Ingest route → Ingestion graph: invokes · Uploaded PDFs are processed by the ingestion workflow
- LangGraph backend → Ingestion graph: hosts · Backend organizes the ingestion workflow
- LangGraph backend → Retrieval graph: hosts · Backend organizes the retrieval workflow
- LangGraph backend → Subjective layer: hosts · Backend organizes the optional check-in workflow
- LangGraph backend → Safety layer: hosts · Backend organizes policy and guardrail checks
- Ingestion graph → Shared backend: uses · Ingestion shares config and state helpers
- Retrieval graph → Shared backend: uses · Retrieval shares config, state, and utility layers
- Subjective layer → Shared backend: uses · Check-in flow shares common backend support code
- Safety layer → Shared backend: uses · Safety logic reuses shared backend types and helpers
- Ingestion graph → Knowledge base: seeds · Knowledge-base seed content supports ingestion and filters
- Retrieval graph → Safety layer: guards · Safety layer constrains assistant output and user intent
- Retrieval graph → Subjective layer: augments · Optional subjective context can inform retrieval responses
- Retrieval graph → Supabase: retrieves from · Vector search returns stored document chunks and embeddings
- Ingestion graph → Supabase: indexes to · Ingested documents are embedded and stored for retrieval
- Subjective layer → LLM provider: analyzes with · Planner and extractor use model calls for structured support context
- Retrieval graph → LLM provider: generates with · Answers and summaries are produced by the model
- Safety layer → LLM provider: evaluates with · Policy and guardrail checks may use model-assisted classification
- LangGraph backend → LangSmith: traces · Optional tracing for graph execution and debugging
