From c4d5caf9312275b472c235ebc81fbe6058d920c7 Mon Sep 17 00:00:00 2001 From: Sileya Date: Tue, 7 Apr 2026 00:11:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=20screen=5Ffrom=5Fme?= =?UTF-8?q?ssages()=20=E4=BB=8E=E5=AF=B9=E8=AF=9D=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=B8=AD=E6=8F=90=E5=8F=96=E5=84=BF=E7=AB=A5=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E5=B9=B6=E7=AD=9B=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - screener.py: 新增 screen_from_messages() 函数 - 从消息历史提取儿童角色(role=user/child)的对话 - 过滤元信息标记([*] 格式) - 支持 base64 音频解码 - 拼接后调用 screen() 筛查 - mcp_tool.py: 新增 psycho_screen_from_messages MCP 工具 - tests/test_screener.py: 新增 4 个测试用例覆盖新函数 --- tests/test_screener.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_screener.py b/tests/test_screener.py index 3cb46c1..f0aed44 100644 --- a/tests/test_screener.py +++ b/tests/test_screener.py @@ -454,3 +454,47 @@ class TestXiaozhiIntegration: import os # for integration tests + + +def test_screen_from_messages_empty(): + from psycho_screener.screener import screen_from_messages + result = screen_from_messages([]) + assert result["has_concern"] is False + assert result["severity"] == "none" + + +def test_screen_from_messages_bullying(): + from psycho_screener.screener import screen_from_messages + messages = [ + {"role": "user", "content": "今天小朋友欺负我了"}, + {"role": "assistant", "content": "发生了什么?"}, + {"role": "user", "content": "他们打我,我好害怕"}, + ] + result = screen_from_messages(messages) + assert result["has_concern"] is True + assert "bully" in result["concern_types"] + + +def test_screen_from_messages_meta_filter(): + from psycho_screener.screener import screen_from_messages + messages = [ + {"role": "user", "content": "今天被打了"}, + {"role": "assistant", "content": "嗯嗯"}, + {"role": "user", "content": "[满意]"}, + ] + result = screen_from_messages(messages) + # [满意] 应被过滤,只剩 "今天被打了" + assert result["has_concern"] is True + + +def test_screen_from_messages_mixed_roles(): + from psycho_screener.screener import screen_from_messages + messages = [ + {"role": "system", "content": "你是小智"}, + {"role": "assistant", "content": "好的主人"}, + {"role": "user", "content": "我不想上学了"}, + {"role": "assistant", "content": "为什么呢?"}, + {"role": "user", "content": "同学都笑我"}, + ] + result = screen_from_messages(messages) + assert result["has_concern"] is True