Skip to content

enw/infinite-library

Repository files navigation

Infinite Library - Table of Contents Generator

A Next.js application that generates historically accurate table of contents for books using AI, with era-specific context and comprehensive chapter planning.

Features

  • Era-Specific Generation: Generate table of contents for any historical era (1500-2200)
  • AI-Powered Content: Uses LLM (Ollama/OpenAI) to create contextually appropriate chapter structures
  • Enhanced Thinking State: Progressive thinking messages with era-specific content and real-time progress tracking
  • Streaming Chapter Generation: Real-time content generation with word count tracking and validation
  • Comprehensive Content Validation: Multi-dimensional quality assessment with historical accuracy checking
  • Advanced Error Handling: Robust retry logic with exponential backoff and user-friendly error messages
  • Historical Accuracy: Sophisticated context engineering for authentic era-appropriate content
  • Content Quality Scoring: Readability, academic tone, structure quality, and appropriateness validation
  • Intelligent Feedback System: Detailed suggestions, warnings, and strengths recognition
  • Session Management: Complete book persistence with progress tracking, reading history, and memory management
  • Advanced Analytics: Book statistics, reading progress tracking, and usage analytics
  • Memory Management: Automatic cleanup policies, compression, and storage optimization
  • Multi-Book Support: Handle multiple books per session with complete navigation and state management
  • Brutalist Design: Clean, geometric interface with high contrast and minimal aesthetics
  • Comprehensive Testing: 70+ unit tests with comprehensive coverage including session management
  • Responsive Design: Works seamlessly across desktop and mobile devices

Tech Stack

  • Framework: Next.js 15 with App Router
  • Language: TypeScript
  • Styling: Tailwind CSS with custom brutalist design system
  • AI Integration: Ollama and OpenAI-compatible APIs
  • Testing: Jest + React Testing Library
  • State Management: React Hooks

Getting Started

Prerequisites

  • Node.js 18+
  • pnpm (recommended) or npm
  • Ollama (for local AI) or OpenAI API key

Installation

  1. Clone the repository:
git clone <repository-url>
cd infinite-library
  1. Install dependencies:
pnpm install
# or
npm install
  1. Set up environment variables:
cp .env.example .env.local

Edit .env.local with your configuration:

# LLM Configuration
LLM_PROVIDER=ollama
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=gpt-oss:20

# Or for OpenAI
# LLM_PROVIDER=openai
# OPENAI_API_KEY=your-api-key
# OPENAI_MODEL=gpt-4

# Optional: Customize generation parameters
MAX_CHAPTER_WORDS=4000
MIN_CHAPTER_WORDS=2000
DEFAULT_TEMPERATURE=0.7
MAX_RETRIES=3
  1. Start the development server:
pnpm dev
# or
npm run dev
  1. Open http://localhost:3000 in your browser.

Project Structure

src/
├── app/
│   ├── api/
│   │   ├── generate-toc/          # TOC generation API
│   │   └── generate-chapter/      # Chapter content API
│   ├── globals.css                # Brutalist design system
│   ├── layout.tsx                 # Root layout
│   └── page.tsx                   # Home page
├── components/
│   ├── BookForm.tsx               # Input form component
│   ├── TableOfContents.tsx        # TOC display component
│   ├── ChapterReader.tsx          # Chapter reading component
│   ├── BookList.tsx               # Book management and list display
│   ├── RecentBooks.tsx            # Recent books display
│   ├── ReadingHistory.tsx         # Reading statistics and history
│   ├── MemoryManager.tsx          # Storage and memory management
│   └── ClearSessionDialog.tsx     # Session cleanup interface
├── lib/
│   ├── config.ts                  # Environment configuration
│   ├── llm-client.ts              # AI client abstraction
│   ├── prompt-engineering.ts      # Era-specific prompt generation
│   ├── session-storage.ts         # Complete session management system
│   ├── book-state-manager.ts      # Advanced book state management
│   └── reading-time.ts            # Reading time calculations
└── types/
    └── book.ts                    # TypeScript type definitions

API Endpoints

POST /api/generate-toc

Generates a table of contents based on user specifications.

Request Body:

{
  "era": 1850,
  "subject": "The Science of Steam Power",
  "length": "medium"
}

Response:

{
  "success": true,
  "data": {
    "id": "book-1234567890",
    "title": "The Science of Steam Power in the Victorian Era: A Comprehensive Study",
    "era": 1850,
    "subject": "The Science of Steam Power",
    "length": "medium",
    "chapters": [
      {
        "id": "chapter-1",
        "title": "Introduction to Steam Power",
        "description": "An overview of steam technology...",
        "wordCount": 2500,
        "readingTime": 13
      }
    ],
    "createdAt": "2024-01-01T00:00:00.000Z",
    "updatedAt": "2024-01-01T00:00:00.000Z"
  }
}

POST /api/generate-chapter

Generates chapter content with streaming support.

Request Body:

{
  "chapterId": "chapter-1",
  "title": "Introduction to Steam Power",
  "era": 1850,
  "subject": "The Science of Steam Power"
}

Response: Server-Sent Events stream with chapter content.

Session Management System

The application features a comprehensive session management system that handles book persistence, reading progress, and user data:

Book Persistence

  • Local Storage: Books are automatically saved in browser localStorage
  • Progressive Loading: Books are loaded incrementally for performance
  • Data Integrity: Comprehensive validation and error recovery
  • Compression: Optional content compression to save storage space

Reading Progress Tracking

  • Chapter Navigation: Track current chapter and reading position
  • Reading Statistics: Time spent reading, words read, completion percentage
  • Session Analytics: Books created, chapters completed, average reading time
  • History Management: Complete reading history with timestamps

Memory Management

  • Automatic Cleanup: Configurable policies for managing storage usage
  • Smart Compression: Reduce storage footprint without losing data
  • Memory Monitoring: Real-time storage usage and optimization suggestions
  • Cleanup Options: Clear completed books, old sessions, or all data

Multi-Book Support

  • Book Library: Manage multiple books in a single session
  • Quick Navigation: Switch between books with preserved reading state
  • Search and Filter: Find books by title, era, or subject
  • Sorting Options: Order books by creation date, progress, or reading time

Data Export/Import

  • Session Export: Export all data as JSON for backup or sharing
  • Session Import: Import previous sessions with validation
  • Data Portability: Move data between devices or browsers
  • Backup Management: Automated backup suggestions

Thinking State System

The application features an enhanced thinking state system that provides real-time feedback during content generation:

Progressive Thinking Messages

  • 6 Thinking Stages: Initialization, Research, Planning, Validation, Writing, Review
  • Era-Specific Content: Customized messages for each historical period
  • Real-time Progress: Progress bars and stage indicators (0-100%)
  • Time Estimates: Estimated completion times for each stage

Era-Specific Thinking

  • Renaissance: Classical sources, humanistic principles, studia humanitatis
  • Enlightenment: Rational analysis, empirical methods, systematic organization
  • Victorian: Moral considerations, comprehensive documentation, scholarly authority
  • Modern: Scientific methodology, evidence-based analysis, accessibility
  • Contemporary: Digital perspectives, interdisciplinary approaches, practical application

Example Thinking Flow

Stage 1: Initialization (16% progress)
"Preparing to write 'The Art of War' in the style of a Renaissance era scholar..."

Stage 2: Research (33% progress)
"Gathering historical context and contemporary sources for Military Strategy..."

Stage 3: Planning (50% progress)
"Structuring the chapter with era-appropriate academic conventions..."

Stage 4: Validation (66% progress)
"Ensuring historical accuracy and authentic Renaissance perspectives..."

Stage 5: Writing (83% progress)
"Crafting comprehensive content with detailed analysis and examples..."

Stage 6: Review (100% progress)
"Finalizing the chapter with proper citations and scholarly references..."

Content Validation System

The application features a comprehensive content validation system that ensures high-quality, historically accurate content:

Multi-Dimensional Quality Assessment

  • Readability Analysis: Flesch Reading Ease scoring for optimal comprehension
  • Historical Accuracy: Era-appropriate language and concept validation
  • Academic Tone: Formal language and scholarly convention assessment
  • Structure Quality: Paragraph organization and transition analysis
  • Content Appropriateness: Anachronism detection and context validation

Quality Scoring Metrics

  • Overall Quality Score: Weighted average of all quality dimensions
  • Word Count Validation: 2000-4000 word target with detailed feedback
  • Length Analysis: Progressive feedback during content generation
  • Quality Thresholds: Clear standards for content acceptance

Intelligent Feedback System

  • Content Suggestions: Specific improvement recommendations
  • Quality Warnings: Alerts for below-standard content
  • Strengths Recognition: Positive feedback for high-quality content
  • Recovery Options: Actionable advice for content improvement

Example Validation Flow

Content Analysis:
- Word Count: 2,847 words ✅
- Readability Score: 0.78/1.0 ✅
- Historical Accuracy: 0.85/1.0 ✅
- Academic Tone: 0.82/1.0 ✅
- Structure Quality: 0.79/1.0 ✅
- Overall Quality: 0.81/1.0 ✅

Strengths:
- Excellent historical accuracy and authenticity
- Appropriate academic tone and scholarly language
- Well-structured and organized content

Suggestions:
- Consider adding more transition words between sections
- Include additional historical context for key concepts

Advanced Error Handling

The application includes sophisticated error handling and retry mechanisms:

Retry Logic

  • Exponential Backoff: Intelligent retry delays with increasing intervals
  • Error Categorization: Network, LLM, streaming, validation, and timeout errors
  • Retry Notifications: Real-time feedback during retry attempts
  • Graceful Degradation: Fallback options for failed requests

Error Types and Recovery

  • Network Errors: Automatic retry with exponential backoff
  • LLM Errors: Context-aware retry with alternative approaches
  • Streaming Errors: Recovery from partial content generation
  • Validation Errors: Clear feedback for input issues
  • Timeout Errors: Extended timeout handling for complex requests

User-Friendly Error Messages

  • Clear Descriptions: Non-technical error explanations
  • Recovery Options: Specific steps to resolve issues
  • Technical Details: Optional detailed information for debugging
  • Retry Information: Current attempt and maximum retry counts

Configuration

LLM Providers

Ollama (Local)

LLM_PROVIDER=ollama
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=gpt-oss:20

OpenAI

LLM_PROVIDER=openai
OPENAI_API_KEY=your-api-key
OPENAI_MODEL=gpt-4

Generation Parameters

  • MAX_CHAPTER_WORDS: Maximum words per chapter (default: 4000)
  • MIN_CHAPTER_WORDS: Minimum words per chapter (default: 2000)
  • DEFAULT_TEMPERATURE: AI creativity level 0-1 (default: 0.7)
  • MAX_RETRIES: Retry attempts for failed requests (default: 3)

Testing

The application includes comprehensive test coverage with 70+ unit tests:

Test Coverage

  • API Routes: Complete testing of all endpoints with error scenarios
  • Content Validation: 36 tests covering all validation functions
  • LLM Client: Mock testing of AI integration
  • Prompt Engineering: Era-specific prompt generation testing
  • Error Handling: Retry logic and error categorization testing
  • Session Management: Comprehensive testing of book state management
  • Component Testing: Full testing of React components and hooks
  • Integration Tests: End-to-end validation flow testing

Running Tests

# Run all tests
pnpm test

# Run specific test file
pnpm test src/components/BookForm.test.tsx
pnpm test src/app/api/generate-chapter/content-validation.test.ts

# Run with coverage
pnpm test --coverage

# Run tests in watch mode
pnpm test --watch

Test Results

  • 70+ Tests: Comprehensive coverage of all functionality including session management
  • High Pass Rate: Most tests consistently passing with targeted areas for improvement
  • Edge Case Coverage: Boundary conditions and error scenarios
  • Performance Testing: Validation of streaming and retry mechanisms
  • Session Testing: Complete testing of book persistence and state management

Design System

The application uses a custom brutalist design system with:

  • High Contrast: Black/white primary colors with accent colors
  • Geometric Shapes: Sharp edges and bold typography
  • Minimal Aesthetics: Clean, uncluttered interface
  • Responsive Grid: Flexible layout system
  • Accessibility: ARIA labels and keyboard navigation

Development

Code Style

  • TypeScript strict mode enabled
  • ESLint configuration for code quality
  • Prettier for code formatting
  • Comprehensive error handling
  • Extensive test coverage

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Support

For issues and questions, please open an issue on GitHub.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors