← Lesson
Typed State Beats a Blob
Joeven
Run
Reset
Python loads on first run
ALLOWED = { "gather": ["search", "get_ticket", "handoff", "finish"], "apply": ["refund", "handoff", "finish"], "done": ["finish"], } def new_state(): return {"phase": "gather", "ticket_id": None, "facts": {}} def can_run(state, name): return name in ALLOWED[state["phase"]] def apply_obs(state, name, obs): if name == "get_ticket" and isinstance(obs, dict): state["ticket_id"] = obs.get("id") if obs.get("ready"): state["phase"] = "apply" if name == "refund": state["phase"] = "done" return state st = new_state() print("refund in gather", can_run(st, "refund")) st = apply_obs(st, "get_ticket", {"id": "T1", "ready": True}) print("phase", st["phase"], "ticket", st["ticket_id"]) print("refund in apply", can_run(st, "refund"))
Run to execute this in your browser. Nothing is sent to a server.