File size: 9,581 Bytes
3370983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""
Voice Screener API Router.
Handles voice screening sessions, configuration, and audio/transcript saving.
"""
import logging
import os
from typing import Optional
from pathlib import Path

from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel

from src.backend.agents.voice_screening.session_service import (
    get_session_config,
    save_voice_screening_session
)
from src.backend.agents.voice_screening.audio_processor import combine_and_export_audio

logger = logging.getLogger(__name__)

router = APIRouter()


# Request/Response Models
class CreateSessionRequest(BaseModel):
    candidate_id: str


class CreateSessionResponse(BaseModel):
    session_id: str
    candidate_name: str
    job_title: str
    message: str


class SessionConfigResponse(BaseModel):
    candidate_name: str
    job_title: str
    instructions: str
    questions: list[str]
    config: dict


class SaveSessionRequest(BaseModel):
    session_id: str
    candidate_id: str
    transcript_text: str
    proxy_token: str  # Token to retrieve audio chunks from proxy


class SaveSessionResponse(BaseModel):
    audio_file_path: Optional[str]
    message: str


@router.post("/session/create", response_model=CreateSessionResponse)
async def create_session(request: CreateSessionRequest):
    """
    Create a new voice screening session for a candidate.
    
    Args:
        request: Contains candidate_id
        
    Returns:
        Session information including session_id
    """
    try:
        import uuid
        
        # Generate session ID
        session_id = str(uuid.uuid4())
        
        # Get session config (validates candidate exists)
        config = get_session_config(request.candidate_id)
        
        logger.info(f"Created session {session_id} for candidate {request.candidate_id}")
        
        return CreateSessionResponse(
            session_id=session_id,
            candidate_name=config["candidate_name"],
            job_title=config["job_title"],
            message="Session created successfully"
        )
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        logger.error(f"Error creating session: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Failed to create session: {str(e)}")


@router.get("/session/{session_id}/config", response_model=SessionConfigResponse)
async def get_config(session_id: str, candidate_id: str = Query(...)):
    """
    Get session configuration for a candidate.
    
    Args:
        session_id: Session identifier (for logging)
        candidate_id: Candidate UUID
        
    Returns:
        Session configuration including instructions and questions
    """
    try:
        config = get_session_config(candidate_id)
        
        logger.info(f"Retrieved config for session {session_id}, candidate {candidate_id}")
        
        return SessionConfigResponse(
            candidate_name=config["candidate_name"],
            job_title=config["job_title"],
            instructions=config["instructions"],
            questions=config["questions"],
            config=config["config"]
        )
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        logger.error(f"Error getting config: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Failed to get config: {str(e)}")


@router.post("/session/{session_id}/save", response_model=SaveSessionResponse)
async def save_session(session_id: str, request: SaveSessionRequest):
    """
    Save audio recording and transcript for a session.
    
    This endpoint:
    1. Retrieves audio chunks from the proxy using the token
    2. Combines and saves the audio file
    3. Saves transcript and audio path to database
    
    Args:
        session_id: Session identifier (must match request.session_id)
        request: Contains candidate_id, transcript_text, and proxy_token
        
    Returns:
        Audio file path and success message
    """
    if session_id != request.session_id:
        raise HTTPException(status_code=400, detail="Session ID mismatch")
    
    try:
        # Import here to avoid circular dependency
        import requests
        
        # Get proxy URL from environment
        proxy_url = os.getenv("WEBSOCKET_PROXY_URL", "ws://localhost:8000/ws/realtime")
        proxy_base = proxy_url.replace("ws://", "http://").replace("wss://", "https://").replace("/ws/realtime", "")
        
        # Retrieve audio chunks from proxy
        try:
            response = requests.post(
                f"{proxy_base}/audio/retrieve",
                params={"token": request.proxy_token},
                json={"session_id": session_id},
                timeout=30
            )
            response.raise_for_status()
            audio_data = response.json()
            
            import base64
            
            user_chunks = audio_data.get("user_chunks", [])
            # Decode Base64 audio data
            for chunk in user_chunks:
                if isinstance(chunk.get("data"), str):
                    chunk["data"] = base64.b64decode(chunk["data"])
                    
            agent_chunks = audio_data.get("agent_chunks", [])
            # Decode Base64 audio data
            for chunk in agent_chunks:
                if isinstance(chunk.get("data"), str):
                    chunk["data"] = base64.b64decode(chunk["data"])
                    
            session_start_time = audio_data.get("session_start_time")
            
            # Get transcript from proxy if available
            proxy_transcript = audio_data.get("transcript", [])
            transcript_text = request.transcript_text
            
            if proxy_transcript:
                logger.info(f"Using transcript from proxy ({len(proxy_transcript)} entries)")
                transcript_text = "\n".join([
                    f"{entry.get('speaker', 'unknown')}: {entry.get('text', '')}"
                    for entry in proxy_transcript
                ])
            
            if not session_start_time:
                raise ValueError("Session start time not found in proxy response")
                
            logger.info(f"Audio Debug: Retrieved {len(user_chunks)} user chunks and {len(agent_chunks)} agent chunks")
            if user_chunks:
                logger.info(f"Audio Debug: First user chunk size: {len(user_chunks[0].get('data', b''))} bytes")
                
        except Exception as e:
            logger.error(f"Error retrieving audio from proxy: {e}")
            raise HTTPException(status_code=500, detail=f"Failed to retrieve audio from proxy: {str(e)}")
        
        # Combine audio chunks
        audio_file_path = None
        if user_chunks or agent_chunks:
            try:
                logger.info("Audio Debug: Combining audio chunks...")
                wav_data = combine_and_export_audio(
                    user_chunks=user_chunks,
                    agent_chunks=agent_chunks,
                    session_start_time=session_start_time,
                    session_id=session_id
                )
                
                logger.info(f"Audio Debug: Generated WAV data size: {len(wav_data)} bytes")
                
                # Save WAV file
                recordings_dir = Path("src/backend/database/voice_recordings")
                recordings_dir.mkdir(parents=True, exist_ok=True)
                audio_file_path = str(recordings_dir / f"{session_id}.wav")
                
                with open(audio_file_path, "wb") as f:
                    f.write(wav_data)
                
                logger.info(f"Saved audio file: {audio_file_path}")
                
                # Verify file exists and size
                if os.path.exists(audio_file_path):
                    size = os.path.getsize(audio_file_path)
                    logger.info(f"Audio Debug: File verified on disk. Size: {size} bytes")
                else:
                    logger.error("Audio Debug: File NOT found on disk after writing!")
                    
            except Exception as e:
                logger.error(f"Error processing audio: {e}", exc_info=True)
                # Continue even if audio fails - we still want to save the transcript
        else:
            logger.warning("Audio Debug: No audio chunks found to process!")
        
        # Save to database
        try:
            save_voice_screening_session(
                candidate_id=request.candidate_id,
                session_id=session_id,
                transcript_text=transcript_text,
                audio_url=audio_file_path
            )
        except ValueError as e:
            # Candidate not found
            logger.warning(f"Failed to save session: {e}")
            raise HTTPException(status_code=404, detail=str(e))
            
        logger.info(f"Saved session {session_id} for candidate {request.candidate_id}")
        
        return SaveSessionResponse(
            audio_file_path=audio_file_path,
            message="Session saved successfully"
        )
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error saving session: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Failed to save session: {str(e)}")


@router.get("/health")
async def health_check():
    """Health check endpoint."""
    return {"status": "healthy", "service": "voice-screener"}