feat: 第五轮开发 - 14项未来路线图功能完整实现
W1-W14 全部完成: - W1: 消息搜索 (ILIKE全文检索 + SearchModal) - W2: 对话导出 (JSON/Markdown/TXT三格式) - W3: 记忆时间线 DevTools 可视化 - W4: 通知推送系统 (WebSocket + Browser Notification API) - W5: 定时提醒 (30s轮询 + 重复提醒 + WebSocket推送) - W6: 每日简报 (08:00自动生成: 天气+新闻+提醒+AI摘要) - W7: IoT场景自动化 (规则引擎 10s轮询 + 条件评估 + 场景执行) - W8: 语音输入 (浏览器 Speech Recognition API) - W9: STT服务 (voice-service + whisper.cpp) - W10: TTS服务 (浏览器 Speech Synthesis + edge-tts三档回退) - W11: 文件管理 (上传/下载/缩略图/纯Go bilinear缩放) - W12: 知识库RAG (PostgreSQL tsvector + 文档分块 + 检索) - W13: 多模态 (图片上传+分析: Vision API + 本地Go分析回退) - W14: PWA (Service Worker + 离线页 + install prompt) 总计: 6个Go微服务 + 10+前端组件 + 10+ PostgreSQL表 + 4个后台调度器
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/yourname/cyrene-ai/voice-service/internal/service"
|
||||
)
|
||||
|
||||
// TTSHandler TTS HTTP API 处理器
|
||||
type TTSHandler struct {
|
||||
svc *service.TTSService
|
||||
}
|
||||
|
||||
// NewTTSHandler 创建 TTS 处理器
|
||||
func NewTTSHandler(svc *service.TTSService) *TTSHandler {
|
||||
return &TTSHandler{svc: svc}
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册 TTS 路由
|
||||
func (h *TTSHandler) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/api/v1/tts/synthesize", h.handleSynthesize)
|
||||
mux.HandleFunc("/api/v1/tts/voices", h.handleVoices)
|
||||
mux.HandleFunc("/api/v1/tts/status", h.handleStatus)
|
||||
}
|
||||
|
||||
// TTSSynthesizeRequest TTS 合成请求体
|
||||
type TTSSynthesizeRequest struct {
|
||||
Text string `json:"text"`
|
||||
Voice string `json:"voice"`
|
||||
Rate string `json:"rate"`
|
||||
}
|
||||
|
||||
// handleSynthesize POST /api/v1/tts/synthesize
|
||||
func (h *TTSHandler) handleSynthesize(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
// 解析 JSON 请求体
|
||||
var req TTSSynthesizeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求体解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if req.Text == "" {
|
||||
writeError(w, http.StatusBadRequest, "text 字段不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查 TTS 引擎是否可用
|
||||
if !h.svc.IsAvailable() {
|
||||
log.Printf("[tts-handler] TTS 引擎不可用")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"error": "TTS 引擎不可用,请安装 edge-tts (pip install edge-tts) 或 espeak-ng",
|
||||
"code": "TTS_UNAVAILABLE",
|
||||
"install": "pip install edge-tts",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 调用合成
|
||||
audioData, format, err := h.svc.Synthesize(req.Text, req.Voice, req.Rate)
|
||||
if err != nil {
|
||||
log.Printf("[tts-handler] TTS 合成失败: %v", err)
|
||||
writeError(w, http.StatusInternalServerError, "TTS 合成失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 返回音频流
|
||||
contentType := "audio/mpeg"
|
||||
if format == "wav" {
|
||||
contentType = "audio/wav"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Disposition", "inline; filename=synthesized."+format)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(audioData)
|
||||
}
|
||||
|
||||
// handleVoices GET /api/v1/tts/voices
|
||||
func (h *TTSHandler) handleVoices(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
voices := h.svc.GetVoices()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"voices": voices,
|
||||
"count": len(voices),
|
||||
})
|
||||
}
|
||||
|
||||
// handleStatus GET /api/v1/tts/status
|
||||
func (h *TTSHandler) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
status := h.svc.GetEngineStatus()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"service": "voice-service",
|
||||
"tts": status,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user