"""组装:把帖子+知识片段+解构,组装成 ingest 请求体(对齐 创作知识-重构设计.md §6)。 纯函数,无 LLM、无 IO。同一帖子拆出的多条知识复用同一 source.id。 """ from __future__ import annotations import json from creation_knowledge.models import ( Deconstruction, IngestPayload, KnowledgeItem, Post, ) def build_ingest_payload( post: Post, item: KnowledgeItem, deco: Deconstruction ) -> IngestPayload: content = json.dumps( {"what": item.what, "why": item.why, "how": item.how}, ensure_ascii=False, ) custom_ext: list[dict] = [] if item.evidence: custom_ext.append( {"key": "原文证据", "type": "str", "value": " / ".join(item.evidence)} ) if deco.stage_reason: custom_ext.append( {"key": "阶段判断理由", "type": "str", "value": deco.stage_reason} ) if deco.scope_reason: custom_ext.append( {"key": "作用域判断理由", "type": "str", "value": deco.scope_reason} ) return IngestPayload( source={ "id": post.id, "source_type": "post", "title": post.title, "author": post.author_name, "source_metadata": {"platform": post.platform, "url": post.url}, }, title=item.title, content=content, dim_attributes=list(item.knowledge_types), dim_creations=list(deco.stages), scopes=[{"scope_type": s.scope_type, "value": s.value} for s in deco.scopes], custom_ext=custom_ext, )