sync update from LAN repo (source master @ ca48c4ec)
This commit is contained in:
1 parent
8bc8b0e082
commit
ea9dcff9c0
873 files changed
+107866
-14011
No files matched your search
@@ -18,6 +18,7 @@ from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
from typing import Any, Literal
|
||||
from urllib.parse import quote, unquote, urlencode, urlsplit
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
from fastapi import APIRouter, BackgroundTasks, File, HTTPException, Query, Request, UploadFile
|
||||
@@ -26,7 +27,7 @@ from langchain_core.messages import HumanMessage
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.csrf_middleware import is_secure_request
|
||||
from app.gateway.deps import get_agent_store, get_optional_user_from_request, get_thread_store
|
||||
from app.gateway.deps import get_agent_store, get_checkpointer, get_optional_user_from_request, get_thread_store
|
||||
from app.gateway.llmwiki_deposit import (
|
||||
CONVERSATION_DEPOSIT_KB_DESCRIPTION,
|
||||
CONVERSATION_DEPOSIT_KB_NAME,
|
||||
@@ -36,6 +37,7 @@ from app.gateway.llmwiki_deposit import (
|
||||
ensure_conversation_deposit_mapping,
|
||||
)
|
||||
from app.gateway.llmwiki_rag import build_llmwiki_rag_context
|
||||
from app.gateway.perf_logging import finish_trace, trace_request, trace_stage
|
||||
from app.gateway.weknora_embed import (
|
||||
WEKNORA_EMBED_API_PREFIX as _WEKNORA_EMBED_API_PREFIX,
|
||||
)
|
||||
@@ -60,6 +62,7 @@ from app.gateway.weknora_embed import (
|
||||
from app.gateway.weknora_static import StaticAsset, StaticAssetCache, prepare_static_asset
|
||||
from app.gateway.wiki_directory_constraints import (
|
||||
directory_roots,
|
||||
ensure_directory_paths,
|
||||
normalize_directory_paths,
|
||||
paths_from_parent_directories,
|
||||
resolve_directory_paths,
|
||||
@@ -88,6 +91,7 @@ from deerflow.integrations.weknora.runtime import build_weknora_client, get_reso
|
||||
from deerflow.persistence.llmwiki import LlmWikiStore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
KNOWLEDGE_RESEARCH_DIRECTORY = "深度研究"
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -346,6 +350,20 @@ class WikiPageUpdate(BaseModel):
|
||||
wiki_path: str | None = Field(default=None, max_length=1024)
|
||||
|
||||
|
||||
class KnowledgeResearchSessionCreate(BaseModel):
|
||||
question: str = Field(..., min_length=1, max_length=20_000)
|
||||
|
||||
|
||||
class KnowledgeResearchSessionUpdate(BaseModel):
|
||||
title: str | None = Field(default=None, min_length=1, max_length=255)
|
||||
draft_markdown: str | None = Field(default=None, max_length=2_000_000)
|
||||
|
||||
|
||||
class KnowledgeResearchPublicationSave(BaseModel):
|
||||
title: str = Field(..., min_length=1, max_length=255)
|
||||
expected_saved_hash: str | None = Field(default=None, min_length=64, max_length=64)
|
||||
|
||||
|
||||
class ConversationDepositCreate(BaseModel):
|
||||
thread_id: str = Field(..., min_length=1, max_length=128)
|
||||
question: str = Field(default="", max_length=200_000)
|
||||
@@ -505,6 +523,28 @@ def _select_embedding_model(models: list[dict[str, Any]], requested_id: str | No
|
||||
return str(selected["id"])
|
||||
|
||||
|
||||
def _select_knowledge_qa_model(models: list[dict[str, Any]]) -> str:
|
||||
available = [
|
||||
item
|
||||
for item in models
|
||||
if str(item.get("type") or "").strip().lower() == "knowledgeqa"
|
||||
and str(item.get("id") or "").strip()
|
||||
and str(item.get("status") or "active").strip().lower() == "active"
|
||||
]
|
||||
available.sort(
|
||||
key=lambda item: (
|
||||
0 if item.get("is_default") is True else 1,
|
||||
str(item.get("id") or "").strip(),
|
||||
)
|
||||
)
|
||||
if not available:
|
||||
raise WeKnoraError(
|
||||
"WeKnora has no active KnowledgeQA model; configure one before creating a Wiki",
|
||||
status_code=400,
|
||||
)
|
||||
return str(available[0]["id"]).strip()
|
||||
|
||||
|
||||
def _json_response(payload: Any, *, status_code: int = 200) -> Response:
|
||||
return Response(
|
||||
content=json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8"),
|
||||
@@ -2326,7 +2366,7 @@ def _mapping_view(row: dict[str, Any], remote: dict[str, Any] | None, *, actor_u
|
||||
})
|
||||
return {
|
||||
"id": row["id"],
|
||||
"remote_status": "available" if remote is not None else "missing",
|
||||
"remote_status": "cached" if source.get("_local_cache_only") else "available" if remote is not None else "missing",
|
||||
"name": display_name,
|
||||
"description": description,
|
||||
"test_questions": resolved_test_questions(profile_row),
|
||||
@@ -3466,42 +3506,76 @@ async def list_knowledge_bases(
|
||||
scope: Literal["personal", "public", "all"] = Query(default="all"),
|
||||
refresh: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
client = _client_or_503()
|
||||
user_id, is_admin = await _actor(request)
|
||||
trace = trace_request(request, "llmwiki_knowledge_bases")
|
||||
try:
|
||||
remote_items = await client.list_knowledge_bases(refresh=refresh)
|
||||
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"]))
|
||||
# A successful remote list is authoritative for every scope. Keep the
|
||||
# mapping for existing references, but never display a deleted base.
|
||||
if remote_item is None:
|
||||
continue
|
||||
row, remote_item = await _sync_conversation_deposit_view(_store(request), client, row, remote_item)
|
||||
knowledge_bases.append(_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin))
|
||||
return {"knowledge_bases": knowledge_bases}
|
||||
stage_started = time.perf_counter()
|
||||
user_id, is_admin = await _actor(request)
|
||||
trace_stage(trace, "actor_and_access", stage_started)
|
||||
|
||||
stage_started = time.perf_counter()
|
||||
store = _store(request)
|
||||
rows = await store.list_visible(user_id, scope=scope, is_admin=False)
|
||||
trace_stage(trace, "local_knowledge_base_query_db", stage_started)
|
||||
|
||||
client: WeKnoraClient | None = None
|
||||
remote_items: list[dict[str, Any]] = []
|
||||
remote: dict[str, dict[str, Any]] = {}
|
||||
if refresh:
|
||||
stage_started = time.perf_counter()
|
||||
client = _client_or_503()
|
||||
try:
|
||||
remote_items = await client.list_knowledge_bases(refresh=True)
|
||||
remote = {str(item.get("id")): item for item in remote_items if item.get("id")}
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
trace_stage(trace, "weknora_explicit_refresh_wait", stage_started)
|
||||
|
||||
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.get_by_weknora_id(str(deposit_remote.get("id") or ""))
|
||||
deposit_mapping = await ensure_conversation_deposit_mapping(
|
||||
store,
|
||||
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,
|
||||
}
|
||||
trace_stage(trace, "conversation_deposit_mapping_sync", stage_started)
|
||||
|
||||
stage_started = time.perf_counter()
|
||||
knowledge_bases: list[dict[str, Any]] = []
|
||||
for row in rows:
|
||||
if refresh:
|
||||
remote_item = remote.get(str(row["weknora_id"]))
|
||||
# An explicit successful refresh is authoritative: do not show
|
||||
# mappings whose remote knowledge base has been deleted.
|
||||
if remote_item is None:
|
||||
continue
|
||||
assert client is not None
|
||||
row, remote_item = await _sync_conversation_deposit_view(store, client, row, remote_item)
|
||||
else:
|
||||
# Normal page loads are served entirely from the local SQL
|
||||
# mapping. A remote check happens only after an explicit refresh.
|
||||
remote_item = {
|
||||
"id": str(row.get("weknora_id") or ""),
|
||||
"name": str(row.get("name") or ""),
|
||||
"description": str(row.get("description") or ""),
|
||||
"_local_cache_only": True,
|
||||
}
|
||||
knowledge_bases.append(_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin))
|
||||
trace_stage(trace, "map_visible_knowledge_bases", stage_started)
|
||||
trace["details"].update({"scope": scope, "refresh": refresh, "remote_count": len(remote_items) if refresh else None, "local_count": len(rows), "result_count": len(knowledge_bases)})
|
||||
return {"knowledge_bases": knowledge_bases}
|
||||
finally:
|
||||
finish_trace(trace)
|
||||
|
||||
|
||||
@router.get("/api/llmwiki/embedding-models")
|
||||
@@ -3523,37 +3597,62 @@ async def list_embedding_models(request: Request) -> dict[str, Any]:
|
||||
async def list_agent_knowledge_bases(request: Request, agent_id: str) -> dict[str, Any]:
|
||||
"""Return the agent-bound bases still visible to the current DeerFlow user."""
|
||||
|
||||
user_id, is_admin = await _actor(request)
|
||||
agent_store = get_agent_store(request)
|
||||
agent = await (agent_store.get_any(agent_id) if is_admin else agent_store.get_visible(agent_id, user_id))
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=404, detail="Agent not found")
|
||||
trace = trace_request(request, "llmwiki_agent_knowledge_bases")
|
||||
try:
|
||||
config = load_agent_config(agent_id)
|
||||
except (FileNotFoundError, ValueError):
|
||||
config = None
|
||||
bound_ids = list(config.llmwiki_knowledge_base_ids or []) if config else []
|
||||
rows: list[dict[str, Any]] = []
|
||||
for mapping_id in bound_ids:
|
||||
row = await _store(request).get_authorized(
|
||||
mapping_id,
|
||||
user_id,
|
||||
write=False,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
if row is not None:
|
||||
rows.append(row)
|
||||
try:
|
||||
remote = {str(item.get("id")): item for item in await _client_or_503().list_knowledge_bases() if item.get("id")}
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
return {"knowledge_bases": [_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin) for row in rows if (remote_item := remote.get(str(row["weknora_id"]))) is not None]}
|
||||
stage_started = time.perf_counter()
|
||||
user_id, is_admin = await _actor(request)
|
||||
agent_store = get_agent_store(request)
|
||||
agent = await (agent_store.get_any(agent_id) if is_admin else agent_store.get_visible(agent_id, user_id))
|
||||
if agent is None:
|
||||
raise HTTPException(status_code=404, detail="Agent not found")
|
||||
trace_stage(trace, "actor_and_agent_access", stage_started)
|
||||
|
||||
stage_started = time.perf_counter()
|
||||
try:
|
||||
config = load_agent_config(agent_id)
|
||||
except (FileNotFoundError, ValueError):
|
||||
config = None
|
||||
bound_ids = list(config.llmwiki_knowledge_base_ids or []) if config else []
|
||||
trace_stage(trace, "load_agent_binding_config", stage_started)
|
||||
|
||||
stage_started = time.perf_counter()
|
||||
store = _store(request)
|
||||
rows: list[dict[str, Any]] = []
|
||||
for mapping_id in bound_ids:
|
||||
row = await store.get_authorized(
|
||||
mapping_id,
|
||||
user_id,
|
||||
write=False,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
if row is not None:
|
||||
rows.append(row)
|
||||
trace_stage(trace, "local_knowledge_base_mapping_query", stage_started)
|
||||
|
||||
stage_started = time.perf_counter()
|
||||
knowledge_bases = []
|
||||
for row in rows:
|
||||
remote_item = {
|
||||
"id": str(row.get("weknora_id") or ""),
|
||||
"name": str(row.get("name") or ""),
|
||||
"description": str(row.get("description") or ""),
|
||||
"_local_cache_only": True,
|
||||
}
|
||||
knowledge_bases.append(_mapping_view(row, remote_item, actor_user_id=user_id, is_admin=is_admin))
|
||||
trace_stage(trace, "map_agent_knowledge_bases", stage_started)
|
||||
trace["details"].update({"bound_count": len(bound_ids), "remote_count": None, "result_count": len(knowledge_bases)})
|
||||
return {"knowledge_bases": knowledge_bases}
|
||||
finally:
|
||||
finish_trace(trace)
|
||||
|
||||
|
||||
@router.post("/api/llmwiki/knowledge-bases", status_code=201)
|
||||
async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) -> dict[str, Any]:
|
||||
client = _client_or_503()
|
||||
trace = trace_request(request, "llmwiki_knowledge_base_create")
|
||||
stage_started = time.perf_counter()
|
||||
user_id, is_admin = await _actor(request)
|
||||
trace_stage(trace, "actor_and_access", stage_started)
|
||||
if body.name.strip() == CONVERSATION_DEPOSIT_KB_NAME:
|
||||
raise HTTPException(status_code=409, detail="对话沉淀是 cmzs 系统保留知识库")
|
||||
wiki_config = body.wiki_config if body.wiki_enabled else None
|
||||
@@ -3595,16 +3694,24 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from None
|
||||
try:
|
||||
stage_started = time.perf_counter()
|
||||
upstream_models = await client.list_models()
|
||||
embedding_model_id = _select_embedding_model(
|
||||
await _embedding_models(client),
|
||||
[view for item in upstream_models if (view := _embedding_model_view(item)) is not None],
|
||||
body.embedding_model_id,
|
||||
)
|
||||
knowledge_qa_model_id = (
|
||||
_select_knowledge_qa_model(upstream_models) if body.wiki_enabled else None
|
||||
)
|
||||
trace_stage(trace, "weknora_model_catalog", stage_started)
|
||||
stage_started = time.perf_counter()
|
||||
remote = await client.create_knowledge_base(
|
||||
name=body.name.strip(),
|
||||
description=body.description,
|
||||
kb_type=body.type,
|
||||
wiki_enabled=body.wiki_enabled,
|
||||
embedding_model_id=embedding_model_id,
|
||||
knowledge_qa_model_id=knowledge_qa_model_id,
|
||||
wiki_content_instructions=(
|
||||
compiled.content_instructions if body.wiki_enabled else ""
|
||||
),
|
||||
@@ -3612,11 +3719,14 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
compiled.extraction_instructions if body.wiki_enabled else ""
|
||||
),
|
||||
)
|
||||
trace_stage(trace, "weknora_create", stage_started)
|
||||
except WeKnoraError as exc:
|
||||
finish_trace(trace)
|
||||
_raise_upstream(exc)
|
||||
try:
|
||||
# Directory settings are references for WeKnora's generator. Creating
|
||||
# folders here would turn the reference into an accidental template.
|
||||
stage_started = time.perf_counter()
|
||||
row = await _store(request).create_mapping(
|
||||
weknora_id=str(remote["id"]),
|
||||
owner_user_id=user_id,
|
||||
@@ -3643,7 +3753,9 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
),
|
||||
wiki_config_apply_error=None,
|
||||
)
|
||||
trace_stage(trace, "local_mapping_create", stage_started)
|
||||
except Exception as exc:
|
||||
finish_trace(trace)
|
||||
logger.exception(
|
||||
"Failed to initialize or persist LLMWiki mapping; compensating remote create"
|
||||
)
|
||||
@@ -3655,6 +3767,7 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
_raise_upstream(exc)
|
||||
raise HTTPException(status_code=500, detail="Failed to register the knowledge base") from None
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
finish_trace(trace)
|
||||
return _mapping_view(row, remote, actor_user_id=user_id, is_admin=is_admin)
|
||||
|
||||
|
||||
@@ -4056,6 +4169,513 @@ async def get_knowledge_base_graph(
|
||||
return {"nodes": nodes, "edges": edges, "meta": meta}
|
||||
|
||||
|
||||
def _knowledge_research_store(request: Request):
|
||||
store = getattr(request.app.state, "knowledge_research_store", None)
|
||||
if store is None:
|
||||
raise HTTPException(status_code=503, detail="知识库研究存储尚未就绪")
|
||||
return store
|
||||
|
||||
|
||||
async def _knowledge_research_can_write(
|
||||
request: Request,
|
||||
mapping: dict[str, Any],
|
||||
user_id: str,
|
||||
is_admin: bool,
|
||||
) -> bool:
|
||||
if _is_conversation_deposit_mapping(mapping):
|
||||
return False
|
||||
writable = await _store(request).get_authorized(
|
||||
str(mapping["id"]),
|
||||
user_id,
|
||||
write=True,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
return writable is not None
|
||||
|
||||
|
||||
def _knowledge_research_publication_view(
|
||||
row: dict[str, Any],
|
||||
*,
|
||||
user_id: str,
|
||||
can_write: bool,
|
||||
include_content: bool = False,
|
||||
) -> dict[str, Any] | None:
|
||||
if not row.get("saved_hash"):
|
||||
return None
|
||||
owned = row.get("user_id") == user_id
|
||||
result: dict[str, Any] = {
|
||||
"id": row["publication_id"],
|
||||
"title": row.get("saved_title") or row.get("title") or "",
|
||||
"wiki_slug": row.get("wiki_slug") or "",
|
||||
"saved_hash": row["saved_hash"],
|
||||
"saved_at": row.get("saved_at"),
|
||||
"updated_at": row.get("saved_at") or row.get("updated_at"),
|
||||
"owned_by_current_user": owned,
|
||||
"can_continue": owned,
|
||||
"can_write": owned and can_write,
|
||||
}
|
||||
if owned:
|
||||
# Only the creator receives a bridge from the public book to the
|
||||
# private research session. The thread id remains session-only.
|
||||
result["session_id"] = row["id"]
|
||||
if include_content:
|
||||
result["content_markdown"] = row.get("saved_markdown") or ""
|
||||
return result
|
||||
|
||||
|
||||
def _knowledge_research_session_view(
|
||||
row: dict[str, Any],
|
||||
*,
|
||||
user_id: str,
|
||||
can_write: bool,
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row["id"],
|
||||
"thread_id": row["thread_id"],
|
||||
"title": row.get("title") or "",
|
||||
"draft_markdown": row.get("draft_markdown") or "",
|
||||
"created_at": row.get("created_at"),
|
||||
"updated_at": row.get("updated_at"),
|
||||
"can_write": can_write,
|
||||
"publication": _knowledge_research_publication_view(
|
||||
row,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _knowledge_research_default_title(question: str) -> str:
|
||||
for line in question.splitlines():
|
||||
normalized = " ".join(line.split())
|
||||
if normalized:
|
||||
return normalized[:255]
|
||||
raise HTTPException(status_code=422, detail="问题不能为空")
|
||||
|
||||
|
||||
@router.get("/api/llmwiki/knowledge-bases/{mapping_id}/research/sessions")
|
||||
async def list_knowledge_research_sessions(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
) -> dict[str, Any]:
|
||||
mapping, user_id, is_admin = await _authorized_mapping(
|
||||
request,
|
||||
mapping_id,
|
||||
write=False,
|
||||
)
|
||||
can_write = await _knowledge_research_can_write(
|
||||
request,
|
||||
mapping,
|
||||
user_id,
|
||||
is_admin,
|
||||
)
|
||||
store = _knowledge_research_store(request)
|
||||
sessions, publications = await asyncio.gather(
|
||||
store.list_owned(mapping_id, user_id),
|
||||
store.list_published(mapping_id),
|
||||
)
|
||||
return {
|
||||
"sessions": [
|
||||
_knowledge_research_session_view(
|
||||
row,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
for row in sessions
|
||||
],
|
||||
"publications": [
|
||||
view
|
||||
for row in publications
|
||||
if (
|
||||
view := _knowledge_research_publication_view(
|
||||
row,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
)
|
||||
is not None
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.post("/api/llmwiki/knowledge-bases/{mapping_id}/research/sessions", status_code=201)
|
||||
async def create_knowledge_research_session(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
body: KnowledgeResearchSessionCreate,
|
||||
) -> dict[str, Any]:
|
||||
mapping, user_id, is_admin = await _authorized_mapping(
|
||||
request,
|
||||
mapping_id,
|
||||
write=False,
|
||||
)
|
||||
can_write = await _knowledge_research_can_write(
|
||||
request,
|
||||
mapping,
|
||||
user_id,
|
||||
is_admin,
|
||||
)
|
||||
question = body.question.strip()
|
||||
title = _knowledge_research_default_title(question)
|
||||
store = _knowledge_research_store(request)
|
||||
session_id = str(uuid4())
|
||||
publication_id = str(uuid4())
|
||||
thread_id = str(uuid4())
|
||||
session = await store.create(
|
||||
session_id=session_id,
|
||||
publication_id=publication_id,
|
||||
thread_id=thread_id,
|
||||
knowledge_base_id=mapping_id,
|
||||
user_id=user_id,
|
||||
title=title,
|
||||
)
|
||||
metadata = {
|
||||
"system": True,
|
||||
"thread_type": "knowledge_research",
|
||||
"knowledge_research_session_id": session_id,
|
||||
"knowledge_research_knowledge_base_id": mapping_id,
|
||||
"llmwiki_knowledge_base_ids": [mapping_id],
|
||||
"assistant_knowledge_base_ids": [],
|
||||
"knowledge_retrieval_mode": "classic",
|
||||
"knowledge_space_strict": True,
|
||||
"rag_mode_enabled": True,
|
||||
"rag_mode": True,
|
||||
}
|
||||
try:
|
||||
from app.gateway.routers.threads import ThreadCreateRequest, create_thread
|
||||
|
||||
await create_thread(
|
||||
ThreadCreateRequest(
|
||||
thread_id=thread_id,
|
||||
assistant_id=None,
|
||||
metadata=metadata,
|
||||
initialize_checkpoint=True,
|
||||
),
|
||||
request,
|
||||
)
|
||||
await get_thread_store(request).update_display_name(
|
||||
thread_id,
|
||||
title,
|
||||
user_id=user_id,
|
||||
)
|
||||
except Exception:
|
||||
# Thread creation writes metadata before its initial checkpoint. Undo
|
||||
# every side on failure so no half-bound private chat remains.
|
||||
await store.delete(session_id)
|
||||
try:
|
||||
checkpointer = get_checkpointer(request)
|
||||
if hasattr(checkpointer, "adelete_thread"):
|
||||
await checkpointer.adelete_thread(thread_id)
|
||||
except Exception:
|
||||
logger.debug("Could not remove failed research checkpoint", exc_info=True)
|
||||
try:
|
||||
await get_thread_store(request).delete(thread_id, user_id=user_id)
|
||||
except Exception:
|
||||
logger.debug("Could not remove failed research thread metadata", exc_info=True)
|
||||
raise
|
||||
return _knowledge_research_session_view(
|
||||
session,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
|
||||
|
||||
async def _owned_knowledge_research_session(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
session_id: str,
|
||||
) -> tuple[dict[str, Any], dict[str, Any], str, bool, bool]:
|
||||
mapping, user_id, is_admin = await _authorized_mapping(
|
||||
request,
|
||||
mapping_id,
|
||||
write=False,
|
||||
)
|
||||
session = await _knowledge_research_store(request).get_owned(
|
||||
session_id,
|
||||
user_id,
|
||||
)
|
||||
if session is None or session["knowledge_base_id"] != mapping_id:
|
||||
raise HTTPException(status_code=404, detail="研究会话不存在或无权访问")
|
||||
can_write = await _knowledge_research_can_write(
|
||||
request,
|
||||
mapping,
|
||||
user_id,
|
||||
is_admin,
|
||||
)
|
||||
return session, mapping, user_id, is_admin, can_write
|
||||
|
||||
|
||||
@router.get(
|
||||
"/api/llmwiki/knowledge-bases/{mapping_id}/research/sessions/{session_id}"
|
||||
)
|
||||
async def get_knowledge_research_session(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
session_id: str,
|
||||
) -> dict[str, Any]:
|
||||
session, _, user_id, _, can_write = await _owned_knowledge_research_session(
|
||||
request,
|
||||
mapping_id,
|
||||
session_id,
|
||||
)
|
||||
return _knowledge_research_session_view(
|
||||
session,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/api/llmwiki/knowledge-bases/{mapping_id}/research/sessions/{session_id}"
|
||||
)
|
||||
async def update_knowledge_research_session(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
session_id: str,
|
||||
body: KnowledgeResearchSessionUpdate,
|
||||
) -> dict[str, Any]:
|
||||
current, _, user_id, _, can_write = await _owned_knowledge_research_session(
|
||||
request,
|
||||
mapping_id,
|
||||
session_id,
|
||||
)
|
||||
fields = body.model_fields_set
|
||||
if not fields:
|
||||
return _knowledge_research_session_view(
|
||||
current,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
title = body.title.strip() if "title" in fields and body.title is not None else None
|
||||
if "title" in fields and not title:
|
||||
raise HTTPException(status_code=422, detail="标题不能为空")
|
||||
updated = await _knowledge_research_store(request).update_owned(
|
||||
session_id,
|
||||
user_id,
|
||||
title=title,
|
||||
draft_markdown=(
|
||||
body.draft_markdown if "draft_markdown" in fields else None
|
||||
),
|
||||
)
|
||||
if updated is None:
|
||||
raise HTTPException(status_code=404, detail="研究会话不存在或无权访问")
|
||||
if title is not None:
|
||||
try:
|
||||
await get_thread_store(request).update_display_name(
|
||||
updated["thread_id"],
|
||||
title,
|
||||
user_id=user_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("Could not update research thread title", exc_info=True)
|
||||
return _knowledge_research_session_view(
|
||||
updated,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
)
|
||||
|
||||
|
||||
async def _upsert_knowledge_research_wiki_page(
|
||||
*,
|
||||
client: WeKnoraClient,
|
||||
knowledge_base_id: str,
|
||||
slug: str,
|
||||
payload: dict[str, Any],
|
||||
previously_saved: bool,
|
||||
) -> dict[str, Any]:
|
||||
if previously_saved:
|
||||
try:
|
||||
return await client.update_wiki_page(
|
||||
knowledge_base_id,
|
||||
slug,
|
||||
payload,
|
||||
)
|
||||
except WeKnoraError as exc:
|
||||
if exc.status_code != 404:
|
||||
raise
|
||||
return await client.create_wiki_page(
|
||||
knowledge_base_id,
|
||||
{"slug": slug, **payload},
|
||||
)
|
||||
try:
|
||||
return await client.create_wiki_page(
|
||||
knowledge_base_id,
|
||||
{"slug": slug, **payload},
|
||||
)
|
||||
except WeKnoraError as create_error:
|
||||
# A prior request may have committed upstream before its local DB write
|
||||
# failed. Probe the stable slug and turn the retry into an update.
|
||||
if create_error.status_code not in {400, 409}:
|
||||
raise
|
||||
try:
|
||||
await client.get_wiki_page(knowledge_base_id, slug)
|
||||
except WeKnoraError:
|
||||
raise create_error
|
||||
return await client.update_wiki_page(knowledge_base_id, slug, payload)
|
||||
|
||||
|
||||
@router.put(
|
||||
"/api/llmwiki/knowledge-bases/{mapping_id}/research/sessions/{session_id}/publication"
|
||||
)
|
||||
async def save_knowledge_research_publication(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
session_id: str,
|
||||
body: KnowledgeResearchPublicationSave,
|
||||
) -> dict[str, Any]:
|
||||
# Write authorization and private-session ownership are independent: even
|
||||
# an administrator may not publish another user's private conversation.
|
||||
mapping, user_id, _ = await _authorized_mapping(request, mapping_id, write=True)
|
||||
if _is_conversation_deposit_mapping(mapping):
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Conversation deposit is managed by cmzs",
|
||||
)
|
||||
title = body.title.strip()
|
||||
if not title:
|
||||
raise HTTPException(status_code=422, detail="标题不能为空")
|
||||
store = _knowledge_research_store(request)
|
||||
client = _client_or_503()
|
||||
remote_id = str(mapping["weknora_id"])
|
||||
saved: dict[str, Any] | None = None
|
||||
async with store.locked_publication(session_id, user_id) as publication:
|
||||
if publication is None:
|
||||
raise HTTPException(status_code=404, detail="研究会话不存在或无权访问")
|
||||
session = publication.snapshot()
|
||||
if session["knowledge_base_id"] != mapping_id:
|
||||
raise HTTPException(status_code=404, detail="研究会话不存在或无权访问")
|
||||
markdown = str(session.get("draft_markdown") or "")
|
||||
if not markdown.strip():
|
||||
raise HTTPException(status_code=422, detail="请先生成或填写 Markdown 正文")
|
||||
digest = hashlib.sha256(
|
||||
json.dumps(
|
||||
{"title": title, "markdown": markdown},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
).encode("utf-8")
|
||||
).hexdigest()
|
||||
current_saved_hash = session.get("saved_hash")
|
||||
if current_saved_hash == digest:
|
||||
# A prior identical request may have committed both upstream and
|
||||
# locally while its HTTP response was lost. Treat that retry as
|
||||
# a no-op even if it carries the pre-save expected hash.
|
||||
return _knowledge_research_session_view(
|
||||
session,
|
||||
user_id=user_id,
|
||||
can_write=True,
|
||||
)
|
||||
if current_saved_hash and body.expected_saved_hash is None:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "KNOWLEDGE_RESEARCH_EXPECTED_HASH_REQUIRED",
|
||||
"message": "覆盖已保存文档时必须携带当前版本,请刷新后再保存。",
|
||||
"saved_hash": current_saved_hash,
|
||||
},
|
||||
)
|
||||
if (
|
||||
body.expected_saved_hash is not None
|
||||
and body.expected_saved_hash != current_saved_hash
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "KNOWLEDGE_RESEARCH_PUBLICATION_CHANGED",
|
||||
"message": "文档已被更新,请刷新后再保存。",
|
||||
"saved_hash": current_saved_hash,
|
||||
},
|
||||
)
|
||||
|
||||
slug = str(
|
||||
session.get("wiki_slug")
|
||||
or f"knowledge-research/{session['publication_id']}"
|
||||
)
|
||||
try:
|
||||
folder_ids = await ensure_directory_paths(
|
||||
client,
|
||||
remote_id,
|
||||
[[KNOWLEDGE_RESEARCH_DIRECTORY]],
|
||||
)
|
||||
folder_id = folder_ids.get(KNOWLEDGE_RESEARCH_DIRECTORY)
|
||||
if not folder_id:
|
||||
raise WeKnoraError("未能创建深度研究 Wiki 目录", status_code=502)
|
||||
page = await _upsert_knowledge_research_wiki_page(
|
||||
client=client,
|
||||
knowledge_base_id=remote_id,
|
||||
slug=slug,
|
||||
previously_saved=bool(current_saved_hash),
|
||||
payload={
|
||||
"title": title,
|
||||
"content": markdown,
|
||||
"summary": "知识库问答沉淀",
|
||||
"page_type": "summary",
|
||||
"status": "published",
|
||||
"category_path": [KNOWLEDGE_RESEARCH_DIRECTORY],
|
||||
"folder_id": folder_id,
|
||||
},
|
||||
)
|
||||
except WeKnoraError as exc:
|
||||
if exc.status_code == 409:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from None
|
||||
_raise_upstream(exc)
|
||||
|
||||
saved = publication.save(
|
||||
title=title,
|
||||
markdown=markdown,
|
||||
saved_hash=digest,
|
||||
wiki_slug=slug,
|
||||
wiki_page_id=str(page.get("id") or page.get("page_id") or "")
|
||||
or None,
|
||||
wiki_folder_id=folder_id,
|
||||
)
|
||||
assert saved is not None
|
||||
_schedule_local_wiki_sync(request, mapping)
|
||||
return _knowledge_research_session_view(
|
||||
saved,
|
||||
user_id=user_id,
|
||||
can_write=True,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/api/llmwiki/knowledge-bases/{mapping_id}/research/publications/{publication_id}"
|
||||
)
|
||||
async def get_knowledge_research_publication(
|
||||
request: Request,
|
||||
mapping_id: str,
|
||||
publication_id: str,
|
||||
) -> dict[str, Any]:
|
||||
mapping, user_id, is_admin = await _authorized_mapping(
|
||||
request,
|
||||
mapping_id,
|
||||
write=False,
|
||||
)
|
||||
publication = await _knowledge_research_store(request).get_publication(
|
||||
publication_id
|
||||
)
|
||||
if (
|
||||
publication is None
|
||||
or publication["knowledge_base_id"] != mapping_id
|
||||
):
|
||||
raise HTTPException(status_code=404, detail="研究文档不存在")
|
||||
can_write = await _knowledge_research_can_write(
|
||||
request,
|
||||
mapping,
|
||||
user_id,
|
||||
is_admin,
|
||||
)
|
||||
view = _knowledge_research_publication_view(
|
||||
publication,
|
||||
user_id=user_id,
|
||||
can_write=can_write,
|
||||
include_content=True,
|
||||
)
|
||||
if view is None:
|
||||
raise HTTPException(status_code=404, detail="研究文档不存在")
|
||||
return view
|
||||
|
||||
|
||||
@router.get("/api/llmwiki/knowledge-bases/{mapping_id}/wiki/pages")
|
||||
async def list_wiki_pages(
|
||||
request: Request,
|
||||
|
||||
Reference in new issue
Block a user