Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions agents/s09_agent_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,16 @@ def send(self, sender: str, to: str, content: str,
f.write(json.dumps(msg) + "\n")
return f"Sent {msg_type} to {to}"

def read_inbox(self, name: str) -> list:
def read_inbox(self, name: str, clear: bool = True) -> list:
inbox_path = self.dir / f"{name}.jsonl"
if not inbox_path.exists():
return []
messages = []
for line in inbox_path.read_text().strip().splitlines():
if line:
messages.append(json.loads(line))
inbox_path.write_text("")
if clear:
inbox_path.write_text("")
return messages

def broadcast(self, sender: str, content: str, teammates: list) -> str:
Expand Down Expand Up @@ -348,7 +349,7 @@ def agent_loop(messages: list):
if inbox:
messages.append({
"role": "user",
"content": f"<inbox>{json.dumps(inbox, indent=2)}</inbox>",
"content": f"<inbox>{json.dumps(inbox)}</inbox>",
})
response = client.messages.create(
model=MODEL,
Expand Down Expand Up @@ -391,7 +392,7 @@ def agent_loop(messages: list):
print(TEAM.list_all())
continue
if query.strip() == "/inbox":
print(json.dumps(BUS.read_inbox("lead"), indent=2))
print(json.dumps(BUS.read_inbox("lead", False), indent=2))
continue
history.append({"role": "user", "content": query})
agent_loop(history)
Expand Down
9 changes: 5 additions & 4 deletions agents/s10_team_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ def send(self, sender: str, to: str, content: str,
f.write(json.dumps(msg) + "\n")
return f"Sent {msg_type} to {to}"

def read_inbox(self, name: str) -> list:
def read_inbox(self, name: str, clear: bool = True) -> list:
inbox_path = self.dir / f"{name}.jsonl"
if not inbox_path.exists():
return []
messages = []
for line in inbox_path.read_text().strip().splitlines():
if line:
messages.append(json.loads(line))
inbox_path.write_text("")
if clear:
inbox_path.write_text("")
return messages

def broadcast(self, sender: str, content: str, teammates: list) -> str:
Expand Down Expand Up @@ -429,7 +430,7 @@ def agent_loop(messages: list):
if inbox:
messages.append({
"role": "user",
"content": f"<inbox>{json.dumps(inbox, indent=2)}</inbox>",
"content": f"<inbox>{json.dumps(inbox)}</inbox>",
})
response = client.messages.create(
model=MODEL,
Expand Down Expand Up @@ -472,7 +473,7 @@ def agent_loop(messages: list):
print(TEAM.list_all())
continue
if query.strip() == "/inbox":
print(json.dumps(BUS.read_inbox("lead"), indent=2))
print(json.dumps(BUS.read_inbox("lead", False), indent=2))
continue
history.append({"role": "user", "content": query})
agent_loop(history)
Expand Down
15 changes: 7 additions & 8 deletions agents/s11_autonomous_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ def send(self, sender: str, to: str, content: str,
f.write(json.dumps(msg) + "\n")
return f"Sent {msg_type} to {to}"

def read_inbox(self, name: str) -> list:
def read_inbox(self, name: str, clear: bool = True) -> list:
inbox_path = self.dir / f"{name}.jsonl"
if not inbox_path.exists():
return []
messages = []
for line in inbox_path.read_text().strip().splitlines():
if line:
messages.append(json.loads(line))
inbox_path.write_text("")
if clear:
inbox_path.write_text("")
return messages

def broadcast(self, sender: str, content: str, teammates: list) -> str:
Expand Down Expand Up @@ -142,11 +143,9 @@ def claim_task(task_id: int, owner: str) -> str:
if not path.exists():
return f"Error: Task {task_id} not found"
task = json.loads(path.read_text())
if task.get("owner"):
existing_owner = task.get("owner") or "someone else"
if existing_owner := task.get("owner"):
return f"Error: Task {task_id} has already been claimed by {existing_owner}"
if task.get("status") != "pending":
status = task.get("status")
if (status := task.get("status")) != "pending":
return f"Error: Task {task_id} cannot be claimed because its status is '{status}'"
if task.get("blockedBy"):
return f"Error: Task {task_id} is blocked by other task(s) and cannot be claimed yet"
Expand Down Expand Up @@ -523,7 +522,7 @@ def agent_loop(messages: list):
if inbox:
messages.append({
"role": "user",
"content": f"<inbox>{json.dumps(inbox, indent=2)}</inbox>",
"content": f"<inbox>{json.dumps(inbox)}</inbox>",
})
response = client.messages.create(
model=MODEL,
Expand Down Expand Up @@ -566,7 +565,7 @@ def agent_loop(messages: list):
print(TEAM.list_all())
continue
if query.strip() == "/inbox":
print(json.dumps(BUS.read_inbox("lead"), indent=2))
print(json.dumps(BUS.read_inbox("lead", False), indent=2))
continue
if query.strip() == "/tasks":
TASKS_DIR.mkdir(exist_ok=True)
Expand Down