feat: improve knowledge sync and entity alignment
This commit is contained in:
1 parent
7dbac9cc56
commit
dea4697ea0
38 files changed
+3742
-634
No files matched your search
@@ -1,4 +1,4 @@
|
||||
"""Administrator APIs for skill knowledge bindings and matrix jobs."""
|
||||
"""User-facing skill distillation APIs plus administrator maintenance APIs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -21,17 +21,28 @@ from deerflow.integrations.weknora.runtime import (
|
||||
get_resolved_llmwiki_runtime,
|
||||
)
|
||||
from deerflow.skill_knowledge import scan_skill
|
||||
from deerflow.skills.storage import get_or_new_skill_storage
|
||||
from deerflow.skills.types import SkillCategory
|
||||
|
||||
router = APIRouter(prefix="/api/skill-knowledge", tags=["skill-knowledge"])
|
||||
|
||||
|
||||
async def _require_admin(request: Request) -> str | None:
|
||||
async def _actor(request: Request) -> tuple[str, bool]:
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
return None
|
||||
if getattr(user, "system_role", None) != "admin":
|
||||
return "system", True
|
||||
return str(user.id), getattr(user, "system_role", None) == "admin"
|
||||
|
||||
|
||||
async def _require_admin(request: Request) -> str:
|
||||
actor, is_admin = await _actor(request)
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="技能知识归纳仅管理员可操作")
|
||||
return str(user.id)
|
||||
return actor
|
||||
|
||||
|
||||
def _owned_by_actor(row: dict[str, Any] | None, actor: str, is_admin: bool) -> bool:
|
||||
return bool(row and (is_admin or str(row.get("created_by") or "") == actor))
|
||||
|
||||
|
||||
def _store(request: Request):
|
||||
@@ -98,9 +109,15 @@ async def list_bindings(
|
||||
skill_names: list[str] | None = Query(default=None),
|
||||
target_id: str | None = Query(default=None),
|
||||
include_detached: bool = Query(default=False),
|
||||
mine_only: bool = Query(default=False),
|
||||
) -> dict[str, Any]:
|
||||
await _require_admin(request)
|
||||
rows = await _store(request).list_bindings(skill_names=skill_names, target_id=target_id, include_detached=include_detached)
|
||||
actor, is_admin = await _actor(request)
|
||||
rows = await _store(request).list_bindings(
|
||||
skill_names=skill_names,
|
||||
target_id=target_id,
|
||||
include_detached=include_detached,
|
||||
created_by=actor if mine_only or not is_admin else None,
|
||||
)
|
||||
return {"bindings": rows}
|
||||
|
||||
|
||||
@@ -164,13 +181,26 @@ async def disable_binding_auto_sync(request: Request, binding_id: str) -> dict[s
|
||||
|
||||
@router.post("/sync-jobs", status_code=202)
|
||||
async def create_sync_job(request: Request, body: CreateSyncJobRequest) -> dict[str, Any]:
|
||||
actor = await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
# Built-in and legacy skills need not have an ownership row. Validate
|
||||
# against the same running filesystem used by the skills list/executor.
|
||||
missing = []
|
||||
favorite_names: set[str] = set()
|
||||
custom_root: Any = None
|
||||
if not is_admin:
|
||||
favorite_names = set(await request.app.state.skill_store.list_favorite_skill_names(actor))
|
||||
storage_root = get_or_new_skill_storage(
|
||||
app_config=request.app.state.config
|
||||
).get_skills_root_path().resolve()
|
||||
custom_root = (storage_root / SkillCategory.CUSTOM.value).resolve()
|
||||
for name in body.skill_names:
|
||||
try:
|
||||
await asyncio.to_thread(_skill_directory, request.app.state.config, name)
|
||||
skill_path = await asyncio.to_thread(_skill_directory, request.app.state.config, name)
|
||||
if not is_admin:
|
||||
visible = await request.app.state.skill_store.get_visible(name, actor)
|
||||
is_public_filesystem_skill = not skill_path.resolve().is_relative_to(custom_root)
|
||||
if visible is None and name not in favorite_names and not is_public_filesystem_skill:
|
||||
missing.append(name)
|
||||
except (FileNotFoundError, ValueError):
|
||||
missing.append(name)
|
||||
if missing:
|
||||
@@ -182,9 +212,14 @@ async def create_sync_job(request: Request, body: CreateSyncJobRequest) -> dict[
|
||||
raise HTTPException(status_code=422, detail="不支持技能直接归纳到助手知识库,请先归纳到 WeKnora 普通知识库")
|
||||
if target.target_mode != "wiki":
|
||||
raise HTTPException(status_code=422, detail="技能归纳目标仅支持 WeKnora Wiki 知识库")
|
||||
mapping = await request.app.state.llmwiki_store.get_authorized(target.target_id, actor or "system", write=True, is_admin=True)
|
||||
mapping = await request.app.state.llmwiki_store.get_authorized(
|
||||
target.target_id,
|
||||
actor,
|
||||
write=True,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
if mapping is None:
|
||||
raise HTTPException(status_code=404, detail=f"普通知识库不存在:{target.target_id}")
|
||||
raise HTTPException(status_code=404, detail=f"普通知识库不存在或当前用户不可写:{target.target_id}")
|
||||
data["target_name"] = target.target_name or str(mapping.get("name") or target.target_id)
|
||||
data["remote_business_key"] = target.remote_business_key or f"weknora:kb:{mapping['weknora_id']}"
|
||||
targets.append(data)
|
||||
@@ -193,7 +228,7 @@ async def create_sync_job(request: Request, body: CreateSyncJobRequest) -> dict[
|
||||
targets=targets,
|
||||
created_by=actor,
|
||||
force=body.force,
|
||||
review_mode=body.review_mode,
|
||||
review_mode=body.review_mode if is_admin else "off",
|
||||
)
|
||||
dispatcher = getattr(request.app.state, "skill_knowledge_dispatcher", None)
|
||||
if dispatcher is not None:
|
||||
@@ -202,23 +237,35 @@ async def create_sync_job(request: Request, body: CreateSyncJobRequest) -> dict[
|
||||
|
||||
|
||||
@router.get("/sync-jobs")
|
||||
async def list_sync_jobs(request: Request, limit: int = Query(default=50, ge=1, le=200)) -> dict[str, Any]:
|
||||
await _require_admin(request)
|
||||
return {"jobs": await _store(request).list_jobs(limit=limit)}
|
||||
async def list_sync_jobs(
|
||||
request: Request,
|
||||
limit: int = Query(default=50, ge=1, le=200),
|
||||
mine_only: bool = Query(default=False),
|
||||
) -> dict[str, Any]:
|
||||
actor, is_admin = await _actor(request)
|
||||
return {
|
||||
"jobs": await _store(request).list_jobs(
|
||||
limit=limit,
|
||||
created_by=actor if mine_only or not is_admin else None,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@router.get("/sync-jobs/{job_id}")
|
||||
async def get_sync_job(request: Request, job_id: str) -> dict[str, Any]:
|
||||
await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
row = await _store(request).get_job(job_id)
|
||||
if row is None:
|
||||
if not _owned_by_actor(row, actor, is_admin):
|
||||
raise HTTPException(status_code=404, detail="归纳任务不存在")
|
||||
return row
|
||||
|
||||
|
||||
@router.post("/sync-jobs/{job_id}/cancel")
|
||||
async def cancel_sync_job(request: Request, job_id: str) -> dict[str, bool]:
|
||||
await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
row = await _store(request).get_job(job_id)
|
||||
if not _owned_by_actor(row, actor, is_admin):
|
||||
raise HTTPException(status_code=404, detail="归纳任务不存在")
|
||||
if not await _store(request).cancel_job(job_id):
|
||||
raise HTTPException(status_code=404, detail="归纳任务不存在")
|
||||
return {"canceled": True}
|
||||
@@ -226,7 +273,20 @@ async def cancel_sync_job(request: Request, job_id: str) -> dict[str, bool]:
|
||||
|
||||
@router.post("/sync-items/{item_id}/retry", status_code=202)
|
||||
async def retry_sync_item(request: Request, item_id: str) -> dict[str, Any]:
|
||||
await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
jobs = await _store(request).list_jobs(limit=200, created_by=None if is_admin else actor)
|
||||
item = next(
|
||||
(item for job in jobs for item in job.get("items", []) if item.get("id") == item_id),
|
||||
None,
|
||||
)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="归纳任务项不存在")
|
||||
if item.get("target_type") == "weknora":
|
||||
mapping = await request.app.state.llmwiki_store.get_authorized(
|
||||
str(item["target_id"]), actor, write=True, is_admin=is_admin
|
||||
)
|
||||
if mapping is None:
|
||||
raise HTTPException(status_code=404, detail="目标知识库不存在或当前用户不可写")
|
||||
row = await _store(request).requeue_item(item_id)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="归纳任务项不存在")
|
||||
@@ -236,10 +296,16 @@ async def retry_sync_item(request: Request, item_id: str) -> dict[str, Any]:
|
||||
|
||||
@router.post("/bindings/{binding_id}/resync", status_code=202)
|
||||
async def resync_binding(request: Request, binding_id: str) -> dict[str, Any]:
|
||||
actor = await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
binding = await _store(request).get_binding(binding_id)
|
||||
if binding is None or not binding.get("active"):
|
||||
if not _owned_by_actor(binding, actor, is_admin) or not binding.get("active"):
|
||||
raise HTTPException(status_code=404, detail="绑定不存在")
|
||||
if binding.get("target_type") == "weknora":
|
||||
mapping = await request.app.state.llmwiki_store.get_authorized(
|
||||
str(binding["target_id"]), actor, write=True, is_admin=is_admin
|
||||
)
|
||||
if mapping is None:
|
||||
raise HTTPException(status_code=404, detail="目标知识库不存在或当前用户不可写")
|
||||
target = {
|
||||
"target_type": binding["target_type"],
|
||||
"target_mode": binding["target_mode"],
|
||||
@@ -260,10 +326,10 @@ async def resync_binding(request: Request, binding_id: str) -> dict[str, Any]:
|
||||
|
||||
@router.post("/bindings/{binding_id}/detach")
|
||||
async def detach_binding(request: Request, binding_id: str, body: DetachRequest) -> dict[str, Any]:
|
||||
await _require_admin(request)
|
||||
actor, is_admin = await _actor(request)
|
||||
store = _store(request)
|
||||
binding = await store.get_binding(binding_id)
|
||||
if binding is None:
|
||||
if not _owned_by_actor(binding, actor, is_admin):
|
||||
raise HTTPException(status_code=404, detail="绑定不存在")
|
||||
if await store.has_active_item(binding_id):
|
||||
raise HTTPException(status_code=409, detail="该绑定仍有运行中的归纳项,请先取消任务")
|
||||
@@ -278,9 +344,9 @@ async def detach_binding(request: Request, binding_id: str, body: DetachRequest)
|
||||
elif binding["target_type"] == "weknora":
|
||||
mapping = await request.app.state.llmwiki_store.get_authorized(
|
||||
str(binding["target_id"]),
|
||||
"system",
|
||||
actor,
|
||||
write=True,
|
||||
is_admin=True,
|
||||
is_admin=is_admin,
|
||||
)
|
||||
runtime = get_resolved_llmwiki_runtime(request.app.state.config)
|
||||
if mapping is None or not runtime.weknora_enabled:
|
||||
|
||||
Reference in new issue
Block a user