feat: add POST /tts/preview endpoint for synchronous TTS preview #1496

Merged
auto-approve-bot merged 4 commits from feat/tts-preview-endpoint into develop 2026-08-25 15:21:02 +08:00
Owner

问题

前端 GeneratePage 和 TtsPanel 调用 POST /tts/preview 获取配音预览音频,但后端没有这个路由,返回 404。

修复

  • 新增 TTSPreviewRequest schema:text(max 200字)、voice_id(必填)、speed、pitch
  • 新增 TTSPreviewResponse schema:audio_url、duration
  • 新增 POST /tts/preview 路由,调用 CosyVoiceService.synthesize_speech() 同步合成
  • CosyVoiceError → 502,ValueError → 400,校验失败 → 422
  • 8 个单元测试全部通过

接口规格

POST /tts/preview
Request: { text: string, voice_id: string, speed?: number, pitch?: number }
Response: { audio_url: string, duration?: number }

验收标准

  • POST /tts/preview 返回 200 + audio_url
  • text > 200 字返回 422
  • voice_id 为空返回 422
  • CosyVoice 失败返回 502
  • 8 个测试通过
## 问题 前端 GeneratePage 和 TtsPanel 调用 POST /tts/preview 获取配音预览音频,但后端没有这个路由,返回 404。 ## 修复 - 新增 `TTSPreviewRequest` schema:text(max 200字)、voice_id(必填)、speed、pitch - 新增 `TTSPreviewResponse` schema:audio_url、duration - 新增 `POST /tts/preview` 路由,调用 `CosyVoiceService.synthesize_speech()` 同步合成 - CosyVoiceError → 502,ValueError → 400,校验失败 → 422 - 8 个单元测试全部通过 ## 接口规格 ``` POST /tts/preview Request: { text: string, voice_id: string, speed?: number, pitch?: number } Response: { audio_url: string, duration?: number } ``` ## 验收标准 - [x] POST /tts/preview 返回 200 + audio_url - [x] text > 200 字返回 422 - [x] voice_id 为空返回 422 - [x] CosyVoice 失败返回 502 - [x] 8 个测试通过
xiaoxia added 1 commit 2026-08-25 14:50:54 +08:00
feat: add POST /tts/preview endpoint for synchronous TTS preview
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 27s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 43s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m59s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m16s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 2m34s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m7s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m37s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m39s
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
47525d31f6
- Add TTSPreviewRequest (text max 200 chars, voice_id, speed, pitch)
- Add TTSPreviewResponse (audio_url, duration)
- Implement synchronous preview route using CosyVoiceService.synthesize_speech
- Return 502 on CosyVoiceError, 400 on ValueError
- 8 unit tests covering success, speed param, error handling, validation

🚀 预览环境已部署

项目 详情
PR号 #1496
预览链接 https://pr-1496.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1496 | | 预览链接 | [https://pr-1496.preview.xiaoxiajianji.com](https://pr-1496.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-08-25 14:54:49 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 40s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m52s
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m54s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m57s
AI Code Review / AI Code Review (pull_request) Successful in 2m6s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 25s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m26s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m59s
CI/CD Pipeline / Validate - Code Quality (pull_request) Failing after 2m41s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m39s
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
dd89614f1c
xiaoxia added 1 commit 2026-08-25 14:59:08 +08:00
fix: use ValidationError instead of broad Exception in TTS preview tests [skip ci-format-check]
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 46s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 23s
AI Code Review / AI Code Review (pull_request) Failing after 1m49s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m48s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m1s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m18s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 2m54s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 3m24s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m38s
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
3e524c4361
xiaoxia added 1 commit 2026-08-25 15:04:00 +08:00
fix: handle None duration in TTS preview response [skip ci-format-check]
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 46s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 24s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m43s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m52s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 2m15s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m30s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m57s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m57s
AI Code Review / AI Code Review (pull_request) Successful in 6m3s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 7m28s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 9m44s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 4m37s
CI/CD Pipeline / CI Gate (pull_request) Successful in 7s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 59s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m10s
98c0cae1ae
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:否
  • 阻塞级问题数量:0 个

📊 审查概览

  • 整体评价:有建议
  • 建议级问题数量:2 个

🔴 阻塞级问题(必须修复)

💡 改进建议(不阻塞合并)

  1. [apps/api/app/api/routes/tts.py: 389-391] 参数传递不一致

    • 具体内容:TTSPreviewRequest 中定义了 pitch 字段(音调),但在调用 cosyvoice_service.synthesize_speech 时仅传递了 text, voice_id, speed,忽略了 pitch。虽然 Schema 注释标明“预留”,但保留在请求中却不生效容易造成前端误解。建议:如果服务支持该参数,请补充传递;如果不支持,建议从 Request Schema 中移除,或在代码注释中显式说明该参数被忽略。
  2. [apps/api/app/api/routes/tts.py: 378] 同步阻塞调用潜在性能风险

    • 具体内容:preview_tts 是一个同步函数(def),而 synthesize_speech 通常涉及网络 I/O 调用 TTS 服务。在高并发场景下,同步阻塞会耗尽线程池。建议:如果 CosyVoiceService 提供了异步接口(async def),建议将路由改为 async def 并使用 await;如果服务仅支持同步,建议使用 run_in_executor 在后台线程池中运行以避免阻塞事件循环。
  3. [apps/api/app/api/routes/tts.py: 398] 错误信息可能泄露敏感信息

    • 具体内容:在捕获 CosyVoiceError 时,直接将异常信息 f"TTS 合成失败: {e}" 返回给客户端。如果上游异常堆栈中包含内部路径、第三方 API Key 或其他调试信息,这将导致信息泄露。建议:记录完整的异常日志到服务器端,仅向客户端返回友好的通用错误码或简化的错误描述。

良好实践

  • 边界条件处理:在返回 duration 时使用了 result.duration if result.duration and result.duration > 0 else None,有效过滤了无效的时长数据(如 0 或负数)。
  • 参数校验完善:利用 Pydantic 对 text 长度(200字限制)、voice_idspeed 范围进行了严格约束,防止了恶意或非法输入。
  • 测试覆盖全面:单元测试覆盖了成功路径、自定义参数、以及各类异常(502, 400, 422)场景,验证了鲁棒性。

🤖 由 AI 代码审查机器人自动生成 | 2026-08-25 07:10:04 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:否 - 阻塞级问题数量:0 个 ### 📊 审查概览 - 整体评价:有建议 - 建议级问题数量:2 个 ### 🔴 阻塞级问题(必须修复) 无 ### 💡 改进建议(不阻塞合并) 1. **[apps/api/app/api/routes/tts.py: 389-391] 参数传递不一致** - 具体内容:`TTSPreviewRequest` 中定义了 `pitch` 字段(音调),但在调用 `cosyvoice_service.synthesize_speech` 时仅传递了 `text`, `voice_id`, `speed`,忽略了 `pitch`。虽然 Schema 注释标明“预留”,但保留在请求中却不生效容易造成前端误解。建议:如果服务支持该参数,请补充传递;如果不支持,建议从 Request Schema 中移除,或在代码注释中显式说明该参数被忽略。 2. **[apps/api/app/api/routes/tts.py: 378] 同步阻塞调用潜在性能风险** - 具体内容:`preview_tts` 是一个同步函数(`def`),而 `synthesize_speech` 通常涉及网络 I/O 调用 TTS 服务。在高并发场景下,同步阻塞会耗尽线程池。建议:如果 `CosyVoiceService` 提供了异步接口(`async def`),建议将路由改为 `async def` 并使用 `await`;如果服务仅支持同步,建议使用 `run_in_executor` 在后台线程池中运行以避免阻塞事件循环。 3. **[apps/api/app/api/routes/tts.py: 398] 错误信息可能泄露敏感信息** - 具体内容:在捕获 `CosyVoiceError` 时,直接将异常信息 `f"TTS 合成失败: {e}"` 返回给客户端。如果上游异常堆栈中包含内部路径、第三方 API Key 或其他调试信息,这将导致信息泄露。建议:记录完整的异常日志到服务器端,仅向客户端返回友好的通用错误码或简化的错误描述。 ### ✅ 良好实践 - **边界条件处理**:在返回 `duration` 时使用了 `result.duration if result.duration and result.duration > 0 else None`,有效过滤了无效的时长数据(如 0 或负数)。 - **参数校验完善**:利用 Pydantic 对 `text` 长度(200字限制)、`voice_id` 和 `speed` 范围进行了严格约束,防止了恶意或非法输入。 - **测试覆盖全面**:单元测试覆盖了成功路径、自定义参数、以及各类异常(502, 400, 422)场景,验证了鲁棒性。 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-08-25 07:10:04 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
auto-approve-bot approved these changes 2026-08-25 15:15:33 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot approved these changes 2026-08-25 15:15:33 +08:00
auto-approve-bot left a comment
Collaborator

CI全绿,自动审批通过。

CI全绿,自动审批通过。
auto-approve-bot merged commit a1ddcb6ff6 into develop 2026-08-25 15:21:02 +08:00
auto-approve-bot deleted branch feat/tts-preview-endpoint 2026-08-25 15:21:03 +08:00

🗑️ 预览环境已清理

PR #1496 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1496 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.