|
| 1 | +"""Base classes for workflow step types. |
| 2 | +
|
| 3 | +Provides: |
| 4 | +- ``StepBase`` — abstract base every step type must implement. |
| 5 | +- ``StepContext`` — execution context passed to each step. |
| 6 | +- ``StepResult`` — return value from step execution. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from abc import ABC, abstractmethod |
| 12 | +from dataclasses import dataclass, field |
| 13 | +from enum import Enum |
| 14 | +from typing import Any |
| 15 | + |
| 16 | + |
| 17 | +class StepStatus(str, Enum): |
| 18 | + """Status of a step execution.""" |
| 19 | + |
| 20 | + PENDING = "pending" |
| 21 | + RUNNING = "running" |
| 22 | + COMPLETED = "completed" |
| 23 | + FAILED = "failed" |
| 24 | + SKIPPED = "skipped" |
| 25 | + PAUSED = "paused" |
| 26 | + |
| 27 | + |
| 28 | +class RunStatus(str, Enum): |
| 29 | + """Status of a workflow run.""" |
| 30 | + |
| 31 | + CREATED = "created" |
| 32 | + RUNNING = "running" |
| 33 | + PAUSED = "paused" |
| 34 | + COMPLETED = "completed" |
| 35 | + FAILED = "failed" |
| 36 | + ABORTED = "aborted" |
| 37 | + |
| 38 | + |
| 39 | +@dataclass |
| 40 | +class StepContext: |
| 41 | + """Execution context passed to each step. |
| 42 | +
|
| 43 | + Contains everything the step needs to resolve expressions, dispatch |
| 44 | + commands, and record results. |
| 45 | + """ |
| 46 | + |
| 47 | + #: Resolved workflow inputs (from user prompts / defaults). |
| 48 | + inputs: dict[str, Any] = field(default_factory=dict) |
| 49 | + |
| 50 | + #: Accumulated step results keyed by step ID. |
| 51 | + #: Each entry is ``{"integration": ..., "model": ..., "options": ..., |
| 52 | + #: "input": ..., "output": ...}``. |
| 53 | + steps: dict[str, dict[str, Any]] = field(default_factory=dict) |
| 54 | + |
| 55 | + #: Current fan-out item (set only inside fan-out iterations). |
| 56 | + item: Any = None |
| 57 | + |
| 58 | + #: Fan-in aggregated results (set only for fan-in steps). |
| 59 | + fan_in: dict[str, Any] = field(default_factory=dict) |
| 60 | + |
| 61 | + #: Workflow-level default integration key. |
| 62 | + default_integration: str | None = None |
| 63 | + |
| 64 | + #: Workflow-level default model. |
| 65 | + default_model: str | None = None |
| 66 | + |
| 67 | + #: Workflow-level default options. |
| 68 | + default_options: dict[str, Any] = field(default_factory=dict) |
| 69 | + |
| 70 | + #: Project root path. |
| 71 | + project_root: str | None = None |
| 72 | + |
| 73 | + #: Current run ID. |
| 74 | + run_id: str | None = None |
| 75 | + |
| 76 | + |
| 77 | +@dataclass |
| 78 | +class StepResult: |
| 79 | + """Return value from a step execution.""" |
| 80 | + |
| 81 | + #: Step status. |
| 82 | + status: StepStatus = StepStatus.COMPLETED |
| 83 | + |
| 84 | + #: Output data (stored as ``steps.<id>.output``). |
| 85 | + output: dict[str, Any] = field(default_factory=dict) |
| 86 | + |
| 87 | + #: Nested steps to execute (for control-flow steps like if/then). |
| 88 | + next_steps: list[dict[str, Any]] = field(default_factory=list) |
| 89 | + |
| 90 | + #: Error message if step failed. |
| 91 | + error: str | None = None |
| 92 | + |
| 93 | + |
| 94 | +class StepBase(ABC): |
| 95 | + """Abstract base class for workflow step types. |
| 96 | +
|
| 97 | + Every step type — built-in or extension-provided — implements this |
| 98 | + interface and registers in ``STEP_REGISTRY``. |
| 99 | + """ |
| 100 | + |
| 101 | + #: Matches the ``type:`` value in workflow YAML. |
| 102 | + type_key: str = "" |
| 103 | + |
| 104 | + @abstractmethod |
| 105 | + def execute(self, config: dict[str, Any], context: StepContext) -> StepResult: |
| 106 | + """Execute the step with the given config and context. |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + config: |
| 111 | + The step configuration from workflow YAML. |
| 112 | + context: |
| 113 | + The execution context with inputs, accumulated step results, etc. |
| 114 | +
|
| 115 | + Returns |
| 116 | + ------- |
| 117 | + StepResult with status, output data, and optional nested steps. |
| 118 | + """ |
| 119 | + |
| 120 | + def validate(self, config: dict[str, Any]) -> list[str]: |
| 121 | + """Validate step configuration and return a list of error messages. |
| 122 | +
|
| 123 | + An empty list means the configuration is valid. |
| 124 | + """ |
| 125 | + errors: list[str] = [] |
| 126 | + if "id" not in config: |
| 127 | + errors.append("Step is missing required 'id' field.") |
| 128 | + return errors |
| 129 | + |
| 130 | + def can_resume(self, state: dict[str, Any]) -> bool: |
| 131 | + """Return whether this step can be resumed from the given state.""" |
| 132 | + return True |
0 commit comments