fix: 语音消息暂存本地 修复显示异常

This commit is contained in:
yangshilin
2026-03-13 17:11:59 +08:00
parent c0906723ac
commit a2b2b6eb76
13 changed files with 232 additions and 21 deletions

View File

@@ -44,9 +44,17 @@ class ConversationAgent:
messages.append(AIMessage(content=msg["content"]))
return messages
async def _save_message(self, conversation_id: str, role: str, content: str):
async def _save_message(
self,
conversation_id: str,
role: str,
content: str,
message_type: str = "text",
):
"""保存消息到 Redis"""
await redis_service.add_message(conversation_id, role, content)
await redis_service.add_message(
conversation_id, role, content, message_type=message_type
)
def _format_history_string(self, messages: List[Any]) -> str:
"""将消息列表格式化为字符串(用于 prompt"""
@@ -236,6 +244,7 @@ class ConversationAgent:
missing_fields: List[str],
filled_fields: Dict[str, str],
nickname: str = "",
is_from_voice: bool = False,
) -> List[str]:
"""在资料收集过程中生成跟进回复"""
if not self.llm:
@@ -250,7 +259,8 @@ class ConversationAgent:
response = await self.llm.ainvoke(full_prompt)
response_text = response.content if hasattr(response, 'content') else str(response)
await self._save_message(conversation_id, "human", user_message)
human_msg_type = "audio" if is_from_voice else "text"
await self._save_message(conversation_id, "human", user_message, message_type=human_msg_type)
await self._save_message(conversation_id, "ai", response_text)
messages = [msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()]
@@ -285,6 +295,7 @@ class ConversationAgent:
user_message: str,
memoir_state: MemoirStateSchema,
user_profile_context: str = "",
is_from_voice: bool = False,
) -> List[str]:
"""
基于共享状态异步生成引导式回复
@@ -294,6 +305,7 @@ class ConversationAgent:
user_message: 用户消息
memoir_state: 共享状态
user_profile_context: 用户基础资料上下文
is_from_voice: 用户消息是否来自语音转写(用于保存正确的 messageType
Returns:
Agent 回应文本列表(支持多条消息)
@@ -333,13 +345,14 @@ class ConversationAgent:
response = await self.llm.ainvoke(full_prompt)
response_text = response.content if hasattr(response, 'content') else str(response)
await self._save_message(conversation_id, "human", user_message)
human_msg_type = "audio" if is_from_voice else "text"
await self._save_message(conversation_id, "human", user_message, message_type=human_msg_type)
await self._save_message(conversation_id, "ai", response_text)
messages = [msg.strip() for msg in response_text.split("[SPLIT]") if msg.strip()]
return messages[:3] if messages else [response_text]
except Exception as e:
logger.error(f"生成回应失败: {e}")
return [f"抱歉,生成回应时出现错误: {str(e)}"]