Files
2026-08-21 16:00:13 +08:00

59 lines
3.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""河图枢世界模型运行时(api-spec §12;design/world-model-design §3/§4)。
与治理视图 ``governance.WorldEntity``(全局、人工维护、无 character_id)区分:
本模块是**按人物隔离的运行时**世界状态,所有写入绑定 ``character_id``。
自动抽取(世界引擎)属 v1 半自动留后项,本表先承载手动 CRUD 与未来抽取结果。
"""
from datetime import datetime
from sqlalchemy import DateTime, Float, ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.core.ids import new_world_entity_id, new_world_relation_id
from app.core.time import utc_now
from app.db.base import Base
class CharacterWorldEntity(Base):
"""人物视角下的世界实体(契约字典 §11.1 type / §11.2 state_type)。"""
__tablename__ = "character_world_entities"
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_world_entity_id)
character_id: Mapped[str] = mapped_column(String(80), ForeignKey("characters.id"), index=True)
type: Mapped[str] = mapped_column(String(40), nullable=False, default="concept", index=True)
name: Mapped[str] = mapped_column(String(200), nullable=False)
attributes_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
state_type: Mapped[str] = mapped_column(String(40), nullable=False, default="inference")
status: Mapped[str] = mapped_column(String(40), nullable=False, default="confirmed", index=True)
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=0.5)
evidence_ids_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
sources_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utc_now, onupdate=utc_now
)
class WorldRelation(Base):
"""实体间关系(design §4;predicate 为自由字符串谓词,非枚举)。"""
__tablename__ = "world_relations"
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_world_relation_id)
character_id: Mapped[str] = mapped_column(String(80), ForeignKey("characters.id"), index=True)
subject_id: Mapped[str] = mapped_column(
String(80), ForeignKey("character_world_entities.id"), index=True
)
predicate: Mapped[str] = mapped_column(String(120), nullable=False)
object_id: Mapped[str] = mapped_column(
String(80), ForeignKey("character_world_entities.id"), index=True
)
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=0.5)
evidence_ids_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utc_now, onupdate=utc_now
)