|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from pydantic import Field |
| 4 | + |
| 5 | +from pipelex.core.memory.working_memory import WorkingMemory |
| 6 | +from pipelex.core.stuffs.stuff_content import ListContent, StructuredContent |
| 7 | +from pipelex.tools.func_registry import func_registry |
| 8 | + |
| 9 | + |
| 10 | +class FilePath(StructuredContent): |
| 11 | + """A path to a file in the codebase.""" |
| 12 | + |
| 13 | + path: str = Field(description="Path to the file") |
| 14 | + |
| 15 | + |
| 16 | +class CodebaseFileContent(StructuredContent): |
| 17 | + """Content of a codebase file.""" |
| 18 | + |
| 19 | + file_path: str = Field(description="Path to the codebase file") |
| 20 | + file_content: str = Field(description="Content of the codebase file") |
| 21 | + |
| 22 | + |
| 23 | +def read_file_content(working_memory: WorkingMemory) -> ListContent[CodebaseFileContent]: |
| 24 | + """Read the content of related codebase files. |
| 25 | +
|
| 26 | + Args: |
| 27 | + working_memory: Working memory containing related_file_paths |
| 28 | +
|
| 29 | + Returns: |
| 30 | + ListContent of CodebaseFileContent objects |
| 31 | + """ |
| 32 | + |
| 33 | + file_paths_list = working_memory.get_stuff_as_list("related_file_paths", item_type=FilePath) |
| 34 | + |
| 35 | + codebase_files: List[CodebaseFileContent] = [] |
| 36 | + for file_path in file_paths_list.items: |
| 37 | + try: |
| 38 | + with open(file_path.path, "r", encoding="utf-8") as file: |
| 39 | + content = file.read() |
| 40 | + codebase_files.append(CodebaseFileContent(file_path=file_path.path, file_content=content)) |
| 41 | + except Exception as e: |
| 42 | + codebase_files.append( |
| 43 | + CodebaseFileContent(file_path=file_path.path, file_content=f"# File not found or unreadable: {file_path.path}\n# Error: {str(e)}") |
| 44 | + ) |
| 45 | + |
| 46 | + return ListContent[CodebaseFileContent](items=codebase_files) |
| 47 | + |
| 48 | + |
| 49 | +func_registry.register_function(read_file_content, name="read_file_content") |
0 commit comments