From a16535fb089e0970d2fcf12e3b11fca002ddb9f8 Mon Sep 17 00:00:00 2001 From: seehi <6580@pm.me> Date: Thu, 12 Sep 2024 13:52:50 +0800 Subject: [PATCH] add handle_exception --- metagpt/memory/memory.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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]