File size: 3,106 Bytes
6bb43c2
 
0a9e70e
 
6bb43c2
 
 
 
 
 
 
 
 
0a9e70e
 
 
 
 
 
 
 
 
6bb43c2
0a9e70e
 
 
 
 
 
 
 
 
6bb43c2
 
 
0a9e70e
 
6bb43c2
0a9e70e
 
 
6bb43c2
0a9e70e
 
 
 
 
 
 
 
 
 
 
 
6bb43c2
0a9e70e
 
 
 
 
6bb43c2
0a9e70e
 
 
 
 
 
6bb43c2
 
0a9e70e
6bb43c2
0a9e70e
 
 
 
 
 
 
 
 
 
 
 
 
 
6bb43c2
0a9e70e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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]"