feat: 优化会话消息处理逻辑,支持在话题切换时正确保存消息并更新内存历史
This commit is contained in:
parent
159c1bbb7a
commit
c817b1dde1
@ -110,15 +110,42 @@ impl AgentExecutionService {
|
|||||||
|
|
||||||
// 将结果消息保存到确定的话题
|
// 将结果消息保存到确定的话题
|
||||||
if let Some(topic_id) = target_topic_id {
|
if let Some(topic_id) = target_topic_id {
|
||||||
if let Err(err) = session.append_messages_to_topic(
|
if is_current_turn {
|
||||||
|
// 如果是最新回合,使用 append_persisted_messages 保存到数据库并更新内存历史
|
||||||
|
if let Err(err) = session.append_persisted_messages(
|
||||||
|
request.chat_id,
|
||||||
|
request.result.emitted_messages.clone(),
|
||||||
|
) {
|
||||||
|
tracing::error!(
|
||||||
|
error = %err,
|
||||||
|
chat_id = %request.chat_id,
|
||||||
|
"Failed to append messages to session history"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果用户已切换话题,只保存到原始话题(不更新内存历史)
|
||||||
|
if let Err(err) = session.append_messages_to_topic(
|
||||||
|
request.chat_id,
|
||||||
|
topic_id,
|
||||||
|
&request.result.emitted_messages,
|
||||||
|
) {
|
||||||
|
tracing::error!(
|
||||||
|
error = %err,
|
||||||
|
topic_id = %topic_id,
|
||||||
|
"Failed to append messages to topic"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if is_current_turn {
|
||||||
|
// 如果没有话题,直接更新内存历史(append_persisted_messages 会处理持久化)
|
||||||
|
if let Err(err) = session.append_persisted_messages(
|
||||||
request.chat_id,
|
request.chat_id,
|
||||||
topic_id,
|
request.result.emitted_messages.clone(),
|
||||||
&request.result.emitted_messages,
|
|
||||||
) {
|
) {
|
||||||
tracing::error!(
|
tracing::error!(
|
||||||
error = %err,
|
error = %err,
|
||||||
topic_id = %topic_id,
|
chat_id = %request.chat_id,
|
||||||
"Failed to append messages to topic"
|
"Failed to append messages to session history"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -235,12 +235,13 @@ impl OpenAIProvider {
|
|||||||
let normalized = self.normalize_tool_arguments(arguments);
|
let normalized = self.normalize_tool_arguments(arguments);
|
||||||
|
|
||||||
if self.uses_json_tool_arguments() {
|
if self.uses_json_tool_arguments() {
|
||||||
// Model expects JSON object format
|
// Model expects JSON object format (e.g., some code models)
|
||||||
normalized
|
normalized
|
||||||
} else {
|
} else {
|
||||||
// Standard OpenAI format: arguments as JSON string
|
// Standard OpenAI format: arguments as JSON string
|
||||||
// But ensure we serialize valid JSON, not null
|
// But ensure we serialize valid JSON, not null
|
||||||
match normalized {
|
match normalized {
|
||||||
|
Value::Null => Value::String("{}".to_string()),
|
||||||
Value::String(raw) => {
|
Value::String(raw) => {
|
||||||
// If the string is already valid JSON, keep it as-is
|
// If the string is already valid JSON, keep it as-is
|
||||||
// Otherwise, ensure it's a proper JSON string
|
// Otherwise, ensure it's a proper JSON string
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user