"""cmzs LLMWiki BFF backed by an independently deployed WeKnora.""" from __future__ import annotations import hashlib import json import logging import re import time from dataclasses import replace from typing import Any, Literal from urllib.parse import quote, urlencode import httpx from fastapi import APIRouter, BackgroundTasks, File, HTTPException, Query, Request, UploadFile from fastapi.responses import JSONResponse, RedirectResponse, Response from pydantic import BaseModel, Field from app.gateway.csrf_middleware import is_secure_request from app.gateway.deps import get_agent_store, get_optional_user_from_request, get_thread_store from app.gateway.llmwiki_deposit import ( CONVERSATION_DEPOSIT_KB_DESCRIPTION, CONVERSATION_DEPOSIT_KB_NAME, CONVERSATION_DEPOSIT_OWNER_USER_ID, ConversationTurnDeposit, deposit_conversation_turn, ensure_conversation_deposit_mapping, ) from app.gateway.weknora_embed import ( WEKNORA_EMBED_COOKIE as _WEKNORA_EMBED_COOKIE, WEKNORA_EMBED_API_PREFIX as _WEKNORA_EMBED_API_PREFIX, WEKNORA_EMBED_TTL_SECONDS as _WEKNORA_EMBED_TTL_SECONDS, WeKnoraEmbedContext, sign_weknora_embed_payload as _sign_embed_payload, verify_weknora_embed_cookie as _verify_embed_cookie, ) from deerflow.config.agents_config import load_agent_config from deerflow.config.llmwiki_config import LlmWikiRuntimeOverride, normalize_service_url from deerflow.config.system_settings import load_system_settings, save_system_settings from deerflow.integrations.weknora.client import WeKnoraClient, WeKnoraError from deerflow.integrations.weknora.runtime import build_weknora_client, get_resolved_llmwiki_runtime from deerflow.persistence.llmwiki import LlmWikiStore logger = logging.getLogger(__name__) router = APIRouter(tags=["llmwiki"]) proxy_router = APIRouter(tags=["llmwiki-proxy"]) _MAX_UPLOAD_BYTES = 100 * 1024 * 1024 _WEKNORA_ADMIN_SESSION_CACHE: dict[str, dict[str, Any]] = {} _HOP_BY_HOP_RESPONSE_HEADERS = { "connection", "content-encoding", "content-length", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade", "x-frame-options", "content-security-policy", } class LlmWikiSettingsUpdate(BaseModel): override_enabled: bool = True api_base_url: str = "" web_base_url: str = "" class LlmWikiConnectionTest(BaseModel): api_base_url: str | None = None class KnowledgeBaseCreate(BaseModel): name: str = Field(..., min_length=1, max_length=255) description: str = Field(default="", max_length=2048) type: Literal["document", "faq"] = "document" wiki_enabled: bool = False class KnowledgeBaseUpdate(BaseModel): name: str | None = Field(default=None, min_length=1, max_length=255) description: str | None = Field(default=None, max_length=2048) class SearchRequest(BaseModel): query: str = Field(..., min_length=1, max_length=4000) scope: Literal["personal", "public", "all"] = "all" knowledge_base_ids: list[str] = Field(default_factory=list, max_length=100) class KnowledgeUrlImport(BaseModel): url: str = Field(..., min_length=1, max_length=4096) class ManualKnowledgeCreate(BaseModel): title: str = Field(..., min_length=1, max_length=255) content: str = Field(..., min_length=1, max_length=2_000_000) class ConversationDepositCreate(BaseModel): thread_id: str = Field(..., min_length=1, max_length=128) question: str = Field(default="", max_length=200_000) answer: str = Field(default="", max_length=2_000_000) human_message_id: str | None = Field(default=None, max_length=128) assistant_message_id: str = Field(..., min_length=1, max_length=128) assistant_id: str | None = Field(default=None, max_length=128) knowledge_base_ids: list[str] = Field(default_factory=list, max_length=100) def _store(request: Request) -> LlmWikiStore: value = getattr(request.app.state, "llmwiki_store", None) if value is None: raise HTTPException(status_code=503, detail="LLMWiki metadata store is unavailable") return value async def _actor(request: Request) -> tuple[str, bool]: user = await get_optional_user_from_request(request) if user is None: return "default", True return str(user.id), getattr(user, "system_role", None) == "admin" async def _require_admin(request: Request) -> tuple[str, bool]: actor = await _actor(request) if not actor[1]: raise HTTPException(status_code=403, detail="Only administrators can configure WeKnora") return actor def _runtime_or_legacy() -> dict[str, Any]: runtime = get_resolved_llmwiki_runtime() return { "provider": runtime.provider, "api_base_url": runtime.api_base_url, "web_base_url": runtime.web_base_url, "override_enabled": runtime.override_enabled, } def _client_or_503() -> WeKnoraClient: runtime = get_resolved_llmwiki_runtime() if not runtime.weknora_enabled: raise HTTPException(status_code=409, detail="WeKnora mode is not enabled") try: return build_weknora_client(runtime) except RuntimeError as exc: raise HTTPException(status_code=503, detail=str(exc)) from None def _raise_upstream(exc: WeKnoraError) -> None: status = 422 if exc.status_code == 400 else exc.status_code if exc.status_code in {404, 502, 504} else 502 raise HTTPException(status_code=status, detail=str(exc)) from None def _json_response(payload: Any, *, status_code: int = 200) -> Response: return Response( content=json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8"), status_code=status_code, media_type="application/json", ) def _runtime_for_iframe_proxy(request: Request | None = None): runtime = get_resolved_llmwiki_runtime(getattr(request.app.state, "config", None) if request is not None else None) if not runtime.weknora_enabled: raise HTTPException(status_code=409, detail="WeKnora mode is not enabled") if not runtime.web_base_url: raise HTTPException(status_code=422, detail="WeKnora web_base_url is required for iframe embedding") if not runtime.admin_email or not runtime.admin_password: raise HTTPException( status_code=422, detail="Configure llmwiki.weknora.admin_email and admin_password for WeKnora iframe SSO", ) return runtime def _admin_session_cache_key(runtime: Any) -> str: digest = hashlib.sha256(runtime.admin_password.encode("utf-8")).hexdigest()[:12] return f"{runtime.api_base_url}|{runtime.admin_email}|{digest}" def _extract_weknora_token(payload: dict[str, Any]) -> tuple[str, str, dict[str, Any] | None, dict[str, Any] | None]: source = payload.get("data") if isinstance(payload.get("data"), dict) else payload token = str(source.get("token") or source.get("access_token") or payload.get("token") or payload.get("access_token") or "") refresh_token = str( source.get("refresh_token") or source.get("refreshToken") or payload.get("refresh_token") or payload.get("refreshToken") or "" ) user = source.get("user") if isinstance(source.get("user"), dict) else payload.get("user") tenant = ( source.get("tenant") if isinstance(source.get("tenant"), dict) else source.get("active_tenant") if isinstance(source.get("active_tenant"), dict) else payload.get("tenant") ) return token, refresh_token, user if isinstance(user, dict) else None, tenant if isinstance(tenant, dict) else None async def _get_weknora_admin_session(runtime: Any, *, force_refresh: bool = False) -> dict[str, Any]: key = _admin_session_cache_key(runtime) cached = _WEKNORA_ADMIN_SESSION_CACHE.get(key) now = time.time() if cached and not force_refresh and float(cached.get("expires_at") or 0) > now + 60: return cached try: async with httpx.AsyncClient(timeout=httpx.Timeout(20.0), follow_redirects=False) as client: response = await client.post( f"{runtime.api_base_url}/api/v1/auth/login", json={"email": runtime.admin_email, "password": runtime.admin_password}, headers={"Accept": "application/json"}, ) except httpx.TimeoutException as exc: raise HTTPException(status_code=504, detail="WeKnora admin login timed out") from exc except httpx.HTTPError as exc: raise HTTPException(status_code=502, detail="WeKnora admin login is unavailable") from exc if response.status_code >= 400: raise HTTPException(status_code=502, detail=f"WeKnora admin login failed ({response.status_code})") try: payload = response.json() except ValueError as exc: raise HTTPException(status_code=502, detail="WeKnora admin login returned invalid JSON") from exc if isinstance(payload, dict) and payload.get("success") is False: raise HTTPException(status_code=502, detail=str(payload.get("message") or "WeKnora admin login failed")) token, refresh_token, user, tenant = _extract_weknora_token(payload if isinstance(payload, dict) else {}) if not token: raise HTTPException(status_code=502, detail="WeKnora admin login did not return a token") session = { "token": token, "refresh_token": refresh_token, "user": user or {}, "tenant": tenant or {}, # WeKnora tokens do not consistently expose expiry in the login body. # Relogin periodically and retry once on upstream 401. "expires_at": now + 25 * 60, } _WEKNORA_ADMIN_SESSION_CACHE[key] = session return session def _tenant_id_from_session(session: dict[str, Any]) -> str: tenant = session.get("tenant") if isinstance(session.get("tenant"), dict) else {} user = session.get("user") if isinstance(session.get("user"), dict) else {} return str(tenant.get("id") or user.get("tenant_id") or "") async def _read_embed_context(request: Request) -> WeKnoraEmbedContext: ctx = _verify_embed_cookie(request.cookies.get(_WEKNORA_EMBED_COOKIE)) if not ctx.mapping_id or not ctx.weknora_id or not ctx.user_id: raise HTTPException(status_code=401, detail="WeKnora iframe session is invalid") store = _store(request) readable = await store.get_authorized(ctx.mapping_id, ctx.user_id, write=False, is_admin=ctx.is_admin) if readable is None or str(readable.get("weknora_id") or "") != ctx.weknora_id: raise HTTPException(status_code=403, detail="WeKnora iframe session is no longer authorized") writable = None if not _is_conversation_deposit_mapping(readable): writable = await store.get_authorized(ctx.mapping_id, ctx.user_id, write=True, is_admin=ctx.is_admin) return replace( ctx, can_write=writable is not None, is_conversation_deposit=_is_conversation_deposit_mapping(readable), ) def _encode_upstream_path(path: str) -> str: if not path.startswith("/"): path = f"/{path}" return "/".join(quote(part, safe="") for part in path.split("/")) def _safe_response_headers(headers: httpx.Headers) -> dict[str, str]: result: dict[str, str] = {} for key, value in headers.items(): if key.lower() in _HOP_BY_HOP_RESPONSE_HEADERS: continue if key.lower() == "set-cookie": continue result[key] = value return result def _forward_request_headers(request: Request) -> dict[str, str]: blocked = { "host", "connection", "content-length", "cookie", "authorization", "x-forwarded-host", "x-forwarded-for", "x-forwarded-proto", } headers = {key: value for key, value in request.headers.items() if key.lower() not in blocked} headers.setdefault("Accept", "*/*") return headers def _path_segments(path: str) -> list[str]: return [segment for segment in path.strip("/").split("/") if segment] def _json_from_body(body: bytes, content_type: str | None) -> Any: if not body or "application/json" not in (content_type or "").lower(): return None try: return json.loads(body) except ValueError: return None def _body_kb_id(body: Any) -> str: return str(body.get("kb_id") or body.get("knowledge_base_id") or body.get("source_kb_id") or "") if isinstance(body, dict) else "" def _request_kb_id(request: Request, body: Any) -> str: return _body_kb_id(body) or str( request.query_params.get("kb_id") or request.query_params.get("knowledge_base_id") or request.query_params.get("source_kb_id") or "" ) async def _admin_json(runtime: Any, path: str, *, token: str, params: dict[str, Any] | None = None) -> dict[str, Any]: try: async with httpx.AsyncClient(timeout=httpx.Timeout(20.0), follow_redirects=False) as client: response = await client.get( f"{runtime.api_base_url}{_encode_upstream_path(path)}", params=params, headers={"Accept": "application/json", "Authorization": f"Bearer {token}"}, ) except httpx.HTTPError as exc: raise HTTPException(status_code=502, detail="WeKnora verification request failed") from exc if response.status_code == 404: raise HTTPException(status_code=404, detail="WeKnora resource was not found") if response.status_code >= 400: raise HTTPException(status_code=502, detail=f"WeKnora verification failed ({response.status_code})") try: payload = response.json() except ValueError as exc: raise HTTPException(status_code=502, detail="WeKnora verification returned invalid JSON") from exc return payload if isinstance(payload, dict) else {} def _payload_data(payload: dict[str, Any]) -> Any: return payload.get("data", payload) async def _verify_proxy_document_belongs_to_kb(runtime: Any, ctx: WeKnoraEmbedContext, document_id: str, token: str) -> None: payload = await _admin_json(runtime, f"/api/v1/knowledge/{document_id}", token=token) data = _payload_data(payload) if not isinstance(data, dict) or str(data.get("knowledge_base_id") or "") != ctx.weknora_id: raise HTTPException(status_code=403, detail="Document is outside the authorized WeKnora knowledge base") async def _verify_proxy_chunk_belongs_to_kb(runtime: Any, ctx: WeKnoraEmbedContext, chunk_id: str, token: str) -> None: payload = await _admin_json(runtime, f"/api/v1/chunks/by-id/{chunk_id}", token=token) data = _payload_data(payload) if not isinstance(data, dict) or str(data.get("knowledge_base_id") or "") != ctx.weknora_id: raise HTTPException(status_code=403, detail="Chunk is outside the authorized WeKnora knowledge base") async def _authorize_weknora_api_proxy( request: Request, ctx: WeKnoraEmbedContext, runtime: Any, path: str, body: bytes, token: str, ) -> None: method = request.method.upper() if method == "OPTIONS": return read_method = method in {"GET", "HEAD"} write_method = method in {"POST", "PUT", "PATCH", "DELETE"} if write_method and not ctx.can_write: raise HTTPException(status_code=403, detail="Current cmzs user cannot write this knowledge base") segments = _path_segments(path) if len(segments) < 2 or segments[0] != "api" or segments[1] != "v1": raise HTTPException(status_code=403, detail="Unsupported WeKnora API path") tail = segments[2:] json_body = _json_from_body(body, request.headers.get("content-type")) if tail[:1] == ["auth"]: if len(tail) >= 2 and tail[1] in {"me", "tenant", "validate", "refresh"}: return raise HTTPException(status_code=403, detail="Only WeKnora session introspection is allowed") if tail[:1] == ["knowledge-bases"]: if len(tail) == 1: if method == "GET": return raise HTTPException(status_code=403, detail="Creating or deleting other WeKnora knowledge bases is not allowed") if tail[1] != ctx.weknora_id: raise HTTPException(status_code=403, detail="Only the current WeKnora knowledge base is allowed") if read_method or (write_method and ctx.can_write): return if tail[:1] == ["knowledgebase"]: if len(tail) < 2 or tail[1] != ctx.weknora_id: raise HTTPException(status_code=403, detail="Only the current WeKnora wiki is allowed") if read_method or (write_method and ctx.can_write): return if tail[:1] == ["knowledge"]: if len(tail) == 1: raise HTTPException(status_code=403, detail="Global WeKnora knowledge collection is not allowed") action = tail[1] if action in {"batch", "batch-delete", "batch-reparse", "folder", "move", "tags"}: if _request_kb_id(request, json_body) != ctx.weknora_id: raise HTTPException(status_code=403, detail="Batch operation must be scoped to the current knowledge base") return if action in {"search"}: raise HTTPException(status_code=403, detail="Global WeKnora search is disabled in embedded detail mode") document_id = tail[2] if action == "manual" and len(tail) >= 3 else action await _verify_proxy_document_belongs_to_kb(runtime, ctx, document_id, token) return if tail[:1] == ["chunks"]: if len(tail) < 2: raise HTTPException(status_code=403, detail="Chunk path is incomplete") if tail[1] == "by-id": if len(tail) < 3: raise HTTPException(status_code=403, detail="Chunk path is incomplete") await _verify_proxy_chunk_belongs_to_kb(runtime, ctx, tail[2], token) return await _verify_proxy_document_belongs_to_kb(runtime, ctx, tail[1], token) return if tail[:1] == ["knowledge-search"]: ids = json_body.get("knowledge_base_ids") if isinstance(json_body, dict) else None if ids == [ctx.weknora_id] or ids == (ctx.weknora_id,): return raise HTTPException(status_code=403, detail="Search must be scoped to the current WeKnora knowledge base") # A few read-only metadata endpoints are required by the original detail # page to render upload controls and current-tenant state. They do not # expose other knowledge-base contents. if read_method and tail and tail[0] in { "models", "system", "storage-backends", "vector-stores", "web-search-providers", "mcp-services", }: return raise HTTPException(status_code=403, detail="This WeKnora endpoint is blocked in embedded detail mode") def _filter_knowledge_base_list_response(response: httpx.Response, ctx: WeKnoraEmbedContext) -> bytes | None: content_type = response.headers.get("content-type", "") if "application/json" not in content_type.lower(): return None try: payload = response.json() except ValueError: return None if not isinstance(payload, dict): return None def keep_current(value: Any) -> Any: if isinstance(value, list): return [item for item in value if isinstance(item, dict) and str(item.get("id") or "") == ctx.weknora_id] if isinstance(value, dict): result = dict(value) for key in ("items", "list", "knowledge_bases", "knowledge", "records", "data"): if isinstance(result.get(key), list): result[key] = keep_current(result[key]) result["total"] = len(result[key]) return result return value if isinstance(payload.get("data"), list): payload["data"] = keep_current(payload["data"]) payload["total"] = len(payload["data"]) elif isinstance(payload.get("data"), dict): payload["data"] = keep_current(payload["data"]) else: for key in ("items", "list", "knowledge_bases", "records"): if isinstance(payload.get(key), list): payload[key] = keep_current(payload[key]) payload["total"] = len(payload[key]) return json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8") def _escape_script_json(value: Any) -> str: return json.dumps(value, ensure_ascii=False, separators=(",", ":")).replace(" str: tenant_id = _tenant_id_from_session(session) user = session.get("user") if isinstance(session.get("user"), dict) else {} tenant = session.get("tenant") if isinstance(session.get("tenant"), dict) else {} payload = { "allowedPath": allowed_path, "tab": tab, "canWrite": ctx.can_write, "token": session.get("token") or "", "refreshToken": session.get("refresh_token") or "", "tenantId": tenant_id, "user": user, "tenant": tenant, "apiPrefix": _WEKNORA_EMBED_API_PREFIX, } data = _escape_script_json(payload) return f""" """ def _inject_weknora_embed_html(html: bytes, *, allowed_path: str, tab: str, session: dict[str, Any], ctx: WeKnoraEmbedContext) -> bytes: text = html.decode("utf-8", errors="replace") injection = _weknora_embed_injection(allowed_path=allowed_path, tab=tab, session=session, ctx=ctx) if "" in text: text = text.replace("", f"{injection}", 1) else: text = f"{injection}{text}" return text.encode("utf-8") async def _proxy_weknora_request( request: Request, *, target_base_url: str, upstream_path: str, ctx: WeKnoraEmbedContext, is_api: bool, tab: str = "wiki", ) -> Response: runtime = _runtime_for_iframe_proxy(request) session = await _get_weknora_admin_session(runtime) token = str(session.get("token") or "") body = await request.body() if is_api: await _authorize_weknora_api_proxy(request, ctx, runtime, upstream_path, body, token) headers = _forward_request_headers(request) if is_api: headers["Authorization"] = f"Bearer {token}" tenant_id = _tenant_id_from_session(session) if tenant_id: headers["X-Tenant-ID"] = tenant_id url = f"{target_base_url.rstrip('/')}{_encode_upstream_path(upstream_path)}" try: async with httpx.AsyncClient(timeout=httpx.Timeout(60.0), follow_redirects=False) as client: response = await client.request( request.method, url, params=request.query_params.multi_items(), headers=headers, content=body, ) if is_api and response.status_code == 401: session = await _get_weknora_admin_session(runtime, force_refresh=True) headers["Authorization"] = f"Bearer {session.get('token')}" async with httpx.AsyncClient(timeout=httpx.Timeout(60.0), follow_redirects=False) as client: response = await client.request( request.method, url, params=request.query_params.multi_items(), headers=headers, content=body, ) except httpx.TimeoutException as exc: raise HTTPException(status_code=504, detail="WeKnora proxy request timed out") from exc except httpx.HTTPError as exc: raise HTTPException(status_code=502, detail="WeKnora proxy request failed") from exc content = response.content content_type = response.headers.get("content-type", "") if is_api and request.method.upper() == "GET" and upstream_path.rstrip("/") == "/api/v1/knowledge-bases": content = _filter_knowledge_base_list_response(response, ctx) or content if is_api and ctx.is_conversation_deposit and "application/json" in content_type.lower(): try: payload = json.loads(content) except (TypeError, ValueError, json.JSONDecodeError): pass else: content = json.dumps( _sanitize_conversation_deposit_value(payload), ensure_ascii=False, separators=(",", ":"), ).encode("utf-8") if not is_api and "text/html" in content_type.lower(): allowed_path = f"/platform/knowledge-bases/{quote(ctx.weknora_id, safe='')}" content = _inject_weknora_embed_html(content, allowed_path=allowed_path, tab=tab, session=session, ctx=ctx) content_type = "text/html; charset=utf-8" headers_out = _safe_response_headers(response.headers) headers_out["Cache-Control"] = "no-store" if is_api or "text/html" in content_type.lower() else "private, max-age=3600" return Response( content=content, status_code=response.status_code, media_type=content_type.split(";", 1)[0] if content_type else None, headers=headers_out, ) def _is_conversation_deposit_mapping(row: dict[str, Any]) -> bool: return ( str(row.get("owner_user_id") or "") == CONVERSATION_DEPOSIT_OWNER_USER_ID and str(row.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME ) _LEGACY_BRAND_PATTERN = re.compile(r"deer[\s_-]*flow", re.IGNORECASE) def _sanitize_conversation_deposit_value(value: Any) -> Any: if isinstance(value, str): return _LEGACY_BRAND_PATTERN.sub("cmzs", value) if isinstance(value, dict): return {key: _sanitize_conversation_deposit_value(item) for key, item in value.items()} if isinstance(value, list): return [_sanitize_conversation_deposit_value(item) for item in value] if isinstance(value, tuple): return tuple(_sanitize_conversation_deposit_value(item) for item in value) return value async def _sync_conversation_deposit_view( store: LlmWikiStore, client: WeKnoraClient, row: dict[str, Any], remote: dict[str, Any] | None, ) -> tuple[dict[str, Any], dict[str, Any] | None]: if not _is_conversation_deposit_mapping(row): return row, remote current_remote = remote if remote is not None and str(remote.get("description") or "") != CONVERSATION_DEPOSIT_KB_DESCRIPTION: try: updated_remote = await client.update_knowledge_base( str(row["weknora_id"]), {"description": CONVERSATION_DEPOSIT_KB_DESCRIPTION}, ) current_remote = { **remote, **updated_remote, "description": CONVERSATION_DEPOSIT_KB_DESCRIPTION, } except WeKnoraError: logger.warning("Failed to synchronize the cmzs conversation-deposit description", exc_info=True) current_row = row if str(row.get("description") or "") != CONVERSATION_DEPOSIT_KB_DESCRIPTION: updated_row = await store.update_mapping( str(row["id"]), description=CONVERSATION_DEPOSIT_KB_DESCRIPTION, ) current_row = updated_row or row return current_row, current_remote def _mapping_view(row: dict[str, Any], remote: dict[str, Any] | None, *, actor_user_id: str, is_admin: bool) -> dict[str, Any]: source = remote or {} is_conversation_deposit = _is_conversation_deposit_mapping(row) is_owner = row.get("owner_user_id") == actor_user_id description = ( CONVERSATION_DEPOSIT_KB_DESCRIPTION if is_conversation_deposit else source.get("description") if source.get("description") is not None else row.get("description") or "" ) return { "id": row["id"], "remote_status": "available" if remote is not None else "missing", "name": source.get("name") or row.get("name") or "", "description": description, "type": source.get("type") or row.get("kb_type") or "document", "publication_status": "published" if is_conversation_deposit else row.get("publication_status") or "private", "owner_user_id": row.get("owner_user_id") if is_admin else None, "is_owner": is_owner, "can_write": not is_conversation_deposit and (is_admin or is_owner), "is_conversation_deposit": is_conversation_deposit, "knowledge_count": int(source.get("knowledge_count") or 0), "chunk_count": int(source.get("chunk_count") or 0), "processing_count": int(source.get("processing_count") or 0), "embedding_model_id": source.get("embedding_model_id"), "vector_store_id": source.get("vector_store_id"), "vector_store_name": source.get("vector_store_name"), "vector_store_source": source.get("vector_store_source"), "vector_store_engine_type": source.get("vector_store_engine_type"), "vector_store_status": source.get("vector_store_status"), "chunking_config": source.get("chunking_config") if isinstance(source.get("chunking_config"), dict) else {}, "indexing_strategy": source.get("indexing_strategy") if isinstance(source.get("indexing_strategy"), dict) else {}, "capabilities": source.get("capabilities") if isinstance(source.get("capabilities"), dict) else {}, "extract_config": source.get("extract_config") if isinstance(source.get("extract_config"), dict) else {}, "wiki_config": source.get("wiki_config") if isinstance(source.get("wiki_config"), dict) else {}, "question_generation_config": (source.get("question_generation_config") if isinstance(source.get("question_generation_config"), dict) else {}), "summary_model_id": source.get("summary_model_id"), "storage_provider": (source.get("storage_provider_config", {}).get("provider") if isinstance(source.get("storage_provider_config"), dict) else None), "created_at": row.get("created_at"), "updated_at": row.get("updated_at"), "published_at": row.get("published_at"), } async def _authorized_mapping( request: Request, mapping_id: str, *, write: bool, ) -> tuple[dict[str, Any], str, bool]: user_id, is_admin = await _actor(request) row = await _store(request).get_authorized(mapping_id, user_id, write=write, is_admin=is_admin) if row is None: raise HTTPException(status_code=404, detail="Knowledge base not found") if write and _is_conversation_deposit_mapping(row): raise HTTPException(status_code=403, detail="Conversation deposit is managed by cmzs") return row, user_id, is_admin async def _verify_document(client: WeKnoraClient, row: dict[str, Any], document_id: str) -> dict[str, Any]: document = await client.get_document(document_id) if str(document.get("knowledge_base_id") or "") != str(row["weknora_id"]): raise HTTPException(status_code=404, detail="Document not found") return document def _document_view(document: dict[str, Any]) -> dict[str, Any]: """Expose useful document state without leaking WeKnora storage paths or hashes.""" fields = ( "id", "type", "title", "description", "source", "channel", "parse_status", "pending_subtasks_count", "summary_status", "enable_status", "embedding_model_id", "file_name", "file_type", "file_size", "storage_size", "created_at", "updated_at", "processed_at", "error_message", ) return {key: document.get(key) for key in fields} def _related_chunk_ids(value: Any) -> list[str]: if not isinstance(value, list): return [] result: list[str] = [] for item in value: candidate = item.get("id") if isinstance(item, dict) else item if candidate and str(candidate) not in result: result.append(str(candidate)) return result def _chunk_view(chunk: dict[str, Any]) -> dict[str, Any]: return { "id": str(chunk.get("id") or ""), "content": str(chunk.get("content") or ""), "chunk_index": int(chunk.get("chunk_index") or 0), "is_enabled": bool(chunk.get("is_enabled")), "status": chunk.get("status"), "start_at": int(chunk.get("start_at") or 0), "end_at": int(chunk.get("end_at") or 0), "chunk_type": str(chunk.get("chunk_type") or "text"), "parent_chunk_id": str(chunk.get("parent_chunk_id") or ""), "relation_chunk_ids": _related_chunk_ids(chunk.get("relation_chunks")), "indirect_relation_chunk_ids": _related_chunk_ids(chunk.get("indirect_relation_chunks")), "metadata": chunk.get("metadata") if isinstance(chunk.get("metadata"), dict) else {}, "created_at": chunk.get("created_at"), "updated_at": chunk.get("updated_at"), } def _string_tokens(value: Any) -> list[str]: tokens: list[str] = [] if isinstance(value, str): raw = value.strip() if raw: tokens.append(raw) for separator in ("#", ":", "/", "\\"): if separator in raw: tokens.extend(part.strip() for part in raw.split(separator) if part.strip()) elif isinstance(value, dict): for key in ("id", "chunk_id", "knowledge_id", "document_id", "source_id", "ref_id", "slug"): tokens.extend(_string_tokens(value.get(key))) return list(dict.fromkeys(tokens)) def _source_ref_tokens(value: Any) -> set[str]: refs: set[str] = set() if isinstance(value, list): for item in value: refs.update(_string_tokens(item)) else: refs.update(_string_tokens(value)) return refs def _candidate_chunk_ref_tokens(chunk: dict[str, Any], chunk_id: str) -> set[str]: refs = {chunk_id, str(chunk.get("id") or "")} metadata = chunk.get("metadata") if isinstance(chunk.get("metadata"), dict) else {} for source in (chunk, metadata): for key in ( "knowledge_id", "document_id", "source_id", "source_document_id", "knowledge_uuid", "file_id", ): refs.update(_string_tokens(source.get(key))) return {item for item in refs if item} def _candidate_wiki_slugs(chunk: dict[str, Any]) -> list[str]: metadata = chunk.get("metadata") if isinstance(chunk.get("metadata"), dict) else {} candidates: list[str] = [] for source in (chunk, metadata): for key in ("wiki_page_slug", "page_slug", "wiki_slug", "llmwiki_slug", "slug"): candidates.extend(_string_tokens(source.get(key))) nested_wiki = metadata.get("wiki") if isinstance(nested_wiki, dict): for key in ("slug", "page_slug"): candidates.extend(_string_tokens(nested_wiki.get(key))) return list(dict.fromkeys(candidates)) def _wiki_page_view(page: dict[str, Any]) -> dict[str, Any] | None: slug = str(page.get("slug") or "").strip() title = str(page.get("title") or slug or "").strip() content = str(page.get("content") or "").strip() summary = str(page.get("summary") or "").strip() if not slug and not title and not content and not summary: return None return { "id": str(page.get("id") or ""), "slug": slug, "title": title, "page_type": str(page.get("page_type") or "page"), "status": str(page.get("status") or ""), "summary": summary, "content": content, "source_refs": list(page.get("source_refs") or []) if isinstance(page.get("source_refs"), list) else [], } def _weknora_embed_target_path(weknora_id: str, tab: str) -> str: target_query = urlencode({"tab": tab}) if tab != "documents" else "" target = f"/platform/knowledge-bases/{quote(weknora_id, safe='')}" if target_query: target = f"{target}?{target_query}" return target def _set_weknora_embed_cookie(request: Request, response: Response, payload: dict[str, Any]) -> None: secure = is_secure_request(request) response.set_cookie( _WEKNORA_EMBED_COOKIE, _sign_embed_payload(payload), max_age=_WEKNORA_EMBED_TTL_SECONDS, httponly=True, secure=secure, samesite="none" if secure else "lax", path="/", ) response.headers["Cache-Control"] = "no-store" async def _find_wiki_page_for_chunk( client: WeKnoraClient, *, knowledge_base_id: str, chunk_id: str, chunks: list[dict[str, Any]], ) -> dict[str, Any] | None: focus = next((chunk for chunk in chunks if str(chunk.get("id") or "") == chunk_id), None) if focus is None: return None for slug in _candidate_wiki_slugs(focus): try: page = await client.get_wiki_page(knowledge_base_id, slug) except WeKnoraError as exc: if exc.status_code != 404: logger.debug("Failed to read WeKnora wiki page slug=%s", slug, exc_info=True) continue view = _wiki_page_view(page) if view and (view.get("content") or view.get("summary")): return view candidate_refs = _candidate_chunk_ref_tokens(focus, chunk_id) query = str(focus.get("knowledge_title") or focus.get("title") or focus.get("file_name") or "").strip() try: page_list = await client.list_wiki_pages(knowledge_base_id, page=1, page_size=100, query=query) except WeKnoraError as exc: if exc.status_code != 404: logger.debug("Failed to list WeKnora wiki pages for source preview", exc_info=True) return None for page in page_list.get("pages", []): if not isinstance(page, dict): continue source_refs = _source_ref_tokens(page.get("source_refs")) if not source_refs or not candidate_refs.intersection(source_refs): continue slug = str(page.get("slug") or "").strip() try: full_page = await client.get_wiki_page(knowledge_base_id, slug) if slug else page except WeKnoraError as exc: if exc.status_code != 404: logger.debug("Failed to hydrate WeKnora wiki page slug=%s", slug, exc_info=True) full_page = page view = _wiki_page_view(full_page) if view and (view.get("content") or view.get("summary")): return view return None async def _prepare_weknora_embed_session( request: Request, mapping_id: str, ) -> tuple[str, dict[str, Any]]: _runtime_for_iframe_proxy(request) row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False) writable = None if not _is_conversation_deposit_mapping(row): writable = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin) weknora_id = str(row["weknora_id"]) return weknora_id, { "mapping_id": mapping_id, "weknora_id": weknora_id, "user_id": user_id, "is_admin": is_admin, "can_write": writable is not None, "is_conversation_deposit": _is_conversation_deposit_mapping(row), "exp": int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS, } @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/weknora/session") async def create_weknora_detail_session( request: Request, mapping_id: str, tab: Literal["documents", "wiki", "graph"] = Query(default="wiki"), ) -> JSONResponse: """Create a signed iframe session using the normal DeerFlow API auth path.""" weknora_id, payload = await _prepare_weknora_embed_session(request, mapping_id) target = _weknora_embed_target_path(weknora_id, tab) response = JSONResponse( { "frame_path": target, "expires_in": _WEKNORA_EMBED_TTL_SECONDS, } ) _set_weknora_embed_cookie(request, response, payload) return response @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/weknora/frame") async def open_weknora_detail_frame( request: Request, mapping_id: str, tab: Literal["documents", "wiki", "graph"] = Query(default="wiki"), ) -> RedirectResponse: """Start a short-lived, DeerFlow-authorized WeKnora detail iframe session.""" weknora_id, payload = await _prepare_weknora_embed_session(request, mapping_id) target = _weknora_embed_target_path(weknora_id, tab) response = RedirectResponse(target, status_code=302) _set_weknora_embed_cookie(request, response, payload) return response @proxy_router.api_route("/platform", methods=["GET", "HEAD"]) @proxy_router.api_route("/platform/{proxied_path:path}", methods=["GET", "HEAD"]) async def proxy_weknora_platform_page(request: Request, proxied_path: str = "") -> Response: ctx = await _read_embed_context(request) requested = f"/platform/{proxied_path}".rstrip("/") allowed_path = f"/platform/knowledge-bases/{ctx.weknora_id}" if requested != allowed_path: target_query = urlencode({"tab": request.query_params.get("tab") or "wiki"}) return RedirectResponse(f"{allowed_path}?{target_query}", status_code=302) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.web_base_url, upstream_path=allowed_path, ctx=ctx, is_api=False, tab=request.query_params.get("tab") or "wiki", ) @proxy_router.api_route("/assets/{proxied_path:path}", methods=["GET", "HEAD"]) async def proxy_weknora_assets(request: Request, proxied_path: str) -> Response: ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.web_base_url, upstream_path=f"/assets/{proxied_path}", ctx=ctx, is_api=False, ) @proxy_router.api_route("/locales/{proxied_path:path}", methods=["GET", "HEAD"]) async def proxy_weknora_locales(request: Request, proxied_path: str) -> Response: ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.web_base_url, upstream_path=f"/locales/{proxied_path}", ctx=ctx, is_api=False, ) @proxy_router.api_route("/favicon.ico", methods=["GET", "HEAD"]) @proxy_router.api_route("/favicon.svg", methods=["GET", "HEAD"]) @proxy_router.api_route("/config.js", methods=["GET", "HEAD"]) @proxy_router.api_route("/logo.svg", methods=["GET", "HEAD"]) @proxy_router.api_route("/manifest.webmanifest", methods=["GET", "HEAD"]) async def proxy_weknora_root_asset(request: Request) -> Response: ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.web_base_url, upstream_path=request.url.path, ctx=ctx, is_api=False, ) @proxy_router.api_route("/tdesign-icons/{proxied_path:path}", methods=["GET", "HEAD"]) async def proxy_weknora_tdesign_icons(request: Request, proxied_path: str) -> Response: ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.web_base_url, upstream_path=f"/tdesign-icons/{proxied_path}", ctx=ctx, is_api=False, ) @proxy_router.api_route( "/api/v1/{proxied_path:path}", methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], ) async def proxy_weknora_api(request: Request, proxied_path: str) -> Response: if request.method.upper() == "OPTIONS": return Response(status_code=204) ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.api_base_url, upstream_path=f"/api/v1/{proxied_path}", ctx=ctx, is_api=True, ) @router.api_route( "/api/llmwiki/weknora-embed/api/v1/{proxied_path:path}", methods=["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"], ) async def proxy_weknora_embed_api(request: Request, proxied_path: str) -> Response: """Proxy iframe API calls under a collision-free cmzs namespace.""" if request.method.upper() == "OPTIONS": return Response(status_code=204) ctx = await _read_embed_context(request) runtime = _runtime_for_iframe_proxy(request) return await _proxy_weknora_request( request, target_base_url=runtime.api_base_url, upstream_path=f"/api/v1/{proxied_path}", ctx=ctx, is_api=True, ) @router.get("/api/llmwiki/runtime") async def get_llmwiki_runtime() -> dict[str, Any]: return _runtime_or_legacy() @router.get("/api/system-settings/llmwiki") async def get_llmwiki_settings(request: Request) -> dict[str, Any]: await _require_admin(request) app_config = request.app.state.config runtime = get_resolved_llmwiki_runtime(app_config) return { **_runtime_or_legacy(), "file_api_base_url": app_config.llmwiki.weknora.api_base_url, "file_web_base_url": app_config.llmwiki.weknora.web_base_url, "effective_provider": runtime.provider, } @router.put("/api/system-settings/llmwiki") async def update_llmwiki_settings(request: Request, body: LlmWikiSettingsUpdate) -> dict[str, Any]: await _require_admin(request) try: override = LlmWikiRuntimeOverride( enabled=body.override_enabled, api_base_url=body.api_base_url, web_base_url=body.web_base_url, ) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from None current = load_system_settings() save_system_settings(current.model_copy(update={"llmwiki": override})) return _runtime_or_legacy() @router.delete("/api/system-settings/llmwiki/override") async def reset_llmwiki_settings(request: Request) -> dict[str, Any]: await _require_admin(request) current = load_system_settings() save_system_settings(current.model_copy(update={"llmwiki": LlmWikiRuntimeOverride()})) return _runtime_or_legacy() @router.post("/api/system-settings/llmwiki/test") async def test_llmwiki_connection(request: Request, body: LlmWikiConnectionTest) -> dict[str, Any]: await _require_admin(request) runtime = get_resolved_llmwiki_runtime(request.app.state.config) if body.api_base_url is not None: try: candidate = normalize_service_url(body.api_base_url) except ValueError as exc: raise HTTPException(status_code=422, detail=str(exc)) from None runtime = replace(runtime, provider="weknora" if candidate else "legacy", api_base_url=candidate) if not runtime.weknora_enabled: raise HTTPException(status_code=422, detail="Enter a WeKnora API address first") try: health = await build_weknora_client(runtime).health() except RuntimeError as exc: raise HTTPException(status_code=503, detail=str(exc)) from None except WeKnoraError as exc: _raise_upstream(exc) return {"success": True, "provider": "weknora", "health": health} @router.get("/api/llmwiki/knowledge-bases") async def list_knowledge_bases( request: Request, scope: Literal["personal", "public", "all"] = Query(default="all"), ) -> dict[str, Any]: client = _client_or_503() user_id, is_admin = await _actor(request) try: remote_items = await client.list_knowledge_bases() remote = {str(item.get("id")): item for item in remote_items if item.get("id")} except WeKnoraError as exc: _raise_upstream(exc) deposit_remote = next( ( item for item in remote_items if str(item.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME ), None, ) if deposit_remote is not None: previous_mapping = await _store(request).get_by_weknora_id(str(deposit_remote.get("id") or "")) deposit_mapping = await ensure_conversation_deposit_mapping( _store(request), client, remote_knowledge_bases=remote_items, ) description_was_synchronized = ( previous_mapping is None or not _is_conversation_deposit_mapping(previous_mapping) or str(previous_mapping.get("description") or "") != CONVERSATION_DEPOSIT_KB_DESCRIPTION ) if deposit_mapping is not None and description_was_synchronized: remote_id = str(deposit_mapping["weknora_id"]) remote[remote_id] = { **remote.get(remote_id, deposit_remote), "description": CONVERSATION_DEPOSIT_KB_DESCRIPTION, } rows = await _store(request).list_visible(user_id, scope=scope, is_admin=False) knowledge_bases: list[dict[str, Any]] = [] for row in rows: remote_item = remote.get(str(row["weknora_id"])) row, remote_item = await _sync_conversation_deposit_view(_store(request), client, row, remote_item) # Owners need to see stale personal mappings so they can clean them up. # Public/selector views must never expose a resource that no longer exists # in WeKnora. if remote_item is None and scope != "personal": continue knowledge_bases.append(_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin)) return {"knowledge_bases": knowledge_bases} @router.get("/api/llmwiki/agents/{agent_id}/knowledge-bases") async def list_agent_knowledge_bases(request: Request, agent_id: str) -> dict[str, Any]: """Return the agent-bound bases still visible to the current DeerFlow user.""" user_id, is_admin = await _actor(request) agent_store = get_agent_store(request) agent = await (agent_store.get_any(agent_id) if is_admin else agent_store.get_visible(agent_id, user_id)) if agent is None: raise HTTPException(status_code=404, detail="Agent not found") try: config = load_agent_config(agent_id) except (FileNotFoundError, ValueError): config = None bound_ids = list(config.llmwiki_knowledge_base_ids or []) if config else [] rows: list[dict[str, Any]] = [] for mapping_id in bound_ids: row = await _store(request).get_authorized( mapping_id, user_id, write=False, is_admin=is_admin, ) if row is not None: rows.append(row) try: remote = {str(item.get("id")): item for item in await _client_or_503().list_knowledge_bases() if item.get("id")} except WeKnoraError as exc: _raise_upstream(exc) return {"knowledge_bases": [_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin) for row in rows if (remote_item := remote.get(str(row["weknora_id"]))) is not None]} @router.post("/api/llmwiki/knowledge-bases", status_code=201) async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) -> dict[str, Any]: client = _client_or_503() user_id, is_admin = await _actor(request) if body.name.strip() == CONVERSATION_DEPOSIT_KB_NAME: raise HTTPException(status_code=409, detail="对话沉淀是 cmzs 系统保留知识库") try: remote = await client.create_knowledge_base( name=body.name.strip(), description=body.description, kb_type=body.type, wiki_enabled=body.wiki_enabled, ) except WeKnoraError as exc: _raise_upstream(exc) try: row = await _store(request).create_mapping( weknora_id=str(remote["id"]), owner_user_id=user_id, name=str(remote.get("name") or body.name), description=str(remote.get("description") or body.description), kb_type=str(remote.get("type") or body.type), ) except Exception: logger.exception("Failed to persist LLMWiki mapping; compensating remote create") try: await client.delete_knowledge_base(str(remote["id"])) except Exception: logger.exception("Failed to compensate orphan WeKnora knowledge base") raise HTTPException(status_code=500, detail="Failed to register the knowledge base") from None return _mapping_view(row, remote, actor_user_id=user_id, is_admin=is_admin) @router.get("/api/llmwiki/knowledge-bases/{mapping_id}") async def get_knowledge_base(request: Request, mapping_id: str) -> dict[str, Any]: row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False) client = _client_or_503() try: remote = await client.get_knowledge_base(str(row["weknora_id"])) except WeKnoraError as exc: _raise_upstream(exc) row, remote = await _sync_conversation_deposit_view(_store(request), client, row, remote) return _mapping_view(row, remote, actor_user_id=user_id, is_admin=is_admin) @router.patch("/api/llmwiki/knowledge-bases/{mapping_id}") async def update_knowledge_base(request: Request, mapping_id: str, body: KnowledgeBaseUpdate) -> dict[str, Any]: row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=True) changes = body.model_dump(exclude_none=True) if not changes: return _mapping_view(row, None, actor_user_id=user_id, is_admin=is_admin) try: remote = await _client_or_503().update_knowledge_base(str(row["weknora_id"]), changes) except WeKnoraError as exc: _raise_upstream(exc) updated = await _store(request).update_mapping(mapping_id, name=body.name, description=body.description) return _mapping_view(updated or row, remote, actor_user_id=user_id, is_admin=is_admin) @router.delete("/api/llmwiki/knowledge-bases/{mapping_id}", status_code=204) async def delete_knowledge_base(request: Request, mapping_id: str) -> None: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) try: await _client_or_503().delete_knowledge_base(str(row["weknora_id"])) except WeKnoraError as exc: # A 404 means the remote data is already gone. The owner/admin must still # be able to remove DeerFlow's stale mapping; other upstream failures stay # visible and never cause local-only deletion. if exc.status_code != 404: _raise_upstream(exc) await _store(request).delete_mapping(mapping_id) @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/documents") async def list_documents( request: Request, mapping_id: str, page: int = Query(default=1, ge=1), page_size: int = Query(default=100, ge=1, le=200), ) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) try: result = await _client_or_503().list_documents(str(row["weknora_id"]), page=page, page_size=page_size) except WeKnoraError as exc: _raise_upstream(exc) return {**result, "items": [_document_view(item) for item in result["items"]]} @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/documents/{document_id}") async def get_document(request: Request, mapping_id: str, document_id: str) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) try: document = await _verify_document(_client_or_503(), row, document_id) except WeKnoraError as exc: _raise_upstream(exc) return _document_view(document) @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/documents/{document_id}/chunks") async def list_document_chunks( request: Request, mapping_id: str, document_id: str, page: int = Query(default=1, ge=1), page_size: int = Query(default=20, ge=1, le=100), ) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) client = _client_or_503() try: await _verify_document(client, row, document_id) result = await client.list_chunks(document_id, page=page, page_size=page_size) except WeKnoraError as exc: _raise_upstream(exc) return {**result, "items": [_chunk_view(item) for item in result["items"]]} @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/documents/{document_id}/preview") async def preview_document(request: Request, mapping_id: str, document_id: str) -> Response: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) client = _client_or_503() try: document = await _verify_document(client, row, document_id) content, media_type = await client.get_document_preview(document_id) except WeKnoraError as exc: _raise_upstream(exc) filename = str(document.get("file_name") or document.get("title") or "document") return Response( content=content, media_type=media_type, headers={ "Content-Disposition": f"inline; filename*=UTF-8''{quote(filename)}", "Cache-Control": "private, max-age=60", }, ) @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/graph") async def get_knowledge_base_graph( request: Request, mapping_id: str, limit: int = Query(default=200, ge=1, le=500), ) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) client = _client_or_503() try: remote = await client.get_knowledge_base(str(row["weknora_id"])) strategy = remote.get("indexing_strategy") if isinstance(remote.get("indexing_strategy"), dict) else {} if not strategy.get("wiki_enabled"): return { "nodes": [], "edges": [], "meta": {"mode": "disabled", "total": 0, "returned": 0, "truncated": False}, } graph = await client.get_wiki_graph(str(row["weknora_id"]), limit=limit) except WeKnoraError as exc: _raise_upstream(exc) nodes = [ { "id": str(node.get("slug") or ""), "title": str(node.get("title") or node.get("slug") or ""), "type": str(node.get("page_type") or "page"), "link_count": int(node.get("link_count") or 0), } for node in graph.get("nodes", []) if isinstance(node, dict) and node.get("slug") ] edges = [{"source": str(edge.get("source") or ""), "target": str(edge.get("target") or "")} for edge in graph.get("edges", []) if isinstance(edge, dict) and edge.get("source") and edge.get("target")] meta = graph.get("meta") if isinstance(graph.get("meta"), dict) else {} return {"nodes": nodes, "edges": edges, "meta": meta} @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/wiki/pages") async def list_wiki_pages( request: Request, mapping_id: str, page: int = Query(default=1, ge=1), page_size: int = Query(default=50, ge=1, le=100), query: str = Query(default="", max_length=255), ) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) try: return await _client_or_503().list_wiki_pages( str(row["weknora_id"]), page=page, page_size=page_size, query=query.strip(), ) except WeKnoraError as exc: _raise_upstream(exc) @router.get("/api/llmwiki/knowledge-bases/{mapping_id}/wiki/pages/{slug:path}") async def get_wiki_page(request: Request, mapping_id: str, slug: str) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=False) try: return await _client_or_503().get_wiki_page(str(row["weknora_id"]), slug) except WeKnoraError as exc: _raise_upstream(exc) @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/documents", status_code=201) async def upload_document(request: Request, mapping_id: str, file: UploadFile = File(...)) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) content = await file.read(_MAX_UPLOAD_BYTES + 1) if len(content) > _MAX_UPLOAD_BYTES: raise HTTPException(status_code=413, detail="File exceeds the 100 MB cmzs upload limit") if not file.filename: raise HTTPException(status_code=422, detail="File name is required") try: return await _client_or_503().upload_document( str(row["weknora_id"]), filename=file.filename, content=content, content_type=file.content_type or "application/octet-stream", ) except WeKnoraError as exc: _raise_upstream(exc) @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/documents/url", status_code=201) async def import_document_url(request: Request, mapping_id: str, body: KnowledgeUrlImport) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) url = body.url.strip() if not url.lower().startswith(("http://", "https://")): raise HTTPException(status_code=422, detail="URL must start with http:// or https://") try: return await _client_or_503().import_document_url(str(row["weknora_id"]), url=url) except WeKnoraError as exc: _raise_upstream(exc) @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/documents/manual", status_code=201) async def create_manual_document(request: Request, mapping_id: str, body: ManualKnowledgeCreate) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) try: return await _client_or_503().create_manual_document( str(row["weknora_id"]), title=body.title.strip(), content=body.content, ) except WeKnoraError as exc: _raise_upstream(exc) @router.delete("/api/llmwiki/knowledge-bases/{mapping_id}/documents/{document_id}", status_code=204) async def delete_document(request: Request, mapping_id: str, document_id: str) -> None: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) client = _client_or_503() try: await _verify_document(client, row, document_id) await client.delete_document(document_id) except WeKnoraError as exc: _raise_upstream(exc) @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/documents/{document_id}/reprocess") async def reprocess_document(request: Request, mapping_id: str, document_id: str) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, mapping_id, write=True) client = _client_or_503() try: await _verify_document(client, row, document_id) return await client.reprocess_document(document_id) except WeKnoraError as exc: _raise_upstream(exc) @router.post("/api/llmwiki/search") async def search_llmwiki(request: Request, body: SearchRequest) -> dict[str, Any]: user_id, is_admin = await _actor(request) store = _store(request) if body.knowledge_base_ids: rows: list[dict[str, Any]] = [] for mapping_id in dict.fromkeys(body.knowledge_base_ids): row = await store.get_authorized(mapping_id, user_id, write=False, is_admin=is_admin) if row is None: raise HTTPException(status_code=404, detail="Knowledge base not found") rows.append(row) else: rows = await store.list_visible(user_id, scope=body.scope, is_admin=False) try: results = await _client_or_503().search(body.query, [str(row["weknora_id"]) for row in rows]) except WeKnoraError as exc: _raise_upstream(exc) local_by_remote = {str(row["weknora_id"]): row for row in rows} visible: list[dict[str, Any]] = [] for result in results: mapping = local_by_remote.get(str(result.get("knowledge_base_id") or "")) if mapping is None: continue visible_result = ( _sanitize_conversation_deposit_value(result) if _is_conversation_deposit_mapping(mapping) else result ) params = urlencode({"knowledge_base_id": mapping["id"], "chunk_id": visible_result.get("chunk_id") or ""}) visible.append( { **visible_result, "knowledge_base_id": mapping["id"], "knowledge_base_name": mapping.get("name") or "", "source": mapping.get("name") or "LLMWiki", "url": f"/weknora-source-preview?{params}", } ) return {"results": visible, "knowledge_base_count": len(rows)} @router.post("/api/llmwiki/conversation-deposits/turn", status_code=202) async def create_conversation_deposit( request: Request, background_tasks: BackgroundTasks, body: ConversationDepositCreate, ) -> dict[str, Any]: runtime = get_resolved_llmwiki_runtime(getattr(request.app.state, "config", None)) if not runtime.weknora_enabled: return {"queued": False, "provider": runtime.provider} user_id, _ = await _actor(request) if not await get_thread_store(request).check_access(body.thread_id, user_id, require_existing=True): raise HTTPException(status_code=404, detail="Thread not found") app = request.app turn = ConversationTurnDeposit( thread_id=body.thread_id, question=body.question, answer=body.answer, human_message_id=body.human_message_id, assistant_message_id=body.assistant_message_id, assistant_id=body.assistant_id, knowledge_base_ids=list(dict.fromkeys(item.strip() for item in body.knowledge_base_ids if item.strip())), created_by_user_id=user_id, ) background_tasks.add_task(deposit_conversation_turn, app, turn) return {"queued": True, "provider": "weknora"} @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/publish") async def request_publish(request: Request, mapping_id: str) -> dict[str, Any]: user_id, is_admin = await _actor(request) existing = await _store(request).get_authorized( mapping_id, user_id, write=True, is_admin=is_admin, ) if existing is None: raise HTTPException(status_code=404, detail="Knowledge base not found") if _is_conversation_deposit_mapping(existing) and existing.get("publication_status") == "published": return {"id": existing["id"], "publication_status": "published"} try: await _client_or_503().get_knowledge_base(str(existing["weknora_id"])) except WeKnoraError as exc: _raise_upstream(exc) row = await _store(request).request_publish(mapping_id, user_id, is_admin=is_admin) if row is None: raise HTTPException(status_code=404, detail="Knowledge base not found") return {"id": row["id"], "publication_status": row["publication_status"]} @router.post("/api/llmwiki/knowledge-bases/{mapping_id}/unpublish") async def unpublish(request: Request, mapping_id: str) -> dict[str, Any]: user_id, is_admin = await _actor(request) existing = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin) if existing is None: raise HTTPException(status_code=404, detail="Knowledge base not found") if _is_conversation_deposit_mapping(existing): raise HTTPException(status_code=403, detail="Conversation deposit is always public") row = await _store(request).unpublish(mapping_id, user_id, is_admin=is_admin) if row is None: raise HTTPException(status_code=404, detail="Knowledge base not found") return {"id": row["id"], "publication_status": row["publication_status"]} @router.get("/api/llmwiki/sources/context") async def get_source_context( request: Request, knowledge_base_id: str = Query(..., min_length=1, max_length=128), chunk_id: str = Query(..., min_length=1, max_length=128), ) -> dict[str, Any]: row, _, _ = await _authorized_mapping(request, knowledge_base_id, write=False) client = _client_or_503() try: chunks = await client.get_chunk_context( chunk_id, expected_knowledge_base_id=str(row["weknora_id"]), radius=2, ) except WeKnoraError as exc: _raise_upstream(exc) wiki_page = await _find_wiki_page_for_chunk( client, knowledge_base_id=str(row["weknora_id"]), chunk_id=chunk_id, chunks=chunks, ) sanitize_deposit = _is_conversation_deposit_mapping(row) if sanitize_deposit and wiki_page is not None: wiki_page = _sanitize_conversation_deposit_value(wiki_page) normalized_chunks = [ { "id": str(chunk.get("id") or ""), "chunk_index": int(chunk.get("chunk_index") or 0), "content": str( _sanitize_conversation_deposit_value(chunk.get("content") or "") if sanitize_deposit else chunk.get("content") or "" ), "is_focus": str(chunk.get("id") or "") == chunk_id, } for chunk in chunks if chunk.get("id") ] return { "success": True, "data": { "knowledge_base_id": row["id"], "knowledge_base_name": row.get("name") or "", "focus_chunk_id": chunk_id, "display_mode": "wiki" if wiki_page is not None else "chunks", "wiki_page": wiki_page, "chunks": normalized_chunks, }, }