mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
The intent REQLLM path (core/handle/intentHandler.py) records a tool result via `Message(role="tool", content=text)` with no tool_call_id and no preceding assistant tool_calls. `Dialogue.getMessages` serializes it as a function_response anyway. When that history is later replayed to an OpenAI-compatible endpoint, the endpoint derives `function_response.name` by matching the tool_call_id back to a function_call; with no match the name is empty. gemini-2.5-flash tolerated the empty name, but gemini-3.5-flash rejects the whole request with HTTP 400 "GenerateContentRequest.contents[N].parts[0] .function_response.name: Name cannot be empty", so every affected turn falls back to "Sorry, the system is busy now." Only tool-using sessions and only the chat turns that replay the accumulated tool results were affected. Guard at the serialization boundary in `getMessages`: emit a role="tool" message as a function_response only when a preceding assistant message carries a matching tool_call id; otherwise demote it to a plain assistant context line so the turn survives. Valid tool-call pairs — including empty-string ids that still match — are unchanged. This defends against any orphan source, not just the intent path. Adds self-contained regression tests (no conftest required).
92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
"""Regression tests for the orphan tool-message guard in ``Dialogue.getMessages``.
|
|
|
|
A ``role="tool"`` message is only valid when a preceding assistant message issued
|
|
a matching ``tool_call`` (correlated by id). Some paths (e.g. the intent REQLLM
|
|
handler in ``core/handle/intentHandler.py``) inject a bare tool result via
|
|
``Message(role="tool", content=text)`` with no ``tool_call_id`` and no preceding
|
|
assistant ``tool_calls``. When that dialogue is later replayed to an OpenAI-compat
|
|
LLM endpoint, the endpoint derives ``function_response.name`` by matching the
|
|
``tool_call_id`` back to a ``function_call``; with no match the name is empty.
|
|
``gemini-2.5-flash`` tolerated it, but ``gemini-3.5-flash`` rejects the whole
|
|
request with HTTP 400 ``function_response.name: Name cannot be empty``, dropping
|
|
the turn to the "system is busy" fallback.
|
|
|
|
The fix demotes such orphans to a plain assistant context line while leaving valid
|
|
tool-call pairs (including empty-string ids that still match) untouched.
|
|
|
|
Self-contained: ``Dialogue`` has no heavy dependencies, so no conftest required.
|
|
"""
|
|
|
|
import pathlib
|
|
import sys
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from core.utils.dialogue import Dialogue, Message # noqa: E402
|
|
|
|
|
|
def _serialize(messages):
|
|
d = Dialogue()
|
|
for m in messages:
|
|
d.put(m)
|
|
return d.get_llm_dialogue_with_memory(None, None)
|
|
|
|
|
|
def test_orphan_tool_message_is_demoted():
|
|
# Mirrors the intent REQLLM path: a bare tool result with no matching tool_call.
|
|
out = _serialize(
|
|
[
|
|
Message(role="user", content="turn on the light"),
|
|
Message(role="tool", content="ok"),
|
|
Message(role="user", content="anything else?"),
|
|
]
|
|
)
|
|
# The orphan must NOT be emitted as a tool/function_response...
|
|
assert all(m.get("role") != "tool" for m in out), out
|
|
# ...but its content must be preserved (demoted, not dropped).
|
|
assert any(m.get("content") == "ok" for m in out), out
|
|
|
|
|
|
def test_valid_tool_pair_is_preserved():
|
|
# An assistant tool_call followed by its matching tool result stays a function_response.
|
|
out = _serialize(
|
|
[
|
|
Message(role="user", content="turn on the light"),
|
|
Message(
|
|
role="assistant",
|
|
tool_calls=[
|
|
{
|
|
"id": "call_1",
|
|
"type": "function",
|
|
"function": {"name": "set_light", "arguments": "{}"},
|
|
}
|
|
],
|
|
),
|
|
Message(role="tool", tool_call_id="call_1", content="ok"),
|
|
]
|
|
)
|
|
assert any(
|
|
m.get("role") == "tool" and m.get("tool_call_id") == "call_1" for m in out
|
|
), out
|
|
|
|
|
|
def test_empty_string_ids_still_match():
|
|
# OpenAI-compat streaming can yield empty tool-call ids; an empty id on both the
|
|
# assistant tool_call and the tool result still correlates and must be preserved.
|
|
out = _serialize(
|
|
[
|
|
Message(role="user", content="x"),
|
|
Message(
|
|
role="assistant",
|
|
tool_calls=[
|
|
{
|
|
"id": "",
|
|
"type": "function",
|
|
"function": {"name": "f", "arguments": "{}"},
|
|
}
|
|
],
|
|
),
|
|
Message(role="tool", tool_call_id="", content="ok"),
|
|
]
|
|
)
|
|
assert any(m.get("role") == "tool" for m in out), out
|