| 1 | from graphrag_sdk.orchestrator.step import PlanStep |
| 2 | from json import loads |
| 3 | |
| 4 | |
| 5 | class OrchestratorDecisionCode: |
| 6 | CONTINUE = "continue" |
| 7 | END = "end" |
| 8 | UPDATE_STEP = "update_step" |
| 9 | |
| 10 | @staticmethod |
| 11 | def from_str(code: str) -> str: |
| 12 | if code == OrchestratorDecisionCode.CONTINUE: |
| 13 | return OrchestratorDecisionCode.CONTINUE |
| 14 | elif code == OrchestratorDecisionCode.END: |
| 15 | return OrchestratorDecisionCode.END |
| 16 | elif code == OrchestratorDecisionCode.UPDATE_STEP: |
| 17 | return OrchestratorDecisionCode.UPDATE_STEP |
| 18 | else: |
| 19 | raise ValueError(f"Unknown code: {code}") |
| 20 | |
| 21 | |
| 22 | class OrchestratorDecision: |
| 23 | def __init__( |
| 24 | self, code: OrchestratorDecisionCode, new_step: PlanStep | None = None |
| 25 | ): |
| 26 | self.code = code |
| 27 | self.new_step = new_step |
| 28 | |
| 29 | def to_json(self) -> dict: |
| 30 | return { |
| 31 | "code": self.code, |
| 32 | "new_step": self.new_step.to_json() if self.new_step else None, |
| 33 | } |
| 34 | |
| 35 | @staticmethod |
| 36 | def from_json(json: dict | str) -> "OrchestratorDecision": |
| 37 | json = json if isinstance(json, dict) else loads(json) |
| 38 | return OrchestratorDecision( |
| 39 | OrchestratorDecisionCode.from_str(json["code"]), |
| 40 | ( |
| 41 | PlanStep.from_json(json["new_step"]) |
| 42 | if "new_step" in json and json["new_step"] |
| 43 | else None |
| 44 | ), |
| 45 | ) |
| 46 | |
| 47 | def __str__(self) -> str: |
| 48 | return f"OrchestratorDecision(code={self.code}, new_step={self.new_step})" |
| 49 | |
| 50 | def __repr__(self) -> str: |
| 51 | return str(self) |