Spaces:
Sleeping
Sleeping
| import yaml | |
| from datetime import datetime, timezone | |
| from typing import Dict, Optional, List, Any | |
| from src.state_machine import State, CardType | |
| class CardGenerator: | |
| def __init__(self): | |
| self.card_id_counter = 0 | |
| def generate( | |
| self, | |
| state: State, | |
| card_type: CardType, | |
| title: str, | |
| description: str, | |
| content: Dict[str, Any], | |
| dataset_result: Optional[Dict] = None, | |
| context_links: Optional[List[str]] = None, | |
| next_actions: Optional[List[str]] = None, | |
| version: str = "1.0.1", | |
| ) -> str: | |
| self.card_id_counter += 1 | |
| ts = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z") | |
| card = { | |
| "STATE": state.value, | |
| "LOCK": "ON", | |
| "CARD_TYPE": card_type.value, | |
| "CARD_ID": f"{card_type.value}_{self.card_id_counter:04d}", | |
| "VERSION": version, | |
| "TIMESTAMP": ts, | |
| "TITLE": title, | |
| "DESCRIPTION": description, | |
| "CONTENT": content or {}, | |
| "CONTEXT_LINKS": context_links or [f"{state.value}.default_context"], | |
| "NEXT_ACTIONS": next_actions | |
| or ["RESET", "MODE <state>", "CARD <type>", "RUN <query>", "LEDGER"], | |
| } | |
| # Dataset annotation (kept small) | |
| if isinstance(dataset_result, dict): | |
| rows = dataset_result.get("rows") | |
| result_count = len(rows) if isinstance(rows, list) else None | |
| card["DATASET_ANNOTATION"] = { | |
| "RESULT_COUNT": result_count, | |
| "HAS_ERROR": bool(dataset_result.get("error")), | |
| } | |
| header = f"{state.value}\n" # optional anchor | |
| yaml_out = yaml.safe_dump(card, sort_keys=False, allow_unicode=True) | |
| footer = "" | |
| if isinstance(dataset_result, dict) and isinstance(dataset_result.get("rows"), list): | |
| footer = f"\n---\nSOURCE: Dataset query returned {len(dataset_result['rows'])} results\n" | |
| return f"[STATE: {state.value} | LOCK: ON]\n\n{yaml_out}{footer}" | |
| def generate_error_card(self, state: State, error: str) -> str: | |
| return self.generate( | |
| state=state, | |
| card_type=CardType.SPEC, | |
| title="Error Response", | |
| description="An error occurred while processing the command.", | |
| content={"ERROR": error, "RECOVERY": "Check command syntax and retry."}, | |
| next_actions=["HELP", "RESET"], | |
| ) | |