本文档详细描述了前后端之间的接口定义,包括WebSocket消息格式、HTTP API接口、错误码定义等。
- 协议: WebSocket (ws://)
- 默认地址:
ws://localhost:12393/client-ws - 数据格式: JSON
interface WSMessage {
type: string // 消息类型 (必需)
action?: string // 操作类型 (可选)
text?: string // 文本内容 (可选)
audio?: number[] // 音频数据 (可选)
images?: string[] // 图片列表 (可选)
history_uid?: string // 历史记录UID (可选)
file?: string // 文件名 (可选)
display_text?: DisplayText // 显示文本对象 (可选)
}interface DisplayText {
text: string // 显示文本
name?: string // 发送者名称 (默认: "AI")
avatar?: string // 头像路径 (可选)
}{
"type": "add-client-to-group",
"text": "group_name"
}{
"type": "remove-client-from-group",
"text": "group_name"
}{
"type": "request-group-info"
}{
"type": "fetch-history-list"
}{
"type": "fetch-and-set-history",
"history_uid": "history-uuid-string"
}{
"type": "create-new-history"
}{
"type": "delete-history",
"history_uid": "history-uuid-string"
}{
"type": "text-input",
"text": "用户输入的文本内容"
}{
"type": "mic-audio-end"
}{
"type": "ai-speak-signal"
}{
"type": "fetch-configs"
}{
"type": "switch-config",
"file": "character-config-name.yaml"
}{
"type": "fetch-backgrounds"
}{
"type": "interrupt-signal"
}{
"type": "audio-play-start"
}{
"type": "mic-audio-data",
"audio": [0.1, 0.2, -0.1, ...]
}{
"type": "raw-audio-data",
"audio": [0.1, 0.2, -0.1, ...]
}{
"type": "audio",
"audio": "/path/to/audio/file.wav",
"display_text": {
"text": "AI回复的文本内容",
"name": "AI",
"avatar": "/path/to/avatar.png"
},
"actions": ["smile", "wave"]
}{
"type": "text",
"text": "AI回复的纯文本内容",
"display_text": {
"text": "AI回复的文本内容",
"name": "AI"
}
}{
"type": "configs",
"configs": {
"character": [
{
"file": "character1.yaml",
"conf_name": "角色1",
"conf_uid": "uuid-string"
}
],
"background": [
{
"file": "bg1.yaml",
"name": "背景1"
}
]
}
}{
"type": "set-model-and-conf",
"client_uid": "client-uuid",
"conf_name": "当前配置名称",
"conf_uid": "config-uuid",
"live2d_model_name": "模型名称",
"character_name": "角色名称",
"human_name": "用户名称"
}{
"type": "history-list",
"histories": [
{
"uid": "history-uuid",
"name": "对话标题",
"created_at": "2024-01-01T00:00:00Z",
"message_count": 10
}
]
}{
"type": "history-data",
"history_uid": "history-uuid",
"messages": [
{
"role": "user",
"content": "用户消息",
"timestamp": "2024-01-01T00:00:00Z"
},
{
"role": "assistant",
"content": "AI回复",
"timestamp": "2024-01-01T00:00:01Z"
}
]
}{
"type": "control",
"text": "interrupt" // 或 "mic-audio-end"
}{
"type": "error",
"message": "错误描述信息",
"code": "ERROR_CODE",
"details": {
"context": "错误上下文",
"timestamp": "2024-01-01T00:00:00Z"
}
}1001: WebSocket连接失败1002: 连接超时1003: 认证失败1004: 协议版本不匹配
1101: 消息格式错误1102: 必需字段缺失1103: 数据类型错误1104: 消息过大
1201: 配置文件不存在1202: 历史记录不存在1203: 群组操作失败1204: 音频处理失败
1301: 服务器内部错误1302: 服务不可用1303: 资源不足1304: 超时错误
const MessagePriority = {
HIGH: 0, // 高优先级 (控制命令)
NORMAL: 1, // 普通优先级 (聊天消息)
LOW: 2 // 低优先级 (状态更新)
}- HIGH: 中断信号、控制命令
- NORMAL: 文本输入、音频数据
- LOW: 状态更新、统计信息
用户输入 → text-input → 后端处理 → audio/text → 前端显示
录音开始 → mic-audio-data → VAD检测 → mic-audio-end →
后端ASR → 后端处理 → audio/text → 前端播放/显示
fetch-configs → 后端扫描 → configs → 前端显示 →
switch-config → 后端切换 → set-model-and-conf → 前端更新
- 所有输入数据必须进行类型和格式验证
- 音频数据大小限制
- 文本长度限制
- 文件路径安全检查
- 优雅的错误降级
- 详细的错误日志
- 用户友好的错误提示
- 自动重连机制
- 消息队列管理
- 连接池复用
- 数据压缩
- 超时控制
// 启用详细日志
window.DEBUG_WEBSOCKET = true
// 监听所有消息
window.addEventListener('websocket:message', (event) => {
console.log('WebSocket消息:', event.detail)
})// 测试连接
const testConnection = async () => {
const ws = new WebSocket('ws://localhost:12393/client-ws')
ws.onopen = () => console.log('连接成功')
ws.onerror = (error) => console.error('连接失败:', error)
}- 保持消息结构简单明确
- 使用有意义的消息类型名称
- 包含必要的错误处理信息
- 避免过大的消息负载
- 提供详细的错误信息
- 实现自动重试机制
- 记录错误日志用于调试
- 向用户提供友好的错误提示
- 合理使用消息优先级
- 避免频繁发送小消息
- 实现消息批处理
- 监控连接状态和性能
- 初始版本,支持基础聊天功能
- 实现配置管理和历史记录
- 添加音频数据传输
- 支持群组操作
- 添加文件传输支持
- 实现消息加密
- 支持多媒体消息
- 优化大数据传输
本接口文档提供了前后端WebSocket通信的完整规范。开发者应该严格按照定义的消息格式进行开发,确保前后端的兼容性。建议在开发过程中使用提供的调试工具进行测试,并及时更新文档以反映接口变更。 AI语音对话功能问题与解决过程总结 问题现象
启动语音对话时,前端 onAudioData 回调收到的数据类型不一致,有时为 { data: Float32Array },有时为 Float32Array。 前端尝试将音频数据转换为 Uint8Array 或 ArrayBuffer,导致调用 webSocketStore.sendAudioData 时类型不匹配。 控制台报错:“音频数据类型仅支持 Float32Array 或 Int16Array”。 排查过程
通过日志输出,确认 onAudioData 回调的数据结构和类型。 检查 websocket.js 的 sendAudioData 方法,发现只接受 Float32Array 或 Int16Array,否则报错。 发现前端多余的类型转换导致类型不兼容。 解决措施
在 onAudioData 回调中,兼容 { data: Float32Array } 结构,提取 .data 字段。 只允许 Float32Array 或 Int16Array 直接传递给 webSocketStore.sendAudioData,不再做多余的类型转换。 增加类型检查和详细日志,便于后续排查。 最终效果
报错消失,音频数据能够正确发送,前后端数据类型完全兼容,AI语音对话功能恢复正常。 经验总结:
前后端音频数据类型必须严格一致,避免多余的类型转换。 回调数据结构不统一时,需在前端做兼容处理。 日志输出有助于快速定位和解决类型兼容性问题。