How beastly713/dad-chatbot works
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: retrieval_graph/graph.ts Retrieval graph · page.tsx Next.js app · state.ts LangGraph backend · ingestion_graph/graph.ts Ingestion graph
Components 20
Tooling
Root-level orchestration and repository checks
- Monorepo · Yarn/Turbo-managed TypeScript repository coordinating frontend and backend
- CI checks · Workspace consistency and path validation in CI and scripts
scripts/checkLanggraphPaths.js
Frontend
Next.js app and browser-facing UI
- Next.js app · App Router UI, layouts, and page shell
frontend/app/page.tsx - Chat route · Server route that forwards chat requests into backend graph execution
frontend/app/api/chat/route.ts - Ingest route · Server route for file upload and ingestion handoff
frontend/app/api/ingest/route.ts - Chat UI · Message display, prompts, and chat interaction widgets
frontend/components/chat-message.tsx - File UI · File preview and upload-facing components
frontend/components/file-preview.tsx - Check-in UI · Optional subjective check-in card and option components
frontend/components/subjective-checkin-card.tsx - Graph client · Shared frontend LangGraph client/server wrappers and graph typing
frontend/lib/langgraph-client.ts - PDF helpers · Frontend-side PDF handling and upload preparation
frontend/lib/pdf.ts
Backend
LangGraph service and shared backend logic
- LangGraph backend · Node/TypeScript backend organizing multiple graph workflows
backend/src/shared/state.ts - Ingestion graph · Document processing, normalization, and indexing pipeline
backend/src/ingestion_graph/graph.ts - Retrieval graph · Question answering over ingested content with summarization support
backend/src/retrieval_graph/graph.ts - Subjective layer · Optional phase 2 check-in planner, extractor, reducer, and controller
backend/src/subjective/index.ts - Safety layer · Triage, subflags, templates, and final guardrail enforcement
backend/src/safety/triage.ts - Shared backend · Common configuration, retrieval, state, and utilities used across graphs
backend/src/shared/configuration.ts - Knowledge base · Alcohol-focused filters, seed content, and provisioning scripts
backend/src/kb/filters.ts
External services
LLM, storage, and tracing dependencies
- Supabase · Persistent document storage and embedding-backed similarity search
- LLM provider · Model-backed generation for retrieval, safety, and subjective flows
- LangSmith · Optional observability for graph execution and debugging
Connections 30
- 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