| 3 | |
| 4 | |
| 5 | def build_tool(config) -> Tool: |
| 6 | tool = Tool( |
| 7 | "File Operation Tool", |
| 8 | "Write / read file to / from disk", |
| 9 | name_for_model="file_operation", |
| 10 | description_for_model="Plugin for operating files", |
| 11 | logo_url=None, |
| 12 | contact_email=None, |
| 13 | legal_info_url=None |
| 14 | ) |
| 15 | |
| 16 | @tool.get("/write_file") |
| 17 | def write_file(file_path: str, text: str) -> str: |
| 18 | '''write file to disk |
| 19 | ''' |
| 20 | write_path = ( |
| 21 | Path(file_path) |
| 22 | ) |
| 23 | try: |
| 24 | write_path.parent.mkdir(exist_ok=True, parents=False) |
| 25 | with write_path.open("w", encoding="utf-8") as f: |
| 26 | f.write(text) |
| 27 | return f"File written successfully to {file_path}." |
| 28 | except Exception as e: |
| 29 | return "Error: " + str(e) |
| 30 | |
| 31 | @tool.get("/read_file") |
| 32 | def read_file(file_path: str) -> str: |
| 33 | '''read file from disk |
| 34 | ''' |
| 35 | read_path = ( |
| 36 | Path(file_path) |
| 37 | ) |
| 38 | try: |
| 39 | with read_path.open("r", encoding="utf-8") as f: |
| 40 | content = f.read() |
| 41 | return content |
| 42 | except Exception as e: |
| 43 | return "Error: " + str(e) |
| 44 | |
| 45 | return tool |