ALwrity Version 0.5.0 (Fastapi + React )

This commit is contained in:
ajaysi
2025-08-06 12:48:02 +05:30
parent f28a919caa
commit 32f97fa6b3
476 changed files with 115544 additions and 28747 deletions

View File

@@ -0,0 +1,302 @@
# Modular Design System: Alwrity Onboarding
## 🎯 **Overview**
This document outlines the modular design system for Alwrity's onboarding flow, ensuring **consistency**, **reusability**, and **maintainability** across all onboarding steps while preserving all current functionality and styling.
---
## **🏗️ Architecture**
### **Core Components**
```
frontend/src/components/OnboardingWizard/
├── common/
│ ├── useOnboardingStyles.ts # Centralized styling hook
│ ├── onboardingUtils.ts # Shared utility functions
│ ├── OnboardingStepLayout.tsx # Reusable layout component
│ ├── OnboardingCard.tsx # Reusable card component
│ └── OnboardingButton.tsx # Reusable button component
├── ApiKeyStep.tsx # Refactored to use design system
├── WebsiteStep.tsx # Will be refactored
├── ResearchStep.tsx # Will be refactored
├── PersonalizationStep.tsx # Will be refactored
├── IntegrationsStep.tsx # Will be refactored
└── FinalStep.tsx # Will be refactored
```
---
## **🎨 Design System Components**
### **1. useOnboardingStyles Hook**
**Purpose**: Centralized styling for all onboarding components
**Benefits**:
- ✅ Consistent styling across all steps
- ✅ Easy to maintain and update
- ✅ Theme-aware styling
- ✅ Reusable style objects
```typescript
// Usage in any step component
const styles = useOnboardingStyles();
// Apply consistent styling
<Box sx={styles.container}>
<Typography sx={styles.headerTitle}>Title</Typography>
<Button sx={styles.primaryButton}>Action</Button>
</Box>
```
### **2. onboardingUtils Functions**
**Purpose**: Shared utility functions for common operations
**Benefits**:
- ✅ DRY (Don't Repeat Yourself) principle
- ✅ Consistent validation logic
- ✅ Reusable animation utilities
- ✅ Standardized error handling
```typescript
// Validation utilities
const isValid = validateApiKey(key, 'openai');
const status = getKeyStatus(key, 'openai');
// Animation utilities
const delay = getAnimationDelay(index);
const direction = getSlideDirection(current, target);
// Form utilities
const isValid = isFormValid(formValues);
const progress = calculateProgress(current, total);
```
### **3. OnboardingStepLayout Component**
**Purpose**: Consistent layout structure for all steps
**Benefits**:
- ✅ Standardized header structure
- ✅ Consistent spacing and typography
- ✅ Reusable animations
- ✅ Flexible content area
```typescript
<OnboardingStepLayout
icon={<Key />}
title="Connect Your AI Services"
subtitle="Add your API keys to enable AI-powered content creation"
>
{/* Step-specific content */}
</OnboardingStepLayout>
```
### **4. OnboardingCard Component**
**Purpose**: Consistent card styling with status indicators
**Benefits**:
- ✅ Standardized card appearance
- ✅ Built-in status validation
- ✅ Consistent hover effects
- ✅ Reusable across all steps
```typescript
<OnboardingCard
title="OpenAI API Key"
icon={<Security />}
status={getKeyStatus(key, 'openai')}
saved={!!savedKeys.openai}
>
<TextField value={key} onChange={handleChange} />
</OnboardingCard>
```
---
## **🔧 Implementation Guidelines**
### **1. Step Component Structure**
Every onboarding step should follow this structure:
```typescript
import { useOnboardingStyles } from './common/useOnboardingStyles';
import { relevantUtils } from './common/onboardingUtils';
const StepComponent: React.FC<StepProps> = ({ onContinue }) => {
const styles = useOnboardingStyles();
// State management
const [formData, setFormData] = useState({});
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
// Validation
const isValid = isFormValid(formData);
// Event handlers
const handleSave = async () => {
// Implementation
};
return (
<Fade in={true} timeout={500}>
<Box sx={styles.container}>
{/* Header */}
<Box sx={styles.header}>
<Zoom in={true} timeout={600}>
{/* Header content */}
</Zoom>
</Box>
{/* Form content */}
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
{/* Cards and form elements */}
</Box>
{/* Alerts */}
{/* Action buttons */}
{/* Skip section */}
</Box>
</Fade>
);
};
```
### **2. Styling Guidelines**
- **Use the styles hook**: Always use `useOnboardingStyles()` for styling
- **Consistent spacing**: Use the predefined spacing values
- **Theme integration**: Leverage Material-UI theme for colors
- **Responsive design**: Use the responsive breakpoints
### **3. Animation Guidelines**
- **Fade in**: Use `Fade` component for step transitions
- **Zoom effects**: Use `Zoom` for important elements
- **Slide transitions**: Use `Slide` for step navigation
- **Consistent timing**: Use predefined timeouts (300ms, 500ms, 700ms)
### **4. Validation Guidelines**
- **Real-time validation**: Use debounced validation for better UX
- **Visual feedback**: Show status chips and border colors
- **Error handling**: Use `formatErrorMessage` for consistent error messages
- **Form validation**: Use `isFormValid` for form completeness
---
## **📋 Component Checklist**
### **For Each Step Component**
- [ ] **Import design system**: Use `useOnboardingStyles` and relevant utilities
- [ ] **Consistent structure**: Follow the standard component structure
- [ ] **Proper animations**: Use `Fade`, `Zoom`, and `Slide` components
- [ ] **Form validation**: Implement real-time validation with visual feedback
- [ ] **Error handling**: Use `formatErrorMessage` for error display
- [ ] **Loading states**: Show loading indicators during async operations
- [ ] **Auto-save**: Implement auto-save functionality where appropriate
- [ ] **Skip options**: Provide skip functionality for optional steps
- [ ] **Help sections**: Include collapsible help content
- [ ] **Responsive design**: Ensure mobile-friendly layout
### **For New Components**
- [ ] **Reusable design**: Make components generic and reusable
- [ ] **Props interface**: Define clear TypeScript interfaces
- [ ] **Default values**: Provide sensible defaults
- [ ] **Documentation**: Add JSDoc comments
- [ ] **Testing**: Include unit tests for utilities
---
## **🎯 Benefits of This System**
### **1. Consistency**
-**Visual consistency**: All steps look and feel the same
-**Behavior consistency**: Same interactions across all steps
-**Animation consistency**: Standardized transitions and effects
-**Error handling**: Consistent error messages and recovery
### **2. Reusability**
-**Shared components**: Common components used across steps
-**Shared utilities**: Validation, animation, and form utilities
-**Shared styles**: Centralized styling system
-**Shared logic**: Common business logic extracted to utilities
### **3. Maintainability**
-**Single source of truth**: Styles and utilities in one place
-**Easy updates**: Change once, affects all components
-**Clear structure**: Consistent file and component organization
-**Type safety**: Full TypeScript support with proper interfaces
### **4. Performance**
-**Optimized animations**: Efficient animation utilities
-**Debounced operations**: Prevent excessive API calls
-**Lazy loading**: Components load only when needed
-**Memory management**: Proper cleanup in useEffect hooks
---
## **🚀 Migration Strategy**
### **Phase 1: Foundation (Complete)**
- ✅ Create design system components
- ✅ Implement utility functions
- ✅ Create styling hook
- ✅ Refactor ApiKeyStep as example
### **Phase 2: Component Migration**
- [ ] Refactor WebsiteStep
- [ ] Refactor ResearchStep
- [ ] Refactor PersonalizationStep
- [ ] Refactor IntegrationsStep
- [ ] Refactor FinalStep
### **Phase 3: Enhancement**
- [ ] Add more utility functions as needed
- [ ] Create additional reusable components
- [ ] Implement advanced animations
- [ ] Add accessibility features
### **Phase 4: Testing & Optimization**
- [ ] Add unit tests for utilities
- [ ] Add integration tests for components
- [ ] Performance optimization
- [ ] Accessibility audit
---
## **📚 Usage Examples**
### **Creating a New Step**
```typescript
// 1. Import design system
import { useOnboardingStyles } from './common/useOnboardingStyles';
import { validateRequired, formatErrorMessage } from './common/onboardingUtils';
// 2. Use the styles hook
const styles = useOnboardingStyles();
// 3. Implement consistent structure
const NewStep: React.FC<StepProps> = ({ onContinue }) => {
// State and logic
return (
<Fade in={true} timeout={500}>
<Box sx={styles.container}>
{/* Header */}
{/* Content */}
{/* Actions */}
</Box>
</Fade>
);
};
```
### **Adding New Utilities**
```typescript
// Add to onboardingUtils.ts
export const validateEmail = (email: string): boolean => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
};
export const formatPhoneNumber = (phone: string): string => {
// Implementation
};
```
---
**This modular design system ensures that all onboarding steps are consistent, maintainable, and provide an excellent user experience while reducing development time and improving code quality.**

View File

@@ -0,0 +1,194 @@
# Alwrity Migration Guide: From Streamlit to React + FastAPI
## Overview
This guide explains how to migrate from the current Streamlit-based `alwrity.py` to the new React + FastAPI architecture while maintaining all present functionality.
---
## Architecture Changes
### Before (Streamlit)
```
alwrity.py (Streamlit app)
├── Onboarding (API key setup)
├── Main UI (sidebar navigation)
└── All features (AI writers, SEO tools, etc.)
```
### After (React + FastAPI)
```
Backend (FastAPI)
├── backend/main.py (replaces alwrity.py)
├── backend/api/onboarding.py (onboarding endpoints)
├── backend/services/api_key_manager.py (API key management)
└── backend/models/onboarding.py (database models)
Frontend (React)
├── frontend/src/App.tsx (main app with onboarding check)
├── frontend/src/components/OnboardingWizard/ (onboarding flow)
└── frontend/src/components/MainApp.tsx (main application)
```
---
## Key Changes
### 1. **alwrity.py → backend/main.py**
- **Before**: Single Streamlit file handling everything
- **After**: FastAPI backend with separate React frontend
- **Maintained**: All environment setup, API key checking, logging
### 2. **Onboarding Flow**
- **Before**: Streamlit-based onboarding in `alwrity.py`
- **After**: React wizard with FastAPI backend
- **Maintained**: Same onboarding steps and validation logic
### 3. **Application Flow**
- **Before**: Direct access to all features after onboarding
- **After**: Onboarding check → React wizard (first-time) → Main app (returning users)
- **Maintained**: All existing functionality preserved
---
## How to Run the New Architecture
### Option 1: Development Mode
```bash
# Terminal 1: Start FastAPI backend
cd backend
python main.py
# Backend runs on http://localhost:8000
# Terminal 2: Start React frontend
cd frontend
npm start
# Frontend runs on http://localhost:3000
```
### Option 2: Production Mode
```bash
# Build React app
cd frontend
npm run build
# Serve with FastAPI
cd backend
python main.py
# Both frontend and backend served from http://localhost:8000
```
---
## Migration Steps
### Phase 1: Backend Setup ✅
1. ✅ Extract API key management to `backend/services/api_key_manager.py`
2. ✅ Create FastAPI onboarding endpoints in `backend/api/onboarding.py`
3. ✅ Set up database models in `backend/models/onboarding.py`
4. ✅ Create main FastAPI app in `backend/main.py`
### Phase 2: Frontend Setup ✅
1. ✅ Create React onboarding wizard
2. ✅ Implement API integration
3. ✅ Create main app structure
4. ✅ Set up onboarding flow
### Phase 3: Feature Migration (Next Steps)
1. **Migrate AI Writers**: Wrap existing AI writer modules as FastAPI endpoints
2. **Migrate SEO Tools**: Create API endpoints for SEO functionality
3. **Migrate UI Components**: Convert Streamlit UI to React components
4. **Add Authentication**: Implement user management and sessions
---
## Maintaining Present Functionality
### ✅ Preserved Features
- **API Key Management**: Same validation and storage logic
- **Onboarding Flow**: Same steps, improved UI
- **Environment Setup**: All paths and configurations preserved
- **Logging**: Same logging configuration
- **Error Handling**: Enhanced with better user feedback
### 🔄 Enhanced Features
- **UI/UX**: Modern React interface with Material-UI
- **Performance**: Faster loading and better responsiveness
- **Scalability**: Backend can handle multiple users
- **Maintainability**: Separated concerns, easier to extend
---
## API Endpoints
### Onboarding Endpoints
- `GET /api/check-onboarding` - Check if onboarding is required
- `POST /api/onboarding/start` - Start onboarding session
- `GET /api/onboarding/step` - Get current step
- `POST /api/onboarding/step` - Set current step
- `GET /api/onboarding/api-keys` - Get saved API keys
- `POST /api/onboarding/api-keys` - Save API key
- `GET /api/onboarding/progress` - Get onboarding progress
- `POST /api/onboarding/progress` - Set onboarding progress
### Application Endpoints
- `GET /api/status` - Get application status
- `GET /health` - Health check
---
## Development Workflow
### For First-Time Users
1. User visits application
2. `App.tsx` checks onboarding status via `/api/check-onboarding`
3. If onboarding required → Show `Wizard.tsx`
4. User completes 6-step onboarding process
5. On completion → Switch to `MainApp.tsx`
### For Returning Users
1. User visits application
2. `App.tsx` checks onboarding status
3. If onboarding complete → Show `MainApp.tsx` directly
4. User accesses all features
---
## Next Steps
### Immediate
1. **Test the onboarding flow** end-to-end
2. **Install dependencies** for React and FastAPI
3. **Configure development environment**
### Short-term
1. **Migrate AI Writers** to FastAPI endpoints
2. **Create React components** for main features
3. **Add authentication** and user management
### Long-term
1. **Add enterprise features** (SSO, multi-user, audit)
2. **Optimize performance** and scalability
3. **Add advanced features** (real-time collaboration, etc.)
---
## Troubleshooting
### Common Issues
1. **CORS errors**: Ensure CORS middleware is configured
2. **API connection errors**: Check backend is running on correct port
3. **Database errors**: Ensure SQLite database is created
4. **React build errors**: Install all required dependencies
### Dependencies Required
```bash
# Backend
pip install fastapi uvicorn sqlalchemy python-dotenv
# Frontend
npm install react @mui/material @mui/icons-material axios
```
---
**The migration maintains all present functionality while providing a modern, scalable foundation for enterprise features.**

View File

@@ -0,0 +1,127 @@
AI Writers
=========
This section documents the AI writer modules that provide specialized content generation for different platforms.
LinkedIn Writer
-------------
.. automodule:: lib.ai_writers.linkedin_writer
:members:
:undoc-members:
:show-inheritance:
LinkedIn Post Generator
~~~~~~~~~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.linkedin_writer.modules.post_generator
:members:
:undoc-members:
:show-inheritance:
LinkedIn Article Generator
~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.linkedin_writer.modules.article_generator
:members:
:undoc-members:
:show-inheritance:
LinkedIn Profile Optimizer
~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.linkedin_writer.modules.profile_optimizer
:members:
:undoc-members:
:show-inheritance:
Twitter Writer
------------
.. automodule:: lib.ai_writers.twitter_writers
:members:
:undoc-members:
:show-inheritance:
Tweet Generator
~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.twitter_writers.tweet_generator
:members:
:undoc-members:
:show-inheritance:
Facebook Writer
-------------
.. automodule:: lib.ai_writers.ai_facebook_writer
:members:
:undoc-members:
:show-inheritance:
Facebook Ad Copy Generator
~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.ai_facebook_writer.modules.ad_copy_generator
:members:
:undoc-members:
:show-inheritance:
Facebook Carousel Generator
~~~~~~~~~~~~~~~~~~~~~~~~~
.. automodule:: lib.ai_writers.ai_facebook_writer.modules.facebook_carousel
:members:
:undoc-members:
:show-inheritance:
YouTube Writers
-------------
.. automodule:: lib.ai_writers.youtube_writers
:members:
:undoc-members:
:show-inheritance:
Story Writer
----------
.. automodule:: lib.ai_writers.ai_story_writer
:members:
:undoc-members:
:show-inheritance:
Copywriter
---------
.. automodule:: lib.ai_writers.ai_copywriter
:members:
:undoc-members:
:show-inheritance:
Blog Writers
----------
GitHub Blogs
~~~~~~~~~~~
.. automodule:: lib.ai_writers.github_blogs
:members:
:undoc-members:
:show-inheritance:
Scholar Blogs
~~~~~~~~~~~
.. automodule:: lib.ai_writers.scholar_blogs
:members:
:undoc-members:
:show-inheritance:
Speech to Blog
~~~~~~~~~~~~
.. automodule:: lib.ai_writers.speech_to_blog
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,12 @@
Analytics
=========
This section documents the analytics modules that provide content performance tracking and visualization.
Analytics Engine
--------------
.. automodule:: lib.analytics
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,51 @@
Core API
========
This section documents the core modules of the AI-Writer platform.
Main Application
--------------
.. automodule:: alwrity
:members:
:undoc-members:
:show-inheritance:
GPT Providers
-----------
Text Generation
~~~~~~~~~~~~~
.. automodule:: lib.gpt_providers.text_generation.gemini_pro_text
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.gpt_providers.text_generation.mistral_chat_completion
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.gpt_providers.text_generation.deepseek_text_gen
:members:
:undoc-members:
:show-inheritance:
Image Generation
~~~~~~~~~~~~~~
.. automodule:: lib.gpt_providers.text_to_image_generation.main_generate_image_from_prompt
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.gpt_providers.text_to_image_generation.gen_gemini_images
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.gpt_providers.text_to_image_generation.gen_dali3_images
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,22 @@
Database
========
This section documents the database modules that handle content storage, retrieval, and vector search capabilities.
Database Models
-------------
.. automodule:: lib.database
:members:
:undoc-members:
:show-inheritance:
Vector Database
-------------
The vector database provides semantic search capabilities for content retrieval.
.. automodule:: lib.workspace.alwrity_data.vectordb
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,83 @@
.. _api-reference:
API Reference
============
This section provides detailed documentation for the AI-Writer API, including module references, class hierarchies, and function specifications.
.. toctree::
:maxdepth: 2
:caption: API Documentation:
core
ai_writers
database
utils
analytics
web_crawlers
Core Modules
-----------
.. automodule:: alwrity
:members:
:undoc-members:
:show-inheritance:
AI Writers
---------
The AI Writers modules provide specialized content generation for different platforms and content types.
.. toctree::
:maxdepth: 1
ai_writers/linkedin
ai_writers/twitter
ai_writers/blog
ai_writers/email
Database
-------
The database modules handle content storage, retrieval, and vector search capabilities.
.. toctree::
:maxdepth: 1
database/models
database/vector_store
database/relational_store
Utilities
--------
Utility modules provide supporting functionality across the application.
.. toctree::
:maxdepth: 1
utils/api_key_manager
utils/ui_setup
utils/seo_tools
Analytics
--------
Analytics modules provide content performance tracking and visualization.
.. toctree::
:maxdepth: 1
analytics/content_analyzer
analytics/analytics_ui
Web Crawlers
-----------
Web crawler modules provide research capabilities by extracting information from the web.
.. toctree::
:maxdepth: 1
web_crawlers/async_web_crawler

View File

@@ -0,0 +1,78 @@
Utilities
=========
This section documents the utility modules that provide supporting functionality across the application.
API Key Manager
-------------
.. automodule:: lib.utils.api_key_manager
:members:
:undoc-members:
:show-inheritance:
Website Analyzer
--------------
.. automodule:: lib.utils.website_analyzer
:members:
:undoc-members:
:show-inheritance:
UI Components
-----------
.. automodule:: lib.alwrity_ui
:members:
:undoc-members:
:show-inheritance:
SEO Tools
--------
.. automodule:: lib.ai_seo_tools
:members:
:undoc-members:
:show-inheritance:
Marketing Tools
-------------
.. automodule:: lib.ai_marketing_tools
:members:
:undoc-members:
:show-inheritance:
Blog Processing
-------------
.. automodule:: lib.blog_metadata
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.blog_postprocessing
:members:
:undoc-members:
:show-inheritance:
.. automodule:: lib.blog_sections
:members:
:undoc-members:
:show-inheritance:
Content Planning
--------------
.. automodule:: lib.content_planning_calender
:members:
:undoc-members:
:show-inheritance:
Personalization
-------------
.. automodule:: lib.personalization
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,28 @@
Web Crawlers
============
This section documents the web crawler modules that provide research capabilities by extracting information from the web.
Web Researcher
------------
.. automodule:: lib.ai_web_researcher
:members:
:undoc-members:
:show-inheritance:
Web Crawlers
----------
.. automodule:: lib.web_crawlers
:members:
:undoc-members:
:show-inheritance:
Research Storage
--------------
.. automodule:: lib.workspace.alwrity_web_research
:members:
:undoc-members:
:show-inheritance:

View File

@@ -0,0 +1,449 @@
API Design
=========
This document outlines the API design principles and specifications for the AI-Writer platform.
API Design Principles
-------------------
The AI-Writer API follows these core design principles:
1. **RESTful Architecture**
* Resource-oriented design
* Standard HTTP methods (GET, POST, PUT, DELETE)
* Consistent URL structure
* Stateless interactions
2. **Consistent Response Format**
* JSON as the primary data format
* Standard error response structure
* Pagination for list endpoints
* Hypermedia links where appropriate
3. **Versioning**
* API versioning in URL path (e.g., `/api/v1/`)
* Backward compatibility within major versions
* Deprecation notices before removing features
4. **Security**
* Authentication via API keys or OAuth 2.0
* Rate limiting to prevent abuse
* Input validation to prevent injection attacks
* HTTPS for all communications
5. **Documentation**
* OpenAPI/Swagger specification
* Interactive documentation
* Code examples for common operations
* Changelog for API updates
API Endpoints
-----------
Content Management
~~~~~~~~~~~~~~~~
.. code-block:: text
# Create content
POST /api/v1/content
# Get content by ID
GET /api/v1/content/{content_id}
# Update content
PUT /api/v1/content/{content_id}
# Delete content
DELETE /api/v1/content/{content_id}
# List content with filtering
GET /api/v1/content?type={type}&limit={limit}&offset={offset}
# Get content versions
GET /api/v1/content/{content_id}/versions
# Revert to specific version
POST /api/v1/content/{content_id}/revert/{version_id}
AI Generation
~~~~~~~~~~~
.. code-block:: text
# Generate content from keywords
POST /api/v1/generate/content
# Generate blog post
POST /api/v1/generate/blog
# Generate social media post
POST /api/v1/generate/social
# Generate email
POST /api/v1/generate/email
# Generate outline
POST /api/v1/generate/outline
# Generate image for content
POST /api/v1/generate/image
Web Research
~~~~~~~~~~
.. code-block:: text
# Perform web research
POST /api/v1/research
# Get research results
GET /api/v1/research/{research_id}
# Search previous research
GET /api/v1/research/search?query={query}
SEO Tools
~~~~~~~~
.. code-block:: text
# Analyze content for SEO
POST /api/v1/seo/analyze
# Generate meta description
POST /api/v1/seo/meta-description
# Generate SEO-friendly title
POST /api/v1/seo/title
# Generate structured data
POST /api/v1/seo/structured-data
# Generate alt text for images
POST /api/v1/seo/alt-text
User Management
~~~~~~~~~~~~~
.. code-block:: text
# Create user
POST /api/v1/users
# Get user profile
GET /api/v1/users/{user_id}
# Update user profile
PUT /api/v1/users/{user_id}
# Delete user
DELETE /api/v1/users/{user_id}
# Get user settings
GET /api/v1/users/{user_id}/settings
# Update user settings
PUT /api/v1/users/{user_id}/settings
API Key Management
~~~~~~~~~~~~~~~
.. code-block:: text
# Create API key
POST /api/v1/api-keys
# List API keys
GET /api/v1/api-keys
# Revoke API key
DELETE /api/v1/api-keys/{key_id}
Analytics
~~~~~~~~
.. code-block:: text
# Get content analytics
GET /api/v1/analytics/content/{content_id}
# Get user analytics
GET /api/v1/analytics/user/{user_id}
# Get system analytics
GET /api/v1/analytics/system
Request and Response Examples
---------------------------
Create Content
~~~~~~~~~~~~
Request:
.. code-block:: json
POST /api/v1/content
Content-Type: application/json
Authorization: Bearer {api_key}
{
"title": "How to Improve SEO with AI",
"content_type": "blog",
"content": "# How to Improve SEO with AI\n\nIn this article, we'll explore...",
"metadata": {
"keywords": ["SEO", "AI", "content marketing"],
"category": "digital marketing",
"language": "en"
}
}
Response:
.. code-block:: json
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "c123e4567-e89b-12d3-a456-426614174000",
"title": "How to Improve SEO with AI",
"content_type": "blog",
"content": "# How to Improve SEO with AI\n\nIn this article, we'll explore...",
"metadata": {
"keywords": ["SEO", "AI", "content marketing"],
"category": "digital marketing",
"language": "en"
},
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"user_id": "u123e4567-e89b-12d3-a456-426614174000",
"links": {
"self": "/api/v1/content/c123e4567-e89b-12d3-a456-426614174000",
"versions": "/api/v1/content/c123e4567-e89b-12d3-a456-426614174000/versions",
"analytics": "/api/v1/analytics/content/c123e4567-e89b-12d3-a456-426614174000"
}
}
Generate Blog Post
~~~~~~~~~~~~~~~
Request:
.. code-block:: json
POST /api/v1/generate/blog
Content-Type: application/json
Authorization: Bearer {api_key}
{
"keywords": ["artificial intelligence", "content creation"],
"title": "The Future of Content Creation with AI",
"tone": "informative",
"length": "medium",
"include_research": true,
"target_audience": "marketers"
}
Response:
.. code-block:: json
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "g123e4567-e89b-12d3-a456-426614174000",
"title": "The Future of Content Creation with AI",
"content": "# The Future of Content Creation with AI\n\nArtificial intelligence is revolutionizing...",
"metadata": {
"keywords": ["artificial intelligence", "content creation"],
"tone": "informative",
"length": "medium",
"word_count": 1250,
"research_sources": [
{
"title": "AI in Content Marketing Report 2023",
"url": "https://example.com/report",
"accessed_at": "2023-01-01T10:30:00Z"
}
]
},
"created_at": "2023-01-01T12:05:00Z",
"links": {
"save": "/api/v1/content",
"regenerate": "/api/v1/generate/blog",
"edit": "/api/v1/generate/edit"
}
}
Error Response
~~~~~~~~~~~~
.. code-block:: json
HTTP/1.1 400 Bad Request
Content-Type: application/json
{
"error": {
"code": "invalid_request",
"message": "The request was invalid",
"details": [
{
"field": "keywords",
"issue": "required",
"description": "The keywords field is required"
}
]
},
"request_id": "req_123456",
"documentation_url": "https://docs.alwrity.com/api/errors#invalid_request"
}
API Authentication
----------------
The AI-Writer API supports the following authentication methods:
1. **API Key Authentication**
* Include the API key in the Authorization header:
`Authorization: Bearer {api_key}`
* API keys can be generated and managed through the API or web interface
* Different permission levels can be assigned to API keys
2. **OAuth 2.0 (for multi-user deployments)**
* Standard OAuth 2.0 flow with authorization code
* Supports scopes for fine-grained permissions
* Refresh token rotation for enhanced security
Rate Limiting
-----------
To ensure fair usage and system stability, the API implements rate limiting:
* Rate limits are based on the user's plan
* Limits are applied per API key
* Rate limit information is included in response headers:
* `X-RateLimit-Limit`: Total requests allowed in the current period
* `X-RateLimit-Remaining`: Requests remaining in the current period
* `X-RateLimit-Reset`: Time when the rate limit resets (Unix timestamp)
When a rate limit is exceeded, the API returns a 429 Too Many Requests response.
Pagination
---------
List endpoints support pagination with the following parameters:
* `limit`: Number of items per page (default: 20, max: 100)
* `offset`: Number of items to skip (for offset-based pagination)
* `cursor`: Cursor for the next page (for cursor-based pagination)
Response includes pagination metadata:
.. code-block:: json
{
"data": [...],
"pagination": {
"total": 45,
"limit": 20,
"offset": 0,
"next_cursor": "cursor_for_next_page",
"has_more": true
}
}
Filtering and Sorting
-------------------
List endpoints support filtering and sorting:
* Filtering: `?field=value&another_field=another_value`
* Range filtering: `?created_at_gte=2023-01-01&created_at_lte=2023-01-31`
* Sorting: `?sort=field` (ascending) or `?sort=-field` (descending)
* Multiple sort fields: `?sort=-created_at,title`
Versioning Strategy
-----------------
The API uses a versioning strategy to ensure backward compatibility:
1. **Major Versions**
* Included in the URL path: `/api/v1/`, `/api/v2/`, etc.
* Major versions may introduce breaking changes
* Previous major versions are supported for at least 12 months after a new version is released
2. **Minor Updates**
* Backward-compatible changes within a major version
* New endpoints or parameters may be added
* Existing functionality remains unchanged
3. **Deprecation Process**
* Features to be removed are marked as deprecated
* Deprecation notices are included in response headers
* Deprecated features are supported for at least 6 months before removal
API Changelog
-----------
The API changelog is maintained to track changes:
* **v1.0.0 (2023-01-01)**
* Initial release with core content management features
* Basic AI generation capabilities
* User management and authentication
* **v1.1.0 (2023-03-15)**
* Added SEO analysis endpoints
* Enhanced content generation with research integration
* Improved error handling and validation
* **v1.2.0 (2023-06-30)**
* Added analytics endpoints
* Introduced cursor-based pagination
* Added support for content versioning
Future API Roadmap
----------------
Planned API enhancements:
1. **Content Collaboration**
* Endpoints for collaborative editing
* Comment and feedback functionality
* Role-based access control
2. **Advanced Analytics**
* Predictive performance metrics
* Competitive analysis
* Content optimization recommendations
3. **Workflow Automation**
* Scheduled content generation
* Approval workflows
* Integration with publishing platforms
4. **Multi-modal Content**
* Enhanced image generation
* Audio content generation
* Video script generation

View File

@@ -0,0 +1,170 @@
Architecture Overview
====================
This document provides a comprehensive overview of the AI-Writer architecture, explaining the system's components, their interactions, and the design principles behind the implementation.
High-Level Architecture
----------------------
.. image:: diagrams/high_level_architecture.png
:alt: High-level architecture diagram of AI-Writer
:width: 100%
The AI-Writer platform consists of several key components:
1. **User Interface Layer**
* Streamlit-based web interface
* Command-line interface for automation
* API endpoints for programmatic access
2. **Core Services Layer**
* AI Writers: Various specialized content generation modules
* Web Research: Tools for gathering factual information from the internet
* SEO Tools: Utilities for optimizing content for search engines
* Analytics: Content performance tracking and analysis
3. **Data Storage Layer**
* Vector Database (ChromaDB): Stores embeddings for semantic search
* Relational Database (SQLite): Stores structured data like user preferences and content metadata
4. **External Integrations Layer**
* LLM Providers: OpenAI, Google Gemini, Anthropic, etc.
* Search Providers: Tavily, SerperDev, Exa, etc.
* Image Generation: Stability AI
* Publishing Platforms: WordPress, Jekyll, etc.
Database Architecture
--------------------
.. image:: diagrams/database_architecture.png
:alt: Database architecture diagram of AI-Writer
:width: 100%
The database architecture consists of two main components:
1. **Vector Storage**
* Uses ChromaDB for storing and retrieving text embeddings
* Enables semantic search capabilities
* Stores content in collections for efficient retrieval
2. **Relational Storage**
* Uses SQLite for structured data storage
* Key models include:
- User: Stores user preferences and settings
- ContentItem: Represents content created by users
- ContentVersion: Tracks version history of content
- Analytics: Stores performance metrics for content
Content Generation Workflow
--------------------------
.. image:: diagrams/content_generation_workflow.png
:alt: Content generation workflow diagram of AI-Writer
:width: 100%
The content generation process follows these steps:
1. **Input Phase**
* User provides keywords, topics, or other input parameters
* System configures the generation process based on user preferences
2. **Research Phase**
* Web research is conducted using various search providers
* Relevant information is gathered and processed
* Facts are extracted and organized for use in content generation
3. **Content Creation Phase**
* Content outline is generated based on research
* Initial draft is created using AI models
* Final content is refined and polished
4. **Enhancement Phase**
* SEO optimization is applied to improve search visibility
* Images are generated or selected to complement the content
* Metadata is generated for better categorization and discovery
5. **Storage Phase**
* Content is stored in both vector and relational databases
* Embeddings are created for semantic search capabilities
* Metadata is indexed for efficient retrieval
6. **Publishing Phase**
* Content is formatted for the target platform
* Publishing options include WordPress, Markdown, and others
* Content is delivered to the user or published directly
Design Principles
----------------
The AI-Writer architecture is built on the following design principles:
1. **Modularity**
* Components are designed to be independent and interchangeable
* New AI models and services can be added with minimal changes
* Functionality is organized into logical modules
2. **Extensibility**
* The system is designed to be easily extended with new features
* Plugin architecture allows for custom integrations
* Configuration options enable customization without code changes
3. **Reliability**
* Error handling is implemented throughout the system
* Fallback mechanisms ensure continued operation
* Logging provides visibility into system behavior
4. **Performance**
* Caching is used to improve response times
* Asynchronous processing for long-running tasks
* Efficient data storage and retrieval mechanisms
5. **Security**
* API keys are securely stored and managed
* User data is protected with appropriate measures
* Input validation prevents common security issues
Future Architecture Enhancements
-------------------------------
Planned improvements to the architecture include:
1. **Distributed Processing**
* Support for distributed content generation
* Load balancing for improved scalability
* Parallel processing of research and generation tasks
2. **Advanced Caching**
* Intelligent caching of common queries and results
* Cache invalidation strategies for fresh content
* Distributed cache for multi-user environments
3. **Enhanced Security**
* Role-based access control
* End-to-end encryption for sensitive data
* Advanced authentication mechanisms
4. **Containerization**
* Docker containers for easier deployment
* Kubernetes support for orchestration
* Microservices architecture for better scalability

View File

@@ -0,0 +1,171 @@
Component Diagram
================
This document provides detailed information about the components of the AI-Writer system and their interactions.
Core Components
--------------
AI Writers
~~~~~~~~~~
The AI Writers component is responsible for generating various types of content using AI models. It includes several specialized writers:
- **Blog Writer**: Generates blog posts based on keywords and web research
- **News Article Writer**: Creates news articles with citations from current events
- **Social Media Writer**: Produces content for various social platforms
- **Email Writer**: Generates professional and business emails
- **Story Writer**: Creates narrative content based on user input
- **YouTube Script Writer**: Develops scripts for video content
Each writer implements a common interface but has specialized logic for its specific content type. The writers interact with LLM providers through a unified API layer that handles authentication, rate limiting, and error handling.
Web Research
~~~~~~~~~~~
The Web Research component gathers information from the internet to provide factual context for content generation. It includes:
- **SERP Integration**: Retrieves search engine results
- **Tavily Integration**: Uses AI-powered search for relevant information
- **Exa Integration**: Performs semantic search for related content
- **Web Crawler**: Extracts content from specified URLs
- **Content Analyzer**: Processes and summarizes gathered information
This component ensures that generated content is factually accurate and up-to-date by providing relevant research data to the AI Writers.
SEO Tools
~~~~~~~~~
The SEO Tools component provides utilities for optimizing content for search engines:
- **Keyword Analyzer**: Identifies and analyzes target keywords
- **Meta Description Generator**: Creates SEO-friendly meta descriptions
- **Title Generator**: Produces optimized titles for content
- **Structured Data Generator**: Creates schema markup for rich snippets
- **Image Optimizer**: Optimizes images for web performance
- **On-Page SEO Analyzer**: Evaluates content for SEO best practices
These tools work together to ensure that generated content has the best chance of ranking well in search engines.
Analytics
~~~~~~~~
The Analytics component tracks and analyzes content performance:
- **Content Metrics**: Measures readability, engagement potential, and other metrics
- **Performance Tracker**: Monitors content performance over time
- **Recommendation Engine**: Suggests improvements based on analytics
- **Report Generator**: Creates reports on content effectiveness
This component helps users understand how their content is performing and how it can be improved.
Data Storage
-----------
Vector Database
~~~~~~~~~~~~~~
The Vector Database component uses ChromaDB to store and retrieve text embeddings:
- **Embedding Generator**: Creates vector representations of text
- **Collection Manager**: Organizes embeddings into collections
- **Semantic Search**: Performs similarity searches on embeddings
- **Metadata Manager**: Associates metadata with embeddings
This component enables semantic search capabilities, allowing users to find content based on meaning rather than just keywords.
Relational Database
~~~~~~~~~~~~~~~~~~
The Relational Database component uses SQLite to store structured data:
- **User Manager**: Handles user data and preferences
- **Content Repository**: Stores content items and metadata
- **Version Control**: Tracks content versions and changes
- **Analytics Storage**: Stores performance metrics and analytics data
This component provides persistent storage for all structured data in the system.
External Integrations
--------------------
LLM Providers
~~~~~~~~~~~~
The LLM Providers component integrates with various AI models:
- **OpenAI Integration**: Connects to GPT models
- **Google Gemini Integration**: Interfaces with Gemini models
- **Anthropic Integration**: Works with Claude models
- **Ollama Integration**: Supports local LLM deployment
This component provides a unified interface to different AI models, allowing the system to use the best model for each task.
Search Providers
~~~~~~~~~~~~~~~
The Search Providers component connects to external search services:
- **Tavily Client**: Interfaces with Tavily AI search
- **SerperDev Client**: Connects to SerperDev API
- **Exa Client**: Integrates with Exa search API
- **Google Search Client**: Provides access to Google search results
These integrations enable the system to gather relevant information from the internet for content generation.
Image Generation
~~~~~~~~~~~~~~~
The Image Generation component creates images to complement content:
- **Stability AI Integration**: Connects to Stable Diffusion models
- **DALL-E Integration**: Interfaces with OpenAI's DALL-E
- **Image Processor**: Optimizes and formats generated images
- **Image Repository**: Stores and manages generated images
This component enhances content with relevant visuals, improving engagement and comprehension.
Publishing Platforms
~~~~~~~~~~~~~~~~~~~
The Publishing Platforms component enables content distribution:
- **WordPress Integration**: Publishes content to WordPress sites
- **Markdown Exporter**: Creates Markdown files for static sites
- **HTML Exporter**: Generates HTML for web publishing
- **API Connectors**: Interfaces with various content platforms
This component streamlines the process of publishing generated content to various platforms.
Component Interactions
---------------------
Content Generation Flow
~~~~~~~~~~~~~~~~~~~~~~
1. User provides input parameters through the UI
2. Web Research gathers relevant information
3. AI Writers generate content using research data and LLM providers
4. SEO Tools optimize the content for search engines
5. Content is stored in both Vector and Relational databases
6. Analytics evaluates the content quality and potential performance
7. Content is prepared for publishing through the Publishing Platforms
Data Flow
~~~~~~~~~
1. User preferences and settings flow from UI to Relational Database
2. Research data flows from Web Research to AI Writers
3. Generated content flows from AI Writers to SEO Tools
4. Optimized content flows to Data Storage components
5. Content metrics flow from Analytics to Relational Database
6. Published content flows from Publishing Platforms to external systems
Error Handling
~~~~~~~~~~~~~
1. LLM provider errors are handled by fallback mechanisms
2. Web Research failures trigger alternative search methods
3. Database errors are logged and retried with exponential backoff
4. Publishing failures are queued for retry
5. All errors are logged for monitoring and debugging

View File

@@ -0,0 +1,442 @@
Database Schema
==============
This document describes the database schema used in the AI-Writer platform, including both the relational database and vector database components.
Relational Database Schema
------------------------
AI-Writer uses SQLAlchemy ORM to interact with the relational database. The schema consists of the following main tables:
User
~~~~
Stores user information and preferences.
.. code-block:: python
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False)
password_hash = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
api_keys = relationship("ApiKey", back_populates="user")
contents = relationship("Content", back_populates="user")
settings = relationship("UserSetting", back_populates="user", uselist=False)
ApiKey
~~~~~~
Stores encrypted API keys for various services.
.. code-block:: python
class ApiKey(Base):
__tablename__ = "api_keys"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
service_name = Column(String, nullable=False)
encrypted_key = Column(String, nullable=False)
is_active = Column(Boolean, default=True)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
user = relationship("User", back_populates="api_keys")
Content
~~~~~~~
Stores generated content with metadata.
.. code-block:: python
class Content(Base):
__tablename__ = "contents"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
title = Column(String, nullable=False)
content_type = Column(String, nullable=False) # blog, linkedin, twitter, etc.
content_text = Column(Text, nullable=False)
metadata = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
user = relationship("User", back_populates="contents")
versions = relationship("ContentVersion", back_populates="content")
analytics = relationship("ContentAnalytics", back_populates="content")
ContentVersion
~~~~~~~~~~~~~
Tracks versions of content for history and rollback.
.. code-block:: python
class ContentVersion(Base):
__tablename__ = "content_versions"
id = Column(Integer, primary_key=True)
content_id = Column(Integer, ForeignKey("contents.id"))
version_number = Column(Integer, nullable=False)
content_text = Column(Text, nullable=False)
metadata = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
content = relationship("Content", back_populates="versions")
ContentAnalytics
~~~~~~~~~~~~~~
Stores analytics data for content performance.
.. code-block:: python
class ContentAnalytics(Base):
__tablename__ = "content_analytics"
id = Column(Integer, primary_key=True)
content_id = Column(Integer, ForeignKey("contents.id"))
views = Column(Integer, default=0)
likes = Column(Integer, default=0)
shares = Column(Integer, default=0)
comments = Column(Integer, default=0)
engagement_rate = Column(Float, default=0.0)
last_updated = Column(DateTime, default=datetime.utcnow)
# Relationships
content = relationship("Content", back_populates="analytics")
UserSetting
~~~~~~~~~~
Stores user preferences and settings.
.. code-block:: python
class UserSetting(Base):
__tablename__ = "user_settings"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"), unique=True)
preferred_ai_provider = Column(String)
default_content_type = Column(String)
ui_theme = Column(String, default="light")
language = Column(String, default="en")
settings_json = Column(JSON)
# Relationships
user = relationship("User", back_populates="settings")
Template
~~~~~~~
Stores reusable content templates.
.. code-block:: python
class Template(Base):
__tablename__ = "templates"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
name = Column(String, nullable=False)
content_type = Column(String, nullable=False)
template_text = Column(Text, nullable=False)
variables = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
user = relationship("User")
ContentGapAnalysis
~~~~~~~~~~~~~~~~~
Stores content gap analysis results.
.. code-block:: python
class ContentGapAnalysis(Base):
__tablename__ = "content_gap_analyses"
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"))
website_url = Column(String, nullable=False)
industry = Column(String, nullable=False)
analysis_date = Column(DateTime, default=datetime.utcnow)
status = Column(String, nullable=False) # completed, in_progress, failed
metadata = Column(JSON)
# Relationships
user = relationship("User", back_populates="content_gap_analyses")
website_analysis = relationship("WebsiteAnalysis", back_populates="content_gap_analysis")
competitor_analysis = relationship("CompetitorAnalysis", back_populates="content_gap_analysis")
keyword_analysis = relationship("KeywordAnalysis", back_populates="content_gap_analysis")
recommendations = relationship("ContentRecommendation", back_populates="content_gap_analysis")
WebsiteAnalysis
~~~~~~~~~~~~~~
Stores website analysis results.
.. code-block:: python
class WebsiteAnalysis(Base):
__tablename__ = "website_analyses"
id = Column(Integer, primary_key=True)
content_gap_analysis_id = Column(Integer, ForeignKey("content_gap_analyses.id"))
content_score = Column(Float)
seo_score = Column(Float)
structure_score = Column(Float)
content_metrics = Column(JSON)
seo_metrics = Column(JSON)
technical_metrics = Column(JSON)
ai_insights = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
content_gap_analysis = relationship("ContentGapAnalysis", back_populates="website_analysis")
CompetitorAnalysis
~~~~~~~~~~~~~~~~
Stores competitor analysis results.
.. code-block:: python
class CompetitorAnalysis(Base):
__tablename__ = "competitor_analyses"
id = Column(Integer, primary_key=True)
content_gap_analysis_id = Column(Integer, ForeignKey("content_gap_analyses.id"))
competitor_url = Column(String, nullable=False)
market_position = Column(JSON)
content_gaps = Column(JSON)
competitive_advantages = Column(JSON)
trend_analysis = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
content_gap_analysis = relationship("ContentGapAnalysis", back_populates="competitor_analysis")
KeywordAnalysis
~~~~~~~~~~~~~
Stores keyword analysis results.
.. code-block:: python
class KeywordAnalysis(Base):
__tablename__ = "keyword_analyses"
id = Column(Integer, primary_key=True)
content_gap_analysis_id = Column(Integer, ForeignKey("content_gap_analyses.id"))
top_keywords = Column(JSON)
search_intent = Column(JSON)
opportunities = Column(JSON)
trend_analysis = Column(JSON)
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
content_gap_analysis = relationship("ContentGapAnalysis", back_populates="keyword_analysis")
ContentRecommendation
~~~~~~~~~~~~~~~~~~~
Stores content recommendations.
.. code-block:: python
class ContentRecommendation(Base):
__tablename__ = "content_recommendations"
id = Column(Integer, primary_key=True)
content_gap_analysis_id = Column(Integer, ForeignKey("content_gap_analyses.id"))
recommendation_type = Column(String, nullable=False) # content, seo, technical, etc.
priority_score = Column(Float)
recommendation = Column(Text, nullable=False)
implementation_steps = Column(JSON)
expected_impact = Column(JSON)
status = Column(String, nullable=False) # pending, in_progress, completed, rejected
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
content_gap_analysis = relationship("ContentGapAnalysis", back_populates="recommendations")
AnalysisHistory
~~~~~~~~~~~~~
Tracks the history of analysis runs.
.. code-block:: python
class AnalysisHistory(Base):
__tablename__ = "analysis_histories"
id = Column(Integer, primary_key=True)
content_gap_analysis_id = Column(Integer, ForeignKey("content_gap_analyses.id"))
run_date = Column(DateTime, default=datetime.utcnow)
status = Column(String, nullable=False) # completed, in_progress, failed
metrics = Column(JSON) # Performance metrics for the analysis run
error_log = Column(Text) # Any errors encountered during analysis
# Relationships
content_gap_analysis = relationship("ContentGapAnalysis")
Vector Database Schema
--------------------
AI-Writer uses ChromaDB for vector storage, which enables semantic search and retrieval of content. The vector database stores:
1. **Content Embeddings**
* Generated from content text using embedding models
* Used for semantic search and content similarity
2. **Metadata**
* Content ID (linking to relational database)
* Content type
* Creation date
* Keywords and tags
3. **Collections**
ChromaDB organizes embeddings into collections:
* `content_embeddings`: Main collection for all content
* `user_{user_id}_content`: Per-user content collections
* `{content_type}_embeddings`: Collections by content type
Vector Database Operations
------------------------
The vector database supports the following operations:
1. **Adding Content**
.. code-block:: python
def add_content_to_vector_db(content_id, content_text, metadata):
"""Add content to the vector database.
Args:
content_id: The ID of the content in the relational database.
content_text: The text content to embed.
metadata: Additional metadata for the content.
"""
embeddings = get_embeddings(content_text)
collection = get_collection("content_embeddings")
collection.add(
ids=[str(content_id)],
embeddings=[embeddings],
metadatas=[metadata],
documents=[content_text]
)
2. **Searching Content**
.. code-block:: python
def search_similar_content(query_text, limit=5):
"""Search for similar content using vector similarity.
Args:
query_text: The query text to search for.
limit: Maximum number of results to return.
Returns:
List of similar content items with their similarity scores.
"""
query_embedding = get_embeddings(query_text)
collection = get_collection("content_embeddings")
results = collection.query(
query_embeddings=[query_embedding],
n_results=limit
)
return results
3. **Updating Content**
.. code-block:: python
def update_content_in_vector_db(content_id, new_content_text, metadata):
"""Update content in the vector database.
Args:
content_id: The ID of the content to update.
new_content_text: The updated text content.
metadata: Updated metadata.
"""
new_embedding = get_embeddings(new_content_text)
collection = get_collection("content_embeddings")
collection.update(
ids=[str(content_id)],
embeddings=[new_embedding],
metadatas=[metadata],
documents=[new_content_text]
)
Database Migrations
-----------------
AI-Writer uses Alembic for database migrations. The migration workflow is:
1. **Create Migration**
.. code-block:: bash
alembic revision --autogenerate -m "Description of changes"
2. **Apply Migration**
.. code-block:: bash
alembic upgrade head
3. **Rollback Migration**
.. code-block:: bash
alembic downgrade -1
Database Backup and Restore
-------------------------
Regular database backups are recommended:
1. **SQLite Backup**
.. code-block:: bash
# Backup
sqlite3 data/alwrity.db .dump > backup.sql
# Restore
sqlite3 data/alwrity.db < backup.sql
2. **Vector Database Backup**
ChromaDB data is stored in the specified directory and can be backed up by copying the directory:
.. code-block:: bash
# Backup
cp -r data/vectordb data/vectordb_backup
# Restore
rm -rf data/vectordb
cp -r data/vectordb_backup data/vectordb

View File

@@ -0,0 +1,571 @@
Deployment Architecture
=====================
This document outlines the deployment architecture for the AI-Writer platform, including deployment models, infrastructure requirements, and operational considerations.
Deployment Models
---------------
AI-Writer supports multiple deployment models to accommodate different user needs and scale requirements:
Single-User Deployment
~~~~~~~~~~~~~~~~~~~~
Ideal for individual content creators or small teams:
1. **Local Installation**
* Runs on a single machine
* SQLite database for data storage
* Local file system for content storage
* Minimal resource requirements
2. **Configuration**
* Simple configuration file
* Environment variables for API keys
* Local storage paths
* Logging configuration
3. **Resource Requirements**
* CPU: 2+ cores
* RAM: 4GB minimum (8GB recommended)
* Storage: 10GB minimum
* Python 3.9+ runtime
Multi-User Deployment
~~~~~~~~~~~~~~~~~~~
Suitable for teams and organizations:
1. **Server Deployment**
* Dedicated server or cloud instance
* PostgreSQL database
* Shared file storage
* Web server (Nginx/Apache) with WSGI/ASGI
2. **Docker Deployment**
* Containerized application
* Docker Compose for orchestration
* Persistent volumes for data
* Separate containers for services
3. **Resource Requirements**
* CPU: 4+ cores
* RAM: 16GB minimum
* Storage: 50GB+ SSD
* Network: 100Mbps+ bandwidth
Enterprise Deployment
~~~~~~~~~~~~~~~~~~~
For large organizations with high volume requirements:
1. **Kubernetes Deployment**
* Containerized microservices
* Horizontal scaling
* Load balancing
* High availability configuration
2. **Database Scaling**
* Database clustering
* Read replicas
* Connection pooling
* Automated backups
3. **Resource Requirements**
* CPU: 8+ cores per node
* RAM: 32GB+ per node
* Storage: 100GB+ SSD with high IOPS
* Network: 1Gbps+ bandwidth
Infrastructure Components
-----------------------
Core Components
~~~~~~~~~~~~~
1. **Application Servers**
* Runs the AI-Writer application code
* Handles HTTP requests
* Processes content generation tasks
* Manages user sessions
2. **Database Servers**
* Stores relational data (SQLite/PostgreSQL)
* Stores vector embeddings (ChromaDB)
* Handles data persistence
* Manages transactions and concurrency
3. **File Storage**
* Stores generated content
* Stores uploaded files
* Manages file versioning
* Handles file access control
4. **Web Servers**
* Handles HTTP/HTTPS traffic
* SSL termination
* Static file serving
* Request routing
Optional Components
~~~~~~~~~~~~~~~~
1. **Cache Servers**
* Redis for caching
* Session storage
* Rate limiting
* Task queuing
2. **Background Workers**
* Processes asynchronous tasks
* Handles long-running operations
* Manages scheduled jobs
* Processes content generation queue
3. **Load Balancers**
* Distributes traffic across servers
* Health checking
* SSL termination
* DDoS protection
4. **Monitoring Services**
* Application performance monitoring
* Log aggregation
* Metrics collection
* Alerting
Deployment Topologies
-------------------
Basic Topology
~~~~~~~~~~~~
For single-user or small team deployments:
```
[User] → [Web Server] → [AI-Writer Application] → [SQLite/PostgreSQL]
→ [File Storage]
→ [External APIs]
```
Standard Topology
~~~~~~~~~~~~~~
For multi-user deployments:
```
[Users] → [Load Balancer] → [Web Servers] → [Application Servers] → [PostgreSQL Cluster]
→ [Background Workers] → [File Storage]
→ [Redis Cache]
→ [External APIs]
```
High-Availability Topology
~~~~~~~~~~~~~~~~~~~~~~~
For enterprise deployments:
```
[Users] → [CDN] → [Load Balancer] → [Web Servers (Multiple AZs)]
→ [Application Servers (Multiple AZs)]
→ [Background Workers (Multiple AZs)]
→ [PostgreSQL (Primary + Replicas)]
→ [Redis Cluster]
→ [Distributed File Storage]
→ [External APIs with Fallbacks]
```
Deployment Process
----------------
Installation Methods
~~~~~~~~~~~~~~~~~
1. **Manual Installation**
* Clone repository
* Install dependencies
* Configure environment
* Initialize database
* Start application
2. **Docker Installation**
* Pull Docker images
* Configure Docker Compose
* Start containers
* Initialize services
* Configure networking
3. **Kubernetes Installation**
* Apply Kubernetes manifests
* Configure Helm charts
* Set up persistent volumes
* Configure ingress
* Deploy services
Configuration Management
~~~~~~~~~~~~~~~~~~~~~
1. **Environment Variables**
* API keys and credentials
* Database connection strings
* Service endpoints
* Feature flags
2. **Configuration Files**
* Application settings
* Logging configuration
* Database settings
* Cache settings
3. **Secrets Management**
* Kubernetes secrets
* Docker secrets
* Vault integration
* Encrypted configuration
Continuous Integration/Deployment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. **CI Pipeline**
* Automated testing
* Code quality checks
* Security scanning
* Build artifacts
2. **CD Pipeline**
* Automated deployment
* Blue/green deployment
* Canary releases
* Rollback capability
3. **Infrastructure as Code**
* Terraform for infrastructure
* Ansible for configuration
* Helm charts for Kubernetes
* Docker Compose for local deployment
Operational Considerations
------------------------
Monitoring and Logging
~~~~~~~~~~~~~~~~~~~
1. **Application Monitoring**
* Performance metrics
* Error tracking
* User activity
* API usage
2. **Infrastructure Monitoring**
* Resource utilization
* Network traffic
* Database performance
* Storage capacity
3. **Logging Strategy**
* Centralized log collection
* Structured logging
* Log retention policy
* Log analysis tools
Backup and Recovery
~~~~~~~~~~~~~~~~
1. **Database Backups**
* Regular automated backups
* Point-in-time recovery
* Backup verification
* Off-site backup storage
2. **File Storage Backups**
* Incremental backups
* Version history
* Disaster recovery
* Backup encryption
3. **Recovery Procedures**
* Database restoration
* File recovery
* System rebuild
* Disaster recovery testing
Scaling Strategies
~~~~~~~~~~~~~~~
1. **Vertical Scaling**
* Increase resources for existing servers
* Upgrade database instances
* Enhance storage performance
* Optimize application code
2. **Horizontal Scaling**
* Add application servers
* Database read replicas
* Distributed caching
* Load balancing
3. **Auto-scaling**
* Scale based on CPU/memory usage
* Scale based on request volume
* Scheduled scaling for predictable loads
* Scale to zero for development environments
Security Considerations
--------------------
Network Security
~~~~~~~~~~~~~
1. **Firewall Configuration**
* Restrict access to necessary ports
* Implement network segmentation
* Configure security groups
* DDoS protection
2. **TLS Configuration**
* TLS 1.3 support
* Strong cipher suites
* Certificate management
* HSTS implementation
3. **VPN Access**
* Secure administrative access
* Multi-factor authentication
* Access logging
* Role-based access control
Data Security
~~~~~~~~~~
1. **Data Encryption**
* Encryption in transit
* Encryption at rest
* Key management
* Regular key rotation
2. **Access Controls**
* Principle of least privilege
* Role-based access
* Regular access reviews
* Privileged access management
3. **Compliance**
* Data residency requirements
* Regulatory compliance
* Privacy regulations
* Security certifications
Deployment Checklist
------------------
Pre-Deployment
~~~~~~~~~~~~
1. **Environment Preparation**
* Verify infrastructure requirements
* Configure networking
* Set up security controls
* Prepare databases
2. **Application Preparation**
* Verify application version
* Check dependencies
* Prepare configuration
* Test in staging environment
3. **Documentation**
* Update deployment documentation
* Prepare rollback procedures
* Document configuration changes
* Update user documentation
Deployment
~~~~~~~~~
1. **Backup**
* Backup existing data
* Backup configuration
* Verify backup integrity
* Prepare rollback point
2. **Deployment Steps**
* Follow deployment procedure
* Monitor deployment progress
* Verify service health
* Run smoke tests
3. **Verification**
* Verify functionality
* Check performance
* Validate security
* Test integrations
Post-Deployment
~~~~~~~~~~~~~
1. **Monitoring**
* Monitor application performance
* Watch for errors
* Track user activity
* Monitor resource usage
2. **Communication**
* Notify users of deployment
* Provide release notes
* Address initial feedback
* Support user questions
3. **Optimization**
* Identify performance bottlenecks
* Optimize resource usage
* Fine-tune configuration
* Plan for future improvements
Deployment Environments
---------------------
Development Environment
~~~~~~~~~~~~~~~~~~~~
1. **Purpose**
* Feature development
* Bug fixing
* Testing
* Integration
2. **Characteristics**
* Minimal resources
* Frequent updates
* Non-production data
* Developer access
3. **Configuration**
* Debug mode enabled
* Verbose logging
* Test API keys
* Local development tools
Staging Environment
~~~~~~~~~~~~~~~~
1. **Purpose**
* Pre-production testing
* Performance testing
* User acceptance testing
* Deployment validation
2. **Characteristics**
* Similar to production
* Controlled access
* Sanitized production data
* Regular refreshes
3. **Configuration**
* Production-like settings
* Monitoring enabled
* Test integrations
* Staging API endpoints
Production Environment
~~~~~~~~~~~~~~~~~~~
1. **Purpose**
* Live user access
* Business operations
* Customer data
* Revenue generation
2. **Characteristics**
* High availability
* Scalability
* Security
* Performance
3. **Configuration**
* Optimized settings
* Minimal logging
* Production API keys
* Strict access controls
Future Deployment Enhancements
----------------------------
1. **Containerization Improvements**
* Optimize container images
* Implement container security scanning
* Enhance orchestration
* Improve container networking
2. **Infrastructure as Code**
* Complete IaC implementation
* Automated environment provisioning
* Configuration management
* Compliance as code
3. **Advanced Deployment Strategies**
* Feature flags
* A/B testing infrastructure
* Canary deployments
* Progressive delivery

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

View File

@@ -0,0 +1,156 @@
System Architecture
==================
This section provides a comprehensive overview of the AI-Writer system architecture, including component interactions, data flow, and design patterns.
.. toctree::
:maxdepth: 2
:caption: Architecture Documentation:
overview
components
database_schema
api_design
security
Architecture Overview
-------------------
.. include:: overview.rst
Component Diagram
---------------
.. image:: diagrams/high_level_architecture.png
:alt: AI-Writer High-Level Architecture Diagram
:width: 800px
.. image:: diagrams/database_architecture.png
:alt: AI-Writer Database Architecture Diagram
:width: 800px
.. image:: diagrams/content_generation_workflow.png
:alt: AI-Writer Content Generation Workflow Diagram
:width: 800px
Key Components
------------
The AI-Writer platform consists of several key components:
1. **User Interface Layer**
* Streamlit-based web interface
* Component-based UI architecture
* Responsive design for multiple devices
2. **Application Layer**
* Content generation modules
* AI provider integrations
* Research and analysis tools
* Analytics and reporting
3. **Data Layer**
* Relational database (SQLite/PostgreSQL)
* Vector database (ChromaDB)
* File storage for generated content
4. **Integration Layer**
* API endpoints for external integration
* Authentication and authorization
* Rate limiting and caching
Component Interactions
--------------------
The components interact through well-defined interfaces:
1. **UI to Application Layer**
* Event-driven interaction
* State management through Streamlit session state
* Asynchronous processing for long-running tasks
2. **Application to Data Layer**
* Repository pattern for data access
* Transaction management
* Connection pooling
3. **Application to External Services**
* API client abstractions
* Retry mechanisms
* Circuit breakers for fault tolerance
Data Flow
--------
The typical data flow in the system:
1. User submits content generation request through UI
2. Application layer validates and processes the request
3. AI provider is called to generate content
4. Generated content is stored in the database
5. Content is returned to the UI for display and editing
6. Analytics data is collected and stored
Deployment Architecture
---------------------
AI-Writer supports multiple deployment models:
1. **Single-User Deployment**
* Local installation
* SQLite database
* Local file storage
2. **Multi-User Deployment**
* Docker-based deployment
* PostgreSQL database
* Shared file storage
* Load balancing
3. **Cloud Deployment**
* Kubernetes orchestration
* Cloud database services
* Object storage
* Auto-scaling
Technology Stack
--------------
The AI-Writer platform is built on the following technologies:
1. **Frontend**
* Streamlit
* HTML/CSS/JavaScript
* Plotly for visualizations
2. **Backend**
* Python 3.9+
* FastAPI for API endpoints
* SQLAlchemy for ORM
* ChromaDB for vector storage
3. **AI and ML**
* OpenAI GPT models
* Google Gemini
* Hugging Face transformers
* Sentence transformers for embeddings
4. **Infrastructure**
* Docker
* Docker Compose
* Kubernetes (for cloud deployment)
* GitHub Actions for CI/CD

View File

@@ -0,0 +1,335 @@
Security Architecture
===================
This document outlines the security architecture of the AI-Writer platform, including authentication, authorization, data protection, and security best practices.
Authentication and Authorization
------------------------------
User Authentication
~~~~~~~~~~~~~~~~~
AI-Writer implements a multi-layered authentication system:
1. **Password-based Authentication**
* Passwords are hashed using bcrypt with appropriate work factors
* Password complexity requirements are enforced
* Account lockout after multiple failed attempts
* Password reset via secure email workflow
2. **API Key Authentication**
* Unique API keys for programmatic access
* Keys are stored using secure hashing
* Keys can be scoped to specific permissions
* Keys can be revoked at any time
3. **OAuth 2.0 (for multi-user deployments)**
* Standard OAuth 2.0 flow with authorization code
* JWT tokens with appropriate expiration
* Refresh token rotation
* PKCE for public clients
Authorization Model
~~~~~~~~~~~~~~~~
The platform uses a role-based access control (RBAC) system:
1. **User Roles**
* **Admin**: Full system access
* **Editor**: Content creation and editing
* **Viewer**: Read-only access to content
* **API**: Programmatic access with limited scope
2. **Permission Scopes**
* `content:read`: View content
* `content:write`: Create and edit content
* `content:delete`: Delete content
* `user:read`: View user information
* `user:write`: Modify user information
* `settings:read`: View settings
* `settings:write`: Modify settings
* `api:manage`: Manage API keys
3. **Resource-level Permissions**
* Permissions are checked at the resource level
* Users can only access their own content
* Sharing functionality with explicit permissions
Data Protection
-------------
Encryption
~~~~~~~~~
1. **Data in Transit**
* TLS 1.3 for all communications
* Strong cipher suites
* HSTS implementation
* Certificate pinning for API clients
2. **Data at Rest**
* Database encryption
* Encrypted file storage
* Secure key management
* Regular key rotation
3. **Sensitive Data**
* API keys and credentials are encrypted
* PII is encrypted with separate keys
* Encryption keys are properly secured
API Key Security
~~~~~~~~~~~~~~
1. **Key Generation**
* Keys are generated using cryptographically secure random functions
* Sufficient entropy (256 bits)
* Keys follow a consistent format for validation
2. **Key Storage**
* Only key hashes are stored in the database
* Secure comparison for validation
* Keys are never logged or exposed in error messages
3. **Key Management**
* Keys can be rotated regularly
* Unused keys are automatically expired
* Key usage is logged for audit purposes
Secure Development Practices
--------------------------
Input Validation
~~~~~~~~~~~~~~
1. **API Input Validation**
* All input is validated against schemas
* Type checking and constraint validation
* Protection against injection attacks
* Input sanitization where appropriate
2. **Content Validation**
* Content is scanned for malicious elements
* HTML/Markdown sanitization
* File upload validation and scanning
3. **Error Handling**
* Secure error handling that doesn't leak sensitive information
* Consistent error responses
* Detailed internal logging for troubleshooting
Dependency Management
~~~~~~~~~~~~~~~~~~
1. **Dependency Scanning**
* Regular scanning for vulnerable dependencies
* Automated updates for security patches
* Dependency pinning for stability
2. **Minimal Dependencies**
* Only necessary dependencies are included
* Regular dependency audits
* Preference for well-maintained libraries
3. **Containerization**
* Minimal base images
* Non-root container execution
* Image scanning for vulnerabilities
Logging and Monitoring
--------------------
Security Logging
~~~~~~~~~~~~~~
1. **Authentication Events**
* Login attempts (successful and failed)
* Password changes and resets
* API key creation and usage
* Session management events
2. **Authorization Events**
* Permission checks
* Access denials
* Privilege escalation
* Role changes
3. **System Events**
* Configuration changes
* Service starts and stops
* Database migrations
* Backup and restore operations
Monitoring and Alerting
~~~~~~~~~~~~~~~~~~~~~
1. **Security Monitoring**
* Real-time monitoring for suspicious activities
* Anomaly detection for unusual patterns
* Rate limiting and abuse detection
* Geographic anomaly detection
2. **Performance Monitoring**
* Resource usage tracking
* API response time monitoring
* Error rate monitoring
* Database performance tracking
3. **Alerting**
* Immediate alerts for security incidents
* Escalation procedures
* On-call rotation
* Incident response playbooks
Compliance and Privacy
--------------------
Data Governance
~~~~~~~~~~~~~
1. **Data Classification**
* Clear classification of data sensitivity
* Handling procedures for each classification
* Access controls based on classification
* Retention policies by data type
2. **Data Minimization**
* Only necessary data is collected
* Automatic data pruning
* Anonymization where possible
* Purpose limitation
3. **User Consent**
* Clear consent mechanisms
* Granular permission options
* Easy consent withdrawal
* Consent records
Privacy Features
~~~~~~~~~~~~~
1. **User Privacy Controls**
* Data export functionality
* Account deletion
* Privacy settings management
* Usage tracking opt-out
2. **Data Portability**
* Export in standard formats
* Complete data export
* Machine-readable formats
* Import capabilities
3. **Transparency**
* Clear privacy policy
* Data usage explanations
* Third-party data sharing disclosure
* Processing activities documentation
Security Testing
--------------
Vulnerability Management
~~~~~~~~~~~~~~~~~~~~~
1. **Security Testing**
* Regular penetration testing
* Static application security testing (SAST)
* Dynamic application security testing (DAST)
* Software composition analysis (SCA)
2. **Bug Bounty Program**
* Responsible disclosure policy
* Security researcher engagement
* Vulnerability triage process
* Remediation tracking
3. **Security Reviews**
* Code reviews with security focus
* Architecture security reviews
* Threat modeling
* Security design reviews
Incident Response
~~~~~~~~~~~~~~~
1. **Incident Response Plan**
* Defined incident response procedures
* Roles and responsibilities
* Communication templates
* Escalation paths
2. **Breach Notification**
* Legal compliance with notification requirements
* User communication plan
* Regulatory reporting procedures
* Post-incident analysis
3. **Recovery Procedures**
* Backup and restore testing
* Business continuity planning
* Disaster recovery procedures
* Service level objectives
Security Roadmap
--------------
Planned Security Enhancements
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. **Short-term (0-6 months)**
* Implement multi-factor authentication
* Enhance API key management
* Improve security logging
* Conduct initial penetration test
2. **Medium-term (6-12 months)**
* Implement security information and event management (SIEM)
* Enhance data encryption
* Develop comprehensive security training
* Implement automated security testing in CI/CD
3. **Long-term (12+ months)**
* Achieve SOC 2 compliance
* Implement advanced threat protection
* Develop zero-trust architecture
* Enhance privacy features for international compliance

View File

@@ -0,0 +1,56 @@
Introduction
============
What is AI-Writer?
-----------------
AI-Writer (Alwrity) is a comprehensive AI-powered content creation platform designed to help content creators, marketers, and businesses generate high-quality content efficiently. The platform leverages advanced language models and AI technologies to assist in creating various types of content, from social media posts to full-length articles.
Key Features
-----------
1. **Multi-Platform Content Generation**
* LinkedIn content (posts, articles, profiles)
* Twitter/X posts and threads
* Blog articles and SEO-optimized content
* Marketing copy and email templates
2. **AI Research Integration**
* Web crawling for relevant information
* Content summarization
* Fact-checking capabilities
* Citation and reference management
3. **Database Integration**
* Content storage and retrieval
* Vector database for semantic search
* Content versioning and history
4. **Analytics Dashboard**
* Content performance metrics
* Usage statistics
* AI model performance analysis
5. **Customization Options**
* Personalized content templates
* Brand voice and tone settings
* Custom workflows
6. **Multiple AI Provider Support**
* OpenAI (GPT models)
* Google Gemini
* Anthropic Claude (coming soon)
* Local models (coming soon)
Project History
--------------
AI-Writer was created to address the growing need for high-quality content creation at scale. The project aims to democratize access to advanced AI writing capabilities while maintaining a focus on quality, accuracy, and ethical content generation.
The platform continues to evolve with new features and improvements based on user feedback and advancements in AI technology.

View File

@@ -0,0 +1,139 @@
# Migration Plan: Alwrity (AI-Writer) to Enterprise-Ready Architecture
## 1. Background & Motivation
Alwrity (AI-Writer) is currently an open-source, Streamlit-based project for AI-powered content creation, SEO, analytics, and more. To serve enterprise customers, we need to move to a scalable, secure, and maintainable architecture, reusing as much of the existing Python codebase as possible while replacing the UI and improving backend robustness.
---
## 2. Current State
- **UI:** Streamlit (great for prototyping, not for enterprise)
- **Backend:** Python modules for AI writing, SEO, analytics, chatbot, etc.
- **Database:** SQLite, ChromaDB, some service layers for Twitter and content
- **AI/ML:** Integrates with OpenAI, Gemini, and other providers
---
## 3. Design Directions & Tech Stack Recommendations
### A. Frontend
- **React** (TypeScript) for scalable, maintainable UI
- **UI Library:** Material-UI (MUI) or Ant Design
- **State/Data:** React Query, Context API or Redux Toolkit
### B. Backend
- **FastAPI** (Python): async, high-performance, easy to wrap existing modules
- **Task Queue:** Celery + Redis for background jobs (if needed)
### C. Database & Storage
- **PostgreSQL** for structured data
- **Redis** for caching and task queue
- **Vector DB:** Pinecone, Weaviate, or Qdrant for semantic search (if needed)
- **Blob Storage:** AWS S3 or Azure Blob for files
### D. AI/ML Integration
- Reuse existing Python modules
- Serve custom models via FastAPI endpoints
### E. Authentication
- **Auth0** or **Keycloak** for OAuth2/SSO, or FastAPI JWT for MVP
### F. DevOps
- **Docker** for containerization
- **GitHub Actions** for CI/CD
- **(Optional) Kubernetes** for orchestration
### G. Security & Compliance
- SSO, RBAC, audit logs, encryption, GDPR/SOC2 readiness
---
## 4. Migration Plan: Step-by-Step
### Phase 1: Preparation
- Audit codebase for reusable business logic
- Separate UI code from backend logic
- Set up monorepo or separate repos for backend (Python/FastAPI) and frontend (React)
### Phase 2: Backend API Layer
- Scaffold FastAPI app
- Wrap existing Python modules as API endpoints (content generation, SEO, analytics, etc.)
- Add authentication (JWT for MVP, SSO for production)
- Write unit/integration tests
### Phase 3: Frontend Migration
- Scaffold React app (TypeScript)
- Set up routing, authentication, dashboard layout
- For each Streamlit feature, create a React page/component
- Use MUI/Ant Design for UI
- Fetch data from FastAPI using React Query
### Phase 4: Feature Parity & Enhancements
- Migrate all features, one by one, to new stack
- Use Celery + Redis for long-running jobs
- Add UI/UX improvements (loading, error handling, feedback)
### Phase 5: Productionization
- Dockerize frontend and backend
- Set up CI/CD with GitHub Actions
- Add logging, monitoring (Sentry, Prometheus, Grafana)
- Harden security (HTTPS, CORS, secure cookies, etc.)
### Phase 6: Launch & Iterate
- Deploy to cloud
- Gather user feedback and iterate
---
## 5. Prioritized Modules for Migration
### Best-fit modules to start with (already decoupled from UI):
1. **AI Writers (lib/ai_writers/):** Blog, news, social, email, story, YouTube script writers
2. **SEO Tools (lib/ai_seo_tools/):** Keyword analyzer, meta generator, content gap, enterprise SEO, content calendar
3. **Website Analyzer (lib/utils/website_analyzer/):** Performance, SEO, content quality analysis
4. **Analytics/Performance (lib/content_performance_predictor/):** Content analytics and prediction
5. **Chatbot Core (lib/chatbot_custom/core/):** Workflow engine, tool router, intent analyzer, context manager
6. **Database Services (lib/database/):** Twitter and content management service layers
7. **AI Marketing Tools (lib/ai_marketing_tools/ai_backlinker/):** Backlinking and marketing automation
### Modules to avoid for now:
- Streamlit UI scripts and thin wrappers
---
## 6. Summary Table
| Layer | Stack/Tooling | Why? |
|---------------|-----------------------------|--------------------------------------------|
| Frontend | React + TypeScript + MUI | Modern, scalable, huge ecosystem |
| Backend | FastAPI (Python) | Async, high-perf, easy to wrap old code |
| Auth | FastAPI JWT/Auth0/Keycloak | Secure, enterprise-ready |
| DB | PostgreSQL, Redis | Reliable, scalable, Python-friendly |
| AI/ML | Existing Python modules | Maximum code reuse |
| Task Queue | Celery + Redis | For background/async jobs |
| DevOps | Docker, GitHub Actions | Easy deployment, automation |
---
## 7. Next Steps
- Start with AI Writers and SEO Tools: wrap as FastAPI endpoints
- Gradually add Website Analyzer, Analytics, and Chatbot features
- Leave UI and Streamlit code aside; focus on modules that dont depend on Streamlit
- Build React frontend to consume new API endpoints
---
## 8. Optional: Sample FastAPI Endpoint (for reference)
```python
from fastapi import FastAPI
from lib.ai_writers.blog_writer import generate_blog_post
app = FastAPI()
@app.post("/generate-blog/")
def generate_blog(data: BlogRequest):
return generate_blog_post(data.topic, data.keywords)
```
---
**This document should be updated as the migration progresses and new architectural decisions are made.**

View File

@@ -0,0 +1,106 @@
#!/usr/bin/env python3
"""
Test script to verify frontend-backend communication.
"""
import requests
import time
def test_backend_endpoints():
"""Test all backend endpoints"""
base_url = "http://localhost:8000"
print("🧪 Testing Backend Endpoints...")
# Test health endpoint
print("\n1⃣ Testing health endpoint...")
try:
response = requests.get(f"{base_url}/health")
if response.status_code == 200:
print("✅ Health endpoint working")
else:
print(f"❌ Health endpoint failed: {response.status_code}")
except Exception as e:
print(f"❌ Health endpoint error: {e}")
# Test onboarding check
print("\n2⃣ Testing onboarding check...")
try:
response = requests.get(f"{base_url}/api/check-onboarding")
if response.status_code == 200:
data = response.json()
print(f"✅ Onboarding check working: {data}")
else:
print(f"❌ Onboarding check failed: {response.status_code}")
except Exception as e:
print(f"❌ Onboarding check error: {e}")
# Test onboarding start
print("\n3⃣ Testing onboarding start...")
try:
response = requests.post(f"{base_url}/api/onboarding/start")
if response.status_code == 200:
data = response.json()
print(f"✅ Onboarding start working: {data}")
else:
print(f"❌ Onboarding start failed: {response.status_code}")
except Exception as e:
print(f"❌ Onboarding start error: {e}")
# Test onboarding step
print("\n4⃣ Testing onboarding step...")
try:
response = requests.get(f"{base_url}/api/onboarding/step")
if response.status_code == 200:
data = response.json()
print(f"✅ Onboarding step working: {data}")
else:
print(f"❌ Onboarding step failed: {response.status_code}")
except Exception as e:
print(f"❌ Onboarding step error: {e}")
def test_frontend_communication():
"""Test if frontend can reach backend"""
print("\n🌐 Testing Frontend-Backend Communication...")
# Simulate frontend API calls
base_url = "http://localhost:8000"
# Test the exact endpoints the frontend uses
endpoints = [
("GET", "/api/check-onboarding"),
("POST", "/api/onboarding/start"),
("GET", "/api/onboarding/step"),
("GET", "/api/onboarding/api-keys"),
("POST", "/api/onboarding/api-keys"),
("GET", "/api/onboarding/progress"),
]
for method, endpoint in endpoints:
print(f"\nTesting {method} {endpoint}...")
try:
if method == "GET":
response = requests.get(f"{base_url}{endpoint}")
elif method == "POST":
response = requests.post(f"{base_url}{endpoint}")
if response.status_code in [200, 404]: # 404 is expected for some endpoints without data
print(f"{method} {endpoint} - Status: {response.status_code}")
else:
print(f"{method} {endpoint} - Status: {response.status_code}")
except Exception as e:
print(f"{method} {endpoint} - Error: {e}")
if __name__ == "__main__":
print("🚀 Starting Frontend-Backend Communication Test...")
# Wait a moment for services to be ready
print("⏳ Waiting for services to be ready...")
time.sleep(2)
test_backend_endpoints()
test_frontend_communication()
print("\n🎯 Test complete!")
print("📝 If all tests pass, the frontend should work correctly.")
print("🌐 Visit http://localhost:3000 to test the onboarding flow.")

View File

@@ -0,0 +1,82 @@
#!/usr/bin/env python3
"""
Test script to reset onboarding state and test the onboarding flow.
"""
import sys
import os
import sqlite3
# Add the backend directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
def reset_database():
"""Reset the onboarding database"""
db_path = "backend/onboarding.db"
if os.path.exists(db_path):
os.remove(db_path)
print("✅ Database file removed")
else:
print(" No database file found")
def check_onboarding_status():
"""Check the current onboarding status"""
import requests
try:
response = requests.get("http://localhost:8000/api/check-onboarding")
if response.status_code == 200:
data = response.json()
print(f"📊 Onboarding Status: {data}")
return data
else:
print(f"❌ Error: {response.status_code}")
return None
except Exception as e:
print(f"❌ Error checking onboarding status: {e}")
return None
def test_onboarding_flow():
"""Test the complete onboarding flow"""
print("\n🧪 Testing Onboarding Flow...")
# Step 1: Check initial status
print("\n1⃣ Checking initial onboarding status...")
status = check_onboarding_status()
if status and status.get('onboarding_required'):
print("✅ Correctly shows onboarding required for first-time user")
else:
print("❌ Incorrectly shows onboarding complete")
# Step 2: Start onboarding
print("\n2⃣ Starting onboarding session...")
try:
import requests
response = requests.post("http://localhost:8000/api/onboarding/start")
if response.status_code == 200:
print("✅ Onboarding session started")
else:
print(f"❌ Error starting onboarding: {response.status_code}")
except Exception as e:
print(f"❌ Error: {e}")
# Step 3: Check status again
print("\n3⃣ Checking status after starting onboarding...")
status = check_onboarding_status()
if status and status.get('onboarding_required'):
print("✅ Still shows onboarding required (correct)")
else:
print("❌ Incorrectly shows onboarding complete")
if __name__ == "__main__":
print("🔄 Resetting onboarding state...")
reset_database()
print("\n⏳ Waiting for backend to restart...")
import time
time.sleep(3)
test_onboarding_flow()
print("\n🎯 Test complete! Check your frontend at http://localhost:3000")