# danish884803/ai-platform architecture

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

- Interactive diagram: https://gitdiagram.com/danish884803/ai-platform
- Repository: https://github.com/danish884803/ai-platform

## Overview

1. **What this repository is**
   - This is a **Next.js App Router** application built around a **legal-document / AI drafting workflow**.
   - The product has two major surfaces: a **public user experience** for chat/generate/edit/save/export, and an **admin console** for managing laws, prompts, templates, and users.

2. **Top-level application shape**
   - The `app/` directory contains both the **pages** and the **server API routes**.
   - The app is organized by user task rather than by technical layer: `chat`, `generate`, `editor`, `dashboard`, `login`, `register`, plus `admin/*`.
   - `components/` holds the reusable UI pieces, while `lib/` and `models/` contain the backend/domain logic.

3. **Public user flow**
   - A user typically lands on the marketing/home pages, authenticates, then enters the **dashboard** or goes directly into **chat**, **generate**, or **editor** flows.
   - `components/ChatBox.jsx`, `components/DocumentForm.jsx`, and `components/Editor.jsx` are the main interaction surfaces for creating and refining content.
   - Generated or edited documents are then persisted through dedicated API routes and can later be exported.

4. **Authentication and session boundary**
   - Authentication is handled through `app/api/auth/[...nextauth]/route.js`, with `app/login` and `app/register` as the entry pages.
   - `components/providers/SessionProvider.jsx` and `app/providers.jsx` indicate client-side session context is shared across the app.
   - Admin access is guarded separately via `lib/isAdmin.js`, so the admin subsystem is not just a UI convention; it has an explicit authorization check.

5. **Core backend API layer**
   - The app uses **Next.js route handlers** in `app/api/*` as its backend boundary.
   - The API is split by domain:
     - `api/chat`, `api/generate`, `api/rewrite` for AI-assisted drafting
     - `api/documents` and `api/save` for persistence
     - `api/export/pdf` and `api/export/word` for output generation
     - `api/templates` for template retrieval
     - `api/admin/*` for privileged CRUD operations
   - This means most business logic is centralized behind HTTP endpoints, not embedded directly in the UI.

6. **AI / drafting subsystem**
   - `lib/openai.js` is the integration point to the model provider.
   - `lib/getPrompt.js` and `app/api/admin/prompts/*` show that prompt text is treated as configurable content, not hardcoded behavior.
   - The system supports at least three AI-assisted actions:
     - **chat** for conversational assistance
     - **generate** for producing a draft from structured input
     - **rewrite** for transforming existing text
   - This subsystem is likely the main “intelligence” layer of the product.

7. **Retrieval and legal knowledge layer**
   - `lib/embeddings.js`, `lib/rag.js`, and `lib/searchLaws.js` indicate a **retrieval-augmented generation** design.
   - The app stores legal reference material in `models/Law.js`, and admin routes under `app/api/admin/laws/*` manage that corpus.
   - The typical flow is: user request → search relevant laws → build context/prompt → call OpenAI → return grounded output.
   - This is an important boundary because the app is not only generative; it also consults structured domain knowledge.

8. **Data persistence and domain models**
   - The persistent entities live in `models/`:
     - `Document.js` for user-created content
     - `Template.js` for reusable document structures
     - `Prompt.js` for configurable prompt definitions
     - `Law.js` for legal references
     - `User.js` for accounts/roles
   - `lib/mongodb.js` is the database connection layer, so MongoDB is the primary datastore.
   - The app’s stateful behavior depends on these models, especially for saved drafts, admin-managed content, and user records.

9. **Templates, localization, and content customization**
   - Templates are a first-class domain with both user-facing and admin-facing routes:
     - `app/api/templates`
     - `app/api/admin/templates/*`
   - `lib/templateLocalization.js`, `lib/translations.js`, and `app/providers/LanguageProvider.jsx` show the app supports **localized UI or localized template output**.
   - `components/LanguageToggle.jsx` suggests end users can switch language, which likely affects both interface text and generated/template text.

10. **Admin console subsystem**
   - The `app/admin/*` pages and `app/api/admin/*` routes form a distinct management plane.
   - Admins can manage:
     - **laws**: create/update/delete/bulk import
     - **prompts**: edit prompt content for chat/generate/rewrite
     - **templates**: CRUD, preview, and toggle activation
     - **users**: account management
   - `components/admin/PromptEditor.jsx` is the clearest sign that admin editing is content-driven and operationally important.

11. **Document lifecycle features**
   - The document APIs (`app/api/documents/*`, `app/api/save`, `app/api/export/*`) suggest a lifecycle of:
     - create or generate content
     - save it as a document record
     - optionally pin it for quick access
     - export it to PDF or Word
   - `app/api/documents/[id]/pin/route.js` indicates some documents have a user-visible “favorite” or priority state.

12. **UI composition and design system**
   - `components/ui/*` provides reusable primitives such as button, input, card, select, badge, and label.
   - `components/layout/*`, `components/landing/*`, and `components/sections/*` separate marketing/layout concerns from application flows.
   - This makes the frontend architecture fairly modular: page routes assemble feature components, which in turn build from shared UI primitives.

13. **Operational and setup concerns**
   - The repo includes `scripts/seedLaw.js` and `scripts/seedTemplate.js`, showing the app depends on seeded domain content to be useful.
   - `lib` utilities and `scripts/config.js` suggest environment-driven setup, likely for database and model/provider configuration.
   - Since the project is built with Next.js and deployed compatibly with Vercel, the runtime is a mix of server-rendered pages and serverless-style route handlers.

14. **How to draw the architecture**
   - A good diagram would show four main layers:
     - **Client/UI layer**: landing pages, editor, chat, dashboard, admin screens
     - **API/business layer**: Next.js route handlers for auth, documents, generation, export, admin
     - **AI/retrieval layer**: OpenAI + embeddings + law search + prompt assembly
     - **Data layer**: MongoDB models for users, documents, templates, prompts, laws
   - The key cross-cutting concerns are **authentication/authorization**, **prompt/config management**, and **localization**.

Main entry points: [`models/Document.js`](https://github.com/danish884803/ai-platform/blob/main/models/Document.js) (Domain Models), [`app/dashboard/page.jsx`](https://github.com/danish884803/ai-platform/blob/main/app/dashboard/page.jsx) (Dashboard), [`components/ChatBox.jsx`](https://github.com/danish884803/ai-platform/blob/main/components/ChatBox.jsx) (Feature UI), [`app/api/chat/route.js`](https://github.com/danish884803/ai-platform/blob/main/app/api/chat/route.js) (Drafting API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Client UI"]
  node_home["Landing<br/>page<br/>[page.js]"]
  node_auth_pages["Auth Pages<br/>[page.js]"]
  node_dashboard["Dashboard<br/>workspace<br/>[page.jsx]"]
  node_chat_flow["Chat<br/>drafting flow<br/>[page.js]"]
  node_generate_flow["Generate<br/>drafting flow<br/>[page.js]"]
  node_editor_flow["Editor<br/>editing flow<br/>[page.js]"]
  node_shared_ui["Feature UI<br/>components<br/>[ChatBox.jsx]"]
  node_session_ctx(("Session Provider<br/>client provider"))
  node_lang_ctx(("Language Provider<br/>localization"))
end

subgraph group_api["API Layer"]
  node_auth_api["Auth API<br/>nextauth route<br/>[route.js]"]
  node_draft_api["Drafting API<br/>ai routes<br/>[route.js]"]
  node_document_api["Documents API<br/>persistence routes<br/>[route.js]"]
  node_export_api["Export API<br/>output routes<br/>[route.js]"]
  node_template_api["Template API<br/>template routes<br/>[route.js]"]
  node_admin_api["Admin API<br/>privileged CRUD<br/>[route.js]"]
end

subgraph group_intelligence["AI & Retrieval"]
  node_prompt_engine["Prompt Engine<br/>prompt assembly<br/>[getPrompt.js]"]
  node_rag_stack["RAG Stack<br/>retrieval pipeline<br/>[rag.js]"]
  node_openai_integration["OpenAI Client<br/>model integration<br/>[openai.js]"]
end

subgraph group_data["Data Layer"]
  node_db_conn[("MongoDB Conn<br/>database connector<br/>[mongodb.js]")]
  node_domain_models[("Domain Models<br/>mongoose models<br/>[Document.js]")]
end

subgraph group_admin["Admin Console"]
  node_admin_console["Admin Console<br/>management UI<br/>[page.js]"]
  node_admin_prompts_ui["Prompt Editor<br/>admin editor<br/>[PromptEditor.jsx]"]
end

node_home -->|"start"| node_auth_pages
node_auth_pages -->|"establish"| node_session_ctx
node_session_ctx -->|"gate"| node_dashboard
node_dashboard -->|"launch"| node_chat_flow
node_dashboard -->|"launch"| node_generate_flow
node_dashboard -->|"open"| node_editor_flow
node_chat_flow -->|"calls"| node_draft_api
node_generate_flow -->|"calls"| node_draft_api
node_editor_flow -->|"loads/saves"| node_document_api
node_shared_ui -->|"supports"| node_chat_flow
node_shared_ui -->|"supports"| node_generate_flow
node_shared_ui -->|"supports"| node_editor_flow
node_lang_ctx -->|"localizes"| node_shared_ui
node_admin_console -->|"manages"| node_admin_api
node_admin_console -->|"uses"| node_admin_prompts_ui
node_admin_api -->|"uses"| node_db_conn
node_admin_api -->|"updates"| node_domain_models
node_document_api -->|"persists"| node_db_conn
node_document_api -->|"stores"| node_domain_models
node_template_api -->|"reads"| node_domain_models
node_draft_api -->|"builds"| node_prompt_engine
node_draft_api -->|"grounds"| node_rag_stack
node_prompt_engine -->|"feeds"| node_openai_integration
node_rag_stack -->|"feeds"| node_openai_integration
node_rag_stack -->|"consults"| node_domain_models
node_auth_api -->|"verifies"| node_domain_models
node_auth_api -->|"hydrates"| node_session_ctx

click node_home "https://github.com/danish884803/ai-platform/blob/main/app/page.js"
click node_auth_pages "https://github.com/danish884803/ai-platform/blob/main/app/login/page.js"
click node_dashboard "https://github.com/danish884803/ai-platform/blob/main/app/dashboard/page.jsx"
click node_chat_flow "https://github.com/danish884803/ai-platform/blob/main/app/chat/page.js"
click node_generate_flow "https://github.com/danish884803/ai-platform/blob/main/app/generate/page.js"
click node_editor_flow "https://github.com/danish884803/ai-platform/blob/main/app/editor/page.js"
click node_shared_ui "https://github.com/danish884803/ai-platform/blob/main/components/ChatBox.jsx"
click node_session_ctx "https://github.com/danish884803/ai-platform/blob/main/components/providers/SessionProvider.jsx"
click node_lang_ctx "https://github.com/danish884803/ai-platform/blob/main/app/providers/LanguageProvider.jsx"
click node_auth_api "https://github.com/danish884803/ai-platform/blob/main/app/api/auth/[...nextauth]/route.js"
click node_draft_api "https://github.com/danish884803/ai-platform/blob/main/app/api/chat/route.js"
click node_document_api "https://github.com/danish884803/ai-platform/blob/main/app/api/documents/route.js"
click node_export_api "https://github.com/danish884803/ai-platform/blob/main/app/api/export/pdf/route.js"
click node_template_api "https://github.com/danish884803/ai-platform/blob/main/app/api/templates/route.js"
click node_admin_api "https://github.com/danish884803/ai-platform/blob/main/app/api/admin/users/route.js"
click node_prompt_engine "https://github.com/danish884803/ai-platform/blob/main/lib/getPrompt.js"
click node_rag_stack "https://github.com/danish884803/ai-platform/blob/main/lib/rag.js"
click node_openai_integration "https://github.com/danish884803/ai-platform/blob/main/lib/openai.js"
click node_db_conn "https://github.com/danish884803/ai-platform/blob/main/lib/mongodb.js"
click node_domain_models "https://github.com/danish884803/ai-platform/blob/main/models/Document.js"
click node_admin_console "https://github.com/danish884803/ai-platform/blob/main/app/admin/page.js"
click node_admin_prompts_ui "https://github.com/danish884803/ai-platform/blob/main/components/admin/PromptEditor.jsx"

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_home,node_auth_pages,node_dashboard,node_chat_flow,node_generate_flow,node_editor_flow,node_shared_ui,node_session_ctx,node_lang_ctx toneBlue
class node_auth_api,node_draft_api,node_document_api,node_export_api,node_template_api,node_admin_api toneAmber
class node_prompt_engine,node_rag_stack,node_openai_integration toneMint
class node_db_conn,node_domain_models toneRose
class node_admin_console,node_admin_prompts_ui toneIndigo
```

## Components

### Client UI

Public pages and reusable client-side interaction components.

- [Landing](https://github.com/danish884803/ai-platform/blob/main/app/page.js): page · Public entry and marketing page. · `app/page.js`
- [Auth Pages](https://github.com/danish884803/ai-platform/blob/main/app/login/page.js): pages · Login and registration entry points. · `app/login/page.js`
- [Dashboard](https://github.com/danish884803/ai-platform/blob/main/app/dashboard/page.jsx): workspace · Authenticated hub for user workflows. · `app/dashboard/page.jsx`
- [Chat](https://github.com/danish884803/ai-platform/blob/main/app/chat/page.js): drafting flow · Conversational assistance for drafting legal content. · `app/chat/page.js`
- [Generate](https://github.com/danish884803/ai-platform/blob/main/app/generate/page.js): drafting flow · Structured document generation from form input. · `app/generate/page.js`
- [Editor](https://github.com/danish884803/ai-platform/blob/main/app/editor/page.js): editing flow · Refine, revise, and manage saved documents. · `app/editor/page.js`
- [Feature UI](https://github.com/danish884803/ai-platform/blob/main/components/ChatBox.jsx): components · Reusable chat, form, editor, layout, and landing components. · `components/ChatBox.jsx`
- [Session Provider](https://github.com/danish884803/ai-platform/blob/main/components/providers/SessionProvider.jsx): client provider · Client-side auth/session context shared across the app. · `components/providers/SessionProvider.jsx`
- [Language Provider](https://github.com/danish884803/ai-platform/blob/main/app/providers/LanguageProvider.jsx): localization · Client-side language and translation context. · `app/providers/LanguageProvider.jsx`

### API Layer

Next.js route handlers for auth, drafting, persistence, export, and admin operations.

- [Auth API](https://github.com/danish884803/ai-platform/blob/main/app/api/auth/[...nextauth]/route.js): nextauth route · Authentication and session backend boundary. · `app/api/auth/[...nextauth]/route.js`
- [Drafting API](https://github.com/danish884803/ai-platform/blob/main/app/api/chat/route.js): ai routes · Chat, generate, and rewrite endpoints. · `app/api/chat/route.js`
- [Documents API](https://github.com/danish884803/ai-platform/blob/main/app/api/documents/route.js): persistence routes · Create, read, update, pin, and save document records. · `app/api/documents/route.js`
- [Export API](https://github.com/danish884803/ai-platform/blob/main/app/api/export/pdf/route.js): output routes · PDF and Word export endpoints. · `app/api/export/pdf/route.js`
- [Template API](https://github.com/danish884803/ai-platform/blob/main/app/api/templates/route.js): template routes · Public template retrieval endpoint. · `app/api/templates/route.js`
- [Admin API](https://github.com/danish884803/ai-platform/blob/main/app/api/admin/users/route.js): privileged CRUD · Admin endpoints for laws, prompts, templates, and users. · `app/api/admin/users/route.js`

### AI & Retrieval

Prompting, embeddings, RAG, and legal search logic.

- [Prompt Engine](https://github.com/danish884803/ai-platform/blob/main/lib/getPrompt.js): prompt assembly · Builds prompt text from stored definitions and localization. · `lib/getPrompt.js`
- [RAG Stack](https://github.com/danish884803/ai-platform/blob/main/lib/rag.js): retrieval pipeline · Embeddings, search, and context assembly over legal knowledge. · `lib/rag.js`
- [OpenAI Client](https://github.com/danish884803/ai-platform/blob/main/lib/openai.js): model integration · Provider integration for generated and rewritten content. · `lib/openai.js`

### Data Layer

MongoDB connection and persisted domain entities.

- [MongoDB Conn](https://github.com/danish884803/ai-platform/blob/main/lib/mongodb.js): database connector · Shared database connection layer. · `lib/mongodb.js`
- [Domain Models](https://github.com/danish884803/ai-platform/blob/main/models/Document.js): mongoose models · Document, Law, Prompt, Template, and User persistence models. · `models/Document.js`

### Admin Console

Management pages for laws, prompts, templates, and users.

- [Admin Console](https://github.com/danish884803/ai-platform/blob/main/app/admin/page.js): management UI · Admin pages for governance and content operations. · `app/admin/page.js`
- [Prompt Editor](https://github.com/danish884803/ai-platform/blob/main/components/admin/PromptEditor.jsx): admin editor · UI for editing AI prompts. · `components/admin/PromptEditor.jsx`

## Connections

- Landing → Auth Pages: start · Users move from landing to authentication.
- Auth Pages → Session Provider: establish · Authenticated sessions are shared through client context.
- Session Provider → Dashboard: gate · Session state unlocks the main workspace.
- Dashboard → Chat: launch · Dashboard routes users into conversational drafting.
- Dashboard → Generate: launch · Dashboard routes users into document generation.
- Dashboard → Editor: open · Dashboard opens saved documents in the editor.
- Chat → Drafting API: calls · Chat requests are processed by the drafting API.
- Generate → Drafting API: calls · Structured generation uses the drafting API.
- Editor → Documents API: loads/saves · Editor reads and persists document state.
- Feature UI → Chat: supports · Reusable components build the chat surface.
- Feature UI → Generate: supports · Reusable components build the generation form.
- Feature UI → Editor: supports · Reusable components build the editor surface.
- Language Provider → Feature UI: localizes · Language context influences displayed UI and copy.
- Admin Console → Admin API: manages · Admin pages operate through privileged API routes.
- Admin Console → Prompt Editor: uses · Prompt editing is a dedicated admin interaction.
- Admin API → MongoDB Conn: uses · Privileged API routes share the database connector.
- Admin API → Domain Models: updates · Admin CRUD modifies prompts, laws, templates, and users.
- Documents API → MongoDB Conn: persists · Document lifecycle operations use MongoDB.
- Documents API → Domain Models: stores · Documents are represented in persisted domain models.
- Template API → Domain Models: reads · Templates are served from persisted domain records.
- Drafting API → Prompt Engine: builds · Drafting endpoints assemble prompts before model calls.
- Drafting API → RAG Stack: grounds · Drafting endpoints can retrieve legal context.
- Prompt Engine → OpenAI Client: feeds · Prompt assembly drives model requests.
- RAG Stack → OpenAI Client: feeds · Retrieved context is passed into generation.
- RAG Stack → Domain Models: consults · Retrieval searches legal and template knowledge stored in models.
- Auth API → Domain Models: verifies · Authentication and user records meet at the data layer.
- Auth API → Session Provider: hydrates · Session state is consumed by the client provider.
