28 lines
902 B
Python
28 lines
902 B
Python
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from app.character.card import DEFAULT_CARD, build_system_prompt, normalize_card
|
|
|
|
CARD_PATH = Path("./data/character.json")
|
|
|
|
|
|
class CharacterService:
|
|
def get_card(self) -> dict[str, Any]:
|
|
if CARD_PATH.is_file():
|
|
try:
|
|
raw = json.loads(CARD_PATH.read_text(encoding="utf-8"))
|
|
return normalize_card(raw)
|
|
except (json.JSONDecodeError, OSError):
|
|
pass
|
|
return normalize_card(DEFAULT_CARD)
|
|
|
|
def save_card(self, raw: dict[str, Any]) -> dict[str, Any]:
|
|
card = normalize_card(raw)
|
|
CARD_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
CARD_PATH.write_text(json.dumps(card, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
return card
|
|
|
|
def get_system_prompt(self) -> str:
|
|
return build_system_prompt(self.get_card())
|