MCPcopy Index your code
hub / github.com/shareAI-lab/learn-claude-code / _loop

Method _loop

agents/s_full.py:441–531  ·  view source on GitHub ↗
(self, name: str, role: str, prompt: str)

Source from the content-addressed store, hash-verified

439 self._save()
440
441 def _loop(self, name: str, role: str, prompt: str):
442 team_name = self.config["team_name"]
443 sys_prompt = (f"You are '{name}', role: {role}, team: {team_name}, at {WORKDIR}. "
444 f"Use idle when done with current work. You may auto-claim tasks.")
445 messages = [{"role": "user", "content": prompt}]
446 tools = [
447 {"name": "bash", "description": "Run command.", "input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
448 {"name": "read_file", "description": "Read file.", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}},
449 {"name": "write_file", "description": "Write file.", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
450 {"name": "edit_file", "description": "Edit file.", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
451 {"name": "send_message", "description": "Send message.", "input_schema": {"type": "object", "properties": {"to": {"type": "string"}, "content": {"type": "string"}}, "required": ["to", "content"]}},
452 {"name": "idle", "description": "Signal no more work.", "input_schema": {"type": "object", "properties": {}}},
453 {"name": "claim_task", "description": "Claim task by ID.", "input_schema": {"type": "object", "properties": {"task_id": {"type": "integer"}}, "required": ["task_id"]}},
454 ]
455 while True:
456 # -- WORK PHASE --
457 for _ in range(50):
458 inbox = self.bus.read_inbox(name)
459 for msg in inbox:
460 if msg.get("type") == "shutdown_request":
461 self._set_status(name, "shutdown")
462 return
463 messages.append({"role": "user", "content": json.dumps(msg)})
464 try:
465 response = client.messages.create(
466 model=MODEL, system=sys_prompt, messages=messages,
467 tools=tools, max_tokens=8000)
468 except Exception:
469 self._set_status(name, "shutdown")
470 return
471 messages.append({"role": "assistant", "content": response.content})
472 if response.stop_reason != "tool_use":
473 break
474 results = []
475 idle_requested = False
476 for block in response.content:
477 if block.type == "tool_use":
478 if block.name == "idle":
479 idle_requested = True
480 output = "Entering idle phase."
481 elif block.name == "claim_task":
482 output = self.task_mgr.claim(block.input["task_id"], name)
483 elif block.name == "send_message":
484 output = self.bus.send(name, block.input["to"], block.input["content"])
485 else:
486 dispatch = {"bash": lambda **kw: run_bash(kw["command"]),
487 "read_file": lambda **kw: run_read(kw["path"]),
488 "write_file": lambda **kw: run_write(kw["path"], kw["content"]),
489 "edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"])}
490 output = dispatch.get(block.name, lambda **kw: "Unknown")(**block.input)
491 print(f" [{name}] {block.name}: {str(output)[:120]}")
492 results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)})
493 messages.append({"role": "user", "content": results})
494 if idle_requested:
495 break
496 # -- IDLE PHASE: poll for messages and unclaimed tasks --
497 self._set_status(name, "idle")
498 resume = False

Callers

nothing calls this directly

Calls 10

_set_statusMethod · 0.95
claimMethod · 0.80
run_bashFunction · 0.70
run_readFunction · 0.70
run_writeFunction · 0.70
run_editFunction · 0.70
read_inboxMethod · 0.45
getMethod · 0.45
createMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected