Upload mythos/pantheon.py
Browse files- mythos/pantheon.py +96 -0
mythos/pantheon.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Pantheon — a collection of agents with defined relationships and workflows."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
from pydantic import BaseModel, Field
|
| 8 |
+
|
| 9 |
+
from .agent import Agent
|
| 10 |
+
from .message import Message, MessageType
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Relationship(BaseModel):
|
| 14 |
+
"""A relationship between two agents in a Pantheon."""
|
| 15 |
+
|
| 16 |
+
source: str # Agent name
|
| 17 |
+
target: str # Agent name
|
| 18 |
+
relation: str # e.g., "reports_to", "collaborates_with", "verifies"
|
| 19 |
+
weight: float = 1.0
|
| 20 |
+
bidirectional: bool = False
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class Pantheon(BaseModel):
|
| 24 |
+
"""A collection of agents with relationships and shared context."""
|
| 25 |
+
|
| 26 |
+
name: str = "default"
|
| 27 |
+
agents: dict[str, Agent] = Field(default_factory=dict)
|
| 28 |
+
relationships: list[Relationship] = Field(default_factory=list)
|
| 29 |
+
shared_memory: dict[str, str] = Field(default_factory=dict)
|
| 30 |
+
|
| 31 |
+
def add_agent(self, agent: Agent) -> None:
|
| 32 |
+
"""Add an agent to the pantheon."""
|
| 33 |
+
self.agents[agent.name] = agent
|
| 34 |
+
|
| 35 |
+
def remove_agent(self, name: str) -> None:
|
| 36 |
+
"""Remove an agent from the pantheon."""
|
| 37 |
+
self.agents.pop(name, None)
|
| 38 |
+
|
| 39 |
+
def get_agent(self, name: str) -> Optional[Agent]:
|
| 40 |
+
"""Get an agent by name."""
|
| 41 |
+
return self.agents.get(name)
|
| 42 |
+
|
| 43 |
+
def add_relationship(self, relationship: Relationship) -> None:
|
| 44 |
+
"""Add a relationship between agents."""
|
| 45 |
+
self.relationships.append(relationship)
|
| 46 |
+
|
| 47 |
+
def get_related(self, agent_name: str, relation: Optional[str] = None) -> list[str]:
|
| 48 |
+
"""Get agents related to the given agent."""
|
| 49 |
+
related = []
|
| 50 |
+
for rel in self.relationships:
|
| 51 |
+
if rel.source == agent_name:
|
| 52 |
+
if relation is None or rel.relation == relation:
|
| 53 |
+
related.append(rel.target)
|
| 54 |
+
if rel.bidirectional and rel.target == agent_name:
|
| 55 |
+
if relation is None or rel.relation == relation:
|
| 56 |
+
related.append(rel.source)
|
| 57 |
+
return related
|
| 58 |
+
|
| 59 |
+
async def broadcast(self, content: str, sender: str = "system") -> list[Message]:
|
| 60 |
+
"""Broadcast a message to all agents."""
|
| 61 |
+
msg = Message(
|
| 62 |
+
type=MessageType.BROADCAST,
|
| 63 |
+
sender=sender,
|
| 64 |
+
recipient="broadcast",
|
| 65 |
+
content=content,
|
| 66 |
+
)
|
| 67 |
+
responses = []
|
| 68 |
+
for agent in self.agents.values():
|
| 69 |
+
response = await agent.handle(msg)
|
| 70 |
+
responses.append(response)
|
| 71 |
+
return responses
|
| 72 |
+
|
| 73 |
+
async def send(self, sender: str, recipient: str, content: str) -> Message:
|
| 74 |
+
"""Send a direct message from one agent to another."""
|
| 75 |
+
sender_agent = self.agents.get(sender)
|
| 76 |
+
recipient_agent = self.agents.get(recipient)
|
| 77 |
+
if not sender_agent or not recipient_agent:
|
| 78 |
+
raise ValueError(f"Unknown agent: {sender} or {recipient}")
|
| 79 |
+
|
| 80 |
+
msg = Message(
|
| 81 |
+
type=MessageType.TASK,
|
| 82 |
+
sender=sender,
|
| 83 |
+
recipient=recipient,
|
| 84 |
+
content=content,
|
| 85 |
+
)
|
| 86 |
+
return await recipient_agent.handle(msg)
|
| 87 |
+
|
| 88 |
+
def list_agents(self) -> list[str]:
|
| 89 |
+
"""List all agent names."""
|
| 90 |
+
return list(self.agents.keys())
|
| 91 |
+
|
| 92 |
+
def __len__(self) -> int:
|
| 93 |
+
return len(self.agents)
|
| 94 |
+
|
| 95 |
+
def __str__(self) -> str:
|
| 96 |
+
return f"Pantheon({self.name}, agents={list(self.agents.keys())})"
|