feat: 添加 fresh_session 选项以支持清理历史记录

This commit is contained in:
ooodc 2026-06-02 23:31:05 +08:00
parent cb58d9f3f0
commit a2c7981d6d
3 changed files with 32 additions and 2 deletions

View File

@ -76,6 +76,7 @@ pub(crate) struct ScheduledExecutionRequest<'a> {
pub(crate) provider_config: LLMProviderConfig,
pub(crate) system_prompt: Option<&'a str>,
pub(crate) metadata: &'a HashMap<String, String>,
pub(crate) fresh_session: bool,
}
impl AgentExecutionService {
@ -284,6 +285,15 @@ impl AgentExecutionService {
session_guard.ensure_persistent_session(request.chat_id)?;
// 如果 fresh_session 为 true清理历史内存 + 数据库)
if request.fresh_session {
session_guard.clear_chat_history(request.chat_id)?;
tracing::info!(
chat_id = %request.chat_id,
"Fresh session enabled, history cleared"
);
}
session_guard.ensure_chat_loaded(request.chat_id)?;
session_guard.ensure_agent_prompt_before_user_message(request.chat_id)?;
@ -351,7 +361,7 @@ impl AgentExecutionService {
let result = agent.process(history, Some(&system_prompt_context)).await?;
self.finalize_result_and_schedule_compaction(
let outbound_messages = self.finalize_result_and_schedule_compaction(
request.session.clone(),
FinalizeAgentResultRequest {
channel_name: request.channel_name,
@ -364,7 +374,19 @@ impl AgentExecutionService {
original_topic_id,
},
)
.await
.await?;
// 清理内存历史,释放内存(数据库历史保留)
{
let mut session_guard = request.session.lock().await;
session_guard.remove_history(request.chat_id);
tracing::info!(
chat_id = %request.chat_id,
"Scheduled task completed, memory history released"
);
}
Ok(outbound_messages)
}
pub(crate) async fn finalize_result_and_schedule_compaction(

View File

@ -52,6 +52,7 @@ impl ScheduledAgentTaskService {
provider_config,
system_prompt: options.system_prompt.as_deref(),
metadata: &options.metadata,
fresh_session: options.fresh_session,
})
.await
}

View File

@ -23,6 +23,7 @@ pub struct ScheduledAgentTaskOptions {
pub system_prompt: Option<String>,
pub metadata: HashMap<String, String>,
pub agent: Option<String>,
pub fresh_session: bool,
}
#[derive(Debug, Clone)]
@ -1029,12 +1030,18 @@ fn parse_scheduled_agent_task_options(
.and_then(|value| value.as_str())
.map(ToString::to_string);
let metadata = parse_metadata_map(job.payload.get("metadata"))?;
let fresh_session = job
.payload
.get("fresh_session")
.and_then(|value| value.as_bool())
.unwrap_or(true);
Ok(ScheduledAgentTaskOptions {
sender_id,
system_prompt,
metadata,
agent,
fresh_session,
})
}