import json
import os

_BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def load_prompt(path: str) -> str:
    full_path = os.path.join(_BASE_DIR, path)
    with open(full_path, "r", encoding="utf-8") as f:
        return f.read()


def parse_json_response(raw: str) -> dict:
    """LLM 응답에서 JSON을 추출합니다."""
    start = raw.find("{")
    end = raw.rfind("}") + 1
    if start == -1 or end == 0:
        return {}
    try:
        return json.loads(raw[start:end])
    except json.JSONDecodeError:
        return {}
