agent-zero
agent-zero copied to clipboard
`code_save_tool.py` a great custom tool ^^
I had request agent create a code_save_tool.py to help agent could have ability auto to save code file tools, and it's great !!!
Note: file saved to "/a0/python/tools/code_save_tool.py"
#!/usr/bin/env python3
# code_save_tool.py
# This tool is designed to save code to a file.
import os
from python.helpers.tool import Tool, Response
class CodeSaveTool(Tool):
async def execute(self, file_path: str, code: str):
self.log.update(progress=f"Saving code to {file_path}")
try:
with open(file_path, 'w') as f:
f.write(code)
self.log.update(progress=f"Code successfully saved to {file_path}")
return Response(message=f"Code successfully saved to {file_path}")
except Exception as e:
self.log.update(progress=f"Error saving code: {e}")
return Response(error=f"Error saving code: {e}")
def get_log_object(self):
return self.agent.context.log.log(
type="code_save",
heading=f"{self.agent.agent_name}: Using tool '{self.name}'",
content="",
kvps=self.args,
)
do i need to do anything else to use it?
create file save_file.py, and upload to "/a0/python/tools/save_file.py" run container in docker, then agent could using it ^^
#!/usr/bin/env python3
# save_file.py
# This tool is designed to help agent save any content to a file.
import os
from python.helpers.tool import Tool, Response
class SaveFileTool(Tool):
async def run(self, file_path: str, content: str) -> Response:
try:
with open(file_path, 'w') as f:
f.write(content)
return Response(message=f"File '{file_path}' saved successfully.", break_loop=False)
except Exception as e:
return Response(message=f"Error saving file '{file_path}': {e}", break_loop=True)