"""Offline, preflighted corpus inventory. python -m KnowledgeService.seed --output FILE.""" from __future__ import annotations import argparse, hashlib, json from pathlib import Path from .models import KnowledgeError ROOT = Path(__file__).resolve().parent LABELS = {'open_license':'开放许可', 'official_reference':'官方参考', 'synthetic':'原创模拟', 'unverified':'待核验'} def checked_file(root, name, expected=None, normalize=False): path=(root/name).resolve() if not path.is_relative_to(root.resolve()) or not path.is_file(): raise KnowledgeError(422,'SEED_PATH_INVALID','知识种子路径不合法。') content=path.read_bytes() if normalize:content=content.replace(b'\r\n',b'\n') actual=hashlib.sha256(content).hexdigest() if expected and actual!=expected: raise KnowledgeError(422,'SEED_HASH_MISMATCH','知识种子校验失败:'+name) return content,actual def authority_catalog(): return json.loads((ROOT/'authority_sources.json').read_text(encoding='utf-8')) def corpus_plan(): """Validate the whole bundle before any DB write; content is never fetched at startup.""" rows=[] def add(folder,item,kind,source_type,expected=None,normalize=False,**extra): content,sha=checked_file(folder,item['file'],expected,normalize) provenance=dict(origin_kind=kind,origin_label=LABELS[kind],author='Work OS 项目',license='项目原创演示材料;允许随项目测试使用',version=str(item.get('version','1')),verified_on='2026-10-11',currency='not_normative',applicability='合成办公样例,仅用于演示测试',notice='模拟材料不代表真实单位制度;需替换为客户核验资料。',**extra) provenance.update(content_sha256=sha,sha256=sha) rows.append(dict(document_id=item['document_id'],base_id=item['base_id'],version=str(item.get('version','1')),title=item['title'],filename=item['file'],content=content,source_type=source_type,valid_from=item.get('valid_from'),valid_to=item.get('valid_to'),private=item.get('private',False),provenance=provenance)) legacy=ROOT.parent/'docs/05_知识库种子素材' m=json.loads((legacy/'manifest.json').read_text(encoding='utf-8-sig')) bases={'KB01':'kb-rules','KB02':'kb-history','KB03':'kb-templates','KB04':'kb-circulation','KB05':'kb-personal'} for d in m['documents']: add(legacy,{**d,'base_id':bases[d['kb_id']],'private':bool(d['owner_user_id'])},'synthetic','synthetic_demo',d['sha256'],True) m=json.loads((ROOT/'seeds/manifest.json').read_text(encoding='utf-8')) for d in m['documents']: add(ROOT/'seeds',d,'synthetic','synthetic_demo',d['sha256'],True) rows[-1]['provenance'].update(applicability=d.get('applicability','合成办公样例,仅用于演示测试'),review_due=d.get('review_due'),version='1.0') m=json.loads((ROOT/'open_sources/manifest.json').read_text(encoding='utf-8')) for d in m['documents']: for guide in (False,True): item={**d,'document_id':d['document_id']+('-zh' if guide else ''),'file':d['bridge_file'] if guide else d['file'],'title':d['title'].replace('(Microsoft 原文)','(中文导读)') if guide else d['title']} add(ROOT/'open_sources',item,'open_license','open_source_guide' if guide else 'open_source',d['bridge_sha256'] if guide else d['sha256'],guide) rows[-1]['provenance'].update({k:d[k] for k in ['source_url','commit','license','author']}) rows[-1]['provenance'].update(origin_sha256=d['sha256'],version=d['commit'],retrieved_at=m['retrieved_at'],license_url='https://creativecommons.org/licenses/by/4.0/',modified='中文主题导读,非逐句翻译' if guide else '原文未修改',applicability='一般写作风格,不是中国公文格式标准或企业制度',notice='保留CC BY 4.0归属;原仓库已归档,固定版本用于写作参考;未经原作者背书。') catalog=authority_catalog() for s in catalog['sources']: if s['status']!='current_reference':continue item=dict(document_id='reference-'+s['source_id'],base_id='kb-rules' if 'rules' in s['source_id'] else 'kb-templates',title=s['title']+'(官方来源参考卡)',file=s['source_id']+'.md') add(ROOT/'references',item,'official_reference','official_reference',s['summary_sha256'],True) rows[-1]['provenance'].update({k:s[k] for k in ['source_url','author','license','version','applicability','use_mode']}) rows[-1]['provenance'].update(currency='current_reference',notice=catalog['notice'],review_due=catalog['review_due'],modified='项目原创参考卡;不是官方全文') ids=[r['document_id'] for r in rows] if len(ids)!=len(set(ids)):raise KnowledgeError(422,'SEED_DUPLICATE','种子编号重复。') return rows def inventory(): rows=corpus_plan() items=[{k:v for k,v in r.items() if k!='content'} for r in rows] counts={} for r in rows: if not r['private']:counts[r['base_id']]=counts.get(r['base_id'],0)+1 return dict(schema_version=1,verified_on='2026-10-11',shared_count=sum(counts.values()),private_fixture_count=sum(r['private'] for r in rows),base_counts=counts,origin_counts={k:sum(r['provenance']['origin_kind']==k and not r['private'] for r in rows) for k in LABELS},outdated_imported_count=0,items=items,authority=authority_catalog()) def import_catalog(service,plan=None): for r in plan if plan is not None else corpus_plan(): if r['private']:continue # Preserve existing immutable originals when a checkout only changed newlines. # New seed versions use LF; arbitrary uploads still retain exact original bytes. from sqlalchemy import select from .models import Document,DocumentInfo content=r['content'] with service.Session() as db: old=db.scalar(select(Document).where(Document.tenant=='demo-tenant',Document.document_id==r['document_id'],Document.version==r['version'])) info=db.get(DocumentInfo,old.id) if old else None if info and info.original is not None and info.original.replace(b'\r\n',b'\n')==content.replace(b'\r\n',b'\n'): content=info.original provenance={**r['provenance'],'bundle_sha256':r['provenance']['content_sha256']} service._save(tenant='demo-tenant',document_id=r['document_id'],version=r['version'],base_id=r['base_id'],title=r['title'],content=content,filename=r['filename'],source_type=r['source_type'],valid_from=r['valid_from'],valid_to=r['valid_to'],provenance=provenance) if __name__=='__main__': parser=argparse.ArgumentParser();parser.add_argument('--output');args=parser.parse_args() value=json.dumps(inventory(),ensure_ascii=False,indent=2) if args.output:Path(args.output).write_text(value+'\n',encoding='utf-8') else:print(value)