A file system operation tool
| 19 | |
| 20 | |
| 21 | class FileSystemTool(ToolBase): |
| 22 | """A file system operation tool""" |
| 23 | |
| 24 | # Directories to exclude from file operations |
| 25 | EXCLUDED_DIRS = { |
| 26 | 'node_modules', 'dist', '.git', '__pycache__', '.venv', 'venv' |
| 27 | } |
| 28 | # File prefixes to exclude |
| 29 | EXCLUDED_FILE_PREFIXES = ('.', '..', '__pycache__') |
| 30 | |
| 31 | SYSTEM_FOR_ABBREVIATIONS = """你是一个帮我简化文件信息并返回缩略的机器人,你需要根据输入文件内容来生成压缩过的文件内容。 |
| 32 | |
| 33 | 要求: |
| 34 | 1. 如果是代码文件,你需要保留imports、exports、类信息、方法信息、异步或同步等可用于其他文件引用或理解的必要信息 |
| 35 | 2. 如果是配置文件,你需要保留所有的key |
| 36 | 3. 如果是文档,你需要总结所有章节,并给出一个精简的版本 |
| 37 | |
| 38 | 你的返回内容会直接存储下来,因此你需要省略其他非必要符号,例如"```"或者"让我来帮忙..."都不需要。 |
| 39 | |
| 40 | 你的优化目标: |
| 41 | 1. 【优先】保留充足的信息,尽量不损失原意 |
| 42 | 2. 【其次】保留尽量少的token数量 |
| 43 | """ |
| 44 | |
| 45 | def __init__(self, config, **kwargs): |
| 46 | super().__init__(config) |
| 47 | self.exclude_func(getattr(config.tools, 'file_system', None)) |
| 48 | self.output_dir = getattr(config, 'output_dir', DEFAULT_OUTPUT_DIR) |
| 49 | if self.exclude_functions and 'edit_file' not in self.exclude_functions \ |
| 50 | or self.include_functions and 'edit_file' in self.include_functions: |
| 51 | self.edit_file_config = getattr(config.tools.file_system, |
| 52 | 'edit_file_config', None) |
| 53 | self.edit_client = OpenAI( |
| 54 | api_key=self.edit_file_config.api_key, |
| 55 | base_url=self.edit_file_config.base_url) |
| 56 | self.trust_remote_code = kwargs.get('trust_remote_code', False) |
| 57 | self.allow_read_all_files = getattr( |
| 58 | getattr(config.tools, 'file_system', {}), 'allow_read_all_files', |
| 59 | False) |
| 60 | if not self.trust_remote_code: |
| 61 | self.allow_read_all_files = False |
| 62 | if hasattr(self.config, 'llm'): |
| 63 | self.llm: LLM = LLM.from_config(self.config) |
| 64 | index_dir = getattr(config, 'index_cache_dir', DEFAULT_INDEX_DIR) |
| 65 | self.index_dir = os.path.join(self.output_dir, index_dir) |
| 66 | self.system = self.SYSTEM_FOR_ABBREVIATIONS |
| 67 | if hasattr(self.config.tools.file_system, 'system_for_abbreviations'): |
| 68 | self.system = self.config.tools.file_system.system_for_abbreviations |
| 69 | |
| 70 | async def connect(self): |
| 71 | logger.warning_once( |
| 72 | '[IMPORTANT]FileSystemTool is not implemented with sandbox, please consider other similar ' |
| 73 | 'tools if you want to run dangerous code.') |
| 74 | |
| 75 | async def _get_tools_inner(self): |
| 76 | tools = { |
| 77 | 'file_system': [ |
| 78 | Tool( |