Extract tool_call blocks. Returns (clean_text, tool_calls_list).
(text: str)
| 402 | |
| 403 | |
| 404 | def parse_tool_calls(text: str) -> tuple: |
| 405 | """Extract tool_call blocks. Returns (clean_text, tool_calls_list).""" |
| 406 | tool_calls = [] |
| 407 | pattern = r'```tool_call\s*\n(.*?)\n```' |
| 408 | for match in re.findall(pattern, text, re.DOTALL): |
| 409 | try: |
| 410 | data = json.loads(match.strip()) |
| 411 | tool_calls.append({ |
| 412 | "id": f"call_{uuid.uuid4().hex[:8]}", |
| 413 | "type": "function", |
| 414 | "function": { |
| 415 | "name": data["name"], |
| 416 | "arguments": json.dumps(data.get("arguments", {}), ensure_ascii=False), |
| 417 | }, |
| 418 | }) |
| 419 | except (json.JSONDecodeError, KeyError): |
| 420 | pass |
| 421 | clean = re.sub(pattern, '', text, flags=re.DOTALL).strip() |
| 422 | return clean, tool_calls |
| 423 | |
| 424 | |
| 425 | # ─── HTTP Handler ──────────────────────────────────────────────────────────── |