feat: complete public wiki and embedded previews

This commit is contained in:
吉鑫升 committed 2026-08-24 20:40:44 +08:00
1 parent bc49df4561
commit 30943159bf
18 files changed
+926 -48

No files matched your search

@@ -483,6 +483,21 @@ def _safe_response_headers(headers: httpx.Headers) -> dict[str, str]:
return result
def _should_follow_weknora_binary_redirect(path: str) -> bool:
"""Keep protected preview/image bytes inside the DeerFlow proxy."""
segments = _path_segments(path)
return (
len(segments) == 5
and segments[:3] == ["api", "v1", "knowledge"]
and segments[4] == "preview"
) or (
len(segments) == 5
and segments[:3] == ["api", "v1", "knowledge-bases"]
and segments[4] == "files"
)
def _forward_request_headers(request: Request) -> dict[str, str]:
blocked = {
"host",
@@ -1571,8 +1586,13 @@ async def _proxy_weknora_request(
headers["X-Tenant-ID"] = tenant_id
url = f"{target_base_url.rstrip('/')}{_encode_upstream_path(upstream_path)}"
follow_binary_redirects = is_api and _should_follow_weknora_binary_redirect(upstream_path)
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0), follow_redirects=False, trust_env=False) as client:
async with httpx.AsyncClient(
timeout=httpx.Timeout(60.0),
follow_redirects=follow_binary_redirects,
trust_env=False,
) as client:
response = await client.request(
request.method,
url,
@@ -1586,7 +1606,11 @@ async def _proxy_weknora_request(
tenant_id = _tenant_id_from_session(session)
if tenant_id:
headers["X-Tenant-ID"] = tenant_id
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0), follow_redirects=False, trust_env=False) as client:
async with httpx.AsyncClient(
timeout=httpx.Timeout(60.0),
follow_redirects=follow_binary_redirects,
trust_env=False,
) as client:
response = await client.request(
request.method,
url,
@@ -2417,6 +2441,27 @@ async def test_llmwiki_connection(request: Request, body: LlmWikiConnectionTest)
return {"success": True, "provider": "weknora", "health": health}
@router.get("/api/public/llmwiki/knowledge-bases")
async def list_public_knowledge_bases(request: Request) -> dict[str, Any]:
"""List the minimal identity of every knowledge base published as a public Wiki."""
rows = await _store(request).list_visible(
"__public_wiki__",
scope="public",
is_admin=False,
)
knowledge_bases = [
{
"id": str(row["id"]),
"name": str(row.get("name") or ""),
}
for row in rows
if row.get("publication_status") == "published"
and not _is_conversation_deposit_mapping(row)
]
return {"knowledge_bases": knowledge_bases, "total": len(knowledge_bases)}
@router.get("/api/public/llmwiki/knowledge-bases/{mapping_id}")
async def get_public_knowledge_base(request: Request, mapping_id: str) -> dict[str, Any]:
"""Return the safe public metadata used by the chrome-free Wiki reader."""
@@ -2465,6 +2510,89 @@ async def list_public_wiki_pages(
return {**result, "pages": pages}
_PUBLIC_WIKI_INDEX_TYPES = ("summary", "entity", "concept", "synthesis", "comparison")
def _public_wiki_index_item(value: Any) -> dict[str, Any] | None:
if not isinstance(value, dict):
return None
slug = str(value.get("slug") or "").strip()
if not slug:
return None
return {
"slug": slug,
"title": str(value.get("title") or slug),
"summary": str(value.get("summary") or ""),
"category_path": (
[str(item) for item in value.get("category_path", []) if str(item).strip()]
if isinstance(value.get("category_path"), list)
else []
),
"wiki_path": str(value.get("wiki_path") or ""),
"depth": int(value.get("depth") or 0),
}
async def _all_public_wiki_index(client: WeKnoraClient, remote_id: str) -> dict[str, Any]:
intro = ""
version = 0
groups: list[dict[str, Any]] = []
for page_type in _PUBLIC_WIKI_INDEX_TYPES:
items: list[dict[str, Any]] = []
cursor = ""
seen_cursors: set[str] = set()
total = 0
for _page_number in range(100):
payload = await client.get_wiki_index(
remote_id,
types=[page_type],
limit=500,
cursor=cursor,
)
intro = intro or str(payload.get("intro") or "")
version = max(version, int(payload.get("version") or 0))
raw_groups = payload.get("groups") if isinstance(payload.get("groups"), list) else []
group = next(
(
value
for value in raw_groups
if isinstance(value, dict) and value.get("type") == page_type
),
{},
)
total = max(total, int(group.get("total") or 0))
raw_items = group.get("items") if isinstance(group.get("items"), list) else []
items.extend(
item
for value in raw_items
if (item := _public_wiki_index_item(value)) is not None
)
next_cursor = str(group.get("next_cursor") or "")
if not next_cursor or next_cursor in seen_cursors:
break
seen_cursors.add(next_cursor)
cursor = next_cursor
else:
logger.warning(
"Stopped public Wiki index pagination at safety limit: mapping_remote_id=%s page_type=%s",
remote_id,
page_type,
)
groups.append({"type": page_type, "total": total or len(items), "items": items})
return {"intro": intro, "version": version, "groups": groups}
@router.get("/api/public/llmwiki/knowledge-bases/{mapping_id}/wiki/index")
async def get_public_wiki_index(request: Request, mapping_id: str) -> dict[str, Any]:
"""Return WeKnora's ordered dynamic Wiki index for the public reader."""
row = await _public_mapping(request, mapping_id)
try:
return await _all_public_wiki_index(_client_or_503(), str(row["weknora_id"]))
except WeKnoraError as exc:
_raise_upstream(exc)
@router.get("/api/public/llmwiki/knowledge-bases/{mapping_id}/wiki/pages/{slug:path}")
async def get_public_wiki_page(
request: Request,