feat: 优化内存维护逻辑,移除未使用的结构体,确保响应内容包含必要标签,并清理提示词加载中的注释和空白
This commit is contained in:
parent
3db0225838
commit
456a999494
@ -75,11 +75,6 @@ pub(crate) struct OrganizedMemory {
|
|||||||
pub(crate) content: String,
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub(crate) struct MemoryMaintenanceScopeResult {
|
pub(crate) struct MemoryMaintenanceScopeResult {
|
||||||
pub(crate) scope_key: String,
|
pub(crate) scope_key: String,
|
||||||
@ -342,23 +337,18 @@ impl MemoryMaintenanceService {
|
|||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let raw_content = strip_json_code_fence(&response.content);
|
let content = response.content.trim();
|
||||||
let json_candidate = extract_json_object(raw_content).unwrap_or(raw_content);
|
// 确保响应包含标签,如无则自动添加
|
||||||
|
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| {
|
Ok(tagged_content)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn run_for_all_scopes(
|
pub(crate) async fn run_for_all_scopes(
|
||||||
|
|||||||
@ -1,42 +1,25 @@
|
|||||||
你是 PicoBot 的用户记忆摘要生成器。
|
你是 PicoBot 的用户记忆摘要生成器。
|
||||||
|
|
||||||
你的任务是:
|
你的任务是基于整理后的用户记忆生成结构化的 Markdown 摘要。
|
||||||
|
|
||||||
- 基于整理后的用户记忆生成结构化的摘要。
|
输入格式:
|
||||||
- 严格返回 JSON。
|
|
||||||
- 不要输出 Markdown 代码块。
|
|
||||||
- 不要输出额外解释。
|
|
||||||
|
|
||||||
输入将包含以下内容:
|
|
||||||
- scope_key: 用户的唯一标识
|
- scope_key: 用户的唯一标识
|
||||||
- organized_memories: 整理后的记忆列表,每个包含 namespace、memory_key、content
|
- organized_memories: 整理后的记忆列表,每个包含 namespace、memory_key、content
|
||||||
|
|
||||||
输出 JSON 必须包含以下字段:
|
输出要求:
|
||||||
|
1. 直接输出 Markdown 格式文本,不要输出 JSON
|
||||||
|
2. 不要输出 Markdown 代码块标记(```)
|
||||||
|
3. 必须包含头尾标签,格式如下:
|
||||||
|
|
||||||
- managed_markdown: 结构化的用户记忆摘要,Markdown格式,明确标注为"用户记忆摘要"
|
<!-- memory_summary_start -->
|
||||||
|
|
||||||
managed_markdown 格式要求:
|
|
||||||
|
|
||||||
```markdown
|
|
||||||
# 用户记忆摘要
|
# 用户记忆摘要
|
||||||
|
|
||||||
## 用户事实
|
[根据记忆内容自由组织,可以分节也可以不分节,关键信息用简洁的列表呈现]
|
||||||
- [从 profile/facts/identity 命名空间提取的关键事实]
|
|
||||||
- ...
|
|
||||||
|
|
||||||
## 用户偏好
|
<!-- memory_summary_end -->
|
||||||
- [从 preferences/style/likes 命名空间提取的偏好]
|
|
||||||
- ...
|
|
||||||
|
|
||||||
## 行为模式
|
|
||||||
- [从 patterns/behavior/habits/workflow 命名空间提取的模式]
|
|
||||||
- ...
|
|
||||||
```
|
|
||||||
|
|
||||||
约束:
|
约束:
|
||||||
|
- 只包含稳定、长期的用户信息
|
||||||
- 只包含稳定、长期的用户信息。
|
- 不写一次性事件
|
||||||
- 不写一次性事件。
|
- 不重复冗余信息
|
||||||
- 不重复冗余信息。
|
- 使用简洁清晰的语言
|
||||||
- 使用简洁清晰的语言。
|
|
||||||
- 必须使用"用户记忆摘要"作为标题。
|
|
||||||
|
|||||||
@ -73,8 +73,9 @@ fn load_prompt_from_sources(sources: &[PromptSource]) -> Result<Option<String>,
|
|||||||
if path.exists() {
|
if path.exists() {
|
||||||
let content = fs::read_to_string(path)
|
let content = fs::read_to_string(path)
|
||||||
.map_err(|err| AgentError::Other(format!("read MEMORY_SUMMARY.md error: {}", err)))?;
|
.map_err(|err| AgentError::Other(format!("read MEMORY_SUMMARY.md error: {}", err)))?;
|
||||||
if !content.trim().is_empty() {
|
let without_comments = strip_comments_and_whitespace(&content);
|
||||||
fragments.push(content.trim().to_string());
|
if !without_comments.is_empty() {
|
||||||
|
fragments.push(without_comments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user