Commit cce7bb5f by sunqi02 Committed by 胡悦

feat: add _merge_text_from_pending helper for extracting text from pending messages

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cb6fb426
...@@ -360,6 +360,19 @@ class MessageGate: ...@@ -360,6 +360,19 @@ class MessageGate:
return str(response) return str(response)
@staticmethod @staticmethod
def _merge_text_from_pending(pending: List[Dict[str, Any]]) -> str:
"""从 pending 列表中提取文本,与 _classify_and_rewrite 拼 merged_text 逻辑一致。"""
parts = []
for m in pending:
mt = m.get("msg_type", "")
mc = m.get("msg_content", "")
if mt == "text" and mc:
parts.append(mc)
elif mc:
parts.append(f"[文件: {mc}]")
return "\n".join(parts) if parts else ""
@staticmethod
def _parse_decision_json(text: str) -> "GateDecision": def _parse_decision_json(text: str) -> "GateDecision":
"""解析 LLM 返回的 JSON 字符串为 GateDecision。""" """解析 LLM 返回的 JSON 字符串为 GateDecision。"""
import json import json
......
...@@ -379,3 +379,48 @@ async def test_steward_agent_excluded_from_prompt(message_gate_with_deps): ...@@ -379,3 +379,48 @@ async def test_steward_agent_excluded_from_prompt(message_gate_with_deps):
system_content = messages[0]["content"] system_content = messages[0]["content"]
assert "音频转写" in system_content # 子 agent 注入 assert "音频转写" in system_content # 子 agent 注入
assert "协调各岗位" not in system_content # 主 agent 不注入 assert "协调各岗位" not in system_content # 主 agent 不注入
class TestMergeTextFromPending:
"""测试 _merge_text_from_pending 辅助方法。"""
def test_text_messages_concatenated(self):
from app.service.message_gate import MessageGate
pending = [
{"msg_type": "text", "msg_content": "第一行"},
{"msg_type": "text", "msg_content": "第二行"},
]
result = MessageGate._merge_text_from_pending(pending)
assert result == "第一行\n第二行"
def test_non_text_tagged_as_file(self):
from app.service.message_gate import MessageGate
pending = [
{"msg_type": "file", "msg_content": "https://example.com/audio.wav"},
]
result = MessageGate._merge_text_from_pending(pending)
assert result == "[文件: https://example.com/audio.wav]"
def test_mixed_text_and_file(self):
from app.service.message_gate import MessageGate
pending = [
{"msg_type": "text", "msg_content": "请总结这个录音"},
{"msg_type": "audio", "msg_content": "https://example.com/audio.wav"},
]
result = MessageGate._merge_text_from_pending(pending)
assert "请总结这个录音" in result
assert "[文件: https://example.com/audio.wav]" in result
def test_empty_pending(self):
from app.service.message_gate import MessageGate
result = MessageGate._merge_text_from_pending([])
assert result == ""
def test_empty_content_skipped(self):
from app.service.message_gate import MessageGate
pending = [
{"msg_type": "text", "msg_content": ""},
{"msg_type": "text", "msg_content": "有效内容"},
]
result = MessageGate._merge_text_from_pending(pending)
assert result == "有效内容"
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!