mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-13 18:16:10 +00:00
Make ReadFileTool independent of language server, adding a read function in Project
This commit is contained in:
1 parent
edb07bc823
commit
34db0f1446
3 files changed
+22
-2
No files matched your search
@@ -30,3 +30,15 @@ class Project:
|
||||
|
||||
def path_to_project_yml(self) -> str:
|
||||
return os.path.join(self.project_root, self.project_config.rel_path_to_project_yml())
|
||||
|
||||
def read_file(self, relative_path: str) -> str:
|
||||
"""
|
||||
Reads a file relative to the project root.
|
||||
|
||||
:param relative_path: the path to the file relative to the project root
|
||||
:return: the content of the file
|
||||
"""
|
||||
abs_path = Path(self.project_root) / relative_path
|
||||
if not abs_path.exists():
|
||||
raise FileNotFoundError(f"File not found: {abs_path}")
|
||||
return abs_path.read_text(encoding=self.project_config.encoding)
|
||||
@@ -41,7 +41,7 @@ class ReadFileTool(Tool):
|
||||
"""
|
||||
self.agent.validate_relative_path(relative_path)
|
||||
|
||||
result = self.language_server.retrieve_full_file_content(relative_path)
|
||||
result = self.project.read_file(relative_path)
|
||||
result_lines = result.splitlines()
|
||||
if end_line is None:
|
||||
result_lines = result_lines[start_line:]
|
||||
|
||||
@@ -11,6 +11,7 @@ from mcp.server.fastmcp.utilities.func_metadata import FuncMetadata, func_metada
|
||||
from sensai.util import logging
|
||||
from sensai.util.string import dict_string
|
||||
|
||||
from serena.project import Project
|
||||
from serena.prompt_factory import PromptFactory
|
||||
from serena.symbol import SymbolManager
|
||||
from serena.util.class_decorators import singleton
|
||||
@@ -55,11 +56,18 @@ class Component(ABC):
|
||||
assert self.agent.symbol_manager is not None
|
||||
return self.agent.symbol_manager
|
||||
|
||||
@property
|
||||
def project(self) -> Project:
|
||||
project = self.agent.get_active_project()
|
||||
if project is None:
|
||||
raise ValueError("No active project")
|
||||
return project
|
||||
|
||||
def create_code_editor(self) -> "CodeEditor":
|
||||
from ..code_editor import JetBrainsCodeEditor, LanguageServerCodeEditor
|
||||
|
||||
if self.agent.serena_config.jetbrains:
|
||||
return JetBrainsCodeEditor(project=self.agent.get_active_project(), agent=self.agent)
|
||||
return JetBrainsCodeEditor(project=self.project, agent=self.agent)
|
||||
else:
|
||||
return LanguageServerCodeEditor(self.symbol_manager, agent=self.agent)
|
||||
|
||||
|
||||
Reference in new issue
Block a user