Compare commits

..

No commits in common. "cc3e890ccd9153e3d0028c729d3566c98dc0ed75" and "ea5a9e0656129f98b3392e8296f7497dbf36046f" have entirely different histories.

4 changed files with 14 additions and 33 deletions

View File

@ -210,13 +210,12 @@ 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> {
// 检查历史是否存在且对应正确的话题 // 检查历史是否存在且对应正确的话题
// 先获取 topic 信息并转换为 owned String避免借用冲突 let current_topic = self.history.chat_topic(chat_id);
let current_topic: Option<String> = self.history.chat_topic(chat_id).map(|s| s.to_string());
let stored_topic = self.history.history_topic(chat_id); let stored_topic = self.history.history_topic(chat_id);
if self.chat_history_exists(chat_id) { if self.chat_history_exists(chat_id) {
// 如果历史已存在,但话题不匹配,需要重新加载 // 如果历史已存在,但话题不匹配,需要重新加载
if current_topic.as_deref() != stored_topic { if current_topic != stored_topic {
tracing::info!( tracing::info!(
chat_id = %chat_id, chat_id = %chat_id,
current_topic = ?current_topic, current_topic = ?current_topic,
@ -228,8 +227,8 @@ impl Session {
return Ok(()); return Ok(());
} }
// 历史不存在,按 topic 加载(如果设置了 topic // 历史不存在,正常加载
self.history.ensure_chat_loaded(chat_id, current_topic.as_deref()) self.history.ensure_chat_loaded(chat_id)
} }
fn chat_history_exists(&self, chat_id: &str) -> bool { fn chat_history_exists(&self, chat_id: &str) -> bool {

View File

@ -65,11 +65,7 @@ impl SessionHistory {
.map_err(|err| AgentError::Other(format!("session persistence error: {}", err))) .map_err(|err| AgentError::Other(format!("session persistence error: {}", err)))
} }
pub(crate) fn ensure_chat_loaded( pub(crate) fn ensure_chat_loaded(&mut self, chat_id: &str) -> Result<(), AgentError> {
&mut self,
chat_id: &str,
topic_id: Option<&str>,
) -> Result<(), AgentError> {
// 获取 session 记录(用于检查最后活跃时间) // 获取 session 记录(用于检查最后活跃时间)
let session_record = self.ensure_persistent_session(chat_id)?; let session_record = self.ensure_persistent_session(chat_id)?;
@ -97,16 +93,10 @@ impl SessionHistory {
return Ok(()); return Ok(());
} }
// 如果提供了 topic_id按 topic 加载;否则按 session 加载 let history = self
let history = if let Some(tid) = topic_id { .conversations
self.conversations
.load_messages_for_topic(tid)
.map_err(|err| AgentError::Other(format!("session history load error: {}", err)))?
} else {
self.conversations
.load_messages(&self.persistent_session_id(chat_id)) .load_messages(&self.persistent_session_id(chat_id))
.map_err(|err| AgentError::Other(format!("session history load error: {}", err)))? .map_err(|err| AgentError::Other(format!("session history load error: {}", err)))?;
};
self.chat_histories.insert(chat_id.to_string(), history); self.chat_histories.insert(chat_id.to_string(), history);
Ok(()) Ok(())
} }

View File

@ -22,8 +22,6 @@ pub trait ConversationRepository: Send + Sync + 'static {
fn load_messages(&self, session_id: &str) -> Result<Vec<ChatMessage>, StorageError>; fn load_messages(&self, session_id: &str) -> Result<Vec<ChatMessage>, StorageError>;
fn load_messages_for_topic(&self, topic_id: &str) -> Result<Vec<ChatMessage>, StorageError>;
fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError>; fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError>;
fn append_message_with_topic( fn append_message_with_topic(
@ -164,10 +162,6 @@ impl ConversationRepository for super::SessionStore {
super::SessionStore::load_messages(self, session_id) super::SessionStore::load_messages(self, session_id)
} }
fn load_messages_for_topic(&self, topic_id: &str) -> Result<Vec<ChatMessage>, StorageError> {
super::SessionStore::load_messages_for_topic(self, topic_id)
}
fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError> { fn append_message(&self, session_id: &str, message: &ChatMessage) -> Result<(), StorageError> {
super::SessionStore::append_message(self, session_id, message) super::SessionStore::append_message(self, session_id, message)
} }

View File

@ -602,26 +602,24 @@ fn chunk_text(text: &str, limit: usize) -> Vec<String> {
chunks.push(remaining.to_string()); chunks.push(remaining.to_string());
break; break;
} }
// Find the nearest valid character boundary let window = &remaining[..limit];
let end = remaining.floor_char_boundary(limit);
let window = &remaining[..end];
let cut = window let cut = window
.rfind("\n\n") .rfind("\n\n")
.filter(|&i| i > end * 3 / 10) .filter(|&i| i > limit * 3 / 10)
.map(|i| i + 2) .map(|i| i + 2)
.or_else(|| { .or_else(|| {
window window
.rfind('\n') .rfind('\n')
.filter(|&i| i > end * 3 / 10) .filter(|&i| i > limit * 3 / 10)
.map(|i| i + 1) .map(|i| i + 1)
}) })
.or_else(|| { .or_else(|| {
window window
.rfind(' ') .rfind(' ')
.filter(|&i| i > end * 3 / 10) .filter(|&i| i > limit * 3 / 10)
.map(|i| i + 1) .map(|i| i + 1)
}) })
.unwrap_or(end); .unwrap_or(limit);
chunks.push(remaining[..cut].to_string()); chunks.push(remaining[..cut].to_string());
remaining = &remaining[cut..]; remaining = &remaining[cut..];
} }