2026-03-18 17:18:23 +08:00
|
|
|
"""OpenAI TTS adapter — implements TTSProvider port."""
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
import asyncio
|
2026-03-18 17:18:23 +08:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
|
feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31)
* add staging ios app build script
* feat(api): add OpenTelemetry LGTM stack for local observability
Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.
Co-authored-by: Cursor <cursoragent@cursor.com>
* feat: expand observability, harden dev tooling, and fix expo staging UX
Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.
Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.
Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.
Co-authored-by: Cursor <cursoragent@cursor.com>
* chore: enable Grafana Assistant Cursor plugin
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp
Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:12:21 +08:00
|
|
|
from app.core.business_telemetry import business_span
|
2026-04-08 15:37:09 +08:00
|
|
|
from app.core.logging import get_logger
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
class OpenAITTSProvider:
|
|
|
|
|
def __init__(self, api_key: str, model: str = "tts-1"):
|
|
|
|
|
self._client = OpenAI(api_key=api_key) if api_key else None
|
|
|
|
|
self._model = model
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
def _synthesize_sync(self, text: str, voice: str) -> bytes:
|
|
|
|
|
if not self._client:
|
|
|
|
|
return b""
|
|
|
|
|
response = self._client.audio.speech.create(
|
|
|
|
|
model=self._model,
|
|
|
|
|
voice=voice,
|
|
|
|
|
input=text,
|
|
|
|
|
)
|
|
|
|
|
buf = BytesIO()
|
|
|
|
|
for chunk in response.iter_bytes():
|
|
|
|
|
buf.write(chunk)
|
|
|
|
|
return buf.getvalue()
|
|
|
|
|
|
feat(i18n): persist language preference and thread through chat, memoir, TTS
- Add users.language_preference (Alembic 0018, default zh); capture at signup/SMS
only; expose on auth and profile APIs
- Lite English prompts for chat and memoir; localized stage labels and agent
names (Life Echo / 岁月知己)
- Tencent TTS: language-aware synthesis, ModelType=1 for 501004, English chunking
- WebSocket pipeline: emit all AGENT_RESPONSE segments when TTS cancels; INFO logs
for tts_this_turn and TTS decisions; on-demand TTS logging
- Expo: device language on auth, i18n tiers/agent name, [SPLIT] streaming UX fixes
- Tests for migration, prompts, pipeline, router tts_this_turn, reply segments
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-11 16:16:49 +08:00
|
|
|
async def synthesize(
|
|
|
|
|
self,
|
|
|
|
|
text: str,
|
|
|
|
|
voice: str = "alloy",
|
|
|
|
|
*,
|
|
|
|
|
language: str = "zh", # noqa: ARG002 — OpenAI TTS auto-detects language
|
|
|
|
|
) -> bytes:
|
feat: OpenTelemetry LGTM observability, dev tooling, and memoir UX fixes (#31)
* add staging ios app build script
* feat(api): add OpenTelemetry LGTM stack for local observability
Wire OTel traces, metrics, and logs through a collector to Tempo,
Prometheus, and Loki, with custom LLM instrumentation, dev compose overlay,
Grafana provisioning, env templates, and development.sh auto-start.
Co-authored-by: Cursor <cursoragent@cursor.com>
* feat: expand observability, harden dev tooling, and fix expo staging UX
Add business and LLM Prometheus metrics with Grafana dashboards, alerting,
and a metrics verification script. Wire telemetry through adapters and core
LLM paths, and document the local LGTM workflow.
Fix development.sh for macOS bash 3.2, open Grafana and eval-web in Chrome,
and repair eval-web auto-open (unbound EVAL_WEB_BROWSER_SCHEDULED). Merge
internal-eval into the main dev script with improved compose handling.
Require EXPO_PUBLIC_* at build time, improve iOS HTTP ATS for staging IPs,
show memoir empty state instead of load errors when no chapters exist, and
add jest env setup plus chapter list response normalization.
Co-authored-by: Cursor <cursoragent@cursor.com>
* chore: enable Grafana Assistant Cursor plugin
Co-authored-by: Cursor <cursoragent@cursor.com>
* fix: memoir empty state and repair withdrawn 0020_chapters_book_id stamp
Show empty memoir UI when the chapter list succeeds with no items; treat auth/404 as non-fatal. Extend alembic revision repair so local dev DBs stamped with the removed 0020_chapters_book_id migration can roll back and upgrade to 0019.
Co-authored-by: Cursor <cursoragent@cursor.com>
---------
Co-authored-by: Kevin <kevin@brighteng.org>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-20 15:12:21 +08:00
|
|
|
with business_span("tts.synthesize", provider="openai"):
|
|
|
|
|
return await self._synthesize_api(text, voice)
|
|
|
|
|
|
|
|
|
|
async def _synthesize_api(self, text: str, voice: str) -> bytes:
|
2026-03-18 17:18:23 +08:00
|
|
|
if not self._client:
|
|
|
|
|
return b""
|
|
|
|
|
try:
|
2026-04-08 15:37:09 +08:00
|
|
|
return await asyncio.to_thread(self._synthesize_sync, text, voice)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning("OpenAI TTS synthesize failed: {}", e)
|
2026-03-18 17:18:23 +08:00
|
|
|
return b""
|