-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
113 lines (99 loc) · 3.86 KB
/
Copy pathtools.py
File metadata and controls
113 lines (99 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
import os
class Tool:
def __init__(self, name: str, description: str, func):
self.name = name
self.description = description
self.func = func # function(input_json: str) -> str
def read_file(input_json: str):
try:
data = json.loads(input_json)
path = data.get("path")
if not path:
return "Error: 'path' key is required."
if not os.path.isfile(path):
return f"Error: File '{path}' not found."
with open(path, "r") as f:
content = f.read()
return content
except Exception as e:
return f"Error reading file: {str(e)}"
read_file_tool = Tool(
name="read_file",
description="Reads the content of a given file path. Input: JSON with {'path': '<relative path>'}. Output: file content as string.",
func=read_file
)
def create_file(input_json: str):
try:
data = json.loads(input_json)
path = data.get("path")
content = data.get("content", "")
if not path:
return "Error: 'path' key is required."
# Prevent overwriting existing files accidentally
if os.path.exists(path):
return f"Error: File '{path}' already exists."
with open(path, "w") as f:
f.write(content)
return f"File '{path}' created successfully."
except Exception as e:
return f"Error creating file: {str(e)}"
create_file_tool = Tool(
name="create_file",
description="Creates a new file with given path and content. Input: JSON with {'path': '<relative path>', 'content': '<file content>'}. Output: Success or error message.",
func=create_file
)
def list_files(input_json: str):
try:
data = json.loads(input_json)
directory = data.get("directory", ".") # default current dir
if not os.path.isdir(directory):
return f"Error: Directory '{directory}' not found."
files = []
for root, dirs, filenames in os.walk(directory):
for filename in filenames:
files.append(os.path.relpath(os.path.join(root, filename), directory))
return json.dumps(files) # Return JSON list of files relative to directory
except Exception as e:
return f"Error listing files: {str(e)}"
list_files_tool = Tool(
name="list_files",
description="Lists all files under the given directory recursively. Input: JSON with {'directory': '<path>'} (optional, default '.'). Output: JSON list of file paths.",
func=list_files
)
def append_file(input_json: str):
try:
data = json.loads(input_json)
path = data.get("path")
content = data.get("content", "")
if not path:
return "Error: 'path' key is required."
if not os.path.isfile(path):
return f"Error: File '{path}' not found."
with open(path, "a") as f:
f.write(content)
return f"Content appended to '{path}' successfully."
except Exception as e:
return f"Error appending to file: {str(e)}"
append_file_tool = Tool(
name="append_file",
description="Appends content to an existing file. Input: JSON with {'path': '<file_path>', 'content': '<text to append>'}. Output: Success or error message.",
func=append_file
)
def write_file(input_json: str):
try:
data = json.loads(input_json)
path = data.get("path")
content = data.get("content", "")
if not path:
return "Error: 'path' key is required."
with open(path, "w") as f:
f.write(content)
return f"File '{path}' written successfully."
except Exception as e:
return f"Error writing file: {str(e)}"
write_file_tool = Tool(
name="write_file",
description="Writes (replaces) content of a file. Input: JSON with {'path': '<file_path>', 'content': '<new content>'}. Output: Success or error message.",
func=write_file
)