Download core/state.py from RDS777/SKKAI: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/spaces/RDS777/SKKAI/resolve/main/core/state.py
- Command line
-
hf download hf://spaces/RDS777/SKKAI/core/state.py
-
curl -L -o state.py https://huggingface.co/spaces/RDS777/SKKAI/resolve/main/core/state.py
2.68 kB
| import json | |
| import os | |
| import threading | |
| from typing import List, Literal | |
| from pydantic import BaseModel, Field | |
| class Task(BaseModel): | |
| id: int | |
| name: str | |
| cost: float | |
| revenue: float = 0.0 | |
| priority: Literal["high", "medium", "low"] = "medium" | |
| deadline_day: int = 7 | |
| status: Literal["pending", "completed"] = "pending" | |
| class WorldState(BaseModel): | |
| company_name: str = "Sky-LG Corp" | |
| initial_budget: float = 5000.0 | |
| budget_usd: float = 5000.0 | |
| day: int = 0 | |
| deadline_days: int = 7 | |
| tasks: List[Task] = Field(default_factory=list) | |
| web_context: str = "" | |
| history: List[dict] = Field(default_factory=list) | |
| def default() -> "WorldState": | |
| return WorldState( | |
| tasks=[ | |
| Task(id=1, name="Concevoir le PCB", cost=1500, revenue=2500, priority="high", deadline_day=3), | |
| Task(id=2, name="Acheter les composants", cost=2000, revenue=2200, priority="medium", deadline_day=5), | |
| Task(id=3, name="Tester le prototype", cost=1000, revenue=1500, priority="low", deadline_day=7), | |
| Task(id=4, name="Rédiger la documentation", cost=400, revenue=600, priority="low", deadline_day=7), | |
| Task(id=5, name="Certification CE", cost=1200, revenue=3000, priority="medium", deadline_day=6), | |
| ] | |
| ) | |
| class StateManager: | |
| def __init__(self, data_dir: str): | |
| self.lock = threading.RLock() | |
| os.makedirs(data_dir, exist_ok=True) | |
| self.path = os.path.join(data_dir, "state.json") | |
| self.state: WorldState = self._load() | |
| def _load(self) -> WorldState: | |
| try: | |
| if os.path.exists(self.path): | |
| with open(self.path, "r", encoding="utf-8") as f: | |
| return WorldState.model_validate(json.load(f)) | |
| except Exception as e: | |
| print(f"[state] chargement impossible, état par défaut: {e}") | |
| return WorldState.default() | |
| def save(self) -> None: | |
| with self.lock: | |
| try: | |
| tmp = self.path + ".tmp" | |
| with open(tmp, "w", encoding="utf-8") as f: | |
| json.dump(self.state.model_dump(), f, ensure_ascii=False, indent=2) | |
| os.replace(tmp, self.path) | |
| except Exception as e: | |
| print(f"[state] sauvegarde impossible: {e}") | |
| def snapshot(self) -> WorldState: | |
| with self.lock: | |
| return self.state.model_copy(deep=True) | |
| def reset(self) -> None: | |
| with self.lock: | |
| web = self.state.web_context | |
| self.state = WorldState.default() | |
| self.state.web_context = web | |
| self.save() |