fix(reply): a tool call written in Qwen's XML keeps its argument types

Every <parameter> value was passed as text, so a delete confirmed with
confirm=true reached chatChannels as "true" and asked again. Values now take
the type the tool's input schema declares (boolean, integer, number, array,
object); a value that does not fit stays text. The roadmap's eval summary now
names the reply-length failure apart from the later passing runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 13:21:45 +09:00
parent 6d435caef0
commit 07c701be54
3 changed files with 50 additions and 5 deletions

View File

@@ -301,7 +301,34 @@ def _is_malformed_model_output(content: str) -> bool:
return False
def _extract_text_tool_call(content_field: str, known_names: set):
def _typed_xml_args(name: str, raw: dict, mcp_tools: Optional[dict] = None) -> dict:
"""Qwen's XML tool call carries every argument as text; give each the type its tool's input
schema declares (``confirm`` true is the boolean True, ``limit`` 5 the integer 5). A value that
does not read as its declared type, or has no declared type, stays text, for the tool to judge."""
schema = _get_tool_input_schema(name, mcp_tools) or {}
props = schema.get("properties") if isinstance(schema.get("properties"), dict) else {}
out: dict = {}
for key, text in raw.items():
kind = (props.get(key) or {}).get("type") if isinstance(props.get(key), dict) else None
value = text
try:
if kind == "boolean" and text.strip().lower() in ("true", "false"):
value = text.strip().lower() == "true"
elif kind == "integer":
value = int(text.strip())
elif kind == "number":
value = float(text.strip())
elif kind in ("array", "object"):
parsed = json.loads(text)
if isinstance(parsed, list if kind == "array" else dict):
value = parsed
except (ValueError, TypeError):
value = text
out[key] = value
return out
def _extract_text_tool_call(content_field: str, known_names: set, mcp_tools: Optional[dict] = None):
"""Parse a tool call out of a content-mode LLM response.
Small models emit several shapes when instructed to use text-based tool
@@ -332,8 +359,9 @@ def _extract_text_tool_call(content_field: str, known_names: set):
body = tc_block.group(1)
fn = re.match(r"<function=([^>\s]+)>(.*?)(?:</function>|$)", body, re.DOTALL)
if fn:
args = {k: v.strip() for k, v in re.findall(r"<parameter=([^>\s]+)>(.*?)</parameter>", fn.group(2), re.DOTALL)}
return fn.group(1).strip(), args, f"call_{uuid.uuid4().hex[:8]}"
name = fn.group(1).strip()
raw = {k: v.strip() for k, v in re.findall(r"<parameter=([^>\s]+)>(.*?)</parameter>", fn.group(2), re.DOTALL)}
return name, _typed_xml_args(name, raw, mcp_tools), f"call_{uuid.uuid4().hex[:8]}"
try:
data = json.loads(body)
name = str(data.get("name", "")).strip() if isinstance(data, dict) else ""
@@ -2046,7 +2074,7 @@ def run_reply_engine(db: "Database", cfg, tts: Optional[Any],
# logic is unit-testable and shared across future callers.
content_field = msg.get("content", "") or ""
known_names = known_tool_names
name, args, tool_call_id = _extract_text_tool_call(content_field, known_names)
name, args, tool_call_id = _extract_text_tool_call(content_field, known_names, mcp_tools)
if name:
return name, args, tool_call_id