feat(knowledge): support standalone file ingestion and configurable wiki entry
Use neutral knowledge-service labels and sanitize user-facing task errors.
This commit is contained in:
1 parent
f9b6d7e322
commit
3ca5528640
24 files changed
+549
-54
No files matched your search
@@ -152,6 +152,18 @@ def _list_export_manifests(mapping_id: str, *, limit: int) -> list[dict[str, Any
|
||||
|
||||
def _delete_base_package_files(base_id: str, import_job_ids: list[str]) -> None:
|
||||
"""Delete package artifacts owned by one assistant base only."""
|
||||
import shutil
|
||||
|
||||
from app.gateway.assistant_file_ingest import file_job_dir
|
||||
|
||||
for job_id in import_job_ids:
|
||||
# UUID validation confines cleanup to exactly one job directory.
|
||||
try:
|
||||
directory = file_job_dir(job_id)
|
||||
except ValueError:
|
||||
continue
|
||||
if directory.is_dir() and not directory.is_symlink():
|
||||
shutil.rmtree(directory)
|
||||
root = _package_root()
|
||||
if not root.is_dir():
|
||||
return
|
||||
@@ -448,10 +460,71 @@ async def import_skills(request: Request, _body: SkillImportRequest) -> dict[str
|
||||
raise HTTPException(status_code=410, detail="不支持技能直接归纳到助手知识库,请先归纳到 WeKnora 普通知识库后再导入助手知识库")
|
||||
|
||||
|
||||
@router.post("/imports/files", status_code=410)
|
||||
async def import_files(request: Request) -> dict[str, Any]:
|
||||
@router.post("/imports/files", status_code=202)
|
||||
async def import_files(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
assistant_base_id: str | None = Form(default=None),
|
||||
) -> dict[str, Any]:
|
||||
import hashlib
|
||||
|
||||
from app.gateway.assistant_file_ingest import ALLOWED_EXTENSIONS, MAX_FILE_BYTES, file_job_dir, save_json, start_file_job
|
||||
|
||||
actor = await _actor(request, admin=True)
|
||||
filename = (file.filename or "document").replace("\\", "/").rsplit("/", 1)[-1][:240]
|
||||
extension = Path(filename).suffix.lower()
|
||||
if extension not in ALLOWED_EXTENSIONS:
|
||||
await file.close()
|
||||
raise HTTPException(status_code=415, detail="支持 Markdown、TXT、CSV、PDF、Word、Excel、PowerPoint 文件")
|
||||
job = await _store(request).create_queued_import_job(
|
||||
source_type="file", source_key=f"file-upload:{uuid4()}", source_name=filename,
|
||||
trigger="local_file_upload", created_by=actor, base_id=assistant_base_id,
|
||||
metadata={"type": "file", "filename": filename}, phase="uploading",
|
||||
)
|
||||
root = file_job_dir(job["id"])
|
||||
await asyncio.to_thread(root.mkdir, parents=True, exist_ok=True)
|
||||
path = root / f"original{extension}"
|
||||
size, digest = 0, hashlib.sha256()
|
||||
try:
|
||||
with path.open("wb") as handle:
|
||||
while chunk := await file.read(1024 * 1024):
|
||||
size += len(chunk)
|
||||
if size > MAX_FILE_BYTES:
|
||||
raise HTTPException(status_code=413, detail="单个文件最大 100 MB,请拆分后上传")
|
||||
digest.update(chunk)
|
||||
await asyncio.to_thread(handle.write, chunk)
|
||||
if not size:
|
||||
raise HTTPException(status_code=422, detail="上传文件为空")
|
||||
await asyncio.to_thread(save_json, root / "manifest.json", {
|
||||
"filename": filename, "stored_name": path.name, "digest": digest.hexdigest(), "actor": actor,
|
||||
})
|
||||
job = await _store(request).update_import_job(job["id"], phase="queued", counts={"uploaded_bytes": size})
|
||||
start_file_job(request.app, job["id"])
|
||||
return job
|
||||
except Exception as exc:
|
||||
await _store(request).update_import_job(job["id"], status="failed", phase="upload_failed", error=str(getattr(exc, "detail", exc)), completed=True)
|
||||
await asyncio.to_thread(path.unlink, missing_ok=True)
|
||||
raise
|
||||
finally:
|
||||
await file.close()
|
||||
|
||||
|
||||
@router.post("/import-jobs/{job_id}/retry-file", status_code=202)
|
||||
async def retry_file_import(request: Request, job_id: str) -> dict[str, Any]:
|
||||
from app.gateway.assistant_file_ingest import file_job_dir, retry_file_job
|
||||
|
||||
await _actor(request, admin=True)
|
||||
raise HTTPException(status_code=410, detail="助手知识库不支持导入任意本地文件,请从 WeKnora 普通知识库导出后导入")
|
||||
store = _store(request)
|
||||
job = await store.get_job(job_id)
|
||||
if not job or job.get("trigger") != "local_file_upload":
|
||||
raise HTTPException(status_code=404, detail="文件处理任务不存在")
|
||||
if job["status"] == "completed":
|
||||
raise HTTPException(status_code=409, detail="文件已处理完成")
|
||||
if not (file_job_dir(job_id) / "manifest.json").exists():
|
||||
raise HTTPException(status_code=409, detail="文件上传未完成,请重新上传")
|
||||
if not await retry_file_job(request.app, job_id):
|
||||
raise HTTPException(status_code=409, detail="文件仍在处理中")
|
||||
return await store.get_job(job_id)
|
||||
|
||||
|
||||
@router.post("/imports/weknora-package", status_code=202)
|
||||
|
||||
Reference in new issue
Block a user