# sopanmunde/trishul_ai architecture

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

- Interactive diagram: https://gitdiagram.com/sopanmunde/trishul_ai
- Repository: https://github.com/sopanmunde/trishul_ai

## Overview

1. **Top-level shape**
   - This is a **two-tier monorepo**: a **Next.js frontend** in `frontend/` and a **FastAPI/Python backend** in `api/`.
   - The architecture boundary is clear: the frontend handles presentation, routing, and chat UX; the backend owns authentication, persistence, retrieval, and LLM orchestration.

2. **Frontend subsystem**
   - The frontend is a **Next.js 15 app** with route groups for **landing**, **login**, **dashboard**, and **settings**.
   - `frontend/app/page.tsx` and supporting marketing components render the public product site, while `app/login`, `app/dashboard`, and `app/setting` drive the authenticated experience.
   - `middleware.ts` likely guards protected routes and redirects unauthenticated users.
   - The component set (`ChatPane`, `Composer`, `Sidebar`, `Message`, modals, etc.) shows a chat-product UI with conversation navigation, templates, folders, profile settings, and responsive interaction patterns.

3. **Backend API subsystem**
   - The backend is a **FastAPI service** exposed through `api/index.py`.
   - It is responsible for the application’s core business logic: login/signup/auth, chat request handling, streaming responses, and data access.
   - `api/auth.py`, `api/models.py`, and `api/db/database.py` indicate a layered backend where authentication, schemas/models, and database connectivity are separated.

4. **Authentication and user state**
   - Authentication is implemented with **JWTs** and hashed passwords, as described in the README and supported by `auth.py`.
   - User-facing state such as users, conversations, folders, and templates is persisted in **MongoDB** via the async driver mentioned in the README.
   - This makes MongoDB the system-of-record for mutable application data, while the vector store is used only for retrieval.

5. **RAG retrieval subsystem**
   - The assistant uses a **Retrieval-Augmented Generation** pipeline rather than answering directly from the model.
   - Incoming questions are embedded with a Hugging Face sentence-transformer model (`all-MiniLM-L6-v2` per README), then matched against a **Pinecone vector index**.
   - The repo’s `api/data/Medical_book.pdf` and `store_index.py` show an offline ingestion path that turns source documents into indexed chunks.

6. **Generation subsystem**
   - Retrieved chunks are injected into prompt templates from `api/src/prompts.py`, with helper logic in `api/src/helper.py`.
   - The backend then calls a **Google GenAI / Gemini** model for response generation.
   - The design is explicitly safety-oriented: the system prompt constrains the assistant to non-diagnostic, medically cautious answers.

7. **Streaming response flow**
   - Chat responses are streamed back to the client, not returned as a single blocking payload.
   - `api/test_stream.py` suggests SSE or similar streaming behavior was developed and tested separately.
   - This streaming boundary matters for the diagram: the backend emits incremental tokens/events, and the frontend chat pane updates in real time.

8. **Conversation and UI state management**
   - The frontend maintains rich local interaction state around conversations, folders, templates, search, settings, and user profile.
   - Components like `Sidebar`, `ConversationRow`, `FolderRow`, `TemplateRow`, `SearchModal`, and `SettingsPopover` indicate that the dashboard is more than a simple chat box: it is a multi-entity workspace.
   - The chat UI is also designed to render markdown and code-like content cleanly, based on the README’s markdown/highlight dependencies.

9. **Supporting scripts and operational boundaries**
   - `store_index.py` is the offline indexing pipeline entrypoint, separate from runtime chat serving.
   - `list_models.py` suggests a utility for inspecting available model options or provider configuration.
   - `render.yaml` and `vercel.json` indicate deployment concerns are split between backend hosting and frontend hosting, which reinforces the service boundary.

10. **What to draw as the main data flows**
   - **User flow:** browser → Next.js UI → FastAPI auth/chat endpoints → MongoDB / Pinecone / Gemini → streamed response back to browser.
   - **Ingestion flow:** PDF/source docs in `api/data/` → chunking/indexing via `store_index.py` → Pinecone vector index.
   - **Persistence flow:** authenticated user actions in the dashboard → backend APIs → MongoDB for durable chat/app metadata.
   - The key architectural distinction is that **MongoDB stores app state**, **Pinecone stores semantic knowledge**, and **Gemini generates the final answer using both retrieved context and prompt policy**.

Main entry points: [`api/index.py`](https://github.com/sopanmunde/trishul_ai/blob/master/api/index.py) (API service), [`frontend/app`](https://github.com/sopanmunde/trishul_ai/tree/master/frontend/app) (Next app), [`frontend/app/dashboard/page.tsx`](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/dashboard/page.tsx) (Dashboard), [`api/src/helper.py`](https://github.com/sopanmunde/trishul_ai/blob/master/api/src/helper.py) (Orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Next app<br/>Next.js 15 app"]
  node_frontend_landing["Landing<br/>marketing routes<br/>[page.tsx]"]
  node_frontend_auth["Login<br/>auth route<br/>[page.tsx]"]
  node_frontend_dashboard["Dashboard<br/>workspace route<br/>[page.tsx]"]
  node_frontend_settings["Settings<br/>[page.tsx]"]
  node_frontend_middleware["Route guard<br/>middleware<br/>[middleware.ts]"]
  node_frontend_shell["App shell<br/>layout + styles<br/>[layout.tsx]"]
  node_frontend_marketing["Marketing UI<br/>landing components<br/>[hero.tsx]"]
  node_frontend_chat["Chat UI<br/>workspace components<br/>[ChatPane.jsx]"]
  node_frontend_sidebar["Workspace nav<br/>navigation components<br/>[Sidebar.jsx]"]
  node_frontend_ui["UI kit<br/>shared UI<br/>[button.tsx]"]
end

subgraph group_backend["Backend"]
  node_backend_api["API service<br/>FastAPI app<br/>[index.py]"]
  node_backend_auth["Auth layer<br/>JWT auth<br/>[auth.py]"]
  node_backend_models["Schemas<br/>data models<br/>[models.py]"]
  node_backend_db[("Mongo layer<br/>database connector<br/>[database.py]")]
  node_backend_prompts["Prompting<br/>prompt templates<br/>[prompts.py]"]
  node_backend_helper["Orchestration<br/>LLM helpers<br/>[helper.py]"]
  node_backend_ingest["Indexer<br/>offline pipeline<br/>[store_index.py]"]
  node_backend_stream["Streaming<br/>response stream<br/>[test_stream.py]"]
  node_backend_data["Source docs<br/>corpus<br/>[Medical_book.pdf]"]
end

subgraph group_external["External systems"]
  node_external_mongo[("MongoDB<br/>document store")]
  node_external_pinecone[("Pinecone<br/>vector index")]
  node_external_gemini{{"Gemini<br/>genai model"}}
  node_external_browser(("Browser<br/>client runtime"))
end

node_external_browser -->|"open app"| node_frontend_app
node_frontend_middleware -.->|"protect"| node_frontend_dashboard
node_frontend_middleware -.->|"protect"| node_frontend_settings
node_frontend_app -->|"render"| node_frontend_landing
node_frontend_app -->|"render"| node_frontend_auth
node_frontend_app -->|"render"| node_frontend_dashboard
node_frontend_app -->|"render"| node_frontend_settings
node_frontend_app -->|"wraps"| node_frontend_shell
node_frontend_landing -->|"compose"| node_frontend_marketing
node_frontend_dashboard -->|"compose"| node_frontend_chat
node_frontend_dashboard -->|"compose"| node_frontend_sidebar
node_frontend_chat -->|"use"| node_frontend_ui
node_frontend_sidebar -->|"use"| node_frontend_ui
node_frontend_auth -->|"login"| node_backend_api
node_frontend_dashboard -->|"chat"| node_backend_api
node_backend_api -->|"delegate"| node_backend_auth
node_backend_api -->|"validate"| node_backend_models
node_backend_api -->|"store"| node_backend_db
node_backend_api -->|"orchestrate"| node_backend_helper
node_backend_helper -->|"build prompt"| node_backend_prompts
node_backend_helper -->|"retrieve"| node_external_pinecone
node_backend_helper -->|"generate"| node_external_gemini
node_backend_db -->|"persist"| node_external_mongo
node_backend_ingest -->|"ingest"| node_backend_data
node_backend_ingest -->|"index"| node_external_pinecone
node_backend_stream -.->|"verify"| node_backend_api

click node_frontend_app "https://github.com/sopanmunde/trishul_ai/tree/master/frontend/app"
click node_frontend_landing "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/page.tsx"
click node_frontend_auth "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/login/page.tsx"
click node_frontend_dashboard "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/dashboard/page.tsx"
click node_frontend_settings "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/setting/page.tsx"
click node_frontend_middleware "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/middleware.ts"
click node_frontend_shell "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/layout.tsx"
click node_frontend_marketing "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/hero.tsx"
click node_frontend_chat "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/ChatPane.jsx"
click node_frontend_sidebar "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/Sidebar.jsx"
click node_frontend_ui "https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/ui/button.tsx"
click node_backend_api "https://github.com/sopanmunde/trishul_ai/blob/master/api/index.py"
click node_backend_auth "https://github.com/sopanmunde/trishul_ai/blob/master/api/auth.py"
click node_backend_models "https://github.com/sopanmunde/trishul_ai/blob/master/api/models.py"
click node_backend_db "https://github.com/sopanmunde/trishul_ai/blob/master/api/db/database.py"
click node_backend_prompts "https://github.com/sopanmunde/trishul_ai/blob/master/api/src/prompts.py"
click node_backend_helper "https://github.com/sopanmunde/trishul_ai/blob/master/api/src/helper.py"
click node_backend_ingest "https://github.com/sopanmunde/trishul_ai/blob/master/api/store_index.py"
click node_backend_stream "https://github.com/sopanmunde/trishul_ai/blob/master/api/test_stream.py"
click node_backend_data "https://github.com/sopanmunde/trishul_ai/blob/master/api/data/Medical_book.pdf"

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_landing,node_frontend_auth,node_frontend_dashboard,node_frontend_settings,node_frontend_middleware,node_frontend_shell,node_frontend_marketing,node_frontend_chat,node_frontend_sidebar,node_frontend_ui toneBlue
class node_backend_api,node_backend_auth,node_backend_models,node_backend_db,node_backend_prompts,node_backend_helper,node_backend_ingest,node_backend_stream,node_backend_data toneAmber
class node_external_mongo,node_external_pinecone,node_external_gemini,node_external_browser toneMint
```

## Components

### Frontend

Next.js app and chat product UI

- [Next app](https://github.com/sopanmunde/trishul_ai/tree/master/frontend/app): Next.js 15 app · Routes, layouts, and page shells for the web client · `frontend/app`
- [Landing](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/page.tsx): marketing routes · Public homepage and product marketing surfaces · `frontend/app/page.tsx`
- [Login](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/login/page.tsx): auth route · Sign-in and authentication entrypoint · `frontend/app/login/page.tsx`
- [Dashboard](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/dashboard/page.tsx): workspace route · Authenticated chat workspace and conversation shell · `frontend/app/dashboard/page.tsx`
- [Settings](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/setting/page.tsx): settings route · User settings and preference screens · `frontend/app/setting/page.tsx`
- [Route guard](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/middleware.ts): middleware · Protects routes and redirects unauthenticated users · `frontend/middleware.ts`
- [App shell](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/app/layout.tsx): layout + styles · Global layout, loading state, and styling foundation · `frontend/app/layout.tsx`
- [Marketing UI](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/hero.tsx): landing components · Hero, pricing, testimonials, FAQ, and footer sections · `frontend/components/hero.tsx`
- [Chat UI](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/ChatPane.jsx): workspace components · Chat pane, composer, message rendering, and assistant wrapper · `frontend/components/ChatPane.jsx`
- [Workspace nav](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/Sidebar.jsx): navigation components · Sidebar, conversations, folders, templates, search, and profile controls · `frontend/components/Sidebar.jsx`
- [UI kit](https://github.com/sopanmunde/trishul_ai/blob/master/frontend/components/ui/button.tsx): shared UI · Reusable primitives and interaction components · `frontend/components/ui/button.tsx`

### Backend

FastAPI service, auth, persistence, and RAG pipeline

- [API service](https://github.com/sopanmunde/trishul_ai/blob/master/api/index.py): FastAPI app · HTTP entrypoint for auth, chat, and streaming endpoints · `api/index.py`
- [Auth layer](https://github.com/sopanmunde/trishul_ai/blob/master/api/auth.py): JWT auth · Login, signup, password hashing, and token handling · `api/auth.py`
- [Schemas](https://github.com/sopanmunde/trishul_ai/blob/master/api/models.py): data models · Application models and request/response shapes · `api/models.py`
- [Mongo layer](https://github.com/sopanmunde/trishul_ai/blob/master/api/db/database.py): database connector · Async MongoDB connection and persistence helpers · `api/db/database.py`
- [Prompting](https://github.com/sopanmunde/trishul_ai/blob/master/api/src/prompts.py): prompt templates · System and response prompts for model orchestration · `api/src/prompts.py`
- [Orchestration](https://github.com/sopanmunde/trishul_ai/blob/master/api/src/helper.py): LLM helpers · Retrieval, prompt assembly, and response helper logic · `api/src/helper.py`
- [Indexer](https://github.com/sopanmunde/trishul_ai/blob/master/api/store_index.py): offline pipeline · Document chunking and vector index build entrypoint · `api/store_index.py`
- [Streaming](https://github.com/sopanmunde/trishul_ai/blob/master/api/test_stream.py): response stream · Standalone streaming behavior and endpoint testing · `api/test_stream.py`
- [Source docs](https://github.com/sopanmunde/trishul_ai/blob/master/api/data/Medical_book.pdf): corpus · Input document used for retrieval indexing · `api/data/Medical_book.pdf`

### External systems

Hosted services used by the backend

- **MongoDB**: document store · System of record for users, chats, folders, and templates
- **Pinecone**: vector index · Semantic retrieval store for embedded document chunks
- **Gemini**: genai model · Generates answers from retrieved context and policy prompts
- **Browser**: client runtime · End-user entry point for the web application

## Connections

- Browser → Next app: open app · Users load the Next.js client in the browser
- Route guard → Dashboard: protect · Redirects unauthenticated users away from protected routes
- Route guard → Settings: protect · Applies access control to settings views
- Next app → Landing: render · Serves public marketing pages
- Next app → Login: render · Serves the login flow
- Next app → Dashboard: render · Serves the main chat workspace
- Next app → Settings: render · Serves account and preference pages
- Next app → App shell: wraps · Provides global layout and styling
- Landing → Marketing UI: compose · Landing page is built from marketing sections
- Dashboard → Chat UI: compose · Dashboard centers on the chat experience
- Dashboard → Workspace nav: compose · Dashboard includes conversation and workspace navigation
- Chat UI → UI kit: use · Chat UI relies on shared primitives and inputs
- Workspace nav → UI kit: use · Sidebar and dialogs use common UI building blocks
- Login → API service: login · Auth flow calls backend endpoints
- Dashboard → API service: chat · Workspace sends chat and persistence requests
- API service → Auth layer: delegate · API routes use auth helpers for credential handling
- API service → Schemas: validate · Request and response payloads are shaped by models
- API service → Mongo layer: store · Persists application state in MongoDB
- API service → Orchestration: orchestrate · Coordinates retrieval and generation logic
- Orchestration → Prompting: build prompt · Assembles system and response prompts
- Orchestration → Pinecone: retrieve · Fetches semantically relevant chunks
- Orchestration → Gemini: generate · Calls the LLM to produce the final answer
- Mongo layer → MongoDB: persist · Reads and writes durable app state
- Indexer → Source docs: ingest · Indexes the source PDF corpus
- Indexer → Pinecone: index · Writes embedded chunks to the vector store
- Streaming → API service: verify · Exercises streaming response behavior
