mohamedelfeky-mo commited on
Commit
655c8ff
·
verified ·
1 Parent(s): 69b0528

Create write_code.py

Browse files
Files changed (1) hide show
  1. tools/write_code.py +23 -0
tools/write_code.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import os
3
+
4
+ @tool
5
+ def write_code_to_file(filename: str, content: str) -> str:
6
+ """
7
+ Writes the provided text or code content to a specific file.
8
+ Use this tool to save code, documentation, or text files.
9
+
10
+ Args:
11
+ filename: The name of the file (e.g., 'script.py', 'README.md').
12
+ content: The full content to write into the file.
13
+ """
14
+ try:
15
+ # Ensure we don't overwrite critical system files by restricting path (optional safety)
16
+ if ".." in filename or filename.startswith("/"):
17
+ return "Error: File path must be relative and strictly within the working directory."
18
+
19
+ with open(filename, 'w', encoding='utf-8') as f:
20
+ f.write(content)
21
+ return f"Successfully created file: {filename}"
22
+ except Exception as e:
23
+ return f"Error writing file: {str(e)}"