fix: 语音消息暂存本地 修复显示异常
This commit is contained in:
@@ -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)}"]
|
||||
|
||||
@@ -195,7 +195,7 @@ async def get_messages(
|
||||
"content": msg.get("content", ""),
|
||||
"senderType": "user" if msg.get("role") == "human" else "assistant",
|
||||
"timestamp": int(datetime.now(timezone.utc).timestamp() * 1000), # Redis中没有时间戳,使用当前时间
|
||||
"messageType": "text"
|
||||
"messageType": msg.get("messageType", "text"), # 保留语音消息类型,使重新进入时仍显示为语音条
|
||||
})
|
||||
return messages
|
||||
except Exception as e:
|
||||
|
||||
@@ -928,12 +928,14 @@ async def process_user_message(
|
||||
|
||||
remaining = _get_missing_profile_fields(user)
|
||||
filled = _get_filled_profile_fields(user)
|
||||
is_from_voice = bool(segment.audio_url)
|
||||
responses = await agent.generate_profile_followup(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
missing_fields=remaining,
|
||||
filled_fields=filled,
|
||||
nickname=user.nickname or "",
|
||||
is_from_voice=is_from_voice,
|
||||
)
|
||||
|
||||
segment.agent_response = "\n\n".join(responses)
|
||||
@@ -978,11 +980,13 @@ async def process_user_message(
|
||||
)
|
||||
|
||||
try:
|
||||
is_from_voice = bool(segment.audio_url)
|
||||
responses = await agent.generate_response_with_state(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
memoir_state=state,
|
||||
user_profile_context=user_profile_context,
|
||||
is_from_voice=is_from_voice,
|
||||
)
|
||||
|
||||
segment.agent_response = "\n\n".join(responses)
|
||||
|
||||
@@ -72,31 +72,33 @@ class RedisService:
|
||||
return []
|
||||
|
||||
async def add_message(
|
||||
self,
|
||||
conversation_id: str,
|
||||
role: str,
|
||||
content: str
|
||||
self,
|
||||
conversation_id: str,
|
||||
role: str,
|
||||
content: str,
|
||||
message_type: str = "text",
|
||||
) -> bool:
|
||||
"""
|
||||
添加消息到对话历史
|
||||
|
||||
|
||||
Args:
|
||||
conversation_id: 对话 ID
|
||||
role: 角色 ("human" 或 "ai")
|
||||
content: 消息内容
|
||||
|
||||
message_type: 消息类型 ("text" 或 "audio"),用于区分文本消息与语音消息
|
||||
|
||||
Returns:
|
||||
是否成功
|
||||
"""
|
||||
try:
|
||||
client = await self.get_client()
|
||||
key = self._conversation_key(conversation_id)
|
||||
|
||||
|
||||
# 获取现有历史
|
||||
history = await self.get_conversation_history(conversation_id)
|
||||
|
||||
|
||||
# 添加新消息
|
||||
history.append({"role": role, "content": content})
|
||||
history.append({"role": role, "content": content, "messageType": message_type})
|
||||
|
||||
# 保存回 Redis(带过期时间)
|
||||
await client.setex(key, self.session_ttl, json.dumps(history, ensure_ascii=False))
|
||||
|
||||
Reference in New Issue
Block a user