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
@@ -379,6 +379,17 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
resolved_llmwiki = get_resolved_llmwiki_runtime(config)
|
||||
weknora_client = build_weknora_client(resolved_llmwiki) if resolved_llmwiki.weknora_enabled else None
|
||||
embedding_config = local_index_config.embedding
|
||||
# The administrator may select or upload the offline encoder from the
|
||||
# UI. This runtime-owned value deliberately overrides config.yaml and
|
||||
# survives application restarts without modifying release assets.
|
||||
if embedding_config.provider == "local":
|
||||
from deerflow.config.system_settings import load_system_settings
|
||||
|
||||
runtime_model_path = load_system_settings().local_wiki_encoder.model_path.strip()
|
||||
if runtime_model_path:
|
||||
embedding_config = embedding_config.model_copy(
|
||||
update={"local_model_path": runtime_model_path}
|
||||
)
|
||||
embedding_configured = bool(
|
||||
embedding_config.enabled
|
||||
and (embedding_config.base_url or embedding_config.provider == "local")
|
||||
|
||||
@@ -49,9 +49,14 @@ async def _sync_once(app: FastAPI) -> None:
|
||||
async def local_wiki_index_scheduler_loop(app: FastAPI) -> None:
|
||||
interval = app.state.config.llmwiki.local_wiki_index.sync_interval_seconds
|
||||
logger.info(
|
||||
"Local Wiki auto-sync scheduler started; first scan delayed by %s seconds",
|
||||
"Local Wiki auto-sync scheduler started; first scan is immediate, interval=%s seconds",
|
||||
interval,
|
||||
)
|
||||
while True:
|
||||
try:
|
||||
await _sync_once(app)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception:
|
||||
logger.exception("Local Wiki auto-sync scheduler tick failed (non-fatal)")
|
||||
await asyncio.sleep(interval)
|
||||
await _sync_once(app)
|
||||
@@ -170,13 +170,14 @@ def _source_from_result(result: dict[str, Any], mapping: dict[str, Any], index:
|
||||
if result.get("kind") == "wiki_page":
|
||||
sections = list(result.get("matched_sections") or [])
|
||||
content = "\n\n".join(str(section.get("content") or "") for section in sections if section.get("content"))
|
||||
content = content or str(result.get("content") or result.get("content_markdown") or "")
|
||||
raw = {**result, "source": mapping.get("name") or "LLMWiki"}
|
||||
return {
|
||||
"index": index,
|
||||
"id": result.get("wiki_page_id") or result.get("id"),
|
||||
"title": result.get("title") or mapping.get("name") or f"Wiki 页面 {index}",
|
||||
"type": "llmwiki_wiki",
|
||||
"snippet": _truncate(content or str(result.get("summary") or ""), 360),
|
||||
"snippet": _truncate(content, 360),
|
||||
"content": content,
|
||||
"source": mapping.get("name") or "LLMWiki",
|
||||
"score": float(result.get("score") or 0),
|
||||
@@ -222,7 +223,9 @@ def _source_from_assistant_result(result: dict[str, Any], index: int) -> dict[st
|
||||
or result.get("summary")
|
||||
or ""
|
||||
)
|
||||
snippet = str(result.get("summary") or content)
|
||||
# The retrieval citation must expose the matched knowledge itself. A page
|
||||
# summary is display metadata and can omit the fact that answered the query.
|
||||
snippet = content
|
||||
raw = {
|
||||
**result,
|
||||
"source": "助手知识库",
|
||||
@@ -348,7 +351,10 @@ async def build_llmwiki_rag_context(
|
||||
mapping = mapping_by_id.get(str(result.get("knowledge_base_id") or ""))
|
||||
if mapping is None:
|
||||
continue
|
||||
sources.append(_source_from_result(result, mapping, len(sources) + 1))
|
||||
source = _source_from_result(result, mapping, len(sources) + 1)
|
||||
if not str(source.get("content") or "").strip():
|
||||
continue
|
||||
sources.append(source)
|
||||
if len(sources) >= _MAX_SOURCES:
|
||||
break
|
||||
|
||||
|
||||
@@ -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"}
|
||||
|
||||
|
||||
|
||||
@@ -255,7 +255,10 @@ def _schedule_local_wiki_sync(request: Request, mapping: dict[str, Any]) -> None
|
||||
if service is not None and config.enabled and config.auto_sync and mapping.get("wiki_index_enabled", True):
|
||||
from app.gateway.llmwiki_index_scheduler import spawn_local_wiki_index_job
|
||||
|
||||
spawn_local_wiki_index_job(request.app, service.sync_mapping(mapping, force=True))
|
||||
# Event-driven refresh is incremental. Content hash + embedding
|
||||
# fingerprint decide which pages need encoding; unchanged Wiki pages
|
||||
# must retain and reuse their existing vectors.
|
||||
spawn_local_wiki_index_job(request.app, service.sync_mapping(mapping, force=False))
|
||||
|
||||
|
||||
async def _actor(request: Request) -> tuple[str, bool]:
|
||||
@@ -3058,6 +3061,7 @@ async def create_knowledge_base(request: Request, body: KnowledgeBaseCreate) ->
|
||||
except Exception:
|
||||
logger.exception("Failed to compensate orphan WeKnora knowledge base")
|
||||
raise HTTPException(status_code=500, detail="Failed to register the knowledge base") from None
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
return _mapping_view(row, remote, actor_user_id=user_id, is_admin=is_admin)
|
||||
|
||||
|
||||
@@ -3345,12 +3349,14 @@ async def upload_document(request: Request, mapping_id: str, file: UploadFile =
|
||||
if not file.filename:
|
||||
raise HTTPException(status_code=422, detail="File name is required")
|
||||
try:
|
||||
return await _client_or_503().upload_document(
|
||||
result = await _client_or_503().upload_document(
|
||||
str(row["weknora_id"]),
|
||||
filename=file.filename,
|
||||
content=content,
|
||||
content_type=file.content_type or "application/octet-stream",
|
||||
)
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
return result
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
|
||||
@@ -3362,7 +3368,9 @@ async def import_document_url(request: Request, mapping_id: str, body: Knowledge
|
||||
if not url.lower().startswith(("http://", "https://")):
|
||||
raise HTTPException(status_code=422, detail="URL must start with http:// or https://")
|
||||
try:
|
||||
return await _client_or_503().import_document_url(str(row["weknora_id"]), url=url)
|
||||
result = await _client_or_503().import_document_url(str(row["weknora_id"]), url=url)
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
return result
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
|
||||
@@ -3371,11 +3379,13 @@ async def import_document_url(request: Request, mapping_id: str, body: Knowledge
|
||||
async def create_manual_document(request: Request, mapping_id: str, body: ManualKnowledgeCreate) -> dict[str, Any]:
|
||||
row, _, _ = await _authorized_mapping(request, mapping_id, write=True)
|
||||
try:
|
||||
return await _client_or_503().create_manual_document(
|
||||
result = await _client_or_503().create_manual_document(
|
||||
str(row["weknora_id"]),
|
||||
title=body.title.strip(),
|
||||
content=body.content,
|
||||
)
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
return result
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
|
||||
@@ -3387,6 +3397,7 @@ async def delete_document(request: Request, mapping_id: str, document_id: str) -
|
||||
try:
|
||||
await _verify_document(client, row, document_id)
|
||||
await client.delete_document(document_id)
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
|
||||
@@ -3397,7 +3408,9 @@ async def reprocess_document(request: Request, mapping_id: str, document_id: str
|
||||
client = _client_or_503()
|
||||
try:
|
||||
await _verify_document(client, row, document_id)
|
||||
return await client.reprocess_document(document_id)
|
||||
result = await client.reprocess_document(document_id)
|
||||
_schedule_local_wiki_sync(request, row)
|
||||
return result
|
||||
except WeKnoraError as exc:
|
||||
_raise_upstream(exc)
|
||||
|
||||
|
||||
@@ -2,15 +2,20 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import shutil
|
||||
from pathlib import Path, PurePosixPath
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, File, Form, HTTPException, Query, Request, UploadFile
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.deps import get_optional_user_from_request
|
||||
from app.gateway.llmwiki_index_scheduler import spawn_local_wiki_index_job
|
||||
from deerflow.config.runtime_paths import resolve_path, runtime_home
|
||||
from deerflow.config.system_settings import load_system_settings, save_system_settings
|
||||
from deerflow.integrations.weknora.local_index.retrieval import is_fully_vectorized
|
||||
from deerflow.integrations.weknora.local_index.search import WikiIndexError
|
||||
|
||||
@@ -37,6 +42,10 @@ class WikiIndexSettingsRequest(BaseModel):
|
||||
external_search_enabled: bool | None = None
|
||||
|
||||
|
||||
class LocalEncoderConfigRequest(BaseModel):
|
||||
model_path: str = Field(min_length=1, max_length=4096)
|
||||
|
||||
|
||||
async def _actor(request: Request) -> tuple[str, bool]:
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
@@ -55,6 +64,149 @@ def _services(request: Request):
|
||||
return config, search, index_store
|
||||
|
||||
|
||||
def _local_encoder(request: Request):
|
||||
config = request.app.state.config.llmwiki.local_wiki_index.embedding
|
||||
embedding = getattr(request.app.state, "llmwiki_embedding", None)
|
||||
if config.provider != "local":
|
||||
raise HTTPException(status_code=409, detail={"code": "EMBEDDING_NOT_LOCAL", "message": "当前 embedding provider 不是 local"})
|
||||
if embedding is None:
|
||||
raise HTTPException(status_code=503, detail={"code": "LOCAL_ENCODER_UNAVAILABLE", "message": "本地编码器未配置或未启用"})
|
||||
return embedding
|
||||
|
||||
|
||||
def _validated_encoder_path(raw_path: str) -> Path:
|
||||
path = resolve_path(raw_path.strip())
|
||||
if not path.is_dir():
|
||||
raise HTTPException(status_code=422, detail="编码器目录不存在")
|
||||
if not ((path / "onnx" / "model.onnx").is_file() or (path / "model.onnx").is_file()):
|
||||
raise HTTPException(status_code=422, detail="编码器目录缺少 onnx/model.onnx 或 model.onnx")
|
||||
return path
|
||||
|
||||
|
||||
def _safe_relative_upload_path(value: str) -> Path:
|
||||
normalized = str(value or "").replace("\\", "/").strip("/")
|
||||
candidate = PurePosixPath(normalized)
|
||||
if not normalized or candidate.is_absolute() or ".." in candidate.parts:
|
||||
raise HTTPException(status_code=422, detail=f"非法模型文件路径:{value}")
|
||||
if len(normalized) > 512:
|
||||
raise HTTPException(status_code=422, detail="模型文件路径过长")
|
||||
return Path(*candidate.parts)
|
||||
|
||||
|
||||
def _copy_encoder_uploads(files: list[UploadFile], paths: list[str], staging: Path) -> int:
|
||||
total = 0
|
||||
for upload, raw_path in zip(files, paths, strict=True):
|
||||
relative = _safe_relative_upload_path(raw_path)
|
||||
destination = staging / relative
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
upload.file.seek(0)
|
||||
with destination.open("wb") as output:
|
||||
while chunk := upload.file.read(4 * 1024 * 1024):
|
||||
output.write(chunk)
|
||||
total += len(chunk)
|
||||
return total
|
||||
|
||||
|
||||
def _activate_uploaded_encoder(staging: Path) -> Path:
|
||||
model_root = (runtime_home() / "models").resolve()
|
||||
model_root.mkdir(parents=True, exist_ok=True)
|
||||
active = (model_root / "bge-m3").resolve()
|
||||
if active.parent != model_root:
|
||||
raise RuntimeError("编码器目标目录越界")
|
||||
backup = (model_root / f".bge-m3-backup-{uuid4().hex}").resolve()
|
||||
if active.exists():
|
||||
active.replace(backup)
|
||||
try:
|
||||
staging.replace(active)
|
||||
except Exception:
|
||||
if backup.exists() and not active.exists():
|
||||
backup.replace(active)
|
||||
raise
|
||||
if backup.exists():
|
||||
shutil.rmtree(backup)
|
||||
return active
|
||||
|
||||
|
||||
async def _save_encoder_path(request: Request, path: Path) -> dict[str, object]:
|
||||
embedding = _local_encoder(request)
|
||||
current = load_system_settings()
|
||||
encoder_settings = current.local_wiki_encoder.model_copy(update={"model_path": str(path)})
|
||||
save_system_settings(current.model_copy(update={"local_wiki_encoder": encoder_settings}))
|
||||
await embedding.reset_local_encoder(model_path=str(path))
|
||||
return embedding.local_encoder_status()
|
||||
|
||||
|
||||
@router.get("/local-encoder/status")
|
||||
async def local_encoder_status(request: Request) -> dict[str, object]:
|
||||
_, is_admin = await _actor(request)
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="Administrator access required")
|
||||
return _local_encoder(request).local_encoder_status()
|
||||
|
||||
|
||||
@router.post("/local-encoder/start", status_code=202)
|
||||
async def start_local_encoder(request: Request) -> dict[str, object]:
|
||||
_, is_admin = await _actor(request)
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="Administrator access required")
|
||||
try:
|
||||
return _local_encoder(request).start_local_encoder()
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=409, detail={"code": "LOCAL_ENCODER_START_REJECTED", "message": str(exc)}) from None
|
||||
|
||||
|
||||
@router.put("/local-encoder/config")
|
||||
async def configure_local_encoder(request: Request, body: LocalEncoderConfigRequest) -> dict[str, object]:
|
||||
_, is_admin = await _actor(request)
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="Administrator access required")
|
||||
return await _save_encoder_path(request, _validated_encoder_path(body.model_path))
|
||||
|
||||
|
||||
@router.post("/local-encoder/upload")
|
||||
async def upload_local_encoder(
|
||||
request: Request,
|
||||
files: list[UploadFile] = File(...),
|
||||
paths: list[str] = Form(...),
|
||||
) -> dict[str, object]:
|
||||
"""Stream an offline model directory into runtime storage and activate it.
|
||||
|
||||
Starlette spools multipart files to disk, and the copy loop is chunked, so
|
||||
multi-gigabyte ONNX models do not need to be loaded into Gateway memory.
|
||||
"""
|
||||
|
||||
_, is_admin = await _actor(request)
|
||||
if not is_admin:
|
||||
raise HTTPException(status_code=403, detail="Administrator access required")
|
||||
if not files or len(files) != len(paths):
|
||||
raise HTTPException(status_code=422, detail="模型文件与相对路径不匹配")
|
||||
if len(files) > 500:
|
||||
raise HTTPException(status_code=422, detail="模型目录文件数不能超过 500")
|
||||
|
||||
staging_root = (runtime_home() / "model-uploads").resolve()
|
||||
staging = (staging_root / uuid4().hex).resolve()
|
||||
if staging.parent != staging_root:
|
||||
raise HTTPException(status_code=500, detail="模型暂存目录越界")
|
||||
staging.mkdir(parents=True, exist_ok=False)
|
||||
try:
|
||||
uploaded_bytes = await asyncio.to_thread(_copy_encoder_uploads, files, paths, staging)
|
||||
_validated_encoder_path(str(staging))
|
||||
await _local_encoder(request).reset_local_encoder()
|
||||
active = await asyncio.to_thread(_activate_uploaded_encoder, staging)
|
||||
status = await _save_encoder_path(request, active)
|
||||
return {**status, "uploaded_files": len(files), "uploaded_bytes": uploaded_bytes}
|
||||
except HTTPException:
|
||||
raise
|
||||
except OSError as exc:
|
||||
logger.exception("Failed to store uploaded local encoder")
|
||||
raise HTTPException(status_code=507, detail=f"编码器文件保存失败:{exc}") from None
|
||||
finally:
|
||||
for upload in files:
|
||||
await upload.close()
|
||||
if staging.exists():
|
||||
await asyncio.to_thread(shutil.rmtree, staging, True)
|
||||
|
||||
|
||||
async def _authorized_mappings(request: Request, mapping_ids: list[str]) -> tuple[list[dict[str, Any]], str, bool]:
|
||||
user_id, is_admin = await _actor(request)
|
||||
mapping_store = request.app.state.llmwiki_store
|
||||
|
||||
@@ -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