feat: 添加历史话题管理功能,支持获取和记录每个 chat 的历史话题
This commit is contained in:
parent
34938f57b8
commit
a2d4ed9193
@ -174,6 +174,11 @@ impl Session {
|
|||||||
self.history.chat_topic(chat_id)
|
self.history.chat_topic(chat_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 获取历史所对应的话题 ID(指定 chat)
|
||||||
|
pub fn history_topic(&self, chat_id: &str) -> Option<&str> {
|
||||||
|
self.history.history_topic(chat_id)
|
||||||
|
}
|
||||||
|
|
||||||
/// 切换话题 - 清除当前历史并加载新话题的历史
|
/// 切换话题 - 清除当前历史并加载新话题的历史
|
||||||
pub fn switch_topic(&mut self, chat_id: &str, topic_id: &str) -> Result<(), AgentError> {
|
pub fn switch_topic(&mut self, chat_id: &str, topic_id: &str) -> Result<(), AgentError> {
|
||||||
// 清除当前历史
|
// 清除当前历史
|
||||||
@ -201,9 +206,32 @@ impl Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> {
|
pub fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> {
|
||||||
|
// 检查历史是否存在且对应正确的话题
|
||||||
|
let current_topic = self.history.chat_topic(chat_id);
|
||||||
|
let stored_topic = self.history.history_topic(chat_id);
|
||||||
|
|
||||||
|
if self.chat_history_exists(chat_id) {
|
||||||
|
// 如果历史已存在,但话题不匹配,需要重新加载
|
||||||
|
if current_topic != stored_topic {
|
||||||
|
tracing::info!(
|
||||||
|
chat_id = %chat_id,
|
||||||
|
current_topic = ?current_topic,
|
||||||
|
stored_topic = ?stored_topic,
|
||||||
|
"Topic changed, reloading history"
|
||||||
|
);
|
||||||
|
self.reload_chat_history(chat_id)?;
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 历史不存在,正常加载
|
||||||
self.history.ensure_chat_loaded(chat_id)
|
self.history.ensure_chat_loaded(chat_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn chat_history_exists(&self, chat_id: &str) -> bool {
|
||||||
|
self.history.get_history(chat_id).is_some()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ensure_agent_prompt_before_user_message(
|
pub fn ensure_agent_prompt_before_user_message(
|
||||||
&mut self,
|
&mut self,
|
||||||
chat_id: &str,
|
chat_id: &str,
|
||||||
|
|||||||
@ -26,6 +26,7 @@ pub(crate) struct SessionHistory {
|
|||||||
channel_name: String,
|
channel_name: String,
|
||||||
chat_histories: HashMap<String, Vec<ChatMessage>>,
|
chat_histories: HashMap<String, Vec<ChatMessage>>,
|
||||||
chat_topic_ids: HashMap<String, String>, // 每个 chat 的当前 topic
|
chat_topic_ids: HashMap<String, String>, // 每个 chat 的当前 topic
|
||||||
|
history_topic_ids: HashMap<String, String>, // 每个 chat 的历史所对应的话题
|
||||||
compression_in_flight: HashSet<String>,
|
compression_in_flight: HashSet<String>,
|
||||||
conversations: Arc<dyn ConversationRepository>,
|
conversations: Arc<dyn ConversationRepository>,
|
||||||
skill_events: Arc<dyn SkillEventRepository>,
|
skill_events: Arc<dyn SkillEventRepository>,
|
||||||
@ -43,6 +44,7 @@ impl SessionHistory {
|
|||||||
channel_name: channel_name.into(),
|
channel_name: channel_name.into(),
|
||||||
chat_histories: HashMap::new(),
|
chat_histories: HashMap::new(),
|
||||||
chat_topic_ids: HashMap::new(),
|
chat_topic_ids: HashMap::new(),
|
||||||
|
history_topic_ids: HashMap::new(),
|
||||||
compression_in_flight: HashSet::new(),
|
compression_in_flight: HashSet::new(),
|
||||||
conversations,
|
conversations,
|
||||||
skill_events,
|
skill_events,
|
||||||
@ -117,6 +119,15 @@ impl SessionHistory {
|
|||||||
|
|
||||||
pub(crate) fn set_history(&mut self, chat_id: &str, history: Vec<ChatMessage>) {
|
pub(crate) fn set_history(&mut self, chat_id: &str, history: Vec<ChatMessage>) {
|
||||||
self.chat_histories.insert(chat_id.to_string(), history);
|
self.chat_histories.insert(chat_id.to_string(), history);
|
||||||
|
// 记录历史对应的话题(当前设置的话题)
|
||||||
|
if let Some(topic_id) = self.chat_topic_ids.get(chat_id) {
|
||||||
|
self.history_topic_ids.insert(chat_id.to_string(), topic_id.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 获取指定 chat 的历史所对应的话题
|
||||||
|
pub(crate) fn history_topic(&self, chat_id: &str) -> Option<&str> {
|
||||||
|
self.history_topic_ids.get(chat_id).map(|s| s.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 设置指定 chat 的当前 topic
|
/// 设置指定 chat 的当前 topic
|
||||||
@ -141,6 +152,7 @@ impl SessionHistory {
|
|||||||
pub(crate) fn remove_history(&mut self, chat_id: &str) {
|
pub(crate) fn remove_history(&mut self, chat_id: &str) {
|
||||||
self.chat_histories.remove(chat_id);
|
self.chat_histories.remove(chat_id);
|
||||||
self.compression_in_flight.remove(chat_id);
|
self.compression_in_flight.remove(chat_id);
|
||||||
|
self.history_topic_ids.remove(chat_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn clear_chat_history(&mut self, chat_id: &str) -> Result<(), AgentError> {
|
pub(crate) fn clear_chat_history(&mut self, chat_id: &str) -> Result<(), AgentError> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user