import json, re
def detect_all_tool_calls(response_text: str) -> list:
tool_calls = []
pattern0 = re.compile(r'(.*?)(?:|$)', re.DOTALL | re.IGNORECASE)
matches0 = pattern0.findall(response_text)
for name, body in matches0:
name = name.strip()
body_str = body.strip()
args = {}
parsed_json = None
try:
parsed_json = json.loads(body_str)
except Exception:
start = body_str.find("{")
end = body_str.rfind("}")
if start != -1 and end != -1:
try:
parsed_json = json.loads(body_str[start:end+1])
except Exception:
pass
if parsed_json and isinstance(parsed_json, dict):
args = parsed_json
else:
child_pattern = re.compile(r'<([a-zA-Z0-9_-]+)[^>]*>(.*?)(?:\1>|$)', re.DOTALL)
child_matches = child_pattern.findall(body_str)
for tag, val in child_matches:
val_clean = val.strip()
val_clean = re.sub(r'?[a-zA-Z0-9_-]+[^>]*>$', '', val_clean).strip()
args[tag.strip()] = val_clean
if name:
tool_calls.append((name, args))
return tool_calls
text = """
echo "=== Shell/Command Execution ===" && echo "OK: Shell execution works" && echo "" && echo "=== System Info ===" && uname -a && echo "Current user: $(whoami)" && echo "Working dir: $(pwd)"
Test shell execution tool
/tmp/claude_tool_test.txt
Tool test file - created at 2026-07-22
This tests the Write tool functionality.
"""
calls = detect_all_tool_calls(text)
print("Extracted tool calls:")
for name, args in calls:
print("NAME:", name)
print("ARGS:", json.dumps(args, indent=2))