1162 lines
36 KiB
Python
1162 lines
36 KiB
Python
"""
|
||
Parsing and response helpers for tool-calling.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import html
|
||
import json
|
||
import re
|
||
import time
|
||
import xml.etree.ElementTree as ET
|
||
from typing import Any, Dict, Iterable, List, Optional, Tuple
|
||
|
||
from bs4 import NavigableString, Tag
|
||
|
||
from app.services.sse_utils import sse_frame_data_text
|
||
|
||
from app.services.tool_calling_common import (
|
||
_LEGACY_XML_ARG_TAG,
|
||
_LEGACY_XML_CALL_TAG,
|
||
_LEGACY_XML_WRAPPER_TAG,
|
||
_PREFERRED_XML_ARG_TAG,
|
||
_PREFERRED_XML_CALL_TAG,
|
||
_PREFERRED_XML_WRAPPER_TAG,
|
||
_debug_preview,
|
||
_contains_unicode_surrogate,
|
||
_new_completion_id,
|
||
_new_tool_call_id,
|
||
_pack_sse_chunk,
|
||
_replace_unicode_surrogates,
|
||
_resolve_tool_name,
|
||
_serialize_content,
|
||
get_tool_calling_sanitize_assistant_content_enabled,
|
||
logger,
|
||
)
|
||
|
||
def parse_tool_response(
|
||
text: str,
|
||
tools: List[Dict[str, Any]],
|
||
) -> Dict[str, Any]:
|
||
raw = str(text or "")
|
||
logger.debug(
|
||
f"[tool_calling] raw assistant text len={len(raw)} "
|
||
f"preview={_debug_preview(raw)}"
|
||
)
|
||
if _is_echoed_assistant_tool_history(raw):
|
||
logger.warning("[tool_calling] rejected echoed assistant tool-call history")
|
||
return {
|
||
"mode": "final",
|
||
"content": "",
|
||
"tool_calls": [],
|
||
"parse_error": "assistant_tool_history_echo",
|
||
}
|
||
allowed = {
|
||
str(item.get("function", {}).get("name", "") or "").strip(): item
|
||
for item in tools or []
|
||
if isinstance(item, dict)
|
||
}
|
||
|
||
json_payload = _try_parse_json_payload(raw, allowed)
|
||
if json_payload is not None:
|
||
tool_names = [
|
||
str(item.get("function", {}).get("name", "") or "").strip()
|
||
for item in json_payload.get("tool_calls") or []
|
||
if isinstance(item, dict)
|
||
]
|
||
content_text = str(json_payload.get("content") or "")
|
||
logger.debug(
|
||
"[tool_calling] parsed JSON payload "
|
||
f"mode={json_payload.get('mode')} "
|
||
f"tool_calls={len(json_payload.get('tool_calls') or [])} "
|
||
f"tool_names={tool_names or ['none']} "
|
||
f"content_len={len(content_text)} "
|
||
f"content={_debug_preview(content_text)}"
|
||
)
|
||
return json_payload
|
||
|
||
xml_payload = _try_parse_xml_tool_calls(raw, allowed)
|
||
if xml_payload is not None:
|
||
logger.debug(
|
||
"[tool_calling] parsed XML payload "
|
||
f"tool_calls={len(xml_payload.get('tool_calls') or [])}"
|
||
)
|
||
return xml_payload
|
||
|
||
logger.debug(f"[tool_calling] falling back to final-text mode (len={len(raw)})")
|
||
return {
|
||
"mode": "final",
|
||
"content": _replace_unicode_surrogates(raw.strip()),
|
||
"tool_calls": [],
|
||
}
|
||
|
||
|
||
def _is_echoed_assistant_tool_history(raw: str) -> bool:
|
||
marker = "[Assistant Tool Calls]\n"
|
||
_, separator, payload_text = str(raw or "").partition(marker)
|
||
if not separator:
|
||
return False
|
||
|
||
try:
|
||
payload = json.loads(payload_text.strip())
|
||
except Exception:
|
||
return False
|
||
if not isinstance(payload, list) or not payload:
|
||
return False
|
||
|
||
for item in payload:
|
||
if not isinstance(item, dict):
|
||
return False
|
||
function_data = item.get("function")
|
||
if not isinstance(function_data, dict) or "arguments" not in function_data:
|
||
return False
|
||
return True
|
||
|
||
|
||
def build_tool_completion_response(
|
||
model: str,
|
||
parsed: Dict[str, Any],
|
||
*,
|
||
legacy_function_call: bool = False,
|
||
) -> Dict[str, Any]:
|
||
completion_id = _new_completion_id()
|
||
tool_calls = parsed.get("tool_calls") or []
|
||
content = parsed.get("content")
|
||
if tool_calls:
|
||
message: Dict[str, Any] = {
|
||
"role": "assistant",
|
||
"content": content if content not in ("", None) else None,
|
||
}
|
||
if legacy_function_call:
|
||
message["function_call"] = _legacy_function_call_from_tool_call(tool_calls[0])
|
||
finish_reason = "function_call"
|
||
else:
|
||
message["tool_calls"] = tool_calls
|
||
finish_reason = "tool_calls"
|
||
else:
|
||
message = {
|
||
"role": "assistant",
|
||
"content": str(content or ""),
|
||
}
|
||
finish_reason = "stop"
|
||
|
||
return {
|
||
"id": completion_id,
|
||
"object": "chat.completion",
|
||
"created": int(time.time()),
|
||
"model": model,
|
||
"choices": [
|
||
{
|
||
"index": 0,
|
||
"message": message,
|
||
"finish_reason": finish_reason,
|
||
}
|
||
],
|
||
"usage": {
|
||
"prompt_tokens": 0,
|
||
"completion_tokens": 0,
|
||
"total_tokens": 0,
|
||
},
|
||
}
|
||
|
||
|
||
def iter_tool_stream_chunks(
|
||
model: str,
|
||
parsed: Dict[str, Any],
|
||
*,
|
||
legacy_function_call: bool = False,
|
||
) -> Iterable[str]:
|
||
completion_id = _new_completion_id()
|
||
created = int(time.time())
|
||
|
||
first_delta: Dict[str, Any] = {"role": "assistant"}
|
||
if parsed.get("content") not in ("", None):
|
||
first_delta["content"] = str(parsed.get("content") or "")
|
||
if parsed.get("tool_calls"):
|
||
if legacy_function_call:
|
||
first_delta["function_call"] = _legacy_function_call_from_tool_call(
|
||
parsed["tool_calls"][0]
|
||
)
|
||
else:
|
||
first_delta["tool_calls"] = _tool_calls_for_stream_delta(parsed["tool_calls"])
|
||
|
||
yield _pack_sse_chunk(
|
||
{
|
||
"id": completion_id,
|
||
"object": "chat.completion.chunk",
|
||
"created": created,
|
||
"model": model,
|
||
"choices": [{"index": 0, "delta": first_delta, "finish_reason": None}],
|
||
}
|
||
)
|
||
|
||
finish_reason = (
|
||
"function_call"
|
||
if parsed.get("tool_calls") and legacy_function_call
|
||
else "tool_calls"
|
||
if parsed.get("tool_calls")
|
||
else "stop"
|
||
)
|
||
yield _pack_sse_chunk(
|
||
{
|
||
"id": completion_id,
|
||
"object": "chat.completion.chunk",
|
||
"created": int(time.time()),
|
||
"model": model,
|
||
"choices": [{"index": 0, "delta": {}, "finish_reason": finish_reason}],
|
||
}
|
||
)
|
||
yield "data: [DONE]\n\n"
|
||
|
||
|
||
def _tool_calls_for_stream_delta(tool_calls: Any) -> List[Dict[str, Any]]:
|
||
normalized: List[Dict[str, Any]] = []
|
||
for fallback_index, item in enumerate(tool_calls or []):
|
||
if not isinstance(item, dict):
|
||
continue
|
||
delta_item = dict(item)
|
||
try:
|
||
delta_item["index"] = int(delta_item.get("index"))
|
||
except Exception:
|
||
delta_item["index"] = fallback_index
|
||
normalized.append(delta_item)
|
||
return normalized
|
||
|
||
|
||
def _legacy_function_call_from_tool_call(tool_call: Any) -> Dict[str, str]:
|
||
item = tool_call if isinstance(tool_call, dict) else {}
|
||
function_data = item.get("function") if isinstance(item.get("function"), dict) else {}
|
||
arguments = function_data.get("arguments")
|
||
if not isinstance(arguments, str):
|
||
arguments = json.dumps(arguments if arguments is not None else {}, ensure_ascii=False)
|
||
return {
|
||
"name": str(function_data.get("name") or "").strip(),
|
||
"arguments": arguments,
|
||
}
|
||
|
||
|
||
_TOOL_CALLING_PLACEHOLDER_URL_RE = re.compile(
|
||
r"^\s*https?://(?:[\w.-]+\.)?googleusercontent\.com/"
|
||
r"(?:image_generation_content|generated_music_content)/\d+\s*$",
|
||
re.IGNORECASE | re.MULTILINE,
|
||
)
|
||
|
||
|
||
def _cleanup_tool_calling_content_text(content: Any) -> str:
|
||
text = _serialize_content(content)
|
||
cleaned = _TOOL_CALLING_PLACEHOLDER_URL_RE.sub("", text or "")
|
||
return re.sub(r"\n{3,}", "\n\n", cleaned).strip()
|
||
|
||
|
||
def _resolve_response_media_ref(item: Any) -> str:
|
||
if not isinstance(item, dict):
|
||
return ""
|
||
ref = str(item.get("url") or item.get("data_uri") or "").strip()
|
||
return ref
|
||
|
||
|
||
def _build_response_media_markdown_block(media_items: Any) -> str:
|
||
if not isinstance(media_items, list):
|
||
return ""
|
||
|
||
image_blocks: List[str] = []
|
||
audio_lines: List[str] = []
|
||
video_lines: List[str] = []
|
||
|
||
for item in media_items:
|
||
ref = _resolve_response_media_ref(item)
|
||
if not ref:
|
||
continue
|
||
|
||
media_type = str((item or {}).get("media_type") or "image").strip().lower()
|
||
if media_type == "image":
|
||
image_blocks.append(f"\n\n")
|
||
continue
|
||
|
||
label = str((item or {}).get("label") or (item or {}).get("mime") or "").strip()
|
||
label_suffix = f" - {label}" if label else ""
|
||
if media_type == "audio":
|
||
audio_lines.append(f"[audio_{len(audio_lines)}]({ref}){label_suffix}")
|
||
elif media_type == "video":
|
||
video_lines.append(f"[video_{len(video_lines)}]({ref}){label_suffix}")
|
||
|
||
blocks: List[str] = []
|
||
if image_blocks:
|
||
blocks.append("".join(image_blocks))
|
||
if audio_lines:
|
||
blocks.append("\n\n" + "\n".join(audio_lines))
|
||
if video_lines:
|
||
blocks.append("\n\n" + "\n".join(video_lines))
|
||
|
||
if not blocks:
|
||
return ""
|
||
|
||
return "".join(blocks) + "\n\n"
|
||
|
||
|
||
def _strip_trailing_response_media_markdown(content: str, media_items: Any) -> str:
|
||
text = str(content or "")
|
||
media_markdown = _build_response_media_markdown_block(media_items)
|
||
if media_markdown:
|
||
candidates = []
|
||
for variant in (media_markdown, media_markdown.rstrip(), media_markdown.strip()):
|
||
if variant and variant not in candidates:
|
||
candidates.append(variant)
|
||
for candidate in candidates:
|
||
if text.endswith(candidate):
|
||
return text[: -len(candidate)].rstrip()
|
||
return text
|
||
|
||
|
||
def extract_tool_calling_assistant_content(response: Dict[str, Any]) -> str:
|
||
try:
|
||
message = (
|
||
response.get("choices", [])[0]
|
||
.get("message", {})
|
||
)
|
||
except Exception:
|
||
message = {}
|
||
|
||
if not isinstance(message, dict):
|
||
return ""
|
||
|
||
sanitize = get_tool_calling_sanitize_assistant_content_enabled()
|
||
content = _serialize_content(message.get("content"))
|
||
if not sanitize:
|
||
return content.strip()
|
||
|
||
content = _cleanup_tool_calling_content_text(content)
|
||
media_items = message.get("media")
|
||
if not isinstance(media_items, list):
|
||
media_items = response.get("media")
|
||
|
||
return _strip_trailing_response_media_markdown(content, media_items)
|
||
|
||
|
||
def decode_browser_non_stream_payload(payload: Any) -> Dict[str, Any]:
|
||
text = str(payload or "").strip()
|
||
if not text:
|
||
raise RuntimeError("empty_browser_response")
|
||
|
||
normalized = text.lstrip()
|
||
looks_like_sse = (
|
||
normalized.startswith(("data:", "event:", ":"))
|
||
or "\ndata:" in normalized
|
||
or "\r\ndata:" in normalized
|
||
)
|
||
candidates = _extract_sse_json_payloads(text) if looks_like_sse else [text]
|
||
last_error: Optional[Exception] = None
|
||
for candidate in candidates:
|
||
try:
|
||
data = json.loads(candidate)
|
||
except (TypeError, ValueError, RecursionError) as exc:
|
||
last_error = exc
|
||
continue
|
||
if isinstance(data, dict):
|
||
return data
|
||
raise RuntimeError(f"browser_response_not_object: {_debug_preview(data)}")
|
||
|
||
preview = _debug_preview(text, 500)
|
||
if last_error:
|
||
raise RuntimeError(
|
||
f"invalid_browser_json_response: {last_error}; payload_preview={preview}"
|
||
)
|
||
raise RuntimeError(f"invalid_browser_json_response: payload_preview={preview}")
|
||
|
||
|
||
def _extract_sse_json_payloads(text: str) -> List[str]:
|
||
payloads: List[str] = []
|
||
blocks = re.split(r"\r?\n\r?\n", text.strip())
|
||
for block in blocks:
|
||
payload_text = sse_frame_data_text(block)
|
||
if payload_text and payload_text.strip() != "[DONE]":
|
||
payloads.append(payload_text)
|
||
return payloads
|
||
|
||
|
||
def _try_parse_json_payload(text: str, allowed_tools: Dict[str, Dict[str, Any]]) -> Optional[Dict[str, Any]]:
|
||
candidate = str(text or "").strip()
|
||
if not candidate.startswith("{"):
|
||
return None
|
||
try:
|
||
payload = json.loads(candidate)
|
||
except (TypeError, ValueError, RecursionError):
|
||
return None
|
||
return _normalize_parsed_payload(payload, allowed_tools)
|
||
|
||
|
||
def _extract_json_candidates(text: str) -> List[str]:
|
||
stripped = str(text or "").strip()
|
||
candidates: List[str] = []
|
||
if not stripped:
|
||
return candidates
|
||
|
||
if stripped.startswith("```"):
|
||
stripped = re.sub(r"^```(?:json)?\s*", "", stripped, flags=re.IGNORECASE)
|
||
stripped = re.sub(r"\s*```$", "", stripped)
|
||
|
||
candidates.append(stripped)
|
||
candidates.extend(_extract_balanced_json_object_candidates(stripped))
|
||
|
||
start = stripped.find("{")
|
||
end = stripped.rfind("}")
|
||
if start != -1 and end != -1 and end > start:
|
||
candidates.append(stripped[start : end + 1])
|
||
|
||
seen = set()
|
||
result = []
|
||
for item in candidates:
|
||
key = item.strip()
|
||
if not key or key in seen:
|
||
continue
|
||
seen.add(key)
|
||
result.append(key)
|
||
return result
|
||
|
||
|
||
def _extract_balanced_json_object_candidates(text: str) -> List[str]:
|
||
value = str(text or "")
|
||
spans: List[List[int]] = []
|
||
stack: List[int] = []
|
||
in_string = False
|
||
escape = False
|
||
|
||
for index, current in enumerate(value):
|
||
if in_string:
|
||
if escape:
|
||
escape = False
|
||
elif current == "\\":
|
||
escape = True
|
||
elif current == '"':
|
||
in_string = False
|
||
continue
|
||
|
||
if current == '"':
|
||
in_string = True
|
||
continue
|
||
if current == "{":
|
||
stack.append(len(spans))
|
||
spans.append([index, -1])
|
||
continue
|
||
if current == "}":
|
||
if not stack:
|
||
continue
|
||
span_index = stack.pop()
|
||
spans[span_index][1] = index + 1
|
||
|
||
return [value[start:end] for start, end in spans if end >= start]
|
||
|
||
|
||
def _normalize_parsed_payload(
|
||
payload: Any,
|
||
allowed_tools: Dict[str, Dict[str, Any]],
|
||
) -> Optional[Dict[str, Any]]:
|
||
if not isinstance(payload, dict):
|
||
return None
|
||
|
||
openai_like = _normalize_openai_like_payload(payload, allowed_tools)
|
||
if openai_like is not None:
|
||
return openai_like
|
||
|
||
mode = str(payload.get("mode", "") or "").strip().lower()
|
||
if "tool_calls" in payload or mode == "tool_calls":
|
||
raw_calls = payload.get("tool_calls")
|
||
if not isinstance(raw_calls, list):
|
||
return None
|
||
tool_calls = _normalize_tool_calls(raw_calls, allowed_tools)
|
||
if tool_calls:
|
||
return {
|
||
"mode": "tool_calls",
|
||
"content": payload.get("content"),
|
||
"tool_calls": tool_calls,
|
||
}
|
||
if raw_calls:
|
||
return None
|
||
# 修复(2b):{"tool_calls": [], "content": "..."} 的空调用形式直接作为最终文本回复返回,
|
||
# 避免返回 None 后退化为原文再被 malformed 检测误判、触发无谓重试。
|
||
if payload.get("content") is not None:
|
||
return {
|
||
"mode": "final",
|
||
"content": str(payload.get("content", "") or ""),
|
||
"tool_calls": [],
|
||
}
|
||
|
||
if mode == "final":
|
||
return {
|
||
"mode": "final",
|
||
"content": str(payload.get("content", "") or ""),
|
||
"tool_calls": [],
|
||
}
|
||
|
||
return None
|
||
|
||
|
||
def _normalize_openai_like_payload(
|
||
payload: Dict[str, Any],
|
||
allowed_tools: Dict[str, Dict[str, Any]],
|
||
) -> Optional[Dict[str, Any]]:
|
||
message: Optional[Dict[str, Any]] = None
|
||
|
||
if isinstance(payload.get("message"), dict):
|
||
message = payload.get("message")
|
||
elif isinstance(payload.get("choices"), list) and payload["choices"]:
|
||
first_choice = payload["choices"][0]
|
||
if isinstance(first_choice, dict) and isinstance(first_choice.get("message"), dict):
|
||
message = first_choice.get("message")
|
||
elif str(payload.get("role", "") or "").strip().lower() == "assistant":
|
||
message = payload
|
||
|
||
if not isinstance(message, dict):
|
||
return None
|
||
|
||
content = message.get("content")
|
||
raw_tool_calls = message.get("tool_calls")
|
||
if isinstance(raw_tool_calls, list):
|
||
tool_calls = _normalize_tool_calls(raw_tool_calls, allowed_tools)
|
||
if tool_calls:
|
||
return {
|
||
"mode": "tool_calls",
|
||
"content": content,
|
||
"tool_calls": tool_calls,
|
||
}
|
||
if raw_tool_calls:
|
||
return None
|
||
|
||
if "content" in message:
|
||
return {
|
||
"mode": "final",
|
||
"content": "" if content is None else str(content),
|
||
"tool_calls": [],
|
||
}
|
||
|
||
return None
|
||
|
||
|
||
def _normalize_tool_calls(
|
||
raw_calls: List[Any],
|
||
allowed_tools: Dict[str, Dict[str, Any]],
|
||
) -> List[Dict[str, Any]]:
|
||
result: List[Dict[str, Any]] = []
|
||
for item in raw_calls:
|
||
if not isinstance(item, dict):
|
||
continue
|
||
|
||
function_data = item.get("function") if isinstance(item.get("function"), dict) else {}
|
||
raw_name = (
|
||
item.get("name")
|
||
or item.get("tool_name")
|
||
or function_data.get("name")
|
||
or ""
|
||
)
|
||
raw_name_text = str(raw_name or "").strip()
|
||
name = _resolve_tool_name(raw_name_text, allowed_tools) or raw_name_text
|
||
|
||
args = item.get("arguments", function_data.get("arguments"))
|
||
if _contains_unicode_surrogate(args):
|
||
continue
|
||
if isinstance(args, str):
|
||
try:
|
||
decoded_args = json.loads(args)
|
||
except (TypeError, ValueError, RecursionError):
|
||
decoded_args = None
|
||
if _contains_unicode_surrogate(decoded_args):
|
||
continue
|
||
arguments_payload = args
|
||
else:
|
||
arguments_payload = json.dumps(args if args is not None else {}, ensure_ascii=False)
|
||
|
||
result.append(
|
||
{
|
||
"id": str(item.get("id") or _new_tool_call_id()),
|
||
"type": "function",
|
||
"function": {
|
||
"name": name,
|
||
"arguments": arguments_payload,
|
||
},
|
||
}
|
||
)
|
||
|
||
return result
|
||
|
||
|
||
def _coerce_arguments_object(args: Any) -> Optional[Dict[str, Any]]:
|
||
if args is None:
|
||
return {}
|
||
if isinstance(args, dict):
|
||
return args
|
||
if isinstance(args, str):
|
||
stripped = args.strip()
|
||
if not stripped:
|
||
return {}
|
||
try:
|
||
parsed = json.loads(stripped)
|
||
if isinstance(parsed, dict):
|
||
return None if _contains_unicode_surrogate(parsed) else parsed
|
||
except Exception:
|
||
return None
|
||
return None
|
||
|
||
|
||
def _decode_tool_arguments(tool_call: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
||
function_data = tool_call.get("function") if isinstance(tool_call.get("function"), dict) else {}
|
||
raw_arguments = function_data.get("arguments")
|
||
if isinstance(raw_arguments, dict):
|
||
return raw_arguments
|
||
if isinstance(raw_arguments, str):
|
||
stripped = raw_arguments.strip()
|
||
if not stripped:
|
||
return {}
|
||
try:
|
||
parsed = json.loads(stripped)
|
||
except (TypeError, ValueError, RecursionError):
|
||
return None
|
||
if isinstance(parsed, dict) and not _contains_unicode_surrogate(parsed):
|
||
return parsed
|
||
return None
|
||
|
||
|
||
def _repair_json_like_argument_string(raw: str) -> str:
|
||
"""Compatibility no-op; malformed arguments must be repaired by the model."""
|
||
return str(raw or "")
|
||
|
||
|
||
_TOOL_XML_WRAPPER_OPEN_RE = re.compile(
|
||
r"<\s*(?:adapter_calls|tool_calls)\b[^>]*>",
|
||
flags=re.IGNORECASE,
|
||
)
|
||
_TOOL_XML_WRAPPER_CLOSE_RE = re.compile(
|
||
r"<\s*/\s*(?:adapter_calls|tool_calls)\s*>",
|
||
flags=re.IGNORECASE,
|
||
)
|
||
_TOOL_XML_INVOKE_OPEN_RE = re.compile(
|
||
r"<\s*(?:call|invoke|tool_call)\b[^>]*>",
|
||
flags=re.IGNORECASE,
|
||
)
|
||
_TOOL_XML_INVOKE_CLOSE_RE = re.compile(
|
||
r"<\s*/\s*(?:call|invoke|tool_call)\s*>",
|
||
flags=re.IGNORECASE,
|
||
)
|
||
_TOOL_XML_STRING_PARAM_NAMES = {
|
||
"code",
|
||
"command",
|
||
"content",
|
||
"description",
|
||
"new_string",
|
||
"old_string",
|
||
"path",
|
||
"prompt",
|
||
"query",
|
||
"question",
|
||
}
|
||
|
||
_TOOL_XML_MAX_CHARS = 200_000
|
||
_TOOL_XML_FORBIDDEN_DECL_RE = re.compile(r"<!\s*(?:DOCTYPE|ENTITY)\b", re.IGNORECASE)
|
||
|
||
|
||
def _mask_ignored_tool_markup_regions(text: str) -> str:
|
||
if not text:
|
||
return ""
|
||
|
||
chars = list(text)
|
||
|
||
def _blank(start: int, end: int) -> None:
|
||
for index in range(max(0, start), min(len(chars), end)):
|
||
if chars[index] not in "\r\n":
|
||
chars[index] = " "
|
||
|
||
fence_pattern = re.compile(
|
||
r"(?ms)^[ \t]*(```+|~~~+)[^\r\n]*\r?\n.*?^[ \t]*\1[ \t]*(?=\r?\n|$)"
|
||
)
|
||
for match in fence_pattern.finditer(text):
|
||
_blank(match.start(), match.end())
|
||
|
||
scan_text = "".join(chars)
|
||
index = 0
|
||
while index < len(scan_text):
|
||
if scan_text[index] != "`":
|
||
index += 1
|
||
continue
|
||
tick_count = 1
|
||
while index + tick_count < len(scan_text) and scan_text[index + tick_count] == "`":
|
||
tick_count += 1
|
||
closing = scan_text.find("`" * tick_count, index + tick_count)
|
||
if closing == -1:
|
||
index += tick_count
|
||
continue
|
||
_blank(index, closing + tick_count)
|
||
scan_text = "".join(chars)
|
||
index = closing + tick_count
|
||
|
||
return "".join(chars)
|
||
|
||
|
||
def _find_tool_xml_wrapper_spans(text: str) -> List[Tuple[int, int, str]]:
|
||
masked = _mask_ignored_tool_markup_regions(text)
|
||
spans: List[Tuple[int, int, str]] = []
|
||
search_from = 0
|
||
while True:
|
||
match = _TOOL_XML_WRAPPER_OPEN_RE.search(masked, search_from)
|
||
if not match:
|
||
break
|
||
block_end = _find_tool_xml_wrapper_end(masked, match.end())
|
||
if block_end == -1:
|
||
spans.append((match.start(), len(text), text[match.start() :]))
|
||
break
|
||
spans.append((match.start(), block_end, text[match.start() : block_end]))
|
||
search_from = block_end
|
||
return spans
|
||
|
||
|
||
def _find_tool_xml_wrapper_blocks(text: str) -> List[str]:
|
||
return [item[2] for item in _find_tool_xml_wrapper_spans(text)]
|
||
|
||
|
||
def _extract_text_excluding_spans(text: str, spans: List[Tuple[int, int]]) -> str:
|
||
if not spans:
|
||
return text
|
||
sorted_spans = sorted(spans, key=lambda s: s[0])
|
||
merged: List[Tuple[int, int]] = []
|
||
for start, end in sorted_spans:
|
||
if start < 0 or end <= start:
|
||
continue
|
||
if not merged:
|
||
merged.append((start, end))
|
||
else:
|
||
prev_start, prev_end = merged[-1]
|
||
if start <= prev_end:
|
||
merged[-1] = (prev_start, max(prev_end, end))
|
||
else:
|
||
merged.append((start, end))
|
||
|
||
parts: List[str] = []
|
||
last_idx = 0
|
||
for start, end in merged:
|
||
if start > last_idx:
|
||
parts.append(text[last_idx:start])
|
||
last_idx = max(last_idx, end)
|
||
if last_idx < len(text):
|
||
parts.append(text[last_idx:])
|
||
|
||
return "".join(parts)
|
||
|
||
|
||
def _find_tool_xml_wrapper_end(masked: str, start: int) -> int:
|
||
depth = 1
|
||
index = max(0, start)
|
||
while index < len(masked):
|
||
if masked.startswith("<![CDATA[", index):
|
||
cdata_end = masked.find("]]>", index + 9)
|
||
if cdata_end == -1:
|
||
return -1
|
||
index = cdata_end + 3
|
||
continue
|
||
|
||
open_match = _TOOL_XML_WRAPPER_OPEN_RE.match(masked, index)
|
||
if open_match:
|
||
depth += 1
|
||
index = open_match.end()
|
||
continue
|
||
|
||
close_match = _TOOL_XML_WRAPPER_CLOSE_RE.match(masked, index)
|
||
if close_match:
|
||
depth -= 1
|
||
index = close_match.end()
|
||
if depth == 0:
|
||
return index
|
||
continue
|
||
|
||
index += 1
|
||
return -1
|
||
|
||
|
||
def _find_tool_xml_invoke_end(masked: str, start: int) -> int:
|
||
depth = 1
|
||
index = max(0, start)
|
||
while index < len(masked):
|
||
if masked.startswith("<![CDATA[", index):
|
||
cdata_end = masked.find("]]>", index + 9)
|
||
if cdata_end == -1:
|
||
return -1
|
||
index = cdata_end + 3
|
||
continue
|
||
|
||
open_match = _TOOL_XML_INVOKE_OPEN_RE.match(masked, index)
|
||
if open_match:
|
||
depth += 1
|
||
index = open_match.end()
|
||
continue
|
||
|
||
close_match = _TOOL_XML_INVOKE_CLOSE_RE.match(masked, index)
|
||
if close_match:
|
||
depth -= 1
|
||
index = close_match.end()
|
||
if depth == 0:
|
||
return index
|
||
continue
|
||
|
||
index += 1
|
||
return -1
|
||
|
||
|
||
def _repair_missing_tool_xml_wrapper(text: str) -> str:
|
||
masked = _mask_ignored_tool_markup_regions(text)
|
||
wrapper_open = _TOOL_XML_WRAPPER_OPEN_RE.search(masked)
|
||
invoke_ranges: List[Tuple[int, int]] = []
|
||
search_from = 0
|
||
while True:
|
||
invoke_match = _TOOL_XML_INVOKE_OPEN_RE.search(masked, search_from)
|
||
if not invoke_match:
|
||
break
|
||
if wrapper_open and wrapper_open.start() <= invoke_match.start():
|
||
search_from = invoke_match.end()
|
||
continue
|
||
invoke_end = _find_tool_xml_invoke_end(masked, invoke_match.end())
|
||
if invoke_end == -1:
|
||
search_from = invoke_match.end()
|
||
continue
|
||
invoke_ranges.append((invoke_match.start(), invoke_end))
|
||
search_from = invoke_end
|
||
|
||
if invoke_ranges:
|
||
start = invoke_ranges[0][0]
|
||
end = invoke_ranges[-1][1]
|
||
return (
|
||
text[:start]
|
||
+ f"<{_PREFERRED_XML_WRAPPER_TAG}>"
|
||
+ text[start:end]
|
||
+ f"</{_PREFERRED_XML_WRAPPER_TAG}>"
|
||
+ text[end:]
|
||
)
|
||
|
||
invoke_match = _TOOL_XML_INVOKE_OPEN_RE.search(masked)
|
||
close_match = _TOOL_XML_WRAPPER_CLOSE_RE.search(masked)
|
||
if not invoke_match or not close_match:
|
||
return text
|
||
if invoke_match.start() >= close_match.start():
|
||
return text
|
||
return (
|
||
text[: invoke_match.start()]
|
||
+ f"<{_PREFERRED_XML_WRAPPER_TAG}>"
|
||
+ text[invoke_match.start() : close_match.start()]
|
||
+ f"</{_PREFERRED_XML_WRAPPER_TAG}>"
|
||
+ text[close_match.end() :]
|
||
)
|
||
|
||
|
||
def _normalize_tool_xml_markup(text: str) -> str:
|
||
return str(text or "")
|
||
|
||
|
||
def _safe_xml_fromstring(text: str) -> ET.Element:
|
||
"""Parse a complete tool envelope without HTML recovery."""
|
||
value = str(text or "")
|
||
if len(value) > _TOOL_XML_MAX_CHARS:
|
||
raise ValueError("tool XML block exceeds maximum length")
|
||
if _TOOL_XML_FORBIDDEN_DECL_RE.search(value):
|
||
raise ValueError("DTD and entity declarations are not allowed in tool XML")
|
||
|
||
try:
|
||
root = ET.fromstring(value)
|
||
except ET.ParseError as exc:
|
||
raise ValueError("tool XML must be complete and well-formed") from exc
|
||
if _xml_local_name(root.tag).lower() not in {
|
||
_PREFERRED_XML_WRAPPER_TAG,
|
||
_LEGACY_XML_WRAPPER_TAG,
|
||
}:
|
||
raise ValueError("tool XML wrapper not found")
|
||
return root
|
||
|
||
|
||
def _xml_local_name(tag: Any) -> str:
|
||
value = str(tag or "")
|
||
if "}" in value:
|
||
value = value.rsplit("}", 1)[-1]
|
||
return value.strip()
|
||
|
||
|
||
def _append_xml_value(target: Dict[str, Any], key: str, value: Any) -> None:
|
||
if key in target:
|
||
existing = target[key]
|
||
if isinstance(existing, list):
|
||
existing.append(value)
|
||
else:
|
||
target[key] = [existing, value]
|
||
return
|
||
target[key] = value
|
||
|
||
|
||
def _schema_prefers_string(schema: Any) -> bool:
|
||
if not isinstance(schema, dict):
|
||
return True
|
||
schema_type = schema.get("type")
|
||
if isinstance(schema_type, str):
|
||
return schema_type.strip().lower() == "string"
|
||
if isinstance(schema_type, list):
|
||
return any(str(item).strip().lower() == "string" for item in schema_type)
|
||
|
||
enum_values = schema.get("enum")
|
||
if isinstance(enum_values, list) and enum_values:
|
||
return any(isinstance(item, str) for item in enum_values)
|
||
if "const" in schema:
|
||
return isinstance(schema.get("const"), str)
|
||
|
||
for keyword in ("anyOf", "oneOf", "allOf"):
|
||
variants = schema.get(keyword)
|
||
if isinstance(variants, list):
|
||
return any(_schema_prefers_string(variant) for variant in variants)
|
||
|
||
# XML has no native scalar type. Preserve lexical values unless the schema
|
||
# explicitly declares a non-string type that needs JSON scalar decoding.
|
||
return True
|
||
|
||
|
||
def _empty_xml_value_for_schema(schema: Any) -> Any:
|
||
if not isinstance(schema, dict):
|
||
return None
|
||
schema_type = schema.get("type")
|
||
if isinstance(schema_type, str):
|
||
normalized_type = schema_type.strip().lower()
|
||
if normalized_type == "array":
|
||
return []
|
||
if normalized_type == "object":
|
||
return {}
|
||
if isinstance(schema_type, list):
|
||
normalized_types = {str(item).strip().lower() for item in schema_type}
|
||
if "array" in normalized_types and "string" not in normalized_types:
|
||
return []
|
||
if "object" in normalized_types and "string" not in normalized_types:
|
||
return {}
|
||
return None
|
||
|
||
|
||
def _schema_property_name(schema: Any, field_name: str) -> str:
|
||
raw_name = str(field_name or "").strip()
|
||
if not isinstance(schema, dict):
|
||
return raw_name
|
||
properties = schema.get("properties") if isinstance(schema.get("properties"), dict) else {}
|
||
if raw_name in properties:
|
||
return raw_name
|
||
folded_name = raw_name.casefold()
|
||
return next(
|
||
(str(name) for name in properties if str(name).casefold() == folded_name),
|
||
raw_name,
|
||
)
|
||
|
||
|
||
def _schema_property_schema(schema: Any, field_name: str) -> Optional[Dict[str, Any]]:
|
||
if not isinstance(schema, dict):
|
||
return None
|
||
properties = schema.get("properties") if isinstance(schema.get("properties"), dict) else {}
|
||
prop_schema = properties.get(_schema_property_name(schema, field_name))
|
||
if isinstance(prop_schema, dict):
|
||
return prop_schema
|
||
return None
|
||
|
||
|
||
def _tool_parameters_schema(tool_def: Any) -> Dict[str, Any]:
|
||
if not isinstance(tool_def, dict):
|
||
return {}
|
||
function_data = tool_def.get("function") if isinstance(tool_def.get("function"), dict) else {}
|
||
parameters = function_data.get("parameters")
|
||
if isinstance(parameters, dict):
|
||
return parameters
|
||
parameters = tool_def.get("parameters")
|
||
return parameters if isinstance(parameters, dict) else {}
|
||
|
||
|
||
def _parse_xml_scalar_value(
|
||
raw_text: str,
|
||
param_name: str = "",
|
||
param_schema: Optional[Dict[str, Any]] = None,
|
||
) -> Any:
|
||
# 修复(6):BeautifulSoup(html.parser) 已把实体解码过一次(已验证 &lt; -> <),
|
||
# 这里不再二次 html.unescape,否则参数里的字面 "<" 样式文本会被改写成 "<"。
|
||
text = str(raw_text or "")
|
||
stripped = text.strip()
|
||
if not stripped:
|
||
return ""
|
||
|
||
normalized_name = str(param_name or "").strip().lower()
|
||
if not _schema_prefers_string(param_schema) and normalized_name not in _TOOL_XML_STRING_PARAM_NAMES:
|
||
try:
|
||
parsed = json.loads(stripped)
|
||
except Exception:
|
||
repaired = _repair_json_like_argument_string(stripped)
|
||
if repaired != stripped:
|
||
try:
|
||
parsed = json.loads(repaired)
|
||
except Exception:
|
||
parsed = None
|
||
else:
|
||
if not isinstance(parsed, str):
|
||
return parsed
|
||
parsed = None
|
||
else:
|
||
if not isinstance(parsed, str):
|
||
return parsed
|
||
|
||
return stripped
|
||
|
||
|
||
def _parse_xml_element_value(
|
||
element: Tag,
|
||
field_name: str = "",
|
||
param_schema: Optional[Dict[str, Any]] = None,
|
||
) -> Any:
|
||
children = element.find_all(recursive=False)
|
||
normalized_name = str(field_name or "").strip().lower()
|
||
if not children and not element.get_text(strip=True):
|
||
empty_value = _empty_xml_value_for_schema(param_schema)
|
||
if empty_value is not None:
|
||
return empty_value
|
||
if _schema_prefers_string(param_schema) or normalized_name in _TOOL_XML_STRING_PARAM_NAMES:
|
||
# 修复(6):NavigableString 已被 BS4 解码一次,直接取值;嵌套 Tag 用 formatter=None
|
||
# 序列化避免输出时再转义,配合 _parse_xml_scalar_value 去掉二次 unescape 后语义一致。
|
||
inner_markup = "".join(
|
||
str(item)
|
||
if isinstance(item, NavigableString)
|
||
else item.decode(formatter=None)
|
||
for item in element.contents
|
||
)
|
||
return _parse_xml_scalar_value(inner_markup, field_name, param_schema)
|
||
if not children:
|
||
return _parse_xml_scalar_value(element.get_text(), field_name, param_schema)
|
||
|
||
result: Dict[str, Any] = {}
|
||
for child in children:
|
||
child_name = _schema_property_name(param_schema, _xml_local_name(child.name))
|
||
if not child_name:
|
||
continue
|
||
child_schema = _schema_property_schema(param_schema, child_name)
|
||
_append_xml_value(
|
||
result,
|
||
child_name,
|
||
_parse_xml_element_value(child, child_name, child_schema),
|
||
)
|
||
|
||
item_key = next((key for key in result if key.lower() == "item"), "")
|
||
if len(result) == 1 and item_key:
|
||
items = result[item_key]
|
||
return items if isinstance(items, list) else [items]
|
||
|
||
text_parts = [
|
||
str(item)
|
||
for item in element.contents
|
||
if isinstance(item, NavigableString) and str(item).strip()
|
||
]
|
||
if text_parts:
|
||
result["_text"] = _parse_xml_scalar_value("".join(text_parts), field_name, param_schema)
|
||
return result
|
||
|
||
|
||
def _parse_xml_invoke_arguments(
|
||
invoke: ET.Element,
|
||
tool_def: Optional[Dict[str, Any]] = None,
|
||
) -> Optional[Dict[str, Any]]:
|
||
if str(invoke.text or "").strip():
|
||
return None
|
||
children = list(invoke)
|
||
if len(children) != 1:
|
||
return None
|
||
arguments_element = children[0]
|
||
if _xml_local_name(arguments_element.tag).lower() != "arguments":
|
||
return None
|
||
if set(arguments_element.attrib) != {"encoding"}:
|
||
return None
|
||
if str(arguments_element.attrib.get("encoding") or "").strip().lower() != "json":
|
||
return None
|
||
if list(arguments_element) or str(arguments_element.tail or "").strip():
|
||
return None
|
||
try:
|
||
payload = json.loads(arguments_element.text or "")
|
||
except Exception:
|
||
return None
|
||
if not isinstance(payload, dict) or _contains_unicode_surrogate(payload):
|
||
return None
|
||
return payload
|
||
|
||
|
||
def _parse_wrapped_xml_tool_calls(
|
||
text: str,
|
||
allowed_tools: Dict[str, Dict[str, Any]],
|
||
) -> List[Dict[str, Any]]:
|
||
normalized = _normalize_tool_xml_markup(text)
|
||
try:
|
||
root = _safe_xml_fromstring(normalized)
|
||
except ValueError:
|
||
return []
|
||
|
||
if _xml_local_name(root.tag).lower() not in {
|
||
_PREFERRED_XML_WRAPPER_TAG,
|
||
_LEGACY_XML_WRAPPER_TAG,
|
||
}:
|
||
return []
|
||
if root.attrib or str(root.text or "").strip():
|
||
return []
|
||
|
||
tool_calls: List[Dict[str, Any]] = []
|
||
for child in list(root):
|
||
if _xml_local_name(child.tag).lower() not in {
|
||
_PREFERRED_XML_CALL_TAG,
|
||
_LEGACY_XML_CALL_TAG,
|
||
"tool_call",
|
||
}:
|
||
return []
|
||
if set(child.attrib) != {"name"} or str(child.tail or "").strip():
|
||
return []
|
||
raw_name = str(child.attrib.get("name", "") or "").strip()
|
||
name = _resolve_tool_name(raw_name, allowed_tools)
|
||
if not name:
|
||
return []
|
||
arguments = _parse_xml_invoke_arguments(child, allowed_tools.get(name))
|
||
if arguments is None:
|
||
return []
|
||
tool_calls.append(
|
||
{
|
||
"id": _new_tool_call_id(),
|
||
"type": "function",
|
||
"function": {
|
||
"name": name,
|
||
"arguments": json.dumps(arguments, ensure_ascii=False),
|
||
},
|
||
}
|
||
)
|
||
return tool_calls
|
||
|
||
|
||
def _try_parse_xml_tool_calls(
|
||
text: str,
|
||
allowed_tools: Dict[str, Dict[str, Any]],
|
||
) -> Optional[Dict[str, Any]]:
|
||
raw = str(text or "")
|
||
wrapper_spans = _find_tool_xml_wrapper_spans(raw)
|
||
if len(wrapper_spans) != 1:
|
||
return None
|
||
start, end, block = wrapper_spans[0]
|
||
tool_calls = _parse_wrapped_xml_tool_calls(block, allowed_tools)
|
||
if not tool_calls:
|
||
return None
|
||
|
||
visible_text = _replace_unicode_surrogates(
|
||
_extract_text_excluding_spans(raw, [(start, end)]).strip()
|
||
)
|
||
|
||
return {
|
||
"mode": "tool_calls",
|
||
"content": visible_text or None,
|
||
"tool_calls": tool_calls,
|
||
}
|
||
|
||
|
||
def _parse_xml_attrs(raw_attrs: str) -> Dict[str, Any]:
|
||
attrs: Dict[str, Any] = {}
|
||
attr_pattern = re.compile(
|
||
r"""([A-Za-z_][A-Za-z0-9_.:-]*)\s*=\s*(['"])(.*?)\2""",
|
||
flags=re.DOTALL,
|
||
)
|
||
for match in attr_pattern.finditer(raw_attrs or ""):
|
||
key = match.group(1)
|
||
value = match.group(3)
|
||
attrs[key] = html.unescape(value)
|
||
return attrs
|