Use new replace content functionality in EditMemoryTool

Resolves #747
This commit is contained in:
Dominik Jain authored and Dominik Jain committed 2025-11-19 15:04:25 +01:00
1 parent b6f88b9517
commit b330a03a15
1 file changed
+10 -8
+10 -8
View File
@@ -1,4 +1,5 @@
import json
from typing import Literal
from serena.tools import ReplaceContentTool, Tool
@@ -70,20 +71,21 @@ class EditMemoryTool(Tool):
def apply(
self,
memory_file_name: str,
regex: str,
needle: str,
repl: str,
mode: Literal["literal", "regex"],
) -> str:
r"""
Replaces content matching a regular expression in a memory.
:param memory_file_name: the name of the memory
:param regex: a Python-style regular expression (flags DOTALL and MULTILINE enabled, i.e.
'.' matches all characters, multi-line matching is enabled).
Apply the usual escaping as needed for reserved characters in Python-style regex.
:param repl: the string to replace the matched content with, which may contain
backreferences like \1, \2, etc. for groups matched by the regex.
Insert new content verbatim, except for backslashes, which have to be escaped.
:param needle: the string or regex pattern to search for.
If `mode` is "literal", this string will be matched exactly.
If `mode` is "regex", this string will be treated as a regular expression (syntax of Python's `re` module,
with flags DOTALL and MULTILINE enabled).
:param repl: the replacement string (verbatim).
:param mode: either "literal" or "regex", specifying how the `needle` parameter is to be interpreted.
"""
replace_content_tool = self.agent.get_tool(ReplaceContentTool)
rel_path = self.memories_manager.get_memory_file_path(memory_file_name).relative_to(self.get_project_root())
return replace_content_tool.replace_content(str(rel_path), regex, repl, mode="regex", require_not_ignored=False)
return replace_content_tool.replace_content(str(rel_path), needle, repl, mode=mode, require_not_ignored=False)