diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index d44753413..0707a36ea 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -7,13 +7,14 @@ @Modified By: mashenquan, 2023-11-1. According to RFC 116: Updated the type of index key. """ from collections import defaultdict -from typing import DefaultDict, Iterable, Set +from typing import DefaultDict, Iterable, Optional, Set from pydantic import BaseModel, Field, SerializeAsAny from metagpt.const import IGNORED_MESSAGE_ID from metagpt.schema import Message from metagpt.utils.common import any_to_str, any_to_str_set +from metagpt.utils.exceptions import handle_exception class Memory(BaseModel): @@ -105,6 +106,7 @@ class Memory(BaseModel): rsp += self.index[action] return rsp - def get_by_position(self, position: int) -> Message: - """Return the message by its position""" + @handle_exception + def get_by_position(self, position: int) -> Optional[Message]: + """Returns the message at the given position if valid, otherwise returns None""" return self.storage[position]