File size: 14,112 Bytes
e8a19b3
 
 
 
 
 
 
 
 
 
 
 
 
871a798
 
 
e8a19b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
STT Lifecycle Manager for Flare
===============================
Manages STT instances lifecycle per session
"""
import asyncio
from typing import Dict, Optional, Any
from datetime import datetime
import traceback
import base64

from event_bus import EventBus, Event, EventType, publish_error
from resource_manager import ResourceManager, ResourceType
from stt.stt_factory import STTFactory
from stt.stt_interface import STTInterface, STTConfig, TranscriptionResult
from utils.logger import log_info, log_error, log_debug, log_warning


class STTSession:
    """STT session wrapper"""
    
    def __init__(self, session_id: str, stt_instance: STTInterface):
        self.session_id = session_id
        self.stt_instance = stt_instance
        self.is_streaming = False
        self.config: Optional[STTConfig] = None
        self.created_at = datetime.utcnow()
        self.last_activity = datetime.utcnow()
        self.total_chunks = 0
        self.total_bytes = 0
        
    def update_activity(self):
        """Update last activity timestamp"""
        self.last_activity = datetime.utcnow()


class STTLifecycleManager:
    """Manages STT instances lifecycle"""
    
    def __init__(self, event_bus: EventBus, resource_manager: ResourceManager):
        self.event_bus = event_bus
        self.resource_manager = resource_manager
        self.stt_sessions: Dict[str, STTSession] = {}
        self._setup_event_handlers()
        self._setup_resource_pool()
        
    def _setup_event_handlers(self):
        """Subscribe to STT-related events"""
        self.event_bus.subscribe(EventType.STT_STARTED, self._handle_stt_start)
        self.event_bus.subscribe(EventType.STT_STOPPED, self._handle_stt_stop)
        self.event_bus.subscribe(EventType.AUDIO_CHUNK_RECEIVED, self._handle_audio_chunk)
        self.event_bus.subscribe(EventType.SESSION_ENDED, self._handle_session_ended)
        
    def _setup_resource_pool(self):
        """Setup STT instance pool"""
        self.resource_manager.register_pool(
            resource_type=ResourceType.STT_INSTANCE,
            factory=self._create_stt_instance,
            max_idle=5,
            max_age_seconds=300  # 5 minutes
        )
        
    async def _create_stt_instance(self) -> STTInterface:
        """Factory for creating STT instances"""
        try:
            stt_instance = STTFactory.create_provider()
            if not stt_instance:
                raise ValueError("Failed to create STT instance")
                
            log_debug("🎀 Created new STT instance")
            return stt_instance
            
        except Exception as e:
            log_error(f"❌ Failed to create STT instance", error=str(e))
            raise
            
    async def _handle_stt_start(self, event: Event):
        """Handle STT start request"""
        session_id = event.session_id
        config_data = event.data
        
        try:
            log_info(f"🎀 Starting STT", session_id=session_id)
            
            # Check if already exists
            if session_id in self.stt_sessions:
                stt_session = self.stt_sessions[session_id]
                if stt_session.is_streaming:
                    log_warning(f"⚠️ STT already streaming", session_id=session_id)
                    return
            else:
                # Acquire STT instance from pool
                resource_id = f"stt_{session_id}"
                stt_instance = await self.resource_manager.acquire(
                    resource_id=resource_id,
                    session_id=session_id,
                    resource_type=ResourceType.STT_INSTANCE,
                    cleanup_callback=self._cleanup_stt_instance
                )
                
                # Create session wrapper
                stt_session = STTSession(session_id, stt_instance)
                self.stt_sessions[session_id] = stt_session
                
            # Get session locale from state orchestrator
            locale = config_data.get("locale", "tr")
            
            # Build STT config
            stt_config = STTConfig(
                language=self._get_language_code(locale),
                sample_rate=config_data.get("sample_rate", 16000),
                encoding=config_data.get("encoding", "WEBM_OPUS"),
                enable_punctuation=config_data.get("enable_punctuation", True),
                enable_word_timestamps=False,
                model=config_data.get("model", "latest_long"),
                use_enhanced=config_data.get("use_enhanced", True),
                single_utterance=False,  # Continuous listening
                interim_results=config_data.get("interim_results", True),
                vad_enabled=config_data.get("vad_enabled", True),
                speech_timeout_ms=config_data.get("speech_timeout_ms", 2000),
                noise_reduction_enabled=config_data.get("noise_reduction_enabled", True),
                noise_reduction_level=config_data.get("noise_reduction_level", 2)
            )
            
            stt_session.config = stt_config
            
            # Start streaming
            await stt_session.stt_instance.start_streaming(stt_config)
            stt_session.is_streaming = True
            stt_session.update_activity()
            
            log_info(f"βœ… STT started", session_id=session_id, language=stt_config.language)
            
            # Notify STT is ready
            await self.event_bus.publish(Event(
                type=EventType.STT_READY,
                session_id=session_id,
                data={"language": stt_config.language}
            ))
            
        except Exception as e:
            log_error(
                f"❌ Failed to start STT",
                session_id=session_id,
                error=str(e),
                traceback=traceback.format_exc()
            )
            
            # Clean up on error
            if session_id in self.stt_sessions:
                await self._cleanup_session(session_id)
                
            # Publish error event
            await publish_error(
                session_id=session_id,
                error_type="stt_error",
                error_message=f"Failed to start STT: {str(e)}"
            )
            
    async def _handle_stt_stop(self, event: Event):
        """Handle STT stop request"""
        session_id = event.session_id
        reason = event.data.get("reason", "unknown")
        
        log_info(f"πŸ›‘ Stopping STT", session_id=session_id, reason=reason)
        
        stt_session = self.stt_sessions.get(session_id)
        if not stt_session:
            log_warning(f"⚠️ No STT session found", session_id=session_id)
            return
            
        try:
            if stt_session.is_streaming:
                # Stop streaming
                final_result = await stt_session.stt_instance.stop_streaming()
                stt_session.is_streaming = False
                
                # If we got a final result, publish it
                if final_result and final_result.text:
                    await self.event_bus.publish(Event(
                        type=EventType.STT_RESULT,
                        session_id=session_id,
                        data={
                            "text": final_result.text,
                            "is_final": True,
                            "confidence": final_result.confidence
                        }
                    ))
                    
            # Don't remove session immediately - might restart
            stt_session.update_activity()
            
            log_info(f"βœ… STT stopped", session_id=session_id)
            
        except Exception as e:
            log_error(
                f"❌ Error stopping STT",
                session_id=session_id,
                error=str(e)
            )
            
    async def _handle_audio_chunk(self, event: Event):
        """Process audio chunk through STT"""
        session_id = event.session_id
        
        stt_session = self.stt_sessions.get(session_id)
        if not stt_session or not stt_session.is_streaming:
            # STT not ready, ignore chunk
            return
            
        try:
            # Decode audio data
            audio_data = base64.b64decode(event.data.get("audio_data", ""))
            
            # Update stats
            stt_session.total_chunks += 1
            stt_session.total_bytes += len(audio_data)
            stt_session.update_activity()
            
            # Stream to STT
            async for result in stt_session.stt_instance.stream_audio(audio_data):
                # Publish transcription results
                await self.event_bus.publish(Event(
                    type=EventType.STT_RESULT,
                    session_id=session_id,
                    data={
                        "text": result.text,
                        "is_final": result.is_final,
                        "confidence": result.confidence,
                        "timestamp": result.timestamp
                    }
                ))
                
                # Log final results
                if result.is_final:
                    log_info(
                        f"πŸ“ STT final result",
                        session_id=session_id,
                        text=result.text[:50] + "..." if len(result.text) > 50 else result.text,
                        confidence=result.confidence
                    )
                    
            # Log progress periodically
            if stt_session.total_chunks % 100 == 0:
                log_debug(
                    f"πŸ“Š STT progress",
                    session_id=session_id,
                    chunks=stt_session.total_chunks,
                    bytes=stt_session.total_bytes
                )
                
        except Exception as e:
            log_error(
                f"❌ Error processing audio chunk",
                session_id=session_id,
                error=str(e)
            )
            
            # Check if it's a recoverable error
            if "stream duration" in str(e) or "timeout" in str(e).lower():
                # STT timeout, restart needed
                await publish_error(
                    session_id=session_id,
                    error_type="stt_timeout",
                    error_message="STT stream timeout, restart needed"
                )
            else:
                # Other STT error
                await publish_error(
                    session_id=session_id,
                    error_type="stt_error",
                    error_message=str(e)
                )
                
    async def _handle_session_ended(self, event: Event):
        """Clean up STT resources when session ends"""
        session_id = event.session_id
        await self._cleanup_session(session_id)
        
    async def _cleanup_session(self, session_id: str):
        """Clean up STT session"""
        stt_session = self.stt_sessions.pop(session_id, None)
        if not stt_session:
            return
            
        try:
            # Stop streaming if active
            if stt_session.is_streaming:
                await stt_session.stt_instance.stop_streaming()
                
            # Release resource
            resource_id = f"stt_{session_id}"
            await self.resource_manager.release(resource_id, delay_seconds=60)
            
            log_info(
                f"🧹 STT session cleaned up",
                session_id=session_id,
                total_chunks=stt_session.total_chunks,
                total_bytes=stt_session.total_bytes
            )
            
        except Exception as e:
            log_error(
                f"❌ Error cleaning up STT session",
                session_id=session_id,
                error=str(e)
            )
            
    async def _cleanup_stt_instance(self, stt_instance: STTInterface):
        """Cleanup callback for STT instance"""
        try:
            # Ensure streaming is stopped
            if hasattr(stt_instance, 'is_streaming') and stt_instance.is_streaming:
                await stt_instance.stop_streaming()
                
            log_debug("🧹 STT instance cleaned up")
            
        except Exception as e:
            log_error(f"❌ Error cleaning up STT instance", error=str(e))
            
    def _get_language_code(self, locale: str) -> str:
        """Convert locale to STT language code"""
        # Map common locales to STT language codes
        locale_map = {
            "tr": "tr-TR",
            "en": "en-US",
            "de": "de-DE",
            "fr": "fr-FR",
            "es": "es-ES",
            "it": "it-IT",
            "pt": "pt-BR",
            "ru": "ru-RU",
            "ja": "ja-JP",
            "ko": "ko-KR",
            "zh": "zh-CN",
            "ar": "ar-SA"
        }
        
        # Check direct match
        if locale in locale_map:
            return locale_map[locale]
            
        # Check if it's already a full code
        if "-" in locale and len(locale) == 5:
            return locale
            
        # Default to locale-LOCALE format
        return f"{locale}-{locale.upper()}"
        
    def get_stats(self) -> Dict[str, Any]:
        """Get STT manager statistics"""
        session_stats = {}
        for session_id, stt_session in self.stt_sessions.items():
            session_stats[session_id] = {
                "is_streaming": stt_session.is_streaming,
                "total_chunks": stt_session.total_chunks,
                "total_bytes": stt_session.total_bytes,
                "uptime_seconds": (datetime.utcnow() - stt_session.created_at).total_seconds(),
                "last_activity": stt_session.last_activity.isoformat()
            }
            
        return {
            "active_sessions": len(self.stt_sessions),
            "streaming_sessions": sum(1 for s in self.stt_sessions.values() if s.is_streaming),
            "sessions": session_stats
        }