-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
109 lines (93 loc) · 3.68 KB
/
functions.py
File metadata and controls
109 lines (93 loc) · 3.68 KB
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
import asyncio
import os
import httpx
async def web_search(query: str, api_key: str):
print(f"\n[Web Search] Searching for: {query}")
if not api_key:
return [{"error": "Brave API key is not configured"}]
url = "https://api.search.brave.com/res/v1/web/search"
headers = {
"Accept": "application/json",
"X-Subscription-Token": api_key,
}
params = {"q": query}
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
results = []
for item in data.get("web", {}).get("results", [])[:5]:
results.append(
{
"title": item.get("title"),
"snippet": item.get("description"),
"url": item.get("url"),
}
)
return results if results else [{"message": "No relevant results found"}]
except Exception as e:
return [{"error": f"Search request failed: {str(e)}"}]
async def execute_commands(type: str, command: list, blocked_commands: set):
results = []
print(f"\n[Tool] Running {len(command)} command(s) in {type}")
for cmd in command:
cmd_stripped = (cmd or "").strip()
if not cmd_stripped:
results.append({"cmd": cmd, "error": "Empty command"})
continue
first_word = cmd_stripped.split(maxsplit=1)[0].lower().rstrip(";")
if first_word in blocked_commands:
results.append(
{
"cmd": cmd,
"error": f"Blocked potentially interactive command: {first_word}",
}
)
print(f" > Blocked: {cmd}")
continue
print(f" > Running: {cmd}")
try:
if type == "bash":
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
elif type == "cmd":
proc = await asyncio.create_subprocess_shell(
f"cmd.exe /c {cmd}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
elif type == "pwsh":
executable = "pwsh" if os.name != "nt" else "powershell"
proc = await asyncio.create_subprocess_shell(
f"{executable} -Command {cmd}",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
else:
results.append({"error": f"Unsupported shell type: {type}"})
continue
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=30.0)
except asyncio.TimeoutError:
proc.terminate()
try:
await asyncio.wait_for(proc.wait(), timeout=5.0)
except asyncio.TimeoutError:
proc.kill()
results.append({"cmd": cmd, "error": "Command timed out after 30 seconds"})
continue
results.append(
{
"cmd": cmd,
"stdout": stdout.decode("utf-8", errors="ignore").strip()[:1000],
"stderr": stderr.decode("utf-8", errors="ignore").strip()[:1000],
"returncode": proc.returncode,
}
)
except Exception as e:
results.append({"cmd": cmd, "error": str(e)})
return results