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
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user