mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 03:09:49 +08:00
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
"""Mastery path loop plugin hooks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from importlib import resources
|
|
from typing import Any
|
|
|
|
from deeptutor.core.context import UnifiedContext
|
|
from deeptutor.loop_plugins.mastery.tools import MASTERY_TOOL_NAMES
|
|
from deeptutor.loop_plugins.protocol import PromptBlock
|
|
|
|
|
|
class MasteryLoopPlugin:
|
|
"""Turn-scoped integration for mastery-path tutoring."""
|
|
|
|
name = "mastery"
|
|
|
|
def is_active(self, context: UnifiedContext) -> bool:
|
|
return bool(context.metadata.get("mastery_mode"))
|
|
|
|
def tool_types(self, context: UnifiedContext) -> tuple[str, ...]:
|
|
return MASTERY_TOOL_NAMES if self.is_active(context) else ()
|
|
|
|
def system_block(
|
|
self,
|
|
context: UnifiedContext,
|
|
*,
|
|
language: str,
|
|
prompts: dict[str, Any],
|
|
) -> PromptBlock | None:
|
|
if not self.is_active(context):
|
|
return None
|
|
override = _prompt_text(prompts, ("mastery", "system"))
|
|
content = override or _load_system_prompt(language)
|
|
return PromptBlock("mastery_tutor", content)
|
|
|
|
def augment_kwargs(
|
|
self,
|
|
tool_name: str,
|
|
kwargs: dict[str, Any],
|
|
context: UnifiedContext,
|
|
) -> dict[str, Any]:
|
|
if self.is_active(context) and tool_name in MASTERY_TOOL_NAMES:
|
|
updated = dict(kwargs)
|
|
updated["_mastery_path_id"] = str(context.metadata.get("mastery_path_id") or "").strip()
|
|
return updated
|
|
return kwargs
|
|
|
|
def pre_loop_seed(self, context: UnifiedContext) -> str:
|
|
_ = context
|
|
return ""
|
|
|
|
|
|
def _prompt_text(prompts: dict[str, Any], path: tuple[str, ...]) -> str:
|
|
value: Any = prompts
|
|
for key in path:
|
|
if not isinstance(value, dict):
|
|
return ""
|
|
value = value.get(key)
|
|
return value if isinstance(value, str) and value else ""
|
|
|
|
|
|
def _load_system_prompt(language: str) -> str:
|
|
lang = "zh" if language.lower().startswith("zh") else "en"
|
|
prompt = resources.files(__package__).joinpath("prompts", lang, "system.md")
|
|
return prompt.read_text(encoding="utf-8").strip()
|
|
|
|
|
|
__all__ = ["MasteryLoopPlugin"]
|