Files
CamTalk/backend/internal/models/models.go
cfy666 da5c727d8c fix: 修复语音与文字不同步的问题,改为句子级流式播放
之前前端 TTSPlayer 攒齐所有音频片段后才播放,导致文字全部显示后才开始语音。
改为后端每句 TTS 发送 is_last: true,前端收到每句即加入播放队列,第一句到达即开始播放。

- 后端 Chunk 结构体新增 Final 字段,区分句子结束和整轮结束
- 前端 TTSPlayer 重写为队列式播放,onended 回调自动衔接下一句
- 同步更新接口文档和测试用例
2026-06-14 13:54:49 +08:00

131 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import "time"
// Session 会话。
type Session struct {
ID string `json:"session_id"`
CreatedAt time.Time `json:"created_at"`
Config SessionConfig `json:"config"`
}
// SessionConfig 会话配置。
type SessionConfig struct {
TTSEnabled bool `json:"tts_enabled"`
DetailLevel string `json:"detail_level"` // "low" | "high"
Language string `json:"language"`
}
// DefaultConfig 默认会话配置。
func DefaultConfig() SessionConfig {
return SessionConfig{TTSEnabled: true, DetailLevel: "low", Language: "zh-CN"}
}
// SessionConfigPatch 会话配置增量更新(指针字段表示"未传则不更新")。
type SessionConfigPatch struct {
TTSEnabled *bool `json:"tts_enabled,omitempty"`
DetailLevel *string `json:"detail_level,omitempty"`
Language *string `json:"language,omitempty"`
}
// Apply 将 patch 中的非 nil 字段覆盖到 cfg。
func (p SessionConfigPatch) Apply(cfg *SessionConfig) {
if p.TTSEnabled != nil {
cfg.TTSEnabled = *p.TTSEnabled
}
if p.DetailLevel != nil {
cfg.DetailLevel = *p.DetailLevel
}
if p.Language != nil {
cfg.Language = *p.Language
}
}
// Message 对话消息。
type Message struct {
Role string `json:"role"` // "user" | "assistant"
Content string `json:"content"`
}
// --- WebSocket 消息 ---
// WsQuery 客户端 query 消息。
type WsQuery struct {
Type string `json:"type"`
RequestID string `json:"request_id"`
Image string `json:"image"` // base64
Audio string `json:"audio"` // base64
Text string `json:"text"` // 用户手动输入的文本(有值时跳过 STT
MimeType string `json:"mime_type"` // 默认 "audio/pcm"
}
// WsConfig 客户端 config 消息。
type WsConfig struct {
Type string `json:"type"`
Payload struct {
TTSEnabled *bool `json:"tts_enabled,omitempty"`
DetailLevel *string `json:"detail_level,omitempty"`
Language *string `json:"language,omitempty"`
} `json:"payload"`
}
// WsConnected 服务端 connected 消息。
type WsConnected struct {
Type string `json:"type"`
SessionID string `json:"session_id"`
ServerVersion string `json:"server_version"`
}
// WsSTTResult 服务端 stt_result 消息。
type WsSTTResult struct {
Type string `json:"type"`
RequestID string `json:"request_id"`
Text string `json:"text"`
IsFinal bool `json:"is_final"`
}
// WsLLMChunk 服务端 llm_chunk 消息。
type WsLLMChunk struct {
Type string `json:"type"`
RequestID string `json:"request_id"`
Delta string `json:"delta"`
Role string `json:"role"`
}
// WsLLMDone 服务端 llm_done 消息。
type WsLLMDone struct {
Type string `json:"type"`
RequestID string `json:"request_id"`
FullText string `json:"full_text"`
TokensUsed struct {
Prompt int `json:"prompt"`
Completion int `json:"completion"`
Total int `json:"total"`
} `json:"tokens_used"`
Model string `json:"model"`
LatencyMs int64 `json:"latency_ms"`
}
// WsTTSAudio 服务端 tts_audio 消息。
type WsTTSAudio struct {
Type string `json:"type"`
RequestID string `json:"request_id"`
Audio string `json:"audio"` // base64
MimeType string `json:"mime_type"` // "audio/mp3" 或 "audio/pcm"
IsLast bool `json:"is_last"` // 当前句子的音频是否完整(每句结束时为 true
Final bool `json:"final"` // 整轮 TTS 是否结束(所有句子合成完毕后为 true
}
// WsError 服务端 error 消息。
type WsError struct {
Type string `json:"type"`
RequestID string `json:"request_id,omitempty"`
Code string `json:"code"`
Message string `json:"message"`
}
// WsPong 服务端 pong 消息。
type WsPong struct {
Type string `json:"type"`
}