|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import inspect |
| 4 | +from collections.abc import Awaitable, Callable, Iterable |
| 5 | +from typing import Any, TypeVar, get_type_hints |
| 6 | + |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from fastapi_cloudflow.core.step import Step |
| 10 | +from fastapi_cloudflow.core.types import Context |
| 11 | + |
| 12 | + |
| 13 | +class Workflow: |
| 14 | + def __init__(self, name: str, nodes: list[Step[Any, Any]]) -> None: |
| 15 | + self.name = name |
| 16 | + self.nodes = nodes |
| 17 | + |
| 18 | + |
| 19 | +class Registry: |
| 20 | + def __init__(self) -> None: |
| 21 | + self.steps: dict[str, Step[Any, Any]] = {} |
| 22 | + self.workflows: dict[str, Workflow] = {} |
| 23 | + |
| 24 | + def register_step(self, step: Step[Any, Any]) -> None: |
| 25 | + if step.name in self.steps: |
| 26 | + raise ValueError(f"Step name collision: {step.name}") |
| 27 | + self.steps[step.name] = step |
| 28 | + |
| 29 | + def register_workflow(self, workflow: Workflow) -> None: |
| 30 | + existing = self.workflows.get(workflow.name) |
| 31 | + if existing is not None: |
| 32 | + existing_names = [s.name for s in existing.nodes] |
| 33 | + new_names = [s.name for s in workflow.nodes] |
| 34 | + if existing_names == new_names: |
| 35 | + return |
| 36 | + raise ValueError(f"Workflow name collision: {workflow.name}") |
| 37 | + self.workflows[workflow.name] = workflow |
| 38 | + |
| 39 | + def get_workflows(self) -> list[Workflow]: |
| 40 | + return list(self.workflows.values()) |
| 41 | + |
| 42 | + |
| 43 | +class WorkflowBuilder: |
| 44 | + def __init__(self, name: str, nodes: list[Step[Any, Any]] | None = None) -> None: |
| 45 | + self.name = name |
| 46 | + self.nodes = nodes or [] |
| 47 | + |
| 48 | + def __rshift__(self, other: Step[Any, Any]) -> WorkflowBuilder: |
| 49 | + if self.nodes: |
| 50 | + prev = self.nodes[-1] |
| 51 | + if prev.output_model is not other.input_model: |
| 52 | + raise TypeError( |
| 53 | + f"Type mismatch: {prev.name} outputs {prev.output_model.__name__} " |
| 54 | + f"but {other.name} expects {other.input_model.__name__}" |
| 55 | + ) |
| 56 | + return WorkflowBuilder(self.name, self.nodes + [other]) |
| 57 | + |
| 58 | + def build(self) -> Workflow: |
| 59 | + if not self.nodes: |
| 60 | + raise ValueError("Workflow has no steps") |
| 61 | + wf = Workflow(self.name, self.nodes) |
| 62 | + _REGISTRY.register_workflow(wf) |
| 63 | + return wf |
| 64 | + |
| 65 | + |
| 66 | +_REGISTRY = Registry() |
| 67 | + |
| 68 | + |
| 69 | +def workflow(name: str) -> WorkflowBuilder: |
| 70 | + return WorkflowBuilder(name) |
| 71 | + |
| 72 | + |
| 73 | +InT = TypeVar("InT", bound=BaseModel) |
| 74 | +OutT = TypeVar("OutT", bound=BaseModel) |
| 75 | + |
| 76 | + |
| 77 | +def step( |
| 78 | + *, |
| 79 | + name: str | None = None, |
| 80 | + retry: Any | None = None, |
| 81 | + timeout: Any | None = None, |
| 82 | + tags: Iterable[str] = (), |
| 83 | +): |
| 84 | + def decorator(fn: Callable[[Context, InT], Awaitable[OutT]]) -> Step[InT, OutT]: |
| 85 | + hints = get_type_hints(fn) |
| 86 | + sig = inspect.signature(fn) |
| 87 | + params = list(sig.parameters.values()) |
| 88 | + if len(params) != 2: |
| 89 | + raise TypeError("@step function must accept exactly two positional parameters: (Context, InModel)") |
| 90 | + in_param = params[1].name |
| 91 | + in_model = hints.get(in_param) |
| 92 | + out_model = hints.get("return") |
| 93 | + if not (isinstance(in_model, type) and issubclass(in_model, BaseModel)): |
| 94 | + raise TypeError("@step function must type its second parameter as a Pydantic BaseModel subclass") |
| 95 | + if not (isinstance(out_model, type) and issubclass(out_model, BaseModel)): |
| 96 | + raise TypeError("@step function must return a Pydantic BaseModel subclass") |
| 97 | + base_name = getattr(fn, "__name__", "step") |
| 98 | + nm = name or base_name.replace("_", "-") |
| 99 | + s: Step[Any, Any] = Step(nm, in_model, out_model, fn=fn, retry=retry, timeout=timeout, tags=tags) |
| 100 | + _REGISTRY.register_step(s) |
| 101 | + return s |
| 102 | + |
| 103 | + return decorator |
| 104 | + |
| 105 | + |
| 106 | +def get_registry() -> Registry: |
| 107 | + return _REGISTRY |
| 108 | + |
| 109 | + |
| 110 | +def get_workflows() -> list[Workflow]: |
| 111 | + return _REGISTRY.get_workflows() |
0 commit comments