12 KiB
Story Writer Backend Migration - Review & Next Steps
✅ What Was Accomplished
1. Backend Service Layer (backend/services/story_writer/)
Status: ✅ Complete
story_service.py- Core story generation service- Migrated from
ToBeMigrated/ai_writers/ai_story_writer/ai_story_generator.py - Updated imports to use
services.llm_providers.main_text_generation - Added
user_idparameter for subscription integration - Removed Streamlit dependencies
- Modular methods:
generate_premise()- Generate story premisegenerate_outline()- Generate story outlinegenerate_story_start()- Generate story beginningcontinue_story()- Continue story generationgenerate_full_story()- Complete story generation with iterations
- Migrated from
Key Features:
- ✅ Subscription support via
main_text_generation - ✅ Supports both Gemini and HuggingFace providers
- ✅ Proper error handling with HTTPException support
- ✅ Comprehensive logging
2. API Layer (backend/api/story_writer/)
Status: ✅ Complete
-
router.py- RESTful API endpoints- Synchronous endpoints: premise, outline, start, continue
- Asynchronous endpoint: full story generation with task management
- Task status and result endpoints
- Cache management endpoints
- Health check endpoint
-
task_manager.py- Async task execution- Background task execution
- Progress tracking (0-100%)
- Status management (pending, processing, completed, failed)
- Automatic cleanup of old tasks
-
cache_manager.py- Result caching- MD5-based cache key generation
- Cache statistics
- Cache clearing
3. Models (backend/models/story_models.py)
Status: ✅ Complete
- Pydantic models for type-safe API:
StoryGenerationRequest- Input parametersStoryPremiseResponse- Premise generation responseStoryOutlineResponse- Outline generation responseStoryContentResponse- Story content responseStoryFullGenerationResponse- Complete story responseStoryContinueRequest/Response- Continuation modelsTaskStatus- Task tracking model
4. Router Registration
Status: ✅ Complete
- Added to
alwrity_utils/router_manager.pyin optional routers section - Automatic registration on app startup
- Error handling for graceful failures
📊 API Endpoints Summary
Synchronous Endpoints
POST /api/story/generate-premise
POST /api/story/generate-outline
POST /api/story/generate-start
POST /api/story/continue
Asynchronous Endpoints
POST /api/story/generate-full → Returns task_id
GET /api/story/task/{task_id}/status
GET /api/story/task/{task_id}/result
Utility Endpoints
GET /api/story/health
GET /api/story/cache/stats
POST /api/story/cache/clear
🎯 Next Steps - Implementation Roadmap
Phase 1: Backend Testing & Validation (Priority: High)
Estimated Time: 1-2 days
Tasks:
-
API Testing
- Test all synchronous endpoints with Postman/curl
- Test async task flow (generate-full → status → result)
- Verify subscription limits work (429 errors)
- Test with both Gemini and HuggingFace providers
- Test error handling (invalid inputs, API failures)
-
Integration Testing
- Test with real user authentication
- Verify usage tracking in database
- Test cache functionality
- Test task cleanup (old tasks removal)
-
Performance Testing
- Measure response times for each endpoint
- Test concurrent requests
- Monitor memory usage during long story generation
Deliverables:
- API test suite (Postman collection or pytest)
- Test results document
- Performance benchmarks
Phase 2: Frontend Foundation (Priority: High)
Estimated Time: 2-3 days
Tasks:
-
Create Frontend Structure
- Create
frontend/src/components/StoryWriter/directory - Create
frontend/src/services/storyWriterApi.ts(API client) - Create
frontend/src/hooks/useStoryWriterState.ts(state management) - Create
frontend/src/hooks/useStoryWriterPhaseNavigation.ts(phase navigation)
- Create
-
API Service Layer
// frontend/src/services/storyWriterApi.ts - generatePremise() - generateOutline() - generateStoryStart() - continueStory() - generateFullStory() // async with polling - getTaskStatus() - getTaskResult() -
State Management Hook
// frontend/src/hooks/useStoryWriterState.ts - Story parameters (persona, setting, characters, etc.) - Premise, outline, story content - Generation progress - Task management -
Phase Navigation Hook
// Similar to usePhaseNavigation.ts from Blog Writer Phases: Setup → Premise → Outline → Writing → Export
Deliverables:
- Frontend directory structure
- API service with TypeScript types
- State management hooks
- Phase navigation hook
Phase 3: UI Components - Core (Priority: High)
Estimated Time: 3-4 days
Tasks:
-
Main Component
StoryWriter.tsx- Main container component- Similar structure to
BlogWriter.tsx
-
Phase Components
-
StorySetup.tsx- Phase 1: Input story parameters- Persona selector (11 options)
- Story setting input
- Characters input
- Plot elements input
- Writing style, tone, POV selectors
- Audience age group, content rating, ending preference
-
StoryPremise.tsx- Phase 2: Review premise- Display generated premise
- Regenerate option
- Continue to outline button
-
StoryOutline.tsx- Phase 3: Review outline- Display generated outline
- Edit/refine option
- Continue to writing button
-
StoryContent.tsx- Phase 4: Generated story- Display story content
- Markdown editor for editing
- Continue generation button
- Progress indicator for async generation
-
StoryExport.tsx- Phase 5: Export options- Download as text/markdown
- Copy to clipboard
- Share options
-
-
Utility Components
HeaderBar.tsx- Phase navigation header (like Blog Writer)PhaseContent.tsx- Phase content wrapperTaskProgressModal.tsx- Progress modal for async operations
Deliverables:
- All phase components
- Main StoryWriter component
- Utility components
Phase 4: CopilotKit Integration (Priority: Medium)
Estimated Time: 2-3 days
Tasks:
-
CopilotKit Actions
useStoryWriterCopilotActions.tshook- Actions:
generateStoryPremise- Generate premisegenerateStoryOutline- Generate outlinestartStoryWriting- Begin story generationcontinueStoryWriting- Continue storyrefineStoryOutline- Refine outlineexportStory- Export story
-
CopilotKit Sidebar
WriterCopilotSidebar.tsx- Suggestions sidebar- Context-aware suggestions based on current phase
- Action buttons for common tasks
-
Integration
- Register actions in StoryWriter component
- Connect sidebar to component state
- Test CopilotKit interactions
Reference: frontend/src/components/BlogWriter/BlogWriterUtils/useBlogWriterCopilotActions.ts
Deliverables:
- CopilotKit actions hook
- CopilotKit sidebar component
- Integrated with main component
Phase 5: Polish & Enhancement (Priority: Low)
Estimated Time: 2-3 days
Tasks:
-
Error Handling
- User-friendly error messages
- Retry mechanisms
- Error boundaries
-
Loading States
- Skeleton loaders
- Progress indicators
- Optimistic UI updates
-
UX Improvements
- Keyboard shortcuts
- Auto-save draft
- Undo/redo functionality
- Story preview
-
Styling
- Match Blog Writer design system
- Responsive design
- Dark mode support (if applicable)
Deliverables:
- Polished UI/UX
- Error handling improvements
- Loading states
Phase 6: Illustration Support (Optional - Future)
Estimated Time: 3-4 days
Tasks:
-
Backend Migration
- Migrate
story_illustrator.pyto backend service - Create illustration API endpoints
- Integrate with image generation API
- Migrate
-
Frontend Integration
- Add illustration phase
- Illustration generation UI
- Preview and download illustrations
Note: Defer to Phase 2 if core story generation is priority
🚀 Quick Start Guide
Testing Backend API
# Health check
curl http://localhost:8000/api/story/health
# Generate premise (requires auth token)
curl -X POST http://localhost:8000/api/story/generate-premise \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"persona": "Award-Winning Science Fiction Author",
"story_setting": "A futuristic city in 2150",
"character_input": "John, a brave explorer",
"plot_elements": "The hero's journey",
"writing_style": "Formal",
"story_tone": "Suspenseful",
"narrative_pov": "Third Person Limited",
"audience_age_group": "Adults",
"content_rating": "PG-13",
"ending_preference": "Happy"
}'
Frontend Development Order
-
Start with API Service (
storyWriterApi.ts)- Define all API calls
- Add TypeScript types
- Test with mock data
-
Build State Management (
useStoryWriterState.ts)- Define state structure
- Add state setters/getters
- Test state updates
-
Create Phase Navigation (
useStoryWriterPhaseNavigation.ts)- Define phases
- Add navigation logic
- Test phase transitions
-
Build Components (Start with Setup phase)
- StorySetup component
- Test form submission
- Connect to API
-
Add Remaining Phases
- Premise → Outline → Writing → Export
- Test each phase independently
-
Integrate CopilotKit
- Add actions
- Connect sidebar
- Test interactions
📝 Key Decisions Made
- Modular Structure: Follows Blog Writer patterns for consistency
- Async Task Pattern: Long-running operations use task management with polling
- Subscription Integration: Automatic via
main_text_generation - Provider Support: Works with both Gemini and HuggingFace automatically
- Caching: Results cached to avoid duplicate generations
- Error Handling: Comprehensive with HTTPException support
⚠️ Important Notes
- Authentication Required: All endpoints require valid Clerk authentication token
- Subscription Limits: Will return 429 if limits exceeded
- Long Operations: Full story generation can take several minutes - use async pattern
- Task Cleanup: Tasks older than 1 hour are automatically cleaned up
- Cache Keys: Based on request parameters - identical requests return cached results
🎯 Recommended Immediate Next Steps
-
Test Backend API (Today)
- Verify all endpoints work
- Test subscription integration
- Document any issues
-
Create Frontend API Service (Day 1-2)
- Set up TypeScript types
- Create API client functions
- Test with Postman/curl responses
-
Build StorySetup Component (Day 2-3)
- Create form with all parameters
- Connect to API
- Test premise generation
-
Add Phase Navigation (Day 3-4)
- Implement phase hook
- Add HeaderBar component
- Test phase transitions
-
Complete Remaining Phases (Day 4-7)
- Build each phase component
- Connect to API
- Test full flow
📚 Reference Files
-
Blog Writer (Reference implementation):
frontend/src/components/BlogWriter/BlogWriter.tsxfrontend/src/hooks/usePhaseNavigation.tsfrontend/src/components/BlogWriter/BlogWriterUtils/useBlogWriterCopilotActions.ts
-
Backend Patterns:
backend/api/blog_writer/router.pybackend/api/blog_writer/task_manager.pybackend/services/blog_writer/blog_service.py
✅ Success Criteria
- All backend endpoints tested and working
- Frontend API service complete
- All phase components built
- Phase navigation working
- CopilotKit integrated
- Full story generation flow works end-to-end
- Error handling comprehensive
- Loading states implemented
- UI matches Blog Writer design
Ready to proceed with Phase 1 (Backend Testing) or Phase 2 (Frontend Foundation)?