File size: 2,032 Bytes
aedcabb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json, re

def detect_all_tool_calls(response_text: str) -> list:
    tool_calls = []
    pattern0 = re.compile(r'<tool_calls?\s+name=["\']([^"\']+)["\']\s*>(.*?)(?:</tool_calls?>|$)', 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 = """
<tool_call name="Bash">
<command>echo "=== Shell/Command Execution ===" && echo "OK: Shell execution works" && echo "" && echo "=== System Info ===" && uname -a && echo "Current user: $(whoami)" && echo "Working dir: $(pwd)"</command>
<description>Test shell execution tool</description>
</tool_call>
<tool_call name="Write">
<file_path>/tmp/claude_tool_test.txt</file_path>
<content>Tool test file - created at 2026-07-22
This tests the Write tool functionality.</content>
</tool_call>
"""

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))