Fix #2762: Make CSV knowledge sources detect and load file updates

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-06 00:04:39 +00:00
parent dabf02a90d
commit 14579a7861
4 changed files with 205 additions and 0 deletions

View File

@@ -43,7 +43,15 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
"""Post-initialization method to load content."""
self.safe_file_paths = self._process_file_paths()
self.validate_content()
self._record_file_mtimes()
self.content = self.load_content()
def _record_file_mtimes(self):
"""Record modification times of all files."""
self._file_mtimes = {}
for path in self.safe_file_paths:
if path.exists() and path.is_file():
self._file_mtimes[path] = path.stat().st_mtime
@abstractmethod
def load_content(self) -> Dict[Path, str]:
@@ -107,3 +115,14 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
)
return [self.convert_to_path(path) for path in path_list]
def files_have_changed(self) -> bool:
"""Check if any of the files have been modified since they were last loaded."""
for path in self.safe_file_paths:
if not path.exists() or not path.is_file():
continue
current_mtime = path.stat().st_mtime
if path not in self._file_mtimes or current_mtime > self._file_mtimes[path]:
self._logger.log("info", f"File {path} has been modified. Reloading data.")
return True
return False