| |
| """ |
| Lightweight planner inspired by LangGraph: composes steps into a DAG and runs them. |
| This is a stub interface to be replaced by LangGraph if installed. |
| """ |
|
|
| from typing import Callable, Dict, List, Any |
|
|
|
|
| class Step: |
| def __init__(self, name: str, fn: Callable[[Dict[str, Any]], Dict[str, Any]], deps: List[str] | None = None): |
| self.name = name |
| self.fn = fn |
| self.deps = deps or [] |
|
|
|
|
| class Plan: |
| def __init__(self, steps: List[Step]): |
| self.steps = {s.name: s for s in steps} |
|
|
| def run(self, ctx: Dict[str, Any]) -> Dict[str, Any]: |
| done: Dict[str, Dict[str, Any]] = {} |
| remaining = set(self.steps.keys()) |
| while remaining: |
| progressed = False |
| for name in list(remaining): |
| step = self.steps[name] |
| if all(d in done for d in step.deps): |
| out = step.fn({**ctx, **{k: done[k] for k in step.deps}}) |
| done[name] = out or {} |
| remaining.remove(name) |
| progressed = True |
| if not progressed: |
| raise RuntimeError("Plan deadlock: check dependencies") |
| return {**ctx, **done} |
|
|
|
|
| def simple_llm_plan(make_prompt: Callable[[Dict[str, Any]], str]) -> Plan: |
| def build_input(ctx: Dict[str, Any]) -> Dict[str, Any]: |
| prompt = make_prompt(ctx) |
| return {"prompt": prompt} |
|
|
| def validate(ctx: Dict[str, Any]) -> Dict[str, Any]: |
| |
| return {"ok": True} |
|
|
| return Plan([ |
| Step("build_input", build_input), |
| Step("validate", validate, deps=["build_input"]), |
| ]) |
|
|
|
|