Skip to content

Commit bf08a91

Browse files
authored
Merge pull request #1085 from xinnan-tech/upload-history
add: 增加聊天记录上报
2 parents 9f9227b + f0e353c commit bf08a91

32 files changed

+707
-49
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
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package xiaozhi.modules.agent.controller;
2+
3+
import org.springframework.web.bind.annotation.PostMapping;
4+
import org.springframework.web.bind.annotation.RequestBody;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
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;
12+
import xiaozhi.common.utils.Result;
13+
import xiaozhi.modules.agent.dto.AgentChatHistoryReportDTO;
14+
import xiaozhi.modules.agent.service.biz.AgentChatHistoryBizService;
15+
16+
@Tag(name = "智能体聊天历史管理")
17+
@RequiredArgsConstructor
18+
@RestController
19+
@RequestMapping("/agent/chat-history")
20+
public class AgentChatHistoryController {
21+
private final AgentChatHistoryBizService agentChatHistoryBizService;
22+
23+
/**
24+
* 小智服务聊天上报请求
25+
* <p>
26+
* 小智服务聊天上报请求,包含Base64编码的音频数据和相关信息。
27+
*
28+
* @param request 包含上传文件及相关信息的请求对象
29+
*/
30+
@Operation(summary = "小智服务聊天上报请求")
31+
@PostMapping("/report")
32+
public Result<Boolean> uploadFile(@Valid @RequestBody AgentChatHistoryReportDTO request) {
33+
Boolean result = agentChatHistoryBizService.report(request);
34+
return new Result<Boolean>().ok(result);
35+
}
36+
}

main/manager-api/src/main/java/xiaozhi/modules/agent/dao/AgentDao.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.ibatis.annotations.Mapper;
44
import org.apache.ibatis.annotations.Param;
55

6+
import org.apache.ibatis.annotations.Select;
67
import xiaozhi.common.dao.BaseDao;
78
import xiaozhi.modules.agent.entity.AgentEntity;
89

@@ -15,4 +16,16 @@ public interface AgentDao extends BaseDao<AgentEntity> {
1516
* @return 设备数量
1617
*/
1718
Integer getDeviceCountByAgentId(@Param("agentId") String agentId);
18-
}
19+
20+
/**
21+
* 根据设备MAC地址查询对应设备的默认智能体信息
22+
*
23+
* @param macAddress 设备MAC地址
24+
* @return 默认智能体信息
25+
*/
26+
@Select(" SELECT a.* FROM ai_device d " +
27+
" LEFT JOIN ai_agent a ON d.agent_id = a.id " +
28+
" WHERE d.mac_address = #{macAddress} " +
29+
" ORDER BY d.id DESC LIMIT 1")
30+
AgentEntity getDefaultAgentByMacAddress(@Param("macAddress") String macAddress);
31+
}
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xiaozhi.modules.agent.dao;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import org.apache.ibatis.annotations.Mapper;
5+
import xiaozhi.modules.agent.entity.AgentChatHistoryEntity;
6+
7+
/**
8+
* {@link AgentChatHistoryEntity} 智能体聊天历史记录Dao对象
9+
*
10+
* @author Goody
11+
* @version 1.0, 2025/4/30
12+
* @since 1.0.0
13+
*/
14+
@Mapper
15+
public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity> {
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package xiaozhi.modules.agent.dto;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import jakarta.validation.constraints.NotBlank;
5+
import jakarta.validation.constraints.NotNull;
6+
import lombok.Data;
7+
8+
/**
9+
* 小智设备聊天上报请求
10+
*
11+
* @author Haotian
12+
* @version 1.0, 2025/5/8
13+
*/
14+
@Data
15+
@Schema(description = "小智设备聊天上报请求")
16+
public class AgentChatHistoryReportDTO {
17+
@Schema(description = "MAC地址", example = "00:11:22:33:44:55")
18+
@NotBlank
19+
private String macAddress;
20+
@Schema(description = "会话ID", example = "79578c31-f1fb-426a-900e-1e934215f05a")
21+
@NotBlank
22+
private String sessionId;
23+
@Schema(description = "消息类型: 1-用户, 2-智能体", example = "1")
24+
@NotNull
25+
private Byte chatType;
26+
@Schema(description = "聊天内容", example = "你好呀")
27+
@NotBlank
28+
private String content;
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package xiaozhi.modules.agent.entity;
2+
3+
import java.util.Date;
4+
5+
import com.baomidou.mybatisplus.annotation.IdType;
6+
import com.baomidou.mybatisplus.annotation.TableField;
7+
import com.baomidou.mybatisplus.annotation.TableId;
8+
import com.baomidou.mybatisplus.annotation.TableName;
9+
10+
import lombok.AllArgsConstructor;
11+
import lombok.Builder;
12+
import lombok.Data;
13+
import lombok.NoArgsConstructor;
14+
15+
/**
16+
* 智能体聊天记录表
17+
*
18+
* @author Goody
19+
* @version 1.0, 2025/4/30
20+
* @since 1.0.0
21+
*/
22+
@Data
23+
@Builder
24+
@AllArgsConstructor
25+
@NoArgsConstructor
26+
@TableName(value = "ai_agent_chat_history")
27+
public class AgentChatHistoryEntity {
28+
/**
29+
* 主键ID
30+
*/
31+
@TableId(type = IdType.AUTO)
32+
private Long id;
33+
34+
/**
35+
* MAC地址
36+
*/
37+
@TableField(value = "mac_address")
38+
private String macAddress;
39+
40+
/**
41+
* 智能体id
42+
*/
43+
@TableField(value = "agent_id")
44+
private String agentId;
45+
46+
/**
47+
* 会话ID
48+
*/
49+
@TableField(value = "session_id")
50+
private String sessionId;
51+
52+
/**
53+
* 消息类型: 1-用户, 2-智能体
54+
*/
55+
@TableField(value = "chat_type")
56+
private Byte chatType;
57+
58+
/**
59+
* 聊天内容
60+
*/
61+
@TableField(value = "content")
62+
private String content;
63+
64+
/**
65+
* 音频base64数据
66+
*/
67+
@TableField(value = "audio_id")
68+
private String audioId;
69+
70+
/**
71+
* 音频URL
72+
*/
73+
@TableField(value = "audio_url")
74+
private String audioUrl;
75+
76+
/**
77+
* 创建时间
78+
*/
79+
@TableField(value = "created_at")
80+
private Date createdAt;
81+
82+
/**
83+
* 更新时间
84+
*/
85+
@TableField(value = "updated_at")
86+
private Date updatedAt;
87+
}
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package xiaozhi.modules.agent.service;
2+
3+
import com.baomidou.mybatisplus.extension.service.IService;
4+
import xiaozhi.modules.agent.entity.AgentChatHistoryEntity;
5+
6+
/**
7+
* 智能体聊天记录表处理service
8+
*
9+
* @author Goody
10+
* @version 1.0, 2025/4/30
11+
* @since 1.0.0
12+
*/
13+
public interface AgentChatHistoryService extends IService<AgentChatHistoryEntity> {
14+
}

main/manager-api/src/main/java/xiaozhi/modules/agent/service/AgentService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public interface AgentService extends BaseService<AgentEntity> {
4242
* @return 设备数量
4343
*/
4444
Integer getDeviceCountByAgentId(String agentId);
45-
}
45+
46+
/**
47+
* 根据设备MAC地址查询对应设备的默认智能体信息
48+
*
49+
* @param macAddress 设备MAC地址
50+
* @return 默认智能体信息,不存在时返回null
51+
*/
52+
AgentEntity getDefaultAgentByMacAddress(String macAddress);
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xiaozhi.modules.agent.service.biz;
2+
3+
import xiaozhi.modules.agent.dto.AgentChatHistoryReportDTO;
4+
5+
/**
6+
* 智能体聊天历史业务逻辑层
7+
*
8+
* @author Goody
9+
* @version 1.0, 2025/4/30
10+
* @since 1.0.0
11+
*/
12+
public interface AgentChatHistoryBizService {
13+
14+
/**
15+
* 聊天上报方法
16+
*
17+
* @param agentChatHistoryReportDTO 包含聊天上报所需信息的输入对象
18+
* 例如:设备MAC地址、文件类型、内容等
19+
* @return 上传结果,true表示成功,false表示失败
20+
*/
21+
Boolean report(AgentChatHistoryReportDTO agentChatHistoryReportDTO);
22+
}

0 commit comments

Comments
 (0)