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
+8
-1
@@ -206,11 +206,18 @@ class WeKnoraClient:
|
||||
return dict(value)
|
||||
|
||||
async def update_knowledge_base(self, knowledge_base_id: str, changes: Mapping[str, Any]) -> dict[str, Any]:
|
||||
payload = dict(changes)
|
||||
if not str(payload.get("name") or "").strip():
|
||||
current = await self.get_knowledge_base(knowledge_base_id)
|
||||
current_name = str(current.get("name") or "").strip()
|
||||
if not current_name:
|
||||
raise WeKnoraError("WeKnora knowledge base name is required for updates")
|
||||
payload["name"] = current_name
|
||||
value = _data(
|
||||
await self._request(
|
||||
"PUT",
|
||||
f"/api/v1/knowledge-bases/{quote(knowledge_base_id, safe='')}",
|
||||
json=dict(changes),
|
||||
json=payload,
|
||||
)
|
||||
)
|
||||
return dict(value) if isinstance(value, dict) else {}
|
||||
|
||||
@@ -24,6 +24,15 @@ class LlmWikiStore(ABC):
|
||||
@abstractmethod
|
||||
async def update_mapping(self, mapping_id: str, *, name: str | None = None, description: str | None = None) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def adopt_conversation_deposit_mapping(
|
||||
self,
|
||||
mapping_id: str,
|
||||
*,
|
||||
name: str,
|
||||
description: str,
|
||||
) -> dict[str, Any] | None: ...
|
||||
|
||||
@abstractmethod
|
||||
async def delete_mapping(self, mapping_id: str) -> bool: ...
|
||||
|
||||
|
||||
+30
-1
@@ -76,6 +76,31 @@ class MemoryLlmWikiStore(LlmWikiStore):
|
||||
row["updated_at"] = _now()
|
||||
return dict(row)
|
||||
|
||||
async def adopt_conversation_deposit_mapping(
|
||||
self,
|
||||
mapping_id: str,
|
||||
*,
|
||||
name: str,
|
||||
description: str,
|
||||
) -> dict[str, Any] | None:
|
||||
row = self._rows.get(mapping_id)
|
||||
if row is None:
|
||||
return None
|
||||
now = _now()
|
||||
row.update(
|
||||
{
|
||||
"owner_user_id": "system",
|
||||
"name": name,
|
||||
"description": description,
|
||||
"publication_status": "published",
|
||||
"published_by": "system",
|
||||
"reviewed_by": None,
|
||||
"published_at": row.get("published_at") or now,
|
||||
"updated_at": now,
|
||||
}
|
||||
)
|
||||
return dict(row)
|
||||
|
||||
async def delete_mapping(self, mapping_id: str) -> bool:
|
||||
return self._rows.pop(mapping_id, None) is not None
|
||||
|
||||
@@ -118,7 +143,11 @@ class MemoryLlmWikiStore(LlmWikiStore):
|
||||
|
||||
async def get_conversation_deposit_mapping(self) -> dict[str, Any] | None:
|
||||
for row in self._rows.values():
|
||||
if row.get("name") == "对话沉淀" and row.get("publication_status") == "published":
|
||||
if (
|
||||
row.get("owner_user_id") == "system"
|
||||
and row.get("name") == "对话沉淀"
|
||||
and row.get("publication_status") == "published"
|
||||
):
|
||||
return dict(row)
|
||||
return None
|
||||
|
||||
|
||||
@@ -95,6 +95,29 @@ class LlmWikiRepository(LlmWikiStore):
|
||||
await session.commit()
|
||||
return _to_dict(row)
|
||||
|
||||
async def adopt_conversation_deposit_mapping(
|
||||
self,
|
||||
mapping_id: str,
|
||||
*,
|
||||
name: str,
|
||||
description: str,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(LlmWikiKnowledgeBaseRow, mapping_id)
|
||||
if row is None:
|
||||
return None
|
||||
now = _now()
|
||||
row.owner_user_id = "system"
|
||||
row.name = name
|
||||
row.description = description
|
||||
row.publication_status = "published"
|
||||
row.published_by = "system"
|
||||
row.reviewed_by = None
|
||||
row.published_at = row.published_at or now
|
||||
row.updated_at = now
|
||||
await session.commit()
|
||||
return _to_dict(row)
|
||||
|
||||
async def delete_mapping(self, mapping_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(LlmWikiKnowledgeBaseRow, mapping_id)
|
||||
@@ -155,6 +178,7 @@ class LlmWikiRepository(LlmWikiStore):
|
||||
stmt = (
|
||||
select(LlmWikiKnowledgeBaseRow)
|
||||
.where(
|
||||
LlmWikiKnowledgeBaseRow.owner_user_id == "system",
|
||||
LlmWikiKnowledgeBaseRow.name == "对话沉淀",
|
||||
LlmWikiKnowledgeBaseRow.publication_status == "published",
|
||||
)
|
||||
|
||||
+38
-8
@@ -1,8 +1,9 @@
|
||||
"""Authorized read-only WeKnora retrieval for DeerFlow Q&A."""
|
||||
"""Authorized read-only WeKnora retrieval for cmzs Q&A."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
from urllib.parse import urlencode
|
||||
@@ -17,6 +18,30 @@ from deerflow.integrations.weknora.runtime import build_weknora_client, get_llmw
|
||||
from deerflow.runtime.user_context import get_current_user, get_effective_user_id
|
||||
|
||||
|
||||
_CONVERSATION_DEPOSIT_NAME = "对话沉淀"
|
||||
_CONVERSATION_DEPOSIT_OWNER_USER_ID = "system"
|
||||
_LEGACY_BRAND_PATTERN = re.compile(r"deer[\s_-]*flow", re.IGNORECASE)
|
||||
|
||||
|
||||
def _is_conversation_deposit_mapping(row: Mapping[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_NAME
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def selected_knowledge_base_ids(context: Mapping[str, Any] | None) -> list[str] | None:
|
||||
"""Read the conversation selection; ``None`` means legacy/no selection key."""
|
||||
|
||||
@@ -84,7 +109,7 @@ async def llmwiki_search_tool(
|
||||
return f"Knowledge base {mapping_id!r} is not available to the current user."
|
||||
rows.append(row)
|
||||
else:
|
||||
rows = await store.list_visible(user_id, scope="all", is_admin=is_admin)
|
||||
rows = await store.list_visible(user_id, scope="all", is_admin=False)
|
||||
if not rows:
|
||||
return "No authorized LLMWiki knowledge bases are available."
|
||||
|
||||
@@ -102,22 +127,27 @@ async def llmwiki_search_tool(
|
||||
mapping = local_by_remote.get(str(result.get("knowledge_base_id") or ""))
|
||||
if mapping is None:
|
||||
continue
|
||||
visible_result = (
|
||||
_sanitize_conversation_deposit_value(result)
|
||||
if _is_conversation_deposit_mapping(mapping)
|
||||
else result
|
||||
)
|
||||
params = urlencode(
|
||||
{
|
||||
"knowledge_base_id": mapping["id"],
|
||||
"chunk_id": result.get("chunk_id") or "",
|
||||
"chunk_id": visible_result.get("chunk_id") or "",
|
||||
}
|
||||
)
|
||||
citations.append(
|
||||
{
|
||||
"title": result.get("title") or result.get("filename") or mapping.get("name") or "LLMWiki source",
|
||||
"content": result.get("content") or "",
|
||||
"score": result.get("score") or 0,
|
||||
"title": visible_result.get("title") or visible_result.get("filename") or mapping.get("name") or "LLMWiki source",
|
||||
"content": visible_result.get("content") or "",
|
||||
"score": visible_result.get("score") or 0,
|
||||
"knowledge_base_id": mapping["id"],
|
||||
"knowledge_base_name": mapping.get("name") or "",
|
||||
"source": mapping.get("name") or "LLMWiki",
|
||||
"knowledge_id": result.get("knowledge_id") or "",
|
||||
"chunk_id": result.get("chunk_id") or "",
|
||||
"knowledge_id": visible_result.get("knowledge_id") or "",
|
||||
"chunk_id": visible_result.get("chunk_id") or "",
|
||||
"url": f"/weknora-source-preview?{params}",
|
||||
}
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user