Commit 1d229159 by sunqi02 Committed by 胡悦

feat: 更新消息处理逻辑,区分文本和文件类型消息,优化消息合并和检查

1 parent 98f80b8a
......@@ -36,8 +36,8 @@ class MessageInfo(BaseModel):
msg_time: str = Field(..., description="消息时间")
sender_id: str = Field(..., description="发送者ID")
sender_name: Optional[str] = Field(None, description="发送者名称")
msg_type: str = Field(..., description="消息类型: text / image")
msg_content: str = Field(default="", description="消息内容(text 为文本,image 为图片 URL)")
msg_type: str = Field(..., description="消息类型: text(文本)/ 其余全部视为文件")
msg_content: str = Field(default="", description="消息内容(text 为文本,其余类型为文件/图片 URL)")
payload: Optional[dict] = Field(default_factory=dict, description="扩展字段")
......@@ -76,23 +76,19 @@ async def chat(
if not session_id:
raise HTTPException(status_code=400, detail="group.group_contact_id 不能为空")
# 1. 提取 query:text/other/file 消息的 msg_content 用 \n 拼接
# 1. 提取 query:只有 text 类型为文本,其余全部当文件处理
text_parts = []
file_url: Optional[str] = None
for msg in request.messages:
if msg.msg_type in ("text", "other", "file"):
if msg.msg_type == "text":
if msg.msg_content:
text_parts.append(msg.msg_content)
elif msg.msg_type == "image":
else:
if msg.msg_content and file_url is None:
file_url = msg.msg_content # 取第一张图片 URL
file_url = msg.msg_content # 取第一个非 text 消息的 URL
query = "\n".join(text_parts)
# 检查是否有任何有效消息(text/other/file/image 均可)
has_any_content = bool(query) or file_url or any(
m.msg_type in ("file", "image", "other") and m.msg_content
for m in request.messages
)
has_any_content = bool(query) or bool(file_url)
if not has_any_content:
raise HTTPException(status_code=400, detail="messages 中没有有效消息")
......@@ -166,10 +162,10 @@ async def _process_message_async_via_gate(
text_parts = []
file_url: Optional[str] = None
for msg in messages:
if msg.msg_type in ("text", "other", "file"):
if msg.msg_type == "text":
if msg.msg_content:
text_parts.append(msg.msg_content)
elif msg.msg_type == "image":
else:
if msg.msg_content and file_url is None:
file_url = msg.msg_content
query = "\n".join(text_parts)
......
......@@ -194,18 +194,15 @@ class MessageGate:
from app.infrastructure.session_store import SessionStore
store = SessionStore()
# 合并 pending 消息文本(逻辑不变)
# 合并 pending 消息文本:text 类型原文照录,非 text 标记为文件
parts = []
for m in pending:
mt = m.get("msg_type", "")
mc = m.get("msg_content", "")
if mt in ("text", "other", "file") and mc:
if mt == "file":
parts.append(f"[文件: {mc}]")
elif mt == "other":
parts.append(f"[其他: {mc}]")
else:
parts.append(mc)
if mt == "text" and mc:
parts.append(mc)
elif mc:
parts.append(f"[文件: {mc}]")
merged_text = "\n".join(parts) if parts else "(空消息)"
# 加载 KV data
......@@ -326,7 +323,7 @@ class MessageGate:
- 若历史上下文中有相关资源,可在 reply 中反问确认("您是想处理刚才的 xxx.mp3 吗?"),但**不能把历史资源写入 rewritten_query**
## 消息格式说明
- 用户直接发送的文件/图片会被标记为 [文件: URL] 或 [图片: URL]
- 非 text 消息(文件、图片等)会被标记为 [文件: URL]
- 用户输入的文本消息保持原样
- `## 当前用户消息` 中的 URL 是用户本轮主动提供的,应保留到 rewritten_query
......@@ -401,7 +398,7 @@ class MessageGate:
store = SessionStore()
merged_text = "\n".join(
m.get("msg_content", "") for m in pending
if m.get("msg_type") in ("text", "other", "file")
if m.get("msg_type") == "text"
)
mem = SessionMemory(session_id, store)
mem.add("user", merged_text)
......@@ -437,7 +434,7 @@ class MessageGate:
store = SessionStore()
merged_text = "\n".join(
m.get("msg_content", "") for m in pending
if m.get("msg_type") in ("text", "other", "file")
if m.get("msg_type") == "text"
)
# 将追问交换追加到 intent_session(分类上下文),不污染 conversation_history
data = store.get(session_id) or {}
......@@ -494,10 +491,10 @@ class MessageGate:
data["intent_session"] = ""
store.set(session_id, data)
# 提取 file_url(兼容 image 和 file 类型)
# 提取 file_url(取第一个非 text 消息的 URL)
file_url = None
for m in pending:
if m.get("msg_type") in ("image", "file") and m.get("msg_content"):
if m.get("msg_type") != "text" and m.get("msg_content"):
file_url = m["msg_content"]
break
......
......@@ -95,8 +95,8 @@ class SessionBuffer:
self.store.set(self.session_id, data)
def has_text_message(self) -> bool:
"""检查 pending_messages 中是否有 text 或 other 类型消息。"""
"""检查 pending_messages 中是否有 text 类型消息。"""
for m in self.get_pending():
if m.get("msg_type") in ("text", "other"):
if m.get("msg_type") == "text":
return True
return False
\ No newline at end of file
......@@ -95,10 +95,10 @@ def _override_get_message_gate():
text_parts = []
file_url = None
for msg in (messages or []):
if msg.msg_type in ("text", "other", "file"):
if msg.msg_type == "text":
if msg.msg_content:
text_parts.append(msg.msg_content)
elif msg.msg_type == "image":
else:
if msg.msg_content and file_url is None:
file_url = msg.msg_content
query = "\n".join(text_parts)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!