fix:editor can not replace text in empty file

This commit is contained in:
黄伟韬 2024-09-11 20:45:37 +08:00
parent a94e282e7f
commit 71f670bfcf
4 changed files with 29 additions and 3 deletions

View file

@ -13,7 +13,6 @@ from typing import List, Optional, Union
from pydantic import BaseModel, ConfigDict
from metagpt.const import DEFAULT_WORKSPACE_ROOT
from metagpt.logs import logger
from metagpt.tools.libs.index_repo import IndexRepo
from metagpt.tools.libs.linter import Linter
from metagpt.tools.tool_registry import register_tool
@ -717,8 +716,6 @@ class Editor(BaseModel):
If you need to use it multiple times, wait for the next turn.
"""
# FIXME: support replacing *all* occurrences
if to_replace.strip() == "":
raise ValueError("`to_replace` must not be empty.")
if to_replace == new_content:
raise ValueError("`to_replace` and `new_content` must be different.")
@ -730,6 +727,12 @@ class Editor(BaseModel):
with file_name.open("r") as file:
file_content = file.read()
if to_replace.strip() == "":
if file_content.strip() == "":
raise ValueError(f"The file '{file_name}' is empty. Please use the append method to add content.")
else:
raise ValueError("`to_replace` must not be empty.")
if file_content.count(to_replace) > 1:
raise ValueError(
"`to_replace` appears more than once, please include enough lines to make code in `to_replace` unique."

View file

@ -32,6 +32,7 @@ class Linter:
self.languages = dict(
python=self.py_lint,
sql=self.fake_lint, # Base_lint lacks support for full SQL syntax. Use fake_lint to bypass the validation.
)
self.all_lint_cmd = None
@ -112,6 +113,9 @@ class Linter:
error = basic_lint(rel_fname, code)
return error
def fake_lint(self, fname, rel_fname, code):
return None
def lint_python_compile(fname, code):
try: