feat: 添加 fresh_session 选项以支持清理历史记录
This commit is contained in:
parent
cb58d9f3f0
commit
a2c7981d6d
@ -76,6 +76,7 @@ pub(crate) struct ScheduledExecutionRequest<'a> {
|
|||||||
pub(crate) provider_config: LLMProviderConfig,
|
pub(crate) provider_config: LLMProviderConfig,
|
||||||
pub(crate) system_prompt: Option<&'a str>,
|
pub(crate) system_prompt: Option<&'a str>,
|
||||||
pub(crate) metadata: &'a HashMap<String, String>,
|
pub(crate) metadata: &'a HashMap<String, String>,
|
||||||
|
pub(crate) fresh_session: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AgentExecutionService {
|
impl AgentExecutionService {
|
||||||
@ -284,6 +285,15 @@ impl AgentExecutionService {
|
|||||||
|
|
||||||
session_guard.ensure_persistent_session(request.chat_id)?;
|
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_chat_loaded(request.chat_id)?;
|
||||||
session_guard.ensure_agent_prompt_before_user_message(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?;
|
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(),
|
request.session.clone(),
|
||||||
FinalizeAgentResultRequest {
|
FinalizeAgentResultRequest {
|
||||||
channel_name: request.channel_name,
|
channel_name: request.channel_name,
|
||||||
@ -364,7 +374,19 @@ impl AgentExecutionService {
|
|||||||
original_topic_id,
|
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(
|
pub(crate) async fn finalize_result_and_schedule_compaction(
|
||||||
|
|||||||
@ -52,6 +52,7 @@ impl ScheduledAgentTaskService {
|
|||||||
provider_config,
|
provider_config,
|
||||||
system_prompt: options.system_prompt.as_deref(),
|
system_prompt: options.system_prompt.as_deref(),
|
||||||
metadata: &options.metadata,
|
metadata: &options.metadata,
|
||||||
|
fresh_session: options.fresh_session,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,7 @@ pub struct ScheduledAgentTaskOptions {
|
|||||||
pub system_prompt: Option<String>,
|
pub system_prompt: Option<String>,
|
||||||
pub metadata: HashMap<String, String>,
|
pub metadata: HashMap<String, String>,
|
||||||
pub agent: Option<String>,
|
pub agent: Option<String>,
|
||||||
|
pub fresh_session: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -1029,12 +1030,18 @@ fn parse_scheduled_agent_task_options(
|
|||||||
.and_then(|value| value.as_str())
|
.and_then(|value| value.as_str())
|
||||||
.map(ToString::to_string);
|
.map(ToString::to_string);
|
||||||
let metadata = parse_metadata_map(job.payload.get("metadata"))?;
|
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 {
|
Ok(ScheduledAgentTaskOptions {
|
||||||
sender_id,
|
sender_id,
|
||||||
system_prompt,
|
system_prompt,
|
||||||
metadata,
|
metadata,
|
||||||
agent,
|
agent,
|
||||||
|
fresh_session,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user