(description: str)
| 227 | "edit_file": run_edit, "glob": run_glob} |
| 228 | |
| 229 | def spawn_subagent(description: str) -> str: |
| 230 | print(f"\n\033[35m[Subagent spawned]\033[0m") |
| 231 | messages = [{"role": "user", "content": description}] |
| 232 | for _ in range(30): |
| 233 | response = client.messages.create(model=MODEL, system=SUB_SYSTEM, |
| 234 | messages=messages, tools=SUB_TOOLS, max_tokens=8000) |
| 235 | messages.append({"role": "assistant", "content": response.content}) |
| 236 | if response.stop_reason != "tool_use": |
| 237 | break |
| 238 | results = [] |
| 239 | for block in response.content: |
| 240 | if block.type == "tool_use": |
| 241 | blocked = trigger_hooks("PreToolUse", block) |
| 242 | if blocked: |
| 243 | results.append({"type": "tool_result", "tool_use_id": block.id, |
| 244 | "content": str(blocked)}) |
| 245 | continue |
| 246 | handler = SUB_HANDLERS.get(block.name) |
| 247 | output = handler(**block.input) if handler else f"Unknown: {block.name}" |
| 248 | trigger_hooks("PostToolUse", block, output) |
| 249 | print(f" \033[90m[sub] {block.name}: {str(output)[:100]}\033[0m") |
| 250 | results.append({"type": "tool_result", "tool_use_id": block.id, "content": output}) |
| 251 | messages.append({"role": "user", "content": results}) |
| 252 | result = extract_text(messages[-1]["content"]) |
| 253 | if not result: |
| 254 | for msg in reversed(messages): |
| 255 | if msg["role"] == "assistant": |
| 256 | result = extract_text(msg["content"]) |
| 257 | if result: |
| 258 | break |
| 259 | if not result: |
| 260 | result = "Subagent stopped after 30 turns without final answer." |
| 261 | print(f"\033[35m[Subagent done]\033[0m") |
| 262 | return result |
| 263 | |
| 264 | |
| 265 | # ═══════════════════════════════════════════════════════════ |
nothing calls this directly
no test coverage detected