How danish884803/ai-platform works
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: Document.js Domain Models · page.jsx Dashboard · ChatBox.jsx Feature UI · route.js Drafting API
Components 22
Client UI
Public pages and reusable client-side interaction components.
- Landing · Public entry and marketing page.
app/page.js - Auth Pages · Login and registration entry points.
app/login/page.js - Dashboard · Authenticated hub for user workflows.
app/dashboard/page.jsx - Chat · Conversational assistance for drafting legal content.
app/chat/page.js - Generate · Structured document generation from form input.
app/generate/page.js - Editor · Refine, revise, and manage saved documents.
app/editor/page.js - Feature UI · Reusable chat, form, editor, layout, and landing components.
components/ChatBox.jsx - Session Provider · Client-side auth/session context shared across the app.
components/providers/SessionProvider.jsx - Language Provider · 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 · Authentication and session backend boundary.
app/api/auth/[...nextauth]/route.js - Drafting API · Chat, generate, and rewrite endpoints.
app/api/chat/route.js - Documents API · Create, read, update, pin, and save document records.
app/api/documents/route.js - Export API · PDF and Word export endpoints.
app/api/export/pdf/route.js - Template API · Public template retrieval endpoint.
app/api/templates/route.js - Admin API · 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 · Builds prompt text from stored definitions and localization.
lib/getPrompt.js - RAG Stack · Embeddings, search, and context assembly over legal knowledge.
lib/rag.js - OpenAI Client · Provider integration for generated and rewritten content.
lib/openai.js
Data Layer
MongoDB connection and persisted domain entities.
- MongoDB Conn · Shared database connection layer.
lib/mongodb.js - Domain Models · Document, Law, Prompt, Template, and User persistence models.
models/Document.js
Admin Console
Management pages for laws, prompts, templates, and users.
- Admin Console · Admin pages for governance and content operations.
app/admin/page.js - Prompt Editor · UI for editing AI prompts.
components/admin/PromptEditor.jsx
Connections 27
- 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.