fix: tool parser and illegal char and /tmp space

This commit is contained in:
2026-04-22 01:19:09 +00:00
parent bce3fe1395
commit 152f01613b
4 changed files with 304 additions and 33 deletions

View File

@@ -128,20 +128,33 @@ def _normalize_message_content_for_template(content, role=""):
def _normalize_tool_call_for_template(tool_call):
if not isinstance(tool_call, dict):
return tool_call
return {
"function": {
"name": "",
"arguments": {"__raw_tool_call__": parse_jsonish(tool_call)},
}
}
normalized = dict(tool_call)
function = normalized.get("function")
normalized_function = dict(function) if isinstance(function, dict) else None
if normalized_function is None and ("name" in normalized or "arguments" in normalized):
normalized_function = {}
if normalized_function is not None:
if "name" not in normalized_function and normalized.get("name"):
normalized_function["name"] = normalized["name"]
if "arguments" not in normalized_function and "arguments" in normalized:
normalized_function["arguments"] = normalized["arguments"]
arguments = parse_jsonish(normalized_function.get("arguments", {}))
normalized_function["arguments"] = arguments if isinstance(arguments, dict) else {"__raw_arguments__": arguments}
normalized["function"] = normalized_function
normalized_function = dict(function) if isinstance(function, dict) else {}
tool_name = (
normalized_function.get("name")
or normalized.get("name")
or normalized.get("tool_name")
or normalized.get("function_name")
or ""
)
raw_arguments = (
normalized_function.get("arguments")
if "arguments" in normalized_function
else normalized.get("arguments", normalized.get("parameters", normalized.get("args", {})))
)
arguments = parse_jsonish(raw_arguments)
normalized_function["name"] = str(tool_name or "")
normalized_function["arguments"] = arguments if isinstance(arguments, dict) else {"__raw_arguments__": arguments}
normalized["function"] = normalized_function
return normalized
@@ -150,11 +163,23 @@ def _normalize_tool_spec_for_template(tool):
return tool
normalized = dict(tool)
function = normalized.get("function")
if isinstance(function, dict):
normalized_function = dict(function)
parameters = parse_jsonish(normalized_function.get("parameters", {}))
normalized_function = dict(function) if isinstance(function, dict) else {}
tool_name = (
normalized_function.get("name")
or normalized.get("name")
or normalized.get("tool_name")
or normalized.get("function_name")
or ""
)
if tool_name or function is not None or "name" in normalized or "parameters" in normalized:
parameters = parse_jsonish(
normalized_function.get("parameters", normalized.get("parameters", normalized.get("args", {})))
)
normalized_function["name"] = str(tool_name or "")
if isinstance(parameters, dict):
normalized_function["parameters"] = parameters
else:
normalized_function["parameters"] = {"__raw_parameters__": parameters}
normalized["function"] = normalized_function
return normalized
@@ -165,7 +190,12 @@ def _normalize_qwen_message_for_template(message):
normalized_message = dict(message)
normalized_message["content"] = _stringify_message_content_for_template(message.get("content"))
normalized_tool_calls = []
for tool_call in message.get("tool_calls", []):
tool_calls = message.get("tool_calls", [])
if isinstance(tool_calls, dict):
tool_calls = [tool_calls]
elif not isinstance(tool_calls, list):
tool_calls = [tool_calls]
for tool_call in tool_calls:
normalized_tool_call = _normalize_tool_call_for_template(tool_call)
if isinstance(normalized_tool_call, dict):
function = normalized_tool_call.get("function")
@@ -244,11 +274,21 @@ def build_glm5_canonical_prompt(payload):
message.get("content"),
role=str(message.get("role", "")),
)
tool_calls = message.get("tool_calls", [])
if isinstance(tool_calls, dict):
tool_calls = [tool_calls]
elif not isinstance(tool_calls, list):
tool_calls = [tool_calls]
normalized_message["tool_calls"] = [
_normalize_tool_call_for_template(tool_call) for tool_call in message.get("tool_calls", [])
_normalize_tool_call_for_template(tool_call) for tool_call in tool_calls
]
messages.append(normalized_message)
tools = [_normalize_tool_spec_for_template(tool) for tool in parameters.get("tools", []) if isinstance(tool, dict)]
tools_payload = parameters.get("tools", [])
if isinstance(tools_payload, dict):
tools_payload = [tools_payload]
elif not isinstance(tools_payload, list):
tools_payload = [tools_payload]
tools = [_normalize_tool_spec_for_template(tool) for tool in tools_payload if isinstance(tool, dict)]
return _load_glm5_chat_template().render(
messages=messages,
tools=tools,
@@ -266,7 +306,12 @@ def build_qwen3_coder_canonical_prompt(payload):
]
if not messages:
messages = [{"role": "system", "content": ""}]
tools = [_normalize_tool_spec_for_template(tool) for tool in parameters.get("tools", []) if isinstance(tool, dict)]
tools_payload = parameters.get("tools", [])
if isinstance(tools_payload, dict):
tools_payload = [tools_payload]
elif not isinstance(tools_payload, list):
tools_payload = [tools_payload]
tools = [_normalize_tool_spec_for_template(tool) for tool in tools_payload if isinstance(tool, dict)]
return _load_qwen3_coder_chat_template().render(
messages=messages,
tools=tools,