backend-cal/
βββ backend/ # Backend Node.js API
β βββ src/
β β βββ controllers/ # Route controllers
β β β βββ imageController.js
β β β βββ analysisController.js
β β βββ middleware/ # Custom middleware
β β β βββ auth.js
β β β βββ errorHandler.js
β β β βββ validation.js
β β βββ models/ # Database models
β β β βββ ImageModel.js
β β βββ routes/ # API routes
β β β βββ image.js
β β β βββ analysis.js
β β βββ services/ # Business logic
β β β βββ ollamaService.js
β β β βββ imageProcessor.js
β β β βββ cacheService.js
β β βββ utils/ # Utility functions
β β β βββ logger.js
β β β βββ helpers.js
β β βββ config/ # Configuration
β β βββ database.js
β β βββ ollama.js
β βββ tests/ # Test files
β βββ uploads/ # File uploads
β βββ package.json
β βββ server.js # Entry point
βββ frontend/ # Frontend React App
β βββ src/
β β βββ components/ # Reusable components
β β β βββ ui/ # Basic UI components
β β β β βββ Button.tsx
β β β β βββ Input.tsx
β β β β βββ index.ts
β β β βββ canvas/ # Canvas-related components
β β β β βββ FabricCanvas.tsx
β β β β βββ Toolbar.tsx
β β β β βββ index.ts
β β β βββ analysis/ # Analysis components
β β β β βββ ResultDisplay.tsx
β β β β βββ LoadingState.tsx
β β β β βββ index.ts
β β β βββ layout/ # Layout components
β β β βββ Navbar.tsx
β β β βββ Sidebar.tsx
β β β βββ index.ts
β β βββ screens/ # Page components
β β β βββ Home/
β β β β βββ index.tsx
β β β β βββ Home.styles.ts
β β β βββ About/
β β βββ hooks/ # Custom React hooks
β β β βββ useCanvas.ts
β β β βββ useAnalysis.ts
β β β βββ useLocalStorage.ts
β β βββ services/ # API services
β β β βββ api.ts
β β β βββ imageService.ts
β β β βββ analysisService.ts
β β βββ store/ # State management
β β β βββ context/
β β β β βββ AppContext.tsx
β β β β βββ CanvasContext.tsx
β β β βββ slices/
β β β βββ canvasSlice.ts
β β β βββ analysisSlice.ts
β β βββ types/ # TypeScript types
β β β βββ canvas.ts
β β β βββ analysis.ts
β β β βββ api.ts
β β βββ utils/ # Utility functions
β β β βββ constants.ts
β β β βββ helpers.ts
β β β βββ validators.ts
β β βββ styles/ # Global styles
β β β βββ globals.css
β β β βββ components.css
β β βββ config/ # Configuration
β β βββ api.ts
β β βββ env.ts
β βββ public/
β βββ package.json
βββ shared/ # Shared types and utilities
β βββ types/
β β βββ analysis.ts
β β βββ api.ts
β βββ constants/
β βββ errors.ts
βββ docs/ # Documentation
βββ scripts/ # Build and deployment scripts
βββ README.md
Client Request β Express Server β Middleware β Controller β Service β Ollama AI β Response
β
MongoDB (Image Storage)
β
In-Memory Cache
Key Components:
-
Express Server (
server.js)- Main entry point
- Sets up middleware and routes
- Handles error management
-
Controllers (
controllers/)- Handle HTTP requests/responses
- Validate input data
- Call appropriate services
-
Services (
services/)- OllamaService: Communicates with Ollama AI model
- ImageProcessor: Optimizes and processes images
- CacheService: Manages in-memory caching
-
Models (
models/)- ImageModel: MongoDB schema for image storage
User Interaction β Component β Hook/Context β API Service β Backend API β Response β State Update β UI Re-render
Key Components:
-
Components (
src/components/)- Canvas Components: Drawing and canvas management
- Analysis Components: Results and loading states
- UI Components: Reusable UI elements
-
Hooks (
src/hooks/)- useCanvas: Canvas drawing logic
- useAnalysis: Analysis state management
- useLocalStorage: Local storage operations
-
Services (
src/services/)- API Service: HTTP client for backend communication
- Image Service: Image upload and processing
- Analysis Service: Analysis requests and responses
-
State Management (
src/store/)- Context API: Global state management
- Local State: Component-specific state
User draws on canvas β Fabric.js captures drawing β Canvas state saved β Ready state shown
User clicks calculate β Loading state shown β Image sent to backend β
Backend processes image β Ollama AI analyzes β Results returned β
Frontend displays results with explanations
Canvas State β Drawing history β Undo/Redo functionality
Analysis State β Loading/Ready/Result states β UI updates
- Node.js + Express: Server framework
- MongoDB: Image storage
- Ollama: AI model (Qwen2.5-VL)
- Sharp: Image processing
- Axios: HTTP client
- React 19: UI framework
- TypeScript: Type safety
- Fabric.js: Canvas manipulation
- Tailwind CSS: Styling
- Lucide React: Icons
- Vite: Build tool
- Uploads canvas image
- Returns image ID
- Stores in MongoDB
- Analyzes image with AI
- Returns mathematical results
- Includes explanations and steps
- Returns cache statistics
- For monitoring and debugging
- Error Handling: Centralized error management
- Validation: Input validation for all endpoints
- Caching: In-memory caching for performance
- Logging: Comprehensive logging for debugging
- Type Safety: Strict TypeScript usage
- Component Reusability: Modular component design
- State Management: Proper state separation
- Performance: Optimized rendering and caching
- Input Validation: All user inputs validated
- File Uploads: Size and type restrictions
- Error Messages: No sensitive information leaked
- Rate Limiting: Prevent API abuse
- Image Optimization: Sharp for image compression
- Caching: In-memory cache for repeated requests
- Lazy Loading: Components loaded as needed
- Code Splitting: Frontend bundle optimization
- Unit Tests: Individual function testing
- Integration Tests: API endpoint testing
- E2E Tests: Full user flow testing
- Performance Tests: Load and stress testing
This architecture ensures scalability, maintainability, and performance for the AI Math Calculator application.