CV Extraction WebSocket Implementation
This document explains the implementation of CV extraction using WebSocket communication in the Add Candidate page.
Files Created/Modified
1. New Hook: lib/hooks/useCvExtractionWebSocket.ts
A custom React hook that handles CV extraction via WebSocket:
- Purpose: Parse-only CV processing without database persistence
- WebSocket Message Format: Follows the CV extraction guide specifications
- Features:
- File upload with progress tracking
- WebSocket-based CV extraction
- Real-time processing status updates
- Error handling and timeouts
- TypeScript interfaces for type safety
Key Functions:
extractCV(file: File): Upload file and extract CV datareset(): Reset extraction state- Real-time status updates via WebSocket messages
2. Updated Page: app/company/pipeline/addCandidate/page.tsx
Enhanced the Add Candidate page to use the new WebSocket-based extraction:
New Features:
- File Upload UI: Visual file selection with status
- Processing Indicators: Real-time upload and processing progress
- Success/Error States: Clear feedback to users
- Debug Tools: WebSocket connection status (development only)
- Auto-navigation: Automatically advance to review step after successful extraction
UI Components:
- File upload dropzone
- Selected file preview with remove option
- AI Extract button with loading states
- Processing progress bars
- Error/success notifications
- Review section with extracted data display
3. Upload API: app/api/upload/route.ts
Simple file upload endpoint for demo purposes:
- Validation: File type and size checking
- Mock MinIO URLs: Simulates file storage for development
- Error Handling: Proper HTTP status codes and error messages
- Security: File type validation and size limits
WebSocket Message Flow
1. File Upload
// User selects file → Upload to API → Get MinIO URL
const fileUrl = await uploadFile(file)
2. CV Extraction Request
// Send extraction request via WebSocket
const message = {
type: 'cv_extraction_request',
pipeline_type: 'cv_extraction',
payload: {
id: requestId,
file_url: fileUrl
}
}
3. WebSocket Responses
// Processing started
{ type: 'processing_started', id: requestId, status: 'processing' }
// Success response
{
type: 'pipeline_result',
data: {
data: {
first_name: 'John',
last_name: 'Doe',
skills: [...],
experiences: [...],
// ... other extracted data
}
}
}
// Error response
{ type: 'pipeline_error', id: requestId, error: 'Error message' }
Data Transformation
The hook transforms server response fields to match frontend interfaces:
// Server → Frontend mapping
first_name → firstName
last_name → lastName
job_title → jobTitle
years_experience → yearsExperience
// etc.
Development Features
Debug Tools
- WebSocket connection status
- Raw extracted data preview
- Error logging and diagnostics
Progress Tracking
- Upload progress bar
- Processing status indicators
- Real-time feedback
Usage Example
const cvExtraction = useCvExtractionWebSocket()
// Extract CV data
const result = await cvExtraction.extractCV(file)
// Check status
if (cvExtraction.isProcessing) {
// Show loading state
}
if (cvExtraction.result) {
// Display extracted data
}
if (cvExtraction.error) {
// Handle error
}
Key Benefits
- Real-time Processing: Live updates via WebSocket
- Parse-only: No database persistence for preview functionality
- Type Safety: Full TypeScript interfaces
- Error Handling: Comprehensive error states
- User Experience: Clear progress indicators and feedback
- Development Tools: Debug tools for troubleshooting
Integration with Existing System
- Uses existing
WebSocketContextfor connection management - Compatible with current pipeline architecture
- Follows established patterns for hooks and components
- Integrates with existing UI components (Button, Input, etc.)
Future Enhancements
- Real MinIO Integration: Replace mock upload with actual MinIO
- Drag & Drop: Enhanced file upload UX
- Multiple Files: Batch CV processing
- Preview: PDF/document preview before extraction
- Retry Logic: Automatic retry for failed extractions