Files
wehub-resource-sync e4dcfc49aa
Tests / Import Check (Python 3.13) (push) Has been cancelled
Tests / Import Check (Python 3.14) (push) Has been cancelled
Tests / Python Tests (Python 3.11) (push) Has been cancelled
Tests / Python Tests (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.14) (push) Has been cancelled
Tests / Test Summary (push) Has been cancelled
Tests / Lint and Format (push) Has been cancelled
Tests / Web Node Tests (push) Has been cancelled
Tests / Import Check (Python 3.11) (push) Has been cancelled
Tests / Import Check (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.13) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:00:43 +08:00

68 lines
2.3 KiB
Python

"""
Shared prompt loader for non-BaseAgent call sites in ``deeptutor/book``.
All LLM-facing prompts inside this module live as YAML under
``deeptutor/book/prompts/{en,zh}/<name>.yaml`` and are loaded through the
unified :class:`~deeptutor.services.prompt.PromptManager`. This helper is the
thin wrapper that block generators and the SectionArchitect use, since they
call ``llm_text`` directly instead of inheriting :class:`BaseAgent`.
Conventions
-----------
* One YAML file per logical writer (block generator, planner stage…), named
after the python module (``text.py`` → ``text.yaml``).
* Top-level keys are short identifiers (``system``, ``user_template``,
``bridge_system``, ``outline_system``…) and contain plain strings.
* Variable interpolation uses ``str.format`` placeholders (``{chapter_title}``).
Missing files / keys raise :class:`RuntimeError` so the failure surfaces at
the call site instead of silently producing degenerate prompts.
"""
from __future__ import annotations
from typing import Any
from deeptutor.services.prompt import get_prompt_manager
def load_book_prompts(name: str, language: str) -> dict[str, Any]:
"""Load a YAML prompt bundle for the ``book`` module.
Args:
name: File stem under ``deeptutor/book/prompts/{lang}/``
(e.g. ``"text"``, ``"section"``, ``"page_planner"``).
language: ``"en"`` or ``"zh"``.
Returns:
Parsed YAML as a dictionary.
Raises:
RuntimeError: When the bundle is missing or empty.
"""
prompts = get_prompt_manager().load_prompts(
module_name="book",
agent_name=name,
language=language,
)
if not prompts:
raise RuntimeError(
f"Missing prompt bundle for book/{name} (language={language}). "
f"Expected deeptutor/book/prompts/{{en,zh}}/{name}.yaml."
)
return prompts
def get_book_prompt(prompts: dict[str, Any], key: str) -> str:
"""Return the prompt string under ``key`` from a loaded bundle.
Raises ``RuntimeError`` if the key is missing or not a non-empty string.
"""
value = prompts.get(key)
if not isinstance(value, str) or not value.strip():
raise RuntimeError(f"Prompt key '{key}' missing or empty in loaded book prompt bundle.")
return value
__all__ = ["load_book_prompts", "get_book_prompt"]