Skip to content

Commit f0e353c

Browse files
committed
update:优化对话数据上传
1 parent 456b1ed commit f0e353c

22 files changed

+295
-340
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ main/manager-api/.vscode
165165
main/manager-web/.webpack_cache/
166166
main/xiaozhi-server/mysql
167167
uploadfile
168+
.vscode

main/manager-api/src/main/java/xiaozhi/common/service/impl/BaseServiceImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,10 @@ protected static boolean retBool(Integer result) {
128128
return SqlHelper.retBool(result);
129129
}
130130

131-
@SuppressWarnings("unchecked")
132131
protected Class<M> currentMapperClass() {
133132
return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 0);
134133
}
135134

136-
@SuppressWarnings("unchecked")
137135
@Override
138136
public Class<T> currentModelClass() {
139137
return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);

main/manager-api/src/main/java/xiaozhi/common/service/impl/CrudServiceImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T, D> extends BaseServiceImpl<M, T>
2525
implements CrudService<T, D> {
2626

27-
@SuppressWarnings("unchecked")
2827
protected Class<D> currentDtoClass() {
2928
return (Class<D>) ReflectionKit.getSuperClassGenericType(getClass(), CrudServiceImpl.class, 2);
3029
}

main/manager-api/src/main/java/xiaozhi/modules/agent/controller/AgentChatHistoryController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package xiaozhi.modules.agent.controller;
22

3-
import io.swagger.v3.oas.annotations.Operation;
4-
import io.swagger.v3.oas.annotations.tags.Tag;
5-
import jakarta.validation.Valid;
6-
import lombok.RequiredArgsConstructor;
73
import org.springframework.web.bind.annotation.PostMapping;
84
import org.springframework.web.bind.annotation.RequestBody;
95
import org.springframework.web.bind.annotation.RequestMapping;
106
import org.springframework.web.bind.annotation.RestController;
7+
8+
import io.swagger.v3.oas.annotations.Operation;
9+
import io.swagger.v3.oas.annotations.tags.Tag;
10+
import jakarta.validation.Valid;
11+
import lombok.RequiredArgsConstructor;
1112
import xiaozhi.common.utils.Result;
1213
import xiaozhi.modules.agent.dto.AgentChatHistoryReportDTO;
1314
import xiaozhi.modules.agent.service.biz.AgentChatHistoryBizService;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package xiaozhi.modules.agent.dao;
2+
3+
import org.apache.ibatis.annotations.Mapper;
4+
5+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
6+
7+
import xiaozhi.modules.agent.entity.AgentChatAudioEntity;
8+
9+
/**
10+
* {@link AgentChatAudioEntity} 智能体聊天音频数据Dao对象
11+
*
12+
* @author Goody
13+
* @version 1.0, 2025/5/8
14+
* @since 1.0.0
15+
*/
16+
@Mapper
17+
public interface AiAgentChatAudioDao extends BaseMapper<AgentChatAudioEntity> {
18+
}

main/manager-api/src/main/java/xiaozhi/modules/agent/dto/AgentChatHistoryReportDTO.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ public class AgentChatHistoryReportDTO {
2020
@Schema(description = "会话ID", example = "79578c31-f1fb-426a-900e-1e934215f05a")
2121
@NotBlank
2222
private String sessionId;
23-
@Schema(description = "排序值(与session_id对应)", example = "1745566378")
24-
@NotNull
25-
private Long sort;
2623
@Schema(description = "消息类型: 1-用户, 2-智能体", example = "1")
2724
@NotNull
2825
private Byte chatType;
2926
@Schema(description = "聊天内容", example = "你好呀")
3027
@NotBlank
3128
private String content;
32-
@Schema(description = "文件数据(Base64编码)", example = "")
33-
private String fileBase64;
34-
@Schema(description = "文件扩展名(如wav、mp3等)", example = "wav")
35-
private String fileExtension;
36-
}
29+
@Schema(description = "文件数据(opus编码)", example = "")
30+
private String opusDataBase64;
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xiaozhi.modules.agent.entity;
2+
3+
import com.baomidou.mybatisplus.annotation.IdType;
4+
import com.baomidou.mybatisplus.annotation.TableId;
5+
import com.baomidou.mybatisplus.annotation.TableName;
6+
7+
import lombok.Data;
8+
9+
/**
10+
* 智能体聊天音频数据表
11+
*
12+
* @author Goody
13+
* @version 1.0, 2025/5/8
14+
* @since 1.0.0
15+
*/
16+
@Data
17+
@TableName("ai_agent_chat_audio")
18+
public class AgentChatAudioEntity {
19+
/**
20+
* 主键ID
21+
*/
22+
@TableId(type = IdType.ASSIGN_UUID)
23+
private String id;
24+
25+
/**
26+
* 音频opus数据
27+
*/
28+
private byte[] audio;
29+
}

main/manager-api/src/main/java/xiaozhi/modules/agent/entity/AgentChatHistoryEntity.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.baomidou.mybatisplus.annotation.TableField;
77
import com.baomidou.mybatisplus.annotation.TableId;
88
import com.baomidou.mybatisplus.annotation.TableName;
9+
910
import lombok.AllArgsConstructor;
1011
import lombok.Builder;
1112
import lombok.Data;
@@ -25,68 +26,62 @@
2526
@TableName(value = "ai_agent_chat_history")
2627
public class AgentChatHistoryEntity {
2728
/**
28-
* 主键ID
29-
*/
29+
* 主键ID
30+
*/
3031
@TableId(type = IdType.AUTO)
3132
private Long id;
3233

3334
/**
34-
* MAC地址
35-
*/
35+
* MAC地址
36+
*/
3637
@TableField(value = "mac_address")
3738
private String macAddress;
3839

3940
/**
40-
* 智能体id
41-
*/
41+
* 智能体id
42+
*/
4243
@TableField(value = "agent_id")
4344
private String agentId;
4445

4546
/**
46-
* 会话ID
47-
*/
47+
* 会话ID
48+
*/
4849
@TableField(value = "session_id")
4950
private String sessionId;
5051

5152
/**
52-
* 排序值(与session_id对应),使用时间戳,方便排序
53-
*/
54-
@TableField(value = "sort")
55-
private Long sort;
56-
57-
/**
58-
* 消息类型: 1-用户, 2-智能体
59-
*/
53+
* 消息类型: 1-用户, 2-智能体
54+
*/
6055
@TableField(value = "chat_type")
6156
private Byte chatType;
6257

6358
/**
64-
* 聊天内容
65-
*/
59+
* 聊天内容
60+
*/
6661
@TableField(value = "content")
6762
private String content;
6863

6964
/**
70-
* 音频base64数据
71-
*/
72-
@TableField(value = "audio")
73-
private String audio;
65+
* 音频base64数据
66+
*/
67+
@TableField(value = "audio_id")
68+
private String audioId;
7469

7570
/**
76-
* 音频URL
77-
*/
71+
* 音频URL
72+
*/
7873
@TableField(value = "audio_url")
7974
private String audioUrl;
8075

8176
/**
82-
* 创建时间
83-
*/
77+
* 创建时间
78+
*/
8479
@TableField(value = "created_at")
8580
private Date createdAt;
8681

8782
/**
88-
* 更新时间
89-
*/
83+
* 更新时间
84+
*/
9085
@TableField(value = "updated_at")
9186
private Date updatedAt;
9287
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xiaozhi.modules.agent.service;
2+
3+
import com.baomidou.mybatisplus.extension.service.IService;
4+
5+
import xiaozhi.modules.agent.entity.AgentChatAudioEntity;
6+
7+
/**
8+
* 智能体聊天音频数据表处理service
9+
*
10+
* @author Goody
11+
* @version 1.0, 2025/5/8
12+
* @since 1.0.0
13+
*/
14+
public interface AgentChatAudioService extends IService<AgentChatAudioEntity> {
15+
/**
16+
* 保存音频数据
17+
*
18+
* @param audioData 音频数据
19+
* @return 音频ID
20+
*/
21+
String saveAudio(byte[] audioData);
22+
}
Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package xiaozhi.modules.agent.service.biz.impl;
22

3-
import lombok.RequiredArgsConstructor;
4-
import lombok.extern.slf4j.Slf4j;
53
import org.springframework.stereotype.Service;
64
import org.springframework.transaction.annotation.Transactional;
5+
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
78
import xiaozhi.modules.agent.dto.AgentChatHistoryReportDTO;
8-
import xiaozhi.modules.agent.entity.AgentEntity;
99
import xiaozhi.modules.agent.entity.AgentChatHistoryEntity;
10+
import xiaozhi.modules.agent.entity.AgentEntity;
11+
import xiaozhi.modules.agent.service.AgentChatAudioService;
1012
import xiaozhi.modules.agent.service.AgentChatHistoryService;
1113
import xiaozhi.modules.agent.service.AgentService;
1214
import xiaozhi.modules.agent.service.biz.AgentChatHistoryBizService;
1315

14-
import javax.annotation.Nullable;
15-
1616
/**
1717
* {@link AgentChatHistoryBizService} impl
1818
*
@@ -26,6 +26,7 @@
2626
public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizService {
2727
private final AgentService agentService;
2828
private final AgentChatHistoryService agentChatHistoryService;
29+
private final AgentChatAudioService agentChatAudioService;
2930

3031
/**
3132
* 处理聊天记录上报,包括文件上传和相关信息记录
@@ -36,48 +37,45 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic
3637
@Override
3738
@Transactional(rollbackFor = Exception.class)
3839
public Boolean report(AgentChatHistoryReportDTO report) {
39-
final String macAddress = report.getMacAddress();
40-
final Byte chatType = report.getChatType();
40+
String macAddress = report.getMacAddress();
41+
Byte chatType = report.getChatType();
4142
log.info("小智设备聊天上报请求: macAddress={}, type={}", macAddress, chatType);
4243

43-
// 1. 上传音频文件
44-
final String uploadUrl = this.upload(report);
44+
// 1. base64解码report.getOpusDataBase64(),存入ai_agent_chat_audio表
45+
String audioId = null;
46+
if (report.getOpusDataBase64() != null && !report.getOpusDataBase64().isEmpty()) {
47+
try {
48+
// TODO: 需要考虑保留什么格式的音频数据,比如是opus还是wave
49+
// byte[] audioData = Base64.getDecoder().decode(report.getOpusDataBase64());
50+
// audioId = agentChatAudioService.saveAudio(audioData);
51+
// log.info("音频数据保存成功,audioId={}", audioId);
52+
} catch (Exception e) {
53+
log.error("音频数据保存失败", e);
54+
return false;
55+
}
56+
}
4557

4658
// 2. 组装上报数据
4759
// 2.1 根据设备MAC地址查询对应的默认智能体,判断是否需要上报
4860
AgentEntity agentEntity = agentService.getDefaultAgentByMacAddress(macAddress);
4961
if (agentEntity == null) {
5062
return false;
5163
}
52-
final String agentId = agentEntity.getId();
64+
String agentId = agentEntity.getId();
5365
log.info("设备 {} 对应智能体 {} 上报", macAddress, agentEntity.getId());
5466

5567
// 2.2 构建聊天记录实体
56-
final AgentChatHistoryEntity entity = AgentChatHistoryEntity.builder()
68+
AgentChatHistoryEntity entity = AgentChatHistoryEntity.builder()
5769
.macAddress(macAddress)
5870
.agentId(agentId)
5971
.sessionId(report.getSessionId())
60-
.sort(report.getSort())
6172
.chatType(report.getChatType())
6273
.content(report.getContent())
63-
.audio(report.getFileBase64())
64-
.audioUrl(uploadUrl)
74+
.audioId(audioId)
6575
.build();
6676

6777
// 3. 保存数据
6878
agentChatHistoryService.save(entity);
6979
return Boolean.TRUE;
7080
}
71-
72-
/**
73-
* 上传文件
74-
*
75-
* @param report 上报文件数据
76-
* @return 上传文件url
77-
*/
78-
@Nullable
79-
private String upload(AgentChatHistoryReportDTO report) {
80-
// TODO(haotian): 2025/4/30 根据需要自定义完成上传生成url即可
81-
return null;
82-
}
8381
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package xiaozhi.modules.agent.service.impl;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
6+
7+
import xiaozhi.modules.agent.dao.AiAgentChatAudioDao;
8+
import xiaozhi.modules.agent.entity.AgentChatAudioEntity;
9+
import xiaozhi.modules.agent.service.AgentChatAudioService;
10+
11+
/**
12+
* 智能体聊天音频数据表处理service {@link AgentChatAudioService} impl
13+
*
14+
* @author Goody
15+
* @version 1.0, 2025/5/8
16+
* @since 1.0.0
17+
*/
18+
@Service
19+
public class AgentChatAudioServiceImpl extends ServiceImpl<AiAgentChatAudioDao, AgentChatAudioEntity>
20+
implements AgentChatAudioService {
21+
@Override
22+
public String saveAudio(byte[] audioData) {
23+
AgentChatAudioEntity entity = new AgentChatAudioEntity();
24+
entity.setAudio(audioData);
25+
save(entity);
26+
return entity.getId();
27+
}
28+
}

main/manager-api/src/main/java/xiaozhi/modules/config/controller/ConfigController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package xiaozhi.modules.config.controller;
22

3-
import jakarta.validation.Valid;
43
import org.springframework.web.bind.annotation.PostMapping;
54
import org.springframework.web.bind.annotation.RequestBody;
65
import org.springframework.web.bind.annotation.RequestMapping;
76
import org.springframework.web.bind.annotation.RestController;
87

98
import io.swagger.v3.oas.annotations.Operation;
109
import io.swagger.v3.oas.annotations.tags.Tag;
10+
import jakarta.validation.Valid;
1111
import lombok.AllArgsConstructor;
1212
import xiaozhi.common.utils.Result;
1313
import xiaozhi.common.validator.ValidatorUtils;
1414
import xiaozhi.modules.config.dto.AgentModelsDTO;
1515
import xiaozhi.modules.config.service.ConfigService;
16-
import xiaozhi.modules.sys.service.SysParamsService;
1716

1817
/**
1918
* xiaozhi-server 配置获取
@@ -26,7 +25,6 @@
2625
@AllArgsConstructor
2726
public class ConfigController {
2827
private final ConfigService configService;
29-
private final SysParamsService sysParamsService;
3028

3129
@PostMapping("server-base")
3230
@Operation(summary = "获取配置")

0 commit comments

Comments
 (0)