| 1 | from abc import ABC, abstractmethod |
| 2 | from .step_result import StepResult |
| 3 | from json import loads |
| 4 | |
| 5 | class StepBlockType: |
| 6 | PARALLEL = "parallel" |
| 7 | AGENT = "agent" |
| 8 | SUMMARY = "summary" |
| 9 | USER_INPUT = "user_input" |
| 10 | |
| 11 | @staticmethod |
| 12 | def from_str(text: str) -> "StepBlockType": |
| 13 | if text == StepBlockType.PARALLEL: |
| 14 | return StepBlockType.PARALLEL |
| 15 | elif text == StepBlockType.AGENT: |
| 16 | return StepBlockType.AGENT |
| 17 | elif text == StepBlockType.SUMMARY: |
| 18 | return StepBlockType.SUMMARY |
| 19 | elif text == StepBlockType.USER_INPUT: |
| 20 | return StepBlockType.USER_INPUT |
| 21 | else: |
| 22 | raise ValueError(f"Unknown step block type: {text}") |
| 23 | |
| 24 | |
| 25 | class PlanStep(ABC): |
| 26 | |
| 27 | @property |
| 28 | @abstractmethod |
| 29 | def id(self) -> str: |
| 30 | pass |
| 31 | |
| 32 | @property |
| 33 | @abstractmethod |
| 34 | def block(self) -> StepBlockType: |
| 35 | pass |
| 36 | |
| 37 | @property |
| 38 | @abstractmethod |
| 39 | def properties(self) -> any: |
| 40 | pass |
| 41 | |
| 42 | @staticmethod |
| 43 | def from_json(json: dict| str) -> "PlanStep": |
| 44 | json = json if isinstance(json, dict) else loads(json) |
| 45 | from graphrag_sdk.orchestrator.steps import PLAN_STEP_TYPE_MAP |
| 46 | block = StepBlockType.from_str(json["block"]) |
| 47 | step_type = PLAN_STEP_TYPE_MAP[block] |
| 48 | |
| 49 | if step_type is None: |
| 50 | raise ValueError(f"Unknown step block type: {block}") |
| 51 | |
| 52 | return step_type.from_json(json) |
| 53 | |
| 54 | @abstractmethod |
| 55 | def run( |
| 56 | self, |
| 57 | runner: any, |
| 58 | config: dict = None, |
| 59 | ) -> StepResult: |
| 60 | pass |