Files
Cyrene/backend/voice-service/internal/handler/tts_handler.go
T
AskaEth 87214b9441 feat: Phase 1+2 架构进化 — 连续思考链/主动消息决策/情感状态机/离线自主思考 (86文件)
Phase 1 (基础设施):
- ThinkChain 思考链连续性 + 差异化思考提示词 (persistent)
- AutonomousToolPolicy 工具安全策略 (safe/unsafe/conditional)
- MessageScheduler 自适应消息节奏 (Idle/Available/Busy)
- SessionEnrichmentStore 渐进式上下文丰富 (5层)
- ConversationBus 事件总线 + ResponseCache (dedup)
- pkg/logger 统一日志 + 所有 handler 替换 fmt.Printf
- NPE 守卫/链路优化/数据库表修复/Go workspace

Phase 2 (人格交互):
- EmotionState/EmotionTracker 情感状态机 (5种心情, 情绪衰减)
- ProactiveGuard 主动消息多维决策 (静默时段/紧急度/频率/校验)
- Gateway↔ai-core 在线状态感知链路 (presence notification)
- 离线思考频率控制 + 重连问候 + 离线消息排队

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-23 15:25:12 +08:00

105 lines
2.8 KiB
Go

package handler
import (
"encoding/json"
"github.com/yourname/cyrene-ai/pkg/logger"
"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
}
// 调用合成 (Synthesize 内部已包含 fallback 链: edge-tts → espeak-ng → 静默 WAV)
audioData, format, err := h.svc.Synthesize(req.Text, req.Voice, req.Rate)
if err != nil {
logger.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,
})
}