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
@@ -1618,10 +1618,10 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
# ??? app/gateway/routers/{multi_agent,intent,recommend}.py?
|
||||
# ??? frontend-web/docs/multi-agent-backend-dev.md
|
||||
app.include_router(multi_agent.router) # Step 2?init(SSE) + run/stream(leader|special)
|
||||
app.include_router(intent.router) # Step 1?/api/intent/init + /stream
|
||||
app.include_router(recommend.router) # Step 1?2?/api/recommend/stream?? init?
|
||||
# Step 3 ????/?????? artifacts.router?/api/threads/{id}/artifacts?
|
||||
|
||||
app.include_router(intent.router) # Step 1?/api/intent/init + /stream
|
||||
app.include_router(recommend.router) # Step 1?2?/api/recommend/stream?? init?
|
||||
# Step 3 ????/?????? artifacts.router?/api/threads/{id}/artifacts?
|
||||
|
||||
# Health probes are always unauthenticated (see
|
||||
# auth_middleware.PUBLIC_HEALTH_PATHS). Support the bare Gateway path,
|
||||
# API base URL, and LangGraph proxy base URL for deployment monitors.
|
||||
@@ -1633,10 +1633,15 @@ This gateway provides custom endpoints for models, MCP configuration, skills, an
|
||||
|
||||
Returns:
|
||||
Service health status information.
|
||||
"""
|
||||
return {"status": "healthy", "service": "deer-flow-gateway"}
|
||||
|
||||
return app
|
||||
"""
|
||||
return {"status": "healthy", "service": "deer-flow-gateway"}
|
||||
|
||||
# WeKnora's embedded frontend uses root-relative routes such as /platform,
|
||||
# /assets, /locales and /api/v1/*. Register that proxy last so it stays a
|
||||
# true fallback and cannot intercept DeerFlow APIs.
|
||||
app.include_router(llmwiki.proxy_router)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
# Create app instance for uvicorn
|
||||
|
||||
@@ -19,10 +19,15 @@ from starlette.types import ASGIApp
|
||||
|
||||
from app.gateway.auth.disabled_mode import get_assumed_user_for_disabled_auth, is_auth_disabled
|
||||
from app.gateway.auth.errors import AuthErrorCode, AuthErrorResponse
|
||||
from app.gateway.authz import _ALL_PERMISSIONS, AuthContext
|
||||
from app.gateway.internal_auth import INTERNAL_AUTH_HEADER_NAME, get_internal_user, is_valid_internal_auth_token
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
from app.gateway.authz import _ALL_PERMISSIONS, AuthContext
|
||||
from app.gateway.internal_auth import INTERNAL_AUTH_HEADER_NAME, get_internal_user, is_valid_internal_auth_token
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
from app.gateway.weknora_embed import (
|
||||
WEKNORA_EMBED_COOKIE,
|
||||
has_valid_weknora_embed_cookie,
|
||||
is_weknora_embed_proxy_path,
|
||||
)
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
|
||||
# Health probes are deliberately public, including the LangGraph-compatible
|
||||
# gateway alias used by some deployments. Keep these as exact paths: health
|
||||
@@ -65,15 +70,31 @@ _PUBLIC_EXACT_PATHS: frozenset[str] = frozenset(
|
||||
"/api/v1/auth/initialize",
|
||||
# 并行多智能体面板专用的「绕过常规登录」取 token 端点(内网部署限制了标准登录时用)。
|
||||
"/api/parallel-agents/auth/token",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _is_public(path: str) -> bool:
|
||||
stripped = path.rstrip("/")
|
||||
if stripped in _PUBLIC_EXACT_PATHS:
|
||||
return True
|
||||
return any(path.startswith(prefix) for prefix in _PUBLIC_PATH_PREFIXES)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _is_public(path: str) -> bool:
|
||||
stripped = path.rstrip("/")
|
||||
if stripped in _PUBLIC_EXACT_PATHS:
|
||||
return True
|
||||
return any(path.startswith(prefix) for prefix in _PUBLIC_PATH_PREFIXES)
|
||||
|
||||
|
||||
def _is_weknora_embed_proxy_request(request: Request) -> bool:
|
||||
"""Let the WeKnora embed proxy verify its own signed iframe cookie.
|
||||
|
||||
The embedded WeKnora frontend loads root-relative routes (``/platform``,
|
||||
``/assets``, ``/locales`` and ``/api/v1/*``). Those requests do not carry
|
||||
DeerFlow's Authorization header, but the iframe bootstrap has already set a
|
||||
short-lived signed cookie that the proxy router validates against the
|
||||
requested knowledge base.
|
||||
"""
|
||||
|
||||
path = app_relative_path(request)
|
||||
if not is_weknora_embed_proxy_path(path):
|
||||
return False
|
||||
return has_valid_weknora_embed_cookie(request.cookies.get(WEKNORA_EMBED_COOKIE))
|
||||
|
||||
|
||||
# 「发起问答」的 run 创建接口(会真正调用大模型):
|
||||
@@ -114,12 +135,14 @@ class AuthMiddleware(BaseHTTPMiddleware):
|
||||
super().__init__(app)
|
||||
|
||||
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
||||
# Match the application-relative path so a reverse-proxy prefix
|
||||
# (e.g. /xxx/api/public/...) does not defeat the public whitelist.
|
||||
if _is_public(app_relative_path(request)):
|
||||
return await call_next(request)
|
||||
|
||||
if is_auth_disabled():
|
||||
# Match the application-relative path so a reverse-proxy prefix
|
||||
# (e.g. /xxx/api/public/...) does not defeat the public whitelist.
|
||||
if _is_public(app_relative_path(request)):
|
||||
return await call_next(request)
|
||||
if _is_weknora_embed_proxy_request(request):
|
||||
return await call_next(request)
|
||||
|
||||
if is_auth_disabled():
|
||||
user = await get_assumed_user_for_disabled_auth()
|
||||
request.state.user = user
|
||||
request.state.auth = AuthContext(user=user, permissions=_ALL_PERMISSIONS)
|
||||
|
||||
@@ -7,16 +7,21 @@ State-changing operations require CSRF protection.
|
||||
import secrets
|
||||
from collections.abc import Callable
|
||||
|
||||
from app.gateway.auth.disabled_mode import is_auth_disabled
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
from fastapi import Request, Response
|
||||
from app.gateway.auth.disabled_mode import is_auth_disabled
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
from app.gateway.weknora_embed import (
|
||||
WEKNORA_EMBED_COOKIE,
|
||||
has_valid_weknora_embed_cookie,
|
||||
is_weknora_embed_proxy_path,
|
||||
)
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.types import ASGIApp
|
||||
|
||||
CSRF_COOKIE_NAME = "csrf_token"
|
||||
CSRF_HEADER_NAME = "X-CSRF-Token"
|
||||
CSRF_TOKEN_LENGTH = 64 # bytes
|
||||
CSRF_COOKIE_NAME = "csrf_token"
|
||||
CSRF_HEADER_NAME = "X-CSRF-Token"
|
||||
CSRF_TOKEN_LENGTH = 64 # bytes
|
||||
|
||||
|
||||
def is_secure_request(request: Request) -> bool:
|
||||
@@ -29,7 +34,7 @@ def generate_csrf_token() -> str:
|
||||
return secrets.token_urlsafe(CSRF_TOKEN_LENGTH)
|
||||
|
||||
|
||||
def should_check_csrf(request: Request) -> bool:
|
||||
def should_check_csrf(request: Request) -> bool:
|
||||
"""Determine if a request needs CSRF validation.
|
||||
|
||||
CSRF is checked for state-changing methods (POST, PUT, DELETE, PATCH).
|
||||
@@ -54,9 +59,18 @@ def should_check_csrf(request: Request) -> bool:
|
||||
return False
|
||||
# 开放问答接口同样是匿名、无会话的(见 auth_middleware._PUBLIC_PATH_PREFIXES),
|
||||
# 双提交 Cookie 既无 token 可比、也无会话可被伪造,豁免之。
|
||||
if path.startswith("/api/open/"):
|
||||
return False
|
||||
return True
|
||||
if path.startswith("/api/open/"):
|
||||
return False
|
||||
if is_weknora_embed_proxy_request(request):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_weknora_embed_proxy_request(request: Request) -> bool:
|
||||
path = app_relative_path(request)
|
||||
if not is_weknora_embed_proxy_path(path):
|
||||
return False
|
||||
return has_valid_weknora_embed_cookie(request.cookies.get(WEKNORA_EMBED_COOKIE))
|
||||
|
||||
|
||||
_AUTH_EXEMPT_PATHS: frozenset[str] = frozenset(
|
||||
|
||||
@@ -619,27 +619,34 @@ _cached_local_provider: LocalAuthProvider | None = None
|
||||
_cached_repo: SQLiteUserRepository | None = None
|
||||
|
||||
|
||||
def get_request_access_token(request: Request) -> str | None:
|
||||
"""Extract access token from cookie or Authorization header.
|
||||
|
||||
Priority:
|
||||
1. ``access_token`` cookie (browser session mode)
|
||||
2. ``Authorization: Bearer <token>`` header (API client mode)
|
||||
"""
|
||||
cookie_token = request.cookies.get("access_token")
|
||||
if cookie_token:
|
||||
return cookie_token
|
||||
|
||||
authorization = request.headers.get("authorization", "").strip()
|
||||
if not authorization:
|
||||
return None
|
||||
|
||||
scheme, _, token = authorization.partition(" ")
|
||||
if scheme.lower() != "bearer":
|
||||
return None
|
||||
|
||||
token = token.strip()
|
||||
return token or None
|
||||
def get_request_access_token(request: Request) -> str | None:
|
||||
"""Extract access token from cookie or Authorization header.
|
||||
|
||||
Priority:
|
||||
1. ``access_token`` cookie (browser session mode)
|
||||
2. ``Authorization: Bearer <token>`` header (API client mode)
|
||||
3. ``access_token`` query parameter for the WeKnora iframe bootstrap only
|
||||
"""
|
||||
cookie_token = request.cookies.get("access_token")
|
||||
if cookie_token:
|
||||
return cookie_token
|
||||
|
||||
authorization = request.headers.get("authorization", "").strip()
|
||||
if authorization:
|
||||
scheme, _, token = authorization.partition(" ")
|
||||
if scheme.lower() == "bearer":
|
||||
token = token.strip()
|
||||
if token:
|
||||
return token
|
||||
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
|
||||
path = app_relative_path(request).rstrip("/")
|
||||
if path.startswith("/api/llmwiki/knowledge-bases/") and path.endswith("/weknora/frame"):
|
||||
query_token = (request.query_params.get("access_token") or "").strip()
|
||||
if query_token:
|
||||
return query_token
|
||||
return None
|
||||
|
||||
|
||||
def get_local_provider() -> LocalAuthProvider:
|
||||
|
||||
@@ -14,6 +14,8 @@ from deerflow.persistence.llmwiki import LlmWikiStore
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CONVERSATION_DEPOSIT_KB_NAME = "对话沉淀"
|
||||
CONVERSATION_DEPOSIT_OWNER_USER_ID = "system"
|
||||
CONVERSATION_DEPOSIT_KB_DESCRIPTION = "cmzs 全局问答沉淀知识库。所有用户可见,不默认参与问答检索。"
|
||||
_DEPOSIT_KB_LOCK = asyncio.Lock()
|
||||
|
||||
|
||||
@@ -59,7 +61,7 @@ def _title_for_turn(turn: ConversationTurnDeposit) -> str:
|
||||
def _format_markdown(turn: ConversationTurnDeposit, title: str) -> str:
|
||||
kb_line = "、".join(turn.knowledge_base_ids) if turn.knowledge_base_ids else "未显式选择知识库"
|
||||
return f"""---
|
||||
source: DeerFlow
|
||||
source: cmzs
|
||||
type: conversation-deposit
|
||||
thread_id: {turn.thread_id}
|
||||
human_message_id: {turn.human_message_id or ""}
|
||||
@@ -71,7 +73,7 @@ created_by_user_id: {turn.created_by_user_id or ""}
|
||||
|
||||
# {title}
|
||||
|
||||
> 本页由 DeerFlow 在一轮问答结束后自动沉淀,用于后续 LLMWiki 检索、Wiki 索引与图谱构建。
|
||||
> 本页由 cmzs 在一轮问答结束后自动沉淀,用于后续 LLMWiki 检索、Wiki 索引与图谱构建。
|
||||
|
||||
## 摘要
|
||||
|
||||
@@ -95,10 +97,114 @@ created_by_user_id: {turn.created_by_user_id or ""}
|
||||
|
||||
## LLMWiki 标签
|
||||
|
||||
#对话沉淀 #DeerFlow #问答知识
|
||||
#对话沉淀 #cmzs #问答知识
|
||||
"""
|
||||
|
||||
|
||||
async def _sync_conversation_deposit_description(
|
||||
store: LlmWikiStore,
|
||||
client: WeKnoraClient,
|
||||
row: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
if str(row.get("description") or "") == CONVERSATION_DEPOSIT_KB_DESCRIPTION:
|
||||
return row
|
||||
try:
|
||||
await client.update_knowledge_base(
|
||||
str(row["weknora_id"]),
|
||||
{"description": CONVERSATION_DEPOSIT_KB_DESCRIPTION},
|
||||
)
|
||||
except WeKnoraError:
|
||||
logger.warning("Failed to update the conversation deposit knowledge-base description", exc_info=True)
|
||||
return row
|
||||
updated = await store.update_mapping(
|
||||
str(row["id"]),
|
||||
description=CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
)
|
||||
return updated or row
|
||||
|
||||
|
||||
async def ensure_conversation_deposit_mapping(
|
||||
store: LlmWikiStore,
|
||||
client: WeKnoraClient,
|
||||
*,
|
||||
remote_knowledge_bases: list[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
existing = await store.get_conversation_deposit_mapping()
|
||||
if existing is not None:
|
||||
return await _sync_conversation_deposit_description(store, client, existing)
|
||||
|
||||
async with _DEPOSIT_KB_LOCK:
|
||||
existing = await store.get_conversation_deposit_mapping()
|
||||
if existing is not None:
|
||||
return await _sync_conversation_deposit_description(store, client, existing)
|
||||
|
||||
remote_id = ""
|
||||
try:
|
||||
remote_items = remote_knowledge_bases
|
||||
if remote_items is None:
|
||||
remote_items = await client.list_knowledge_bases()
|
||||
for remote in remote_items:
|
||||
if str(remote.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME and remote.get("id"):
|
||||
remote_id = str(remote["id"])
|
||||
break
|
||||
except WeKnoraError:
|
||||
logger.debug("Could not scan WeKnora knowledge bases before creating deposit base", exc_info=True)
|
||||
|
||||
if remote_id:
|
||||
mapped = await store.get_by_weknora_id(remote_id)
|
||||
if mapped is not None:
|
||||
try:
|
||||
await client.update_knowledge_base(
|
||||
remote_id,
|
||||
{"description": CONVERSATION_DEPOSIT_KB_DESCRIPTION},
|
||||
)
|
||||
except WeKnoraError:
|
||||
logger.warning("Failed to update the existing conversation deposit knowledge-base description", exc_info=True)
|
||||
return await store.adopt_conversation_deposit_mapping(
|
||||
str(mapped["id"]),
|
||||
name=CONVERSATION_DEPOSIT_KB_NAME,
|
||||
description=CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
)
|
||||
try:
|
||||
remote = await client.update_knowledge_base(
|
||||
remote_id,
|
||||
{"description": CONVERSATION_DEPOSIT_KB_DESCRIPTION},
|
||||
)
|
||||
except WeKnoraError:
|
||||
logger.warning("Failed to update the existing conversation deposit knowledge-base description", exc_info=True)
|
||||
remote = {
|
||||
"id": remote_id,
|
||||
"name": CONVERSATION_DEPOSIT_KB_NAME,
|
||||
"description": CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
"type": "document",
|
||||
}
|
||||
else:
|
||||
try:
|
||||
remote = await client.create_knowledge_base(
|
||||
name=CONVERSATION_DEPOSIT_KB_NAME,
|
||||
description=CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
kb_type="document",
|
||||
wiki_enabled=True,
|
||||
)
|
||||
except WeKnoraError:
|
||||
logger.warning("Failed to create WeKnora conversation deposit knowledge base", exc_info=True)
|
||||
return None
|
||||
|
||||
try:
|
||||
row = await store.create_mapping(
|
||||
weknora_id=str(remote["id"]),
|
||||
owner_user_id=CONVERSATION_DEPOSIT_OWNER_USER_ID,
|
||||
name=CONVERSATION_DEPOSIT_KB_NAME,
|
||||
description=CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
kb_type="document",
|
||||
)
|
||||
published = await store.request_publish(str(row["id"]), CONVERSATION_DEPOSIT_OWNER_USER_ID, is_admin=True)
|
||||
return published or row
|
||||
except Exception:
|
||||
logger.exception("Failed to persist conversation deposit knowledge-base mapping")
|
||||
return None
|
||||
|
||||
|
||||
async def ensure_conversation_deposit_knowledge_base(
|
||||
app: Any,
|
||||
*,
|
||||
@@ -108,61 +214,7 @@ async def ensure_conversation_deposit_knowledge_base(
|
||||
client = _client(app)
|
||||
if store is None or client is None:
|
||||
return None
|
||||
|
||||
existing = await store.get_conversation_deposit_mapping()
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
async with _DEPOSIT_KB_LOCK:
|
||||
existing = await store.get_conversation_deposit_mapping()
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
remote_id = ""
|
||||
try:
|
||||
for remote in await client.list_knowledge_bases():
|
||||
if str(remote.get("name") or "").strip() == CONVERSATION_DEPOSIT_KB_NAME and remote.get("id"):
|
||||
remote_id = str(remote["id"])
|
||||
break
|
||||
except WeKnoraError:
|
||||
logger.debug("Could not scan WeKnora knowledge bases before creating deposit base", exc_info=True)
|
||||
|
||||
if remote_id:
|
||||
mapped = await store.get_by_weknora_id(remote_id)
|
||||
if mapped is not None:
|
||||
if mapped.get("publication_status") != "published":
|
||||
return await store.request_publish(
|
||||
str(mapped["id"]),
|
||||
owner_user_id or "system",
|
||||
is_admin=True,
|
||||
)
|
||||
return mapped
|
||||
remote = {"id": remote_id, "name": CONVERSATION_DEPOSIT_KB_NAME, "description": "DeerFlow 全局问答沉淀知识库", "type": "document"}
|
||||
else:
|
||||
try:
|
||||
remote = await client.create_knowledge_base(
|
||||
name=CONVERSATION_DEPOSIT_KB_NAME,
|
||||
description="DeerFlow 全局问答沉淀知识库。所有用户可见,不默认参与问答检索。",
|
||||
kb_type="document",
|
||||
wiki_enabled=True,
|
||||
)
|
||||
except WeKnoraError:
|
||||
logger.warning("Failed to create WeKnora conversation deposit knowledge base", exc_info=True)
|
||||
return None
|
||||
|
||||
try:
|
||||
row = await store.create_mapping(
|
||||
weknora_id=str(remote["id"]),
|
||||
owner_user_id=owner_user_id or "system",
|
||||
name=CONVERSATION_DEPOSIT_KB_NAME,
|
||||
description=str(remote.get("description") or "DeerFlow 全局问答沉淀知识库"),
|
||||
kb_type="document",
|
||||
)
|
||||
published = await store.request_publish(str(row["id"]), owner_user_id or "system", is_admin=True)
|
||||
return published or row
|
||||
except Exception:
|
||||
logger.exception("Failed to persist conversation deposit knowledge-base mapping")
|
||||
return None
|
||||
return await ensure_conversation_deposit_mapping(store, client)
|
||||
|
||||
|
||||
async def deposit_conversation_turn(app: Any, turn: ConversationTurnDeposit) -> None:
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Shared helpers for WeKnora iframe proxy sessions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
WEKNORA_EMBED_COOKIE = "deerflow_weknora_embed"
|
||||
WEKNORA_EMBED_TTL_SECONDS = 15 * 60
|
||||
WEKNORA_EMBED_API_PREFIX = "/api/llmwiki/weknora-embed"
|
||||
WEKNORA_PROXY_SECRET = os.getenv("DEERFLOW_LLMWIKI_PROXY_SECRET") or secrets.token_urlsafe(48)
|
||||
WEKNORA_ROOT_ASSET_PATHS: frozenset[str] = frozenset(
|
||||
{
|
||||
"/favicon.ico",
|
||||
"/favicon.svg",
|
||||
"/config.js",
|
||||
"/logo.svg",
|
||||
"/manifest.webmanifest",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeKnoraEmbedContext:
|
||||
mapping_id: str
|
||||
weknora_id: str
|
||||
user_id: str
|
||||
is_admin: bool
|
||||
can_write: bool
|
||||
exp: int
|
||||
is_conversation_deposit: bool = False
|
||||
|
||||
|
||||
def is_weknora_embed_proxy_path(path: str) -> bool:
|
||||
return (
|
||||
path == "/platform"
|
||||
or path.startswith("/platform/")
|
||||
or path.startswith("/assets/")
|
||||
or path.startswith("/locales/")
|
||||
or path.startswith("/tdesign-icons/")
|
||||
or path in WEKNORA_ROOT_ASSET_PATHS
|
||||
or path.startswith(f"{WEKNORA_EMBED_API_PREFIX}/api/v1/")
|
||||
or (path.startswith("/api/v1/") and not path.startswith("/api/v1/auth/"))
|
||||
)
|
||||
|
||||
|
||||
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_weknora_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_weknora_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),
|
||||
is_conversation_deposit=bool(payload.get("is_conversation_deposit")),
|
||||
)
|
||||
|
||||
|
||||
def has_valid_weknora_embed_cookie(raw: str | None) -> bool:
|
||||
try:
|
||||
verify_weknora_embed_cookie(raw)
|
||||
except HTTPException:
|
||||
return False
|
||||
return True
|
||||
Reference in new issue
Block a user