Compare commits

...

2 Commits

12 changed files with 161 additions and 139 deletions

View File

@ -1128,6 +1128,7 @@ mod tests {
api_key: "test-key".to_string(),
extra_headers: std::collections::HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: "test-model".to_string(),
temperature: Some(0.0),
max_tokens: Some(32),

View File

@ -248,6 +248,8 @@ pub struct ProviderConfig {
pub extra_headers: HashMap<String, String>,
#[serde(default = "default_llm_timeout_secs")]
pub llm_timeout_secs: u64,
#[serde(default = "default_memory_maintenance_timeout_secs")]
pub memory_maintenance_timeout_secs: u64,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
@ -291,6 +293,10 @@ fn default_llm_timeout_secs() -> u64 {
120
}
fn default_memory_maintenance_timeout_secs() -> u64 {
600
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GatewayConfig {
#[serde(default = "default_gateway_host")]
@ -623,6 +629,7 @@ pub struct LLMProviderConfig {
pub api_key: String,
pub extra_headers: HashMap<String, String>,
pub llm_timeout_secs: u64,
pub memory_maintenance_timeout_secs: u64,
pub model_id: String,
pub temperature: Option<f32>,
pub max_tokens: Option<u32>,
@ -712,6 +719,7 @@ impl Config {
api_key: provider.api_key.clone(),
extra_headers: provider.extra_headers.clone(),
llm_timeout_secs: provider.llm_timeout_secs,
memory_maintenance_timeout_secs: provider.memory_maintenance_timeout_secs,
model_id: model.model_id.clone(),
temperature: model.temperature,
max_tokens: model.max_tokens,

View File

@ -61,7 +61,7 @@ impl SchedulerMaintenanceService {
self.session_manager.cleanup_expired_sessions().await
}
async fn run_memory_maintenance(&self) -> Result<Vec<MemoryMaintenanceScopeResult>, AgentError> {
async fn run_memory_maintenance(&self) -> Result<Option<MemoryMaintenanceScopeResult>, AgentError> {
self.session_manager.run_memory_maintenance_for_all_scopes().await
}
}

View File

@ -50,6 +50,7 @@ mod tests {
api_key: "test-key".to_string(),
extra_headers: HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: "test-model".to_string(),
temperature: Some(0.0),
max_tokens: Some(32),

View File

@ -4,9 +4,9 @@ use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::agent::{AgentError, AgentRuntimeConfig};
use crate::agent::AgentError;
use crate::config::LLMProviderConfig;
use crate::providers::{ChatCompletionRequest, Message, create_provider};
use crate::providers::{ChatCompletionRequest, Message, ProviderRuntimeConfig, create_provider};
use crate::storage::{MemoryRecord, SessionStore};
use super::prompt::upsert_managed_agent_memory_summary;
@ -17,14 +17,6 @@ const MEMORY_MAINTENANCE_STEP2_SYSTEM_PROMPT: &str =
include_str!("memory_maintenance_step2_system_prompt.md");
const MEMORY_MAINTENANCE_RETRY_DELAYS_MS: &[u64] = &[1_000, 3_000];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MemoryMaintenanceCategory {
UserFacts,
Preferences,
BehaviorPatterns,
Other,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct MemoryMaintenanceCandidate {
pub(crate) id: String,
@ -35,10 +27,7 @@ pub(crate) struct MemoryMaintenanceCandidate {
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct MemoryMaintenancePlan {
pub(crate) user_facts: Vec<MemoryMaintenanceCandidate>,
pub(crate) preferences: Vec<MemoryMaintenanceCandidate>,
pub(crate) behavior_patterns: Vec<MemoryMaintenanceCandidate>,
pub(crate) others: Vec<MemoryMaintenanceCandidate>,
pub(crate) candidates: Vec<MemoryMaintenanceCandidate>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@ -64,7 +53,6 @@ pub(crate) struct MemoryOrganizationOutput {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct MemorySummaryInput {
pub(crate) scope_key: String,
pub(crate) organized_memories: Vec<OrganizedMemory>,
}
@ -95,6 +83,26 @@ impl MemoryMaintenanceService {
}
}
/// 创建记忆整理专用的 provider使用 memory_maintenance_timeout_secs 作为超时时间
fn create_maintenance_provider(
&self,
) -> Result<Box<dyn crate::providers::LLMProvider>, crate::providers::ProviderError> {
let config = &self.provider_config;
let runtime_config = ProviderRuntimeConfig {
provider_type: config.provider_type.clone(),
name: config.name.clone(),
base_url: config.base_url.clone(),
api_key: config.api_key.clone(),
extra_headers: config.extra_headers.clone(),
llm_timeout_secs: config.memory_maintenance_timeout_secs,
model_id: config.model_id.clone(),
temperature: config.temperature,
max_tokens: config.max_tokens,
model_extra: config.model_extra.clone(),
};
create_provider(runtime_config)
}
pub(crate) fn build_plan_for_scope(
&self,
scope_key: &str,
@ -127,8 +135,7 @@ impl MemoryMaintenanceService {
scope_key: &str,
plan: &MemoryMaintenancePlan,
) -> Result<MemoryOrganizationOutput, AgentError> {
let runtime_config = AgentRuntimeConfig::from(self.provider_config.clone());
let provider = create_provider(runtime_config.provider).map_err(|err| {
let provider = self.create_maintenance_provider().map_err(|err| {
AgentError::Other(format!("create maintenance provider error: {}", err))
})?;
@ -217,6 +224,7 @@ impl MemoryMaintenanceService {
Ok(output)
}
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) async fn run_for_scope(
&self,
scope_key: &str,
@ -258,13 +266,11 @@ impl MemoryMaintenanceService {
scope_key: &str,
remaining_memories: &[MemoryRecord],
) -> Result<String, AgentError> {
let runtime_config = AgentRuntimeConfig::from(self.provider_config.clone());
let provider = create_provider(runtime_config.provider).map_err(|err| {
let provider = self.create_maintenance_provider().map_err(|err| {
AgentError::Other(format!("create maintenance provider error: {}", err))
})?;
let input = MemorySummaryInput {
scope_key: scope_key.to_string(),
organized_memories: remaining_memories
.iter()
.map(|m| OrganizedMemory {
@ -353,15 +359,20 @@ impl MemoryMaintenanceService {
pub(crate) async fn run_for_all_scopes(
&self,
) -> Result<Vec<MemoryMaintenanceScopeResult>, AgentError> {
) -> Result<Option<MemoryMaintenanceScopeResult>, AgentError> {
let scope_keys = self.store.list_memory_scope_keys("user").map_err(|err| {
AgentError::Other(format!("list memory scope keys error: {}", err))
})?;
let mut results = Vec::new();
for scope_key in scope_keys {
let result = match self.run_for_scope(&scope_key).await {
Ok(Some(result)) => result,
if scope_keys.is_empty() {
return Ok(None);
}
// 步骤1逐个 scope 整理记忆merge/delete但不生成摘要
let mut all_outputs = Vec::new();
for scope_key in &scope_keys {
match self.run_organize_for_scope(scope_key).await {
Ok(Some(output)) => all_outputs.push((scope_key.clone(), output)),
Ok(None) => continue,
Err(error) if is_recoverable_maintenance_scope_error(&error) => {
tracing::warn!(
@ -372,23 +383,78 @@ impl MemoryMaintenanceService {
continue;
}
Err(error) => return Err(error),
};
results.push(result);
}
}
let combined_markdown = combine_managed_memory_markdown(
&results
if all_outputs.is_empty() {
return Ok(None);
}
// 步骤2收集所有 scope 整理后的剩余记忆
let mut all_remaining_memories = Vec::new();
for (scope_key, _) in &all_outputs {
let memories = self
.store
.list_memories_for_scope("user", scope_key)
.map_err(|err| {
AgentError::Other(format!(
"list remaining memories for scope {} error: {}",
scope_key, err
))
})?;
all_remaining_memories.extend(memories);
}
// 步骤3统一生成一个摘要
let managed_markdown = if all_remaining_memories.is_empty() {
String::new()
} else {
self.generate_summary("all", &all_remaining_memories).await?
};
if !managed_markdown.is_empty() {
upsert_managed_agent_memory_summary(&managed_markdown)?;
}
// 合并所有输出用于返回
let combined_output = MemoryOrganizationOutput {
merges: all_outputs
.iter()
.map(|result| result.managed_markdown.clone())
.collect::<Vec<_>>(),
);
.flat_map(|(_, o)| o.merges.clone())
.collect(),
conflicts: all_outputs
.iter()
.flat_map(|(_, o)| o.conflicts.clone())
.collect(),
low_value_ids: all_outputs
.iter()
.flat_map(|(_, o)| o.low_value_ids.clone())
.collect(),
};
if !combined_markdown.is_empty() {
upsert_managed_agent_memory_summary(&combined_markdown)?;
}
Ok(Some(MemoryMaintenanceScopeResult {
scope_key: "all".to_string(),
output: combined_output,
managed_markdown,
}))
}
Ok(results)
/// 仅执行整理步骤organize + apply不生成摘要
async fn run_organize_for_scope(
&self,
scope_key: &str,
) -> Result<Option<MemoryOrganizationOutput>, AgentError> {
let Some(plan) = self.build_plan_for_scope(scope_key)? else {
return Ok(None);
};
// 步骤1整理记忆
let organize_output = self.organize_plan(scope_key, &plan).await?;
// 应用整理结果merge和delete
apply_memory_maintenance_output(self.store.as_ref(), scope_key, &plan, &organize_output)?;
Ok(Some(organize_output))
}
}
@ -419,28 +485,12 @@ pub(crate) fn build_memory_maintenance_plan(memories: &[MemoryRecord]) -> Memory
content: normalized_content.to_string(),
};
match memory_maintenance_category(&memory.namespace) {
MemoryMaintenanceCategory::UserFacts => plan.user_facts.push(candidate),
MemoryMaintenanceCategory::Preferences => plan.preferences.push(candidate),
MemoryMaintenanceCategory::BehaviorPatterns => plan.behavior_patterns.push(candidate),
MemoryMaintenanceCategory::Other => plan.others.push(candidate),
}
plan.candidates.push(candidate);
}
plan
}
fn memory_maintenance_category(namespace: &str) -> MemoryMaintenanceCategory {
match namespace.trim().to_ascii_lowercase().as_str() {
"profile" | "facts" | "identity" => MemoryMaintenanceCategory::UserFacts,
"preferences" | "style" | "likes" => MemoryMaintenanceCategory::Preferences,
"patterns" | "behavior" | "habits" | "workflow" => {
MemoryMaintenanceCategory::BehaviorPatterns
}
_ => MemoryMaintenanceCategory::Other,
}
}
pub(crate) fn is_recoverable_maintenance_llm_error(error: &str) -> bool {
let normalized = error.to_ascii_lowercase();
normalized.contains("error sending request for url")
@ -512,61 +562,13 @@ pub(crate) fn extract_json_object(content: &str) -> Option<&str> {
None
}
pub(crate) fn combine_managed_memory_markdown(chunks: &[String]) -> String {
let normalized_chunks = chunks
.iter()
.map(|chunk| chunk.trim())
.filter(|chunk| !chunk.is_empty())
.collect::<Vec<_>>();
let mut combined = Vec::new();
for (index, chunk) in normalized_chunks.iter().enumerate() {
let chunk_lines = chunk
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.collect::<HashSet<_>>();
let is_subset_of_other =
normalized_chunks
.iter()
.enumerate()
.any(|(other_index, other)| {
if index == other_index {
return false;
}
let other_lines = other
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.collect::<HashSet<_>>();
chunk_lines.len() < other_lines.len() && chunk_lines.is_subset(&other_lines)
});
if !is_subset_of_other && !combined.iter().any(|existing: &String| existing == chunk) {
combined.push((*chunk).to_string());
}
}
combined.join("\n\n")
}
pub(crate) fn apply_memory_maintenance_output(
store: &SessionStore,
scope_key: &str,
plan: &MemoryMaintenancePlan,
output: &MemoryOrganizationOutput,
) -> Result<(), AgentError> {
let all_candidates = plan
.user_facts
.iter()
.chain(plan.preferences.iter())
.chain(plan.behavior_patterns.iter())
.chain(plan.others.iter())
.cloned()
.collect::<Vec<_>>();
let all_candidates = plan.candidates.clone();
let candidates_by_id = all_candidates
.iter()
@ -651,3 +653,6 @@ fn preview_text(content: &str, max_chars: usize) -> String {
}
preview.replace('\n', "\\n")
}
#[cfg(test)]
mod tests {}

View File

@ -32,7 +32,7 @@ impl MemoryMaintenanceCoordinator {
pub(crate) async fn run_for_all_scopes(
&self,
) -> Result<Vec<MemoryMaintenanceScopeResult>, AgentError> {
) -> Result<Option<MemoryMaintenanceScopeResult>, AgentError> {
self.service()?.run_for_all_scopes().await
}

View File

@ -19,14 +19,22 @@
- merges对象数组。每个对象必须包含 source_ids、namespace、memory_key、content。
- source_ids: 字符串数组要合并的源记忆ID列表
- namespace: 目标命名空间
- memory_key: 目标记忆键
- namespace: 目标命名空间(可以自由决定,不限于固定分类)
- memory_key: 目标记忆键(可以自由决定)
- content: 合并后的内容
- conflicts对象数组。每个对象必须包含 source_ids、note。
- source_ids: 冲突的记忆ID列表
- note: 冲突说明
- low_value_ids需要删除的低价值候选记忆 ID 数组
组织原则(由你自主决定):
- 根据记忆的语义内容自然分组,不必拘泥于预定义分类
- 相似的、互补的记忆可以合并
- 过期、重复、过细的记忆可以标记为低价值
- namespace 和 memory_key 的命名应当简洁、有意义
- 可以自由创建新的 namespace 来组织相关记忆
额外约束:
- 只能引用输入里出现过的候选 id。

View File

@ -3,7 +3,6 @@
你的任务是基于整理后的用户记忆生成结构化的 Markdown 摘要。
输入格式:
- scope_key: 用户的唯一标识
- organized_memories: 整理后的记忆列表,每个包含 namespace、memory_key、content
输出要求:

View File

@ -51,6 +51,7 @@ mod tests {
api_key: "test-key".to_string(),
extra_headers: HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: model_id.to_string(),
temperature: Some(0.0),
max_tokens: Some(32),

View File

@ -20,7 +20,7 @@ use super::execution::should_display_message_to_user;
#[cfg(test)]
use super::memory_maintenance::{
MemoryMaintenanceMerge, apply_memory_maintenance_output, build_memory_maintenance_plan,
combine_managed_memory_markdown, extract_json_object, is_recoverable_maintenance_llm_error,
extract_json_object, is_recoverable_maintenance_llm_error,
strip_json_code_fence,
};
use super::memory_maintenance::{MemoryMaintenanceScopeResult, MemoryOrganizationOutput};
@ -427,7 +427,7 @@ impl SessionManager {
pub(crate) async fn run_memory_maintenance_for_all_scopes(
&self,
) -> Result<Vec<MemoryMaintenanceScopeResult>, AgentError> {
) -> Result<Option<MemoryMaintenanceScopeResult>, AgentError> {
self.memory_maintenance.run_for_all_scopes().await
}
@ -520,6 +520,7 @@ mod tests {
api_key: "test-key".to_string(),
extra_headers: HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: "test-model".to_string(),
temperature: Some(0.0),
max_tokens: Some(32),
@ -786,6 +787,7 @@ mod tests {
model_extra: HashMap::new(),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -827,6 +829,7 @@ mod tests {
model_extra: HashMap::new(),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -900,6 +903,7 @@ mod tests {
model_extra: HashMap::new(),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -982,6 +986,7 @@ mod tests {
)]),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1065,6 +1070,7 @@ mod tests {
model_extra: HashMap::new(),
max_tool_iterations: 1,
llm_timeout_secs: 1,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1113,7 +1119,7 @@ mod tests {
assert!(error.contains("provider=maintenance-provider"));
assert!(error.contains("model=maintenance-model"));
assert!(error.contains("url=https://example.invalid/v1/chat/completions"));
assert!(error.contains("timeout_secs=1"));
assert!(error.contains("timeout_secs=600"));
assert!(error.contains("error sending request for url"));
}
@ -1147,6 +1153,7 @@ mod tests {
)]),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1211,6 +1218,7 @@ mod tests {
)]),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1284,6 +1292,7 @@ mod tests {
)]),
max_tool_iterations: 1,
llm_timeout_secs: 30,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1317,15 +1326,16 @@ mod tests {
})
.unwrap();
let results = session_manager
let result = session_manager
.run_memory_maintenance_for_all_scopes()
.await
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].scope_key, "feishu:user-1");
assert!(result.is_some());
let result = result.unwrap();
assert_eq!(result.scope_key, "all");
// 由于步骤2需要新的提示词和输入格式这里只验证基本功能
assert!(!results[0].managed_markdown.is_empty());
assert!(!result.managed_markdown.is_empty());
}
#[tokio::test]
@ -1343,6 +1353,7 @@ mod tests {
model_extra: HashMap::new(),
max_tool_iterations: 1,
llm_timeout_secs: 1,
memory_maintenance_timeout_secs: 600,
tool_result_max_chars: 20_000,
context_tool_result_trim_chars: 20_000,
};
@ -1378,12 +1389,13 @@ mod tests {
.unwrap();
}
let results = session_manager
let result = session_manager
.run_memory_maintenance_for_all_scopes()
.await
.unwrap();
assert!(results.is_empty());
// 当遇到可恢复错误时,没有整理任何记忆,返回 None
assert!(result.is_none());
}
#[test]
@ -1460,21 +1472,6 @@ mod tests {
assert_eq!(all_memories[0].content, "用户主要在做AI产品设计与实现");
}
#[test]
fn test_combine_managed_memory_markdown_prefers_richer_summary_over_subset() {
let combined = combine_managed_memory_markdown(&[
"### 用户事实\n- 用户在做AI产品\n\n### 用户偏好\n- 偏好简洁表达".to_string(),
"- 用户在做AI产品".to_string(),
"### 用户事实\n- 用户名为区德成昵称DC。".to_string(),
]);
assert!(
combined.contains("### 用户事实\n- 用户在做AI产品\n\n### 用户偏好\n- 偏好简洁表达")
);
assert!(combined.contains("### 用户事实\n- 用户名为区德成昵称DC。"));
assert_eq!(combined.matches("- 用户在做AI产品").count(), 1);
}
#[test]
fn test_should_display_message_to_user_hides_completed_tool_results_by_default() {
let completed = ChatMessage::tool("call-1", "calculator", "2");
@ -1762,12 +1759,12 @@ mod tests {
];
let plan = build_memory_maintenance_plan(&memories);
assert_eq!(plan.user_facts.len(), 1);
assert_eq!(plan.preferences.len(), 1);
assert_eq!(plan.behavior_patterns.len(), 1);
assert!(plan.others.is_empty());
assert_eq!(plan.user_facts[0].content, "用户在做AI产品");
assert_eq!(plan.preferences[0].content, "偏好简洁表达");
assert_eq!(plan.behavior_patterns[0].content, "习惯先问方案再要代码");
// 去重后应该有3条第1、2条重复
assert_eq!(plan.candidates.len(), 3);
// 验证内容包含所有唯一的记忆
let contents: Vec<String> = plan.candidates.iter().map(|c| c.content.clone()).collect();
assert!(contents.contains(&"用户在做AI产品".to_string()));
assert!(contents.contains(&"偏好简洁表达".to_string()));
assert!(contents.contains(&"习惯先问方案再要代码".to_string()));
}
}

View File

@ -35,6 +35,7 @@ fn load_config() -> Option<LLMProviderConfig> {
api_key: openai_api_key,
extra_headers: HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: openai_model,
temperature: Some(0.0),
max_tokens: Some(100),

View File

@ -37,6 +37,7 @@ fn load_openai_config() -> Option<LLMProviderConfig> {
api_key: openai_api_key,
extra_headers: HashMap::new(),
llm_timeout_secs: 120,
memory_maintenance_timeout_secs: 600,
model_id: openai_model,
temperature: Some(0.0),
max_tokens: Some(100),