File size: 3,663 Bytes
ab01d3f
 
 
2df004c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6037ed6
 
 
 
ab01d3f
6037ed6
 
 
ab01d3f
 
 
 
 
 
6037ed6
ab01d3f
6037ed6
ab01d3f
 
6037ed6
ab01d3f
 
6037ed6
 
 
 
 
ab01d3f
6037ed6
 
ab01d3f
 
6037ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab01d3f
 
6037ed6
 
 
 
 
 
 
 
ab01d3f
6037ed6
 
 
 
 
ab01d3f
 
6037ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
2df004c
6037ed6
 
 
 
 
 
 
 
 
 
2df004c
 
 
6037ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2df004c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6037ed6
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import subprocess

# =========================
# BROWSER LAYER
# =========================
from browser_tools import BrowserTool

browser = BrowserTool()

def browser_open(url: str):
    return browser.open_url(url)

def browser_click(selector: str):
    return browser.click(selector)

def browser_type(selector: str, text: str):
    return browser.type(selector, text)

def browser_screenshot(path: str = "screen.png"):
    return browser.screenshot(path)


# =========================
# BASIC SYSTEM TOOLS
# =========================

def run_shell(cmd: str):
    """
    Execute shell command safely and return structured output
    """
    result = subprocess.run(
        cmd,
        shell=True,
        capture_output=True,
        text=True
    )

    return {
        "tool": "run_shell",
        "stdout": result.stdout,
        "stderr": result.stderr,
        "exit_code": result.returncode
    }


def write_file(path: str, content: str):
    """
    Create or overwrite a file
    """
    os.makedirs(os.path.dirname(path), exist_ok=True)

    with open(path, "w", encoding="utf-8") as f:
        f.write(content)

    return {
        "tool": "write_file",
        "status": "written",
        "path": path
    }


def read_file(path: str):
    """
    Read file content safely
    """
    if not os.path.exists(path):
        return {
            "tool": "read_file",
            "error": "file_not_found",
            "path": path
        }

    with open(path, "r", encoding="utf-8") as f:
        content = f.read()

    return {
        "tool": "read_file",
        "path": path,
        "content": content
    }


def list_files(path="."):
    """
    List directory contents
    """
    return {
        "tool": "list_files",
        "path": path,
        "files": os.listdir(path)
    }


def delete_file(path: str):
    """
    Delete file safely
    """
    if os.path.exists(path):
        os.remove(path)
        return {
            "tool": "delete_file",
            "status": "deleted",
            "path": path
        }

    return {
        "tool": "delete_file",
        "error": "not_found",
        "path": path
    }


# =========================
# TOOL ROUTER (AI → ACTION)
# =========================

def execute_tool_call(tool_call: dict):
    """
    Main dispatcher: AI → Tool execution bridge
    """

    tool = tool_call.get("tool")
    args = tool_call.get("args", {})

    # -------------------------
    # SYSTEM TOOLS
    # -------------------------
    if tool == "run_shell":
        return run_shell(args.get("cmd", ""))

    elif tool == "write_file":
        return write_file(
            args.get("path", ""),
            args.get("content", "")
        )

    elif tool == "read_file":
        return read_file(args.get("path", ""))

    elif tool == "list_files":
        return list_files(args.get("path", "."))

    elif tool == "delete_file":
        return delete_file(args.get("path", ""))

    # -------------------------
    # BROWSER TOOLS (STEP 4)
    # -------------------------
    elif tool == "browser_open":
        return browser_open(args.get("url", ""))

    elif tool == "browser_click":
        return browser_click(args.get("selector", ""))

    elif tool == "browser_type":
        return browser_type(
            args.get("selector", ""),
            args.get("text", "")
        )

    elif tool == "browser_screenshot":
        return browser_screenshot(
            args.get("path", "screen.png")
        )

    # -------------------------
    # UNKNOWN TOOL
    # -------------------------
    return {
        "tool": tool,
        "error": "unknown_tool"
    }