igeng
This commit is contained in:
+260
-58
@@ -5,16 +5,29 @@ import asyncio
|
||||
import json
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from typing import Any, Literal
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
import httpx
|
||||
from fastapi import Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from pydantic import BaseModel, Field, StrictBool, field_validator, model_validator
|
||||
|
||||
from archive_store import tenant_scope
|
||||
from archive_store import TenantScope, tenant_scope
|
||||
from knowledge_assignments import ASSIGNOR, MAINTAINER, REVIEWER, KnowledgeAssignments
|
||||
from knowledge_retriever import KnowledgeRetriever, VectorIndex
|
||||
from knowledge_store import KnowledgeStore
|
||||
from knowledge_review_policy import POLICY_PERMISSIONS, allow_self_review, save_policy
|
||||
from knowledge_store import KnowledgeContentType, KnowledgeStore
|
||||
from knowledge_access import (
|
||||
TASK_ARCHIVE, TASK_CONTROL, TASK_CREATE, TASKS_READ, TASK_WATCH,
|
||||
KnowledgeAccessDenied, current_access,
|
||||
)
|
||||
|
||||
|
||||
OVERVIEW_PERMISSIONS = ('knowledge:read', *ASSIGNOR, TASKS_READ, 'knowledge:search')
|
||||
|
||||
|
||||
class SelfReviewPolicyBody(BaseModel):
|
||||
allow_self_review: StrictBool
|
||||
|
||||
|
||||
class JobBody(BaseModel):
|
||||
@@ -96,6 +109,13 @@ class EditBody(BaseModel):
|
||||
category: str = Field(default="", max_length=100)
|
||||
kind: Literal["qa", "procedure", "case"] = "qa"
|
||||
valid_until: str = ""
|
||||
content_type: KnowledgeContentType | None = None
|
||||
|
||||
def edit_values(self, *, exclude: set[str] | None = None) -> dict:
|
||||
values = self.model_dump(exclude=exclude)
|
||||
if "content_type" not in self.model_fields_set:
|
||||
values.pop("content_type", None)
|
||||
return values
|
||||
|
||||
|
||||
class ActionBody(BaseModel):
|
||||
@@ -104,6 +124,39 @@ class ActionBody(BaseModel):
|
||||
restore_revision: int = Field(default=0, ge=0)
|
||||
|
||||
|
||||
class ApprovePublishBody(BaseModel):
|
||||
revision: int = Field(ge=1)
|
||||
confirmed: StrictBool = False
|
||||
|
||||
|
||||
class ReviewSelectionItem(BaseModel):
|
||||
id: str = Field(min_length=1, max_length=128)
|
||||
revision: int = Field(strict=True, ge=1)
|
||||
|
||||
|
||||
class ReviewSelection(BaseModel):
|
||||
items: list[ReviewSelectionItem] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator('items')
|
||||
@classmethod
|
||||
def unique_items(cls, items):
|
||||
if len({item.id for item in items}) != len(items):
|
||||
raise ValueError('所选知识不能重复')
|
||||
return items
|
||||
|
||||
|
||||
class ReviewAssignmentBody(ReviewSelection):
|
||||
assignee_id: Annotated[int, Field(strict=True, ge=1, le=2**63 - 1)] | None
|
||||
|
||||
|
||||
class AssignedEditBody(EditBody):
|
||||
assignment_id: str = Field(min_length=1, max_length=128)
|
||||
|
||||
|
||||
class AssignedActionBody(ApprovePublishBody):
|
||||
assignment_id: str = Field(min_length=1, max_length=128)
|
||||
|
||||
|
||||
class SearchBody(BaseModel):
|
||||
query: str = Field(min_length=2, max_length=2000)
|
||||
|
||||
@@ -115,6 +168,14 @@ class SearchBody(BaseModel):
|
||||
|
||||
class ReviewConfirmBody(BaseModel):
|
||||
confirmed: bool = False
|
||||
raw_confirmed: Any = Field(default=False, exclude=True)
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def capture_confirmation(cls, values):
|
||||
if isinstance(values, dict):
|
||||
return {**values, 'raw_confirmed': values.get('confirmed', False)}
|
||||
return values
|
||||
|
||||
|
||||
class SettingsBody(BaseModel):
|
||||
@@ -125,6 +186,8 @@ class SettingsBody(BaseModel):
|
||||
def errors():
|
||||
try:
|
||||
yield
|
||||
except KnowledgeAccessDenied as exc:
|
||||
raise HTTPException(403, str(exc)) from exc
|
||||
except KeyError as exc:
|
||||
raise HTTPException(404, str(exc).strip("'")) from exc
|
||||
except ValueError as exc:
|
||||
@@ -146,10 +209,35 @@ ITEM_ACTIONS = {
|
||||
|
||||
def register_knowledge_routes(app, database, require, client_ip, resolve_admin_tenant, current_desktop,
|
||||
require_any=None):
|
||||
existing_resolve_admin_tenant = resolve_admin_tenant
|
||||
|
||||
def resolve_admin_tenant(principal, account_id):
|
||||
# Keep archive authorization unchanged. Knowledge applies the current
|
||||
# account restriction to its own routes, including previews and labels.
|
||||
with database.connect() as db:
|
||||
access = current_access(db, principal.id)
|
||||
account = (db.execute('SELECT tenant_id FROM desktop_accounts WHERE id=?', (int(account_id),)).fetchone()
|
||||
if access.own and account_id and account_id != -1 else None)
|
||||
if not access.own:
|
||||
return existing_resolve_admin_tenant(principal, account_id)
|
||||
allowed = access.tenants or frozenset()
|
||||
if account_id == -1:
|
||||
return TenantScope(tuple(sorted(allowed)), False)
|
||||
if account_id and account is None:
|
||||
raise HTTPException(404, '客户端账号不存在')
|
||||
tenant = account['tenant_id'] if account is not None else 'default'
|
||||
if tenant not in allowed:
|
||||
raise HTTPException(403, '客户端账号不在你的知识授权范围内')
|
||||
return tenant
|
||||
|
||||
store = KnowledgeStore(database)
|
||||
store.initialize()
|
||||
app.state.knowledge_store = store
|
||||
|
||||
@app.exception_handler(KnowledgeAccessDenied)
|
||||
async def access_changed(_request, exc):
|
||||
return JSONResponse(status_code=403, content={'detail': str(exc)})
|
||||
|
||||
def single(principal, account_id):
|
||||
scope = resolve_admin_tenant(principal, account_id)
|
||||
if not isinstance(scope, str) or scope == "*":
|
||||
@@ -169,8 +257,60 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
raise HTTPException(404, "知识不存在或不在你的授权范围内")
|
||||
return row[0]
|
||||
|
||||
from knowledge_review import KnowledgeReviewStore
|
||||
from knowledge_review import PUBLISH_PERMISSIONS, REVIEW_PERMISSIONS, KnowledgeReviewStore
|
||||
reviews = KnowledgeReviewStore(store)
|
||||
assignments = KnowledgeAssignments(store)
|
||||
|
||||
@app.get('/api/v2/knowledge/review-policy')
|
||||
def review_policy(principal=Depends(require('knowledge:read'))):
|
||||
with database.connect() as db:
|
||||
return {'allow_self_review': allow_self_review(db)}
|
||||
|
||||
@app.put('/api/v2/knowledge/review-policy')
|
||||
def update_review_policy(body: SelfReviewPolicyBody, request: Request,
|
||||
principal=Depends(require(*POLICY_PERMISSIONS))):
|
||||
with errors():
|
||||
return save_policy(database, principal.id, client_ip(request), body.allow_self_review)
|
||||
|
||||
@app.post('/api/v2/knowledge/review-assignees')
|
||||
def review_assignees(body: ReviewSelection, account_id: int | None = None,
|
||||
principal=Depends(require(*ASSIGNOR))):
|
||||
with errors():
|
||||
return assignments.assignees(resolve_admin_tenant(principal, account_id), body.model_dump()['items'], principal.id)
|
||||
|
||||
@app.put('/api/v2/knowledge/review-assignments')
|
||||
def review_assignments(body: ReviewAssignmentBody, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require(*ASSIGNOR))):
|
||||
with errors():
|
||||
return assignments.assign(resolve_admin_tenant(principal, account_id), body.model_dump()['items'],
|
||||
body.assignee_id, principal.id, client_ip(request))
|
||||
|
||||
@app.get('/api/v2/knowledge/my-reviews')
|
||||
def my_reviews(page: int = Query(default=1, ge=1), page_size: int = Query(default=30, ge=1, le=100),
|
||||
principal=Depends(require(*REVIEWER))):
|
||||
with errors():
|
||||
return assignments.inbox(principal.id, page, page_size)
|
||||
|
||||
@app.get('/api/v2/knowledge/my-reviews/{item_id}')
|
||||
def my_review(item_id: str, principal=Depends(require(*REVIEWER))):
|
||||
with errors():
|
||||
return assignments.detail(principal.id, item_id)
|
||||
|
||||
@app.put('/api/v2/knowledge/my-reviews/{item_id}')
|
||||
def edit_my_review(item_id: str, body: AssignedEditBody, request: Request,
|
||||
principal=Depends(require(*REVIEWER, 'knowledge:write'))):
|
||||
with errors():
|
||||
return assignments.save(principal.id, item_id, body.edit_values(exclude={'assignment_id'}),
|
||||
body.assignment_id, client_ip(request))
|
||||
|
||||
@app.post('/api/v2/knowledge/my-reviews/{item_id}/{action}')
|
||||
def act_my_review(item_id: str, action: Literal['approve', 'reject', 'approve_publish'], body: AssignedActionBody, request: Request,
|
||||
principal=Depends(require(*REVIEWER))):
|
||||
if action == 'approve_publish' and 'knowledge:publish' not in principal.permissions:
|
||||
raise HTTPException(403, '缺少权限:knowledge:publish')
|
||||
with errors():
|
||||
return assignments.transition(principal.id, item_id, body.revision, body.assignment_id,
|
||||
action, client_ip(request), body.confirmed)
|
||||
|
||||
def run_tenants(principal, account_ids):
|
||||
"""整理范围 → 租户列表。不选账号 = 授权范围内全部有聊天归档的账号。"""
|
||||
@@ -185,13 +325,13 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
|
||||
# ── 整理批次:一次整理多个账号、全部聊天 ─────────────────────────────────
|
||||
@app.post('/api/v2/knowledge/runs/preview')
|
||||
def preview_run(body: RunScopeBody, principal=Depends(require('knowledge:run', 'im:content:read'))):
|
||||
def preview_run(body: RunScopeBody, principal=Depends(require(*TASK_CREATE))):
|
||||
with errors():
|
||||
tenants, label = run_tenants(principal, body.account_ids)
|
||||
return {**store.preview_run(tenants, body.model_dump()), 'scope_label': label}
|
||||
return {**store.preview_run(tenants, body.model_dump(), actor=principal.id), 'scope_label': label}
|
||||
|
||||
@app.post('/api/v2/knowledge/runs')
|
||||
def create_run(body: RunBody, request: Request, principal=Depends(require('knowledge:run', 'im:content:read'))):
|
||||
def create_run(body: RunBody, request: Request, principal=Depends(require(*TASK_CREATE))):
|
||||
if body.auto_watch and 'knowledge:watch' not in principal.permissions:
|
||||
raise HTTPException(403, '缺少权限:knowledge:watch') # 「新聊天也自动整理」是另一个按钮
|
||||
with errors():
|
||||
@@ -200,25 +340,31 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
return store.create_run(tenants, values, principal.id, client_ip(request), scope_label=label)
|
||||
|
||||
@app.get('/api/v2/knowledge/runs')
|
||||
def runs(principal=Depends(require('knowledge:read'))):
|
||||
def runs(principal=Depends(require(TASKS_READ))):
|
||||
scope = resolve_admin_tenant(principal, -1)
|
||||
return {**store.runs(scope), 'auto': store.auto_summary(scope)}
|
||||
return {**store.runs(scope, actor=principal.id), 'auto': store.auto_summary(scope, actor=principal.id)}
|
||||
|
||||
@app.put('/api/v2/knowledge/runs/{run_id}/config')
|
||||
def configure_run(run_id: str, body: RunConfigBody, request: Request,
|
||||
principal=Depends(require('knowledge:run:control', 'im:content:read'))):
|
||||
principal=Depends(require(*TASK_CONTROL))):
|
||||
with errors():
|
||||
return store.configure_run(resolve_admin_tenant(principal, -1), run_id, body.model_dump(),
|
||||
principal.id, client_ip(request))
|
||||
|
||||
@app.post('/api/v2/knowledge/runs/{run_id}/{action}')
|
||||
def run_action(run_id: str, action: str, request: Request,
|
||||
principal=Depends(require('knowledge:run:control', 'im:content:read'))):
|
||||
principal=Depends(require(*TASK_CONTROL))):
|
||||
with errors():
|
||||
return store.run_action(resolve_admin_tenant(principal, -1), run_id, action, principal.id, client_ip(request))
|
||||
|
||||
@app.get('/api/v2/knowledge/config')
|
||||
def get_config(principal=Depends(require('knowledge:read'))):
|
||||
def get_config(principal=Depends(require_any('knowledge:read', TASKS_READ))):
|
||||
with database.connect() as db:
|
||||
access = current_access(db, principal.id)
|
||||
if ('knowledge:read' not in access.permissions
|
||||
and not {TASKS_READ, 'knowledge:settings'} <= access.permissions
|
||||
and not set(TASK_CREATE) <= access.permissions):
|
||||
raise KnowledgeAccessDenied('权限已变化或账号已停用,请刷新后重试')
|
||||
return store.config()
|
||||
|
||||
@app.put('/api/v2/knowledge/config')
|
||||
@@ -259,43 +405,68 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
'draft': {key: draft[key] for key in ('title', 'question', 'answer', 'conditions', 'category', 'kind')}}
|
||||
|
||||
@app.post('/api/v2/knowledge/review-tasks/preview')
|
||||
def preview_reviews(request: Request, account_id: int = -1,
|
||||
principal=Depends(require('knowledge:read', 'knowledge:review:auto', 'im:content:read'))):
|
||||
def preview_reviews(request: Request, account_id: int = -1, mode: Literal['approve', 'approve_publish'] = 'approve',
|
||||
principal=Depends(require(*REVIEW_PERMISSIONS))):
|
||||
with errors():
|
||||
return reviews.preview(resolve_admin_tenant(principal, account_id), account_id, principal.id, client_ip(request))
|
||||
if account_id != -1:
|
||||
raise ValueError('批量审核的范围为指派给本人的草稿')
|
||||
if mode == 'approve_publish':
|
||||
with database.connect() as db:
|
||||
current_access(db, principal.id, PUBLISH_PERMISSIONS)
|
||||
return reviews.preview('*', account_id, principal.id, client_ip(request), mode)
|
||||
|
||||
@app.get('/api/v2/knowledge/review-tasks')
|
||||
def review_tasks(principal=Depends(require('knowledge:read', 'knowledge:review:auto', 'im:content:read'))):
|
||||
def review_tasks(principal=Depends(require(*REVIEW_PERMISSIONS))):
|
||||
return {'items': reviews.jobs(principal.id)}
|
||||
|
||||
@app.get('/api/v2/knowledge/review-tasks/{job_id}/results')
|
||||
def review_results(job_id: str, result: str = 'skipped', page: int = Query(default=1, ge=1),
|
||||
page_size: int = Query(default=30, ge=1, le=100),
|
||||
principal=Depends(require('knowledge:read', 'knowledge:review:auto', 'im:content:read'))):
|
||||
principal=Depends(require(*REVIEW_PERMISSIONS))):
|
||||
with errors():
|
||||
return reviews.results(job_id, principal.id, resolve_admin_tenant(principal, -1), result, page, page_size)
|
||||
return reviews.results(job_id, principal.id, '*', result, page, page_size)
|
||||
|
||||
@app.get('/api/v2/knowledge/review-tasks/{job_id}')
|
||||
def review_task(job_id: str, principal=Depends(require('knowledge:read', 'knowledge:review:auto', 'im:content:read'))):
|
||||
def review_task(job_id: str, principal=Depends(require(*REVIEW_PERMISSIONS))):
|
||||
with errors():
|
||||
return reviews.get(job_id, principal.id)
|
||||
|
||||
@app.post('/api/v2/knowledge/review-tasks/{job_id}/{action}')
|
||||
def review_action(job_id: str, action: str, body: ReviewConfirmBody, request: Request,
|
||||
principal=Depends(require('knowledge:read', 'knowledge:review:auto', 'im:content:read'))):
|
||||
principal=Depends(require(*REVIEW_PERMISSIONS))):
|
||||
with errors():
|
||||
return reviews.action(job_id, action, principal.id, client_ip(request), body.confirmed)
|
||||
if action in {'start', 'resume'} and reviews.get(job_id, principal.id)['mode'] == 'approve_publish':
|
||||
with database.connect() as db:
|
||||
current_access(db, principal.id, PUBLISH_PERMISSIONS)
|
||||
return reviews.action(job_id, action, principal.id, client_ip(request), body.confirmed,
|
||||
raw_confirmed=body.raw_confirmed)
|
||||
|
||||
@app.get("/api/v2/knowledge/overview")
|
||||
def overview(account_id: int | None = None, principal=Depends(require("knowledge:read"))):
|
||||
def overview(account_id: int | None = None, principal=Depends(require_any(*OVERVIEW_PERMISSIONS))):
|
||||
scope = resolve_admin_tenant(principal, account_id)
|
||||
ts = tenant_scope(scope)
|
||||
with database.connect() as db:
|
||||
db.execute('BEGIN')
|
||||
access = current_access(db, principal.id)
|
||||
if not access.permissions.intersection(OVERVIEW_PERMISSIONS):
|
||||
raise KnowledgeAccessDenied('权限已变化或账号已停用,请刷新后重试')
|
||||
knowledge_reader = 'knowledge:read' in access.permissions
|
||||
search_reader = 'knowledge:search' in access.permissions
|
||||
assignor = set(ASSIGNOR) <= access.permissions
|
||||
archive_reader = set(TASK_ARCHIVE) <= access.permissions
|
||||
tenant_clause, tenant_args = access.tenant_sql('archive_source_account')
|
||||
accounts = [dict(r) for r in db.execute(
|
||||
f"SELECT id,tenant_id,display_name,external_account_id FROM archive_source_account WHERE {ts.clause()}", ts.params)]
|
||||
return {"counts": store.stats(scope), "source_accounts": accounts, "watches": store.watches(scope),
|
||||
"settings": store.settings(scope) if isinstance(scope, str) and scope != "*" else None,
|
||||
"semantic_configured": VectorIndex().configured}
|
||||
f"SELECT id,tenant_id,display_name,external_account_id FROM archive_source_account WHERE {ts.clause()} AND {tenant_clause}",
|
||||
[*ts.params, *tenant_args])] if archive_reader else []
|
||||
clause, args = access.tenant_sql('d')
|
||||
account_ids = [r[0] for r in db.execute(
|
||||
f"SELECT d.id FROM desktop_accounts d WHERE d.status='active' AND {clause} ORDER BY d.id", args)] if access.own else []
|
||||
return {"knowledge_scope": {"mode": "zyt_account" if access.own else "existing", "account_ids": account_ids},
|
||||
"counts": store.stats(scope, actor=principal.id) if assignor else {},
|
||||
"source_accounts": accounts,
|
||||
"watches": store.watches(scope, actor=principal.id) if archive_reader else [],
|
||||
"settings": store.settings(scope) if knowledge_reader and isinstance(scope, str) and scope != "*" else None,
|
||||
"semantic_configured": (knowledge_reader or search_reader) and VectorIndex().configured}
|
||||
|
||||
@app.put("/api/v2/knowledge/settings")
|
||||
def settings(body: SettingsBody, request: Request, account_id: int | None = None,
|
||||
@@ -305,7 +476,7 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
|
||||
@app.put("/api/v2/knowledge/watch/{source_id}")
|
||||
def watch(source_id: str, body: SettingsBody, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:watch", "im:content:read"))):
|
||||
principal=Depends(require(*TASK_WATCH))):
|
||||
with errors():
|
||||
return store.set_watch(single(principal, account_id), source_id, body.enabled, principal.id, client_ip(request))
|
||||
|
||||
@@ -318,23 +489,23 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
|
||||
@app.put('/api/v2/knowledge/jobs/{job_id}/config')
|
||||
def job_config(job_id: str, body: JobConfigBody, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require('knowledge:run:control', 'im:content:read'))):
|
||||
principal=Depends(require(*TASK_CONTROL))):
|
||||
with errors():
|
||||
return store.configure_job(single(principal, account_id), job_id, body.model_dump(), principal.id, client_ip(request))
|
||||
|
||||
@app.get("/api/v2/knowledge/jobs")
|
||||
def jobs(account_id: int | None = None, include_auto: bool = False, principal=Depends(require("knowledge:read"))):
|
||||
return {"items": store.jobs(resolve_admin_tenant(principal, account_id), include_auto=include_auto)}
|
||||
def jobs(account_id: int | None = None, include_auto: bool = False, principal=Depends(require(TASKS_READ))):
|
||||
return {"items": store.jobs(resolve_admin_tenant(principal, account_id), include_auto=include_auto, actor=principal.id)}
|
||||
|
||||
@app.post("/api/v2/knowledge/jobs/preview")
|
||||
def preview_job(body: JobBody, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:run", "im:content:read"))):
|
||||
principal=Depends(require(*TASK_CREATE))):
|
||||
with errors():
|
||||
return store.preview_job(single(principal, account_id), body.model_dump())
|
||||
return store.preview_job(single(principal, account_id), body.model_dump(), actor=principal.id)
|
||||
|
||||
@app.post("/api/v2/knowledge/jobs")
|
||||
def create_job(body: JobBody, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:run", "im:content:read"))):
|
||||
principal=Depends(require(*TASK_CREATE))):
|
||||
if body.auto_watch and 'knowledge:watch' not in principal.permissions:
|
||||
raise HTTPException(403, '缺少权限:knowledge:watch')
|
||||
with errors():
|
||||
@@ -342,16 +513,16 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
|
||||
@app.post("/api/v2/knowledge/jobs/{job_id}/{action}")
|
||||
def job_action(job_id: str, action: str, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:run:control", "im:content:read"))):
|
||||
principal=Depends(require(*TASK_CONTROL))):
|
||||
with errors():
|
||||
return store.job_action(single(principal, account_id), job_id, action, principal.id, client_ip(request))
|
||||
|
||||
@app.get("/api/v2/knowledge/items")
|
||||
def items(account_id: int | None = None, status: str = "", query: str = "",
|
||||
page: int = Query(default=1, ge=1), page_size: int = Query(default=30, ge=1, le=100),
|
||||
principal=Depends(require("knowledge:read"))):
|
||||
result = store.list_items(resolve_admin_tenant(principal, account_id), status, query, page, page_size)
|
||||
# 「全部账号」下列表混着多个账号的知识:每条带上所属账号,点开就能直接看、直接审
|
||||
page: int = Query(default=1, ge=1), page_size: int = Query(default=30, ge=1, le=2000),
|
||||
principal=Depends(require(*ASSIGNOR))):
|
||||
result = store.list_items(resolve_admin_tenant(principal, account_id), status, query, page, page_size, actor=principal.id)
|
||||
# 指派管理需要知道草稿归属,审核人从独立收件箱读取条目。
|
||||
with database.connect() as db:
|
||||
labels = store._tenant_labels(db, {item["tenant_id"] for item in result["items"]})
|
||||
for item in result["items"]:
|
||||
@@ -359,19 +530,19 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
return result
|
||||
|
||||
@app.get("/api/v2/knowledge/items/{item_id}")
|
||||
def detail(item_id: str, account_id: int | None = None, principal=Depends(require("knowledge:read"))):
|
||||
def detail(item_id: str, account_id: int | None = None, principal=Depends(require(*MAINTAINER))):
|
||||
with errors():
|
||||
tenant = item_tenant(principal, account_id, item_id)
|
||||
item = store.detail(tenant, item_id, sources="im:content:read" in principal.permissions)
|
||||
item = store.detail(tenant, item_id, actor=principal.id)
|
||||
with database.connect() as db:
|
||||
label = store._tenant_labels(db, [tenant])[tenant]
|
||||
return {**item, "account_id": label["account_id"], "account_label": label["label"]}
|
||||
|
||||
@app.put("/api/v2/knowledge/items/{item_id}")
|
||||
def edit(item_id: str, body: EditBody, request: Request, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:write", "im:content:read"))):
|
||||
principal=Depends(require(*MAINTAINER, "knowledge:write"))):
|
||||
with errors():
|
||||
return store.save(item_tenant(principal, account_id, item_id), item_id, body.model_dump(), principal.id,
|
||||
return store.save(item_tenant(principal, account_id, item_id), item_id, body.edit_values(), principal.id,
|
||||
client_ip(request))
|
||||
|
||||
for code in ITEM_ACTIONS.values():
|
||||
@@ -379,13 +550,14 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
|
||||
@app.post("/api/v2/knowledge/items/{item_id}/{action}")
|
||||
def transition(item_id: str, action: str, body: ActionBody, request: Request,
|
||||
account_id: int | None = None, principal=Depends(require("knowledge:read"))):
|
||||
account_id: int | None = None, principal=Depends(require(*MAINTAINER))):
|
||||
if action in {'approve', 'reject', 'approve_publish'}:
|
||||
raise HTTPException(403, '请在待我审核处理指派给本人的草稿')
|
||||
permission = ITEM_ACTIONS.get(action)
|
||||
if permission is None:
|
||||
raise HTTPException(400, "未知操作")
|
||||
missing = [code for code in (permission, "im:content:read") if code not in principal.permissions]
|
||||
if missing:
|
||||
raise HTTPException(403, f"缺少权限:{'、'.join(missing)}")
|
||||
if permission not in principal.permissions:
|
||||
raise HTTPException(403, f"缺少权限:{permission}")
|
||||
tenant = item_tenant(principal, account_id, item_id)
|
||||
with errors():
|
||||
if action == 'delete':
|
||||
@@ -396,47 +568,77 @@ def register_knowledge_routes(app, database, require, client_ip, resolve_admin_t
|
||||
return store.publish(tenant, item_id, body.revision, principal.id, client_ip(request))
|
||||
if action == "restore":
|
||||
with database.connect() as db:
|
||||
store._item(db, tenant, item_id)
|
||||
db.execute('BEGIN')
|
||||
store._management_item(db, tenant, item_id, principal.id, extra_permissions=('knowledge:restore',))
|
||||
previous = db.execute("SELECT snapshot_json FROM knowledge_revision WHERE item_id=? AND revision=? "
|
||||
"ORDER BY created_at DESC LIMIT 1", (item_id, body.restore_revision)).fetchone()
|
||||
if previous is None:
|
||||
raise KeyError("历史版本不存在")
|
||||
values = json.loads(previous[0])
|
||||
# Snapshots from before the migration represent an unclassified version.
|
||||
values.setdefault("content_type", None)
|
||||
values["revision"] = body.revision
|
||||
return store.save(tenant, item_id, values, principal.id, client_ip(request))
|
||||
return store.save(tenant, item_id, values, principal.id, client_ip(request), permission='knowledge:restore')
|
||||
return store.transition(tenant, item_id, body.revision, action, principal.id, client_ip(request), body.confirmed)
|
||||
|
||||
@app.post("/api/v2/knowledge/search")
|
||||
def search(body: SearchBody, account_id: int | None = None, principal=Depends(require("knowledge:search"))):
|
||||
return KnowledgeRetriever(store).search(single(principal, account_id), body.query)
|
||||
with errors():
|
||||
return KnowledgeRetriever(store).search(single(principal, account_id), body.query, management_actor=principal.id)
|
||||
|
||||
@app.get("/api/v2/knowledge/retrievals")
|
||||
def retrievals(account_id: int | None = None, principal=Depends(require("knowledge:read", "stats:read"))):
|
||||
def retrievals(account_id: int | None = None, principal=Depends(require(*MAINTAINER, "stats:read"))):
|
||||
scope = tenant_scope(resolve_admin_tenant(principal, account_id))
|
||||
with database.connect() as db:
|
||||
rows = db.execute(f"SELECT * FROM knowledge_retrieval_log WHERE {scope.clause()} "
|
||||
"ORDER BY created_at DESC LIMIT 100", scope.params).fetchall()
|
||||
db.execute('BEGIN')
|
||||
access = current_access(db, principal.id, (*MAINTAINER, 'stats:read'))
|
||||
current_clause, current_args = access.tenant_sql('knowledge_retrieval_log')
|
||||
rows = db.execute(f"SELECT * FROM knowledge_retrieval_log WHERE {scope.clause()} AND {current_clause} "
|
||||
"ORDER BY created_at DESC LIMIT 100", [*scope.params, *current_args]).fetchall()
|
||||
return {"items": [{**dict(r), "hits": json.loads(r["hits_json"])} for r in rows]}
|
||||
|
||||
@app.get("/api/v2/knowledge/export")
|
||||
def export(request: Request, account_id: int | None = None,
|
||||
principal=Depends(require("knowledge:read", "knowledge:export"))):
|
||||
principal=Depends(require(*MAINTAINER, "knowledge:export"))):
|
||||
tenant = single(principal, account_id)
|
||||
required = (*MAINTAINER, 'knowledge:export')
|
||||
with database.connect() as db:
|
||||
db.execute('BEGIN IMMEDIATE')
|
||||
current_access(db, principal.id, required).check_tenants([tenant])
|
||||
database._audit(db, principal.id, "knowledge.export", f"tenant={tenant}", client_ip(request))
|
||||
|
||||
def stream():
|
||||
cursor = ""
|
||||
while True:
|
||||
with database.connect() as db:
|
||||
rows = db.execute("SELECT * FROM knowledge_item WHERE tenant_id=? AND status='published' AND id>? "
|
||||
db.execute('BEGIN')
|
||||
try:
|
||||
access = current_access(db, principal.id, required)
|
||||
except KnowledgeAccessDenied:
|
||||
return
|
||||
clause, args = access.item_sql('knowledge_item')
|
||||
rows = db.execute(f"SELECT * FROM knowledge_item WHERE tenant_id=? AND status='published' AND id>? AND {clause} "
|
||||
"AND (valid_until='' OR valid_until>=?) ORDER BY id LIMIT 200",
|
||||
(tenant, cursor, KnowledgeRetriever._today())).fetchall()
|
||||
(tenant, cursor, *args, KnowledgeRetriever._today())).fetchall()
|
||||
if not rows:
|
||||
return
|
||||
cursor = rows[-1]["id"]
|
||||
rows = [r for r in rows if store._fresh(db, r["id"], tenant)]
|
||||
for row in rows:
|
||||
for candidate in rows:
|
||||
# Streaming may outlive the original request. Recheck each record
|
||||
# at its emission boundary so a newly granted restriction applies.
|
||||
with database.connect() as db:
|
||||
db.execute('BEGIN')
|
||||
try:
|
||||
access = current_access(db, principal.id, required)
|
||||
except KnowledgeAccessDenied:
|
||||
return
|
||||
clause, args = access.item_sql('k')
|
||||
row = db.execute(f"SELECT * FROM knowledge_item k WHERE k.id=? AND k.tenant_id=? "
|
||||
f"AND k.status='published' AND (k.valid_until='' OR k.valid_until>=?) AND {clause}",
|
||||
[candidate['id'], tenant, KnowledgeRetriever._today(), *args]).fetchone()
|
||||
if row is None or not store._fresh(db, row['id'], tenant):
|
||||
continue
|
||||
yield json.dumps({"schema_version": "knowledge-v1", **{k: row[k] for k in
|
||||
("id", "revision", "title", "question", "answer", "conditions", "category", "kind", "valid_until")}},
|
||||
ensure_ascii=False) + "\n"
|
||||
|
||||
Reference in New Issue
Block a user