From 70f1a7bfd8e8ef411a379b0fe70417c9dd77e839 Mon Sep 17 00:00:00 2001 From: Bao1573 <77220108+PG2047@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:50:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(hass):=20devices=20=E5=86=99=E6=88=90?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=97=B6=E4=B9=9F=E8=83=BD=E6=8B=BC=E8=BF=9B?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=8D=20(#3541)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit config.yaml 示例和手机端智控台给的 devices 是列表,append_devices_to_prompt 却按字符串直接拼,每次连接都报 can only concatenate str (not "list") to str,设备清单进不了提示词,模型只能猜 entity_id。列表按每项一行拼,字符串原样用,其它当空。附测试。 --- .../plugins_func/functions/hass_init.py | 15 +++- .../tests/plugins_func/test_hass_init.py | 76 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 main/xiaozhi-server/tests/plugins_func/test_hass_init.py diff --git a/main/xiaozhi-server/plugins_func/functions/hass_init.py b/main/xiaozhi-server/plugins_func/functions/hass_init.py index a106d16d3..2487b5ee7 100644 --- a/main/xiaozhi-server/plugins_func/functions/hass_init.py +++ b/main/xiaozhi-server/plugins_func/functions/hass_init.py @@ -5,6 +5,17 @@ TAG = __name__ logger = setup_logging() +def _devices_text(devices) -> str: + """把 devices 配置转成拼进提示词的文字。 + + config.yaml 示例和手机端智控台给的是列表,每项一条"位置,设备名,entity_id",这里拼成每行一条; + 网页端智控台和 HA 集成文档是一段字符串,原样用;其它(如 None)当空。 + """ + if isinstance(devices, (list, tuple)): + return "\n".join(str(d).strip() for d in devices if str(d).strip()) + return devices if isinstance(devices, str) else "" + + def append_devices_to_prompt(conn): if conn.intent_type == "function_call": funcs = conn.config["Intent"][conn.config["selected_module"]["Intent"]].get( @@ -21,7 +32,9 @@ def append_devices_to_prompt(conn): if "hass_get_state" in funcs or "hass_set_state" in funcs: prompt = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n" - deviceStr = plugins_config.get(config_source, {}).get("devices", "") + deviceStr = _devices_text( + plugins_config.get(config_source, {}).get("devices", "") + ) conn.prompt += prompt + deviceStr + "\n" # 更新提示词 conn.dialogue.update_system_message(conn.prompt) diff --git a/main/xiaozhi-server/tests/plugins_func/test_hass_init.py b/main/xiaozhi-server/tests/plugins_func/test_hass_init.py new file mode 100644 index 000000000..49b5b2989 --- /dev/null +++ b/main/xiaozhi-server/tests/plugins_func/test_hass_init.py @@ -0,0 +1,76 @@ +"""append_devices_to_prompt:devices 写成列表或一段字符串都能拼进提示词。 + +config.yaml 示例和手机端智控台给的是列表,以前会报 +can only concatenate str (not "list") to str,设备清单进不了提示词,模型只能猜 entity_id。 +""" +import pytest + +from plugins_func.functions.hass_init import append_devices_to_prompt + +HEADER = "\n下面是我家智能设备列表(位置,设备名,entity_id),可以通过homeassistant控制\n" +BASE_PROMPT = "你是小智。" + + +class FakeDialogue: + def __init__(self): + self.system_messages = [] + + def update_system_message(self, prompt): + self.system_messages.append(prompt) + + +class FakeConn: + def __init__(self, plugin, source="home_assistant", functions=("hass_get_state", "hass_set_state")): + self.intent_type = "function_call" + self.config = { + "selected_module": {"Intent": "function_call"}, + "Intent": {"function_call": {"functions": list(functions)}}, + "plugins": {source: plugin}, + } + self.prompt = BASE_PROMPT + self.dialogue = FakeDialogue() + + +def _plugin(**extra): + return {"base_url": "http://homeassistant.local:8123", "api_key": "test-token", **extra} + + +def test_list_devices_one_per_line(): + conn = FakeConn(_plugin(devices=[ + "客厅,玩具灯,switch.toy_lamp", + " 卧室,台灯,switch.desk_lamp ", + "", + ])) + append_devices_to_prompt(conn) + expected = BASE_PROMPT + HEADER + "客厅,玩具灯,switch.toy_lamp\n卧室,台灯,switch.desk_lamp\n" + assert conn.prompt == expected + assert conn.dialogue.system_messages == [expected] + + +def test_string_devices_kept_as_is(): + # 网页端智控台和 HA 集成文档给的是一段字符串 + devices = "客厅,玩具灯,switch.toy_lamp;\n卧室,台灯,switch.desk_lamp;" + conn = FakeConn(_plugin(devices=devices)) + append_devices_to_prompt(conn) + assert conn.prompt == BASE_PROMPT + HEADER + devices + "\n" + + +def test_mobile_console_hass_state_list(): + # 智控台下发的插件配置键是 hass_state;手机端把 array 字段按行切成了列表 + conn = FakeConn(_plugin(devices=["客厅,玩具灯,switch.toy_lamp"]), source="hass_state") + append_devices_to_prompt(conn) + assert conn.prompt.endswith(HEADER + "客厅,玩具灯,switch.toy_lamp\n") + + +@pytest.mark.parametrize("plugin", [_plugin(), _plugin(devices=None), _plugin(devices=[])]) +def test_missing_or_empty_devices_does_not_raise(plugin): + conn = FakeConn(plugin) + append_devices_to_prompt(conn) + assert conn.prompt == BASE_PROMPT + HEADER + "\n" + + +def test_without_hass_functions_prompt_unchanged(): + conn = FakeConn(_plugin(devices=["客厅,玩具灯,switch.toy_lamp"]), functions=("get_weather",)) + append_devices_to_prompt(conn) + assert conn.prompt == BASE_PROMPT + assert conn.dialogue.system_messages == []