File size: 13,553 Bytes
99caad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Tenant-scoped administration; no administrator impersonation for private content."""
from observability import traced
import uuid
from datetime import datetime, timezone
from sqlalchemy import select, func
from .models import KnowledgeBase, Grant, Document, Chunk, LibraryConfig, DocumentInfo, DocumentSource, Audit, KnowledgeError


def classify(title, text):
    """Inspectable local classification rules, usable without sending intranet data out."""
    value = title + "\n" + text
    rules = [("邮件", "邮件沟通", ("邮件", "收件人", "抄送")),
             ("会议", "会议记录", ("会议", "纪要", "议题")),
             ("审批", "办理流程", ("审批", "流转", "签发")),
             ("公文", "公文写作", ("公文", "通知", "请示", "函")),
             ("制度", "制度依据", ("制度", "规定", "办法")),
             ("归档", "档案管理", ("归档", "档案"))]
    hits = [(tag, category) for tag, category, words in rules if any(w in value for w in words)]
    return [tag for tag, _ in hits][:8] or ["办公"], hits[0][1] if hits else "综合办公"


class Management:
    @staticmethod
    def _admin(user):
        if "admin" not in user.get("roles", []):
            raise KnowledgeError(403, "ADMIN_REQUIRED", "需要知识库管理员权限。")

    @staticmethod
    def _audit(db, user, action, resource, detail=None):
        db.add(Audit(id="audit-" + uuid.uuid4().hex, tenant=user["tenant_id"], actor=user["user_id"],
                     action=action, resource=resource, detail=detail or {},
                     created_at=datetime.now(timezone.utc).isoformat()))

    @staticmethod
    def _info(db, doc, filename, content, chunks):
        if db.get(DocumentInfo, doc.id) is None:
            tags, category = classify(doc.title, "\n".join(x[1] for x in chunks)[:20000])
            db.add(DocumentInfo(document_key=doc.id, filename=filename, original=content,
                tags=tags, category=category, publication="published" if doc.status == "ready" else "draft", revision=1))

    @staticmethod
    def _published(db, doc):
        info = db.get(DocumentInfo, doc.id)
        return info is None or info.publication == "published"

    def library_list(self, user, admin=False):
        if admin:
            self._admin(user)
        with self.Session() as db:
            bases = (db.scalars(select(KnowledgeBase).where(KnowledgeBase.tenant == user["tenant_id"])).all()
                     if admin else self._bases(db, user))
            items = []
            for b in bases:
                config = db.get(LibraryConfig, (b.tenant, b.id))
                docs = db.scalars(select(Document).where(Document.tenant == b.tenant, Document.base_id == b.id)).all()
                # Employee counts must not disclose other people's private materials.
                if b.visibility == "personal" and not admin:
                    docs = [d for d in docs if d.owner == user["user_id"]]
                latest = {d.document_id for d in docs if self._published(db, d)}
                items.append({"base_id": b.id, "name": b.name, "visibility": b.visibility,
                    "description": config.description if config else "", "category": config.category if config else "综合办公",
                    "status": config.status if config else "enabled", "revision": config.revision if config else 1,
                    "document_count": len(latest), "version_count": len(docs), "access": "read", "mode": self.mode,
                    "grants": list(db.scalars(select(Grant.user_id).where(Grant.tenant == b.tenant, Grant.base_id == b.id))) if admin else None})
            return {"items": items, "next_cursor": None}

    @traced("Knowledge Service")
    def save_library(self, user, data, bid=None):
        self._admin(user)
        bid = bid or "kb-" + uuid.uuid4().hex[:16]
        with self.lock, self.Session.begin() as db:
            base = db.get(KnowledgeBase, (user["tenant_id"], bid))
            config = db.get(LibraryConfig, (user["tenant_id"], bid))
            if base:
                revision = config.revision if config else 1
                if data["expected_revision"] != revision:
                    raise KnowledgeError(409, "VERSION_CONFLICT", "知识库已更新,请刷新后重试。")
                if base.visibility != data["visibility"]:
                    raise KnowledgeError(422, "IMMUTABLE_SCOPE", "知识库的共享或个人属性不能改变。")
                base.name = data["name"]
            else:
                if data.get("expected_revision") is not None:
                    raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "知识库不存在。")
                db.add(KnowledgeBase(tenant=user["tenant_id"], id=bid, name=data["name"], visibility=data["visibility"]))
                db.add(Grant(tenant=user["tenant_id"], base_id=bid, user_id=user["user_id"]))
            if not config:
                config = LibraryConfig(tenant=user["tenant_id"], base_id=bid, revision=2 if base else 1)
                db.add(config)
            elif base:
                config.revision += 1
            for k in ("description", "category", "status"):
                setattr(config, k, data[k])
            self._audit(db, user, "library.save", bid, {"status": data["status"]})
        return next(x for x in self.library_list(user, True)["items"] if x["base_id"] == bid)

    @traced("Knowledge Service")
    def grant(self, user, bid, uid, enabled, expected_revision):
        self._admin(user)
        with self.lock, self.Session.begin() as db:
            base = db.get(KnowledgeBase, (user["tenant_id"], bid))
            if not base:
                raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "知识库不存在。")
            cfg = db.get(LibraryConfig, (user["tenant_id"], bid))
            revision = cfg.revision if cfg else 1
            if revision != expected_revision:
                raise KnowledgeError(409, "VERSION_CONFLICT", "授权已变化,请刷新。")
            if not cfg:
                cfg = LibraryConfig(tenant=user["tenant_id"], base_id=bid, revision=1)
                db.add(cfg)
            cfg.revision += 1
            old = db.get(Grant, (user["tenant_id"], bid, uid))
            if enabled and old is None:
                db.add(Grant(tenant=user["tenant_id"], base_id=bid, user_id=uid))
            elif not enabled and old:
                db.delete(old)
            self._audit(db, user, "grant.enable" if enabled else "grant.revoke", bid, {"user_id": uid})
        return self.library_list(user, True)

    def admin_documents(self, user, bid):
        self._admin(user)
        with self.Session() as db:
            if db.get(KnowledgeBase, (user["tenant_id"], bid)) is None:
                raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "知识库不存在。")
            docs = db.scalars(select(Document).where(Document.tenant == user["tenant_id"], Document.base_id == bid)
                              .order_by(Document.created_at.desc(), Document.id.desc())).all()
            sources={x.document_key:x.body for x in db.scalars(select(DocumentSource).where(DocumentSource.document_key.in_([d.id for d in docs])))}
            infos={x.document_key:x for x in db.scalars(select(DocumentInfo).where(DocumentInfo.document_key.in_([d.id for d in docs])))}
            chunk_counts=dict(db.execute(select(Chunk.document_key,func.count()).where(Chunk.document_key.in_([d.id for d in docs])).group_by(Chunk.document_key)).all())
            items = []
            for d in docs:
                info = infos.get(d.id)
                items.append({"document_key": d.id, "document_id": d.document_id, "version": d.version,
                    "title": d.title, "status": d.status, "error_code": d.error_code, "source_type": d.source_type,
                    "publication": info.publication if info else "published", "tags": info.tags if info else [],
                    "category": info.category if info else "综合办公", "revision": info.revision if info else 1,
                    "chunk_count": chunk_counts.get(d.id,0),
                    "owner_user_id": d.owner, "created_at": d.created_at,
                    "valid_from": d.valid_from, "valid_to": d.valid_to, "provenance": sources.get(d.id)})
            return {"items": items, "next_cursor": None}

    @traced("Knowledge Service")
    def admin_ingest(self, user, bid, content, filename, document_id=None, title=None, valid_from=None, valid_to=None):
        self._admin(user)
        document_id = document_id or "document-" + uuid.uuid4().hex
        # Serialize version allocation and immutable save as one service command.
        with self.lock:
            with self.Session() as db:
                base = db.get(KnowledgeBase, (user["tenant_id"], bid))
                if not base or base.visibility != "shared":
                    raise KnowledgeError(422, "PRIVATE_UPLOAD_SCOPE", "个人来文请从本人事项上传;管理端仅向共享库入库。")
                versions = list(db.scalars(select(Document.version).where(Document.tenant == user["tenant_id"], Document.document_id == document_id)))
            version = str(max((int(v) for v in versions if v.isdigit()), default=0) + 1)
            doc = self._save(tenant=user["tenant_id"], document_id=document_id, version=version, base_id=bid,
                title=title or filename, content=content, filename=filename, source_type="admin_upload",
                valid_from=valid_from, valid_to=valid_to)
            with self.Session.begin() as db:
                self._audit(db, user, "document.ingest", doc.id, {"status": doc.status, "version": version})
            return next(d for d in self.admin_documents(user, bid)["items"] if d["document_key"] == doc.id)

    @traced("Knowledge Service")
    def edit_document(self, user, key, data):
        self._admin(user)
        with self.lock, self.Session.begin() as db:
            doc = db.get(Document, key)
            if not doc or doc.tenant != user["tenant_id"]:
                raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "文档不存在。")
            if doc.owner and doc.owner != user["user_id"]:
                raise KnowledgeError(403, "PRIVATE_DOCUMENT", "个人来文由其所有者管理。")
            info = db.get(DocumentInfo, key)
            if not info:
                info = DocumentInfo(document_key=key, filename=doc.title, original=None, tags=[], revision=1)
                db.add(info)
            if data["expected_revision"] != info.revision:
                raise KnowledgeError(409, "VERSION_CONFLICT", "文档已改变,请刷新。")
            if data["publication"] == "published" and doc.status != "ready":
                raise KnowledgeError(422, "DOCUMENT_NOT_READY", "解析失败的文档不能发布,请上传修正版。")
            info.tags = data["tags"]
            info.category = data["category"]
            info.publication = data["publication"]
            info.revision += 1
            self._audit(db, user, "document.metadata", key, {"publication": info.publication})
        return {"document_key": key, "revision": data["expected_revision"] + 1}

    @traced("Knowledge Service")
    def original(self, user, cid, wid):
        citation = self.citation(user, cid, wid)
        with self.Session() as db:
            chunk = db.get(Chunk, citation["citation_id"])
            info = db.get(DocumentInfo, chunk.document_key)
            if not info or info.original is None:
                raise KnowledgeError(404, "ORIGINAL_UNAVAILABLE", "该历史资料没有保留原文件。")
            return info.filename, info.original

    def preview(self, user, key):
        self._admin(user)
        with self.Session() as db:
            doc = db.get(Document, key)
            if not doc or doc.tenant != user["tenant_id"] or doc.owner and doc.owner != user["user_id"]:
                raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "文档不存在或无权查看。")
            if doc.base_id not in {b.id for b in self._bases(db, user)}:
                raise KnowledgeError(404, "RESOURCE_NOT_FOUND", "当前管理员没有该库的读取权限。")
            chunks = db.scalars(select(Chunk).where(Chunk.document_key == key).order_by(Chunk.ordinal)).all()
            source=db.get(DocumentSource,key)
            return {"title": doc.title, "version": doc.version, "status": doc.status, "error_code": doc.error_code, "provenance": source.body if source else None,
                    "items": [{"locator": c.locator, "text": c.text} for c in chunks]}

    def audit_list(self, user):
        self._admin(user)
        with self.Session() as db:
            rows = db.scalars(select(Audit).where(Audit.tenant == user["tenant_id"]).order_by(Audit.created_at.desc()).limit(200)).all()
            return {"items": [{k: getattr(r, k) for k in ("id", "actor", "action", "resource", "detail", "created_at")} for r in rows]}

    def source_catalog(self,user):
        """Bundled public corpus only; never exposes uploaded tenant/private documents."""
        self._admin(user)
        from .seed import inventory
        result=inventory()
        result['items']=[r for r in result['items'] if not r['private']]
        return result