update:优化记忆体布局

This commit is contained in:
hrz
2025-05-14 12:09:29 +08:00
parent da3a5df5c3
commit be3a030302
5 changed files with 96 additions and 70 deletions
@@ -39,6 +39,7 @@ import xiaozhi.modules.agent.dto.AgentChatHistoryDTO;
import xiaozhi.modules.agent.dto.AgentChatSessionDTO;
import xiaozhi.modules.agent.dto.AgentCreateDTO;
import xiaozhi.modules.agent.dto.AgentDTO;
import xiaozhi.modules.agent.dto.AgentMemoryDTO;
import xiaozhi.modules.agent.dto.AgentUpdateDTO;
import xiaozhi.modules.agent.entity.AgentEntity;
import xiaozhi.modules.agent.entity.AgentTemplateEntity;
@@ -128,14 +129,16 @@ public class AgentController {
return new Result<String>().ok(entity.getId());
}
@PutMapping("/device/{macAddress}")
@PutMapping("/saveMemory/{macAddress}")
@Operation(summary = "根据设备id更新智能体")
public Result<Void> updateByDeviceId(@PathVariable String macAddress, @RequestBody @Valid AgentUpdateDTO dto) {
public Result<Void> updateByDeviceId(@PathVariable String macAddress, @RequestBody @Valid AgentMemoryDTO dto) {
DeviceEntity device = deviceService.getDeviceByMacAddress(macAddress);
if (device == null) {
return new Result<>();
}
return updateAgentById(device.getAgentId(), dto);
AgentUpdateDTO agentUpdateDTO = new AgentUpdateDTO();
agentUpdateDTO.setSummaryMemory(dto.getSummaryMemory());
return updateAgentById(device.getAgentId(), agentUpdateDTO);
}
@PutMapping("/{id}")
@@ -204,17 +207,16 @@ public class AgentController {
existingEntity.setUpdater(user.getId());
existingEntity.setUpdatedAt(new Date());
agentService.updateById(existingEntity);
// 更新记忆策略
if (existingEntity.getMemModelId() == null || existingEntity.getMemModelId().equals(Constant.MEMORY_NO_MEM)) {
// 删除所有记录
agentChatHistoryService.deleteByAgentId(existingEntity.getId(), true, true);
existingEntity.setSummaryMemory("");
} else if (existingEntity.getChatHistoryConf() != null && existingEntity.getChatHistoryConf() == 1) {
// 删除音频数据
agentChatHistoryService.deleteByAgentId(existingEntity.getId(), true, false);
}
agentService.updateById(existingEntity);
return new Result<>();
}
@@ -0,0 +1,19 @@
package xiaozhi.modules.agent.dto;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 智能体记忆更新DTO
*/
@Data
@Schema(description = "智能体记忆更新对象")
public class AgentMemoryDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "总结记忆", example = "构建可生长的动态记忆网络,在有限空间内保留关键信息的同时,智能维护信息演变轨迹\n" +
"根据对话记录,总结user的重要信息,以便在未来的对话中提供更个性化的服务", required = false)
private String summaryMemory;
}
@@ -86,7 +86,7 @@ public class ShiroConfig {
// 将config路径使用server服务过滤器
filterMap.put("/config/**", "server");
filterMap.put("/agent/chat-history/report", "server");
filterMap.put("/agent/device/**", "server");
filterMap.put("/agent/saveMemory/**", "server");
filterMap.put("/agent/play/**", "anon");
filterMap.put("/**", "oauth2");
shiroFilter.setFilterChainDefinitionMap(filterMap);
+65 -59
View File
@@ -15,9 +15,17 @@
<img loading="lazy" src="@/assets/home/setting-user.png" alt="">
</div>
<span class="header-title">{{ form.agentName }}</span>
<button class="custom-close-btn" @click="goToHome">
×
</button>
<div class="header-actions">
<div class="hint-text">
<img loading="lazy" src="@/assets/home/info.png" alt="">
<span>保存配置后需要重启设备新的配置才会生效</span>
</div>
<el-button type="primary" class="save-btn" @click="saveConfig">保存配置</el-button>
<el-button class="reset-btn" @click="resetConfig">重置</el-button>
<button class="custom-close-btn" @click="goToHome">
×
</button>
</div>
</div>
<div class="divider"></div>
@@ -26,7 +34,7 @@
<div class="form-grid">
<div class="form-column">
<el-form-item label="助手昵称:">
<el-input v-model="form.agentName" class="form-input" />
<el-input v-model="form.agentName" class="form-input" maxlength="10" />
</el-form-item>
<el-form-item label="角色模版:">
<div class="template-container">
@@ -40,10 +48,11 @@
<el-input type="textarea" rows="12" resize="none" placeholder="请输入内容" v-model="form.systemPrompt"
maxlength="2000" show-word-limit class="form-textarea" />
</el-form-item>
<el-form-item label="总结记忆:" :style="{ display: form.model.memModelId === 'Memory_mem_local_short' ? 'block' : 'none' }">
<el-input type="textarea" rows="12" resize="none" placeholder="请输入内容" v-model="form.summaryMemory"
maxlength="2000" show-word-limit class="form-textarea" />
<el-form-item label="记忆:">
<el-input type="textarea" rows="3" resize="none" v-model="form.summaryMemory" maxlength="2000"
show-word-limit class="form-textarea"
:disabled="form.model.memModelId !== 'Memory_mem_local_short'" />
</el-form-item>
<el-form-item label="语言编码:" style="display: none;">
<el-input v-model="form.langCode" placeholder="请输入语言编码,如:zh_CN" maxlength="10" show-word-limit
@@ -53,14 +62,6 @@
<el-input v-model="form.language" placeholder="请输入交互语种,如:中文" maxlength="10" show-word-limit
class="form-input" />
</el-form-item>
<div class="action-bar">
<el-button type="primary" class="save-btn" @click="saveConfig">保存配置</el-button>
<el-button class="reset-btn" @click="resetConfig">重置</el-button>
<div class="hint-text">
<img loading="lazy" src="@/assets/home/red-info.png" alt="">
<span>保存配置后需要重启设备新的配置才会生效</span>
</div>
</div>
</div>
<div class="form-column">
<el-form-item v-for="(model, index) in models" :key="`model-${index}`" :label="model.label"
@@ -580,49 +581,6 @@ export default {
background-color: #d0d8ff;
}
.action-bar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 2vh;
align-items: center;
}
.action-bar {
.el-button.save-btn {
background: #5778ff;
color: white;
border: none;
border-radius: 18px;
padding: 10px 20px;
width: 100px;
height: 35px;
font-size: 14px;
}
.el-button.reset-btn {
background: #e6ebff;
color: #5778ff;
border: 1px solid #adbdff;
border-radius: 18px;
padding: 10px 20px;
}
}
.hint-text {
display: flex;
align-items: center;
gap: 8px;
color: #979db1;
font-size: 11px;
margin-left: 16px;
}
.hint-text img {
width: 19px;
height: 19px;
}
.model-select-wrapper {
display: flex;
align-items: center;
@@ -713,4 +671,52 @@ export default {
min-width: 250px;
justify-content: flex-end;
}
.header-actions {
display: flex;
align-items: center;
gap: 8px;
margin-left: auto;
}
.header-actions .hint-text {
display: flex;
align-items: center;
gap: 4px;
color: #979db1;
font-size: 12px;
margin-right: 8px;
}
.header-actions .hint-text img {
width: 16px;
height: 16px;
}
.header-actions .save-btn {
background: #5778ff;
color: white;
border: none;
border-radius: 18px;
padding: 8px 16px;
height: 32px;
font-size: 14px;
}
.header-actions .reset-btn {
background: #e6ebff;
color: #5778ff;
border: 1px solid #adbdff;
border-radius: 18px;
padding: 8px 16px;
height: 32px;
}
.header-actions .custom-close-btn {
position: static;
transform: none;
width: 32px;
height: 32px;
margin-left: 8px;
}
</style>
@@ -144,13 +144,12 @@ def get_agent_models(
},
)
def save_mem_local_short(
mac_address: str, short_momery: str
) -> Optional[Dict]:
def save_mem_local_short(mac_address: str, short_momery: str) -> Optional[Dict]:
try:
return ManageApiClient._instance._execute_request(
"PUT",
f"/agent/device/" + mac_address,
f"/agent/saveMemory/" + mac_address,
json={
"summaryMemory": short_momery,
},