fix: harden WeKnora knowledge permissions and details
This commit is contained in:
1 parent
e0c09f6704
commit
b4d076a2d4
20 files changed
+1713
-298
No files matched your search
@@ -1,26 +1,39 @@
|
||||
"""DeerFlow-native LLMWiki BFF backed by an independently deployed WeKnora."""
|
||||
"""cmzs LLMWiki BFF backed by an independently deployed WeKnora."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import re
|
||||
import time
|
||||
from dataclasses import dataclass, replace
|
||||
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 RedirectResponse, Response
|
||||
from fastapi.responses import JSONResponse, RedirectResponse, Response
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_agent_store, get_optional_user_from_request
|
||||
from app.gateway.llmwiki_deposit import ConversationTurnDeposit, deposit_conversation_turn
|
||||
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
|
||||
@@ -31,11 +44,9 @@ 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_EMBED_COOKIE = "deerflow_weknora_embed"
|
||||
_WEKNORA_EMBED_TTL_SECONDS = 15 * 60
|
||||
_WEKNORA_PROXY_SECRET = os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET") or secrets.token_urlsafe(48)
|
||||
_WEKNORA_ADMIN_SESSION_CACHE: dict[str, dict[str, Any]] = {}
|
||||
_HOP_BY_HOP_RESPONSE_HEADERS = {
|
||||
"connection",
|
||||
@@ -53,16 +64,6 @@ _HOP_BY_HOP_RESPONSE_HEADERS = {
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeKnoraEmbedContext:
|
||||
mapping_id: str
|
||||
weknora_id: str
|
||||
user_id: str
|
||||
is_admin: bool
|
||||
can_write: bool
|
||||
exp: int
|
||||
|
||||
|
||||
class LlmWikiSettingsUpdate(BaseModel):
|
||||
override_enabled: bool = True
|
||||
api_base_url: str = ""
|
||||
@@ -156,46 +157,6 @@ def _raise_upstream(exc: WeKnoraError) -> None:
|
||||
raise HTTPException(status_code=status, detail=str(exc)) from None
|
||||
|
||||
|
||||
def _b64url_encode(data: bytes) -> str:
|
||||
return base64.urlsafe_b64encode(data).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def _b64url_decode(data: str) -> bytes:
|
||||
padded = data + ("=" * (-len(data) % 4))
|
||||
return base64.urlsafe_b64decode(padded.encode("ascii"))
|
||||
|
||||
|
||||
def _sign_embed_payload(payload: dict[str, Any]) -> str:
|
||||
body = _b64url_encode(json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8"))
|
||||
sig = hmac.new(_WEKNORA_PROXY_SECRET.encode("utf-8"), body.encode("ascii"), hashlib.sha256).digest()
|
||||
return f"{body}.{_b64url_encode(sig)}"
|
||||
|
||||
|
||||
def _verify_embed_cookie(raw: str | None) -> WeKnoraEmbedContext:
|
||||
if not raw or "." not in raw:
|
||||
raise HTTPException(status_code=401, detail="WeKnora iframe session is missing")
|
||||
body, sig = raw.rsplit(".", 1)
|
||||
expected = _b64url_encode(
|
||||
hmac.new(_WEKNORA_PROXY_SECRET.encode("utf-8"), body.encode("ascii"), hashlib.sha256).digest()
|
||||
)
|
||||
if not hmac.compare_digest(sig, expected):
|
||||
raise HTTPException(status_code=401, detail="WeKnora iframe session is invalid")
|
||||
try:
|
||||
payload = json.loads(_b64url_decode(body))
|
||||
except (ValueError, TypeError, json.JSONDecodeError):
|
||||
raise HTTPException(status_code=401, detail="WeKnora iframe session is invalid") from None
|
||||
if int(payload.get("exp") or 0) < int(time.time()):
|
||||
raise HTTPException(status_code=401, detail="WeKnora iframe session has expired")
|
||||
return WeKnoraEmbedContext(
|
||||
mapping_id=str(payload.get("mapping_id") or ""),
|
||||
weknora_id=str(payload.get("weknora_id") or ""),
|
||||
user_id=str(payload.get("user_id") or ""),
|
||||
is_admin=bool(payload.get("is_admin")),
|
||||
can_write=bool(payload.get("can_write")),
|
||||
exp=int(payload.get("exp") or 0),
|
||||
)
|
||||
|
||||
|
||||
def _json_response(payload: Any, *, status_code: int = 200) -> Response:
|
||||
return Response(
|
||||
content=json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8"),
|
||||
@@ -302,8 +263,14 @@ async def _read_embed_context(request: Request) -> WeKnoraEmbedContext:
|
||||
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 = 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)
|
||||
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:
|
||||
@@ -418,7 +385,7 @@ async def _authorize_weknora_api_proxy(
|
||||
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 DeerFlow user cannot write this knowledge base")
|
||||
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":
|
||||
@@ -553,6 +520,7 @@ def _weknora_embed_injection(
|
||||
"tenantId": tenant_id,
|
||||
"user": user,
|
||||
"tenant": tenant,
|
||||
"apiPrefix": _WEKNORA_EMBED_API_PREFIX,
|
||||
}
|
||||
data = _escape_script_json(payload)
|
||||
return f"""
|
||||
@@ -567,19 +535,13 @@ html.deerflow-weknora-embed #app {{
|
||||
background: var(--td-bg-color-page) !important;
|
||||
color: var(--td-text-color-primary) !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed:not(.deerflow-weknora-ready) body {{
|
||||
opacity: 0 !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed body {{
|
||||
transition: opacity 120ms ease;
|
||||
opacity: 1 !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed .main {{
|
||||
min-width: 0 !important;
|
||||
background: var(--td-bg-color-page) !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed .main > :first-child:not(.platform-route-outlet) {{
|
||||
display: none !important;
|
||||
}}
|
||||
html.deerflow-weknora-embed .platform-route-outlet,
|
||||
html.deerflow-weknora-embed .knowledge-layout {{
|
||||
width: 100% !important;
|
||||
@@ -657,6 +619,28 @@ html.deerflow-weknora-embed [class*=" guide__"] {{
|
||||
safeSessionSet(key, "1");
|
||||
}});
|
||||
|
||||
function rewriteApiUrl(value) {{
|
||||
try {{
|
||||
const next = new URL(value, location.origin);
|
||||
if (next.origin !== location.origin || !next.pathname.startsWith("/api/v1/")) return value;
|
||||
next.pathname = cfg.apiPrefix + next.pathname;
|
||||
return next.href;
|
||||
}} catch (_) {{
|
||||
return value;
|
||||
}}
|
||||
}}
|
||||
const nativeFetch = window.fetch.bind(window);
|
||||
window.fetch = (input, init) => {{
|
||||
if (input instanceof Request) {{
|
||||
return nativeFetch(new Request(rewriteApiUrl(input.url), input), init);
|
||||
}}
|
||||
return nativeFetch(rewriteApiUrl(input), init);
|
||||
}};
|
||||
const nativeXhrOpen = XMLHttpRequest.prototype.open;
|
||||
XMLHttpRequest.prototype.open = function(method, url, ...rest) {{
|
||||
return nativeXhrOpen.call(this, method, rewriteApiUrl(url), ...rest);
|
||||
}};
|
||||
|
||||
function tone(color, alpha) {{
|
||||
return `color-mix(in srgb, ${{color}} ${{Math.round(alpha * 100)}}%, transparent)`;
|
||||
}}
|
||||
@@ -830,6 +814,17 @@ async def _proxy_weknora_request(
|
||||
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)
|
||||
@@ -845,18 +840,84 @@ async def _proxy_weknora_request(
|
||||
)
|
||||
|
||||
|
||||
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": source.get("description") if source.get("description") is not None else row.get("description") or "",
|
||||
"description": description,
|
||||
"type": source.get("type") or row.get("kb_type") or "document",
|
||||
"publication_status": row.get("publication_status") or "private",
|
||||
"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_admin or row.get("owner_user_id") == actor_user_id,
|
||||
"can_write": is_admin or row.get("owner_user_id") == actor_user_id,
|
||||
"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),
|
||||
@@ -890,6 +951,8 @@ async def _authorized_mapping(
|
||||
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
|
||||
|
||||
|
||||
@@ -957,6 +1020,192 @@ def _chunk_view(chunk: dict[str, Any]) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
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,
|
||||
@@ -965,38 +1214,15 @@ async def open_weknora_detail_frame(
|
||||
) -> RedirectResponse:
|
||||
"""Start a short-lived, DeerFlow-authorized WeKnora detail iframe session."""
|
||||
|
||||
_runtime_for_iframe_proxy(request)
|
||||
row, user_id, is_admin = await _authorized_mapping(request, mapping_id, write=False)
|
||||
writable = await _store(request).get_authorized(mapping_id, user_id, write=True, is_admin=is_admin)
|
||||
weknora_id = str(row["weknora_id"])
|
||||
payload = {
|
||||
"mapping_id": mapping_id,
|
||||
"weknora_id": weknora_id,
|
||||
"user_id": user_id,
|
||||
"is_admin": is_admin,
|
||||
"can_write": writable is not None,
|
||||
"exp": int(time.time()) + _WEKNORA_EMBED_TTL_SECONDS,
|
||||
}
|
||||
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}"
|
||||
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)
|
||||
response.set_cookie(
|
||||
_WEKNORA_EMBED_COOKIE,
|
||||
_sign_embed_payload(payload),
|
||||
max_age=_WEKNORA_EMBED_TTL_SECONDS,
|
||||
httponly=True,
|
||||
secure=request.url.scheme == "https",
|
||||
samesite="none" if request.url.scheme == "https" else "lax",
|
||||
path="/",
|
||||
)
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
_set_weknora_embed_cookie(request, response, payload)
|
||||
return response
|
||||
|
||||
|
||||
@router.api_route("/platform", methods=["GET", "HEAD"])
|
||||
@router.api_route("/platform/{proxied_path:path}", methods=["GET", "HEAD"])
|
||||
@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("/")
|
||||
@@ -1015,7 +1241,7 @@ async def proxy_weknora_platform_page(request: Request, proxied_path: str = "")
|
||||
)
|
||||
|
||||
|
||||
@router.api_route("/assets/{proxied_path:path}", methods=["GET", "HEAD"])
|
||||
@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)
|
||||
@@ -1028,7 +1254,7 @@ async def proxy_weknora_assets(request: Request, proxied_path: str) -> Response:
|
||||
)
|
||||
|
||||
|
||||
@router.api_route("/locales/{proxied_path:path}", methods=["GET", "HEAD"])
|
||||
@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)
|
||||
@@ -1041,10 +1267,11 @@ async def proxy_weknora_locales(request: Request, proxied_path: str) -> Response
|
||||
)
|
||||
|
||||
|
||||
@router.api_route("/favicon.ico", methods=["GET", "HEAD"])
|
||||
@router.api_route("/favicon.svg", methods=["GET", "HEAD"])
|
||||
@router.api_route("/logo.svg", methods=["GET", "HEAD"])
|
||||
@router.api_route("/manifest.webmanifest", methods=["GET", "HEAD"])
|
||||
@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)
|
||||
@@ -1057,7 +1284,20 @@ async def proxy_weknora_root_asset(request: Request) -> Response:
|
||||
)
|
||||
|
||||
|
||||
@router.api_route(
|
||||
@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"],
|
||||
)
|
||||
@@ -1075,6 +1315,26 @@ async def proxy_weknora_api(request: Request, proxied_path: str) -> Response:
|
||||
)
|
||||
|
||||
|
||||
@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()
|
||||
@@ -1145,14 +1405,42 @@ async def list_knowledge_bases(
|
||||
) -> dict[str, Any]:
|
||||
client = _client_or_503()
|
||||
user_id, is_admin = await _actor(request)
|
||||
rows = await _store(request).list_visible(user_id, scope=scope, is_admin=is_admin and scope == "personal")
|
||||
try:
|
||||
remote = {str(item.get("id")): item for item in await client.list_knowledge_bases() if item.get("id")}
|
||||
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.
|
||||
@@ -1197,6 +1485,8 @@ async def list_agent_knowledge_bases(request: Request, agent_id: str) -> dict[st
|
||||
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(),
|
||||
@@ -1227,10 +1517,12 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
@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_or_503().get_knowledge_base(str(row["weknora_id"]))
|
||||
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)
|
||||
|
||||
|
||||
@@ -1394,7 +1686,7 @@ async def upload_document(request: Request, mapping_id: str, file: UploadFile =
|
||||
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 DeerFlow upload limit")
|
||||
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:
|
||||
@@ -1467,7 +1759,7 @@ async def search_llmwiki(request: Request, body: SearchRequest) -> dict[str, Any
|
||||
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=is_admin and body.scope == "personal")
|
||||
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:
|
||||
@@ -1478,10 +1770,15 @@ async def search_llmwiki(request: Request, body: SearchRequest) -> dict[str, Any
|
||||
mapping = local_by_remote.get(str(result.get("knowledge_base_id") or ""))
|
||||
if mapping is None:
|
||||
continue
|
||||
params = urlencode({"knowledge_base_id": mapping["id"], "chunk_id": result.get("chunk_id") or ""})
|
||||
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(
|
||||
{
|
||||
**result,
|
||||
**visible_result,
|
||||
"knowledge_base_id": mapping["id"],
|
||||
"knowledge_base_name": mapping.get("name") or "",
|
||||
"source": mapping.get("name") or "LLMWiki",
|
||||
@@ -1501,6 +1798,8 @@ async def create_conversation_deposit(
|
||||
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,
|
||||
@@ -1527,6 +1826,8 @@ async def request_publish(request: Request, mapping_id: str) -> dict[str, Any]:
|
||||
)
|
||||
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:
|
||||
@@ -1540,6 +1841,11 @@ async def request_publish(request: Request, mapping_id: str) -> dict[str, Any]:
|
||||
@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")
|
||||
@@ -1553,19 +1859,33 @@ async def get_source_context(
|
||||
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_or_503().get_chunk_context(
|
||||
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(chunk.get("content") or ""),
|
||||
"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
|
||||
@@ -1577,6 +1897,8 @@ async def get_source_context(
|
||||
"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,
|
||||
},
|
||||
}
|
||||
Reference in new issue
Block a user