sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -7,6 +7,7 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
|
||||
from app.gateway.routers import system_settings as ss_router
|
||||
@@ -31,8 +32,12 @@ def test_load_returns_defaults_when_missing(tmp_runtime_home: Path) -> None:
|
||||
assert settings.prompt_prefix.enabled is False
|
||||
assert settings.prompt_prefix.prompt_prefix == ""
|
||||
assert settings.prompt_prefix.ordinary_qa_markdown_format_enabled is False
|
||||
assert settings.prompt_prefix.word_heading_numbering_style == "hybrid_decimal"
|
||||
assert settings.citation_display.reference_mode_enabled is False
|
||||
assert settings.citation_display.cleanup_words == []
|
||||
assert settings.memory.enabled is False
|
||||
assert settings.write_file_compatibility.enabled is False
|
||||
assert settings.conversation_context.recent_turn_window_enabled is False
|
||||
|
||||
|
||||
def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
|
||||
@@ -42,6 +47,7 @@ def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
|
||||
switch_label="加入立场前缀",
|
||||
prompt_prefix="你需要站在中方角度",
|
||||
ordinary_qa_markdown_format_enabled=True,
|
||||
word_heading_numbering_style="official_chinese",
|
||||
)
|
||||
)
|
||||
ss.save_system_settings(settings)
|
||||
@@ -56,6 +62,7 @@ def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
|
||||
assert reloaded.prompt_prefix.switch_label == "加入立场前缀"
|
||||
assert reloaded.prompt_prefix.prompt_prefix == "你需要站在中方角度"
|
||||
assert reloaded.prompt_prefix.ordinary_qa_markdown_format_enabled is True
|
||||
assert reloaded.prompt_prefix.word_heading_numbering_style == "official_chinese"
|
||||
|
||||
|
||||
def test_runtime_markdown_format_switch_defaults_off_and_updates_without_restart(
|
||||
@@ -103,11 +110,15 @@ async def test_prompt_settings_update_can_enable_markdown_format_without_changin
|
||||
ss.save_system_settings(current)
|
||||
|
||||
response = await ss_router.update_prompt_prefix(
|
||||
ss_router.PromptPrefixUpdateRequest(ordinary_qa_markdown_format_enabled=True),
|
||||
ss_router.PromptPrefixUpdateRequest(
|
||||
ordinary_qa_markdown_format_enabled=True,
|
||||
word_heading_numbering_style="chapter_section",
|
||||
),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert response.ordinary_qa_markdown_format_enabled is True
|
||||
assert response.word_heading_numbering_style == "chapter_section"
|
||||
assert response.enabled is True
|
||||
assert response.switch_label == "加入立场前缀"
|
||||
assert response.prompt_prefix == "你需要站在中方角度"
|
||||
@@ -139,6 +150,82 @@ async def test_citation_display_update_preserves_unspecified_fields(
|
||||
assert payload["citation_display"]["cleanup_words"] == ["广告", "免责声明"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_enable_memory_runtime_switch(
|
||||
tmp_runtime_home: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_patch_admin(monkeypatch)
|
||||
|
||||
response = await ss_router.update_memory_runtime_setting(
|
||||
ss_router.MemoryRuntimeUpdateRequest(enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert response.enabled is True
|
||||
assert ss.load_system_settings().memory.enabled is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_toggle_write_file_compatibility_without_restart(
|
||||
tmp_runtime_home: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_patch_admin(monkeypatch)
|
||||
|
||||
enabled = await ss_router.update_write_file_compatibility_setting(
|
||||
ss_router.WriteFileCompatibilityUpdateRequest(enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
assert enabled.enabled is True
|
||||
assert ss.get_write_file_compatibility_settings().enabled is True
|
||||
|
||||
disabled = await ss_router.update_write_file_compatibility_setting(
|
||||
ss_router.WriteFileCompatibilityUpdateRequest(enabled=False),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
assert disabled.enabled is False
|
||||
assert ss.get_write_file_compatibility_settings().enabled is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_enable_recent_turn_context_window(
|
||||
tmp_runtime_home: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_patch_admin(monkeypatch)
|
||||
|
||||
response = await ss_router.update_conversation_context_setting(
|
||||
ss_router.ConversationContextUpdateRequest(recent_turn_window_enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert response.recent_turn_window_enabled is True
|
||||
assert response.recent_turns == 3
|
||||
assert ss.load_system_settings().conversation_context.recent_turn_window_enabled is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_non_admin_cannot_enable_recent_turn_context_window(
|
||||
tmp_runtime_home: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
user = SimpleNamespace(id="user1", system_role="user", email="user@example.com")
|
||||
|
||||
async def _optional(_request):
|
||||
return user
|
||||
|
||||
monkeypatch.setattr(ss_router, "get_optional_user_from_request", _optional)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await ss_router.update_conversation_context_setting(
|
||||
ss_router.ConversationContextUpdateRequest(recent_turn_window_enabled=True),
|
||||
SimpleNamespace(),
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 403
|
||||
|
||||
|
||||
def test_prepend_prefix_tags_first_human_without_touching_content() -> None:
|
||||
graph_input = {"messages": [HumanMessage(content="评价一下中美贸易")]}
|
||||
out = _prepend_prompt_prefix(graph_input, "你需要站在中方角度")
|
||||
@@ -220,6 +307,52 @@ def test_prompt_prefix_middleware_block_content() -> None:
|
||||
assert patched[0].content[1] == {"type": "text", "text": "评价一下中美贸易"}
|
||||
|
||||
|
||||
def test_smart_knowledge_scope_uses_only_latest_real_user_turn() -> None:
|
||||
from deerflow.agents.middlewares.prompt_prefix_middleware import (
|
||||
SMART_KNOWLEDGE_SCOPE_PROMPT_KEY,
|
||||
PromptPrefixMiddleware,
|
||||
)
|
||||
|
||||
old_turn = HumanMessage(
|
||||
content="这个库讲了什么",
|
||||
additional_kwargs={SMART_KNOWLEDGE_SCOPE_PROMPT_KEY: "旧库:政策库"},
|
||||
)
|
||||
new_turn = HumanMessage(
|
||||
content="这个库有哪些新闻",
|
||||
additional_kwargs={SMART_KNOWLEDGE_SCOPE_PROMPT_KEY: "新库:新闻库"},
|
||||
)
|
||||
|
||||
patched = PromptPrefixMiddleware._apply(
|
||||
[old_turn, AIMessage(content="旧回答"), new_turn]
|
||||
)
|
||||
|
||||
assert patched is not None
|
||||
assert patched[0].content == "这个库讲了什么"
|
||||
assert patched[2].content == "新库:新闻库\n\n这个库有哪些新闻"
|
||||
assert "旧库:政策库" not in str(patched[2].content)
|
||||
|
||||
|
||||
def test_classic_latest_turn_does_not_revive_historical_smart_scope() -> None:
|
||||
from deerflow.agents.middlewares.prompt_prefix_middleware import (
|
||||
SMART_KNOWLEDGE_SCOPE_PROMPT_KEY,
|
||||
PromptPrefixMiddleware,
|
||||
)
|
||||
|
||||
old_smart_turn = HumanMessage(
|
||||
content="这个库讲了什么",
|
||||
additional_kwargs={SMART_KNOWLEDGE_SCOPE_PROMPT_KEY: "旧智能库范围"},
|
||||
)
|
||||
latest_classic_turn = HumanMessage(content="请继续回答")
|
||||
|
||||
patched = PromptPrefixMiddleware._apply(
|
||||
[old_smart_turn, AIMessage(content="旧回答"), latest_classic_turn]
|
||||
)
|
||||
|
||||
assert patched is None
|
||||
assert old_smart_turn.content == "这个库讲了什么"
|
||||
assert latest_classic_turn.content == "请继续回答"
|
||||
|
||||
|
||||
def test_opted_in_via_context() -> None:
|
||||
body = SimpleNamespace(context={"prompt_prefix_enabled": True}, metadata=None)
|
||||
assert _prompt_prefix_opted_in(body) is True
|
||||
|
||||
Reference in new issue
Block a user