File size: 2,625 Bytes
ec342d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import PythonInterpreterTool, tool
import requests
import json

@tool
def TextInverterTool(input_string: str) -> str:
    """
    Inverts the order of characters in a given text string.

    Args:
        input_string: Text string to be inverted

    Returns:
        str: Character-reversed version of the input text
    """
    return input_string[::-1]


@tool
def PythonScriptExecutor(script_location: str) -> str:
    """
    Loads and executes Python code from a specified file path using interpreter tools.

    Args:
        script_location: Complete file system path to the Python script (.py extension)

    Returns:
        str: Execution results or error description if the operation fails
    """
    try:
        # Read the Python file content
        with open(script_location, "r", encoding='utf-8') as file_handle:
            python_code = file_handle.read()

        # Initialize interpreter and execute
        code_interpreter = PythonInterpreterTool()
        execution_result = code_interpreter.run({"code": python_code})

        return execution_result.get("output", "Execution completed without output.")
    except FileNotFoundError:
        return f"File not found: {script_location}"
    except Exception as error:
        return f"Script execution error: {str(error)}"


@tool
def WebFileDownloader(source_url: str, destination_path: str) -> str:
    """
    Retrieves a file from a web URL and stores it locally at the specified path.

    Args:
        source_url: Web address of the file to download
        destination_path: Local filesystem path for saving the downloaded content

    Returns:
        str: Status message describing the download operation result
    """
    try:
        # Configure request with headers and timeout
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }

        web_response = requests.get(source_url, headers=headers, timeout=45)
        web_response.raise_for_status()

        # Save file content to destination
        with open(destination_path, "wb") as output_file:
            output_file.write(web_response.content)

        file_size = len(web_response.content)
        return f"Successfully downloaded {file_size} bytes to {destination_path}"

    except requests.exceptions.RequestException as req_error:
        return f"Download request failed: {str(req_error)}"
    except IOError as io_error:
        return f"File save operation failed: {str(io_error)}"
    except Exception as general_error:
        return f"Unexpected download error: {str(general_error)}"