File size: 17,454 Bytes
699677f
 
 
 
 
 
0b2d478
699677f
 
 
 
 
 
0b2d478
699677f
 
0b2d478
699677f
2bb7b65
699677f
0b2d478
699677f
 
 
 
 
af380f4
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699677f
 
 
 
 
b9457bc
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2d478
 
 
 
 
b9457bc
 
0b2d478
b9457bc
 
0b2d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee0e82d
 
b9457bc
2bb7b65
 
0b2d478
2bb7b65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2d478
2bb7b65
 
 
 
 
 
 
 
 
 
0b2d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb7b65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb7b65
 
 
 
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9457bc
0b2d478
b9457bc
 
 
0b2d478
b9457bc
 
 
 
0b2d478
b9457bc
 
699677f
 
 
2bb7b65
699677f
 
 
 
 
 
 
 
 
 
 
 
 
 
b9457bc
0b2d478
ee0e82d
 
b9457bc
 
 
0b2d478
b9457bc
 
 
 
0b2d478
b9457bc
 
2bb7b65
699677f
 
3e66461
 
 
 
 
 
0b2d478
3e66461
 
 
 
 
0b2d478
3e66461
 
 
 
0b2d478
3e66461
 
2bb7b65
 
 
 
 
 
 
 
 
 
af380f4
2bb7b65
 
 
 
3e66461
 
699677f
 
3e66461
699677f
 
 
 
ee0e82d
 
 
 
 
 
 
 
699677f
 
 
 
 
0b2d478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699677f
ee0e82d
 
 
 
a9430e5
ee0e82d
 
 
 
699677f
 
a9430e5
 
 
 
 
 
 
 
699677f
 
 
 
 
 
 
 
 
 
 
 
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""API routes for the voice agent."""
from __future__ import annotations

import json
import uuid
from pathlib import Path
import shutil

import anyio
from fastapi import APIRouter, File, Form, UploadFile, WebSocket, WebSocketDisconnect
from fastapi.responses import FileResponse, JSONResponse
from starlette.websockets import WebSocketState

from ..core.errors import SpeechError, ValidationError, LLMError
from ..core.logging import get_logger
from ..services.pipeline import VoicePipeline
from ..services.agent.agent_pipeline import AgentPipeline
from ..services.stt import SpeechToTextService
from ..services.vad import SileroVADStream
from ..utils.audio import encode_base64
from ..core.config import get_settings

router = APIRouter()

MAX_FILE_SIZE_BYTES = 15 * 1024 * 1024
DEFAULT_STREAM_CONTENT_TYPE = "audio/ogg"
NO_MATCH_REPLY = "Sorry, I didn't catch that. Please try again."


@router.get("/health")
async def health() -> dict[str, str]:
    """Health check endpoint."""
    return {"status": "ok"}


@router.get("/ws-demo")
async def ws_demo() -> FileResponse:
    """Serve a simple WebSocket streaming demo page."""
    demo_path = Path(__file__).resolve().parent.parent / "utils" / "ws_demo.html"
    return FileResponse(demo_path)


@router.post("/v1/agent/upload")
async def agent_upload(files: list[UploadFile] = File(...)) -> JSONResponse:
    """Upload files for local RAG indexing."""
    settings = get_settings()
    data_dir = Path(settings.data_dir)
    data_dir.mkdir(parents=True, exist_ok=True)
    saved: list[str] = []
    for file in files:
        content = await file.read()
        if not content:
            continue
        target = data_dir / file.filename
        target.write_bytes(content)
        saved.append(file.filename)
    await AgentPipeline().rebuild_rag()
    return JSONResponse({"status": "ok", "files": saved})


@router.post("/v1/agent/reset")
async def agent_reset() -> JSONResponse:
    """Reset local RAG + memory storage."""
    settings = get_settings()
    data_dir = Path(settings.data_dir)
    store_dir = Path(settings.vector_store_dir)
    if data_dir.exists():
        shutil.rmtree(data_dir, ignore_errors=True)
    if store_dir.exists():
        shutil.rmtree(store_dir, ignore_errors=True)
    data_dir.mkdir(parents=True, exist_ok=True)
    store_dir.mkdir(parents=True, exist_ok=True)
    AgentPipeline().reset()
    return JSONResponse({"status": "ok"})


@router.post("/v1/voice/file")
async def voice_file(
    file: UploadFile = File(...),
    prompt: str | None = Form(default=None),
    return_audio: bool = Form(default=True),
    llm_provider: str | None = Form(default=None),
) -> JSONResponse:
    """Process uploaded audio file and return transcript + reply."""
    request_id = str(uuid.uuid4())
    log = get_logger(request_id=request_id, endpoint="/v1/voice/file")

    if not file:
        raise ValidationError(code="file_missing", message="Audio file is required.")

    audio_bytes = await file.read()
    if not audio_bytes:
        raise ValidationError(code="file_empty", message="Audio file is empty.")
    if len(audio_bytes) > MAX_FILE_SIZE_BYTES:
        raise ValidationError(code="file_too_large", message="File exceeds 15MB limit.")

    pipeline = VoicePipeline()
    if llm_provider and llm_provider not in {
        "foundry_agent",
        "azure_openai",
        "local_agent",
    }:
        raise ValidationError(
            code="llm_provider",
            message="LLM provider must be 'foundry_agent', 'azure_openai', or 'local_agent'.",
        )

    if llm_provider == "local_agent":
        agent = AgentPipeline()
        result = await agent.run_audio(
            audio_bytes=audio_bytes,
            filename=file.filename,
            content_type=file.content_type,
            prompt=prompt,
            return_audio=return_audio,
            llm_provider="azure_openai",
        )
    else:
        result = await pipeline.run(
            audio_bytes=audio_bytes,
            filename=file.filename,
            content_type=file.content_type,
            prompt=prompt,
            return_audio=return_audio,
            llm_provider=llm_provider,
        )

    reply_audio_base64 = (
        encode_base64(result.reply_audio) if result.reply_audio else None
    )

    response_body = {
        "transcript": result.transcript,
        "reply_text": result.reply_text,
        "audio_format": "wav",
        "reply_audio_base64": reply_audio_base64,
        "timings_ms": result.timings_ms,
    }

    log.info(
        "voice_request_complete",
        file_name=file.filename,
        file_size=len(audio_bytes),
        timings_ms=result.timings_ms,
        return_audio=return_audio,
    )

    return JSONResponse(response_body)


@router.websocket("/ws/voice")
async def voice_stream(websocket: WebSocket) -> None:
    """Stream audio over WebSocket, then process on 'stop'."""
    await websocket.accept()
    request_id = str(uuid.uuid4())
    log = get_logger(request_id=request_id, endpoint="/ws/voice")
    buffer = bytearray()
    content_type: str | None = DEFAULT_STREAM_CONTENT_TYPE
    prompt: str | None = None
    return_audio = True
    stt_session = None
    frames_sent: int | None = None
    avg_rms: float | None = None
    llm_provider: str | None = None
    vad_stream: SileroVADStream | None = None
    segment_processing = False
    session_id: str | None = None

    async def _finalize_segment() -> None:
        nonlocal stt_session, segment_processing, vad_stream
        if stt_session is None:
            raise ValidationError(
                code="stt_not_started", message="STT session not started."
            )
        if not buffer:
            return
        segment_processing = True
        try:
            stt_result = await anyio.to_thread.run_sync(stt_session.finish)
        except SpeechError as exc:
            if exc.code in {"stt_empty", "stt_no_match"}:
                try:
                    stt_result = await anyio.to_thread.run_sync(
                        SpeechToTextService().transcribe,
                        bytes(buffer),
                        None,
                        content_type,
                    )
                except SpeechError as exc_fallback:
                    if exc_fallback.code in {"stt_empty", "stt_no_match"}:
                        await websocket.send_json(
                            {
                                "event": "result",
                                "transcript": "",
                                "reply_text": NO_MATCH_REPLY,
                                "audio_format": "wav",
                                "reply_audio_base64": None,
                                "timings_ms": {"stt": 0, "llm": 0, "tts": 0, "total": 0},
                            }
                        )
                        buffer.clear()
                        stt_session = SpeechToTextService().start_streaming(
                            end_silence_ms=1400, initial_silence_ms=5000
                        )
                        vad_stream = SileroVADStream()
                        return
                    raise
            else:
                raise

        await websocket.send_json(
            {"event": "transcript", "transcript": stt_result.transcript}
        )
        if llm_provider == "local_agent":
            agent = AgentPipeline()
            result = await agent.run_with_transcript(
                transcript=stt_result.transcript,
                language=stt_result.language,
                prompt=prompt,
                return_audio=return_audio,
                llm_provider="azure_openai",
                session_id=session_id,
            )
        else:
            pipeline = VoicePipeline()
            result = await pipeline.run(
                audio_bytes=bytes(buffer),
                filename=None,
                content_type=content_type,
                prompt=prompt,
                return_audio=return_audio,
                transcript_override=stt_result.transcript,
                language_override=stt_result.language,
                llm_provider=llm_provider,
            )
        response_body = {
            "event": "result",
            "transcript": result.transcript,
            "reply_text": result.reply_text,
            "audio_format": "wav",
            "reply_audio_base64": None,
            "timings_ms": result.timings_ms,
        }
        log.info(
            "voice_stream_complete",
            bytes_received=len(buffer),
            timings_ms=result.timings_ms,
            return_audio=return_audio,
            content_type=content_type,
            frames_sent=frames_sent,
            avg_rms=avg_rms,
        )
        await websocket.send_json(response_body)
        if result.reply_audio and return_audio:
            await websocket.send_bytes(result.reply_audio)
        buffer.clear()
        stt_session = SpeechToTextService().start_streaming(
            end_silence_ms=1200, initial_silence_ms=5000
        )
        vad_stream = SileroVADStream()
        segment_processing = False

    try:
        while True:
            try:
                message = await websocket.receive()
            except RuntimeError:
                log.info("voice_stream_disconnect")
                break

            if "bytes" in message and message["bytes"] is not None:
                chunk = message["bytes"]
                if stt_session is not None:
                    stt_session.write(chunk)
                buffer.extend(chunk)
                if vad_stream is not None and not segment_processing:
                    decision = vad_stream.update(chunk)
                    if decision.speech_ended:
                        await _finalize_segment()
                if len(buffer) > MAX_FILE_SIZE_BYTES:
                    raise ValidationError(
                        code="file_too_large", message="Stream exceeds 15MB limit."
                    )
                continue

            if "text" in message and message["text"] is not None:
                try:
                    payload = json.loads(message["text"])
                except json.JSONDecodeError as exc:
                    raise ValidationError(
                        code="invalid_message", message="Invalid JSON message."
                    ) from exc

                event = str(payload.get("event", "")).lower()
                if event == "start":
                    content_type = payload.get("content_type") or content_type
                    prompt = payload.get("prompt")
                    return_audio = payload.get("return_audio", True)
                    llm_provider = payload.get("llm_provider", llm_provider)
                    session_id = payload.get("session_id", session_id) or request_id
                    if llm_provider and llm_provider not in {
                        "foundry_agent",
                        "azure_openai",
                        "local_agent",
                    }:
                        raise ValidationError(
                            code="llm_provider",
                            message=(
                                "LLM provider must be 'foundry_agent', 'azure_openai', or 'local_agent'."
                            ),
                        )
                    stt_session = SpeechToTextService().start_streaming(
                        end_silence_ms=1200, initial_silence_ms=5000
                    )
                    vad_stream = SileroVADStream()
                    continue

                if event == "stop":
                    if not buffer:
                        raise ValidationError(
                            code="file_empty", message="Audio stream is empty."
                        )
                    if stt_session is None:
                        raise ValidationError(
                            code="stt_not_started",
                            message="STT session not started.",
                        )
                    prompt = payload.get("prompt", prompt)
                    return_audio = payload.get("return_audio", return_audio)
                    llm_provider = payload.get("llm_provider", llm_provider)
                    session_id = payload.get("session_id", session_id) or request_id
                    frames_sent = payload.get("frames_sent", frames_sent)
                    avg_rms = payload.get("avg_rms", avg_rms)
                    if llm_provider and llm_provider not in {
                        "foundry_agent",
                        "azure_openai",
                        "local_agent",
                    }:
                        raise ValidationError(
                            code="llm_provider",
                            message=(
                                "LLM provider must be 'foundry_agent', 'azure_openai', or 'local_agent'."
                            ),
                        )
                    await _finalize_segment()
                    break

                if event == "segment_end":
                    if not buffer:
                        continue
                    prompt = payload.get("prompt", prompt)
                    return_audio = payload.get("return_audio", return_audio)
                    llm_provider = payload.get("llm_provider", llm_provider)
                    session_id = payload.get("session_id", session_id) or request_id
                    frames_sent = payload.get("frames_sent", frames_sent)
                    avg_rms = payload.get("avg_rms", avg_rms)
                    if llm_provider and llm_provider not in {
                        "foundry_agent",
                        "azure_openai",
                        "local_agent",
                    }:
                        raise ValidationError(
                            code="llm_provider",
                            message=(
                                "LLM provider must be 'foundry_agent', 'azure_openai', or 'local_agent'."
                            ),
                        )
                    if vad_stream is not None and not vad_stream.has_speech():
                        await websocket.send_json(
                            {
                                "event": "result",
                                "transcript": "",
                                "reply_text": NO_MATCH_REPLY,
                                "audio_format": "wav",
                                "reply_audio_base64": None,
                                "timings_ms": {"stt": 0, "llm": 0, "tts": 0, "total": 0},
                            }
                        )
                        buffer.clear()
                        vad_stream.reset()
                        continue
                    await _finalize_segment()
                    continue

                raise ValidationError(
                    code="invalid_event",
                    message="Event must be 'start', 'stop', or 'segment_end'.",
                )
    except WebSocketDisconnect:
        log.info("voice_stream_disconnect")
    except ValidationError as exc:
        log.warning(
            "voice_stream_error",
            code=exc.code,
            message=exc.message,
            bytes_received=len(buffer),
            frames_sent=frames_sent,
            avg_rms=avg_rms,
        )
        if websocket.application_state == WebSocketState.CONNECTED:
            await websocket.send_json(
                {"event": "error", "error": {"code": exc.code, "message": exc.message}}
            )
            await websocket.close()
    except LLMError as exc:
        log.warning(
            "voice_stream_error",
            code=exc.code,
            message=exc.message,
            details=exc.details,
            bytes_received=len(buffer),
            frames_sent=frames_sent,
            avg_rms=avg_rms,
        )
        if websocket.application_state == WebSocketState.CONNECTED:
            await websocket.send_json(
                {
                    "event": "error",
                    "error": {"code": exc.code, "message": exc.message},
                }
            )
            if exc.code != "llm_guardrail":
                await websocket.close()
        return
    except SpeechError as exc:
        log.warning(
            "voice_stream_error",
            code=exc.code,
            message=exc.message,
            details=exc.details,
            bytes_received=len(buffer),
            frames_sent=frames_sent,
            avg_rms=avg_rms,
        )
        if websocket.application_state == WebSocketState.CONNECTED:
            await websocket.send_json(
                {
                    "event": "error",
                    "error": {
                        "code": exc.code,
                        "message": exc.message,
                        "details": exc.details,
                    },
                }
            )
            await websocket.close()
    except Exception as exc:  # pragma: no cover - safety net
        log.error("voice_stream_unhandled", error=repr(exc))
        if websocket.application_state == WebSocketState.CONNECTED:
            await websocket.send_json(
                {
                    "event": "error",
                    "error": {"code": "internal_error", "message": "Server error."},
                }
            )
            await websocket.close()