from enum import Enum from typing import Dict, List, Optional from datetime import datetime, timezone class State(Enum): CORE_OS = "CORE_OS" LEDGER_CORE = "LEDGER_CORE" DATA_SPLITTER = "DATA_SPLITTER" MINDSCRIPT_TEMPLATES = "MINDSCRIPT_TEMPLATES" SQL_CORE = "SQL_CORE" BUILD_SPACE = "BUILD_SPACE" class CardType(Enum): CONCEPT = "CONCEPT" FLOW = "FLOW" SPEC = "SPEC" TEST = "TEST" BUILD = "BUILD" class StateMachine: STATE_CARD_MAPPING = { State.CORE_OS: [CardType.CONCEPT, CardType.FLOW], State.LEDGER_CORE: [CardType.SPEC, CardType.TEST], State.DATA_SPLITTER: [CardType.FLOW, CardType.BUILD], State.MINDSCRIPT_TEMPLATES: [CardType.BUILD, CardType.SPEC], State.SQL_CORE: [CardType.SPEC, CardType.FLOW], State.BUILD_SPACE: [CardType.BUILD, CardType.SPEC], } def __init__(self): self.current_state: State = State.CORE_OS self.context: Dict = {} self.history: List[Dict] = [] self.card_filter: Optional[CardType] = None def transition(self, command: str) -> Dict: raw = (command or "").strip() upper = raw.upper() if upper.startswith("MODE "): state_name = upper.split(maxsplit=1)[1].strip() if hasattr(State, state_name): self.history.append( { "from": self.current_state.value, "to": state_name, "timestamp": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"), } ) self.current_state = State[state_name] return {"action": "transition", "state": self.current_state.value} if upper.startswith("CARD "): card_name = upper.split(maxsplit=1)[1].strip() if hasattr(CardType, card_name): self.card_filter = CardType[card_name] return {"action": "filter", "card_type": self.card_filter.value} if upper.startswith("REPO "): repo_name = raw.split(maxsplit=1)[1].strip() if len(raw.split()) > 1 else "" self.context["repo"] = repo_name return {"action": "context", "repo": repo_name} if upper == "RESET": self.current_state = State.CORE_OS self.context = {} self.card_filter = None self.history = [] return {"action": "reset", "state": self.current_state.value} if upper in ("STATE", "HELP"): return {"action": upper.lower(), "state": self.current_state.value} if upper.startswith(("RUN ", "TEST", "LEDGER")): return {"action": "query", "command": raw} return {"action": "unknown", "command": raw} def get_output_card_type(self) -> CardType: if self.card_filter: return self.card_filter return self.STATE_CARD_MAPPING.get(self.current_state, [CardType.CONCEPT])[0] def get_state_header(self) -> str: return f"[STATE: {self.current_state.value} | LOCK: ON]"