agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

`code_save_tool.py` a great custom tool ^^

Open Mr-Jack-Tung opened this issue 8 months ago • 2 comments

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,
        )

Mr-Jack-Tung avatar Jun 02 '25 06:06 Mr-Jack-Tung

do i need to do anything else to use it?

Endervven avatar Jun 02 '25 14:06 Endervven

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)

Mr-Jack-Tung avatar Jun 02 '25 15:06 Mr-Jack-Tung