mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 17:40:18 +08:00
Remove course pages, APIs and the course_study capability. Add anchored note threads and reviewed learning-block generation, queued and withdrawable chat messages, quote attachments, side chats, and the calendar/favorites/navigation updates. Keep preview's existing backend architecture. Wire the new features through its Python application and existing chat engine, retain legacy selected-text requests, persist mid-turn messages in replay, and restore note context when regenerating. Leave stored user data intact. Validate frontend contracts, dependency boundaries, types, 1290 node tests, 840 rendered tests, lint, localization and the production build. Exercise session/notes/API regressions, 53 chat-engine tests and 20 Python/native integration tests with isolated runtime data.
198 lines
7.4 KiB
Python
198 lines
7.4 KiB
Python
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from deeptutor.agents.chat.agentic_pipeline import AgenticChatPipeline
|
|
from deeptutor.agents.loop.prompt_blocks import ChatPromptAssembler
|
|
from deeptutor.capabilities.mastery.pipeline import MasteryLoopPipeline
|
|
from deeptutor.core.context import TurnRuntimeContext, UnifiedContext, WorkspaceRuntimeContext
|
|
from deeptutor.runtime.agentic.tool_dispatch import MAX_PARALLEL_TOOL_CALLS
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _fake_llm_config(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
cfg = SimpleNamespace(
|
|
binding="openai",
|
|
model="gpt-test",
|
|
api_key="sk-test",
|
|
base_url="https://example.test/v1",
|
|
api_version=None,
|
|
)
|
|
monkeypatch.setattr(
|
|
"deeptutor.agents.loop.pipeline.get_llm_config",
|
|
lambda: cfg,
|
|
)
|
|
monkeypatch.setattr("deeptutor.agents.base_agent.get_llm_config", lambda: cfg)
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "zh"])
|
|
@pytest.mark.parametrize("mode", ["chat", "immersive_reading", "mastery_path"])
|
|
def test_tool_call_limit_reaches_each_loop_prompt(language: str, mode: str) -> None:
|
|
pipeline_type = MasteryLoopPipeline if mode == "mastery_path" else AgenticChatPipeline
|
|
pipeline = pipeline_type(language=language)
|
|
context = UnifiedContext(active_capability=mode)
|
|
prompt = pipeline._build_system_prompt([], context)
|
|
|
|
assert MAX_PARALLEL_TOOL_CALLS == 15
|
|
assert prompt.count("## tool_call_policy\n") == 1
|
|
assert "{limit}" not in prompt
|
|
if language == "zh":
|
|
assert "最多请求 15 次工具调用" in prompt
|
|
assert "不是用户整个请求的总次数" in prompt
|
|
assert "超出上限的调用不会执行" in prompt
|
|
else:
|
|
assert "at most 15 tool calls" in prompt
|
|
assert "not a total for the user's request" in prompt
|
|
assert "Calls beyond the limit\nare not executed" in prompt
|
|
|
|
|
|
def test_agentic_chat_final_prompt_uses_selected_language(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
class FakeRegistry:
|
|
def build_prompt_text(self, *_args, **_kwargs) -> str:
|
|
return "- tool"
|
|
|
|
monkeypatch.setattr(
|
|
"deeptutor.agents.loop.pipeline.get_tool_registry",
|
|
lambda: FakeRegistry(),
|
|
)
|
|
|
|
from deeptutor.core.context import UnifiedContext
|
|
|
|
ctx = UnifiedContext()
|
|
zh_prompt = AgenticChatPipeline(language="zh")._build_system_prompt([], ctx)
|
|
en_prompt = AgenticChatPipeline(language="en")._build_system_prompt([], ctx)
|
|
|
|
# Prompt blocks are phase-specific, but the shared language directive
|
|
# still runs at the end, so per-language imperatives must surface.
|
|
assert "请严格使用中文" in zh_prompt
|
|
assert "Write ALL reader-facing text" in en_prompt
|
|
# Persona phrasing differs by language so the prompts are not just
|
|
# English text with a Chinese tail appended.
|
|
assert "你是 DeepTutor" in zh_prompt
|
|
assert "You are DeepTutor" in en_prompt
|
|
|
|
|
|
def test_mastery_plugin_system_prompt_uses_localized_fallback(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
class FakeRegistry:
|
|
def build_prompt_text(self, *_args, **_kwargs) -> str:
|
|
return "- tool"
|
|
|
|
monkeypatch.setattr(
|
|
"deeptutor.agents.loop.pipeline.get_tool_registry",
|
|
lambda: FakeRegistry(),
|
|
)
|
|
|
|
from deeptutor.core.context import UnifiedContext
|
|
|
|
ctx = UnifiedContext(metadata={"mastery_mode": True, "mastery_path_id": "p1"})
|
|
zh_prompt = AgenticChatPipeline(language="zh")._build_system_prompt([], ctx)
|
|
en_prompt = AgenticChatPipeline(language="en")._build_system_prompt([], ctx)
|
|
|
|
assert "## mastery_tutor" in zh_prompt
|
|
assert "掌握式导师" in zh_prompt
|
|
assert "## mastery_tutor" in en_prompt
|
|
assert "mastery tutor" in en_prompt
|
|
|
|
|
|
def test_ask_questions_plugin_system_prompt_uses_localized_fallback(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
class FakeRegistry:
|
|
def build_prompt_text(self, *_args, **_kwargs) -> str:
|
|
return "- tool"
|
|
|
|
monkeypatch.setattr(
|
|
"deeptutor.agents.loop.pipeline.get_tool_registry",
|
|
lambda: FakeRegistry(),
|
|
)
|
|
|
|
from deeptutor.core.context import UnifiedContext
|
|
|
|
ctx = UnifiedContext(metadata={"ask_questions_mode": True})
|
|
zh_prompt = AgenticChatPipeline(language="zh")._build_system_prompt([], ctx)
|
|
en_prompt = AgenticChatPipeline(language="en")._build_system_prompt([], ctx)
|
|
|
|
assert "## ask_questions" in zh_prompt
|
|
assert "主动提问模式" in zh_prompt
|
|
assert "必须以一次 `ask_user` 调用开始" in zh_prompt
|
|
assert "第 2、3、10 轮" in zh_prompt
|
|
assert "此前所有对话" in zh_prompt
|
|
assert "## ask_questions" in en_prompt
|
|
assert "Ask Questions mode" in en_prompt
|
|
assert "second, third, tenth" in en_prompt
|
|
assert "calling `ask_user` exactly once" in en_prompt
|
|
|
|
|
|
def test_prompt_blocks_include_localized_optional_context() -> None:
|
|
from deeptutor.core.context import UnifiedContext
|
|
|
|
prompts = {
|
|
"general": "通用",
|
|
"runtime_policy": "策略",
|
|
"loop": {
|
|
"system": "循环",
|
|
"user": "用户说:{user_message}",
|
|
"finish_exhausted": "预算已用完,请直接回答。",
|
|
},
|
|
}
|
|
ctx = UnifiedContext(
|
|
user_message="解释光合作用",
|
|
sidebar_context="[选中内容]\n把代码和静态数据加载进内存",
|
|
persona_context="用苏格拉底式提问",
|
|
memory_context="学生喜欢例子",
|
|
)
|
|
assembler = ChatPromptAssembler(prompts=prompts, language="zh")
|
|
|
|
blocks = assembler.blocks(context=ctx, tool_manifest="", workspace_note="工作区可用")
|
|
|
|
names = [block.name for block in blocks]
|
|
assert names[:4] == ["general", "runtime_context", "runtime_policy", "loop"]
|
|
assert "sidebar_tutor_context" in names
|
|
assert "persona_style" in names
|
|
assert "memory" in names
|
|
assert "workspace" in names
|
|
assert assembler.user_message(context=ctx) == "用户说:解释光合作用"
|
|
assert assembler.finish_exhausted_instruction() == "预算已用完,请直接回答。"
|
|
system_prompt = assembler.render(blocks)
|
|
assert "## sidebar_tutor_context" in system_prompt
|
|
assert "把代码和静态数据加载进内存" in system_prompt
|
|
|
|
|
|
@pytest.mark.parametrize("language", ["en", "zh"])
|
|
@pytest.mark.parametrize(
|
|
"mode",
|
|
[
|
|
"chat",
|
|
"deep_solve",
|
|
"ask_questions",
|
|
"immersive_reading",
|
|
"visualize",
|
|
"mastery_path",
|
|
],
|
|
)
|
|
def test_workspace_instructions_reach_shared_loop_prompts_and_stay_in_the_system(language, mode):
|
|
pipeline_type = MasteryLoopPipeline if mode == "mastery_path" else AgenticChatPipeline
|
|
pipeline = pipeline_type(language=language)
|
|
text = "先讲直觉。Keep {literal braces} and line breaks.\nThen show examples."
|
|
context = UnifiedContext(
|
|
active_capability=mode,
|
|
runtime=TurnRuntimeContext(workspace=WorkspaceRuntimeContext(instructions=text)),
|
|
)
|
|
prompt = pipeline._build_system_prompt([], context)
|
|
assert prompt.count("## workspace_instructions\n") == 1
|
|
assert text in prompt
|
|
assert "workspace_instructions" not in pipeline._runtime_snapshots
|
|
assert (
|
|
"当前能力必需的工作流程" in prompt
|
|
if language == "zh"
|
|
else "capability's required workflow" in prompt
|
|
)
|
|
context.runtime.workspace = WorkspaceRuntimeContext()
|
|
assert "## workspace_instructions\n" not in pipeline._build_system_prompt([], context)
|