ai-coding-system / app /agent /tools_runtime.py
PYAE1994's picture
Update app/agent/tools_runtime.py
2df004c verified
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"
}