feat(state): AppState singleton (settings, bus, game, bindings)
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from ai_chess.chess_game import ChessGame
|
||||
from ai_chess.events import EventBus
|
||||
from ai_chess.settings import Settings, load_settings
|
||||
from ai_chess.sides import Side
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
GAMES_DIR = ROOT / "games"
|
||||
CONFIG_DIR = ROOT / "config"
|
||||
LOGS_DIR = ROOT / "logs"
|
||||
PUBLIC_DIR = ROOT / "public"
|
||||
|
||||
|
||||
@dataclass
|
||||
class AgentBinding:
|
||||
side: Side
|
||||
agent: str # "claude" | "codex"
|
||||
model: str
|
||||
effort: str = ""
|
||||
extra: dict = field(default_factory=dict)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {"side": self.side.value, "agent": self.agent, "model": self.model,
|
||||
"effort": self.effort, **self.extra}
|
||||
|
||||
|
||||
class AppState:
|
||||
def __init__(self) -> None:
|
||||
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
GAMES_DIR.mkdir(parents=True, exist_ok=True)
|
||||
LOGS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
self.settings: Settings = load_settings(CONFIG_DIR / "settings.json")
|
||||
self.events: EventBus = EventBus()
|
||||
self.game: Optional[ChessGame] = None
|
||||
self.bindings: dict[Side, AgentBinding] = {}
|
||||
self.first_move_sent: dict[Side, bool] = {Side.WHITE: False, Side.BLACK: False}
|
||||
self.nudge_count: dict[Side, int] = {Side.WHITE: 0, Side.BLACK: 0}
|
||||
|
||||
|
||||
state = AppState()
|
||||
Reference in New Issue
Block a user