feat: add public wiki embeds and ES query routing
This commit is contained in:
1 parent
e818c24769
commit
bc49df4561
16 files changed
+704
-52
No files matched your search
@@ -0,0 +1,70 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.agents.lead_agent import prompt as prompt_module
|
||||
from deerflow.config.skills_config import EsQueryRoutingConfig, SkillsConfig
|
||||
|
||||
|
||||
def _app_config(*, enabled: bool, prompt: str = "自定义 ES 路由规则") -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
skills=SkillsConfig(
|
||||
es_query_routing=EsQueryRoutingConfig(enabled=enabled, prompt=prompt),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_es_query_routing_config_is_disabled_by_default() -> None:
|
||||
config = SkillsConfig()
|
||||
|
||||
assert config.es_query_routing.enabled is False
|
||||
assert "es_query" in config.es_query_routing.prompt
|
||||
|
||||
|
||||
def test_es_query_routing_section_honors_switch_prompt_and_allowlist() -> None:
|
||||
enabled = _app_config(enabled=True, prompt=" 自定义 ES 路由规则 ")
|
||||
|
||||
assert prompt_module._build_es_query_routing_section(app_config=enabled) == (
|
||||
'<skill_routing skill="es_query">\n自定义 ES 路由规则\n</skill_routing>'
|
||||
)
|
||||
assert (
|
||||
prompt_module._build_es_query_routing_section(
|
||||
app_config=enabled,
|
||||
available_skills={"other_skill"},
|
||||
)
|
||||
== ""
|
||||
)
|
||||
assert "自定义 ES 路由规则" in prompt_module._build_es_query_routing_section(
|
||||
app_config=enabled,
|
||||
available_skills={"es_query"},
|
||||
)
|
||||
assert prompt_module._build_es_query_routing_section(app_config=_app_config(enabled=False)) == ""
|
||||
assert prompt_module._build_es_query_routing_section(app_config=_app_config(enabled=True, prompt=" ")) == ""
|
||||
|
||||
|
||||
def test_es_query_routing_is_injected_into_lead_and_allowed_custom_agent_prompts(monkeypatch) -> None:
|
||||
config = _app_config(enabled=True)
|
||||
monkeypatch.setattr(prompt_module, "_get_memory_context", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "get_agent_soul", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "get_skills_prompt_section", lambda *args, **kwargs: "<skill_system />")
|
||||
monkeypatch.setattr(prompt_module, "get_deferred_tools_prompt_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_build_acp_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_build_custom_mounts_section", lambda *args, **kwargs: "")
|
||||
monkeypatch.setattr(prompt_module, "_current_time_context", lambda: "")
|
||||
|
||||
lead_prompt = prompt_module.apply_prompt_template(app_config=config)
|
||||
allowed_custom_prompt = prompt_module.apply_agent_only_prompt_template(
|
||||
agent_id="agent-with-es",
|
||||
agent_name="Agent with ES",
|
||||
available_skills={"es_query"},
|
||||
app_config=config,
|
||||
)
|
||||
denied_custom_prompt = prompt_module.apply_agent_only_prompt_template(
|
||||
agent_id="agent-without-es",
|
||||
agent_name="Agent without ES",
|
||||
available_skills={"other_skill"},
|
||||
app_config=config,
|
||||
)
|
||||
|
||||
assert '<skill_routing skill="es_query">' in lead_prompt
|
||||
assert "自定义 ES 路由规则" in lead_prompt
|
||||
assert '<skill_routing skill="es_query">' in allowed_custom_prompt
|
||||
assert '<skill_routing skill="es_query">' not in denied_custom_prompt
|
||||
@@ -0,0 +1,128 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from starlette.requests import Request
|
||||
|
||||
from app.gateway.routers import llmwiki
|
||||
|
||||
|
||||
def _request(store: object) -> Request:
|
||||
return Request(
|
||||
{
|
||||
"type": "http",
|
||||
"method": "GET",
|
||||
"path": "/api/public/llmwiki/knowledge-bases/kb-one",
|
||||
"headers": [],
|
||||
"app": SimpleNamespace(state=SimpleNamespace(llmwiki_store=store)),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class PublicStore:
|
||||
def __init__(self, row: dict[str, object] | None) -> None:
|
||||
self.row = row
|
||||
self.calls: list[tuple[str, str, bool, bool]] = []
|
||||
|
||||
async def get_authorized(
|
||||
self,
|
||||
mapping_id: str,
|
||||
user_id: str,
|
||||
*,
|
||||
write: bool,
|
||||
is_admin: bool,
|
||||
) -> dict[str, object] | None:
|
||||
self.calls.append((mapping_id, user_id, write, is_admin))
|
||||
return self.row
|
||||
|
||||
|
||||
def _published_row() -> dict[str, object]:
|
||||
return {
|
||||
"id": "kb-one",
|
||||
"weknora_id": "remote-one",
|
||||
"owner_user_id": "owner-one",
|
||||
"name": "公开知识库",
|
||||
"description": "公开说明",
|
||||
"kb_type": "document",
|
||||
"publication_status": "published",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_public_wiki_reader_uses_anonymous_non_admin_authorization(monkeypatch) -> None:
|
||||
store = PublicStore(_published_row())
|
||||
|
||||
class Client:
|
||||
async def get_knowledge_base(self, knowledge_base_id: str):
|
||||
assert knowledge_base_id == "remote-one"
|
||||
return {"id": knowledge_base_id, "name": "公开知识库", "knowledge_count": 2}
|
||||
|
||||
monkeypatch.setattr(llmwiki, "_client_or_503", lambda: Client())
|
||||
|
||||
payload = await llmwiki.get_public_knowledge_base(_request(store), "kb-one")
|
||||
|
||||
assert store.calls == [("kb-one", "__public_wiki__", False, False)]
|
||||
assert payload["publication_status"] == "published"
|
||||
assert payload["owner_user_id"] is None
|
||||
assert payload["can_write"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"row",
|
||||
[
|
||||
{**_published_row(), "publication_status": "private"},
|
||||
{
|
||||
**_published_row(),
|
||||
"owner_user_id": "system",
|
||||
"name": "对话沉淀",
|
||||
},
|
||||
None,
|
||||
],
|
||||
)
|
||||
async def test_public_wiki_reader_rejects_non_public_resources(row) -> None:
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await llmwiki._public_mapping(_request(PublicStore(row)), "kb-one")
|
||||
|
||||
assert exc_info.value.status_code == 404
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_public_wiki_page_list_is_normalized(monkeypatch) -> None:
|
||||
store = PublicStore(_published_row())
|
||||
|
||||
class Client:
|
||||
async def list_wiki_pages(self, knowledge_base_id: str, **kwargs):
|
||||
assert knowledge_base_id == "remote-one"
|
||||
assert kwargs == {"page": 1, "page_size": 50, "query": "目录"}
|
||||
return {
|
||||
"pages": [
|
||||
{
|
||||
"id": "page-one",
|
||||
"slug": "guide/start",
|
||||
"title": "开始使用",
|
||||
"page_type": "article",
|
||||
"status": "completed",
|
||||
"content": "# 开始使用",
|
||||
}
|
||||
],
|
||||
"total": 1,
|
||||
"page": 1,
|
||||
"page_size": 50,
|
||||
"total_pages": 1,
|
||||
}
|
||||
|
||||
monkeypatch.setattr(llmwiki, "_client_or_503", lambda: Client())
|
||||
|
||||
payload = await llmwiki.list_public_wiki_pages(
|
||||
_request(store),
|
||||
"kb-one",
|
||||
page=1,
|
||||
page_size=50,
|
||||
query=" 目录 ",
|
||||
)
|
||||
|
||||
assert payload["pages"][0]["slug"] == "guide/start"
|
||||
assert payload["pages"][0]["aliases"] == []
|
||||
Reference in new issue
Block a user