主人 2026-10-03 拍板「注释一律中文」;gateway 至此整包注释中文化完成(本会话共 3 批: 已推库 16 个文件 + 本批剩余文件)。 校验:机械校验(非注释改动行)= 空;剥注释后代码文本逐行比对全等(不一致文件数 0); 全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。
178 lines
6.6 KiB
Go
178 lines
6.6 KiB
Go
package gateway
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
|
||
apperr "git.yeij.top/AskaEth/Cyrene_v2/internal/errors"
|
||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/multimodal/voice"
|
||
)
|
||
|
||
// maxAudioRequestBytes 限定 /api/v1/audio/* 请求可携带的 JSON 体大小,
|
||
// 使巨大的 base64 音频载荷无法耗尽内存。
|
||
const maxAudioRequestBytes = 32 << 20
|
||
|
||
// transcribeRequest 是 POST /api/v1/audio/transcribe 的请求体。Audio 是 base64
|
||
// (或 data: URI)数据块;Format 是其编码("wav"/"mp3"/"ogg")。
|
||
type transcribeRequest struct {
|
||
Audio string `json:"audio"`
|
||
Format string `json:"format"`
|
||
DeviceID string `json:"deviceId,omitempty"`
|
||
}
|
||
|
||
// speechRequest 是 POST /api/v1/audio/speech 的请求体。Voice/format 为前向
|
||
// 兼容而被接受;合成目前遵循服务端的
|
||
// VOICE_TTS_VOICE_ID / VOICE_TTS_FORMAT 配置。Instruction 是可选的
|
||
// 每请求情绪/语气覆盖,用于克隆音色(声音复刻);系统音色
|
||
// (longxiaochun_v3)忽略它,绝不接收自由形式的指令。
|
||
type speechRequest struct {
|
||
Text string `json:"text"`
|
||
Voice string `json:"voice,omitempty"`
|
||
Format string `json:"format,omitempty"`
|
||
Instruction string `json:"instruction,omitempty"`
|
||
}
|
||
|
||
// handleTranscribe 对上传的音频执行 STT 并返回识别出的
|
||
// 文本。它是原生客户端的 REST 来源,绕过 4KB WebSocket
|
||
// 帧上限(该上限会截断 base64 音频块)。
|
||
func (s *Server) handleTranscribe(w http.ResponseWriter, r *http.Request) {
|
||
r.Body = http.MaxBytesReader(w, r.Body, maxAudioRequestBytes)
|
||
var req transcribeRequest
|
||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
s.auditClaims(r, "audio.transcribe", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "invalid request body"))
|
||
return
|
||
}
|
||
if s.voice == nil {
|
||
s.auditClaims(r, "audio.transcribe", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "voice_unavailable", "voice service is not configured"))
|
||
return
|
||
}
|
||
audio, err := decodeVoiceAudio(req.Audio, req.Format)
|
||
if err != nil {
|
||
s.auditClaims(r, "audio.transcribe", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "audio could not be decoded"))
|
||
return
|
||
}
|
||
if len(audio.Data) == 0 {
|
||
s.auditClaims(r, "audio.transcribe", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "audio is required"))
|
||
return
|
||
}
|
||
text, rung, err := sttWithProvider(r.Context(), s.voice, audio)
|
||
if err != nil {
|
||
s.auditClaims(r, "audio.transcribe", "error")
|
||
s.log.Warn("voice transcribe", "err", err)
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "stt_failed", "speech transcription failed"))
|
||
return
|
||
}
|
||
s.auditClaims(r, "audio.transcribe", "ok")
|
||
out := map[string]any{"text": text}
|
||
if sig := providerSignal(rung, strings.TrimSpace(text) == ""); sig != "" {
|
||
out["provider"] = sig
|
||
}
|
||
writeJSON(w, http.StatusOK, out)
|
||
}
|
||
|
||
// handleSpeech 为给定文本合成语音并写回原始音频
|
||
// 字节(audio/<format>)—— 二进制响应,不是 JSON 信封。
|
||
func (s *Server) handleSpeech(w http.ResponseWriter, r *http.Request) {
|
||
r.Body = http.MaxBytesReader(w, r.Body, maxAudioRequestBytes)
|
||
var req speechRequest
|
||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
s.auditClaims(r, "audio.speech", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "invalid request body"))
|
||
return
|
||
}
|
||
if strings.TrimSpace(req.Text) == "" {
|
||
s.auditClaims(r, "audio.speech", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "text is required"))
|
||
return
|
||
}
|
||
if s.voice == nil {
|
||
s.auditClaims(r, "audio.speech", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "voice_unavailable", "voice service is not configured"))
|
||
return
|
||
}
|
||
ctx := r.Context()
|
||
if req.Instruction != "" {
|
||
ctx = voice.WithTTSInstruction(ctx, req.Instruction)
|
||
}
|
||
audi, rung, err := ttsWithProvider(ctx, s.voice, req.Text)
|
||
if err != nil {
|
||
s.auditClaims(r, "audio.speech", "error")
|
||
s.log.Warn("voice speech", "err", err)
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "tts_failed", "speech synthesis failed"))
|
||
return
|
||
}
|
||
if len(audi.Data) == 0 {
|
||
s.auditClaims(r, "audio.speech", "error")
|
||
w.Header().Set(voiceProviderHeader, providerSignal(rung, true))
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "tts_empty", "no audio synthesized"))
|
||
return
|
||
}
|
||
s.auditClaims(r, "audio.speech", "ok")
|
||
w.Header().Set("Content-Type", audioMime(audi.Format))
|
||
w.Header().Set("Content-Length", strconv.Itoa(len(audi.Data)))
|
||
w.WriteHeader(http.StatusOK)
|
||
_, _ = w.Write(audi.Data)
|
||
}
|
||
|
||
// voiceCloneRequest 是 POST /api/v1/audio/voice-clone 的请求体。URL 是公开的
|
||
// 参考音频 URL;prefix 是可选音色名前缀(≤10 个字母数字字符)。
|
||
// 二者都不记日志,URL 也不持久化(仅 owner + 审计,红线)。
|
||
type voiceCloneRequest struct {
|
||
URL string `json:"url"`
|
||
Prefix string `json:"prefix,omitempty"`
|
||
}
|
||
|
||
// handleVoiceClone 经百炼声音注册 API(声音复刻 create_voice)从公开
|
||
// 参考音频 URL 创建自定义 TTS 音色,并返回新的
|
||
// voice_id。仅 owner + 带审计;参考 URL 绝不写入审计
|
||
// 记录或日志(红线)。
|
||
func (s *Server) handleVoiceClone(w http.ResponseWriter, r *http.Request) {
|
||
r.Body = http.MaxBytesReader(w, r.Body, maxAudioRequestBytes)
|
||
var req voiceCloneRequest
|
||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
s.auditClaims(r, "audio.voice-clone", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "invalid request body"))
|
||
return
|
||
}
|
||
if strings.TrimSpace(req.URL) == "" {
|
||
s.auditClaims(r, "audio.voice-clone", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrBadRequest, "bad_request", "url is required"))
|
||
return
|
||
}
|
||
if s.voice == nil {
|
||
s.auditClaims(r, "audio.voice-clone", "error")
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "voice_unavailable", "voice service is not configured"))
|
||
return
|
||
}
|
||
voiceID, err := s.voice.CloneVoice(r.Context(), req.URL, req.Prefix)
|
||
if err != nil {
|
||
s.auditClaims(r, "audio.voice-clone", "error")
|
||
// 不要记录原因:provider 的错误体可能回显参考 URL。
|
||
s.log.Warn("voice clone failed")
|
||
writeError(w, apperr.Wrap(apperr.ErrInternal, "voice_clone_failed", "voice clone failed"))
|
||
return
|
||
}
|
||
s.auditClaims(r, "audio.voice-clone", "ok")
|
||
writeJSON(w, http.StatusOK, map[string]any{"voiceId": voiceID})
|
||
}
|
||
|
||
// audioMime 把音频格式映射为其用于语音响应的 Content-Type。
|
||
func audioMime(format string) string {
|
||
switch strings.ToLower(format) {
|
||
case "mp3", "mpeg":
|
||
return "audio/mpeg"
|
||
case "ogg", "oga":
|
||
return "audio/ogg"
|
||
case "webm":
|
||
return "audio/webm"
|
||
default:
|
||
return "audio/wav"
|
||
}
|
||
}
|