memory_contract.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 记忆契约类型与 Protocol 定义。
  5. 上层消费者(context-agent、data-agent、reviewer)只依赖本模块的类型和协议,
  6. 不直接依赖 StateManager / IndexManager / ScratchpadManager 等具体实现。
  7. 具体实现见 memory_contract_adapter.py。
  8. """
  9. from __future__ import annotations
  10. from dataclasses import asdict, dataclass, field
  11. from typing import Any, Dict, List, Optional, Protocol, runtime_checkable
  12. # ---------------------------------------------------------------------------
  13. # 契约返回类型
  14. # ---------------------------------------------------------------------------
  15. @dataclass
  16. class CommitResult:
  17. """commit_chapter 的返回值。"""
  18. chapter: int
  19. entities_added: int = 0
  20. entities_updated: int = 0
  21. state_changes_recorded: int = 0
  22. relationships_added: int = 0
  23. memory_items_added: int = 0
  24. summary_path: str = ""
  25. warnings: List[str] = field(default_factory=list)
  26. def to_dict(self) -> Dict[str, Any]:
  27. return asdict(self)
  28. @dataclass
  29. class EntitySnapshot:
  30. """query_entity 的返回值。"""
  31. id: str
  32. name: str
  33. type: str = "角色"
  34. tier: str = "核心"
  35. aliases: List[str] = field(default_factory=list)
  36. attributes: Dict[str, Any] = field(default_factory=dict)
  37. first_appearance: int = 0
  38. last_appearance: int = 0
  39. recent_state_changes: List[Dict[str, Any]] = field(default_factory=list)
  40. def to_dict(self) -> Dict[str, Any]:
  41. return asdict(self)
  42. @dataclass
  43. class Rule:
  44. """query_rules 的返回值项。"""
  45. id: str
  46. subject: str
  47. field: str
  48. value: str
  49. domain: str = ""
  50. source_chapter: int = 0
  51. def to_dict(self) -> Dict[str, Any]:
  52. return asdict(self)
  53. @dataclass
  54. class OpenLoop:
  55. """get_open_loops 的返回值项。"""
  56. id: str
  57. content: str
  58. status: str = "active"
  59. planted_chapter: int = 0
  60. expected_payoff: str = ""
  61. urgency: float = 0.0
  62. def to_dict(self) -> Dict[str, Any]:
  63. return asdict(self)
  64. @dataclass
  65. class TimelineEvent:
  66. """get_timeline 的返回值项。"""
  67. event: str
  68. chapter: int = 0
  69. time_hint: str = ""
  70. event_type: str = ""
  71. def to_dict(self) -> Dict[str, Any]:
  72. return asdict(self)
  73. @dataclass
  74. class ContextPack:
  75. """load_context 的返回值。sections 由调用者解释具体结构。"""
  76. chapter: int
  77. sections: Dict[str, Any] = field(default_factory=dict)
  78. budget_used_tokens: int = 0
  79. def to_dict(self) -> Dict[str, Any]:
  80. return asdict(self)
  81. # ---------------------------------------------------------------------------
  82. # 契约 Protocol
  83. # ---------------------------------------------------------------------------
  84. @runtime_checkable
  85. class MemoryContract(Protocol):
  86. """记忆模块统一契约。
  87. 上层消费者依赖此 Protocol,不依赖具体实现类。
  88. """
  89. def commit_chapter(self, chapter: int, result: dict) -> CommitResult:
  90. """写后提交:将章节处理结果写入所有存储。"""
  91. ...
  92. def load_context(self, chapter: int, budget_tokens: int = 4000) -> ContextPack:
  93. """写前读取:加载章节上下文包。"""
  94. ...
  95. def query_entity(self, entity_id: str) -> Optional[EntitySnapshot]:
  96. """查询单个实体快照。"""
  97. ...
  98. def query_rules(self, domain: str = "") -> List[Rule]:
  99. """查询世界规则,可按 domain 过滤。"""
  100. ...
  101. def read_summary(self, chapter: int) -> str:
  102. """读取章节摘要文本。"""
  103. ...
  104. def get_open_loops(self, status: str = "active") -> List[OpenLoop]:
  105. """查询未闭合伏笔/悬念。"""
  106. ...
  107. def get_timeline(self, from_ch: int, to_ch: int) -> List[TimelineEvent]:
  108. """查询章节范围内的时间线事件。"""
  109. ...