# mahadevaperuka/notebookreader architecture

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

- Interactive diagram: https://gitdiagram.com/mahadevaperuka/notebookreader
- Repository: https://github.com/mahadevaperuka/notebookreader

## Overview

#### Purpose and Entry Points
This is a Next.js App Router application for document-grounded chat: users upload documents, index them into searchable chunks, and converse through chat sessions. Browser entry points are the landing page at `app/page.tsx` and per-chat routes at `app/chat/[chatId]/page.tsx`; shared application shell and providers live in `app/layout.tsx` and `app/providers.tsx`.

#### UI and Session Navigation
The UI creates or opens chat-specific views under `/chat/[chatId]`, isolating each conversation by chat ID. Client-side state/service integration is established through the app provider boundary. Relevant paths: `app/chat/[chatId]/page.tsx`, `app/providers.tsx`, `app/globals.css`.

#### HTTP API Boundary
Next.js route handlers expose the server-side workflow:
- `app/api/upload/route.ts` accepts source documents.
- `app/api/index-chat/route.ts` associates/indexes document content for a chat.
- `app/api/search/route.ts` retrieves relevant indexed content.
- `app/api/chat/[...slug]/route.ts` handles chat/agent requests.

These handlers are the boundary between browser interactions and persistence, parsing, retrieval, and model inference.

#### Document Ingestion and Indexing
Uploaded PDFs are parsed through `lib/pdf.ts`, then represented as documents and chunks in Convex. The indexing stage persists document metadata/content, partitions text into retrieval units, and builds chat-scoped index records. Core storage modules are `convex/documents.ts`, `convex/chunks.ts`, and `convex/chatIndex.ts`.

#### Retrieval-Augmented Agent Flow
Chat requests are orchestrated by `lib/agent.ts`. The expected control flow is: resolve the target chat, retrieve matching indexed chunks through the search/index layer, construct grounded model context, and return the generated response. `lib/ollama.ts` provides the local/model-service integration boundary, while `app/api/search/route.ts` exposes retrieval independently where needed.

#### Persistent Backend and Infrastructure
Convex is the primary application backend, configured by `convex.config.ts` and modeled in `convex/schema.ts`. It holds chats, source documents, chunks, and chat-index relationships; query/mutation logic is separated into `convex/chats.ts`, `convex/documents.ts`, `convex/chunks.ts`, and `convex/chatIndex.ts`. Generated Convex bindings under `convex/_generated/` connect typed frontend/server code to that backend.

#### Material Runtime Boundaries
The application runs on Next.js/React with App Router API routes. Convex supplies persistent server-side data operations and reactive application state. Ollama is the external/local LLM runtime accessed via `lib/ollama.ts`; PDF parsing is isolated behind `lib/pdf.ts`. This separation keeps model inference and file parsing outside UI components and makes API handlers the orchestration layer.

Main entry points: [`app/chat/[chatId]/page.tsx`](https://github.com/mahadevaperuka/notebookreader/blob/main/app/chat/%5BchatId%5D/page.tsx) (Chat view), [`convex/schema.ts`](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/schema.ts) (Data schema), [`app/api/index-chat/route.ts`](https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/index-chat/route.ts) (Index chat API), [`lib/agent.ts`](https://github.com/mahadevaperuka/notebookreader/blob/main/lib/agent.ts) (Grounded agent)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Next.js UI"]
  node_landing["Landing page<br/>App Router page<br/>[page.tsx]"]
  node_chat_page["Chat view<br/>App Router page<br/>[page.tsx]"]
  node_providers["App providers<br/>client state boundary<br/>[providers.tsx]"]
  node_app_shell["App shell<br/>root layout<br/>[layout.tsx]"]
end

subgraph group_api["API workflow"]
  node_upload_api["Upload API<br/>route handler<br/>[route.ts]"]
  node_index_api["Index chat API<br/>route handler<br/>[route.ts]"]
  node_search_api["Search API<br/>route handler<br/>[route.ts]"]
  node_chat_api["Chat agent API<br/>route handler<br/>[route.ts]"]
  node_agent["Grounded agent<br/>RAG orchestrator<br/>[agent.ts]"]
end

subgraph group_backend["Convex backend"]
  node_convex_schema[("Data schema<br/>Convex schema<br/>[schema.ts]")]
  node_chats["Chats<br/>Convex operations<br/>[chats.ts]"]
  node_documents["Documents<br/>Convex operations<br/>[documents.ts]"]
  node_chunks["Chunks<br/>Convex operations<br/>[chunks.ts]"]
  node_chat_index["Chat index<br/>Convex operations<br/>[chatIndex.ts]"]
  node_convex_bindings["Typed Convex bindings<br/>generated client/server bindings"]
end

subgraph group_integrations["Runtime integrations"]
  node_pdf_parser{{"PDF parser<br/>parsing adapter<br/>[pdf.ts]"}}
  node_ollama{{"Ollama<br/>LLM adapter<br/>[ollama.ts]"}}
end

node_app_shell -->|"wraps"| node_providers
node_providers -->|"supports"| node_landing
node_providers -->|"supports"| node_chat_page
node_landing -->|"opens /chat/&#91;chatId&#93;"| node_chat_page
node_chat_page -->|"uploads documents"| node_upload_api
node_chat_page -->|"indexes for chat"| node_index_api
node_chat_page -->|"retrieves context"| node_search_api
node_chat_page -->|"sends messages"| node_chat_api
node_upload_api -->|"parses PDF"| node_pdf_parser
node_upload_api -->|"stores source"| node_documents
node_index_api -->|"reads source"| node_documents
node_index_api -->|"creates chunks"| node_chunks
node_index_api -->|"associates with chat"| node_chat_index
node_search_api -->|"resolves chat scope"| node_chat_index
node_search_api -->|"returns matching chunks"| node_chunks
node_chat_api -->|"orchestrates request"| node_agent
node_agent -->|"resolves chat"| node_chats
node_agent -->|"retrieves indexed context"| node_chat_index
node_agent -->|"generates response"| node_ollama
node_convex_bindings -->|"typed from"| node_convex_schema
node_chats -->|"uses"| node_convex_schema
node_documents -->|"uses"| node_convex_schema
node_chunks -->|"uses"| node_convex_schema
node_chat_index -->|"uses"| node_convex_schema

click node_landing "https://github.com/mahadevaperuka/notebookreader/blob/main/app/page.tsx"
click node_chat_page "https://github.com/mahadevaperuka/notebookreader/blob/main/app/chat/%5BchatId%5D/page.tsx"
click node_providers "https://github.com/mahadevaperuka/notebookreader/blob/main/app/providers.tsx"
click node_app_shell "https://github.com/mahadevaperuka/notebookreader/blob/main/app/layout.tsx"
click node_upload_api "https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/upload/route.ts"
click node_index_api "https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/index-chat/route.ts"
click node_search_api "https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/search/route.ts"
click node_chat_api "https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/chat/%5B...slug%5D/route.ts"
click node_pdf_parser "https://github.com/mahadevaperuka/notebookreader/blob/main/lib/pdf.ts"
click node_agent "https://github.com/mahadevaperuka/notebookreader/blob/main/lib/agent.ts"
click node_ollama "https://github.com/mahadevaperuka/notebookreader/blob/main/lib/ollama.ts"
click node_convex_schema "https://github.com/mahadevaperuka/notebookreader/blob/main/convex/schema.ts"
click node_chats "https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chats.ts"
click node_documents "https://github.com/mahadevaperuka/notebookreader/blob/main/convex/documents.ts"
click node_chunks "https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chunks.ts"
click node_chat_index "https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chatIndex.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_landing,node_chat_page,node_providers,node_app_shell toneBlue
class node_upload_api,node_index_api,node_search_api,node_chat_api,node_agent toneAmber
class node_convex_schema,node_chats,node_documents,node_chunks,node_chat_index,node_convex_bindings toneMint
class node_pdf_parser,node_ollama toneRose
```

## Components

### Next.js UI

- [Landing page](https://github.com/mahadevaperuka/notebookreader/blob/main/app/page.tsx): App Router page · Browser entry for starting or opening chats. · `app/page.tsx`
- [Chat view](https://github.com/mahadevaperuka/notebookreader/blob/main/app/chat/%5BchatId%5D/page.tsx): App Router page · Conversation UI isolated by chat ID. · `app/chat/[chatId]/page.tsx`
- [App providers](https://github.com/mahadevaperuka/notebookreader/blob/main/app/providers.tsx): client state boundary · Connects client state and services. · `app/providers.tsx`
- [App shell](https://github.com/mahadevaperuka/notebookreader/blob/main/app/layout.tsx): root layout · `app/layout.tsx`

### API workflow

- [Upload API](https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/upload/route.ts): route handler · `app/api/upload/route.ts`
- [Index chat API](https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/index-chat/route.ts): route handler · `app/api/index-chat/route.ts`
- [Search API](https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/search/route.ts): route handler · Independent retrieval endpoint. · `app/api/search/route.ts`
- [Chat agent API](https://github.com/mahadevaperuka/notebookreader/blob/main/app/api/chat/%5B...slug%5D/route.ts): route handler · `app/api/chat/[...slug]/route.ts`
- [Grounded agent](https://github.com/mahadevaperuka/notebookreader/blob/main/lib/agent.ts): RAG orchestrator · Builds context from chat-scoped retrieval and generates replies. · `lib/agent.ts`

### Convex backend

- [Data schema](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/schema.ts): Convex schema · Models chats, documents, chunks, and index relationships. · `convex/schema.ts`
- [Chats](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chats.ts): Convex operations · `convex/chats.ts`
- [Documents](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/documents.ts): Convex operations · Persists source document metadata and content. · `convex/documents.ts`
- [Chunks](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chunks.ts): Convex operations · Partitions and retrieves text units. · `convex/chunks.ts`
- [Chat index](https://github.com/mahadevaperuka/notebookreader/blob/main/convex/chatIndex.ts): Convex operations · Maintains chat-scoped document/chunk associations. · `convex/chatIndex.ts`
- **Typed Convex bindings**: generated client/server bindings

### Runtime integrations

- [PDF parser](https://github.com/mahadevaperuka/notebookreader/blob/main/lib/pdf.ts): parsing adapter · `lib/pdf.ts`
- [Ollama](https://github.com/mahadevaperuka/notebookreader/blob/main/lib/ollama.ts): LLM adapter · Local or external model-service boundary. · `lib/ollama.ts`

## Connections

- App shell → App providers: wraps
- App providers → Landing page: supports
- App providers → Chat view: supports
- Landing page → Chat view: opens /chat/[chatId]
- Chat view → Upload API: uploads documents
- Chat view → Index chat API: indexes for chat
- Chat view → Search API: retrieves context
- Chat view → Chat agent API: sends messages
- Upload API → PDF parser: parses PDF
- Upload API → Documents: stores source
- Index chat API → Documents: reads source
- Index chat API → Chunks: creates chunks
- Index chat API → Chat index: associates with chat
- Search API → Chat index: resolves chat scope
- Search API → Chunks: returns matching chunks
- Chat agent API → Grounded agent: orchestrates request
- Grounded agent → Chats: resolves chat
- Grounded agent → Chat index: retrieves indexed context
- Grounded agent → Ollama: generates response
- Typed Convex bindings → Data schema: typed from
- Chats → Data schema: uses
- Documents → Data schema: uses
- Chunks → Data schema: uses
- Chat index → Data schema: uses
