接收(并清空)收件箱中的所有消息。
(self, agent_name: str)
| 224 | json.dump(messages, f, ensure_ascii=False, indent=2) |
| 225 | |
| 226 | def receive(self, agent_name: str) -> list[AgentMessage]: |
| 227 | """ |
| 228 | 接收(并清空)收件箱中的所有消息。 |
| 229 | """ |
| 230 | inbox_path = self._inbox_path(agent_name) |
| 231 | if not os.path.exists(inbox_path): |
| 232 | return [] |
| 233 | |
| 234 | with open(inbox_path, "r") as f: |
| 235 | try: |
| 236 | raw_messages = json.load(f) |
| 237 | except json.JSONDecodeError: |
| 238 | return [] |
| 239 | |
| 240 | # 清空收件箱 |
| 241 | with open(inbox_path, "w") as f: |
| 242 | json.dump([], f) |
| 243 | |
| 244 | return [AgentMessage.from_dict(m) for m in raw_messages] |
| 245 | |
| 246 | def peek(self, agent_name: str) -> int: |
| 247 | """查看收件箱中有多少消息(不取出)""" |
no test coverage detected