Overview
An AI-powered document Generator built on Next.js that helps users create, edit, and manage documents from predefined templates and AI-assisted generation. It centralizes document creation workflows, template management, and AI generation while persisting data in MongoDB with secure, session-based access control.What it does (Capabilities)
- Document creation and editing: Create new documents, edit content in rich-text/HTML/Markdown editors, and manage updates.
- Template library: Browse and fetch predefined templates for common document types (e.g., resumes, reports).
- AI generation: Generate document content via OpenAI-powered endpoints.
- Document storage & retrieval: Persist documents to MongoDB; fetch by ID.
- Chat history storage: Save and retrieve chat interactions related to AI generation.
- Session-based access control: Validate access via session middleware and server-side checks.
- Export utilities: Utilities for exporting or transforming content.
User Flows
- Start from a template: Select a template → open in editor → customize and save.
- Create from scratch: Open a blank editor → compose content → save as a new document.
- AI-assisted creation: Provide prompts/inputs → trigger generation → refine in the editor → save.
- Revisit or share: Open existing document by ID → edit or export as needed.
- Review history: Retrieve chat history for context and iteration.
Architecture
- UI (Next.js App Router): Pages under
app/
render the editor, template browsing, and session-aware views. Editors include rich-text, HTML, Markdown, and specialized layouts. - API routes (Next.js): REST-like endpoints under
app/api/
handle templates, documents, generation, chat history, auth checks, and health checks. - Data store (MongoDB via Mongoose): Models in
models/
defineUser
,Document
,Template
, andChatHistory
. - External services (OpenAI): AI generation and health checks via OpenAI API.
- Session layer (iron-session): Session options and helpers in
app/config/ironOption.tsx
andapp/config/withSession.tsx
;middleware.ts
supports session-aware routing. - Utilities and export: Helpers in
lib/
for export, utils, and Mongo connection URIs. HTML template helpers inhtml/
.
Technical Design
- Editors: Rich text, Markdown, HTML, font size selector, two-column layout.
- Templates module: Catalog of TypeScript/TSX templates with API access.
- Documents module: CRUD operations over documents.
- AI generation module: Integrates with OpenAI; health checks.
- Chat history module: Stores and fetches interactions.
- Session & access control: Iron Session config; access enforcement.
- Data access layer: Mongo connection pooling and helper functions.
- Security & validation: Session-based auth, scoped access checks.
- Pipelines: Not applicable.
API Reference
GET /api/templates
- List or fetch available templates.POST /api/generate
- Generate content via OpenAI.GET /api/check-openai
- Health check for OpenAI.GET /api/documents
- List/query documents.POST /api/documents
- Create a document.GET /api/documents/[id]
- Retrieve a document.PATCH /api/documents/[id]
- Update a document.DELETE /api/documents/[id]
- Delete a document.GET /api/chat-history
- Retrieve chat history.POST /api/chat-history
- Store chat history.GET /api/auth/check-access
- Validate session-based access.
Data and Schemas (Conceptual)
- User: Authenticated session owner.
- Template: Metadata and content for predefined structures.
- Document: Saved artifact of user content.
- ChatHistory: AI prompts/responses for context and iteration.
Troubleshooting
Troubleshooting Guide
- Document Creation Issues
- Editor & Export Problems
- AI Generation Errors
- API & Authentication Issues
- Session & Access Problems
- Performance & Cost Optimization
Cannot create or save a document
Cannot create or save a document
Symptoms
- Save button fails, API errors, document missing after creation.
- Missing session, MongoDB connection issues, invalid payload.
- Re-authenticate, check MongoDB config, validate payload.
Document not found by ID
Document not found by ID
Symptoms
- 404 errors, broken links.
- ID mismatch, deleted document, incorrect URL.
- Verify ID, re-copy URL, recreate document.
Template does not load
Template does not load
Symptoms
- Empty list, API errors.
- Missing seed data, template build errors.
- Seed templates, fix syntax, return metadata.
Tech Stack
Layer | Technologies | Purpose |
---|---|---|
Frontend UI | Next.js App Router, React, TypeScript, Tailwind CSS | Pages, editors, styling |
Backend API | Next.js API routes | Handle templates, documents, generation |
Database | MongoDB, Mongoose | Persist users, documents, templates, chats |
Auth & Sessions | iron-session | Session-based access control |
AI/ML | OpenAI API | Text generation |
Build & Package | pnpm, TypeScript, PostCSS | Build tooling |
Styling | Tailwind CSS, globals.css | Utility-first styling |
Utilities | lib/* | Helpers and export routines |
Seeding | scripts/seed-templates.js | Populate template catalog |
Best Practices
- Scope all data by session.
- Reuse MongoDB connections.
- Validate input before persisting.
- Include template structure in AI prompts.
- Stick to one editor mode per document.
- Paginate and index lists/histories.
- Fail fast on external calls.
- Prefer idempotent updates.