feat: complete public wiki and embedded previews

This commit is contained in:
吉鑫升 committed 2026-08-24 20:41:24 +08:00
1 parent 007ed5a7de
commit 1736227723
18 files changed
+926 -48

No files matched your search

@@ -117,6 +117,23 @@ def test_weknora_url_rejects_credentials_and_non_http_schemes() -> None:
LlmWikiConfig.model_validate({"weknora": {"api_base_url": "http://user:pass@weknora.local"}})
def test_weknora_embed_follows_only_protected_binary_redirects() -> None:
from app.gateway.routers import llmwiki as llmwiki_router
assert llmwiki_router._should_follow_weknora_binary_redirect(
"/api/v1/knowledge/doc-one/preview"
)
assert llmwiki_router._should_follow_weknora_binary_redirect(
"/api/v1/knowledge-bases/kb-one/files"
)
assert not llmwiki_router._should_follow_weknora_binary_redirect(
"/api/v1/knowledge/doc-one"
)
assert not llmwiki_router._should_follow_weknora_binary_redirect(
"/api/v1/knowledge-bases/kb-one"
)
def test_agent_config_tracks_allowed_llmwiki_knowledge_bases() -> None:
config = AgentConfig(
name="Research agent",
@@ -547,6 +564,42 @@ async def test_weknora_client_imports_url_manual_content_and_lists_wiki_pages()
assert pages["pages"][0]["slug"] == "guide"
@pytest.mark.asyncio
async def test_weknora_client_reads_ordered_wiki_index_groups() -> None:
async def handler(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/v1/knowledgebase/kb-one/wiki/index"
assert request.url.params["types"] == "entity,concept"
assert request.url.params["limit"] == "50"
assert request.url.params["cursor"] == "next-page"
return httpx.Response(
200,
json={
"data": {
"intro": "# Wiki Index",
"version": 2,
"groups": [
{
"type": "entity",
"total": 1,
"items": [{"slug": "entity/one", "title": "实体一"}],
}
],
}
},
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http_client:
client = WeKnoraClient(base_url="http://weknora.local:8080", http_client=http_client)
index = await client.get_wiki_index(
"kb-one",
types=["entity", "concept"],
limit=50,
cursor="next-page",
)
assert index["groups"][0]["items"][0]["slug"] == "entity/one"
@pytest.mark.asyncio
async def test_weknora_client_creates_updates_and_deletes_wiki_pages() -> None:
seen: list[tuple[str, str]] = []
@@ -1,8 +1,12 @@
from __future__ import annotations
import json
import httpx
import pytest
from deerflow.config.llmwiki_config import LocalWikiIndexConfig
from deerflow.config.llmwiki_config import LocalWikiEmbeddingConfig, LocalWikiIndexConfig
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient
from deerflow.integrations.weknora.local_index.sync import WikiSyncService
from deerflow.integrations.weknora.local_index.vector_cache import WikiVectorCache
from deerflow.persistence.llmwiki_index.memory import MemoryLlmWikiIndexStore
@@ -53,6 +57,55 @@ class FakeWeKnora:
return dict(self.pages[slug])
@pytest.mark.asyncio
async def test_configured_dimensions_are_validated_but_not_sent_to_bge() -> None:
requests: list[dict] = []
def handler(request: httpx.Request) -> httpx.Response:
payload = json.loads(request.content)
requests.append(payload)
if "dimensions" in payload:
return httpx.Response(400, json={"detail": "dimensions is not supported"})
return httpx.Response(
200,
json={"data": [{"index": 0, "embedding": [1.0, 0.0, 0.0]}]},
)
config = LocalWikiEmbeddingConfig(
model="bge-embedding-m3",
base_url="http://embedding.test/v1",
dimensions=3,
max_retries=0,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http_client:
client = StrictWikiEmbeddingClient(config, http_client=http_client)
vector = await client.embed_query("Wiki page")
assert vector == [1.0, 0.0, 0.0]
assert requests == [{"model": "bge-embedding-m3", "input": ["Wiki page"]}]
@pytest.mark.asyncio
async def test_omitted_dimensions_are_discovered_from_first_response() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
json={"data": [{"index": 0, "embedding": [0.0, 1.0]}]},
)
config = LocalWikiEmbeddingConfig(
model="bge-embedding-m3",
base_url="http://embedding.test/v1",
dimensions=None,
max_retries=0,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as http_client:
client = StrictWikiEmbeddingClient(config, http_client=http_client)
await client.embed_query("dimension probe")
assert client.dimensions == 2
@pytest.mark.asyncio
async def test_sync_is_incremental_and_full_scan_deletes_missing_pages() -> None:
config = LocalWikiIndexConfig(chunk_min_chars=10, min_similarity=0)
@@ -38,6 +38,22 @@ class PublicStore:
return self.row
class PublicListStore:
def __init__(self, rows: list[dict[str, object]]) -> None:
self.rows = rows
self.calls: list[tuple[str, str, bool]] = []
async def list_visible(
self,
user_id: str,
*,
scope: str,
is_admin: bool,
) -> list[dict[str, object]]:
self.calls.append((user_id, scope, is_admin))
return self.rows
def _published_row() -> dict[str, object]:
return {
"id": "kb-one",
@@ -50,6 +66,39 @@ def _published_row() -> dict[str, object]:
}
@pytest.mark.asyncio
async def test_public_knowledge_base_list_exposes_only_published_wiki_identity() -> None:
store = PublicListStore(
[
{
**_published_row(),
"id": "1063edee-332a-43b4-bbd9-73bf0a708b94",
"owner_user_id": "secret-owner",
},
{**_published_row(), "id": "private-kb", "publication_status": "private"},
{
**_published_row(),
"id": "conversation-deposit",
"owner_user_id": "system",
"name": "对话沉淀",
},
]
)
payload = await llmwiki.list_public_knowledge_bases(_request(store))
assert store.calls == [("__public_wiki__", "public", False)]
assert payload == {
"knowledge_bases": [
{
"id": "1063edee-332a-43b4-bbd9-73bf0a708b94",
"name": "公开知识库",
}
],
"total": 1,
}
@pytest.mark.asyncio
async def test_public_wiki_reader_uses_anonymous_non_admin_authorization(monkeypatch) -> None:
store = PublicStore(_published_row())
@@ -126,3 +175,81 @@ async def test_public_wiki_page_list_is_normalized(monkeypatch) -> None:
assert payload["pages"][0]["slug"] == "guide/start"
assert payload["pages"][0]["aliases"] == []
@pytest.mark.asyncio
async def test_public_wiki_index_preserves_weknora_group_and_item_order(monkeypatch) -> None:
store = PublicStore(_published_row())
calls: list[tuple[tuple[str, ...], str]] = []
class Client:
async def get_wiki_index(
self,
knowledge_base_id: str,
*,
types: list[str],
limit: int,
cursor: str,
):
assert knowledge_base_id == "remote-one"
assert limit == 500
page_type = types[0]
calls.append((tuple(types), cursor))
if page_type == "concept" and not cursor:
return {
"intro": "# Wiki Index\n\n公开目录",
"version": 3,
"groups": [
{
"type": "concept",
"total": 2,
"next_cursor": "concept-next",
"items": [
{
"slug": "concept/first",
"title": "第一项",
"summary": "第一项摘要",
"category_path": ["流程图", "规范"],
}
],
}
],
}
if page_type == "concept":
return {
"intro": "# Wiki Index\n\n公开目录",
"version": 3,
"groups": [
{
"type": "concept",
"total": 2,
"items": [
{
"slug": "concept/second",
"title": "第二项",
"summary": "第二项摘要",
"category_path": ["流程图", "规范"],
}
],
}
],
}
return {
"intro": "# Wiki Index\n\n公开目录",
"version": 3,
"groups": [{"type": page_type, "total": 0, "items": []}],
}
monkeypatch.setattr(llmwiki, "_client_or_503", lambda: Client())
payload = await llmwiki.get_public_wiki_index(_request(store), "kb-one")
concept = next(group for group in payload["groups"] if group["type"] == "concept")
assert payload["intro"] == "# Wiki Index\n\n公开目录"
assert payload["version"] == 3
assert [item["slug"] for item in concept["items"]] == [
"concept/first",
"concept/second",
]
assert concept["total"] == 2
assert (("concept",), "concept-next") in calls