youtube-summarizer/IMMEDIATE_FIX_PLAN.md

7.8 KiB

Immediate Fix Plan for Epic 4 Integration

Quick Fix Steps (30 minutes to working state)

Step 1: Fix Model Table Arguments (5 min)

Add extend_existing=True to prevent duplicate table errors:

# backend/models/rag_models.py - Line 47
class RAGChunk(Model):
    """Text chunks for RAG processing and vector embeddings."""
    __tablename__ = "rag_chunks"
    __table_args__ = {'extend_existing': True}  # ADD THIS LINE

# backend/models/export_models.py - Line 47  
class EnhancedExport(Model):
    """Enhanced export configurations and results."""
    __tablename__ = "enhanced_exports"
    __table_args__ = {'extend_existing': True}  # ADD THIS LINE

class ExportSection(Model):
    """Export sections with timestamps."""
    __tablename__ = "export_sections"
    __table_args__ = {'extend_existing': True}  # ADD THIS LINE

Step 2: Create Missing Epic 4 Models (10 min)

Create the missing models that multi-agent system needs:

# backend/models/agent_models.py (NEW FILE)
"""Models for multi-agent analysis system"""

from sqlalchemy import Column, String, Text, Float, DateTime, ForeignKey, JSON
from sqlalchemy.orm import relationship
from backend.models.base import Model, GUID
import uuid
from datetime import datetime

class AgentSummary(Model):
    """Multi-agent analysis results"""
    __tablename__ = "agent_summaries"
    __table_args__ = {'extend_existing': True}
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    summary_id = Column(String(36), ForeignKey("summaries.id", ondelete='CASCADE'))
    agent_type = Column(String(20), nullable=False)  # technical, business, user, synthesis
    agent_summary = Column(Text, nullable=True)
    key_insights = Column(JSON, nullable=True)
    focus_areas = Column(JSON, nullable=True)
    recommendations = Column(JSON, nullable=True)
    confidence_score = Column(Float, nullable=True)
    processing_time_seconds = Column(Float, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Relationship
    summary = relationship("Summary", back_populates="agent_analyses")

# backend/models/template_models.py (NEW FILE)
"""Models for prompt template system"""

from sqlalchemy import Column, String, Text, Float, DateTime, Boolean, Integer, JSON
from backend.models.base import Model
import uuid
from datetime import datetime

class PromptTemplate(Model):
    """Custom prompt templates for AI models"""
    __tablename__ = "prompt_templates"
    __table_args__ = {'extend_existing': True}
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = Column(String(36), nullable=True)
    name = Column(String(200), nullable=False)
    description = Column(Text, nullable=True)
    prompt_text = Column(Text, nullable=False)
    domain_category = Column(String(50), nullable=True)
    model_config = Column(JSON, nullable=True)
    is_public = Column(Boolean, default=False)
    usage_count = Column(Integer, default=0)
    rating = Column(Float, default=0.0)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

Step 3: Update Models init.py (2 min)

Update to import models in correct order:

# backend/models/__init__.py
"""Database and API models for YouTube Summarizer."""

# Base models (no Epic 4 dependencies)
from .user import User, RefreshToken, APIKey, EmailVerificationToken, PasswordResetToken
from .summary import Summary, ExportHistory
from .batch_job import BatchJob, BatchJobItem
from .playlist_models import PlaylistVideo, MultiVideoAnalysis

# Epic 4 base models (no cross-dependencies)
from .template_models import PromptTemplate  # NEW
from .agent_models import AgentSummary  # NEW

# Epic 4 dependent models (reference above models)
from .export_models import EnhancedExport, ExportSection
from .rag_models import RAGChunk, VectorEmbedding, SemanticSearchResult

__all__ = [
    # User models
    "User", "RefreshToken", "APIKey", "EmailVerificationToken", "PasswordResetToken",
    # Summary models
    "Summary", "ExportHistory",
    # Batch job models
    "BatchJob", "BatchJobItem",
    # Playlist and multi-video models
    "PlaylistVideo", "MultiVideoAnalysis",
    # Epic 4 models
    "PromptTemplate", "AgentSummary",
    "EnhancedExport", "ExportSection",
    "RAGChunk", "VectorEmbedding", "SemanticSearchResult",
]

Step 4: Update Summary Model (2 min)

Add relationship to agent analyses:

# backend/models/summary.py
# Add to Summary class:
class Summary(Model):
    # ... existing fields ...
    
    # Add this relationship
    agent_analyses = relationship("AgentSummary", back_populates="summary", cascade="all, delete-orphan")

Step 5: Apply Database Migrations (5 min)

cd /Users/enias/projects/my-ai-projects/apps/youtube-summarizer
source ../venv/bin/activate

# Check current status
PYTHONPATH=. ../venv/bin/python3 -m alembic current

# Apply the Epic 4 migration
PYTHONPATH=. ../venv/bin/python3 -m alembic upgrade add_epic_4_features

# If that fails, create tables manually via Python
PYTHONPATH=. ../venv/bin/python3 -c "
from backend.core.database import engine
from backend.core.database_registry import registry
from backend.models import *
registry.create_all_tables(engine)
print('Tables created successfully')
"

Step 6: Re-enable API Routers (3 min)

# backend/main.py - Lines 25-26 and 87-88
# UNCOMMENT these lines:
from backend.api.multi_agent import router as multi_agent_router
# from backend.api.analysis_templates import router as analysis_templates_router

# Lines 87-88, UNCOMMENT:
app.include_router(multi_agent_router)  # Multi-agent analysis system
# app.include_router(analysis_templates_router)  # If this router exists

Step 7: Test the System (3 min)

# Start backend
./scripts/restart-backend.sh

# Check for errors in logs
tail -f logs/backend.log

# Test multi-agent API
curl -X GET http://localhost:8000/api/analysis/health

# Test with frontend
npm run dev
# Navigate to http://localhost:3002

If Quick Fix Doesn't Work

Nuclear Option - Fresh Database

# Backup current database
cp data/app.db data/app.db.backup_$(date +%Y%m%d_%H%M%S)

# Remove current database
rm data/app.db

# Start fresh - backend will create all tables
./scripts/restart-backend.sh

Verification Checklist

Backend starts without errors No "table already exists" errors in logs Multi-agent health endpoint returns 200 Frontend can load without errors Can process a video with multi-agent analysis Export features work

Common Error Solutions

Error: "Table 'rag_chunks' is already defined"

Solution: Add __table_args__ = {'extend_existing': True} to the model class

Error: "Foreign key references non-existent table 'prompt_templates'"

Solution: Create PromptTemplate model and ensure it's imported before EnhancedExport

Error: "Circular import detected"

Solution: Use string references in relationships: relationship("ModelName", ...)

Error: "No module named 'backend.api.multi_agent'"

Solution: Ensure multi_agent.py exists in backend/api/ directory

Expected Result

After these fixes:

  1. All Epic 4 models properly defined and registered
  2. Multi-agent API endpoints accessible at /api/analysis/multi-agent/{video_id}
  3. Enhanced export ready for Story 4.4 implementation
  4. Database has all required tables for Epic 4 features
  5. No circular dependencies or import errors

Next Steps After Fix

  1. Test multi-agent analysis with a real YouTube video
  2. Verify agent summaries are saved to database
  3. Begin implementing Story 4.4 (Enhanced Export) features
  4. Create integration tests for Epic 4 features

This immediate fix plan should get the system working within 30 minutes, allowing you to continue with Epic 4 development.