feat: improve knowledge sync and entity alignment

This commit is contained in:
吉鑫升 committed 2026-09-04 18:43:12 +08:00
1 parent 7dbac9cc56
commit dea4697ea0
38 files changed
+3742 -634

No files matched your search

@@ -150,6 +150,31 @@ def _list_export_manifests(mapping_id: str, *, limit: int) -> list[dict[str, Any
return rows[:limit]
def _delete_base_package_files(base_id: str, import_job_ids: list[str]) -> None:
"""Delete package artifacts owned by one assistant base only."""
root = _package_root()
if not root.is_dir():
return
job_ids = {str(value) for value in import_job_ids if value}
scope = f"assistant:{base_id}"
for manifest_path in root.glob("*.manifest.json"):
try:
value = json.loads(manifest_path.read_text(encoding="utf-8"))
except (OSError, ValueError, TypeError):
continue
if not isinstance(value, dict) or str(value.get("mapping_id") or "") != scope:
continue
job_id = str(value.get("id") or "")
if job_id:
job_ids.add(job_id)
manifest_path.unlink(missing_ok=True)
for job_id in job_ids:
path = package_path(root, job_id)
path.unlink(missing_ok=True)
path.with_suffix(".tmp").unlink(missing_ok=True)
path.with_suffix(".manifest.json").unlink(missing_ok=True)
async def _actor(request: Request, *, admin: bool = False) -> str | None:
user = await get_optional_user_from_request(request)
if user is None:
@@ -175,6 +200,11 @@ class BaseCreateRequest(BaseModel):
description: str = Field(default="", max_length=2048)
class BaseUpdateRequest(BaseModel):
name: str = Field(min_length=1, max_length=255)
description: str = Field(default="", max_length=2048)
class SkillImportRequest(BaseModel):
skill_names: list[str] = Field(min_length=1, max_length=200)
force: bool = False
@@ -224,6 +254,51 @@ async def get_base_by_id(request: Request, base_id: str) -> dict[str, Any]:
return {"base": base}
@router.patch("/bases/{base_id}")
async def update_base(request: Request, base_id: str, body: BaseUpdateRequest) -> dict[str, Any]:
await _actor(request, admin=True)
base = await _store(request).update_base(
base_id,
name=body.name,
description=body.description,
)
if base is None:
raise HTTPException(status_code=404, detail="助手知识库不存在")
return {"base": base}
@router.delete("/bases/{base_id}", status_code=204)
async def delete_base(request: Request, base_id: str) -> None:
await _actor(request, admin=True)
store = _store(request)
base = await store.get_base(base_id)
if base is None:
raise HTTPException(status_code=404, detail="助手知识库不存在")
if base.get("is_global"):
raise HTTPException(status_code=409, detail="知识梳理总库承担全库沉淀,不能删除")
jobs = await store.list_jobs(limit=500, base_id=base_id)
if any(job.get("status") in {"queued", "running"} for job in jobs):
raise HTTPException(status_code=409, detail="知识库正在导入数据,请等待任务完成后再删除")
exports = await asyncio.to_thread(
_list_export_manifests,
f"assistant:{base_id}",
limit=10_000,
)
if any(job.get("status") in {"queued", "running"} for job in exports):
raise HTTPException(status_code=409, detail="知识库正在生成导出包,请等待任务完成后再删除")
try:
deleted = await store.delete_base(base_id)
except ValueError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
if not deleted:
raise HTTPException(status_code=404, detail="助手知识库不存在")
await asyncio.to_thread(
_delete_base_package_files,
base_id,
[str(job.get("id") or "") for job in jobs],
)
@router.get("/wiki-pages")
async def list_wiki_pages(
request: Request,
@@ -287,7 +362,13 @@ async def search(
store = _store(request)
base = await store.get_base(base_id)
embedding = getattr(request.app.state, "llmwiki_embedding", None)
rows = await store.search_pages(base_ids=[base["id"]] if base else [], query=q, limit=20, include_chunks=False, embedding_client=embedding)
rows = await store.search_pages(
base_ids=[base["id"]] if base else [],
query=q,
limit=20,
include_chunks=True,
embedding_client=embedding,
)
return {"results": rows, "total": len(rows), "retrieval_mode": rows[0].get("retrieval_mode", "keyword") if rows else "empty"}