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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
"""Text-based tool calling extraction and system prompt formatting.
Allows smaller or local models (e.g. qwen2.5-coder:3b, llama-3, phi-3) whose
chat templates do not emit structured OpenAI API tool_calls to invoke tools
reliably via clear, structured markdown or XML text blocks.
"""
from __future__ import annotations
import json
import re
import uuid
from typing import Any, Dict, List, Optional, Set, Tuple
from .models import ToolCall, ToolDefinition
TOOL_PROMPT_INSTRUCTIONS = """
## Tool Calling Format
You can invoke workspace tools directly. If your environment does not support native API tool calling, you MUST output your tool invocation using this exact, recognizable format:
### tool
*<tool_name>
```json
{
"<argument_name>": "<argument_value>"
}
```
Or using standard tool call tags:
<tool_call>
{"name": "<tool_name>", "arguments": { ... }}
</tool_call>
When a tool finishes running, the system will return the output in this format:
### tool_result
*<tool_name>
<result>
You can then inspect the output and proceed to the next step or give your final answer.
"""
# Aliases for common shorthand operations
TOOL_ALIASES: Dict[str, Tuple[str, Optional[Dict[str, Any]]]] = {
"read": ("filesystem", {"operation": "read"}),
"write": ("filesystem", {"operation": "write"}),
"edit": ("filesystem", {"operation": "edit"}),
"ls": ("filesystem", {"operation": "ls"}),
"mkdir": ("filesystem", {"operation": "mkdir"}),
"bash": ("shell", None),
"sh": ("shell", None),
"run": ("shell", None),
}
def extract_text_tool_calls(
content: str,
tools: Optional[List[ToolDefinition]] = None,
) -> Tuple[List[ToolCall], str]:
"""Extract tool calls from text content.
Returns:
(tool_calls, cleaned_content)
"""
if not content:
return [], ""
known_tools: Set[str] = {t.name for t in tools} if tools else set()
tool_calls: List[ToolCall] = []
cleaned: str = content
# 1. User/Markdown format:
# ### tool
# *<name>
# ```json
# { ... }
# ```
pattern_md = re.compile(
r"###\s*tool\s*\n\*\s*([a-zA-Z0-9_]+)\s*\n(?:```(?:json)?\s*)?(\{[\s\S]*?\})(?:\s*```)?",
re.DOTALL | re.IGNORECASE,
)
for m in pattern_md.finditer(content):
raw_name = m.group(1).strip()
raw_json = m.group(2).strip()
name, default_args = _resolve_tool_name(raw_name, known_tools)
if name:
try:
args = json.loads(raw_json)
if not isinstance(args, dict):
args = {}
if default_args:
args = {**default_args, **args}
tool_calls.append(
ToolCall(
id=f"call_{uuid.uuid4().hex[:12]}",
name=name,
arguments=args,
)
)
cleaned = cleaned.replace(m.group(0), "")
except Exception:
pass
# 2. XML tag format:
# <tool_call>
# {"name": "...", "arguments": { ... }}
# </tool_call>
pattern_xml = re.compile(
r"<tool_call>\s*(?:```(?:json)?\s*)?(\{[\s\S]*?\})(?:\s*```)?\s*</tool_call>",
re.DOTALL | re.IGNORECASE,
)
for m in pattern_xml.finditer(content):
raw_json = m.group(1).strip()
try:
data = json.loads(raw_json)
if isinstance(data, dict):
raw_name = data.get("name") or data.get("function")
if isinstance(raw_name, dict):
raw_name = raw_name.get("name")
if isinstance(raw_name, str):
name, default_args = _resolve_tool_name(raw_name, known_tools)
if name:
args = data.get("arguments", {})
if isinstance(args, str):
try:
args = json.loads(args)
except Exception:
args = {}
if not isinstance(args, dict):
args = {}
if default_args:
args = {**default_args, **args}
tool_calls.append(
ToolCall(
id=f"call_{uuid.uuid4().hex[:12]}",
name=name,
arguments=args,
)
)
cleaned = cleaned.replace(m.group(0), "")
except Exception:
pass
# 3. Fenced code block or bare JSON:
# {"name": "filesystem", "arguments": { ... }}
if not tool_calls:
text_strip = content.strip()
# Check if entire content is bare JSON or fenced JSON
json_candidate = text_strip
if json_candidate.startswith("```"):
lines = json_candidate.split("\n")
if len(lines) >= 3 and lines[-1].strip().startswith("```"):
json_candidate = "\n".join(lines[1:-1]).strip()
if json_candidate.startswith("{") and json_candidate.endswith("}"):
try:
parsed = json.loads(json_candidate)
if isinstance(parsed, dict):
candidate = parsed.get("tool_call") if isinstance(parsed.get("tool_call"), dict) else parsed
raw_name = candidate.get("name") or candidate.get("function")
if isinstance(raw_name, dict):
raw_name = raw_name.get("name")
if isinstance(raw_name, str):
name, default_args = _resolve_tool_name(raw_name, known_tools)
if name:
args = candidate.get("arguments", {})
if isinstance(args, str):
try:
args = json.loads(args)
except Exception:
args = {}
if not isinstance(args, dict):
args = {}
if default_args:
args = {**default_args, **args}
tool_calls.append(
ToolCall(
id=f"call_{uuid.uuid4().hex[:12]}",
name=name,
arguments=args,
)
)
cleaned = ""
except Exception:
pass
return tool_calls, cleaned.strip()
def _resolve_tool_name(
raw_name: str,
known_tools: Set[str],
) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
"""Resolve tool name against known registered tools and shorthand aliases."""
name_clean = raw_name.strip()
if name_clean in known_tools:
return name_clean, None
# Check lowercase alias
alias_match = TOOL_ALIASES.get(name_clean.lower())
if alias_match:
target, def_args = alias_match
if target in known_tools:
return target, def_args
return None, None
__all__ = ["extract_text_tool_calls", "TOOL_PROMPT_INSTRUCTIONS", "TOOL_ALIASES"]
|