From c49d130d5c267aaaa3333fdf377cc2fe28dee7e8 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Mon, 19 Oct 2026 03:29:21 +0800 Subject: [PATCH] fix(mastery): stream localized lesson preparation progress (#1902) --- deeptutor/capabilities/mastery/loop.py | 1 + .../mastery/prompts/en/mastery_loop.yaml | 6 ++++ .../mastery/prompts/zh/mastery_loop.yaml | 6 ++++ deeptutor/capabilities/mastery/tools.py | 4 +++ deeptutor/learning/lesson_evidence.py | 32 ++++++++++--------- .../services/rag/test_mastery_source_pages.py | 31 ++++++++++++++++++ 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/deeptutor/capabilities/mastery/loop.py b/deeptutor/capabilities/mastery/loop.py index e59b15286..0ed181d02 100644 --- a/deeptutor/capabilities/mastery/loop.py +++ b/deeptutor/capabilities/mastery/loop.py @@ -398,6 +398,7 @@ class MasteryLoopCapability: # request, not a model-supplied claim about their intention. updated["_learner_message"] = context.user_message or "" if tool_name == "mastery_prepare_lesson": + updated["_language"] = context.language from deeptutor.learning.policy import next_objective from deeptutor.learning.storage import LearningStore diff --git a/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml b/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml index 275637534..0a9369e68 100644 --- a/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml +++ b/deeptutor/capabilities/mastery/prompts/en/mastery_loop.yaml @@ -7,6 +7,12 @@ # inherited from chat's pack (see MasteryLoopPipeline.prompt_base_module); # only what genuinely belongs to the tutor is overridden here. +preparation: + checking_sources: Checking selected lesson sources and page references + finding_sources: Finding sources for this teaching step + checking_pages: Checking original pages and figures + repairing: Resolving a missing or unreadable detail with one targeted retry + general: |- You are the learner's one-on-one mastery tutor. They work through an outline of objectives, each behind a HARD mastery gate: an objective counts as mastered once its gate clears, and you do not move on until it does. diff --git a/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml b/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml index fe54ef653..d9eb9edf7 100644 --- a/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml +++ b/deeptutor/capabilities/mastery/prompts/zh/mastery_loop.yaml @@ -6,6 +6,12 @@ # 提示词包兜底(见 MasteryLoopPipeline.prompt_base_module),这里只覆盖真正 # 属于导师的部分。 +preparation: + checking_sources: 正在检查已选课程资料和页码 + finding_sources: 正在为本次教学查找资料 + checking_pages: 正在检查原始页面和图表 + repairing: 正在对缺失或难以辨认的细节进行一次针对性重试 + general: |- 你是学习者的一对一掌握式导师。 他沿着一份精通大纲前进,大纲里每个知识点后面都有一道硬性掌握门槛:门槛过了,这个知识点才算掌握;没过,就不推进到下一个。 diff --git a/deeptutor/capabilities/mastery/tools.py b/deeptutor/capabilities/mastery/tools.py index 0bb28cd41..d9533866c 100644 --- a/deeptutor/capabilities/mastery/tools.py +++ b/deeptutor/capabilities/mastery/tools.py @@ -3790,11 +3790,15 @@ class MasteryPrepareLessonTool(BaseTool): if not path_id: return _no_path_result() try: + from deeptutor.runtime.stream_bus import get_bus + evidence = await prepare_lesson( service.store, path_id, str(kwargs.get("knowledge_point_id") or ""), get_llm_config(), + stream=get_bus(_resolve_turn_id(kwargs)), + language=kwargs.get("_language") or "en", focus=kwargs.get("focus") or "", learner_request=str(kwargs.get("_learner_message") or "")[:1500], refresh=kwargs.get("refresh", False), diff --git a/deeptutor/learning/lesson_evidence.py b/deeptutor/learning/lesson_evidence.py index 599f18101..b216d3181 100644 --- a/deeptutor/learning/lesson_evidence.py +++ b/deeptutor/learning/lesson_evidence.py @@ -579,15 +579,28 @@ async def prepare_lesson( config: Any, stream: Any = None, *, + language: str = "en", focus: str = "", learner_request: str = "", refresh: bool = False, ) -> dict[str, Any]: """No write transaction is held across retrieval or model calls.""" from deeptutor.services.llm.capabilities import supports_vision + from deeptutor.services.prompt import get_prompt_manager + from deeptutor.services.prompt.lookup import prompt_text from deeptutor.services.rag.pipelines.llamaindex.pipeline import LlamaIndexPipeline from deeptutor.services.workspace.knowledge import learning_source_access + prompts = get_prompt_manager().load_prompts("mastery", "mastery_loop", language) + + async def status(key: str) -> None: + if stream is not None: + await stream.progress( + prompt_text(prompts, ("preparation", key)), + source="mastery_path", + stage="responding", + ) + if ( not isinstance(focus, str) or len(focus) > 800 @@ -614,12 +627,7 @@ async def prepare_lesson( obj_key = objective_fingerprint(progress, kp_id) course_intent = _course_intent(topic, progress) intent_key = _fingerprint(course_intent) - if stream is not None: - await stream.progress( - "A lecke forrásait és oldalhivatkozásait ellenőrzöm", - source="mastery_path", - stage="responding", - ) + await status("checking_sources") scope_key, indices = await asyncio.to_thread( scope_snapshot, topic, progress, kp_id, prepare_legacy=True ) @@ -658,11 +666,7 @@ async def prepare_lesson( "learner_request": learner_request, } - async def status(text: str) -> None: - if stream is not None: - await stream.progress(text, source="mastery_path", stage="responding") - - await status("Forrásokat keresek ehhez a témához") + await status("finding_sources") query_result = await _ask( config, 'Return JSON {"queries":[...]} with up to 3 concise retrieval queries for this learning objective. ' @@ -950,7 +954,7 @@ async def prepare_lesson( feedback = None start = 0 if sources: - await status("Az eredeti oldalakat és ábrákat ellenőrzöm") + await status("checking_pages") observations, feedback, start = await inspect(sources) retain_inspected(sources, observations) if feedback is None and ranking.get("sufficient") is True: @@ -982,9 +986,7 @@ async def prepare_lesson( "reason": repair["reason"], "status": "not_applied", } - await status( - "Hiányzó vagy nehezen olvasható részletet pótolok (egyszeri célzott javítás)" - ) + await status("repairing") if repair["action"] == "search": repair_queries = repair.get("queries", [repair.get("query")]) if ( diff --git a/tests/services/rag/test_mastery_source_pages.py b/tests/services/rag/test_mastery_source_pages.py index a591c0aa8..6f75db189 100644 --- a/tests/services/rag/test_mastery_source_pages.py +++ b/tests/services/rag/test_mastery_source_pages.py @@ -87,6 +87,37 @@ async def test_successful_explicit_preparation_supersedes_same_turn_failure(mate assert not material.store.load("path").mastery_levels +@pytest.mark.asyncio +@pytest.mark.parametrize("language, expected", [("en", "Finding sources"), ("zh", "查找资料")]) +async def test_explicit_preparation_reports_localized_stages_on_the_owned_stream( + material, monkeypatch, language, expected +): + from deeptutor.capabilities.mastery.tools import MasteryPrepareLessonTool + from deeptutor.runtime.stream_bus import StreamBus, register_bus, unregister_bus + + _install_preparation(material, monkeypatch) + monkeypatch.setattr( + "deeptutor.capabilities.mastery.tools._new_service", lambda: LearningService(material.store) + ) + monkeypatch.setattr("deeptutor.services.llm.config.get_llm_config", lambda: material.config) + stream = StreamBus() + register_bus("preparing", stream) + try: + result = await MasteryPrepareLessonTool().execute( + _mastery_path_id="path", + knowledge_point_id="kp", + _turn_id="preparing", + _language=language, + ) + await stream.close() + events = [event async for event in stream.subscribe()] + assert result.success, result.content + assert any(expected in event.content for event in events) + assert not any(event.type.value == "content" for event in events) + finally: + unregister_bus("preparing") + + @pytest.fixture def material(tmp_path, monkeypatch): raw = tmp_path / "kb" / "raw"