feat: 优化内存维护逻辑,移除未使用的结构体,确保响应内容包含必要标签,并清理提示词加载中的注释和空白

This commit is contained in:
ooodc 2026-05-11 22:43:22 +08:00
parent 3db0225838
commit 456a999494
3 changed files with 27 additions and 53 deletions

View File

@ -75,11 +75,6 @@ pub(crate) struct OrganizedMemory {
pub(crate) content: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct MemorySummaryModelOutput {
pub(crate) managed_markdown: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct MemoryMaintenanceScopeResult {
pub(crate) scope_key: String,
@ -342,23 +337,18 @@ impl MemoryMaintenanceService {
))
})?;
let raw_content = strip_json_code_fence(&response.content);
let json_candidate = extract_json_object(raw_content).unwrap_or(raw_content);
let content = response.content.trim();
// 确保响应包含标签,如无则自动添加
let tagged_content = if content.contains("<!-- memory_summary_start -->") {
content.to_string()
} else {
format!(
"<!-- memory_summary_start -->\n{}\n<!-- memory_summary_end -->",
content
)
};
let output: MemorySummaryModelOutput = serde_json::from_str(json_candidate).map_err(|err| {
tracing::error!(
scope_key = %scope_key,
error = %err,
raw_len = raw_content.len(),
raw_preview = %preview_text(raw_content, 400),
json_candidate_len = json_candidate.len(),
json_candidate_preview = %preview_text(json_candidate, 400),
"Memory summary JSON decode failed"
);
AgentError::Other(format!("memory summary JSON decode error: {}", err))
})?;
Ok(output.managed_markdown)
Ok(tagged_content)
}
pub(crate) async fn run_for_all_scopes(

View File

@ -1,42 +1,25 @@
你是 PicoBot 的用户记忆摘要生成器。
你的任务是
你的任务是基于整理后的用户记忆生成结构化的 Markdown 摘要。
- 基于整理后的用户记忆生成结构化的摘要。
- 严格返回 JSON。
- 不要输出 Markdown 代码块。
- 不要输出额外解释。
输入将包含以下内容:
输入格式:
- scope_key: 用户的唯一标识
- organized_memories: 整理后的记忆列表,每个包含 namespace、memory_key、content
输出 JSON 必须包含以下字段:
输出要求:
1. 直接输出 Markdown 格式文本,不要输出 JSON
2. 不要输出 Markdown 代码块标记(```
3. 必须包含头尾标签,格式如下:
- managed_markdown: 结构化的用户记忆摘要Markdown格式明确标注为"用户记忆摘要"
managed_markdown 格式要求:
```markdown
<!-- memory_summary_start -->
# 用户记忆摘要
## 用户事实
- [从 profile/facts/identity 命名空间提取的关键事实]
- ...
[根据记忆内容自由组织,可以分节也可以不分节,关键信息用简洁的列表呈现]
## 用户偏好
- [从 preferences/style/likes 命名空间提取的偏好]
- ...
## 行为模式
- [从 patterns/behavior/habits/workflow 命名空间提取的模式]
- ...
```
<!-- memory_summary_end -->
约束:
- 只包含稳定、长期的用户信息。
- 不写一次性事件。
- 不重复冗余信息。
- 使用简洁清晰的语言。
- 必须使用"用户记忆摘要"作为标题。
- 只包含稳定、长期的用户信息
- 不写一次性事件
- 不重复冗余信息
- 使用简洁清晰的语言

View File

@ -73,8 +73,9 @@ fn load_prompt_from_sources(sources: &[PromptSource]) -> Result<Option<String>,
if path.exists() {
let content = fs::read_to_string(path)
.map_err(|err| AgentError::Other(format!("read MEMORY_SUMMARY.md error: {}", err)))?;
if !content.trim().is_empty() {
fragments.push(content.trim().to_string());
let without_comments = strip_comments_and_whitespace(&content);
if !without_comments.is_empty() {
fragments.push(without_comments);
}
}
}