| 1 | from graphrag_sdk.agents.agent import AgentResponseCode |
| 2 | from graphrag_sdk.orchestrator.step_result import StepResult |
| 3 | from graphrag_sdk.orchestrator.step import PlanStep, StepBlockType |
| 4 | from graphrag_sdk.orchestrator.orchestrator_runner import OrchestratorRunner |
| 5 | import logging |
| 6 | |
| 7 | logger = logging.getLogger(__name__) |
| 8 | |
| 9 | |
| 10 | class AgentStepResult(StepResult): |
| 11 | |
| 12 | def __init__(self, response_code: AgentResponseCode, payload: dict): |
| 13 | self.response_code = response_code |
| 14 | self.payload = payload |
| 15 | |
| 16 | def to_json(self) -> dict: |
| 17 | return { |
| 18 | "response_code": self.response_code, |
| 19 | "payload": self.payload, |
| 20 | } |
| 21 | |
| 22 | @staticmethod |
| 23 | def from_json(json: dict) -> "AgentStepResult": |
| 24 | return AgentStepResult( |
| 25 | AgentResponseCode.from_str(json["response_code"]), |
| 26 | json["payload"], |
| 27 | ) |
| 28 | |
| 29 | def __str__(self) -> str: |
| 30 | return f"AgentStepResult(response_code={self.response_code}, payload={self.payload})" |
| 31 | |
| 32 | def __repr__(self) -> str: |
| 33 | return str(self) |
| 34 | |
| 35 | @property |
| 36 | def output(self) -> str: |
| 37 | return self.payload.get("output", str(self)) |
| 38 | |
| 39 | |
| 40 | class AgentProperties: |
| 41 | |
| 42 | def __init__(self, agent_id: str, session_id: str = None, payload: dict = None): |
| 43 | self.agent_id = agent_id |
| 44 | self.session_id = session_id |
| 45 | self.payload = payload |
| 46 | |
| 47 | @staticmethod |
| 48 | def from_json(json: dict) -> "AgentProperties": |
| 49 | return AgentProperties( |
| 50 | json["agent_id"], |
| 51 | json.get("session_id", None), |
| 52 | json.get("payload", None), |
| 53 | ) |
| 54 | |
| 55 | def to_json(self) -> dict: |
| 56 | return { |
| 57 | "agent_id": self.agent_id, |
| 58 | "session_id": self.session_id, |
| 59 | "payload": self.payload, |
| 60 | } |
| 61 | |
| 62 | def __str__(self) -> str: |
| 63 | return f"AgentProperties(agent_id={self.agent_id}, session_id={self.session_id}, payload={self.payload})" |
| 64 | |
| 65 | def __repr__(self) -> str: |
| 66 | return str(self) |
| 67 | |
| 68 | |
| 69 | class AgentStep(PlanStep): |
| 70 | |
| 71 | def __init__(self, id: str, properties: AgentProperties): |
| 72 | self._id = id |
| 73 | self._properties = properties |
| 74 | |
| 75 | @property |
| 76 | def id(self) -> str: |
| 77 | return self._id |
| 78 | |
| 79 | @property |
| 80 | def block(self) -> StepBlockType: |
| 81 | return StepBlockType.AGENT |
| 82 | |
| 83 | @property |
| 84 | def properties(self) -> AgentProperties: |
| 85 | return self._properties |
| 86 | |
| 87 | def to_json(self) -> dict: |
| 88 | return { |
| 89 | "id": self.id, |
| 90 | "block": self.block, |
| 91 | "properties": self.properties.to_json(), |
| 92 | } |
| 93 | |
| 94 | @staticmethod |
| 95 | def from_json(json: dict) -> "AgentStep": |
| 96 | return AgentStep(json["id"], AgentProperties.from_json(json["properties"])) |
| 97 | |
| 98 | def __str__(self) -> str: |
| 99 | return f"AgentStep(id={self.id}, properties={self.properties})" |
| 100 | |
| 101 | def __repr__(self) -> str: |
| 102 | return str(self) |
| 103 | |
| 104 | def run( |
| 105 | self, |
| 106 | runner: "OrchestratorRunner", |
| 107 | config: dict = None, |
| 108 | ) -> AgentStepResult: |
| 109 | logger.info(f"Running agent {self.properties.agent_id}, step: {self.id}, payload: {self.properties.payload}") |
| 110 | session = ( |
| 111 | runner.get_session(self.properties.session_id) |
| 112 | if self.properties.session_id |
| 113 | else None |
| 114 | ) |
| 115 | agent = runner.get_agent(self.properties.agent_id) |
| 116 | if agent is None: |
| 117 | raise ValueError(f"Agent with id {self.properties.agent_id} not found") |
| 118 | |
| 119 | (response, chat_session) = agent.run(self.properties.payload, session) |
| 120 | runner.set_session(self.properties.session_id, chat_session) |
| 121 | logger.debug(f"Agent response: {response}") |
| 122 | return AgentStepResult(AgentResponseCode.AGENT_RESPONSE, {"output": response}) |