Feat : Save exec block to file, More programming language supported in tools
This commit is contained in:
@@ -17,26 +17,37 @@ class BashInterpreter(Tools):
|
||||
super().__init__()
|
||||
self.tag = "bash"
|
||||
|
||||
def execute(self, commands: str, safety = False, timeout = 1000):
|
||||
def execute(self, commands: str, safety=False, timeout=1000):
|
||||
"""
|
||||
Execute bash commands.
|
||||
Execute bash commands and display output in real-time.
|
||||
"""
|
||||
if safety and input("Execute command? y/n ") != "y":
|
||||
return "Command rejected by user."
|
||||
|
||||
|
||||
concat_output = ""
|
||||
for command in commands:
|
||||
try:
|
||||
output = subprocess.check_output(command,
|
||||
shell=True,
|
||||
stderr=subprocess.STDOUT,
|
||||
universal_newlines=True,
|
||||
timeout=timeout
|
||||
)
|
||||
return output.strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
return f"Command execution failed:\n{e.output}"
|
||||
except subprocess.TimeoutExpired as e:
|
||||
return f"Command timed out. Output:\n{e.output}"
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
universal_newlines=True
|
||||
)
|
||||
command_output = ""
|
||||
for line in process.stdout:
|
||||
print(line, end="")
|
||||
command_output += line
|
||||
return_code = process.wait(timeout=timeout)
|
||||
if return_code != 0:
|
||||
return f"Command {command} failed with return code {return_code}:\n{command_output}"
|
||||
concat_output += f"Output of {command}:\n{command_output.strip()}\n\n"
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill() # Kill the process if it times out
|
||||
return f"Command {command} timed out. Output:\n{command_output}"
|
||||
except Exception as e:
|
||||
return f"Command {command} failed:\n{str(e)}"
|
||||
return concat_output
|
||||
|
||||
def interpreter_feedback(self, output):
|
||||
"""
|
||||
@@ -86,4 +97,4 @@ class BashInterpreter(Tools):
|
||||
|
||||
if __name__ == "__main__":
|
||||
bash = BashInterpreter()
|
||||
print(bash.execute(["ls", "pwd"]))
|
||||
print(bash.execute(["ls", "pwd", "ip a", "nmap -sC 127.0.0.1"]))
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import subprocess
|
||||
import os
|
||||
import tempfile
|
||||
import re
|
||||
|
||||
if __name__ == "__main__":
|
||||
from tools import Tools
|
||||
else:
|
||||
from sources.tools.tools import Tools
|
||||
|
||||
class CInterpreter(Tools):
|
||||
"""
|
||||
This class is a tool to allow agent for C code execution
|
||||
"""
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.tag = "c"
|
||||
|
||||
def execute(self, codes: str, safety=False) -> str:
|
||||
"""
|
||||
Execute C code by compiling and running it.
|
||||
"""
|
||||
output = ""
|
||||
code = '\n'.join(codes) if isinstance(codes, list) else codes
|
||||
|
||||
if safety and input("Execute code? y/n ") != "y":
|
||||
return "Code rejected by user."
|
||||
|
||||
exec_extension = ".exe" if os.name == "nt" else "" # Windows uses .exe, Linux/Unix does not
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
source_file = os.path.join(tmpdirname, "temp.c")
|
||||
exec_file = os.path.join(tmpdirname, "temp") + exec_extension
|
||||
with open(source_file, 'w') as f:
|
||||
f.write(code)
|
||||
|
||||
try:
|
||||
compile_command = ["gcc", source_file, "-o", exec_file]
|
||||
compile_result = subprocess.run(
|
||||
compile_command,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if compile_result.returncode != 0:
|
||||
return f"Compilation failed: {compile_result.stderr}"
|
||||
|
||||
run_command = [exec_file]
|
||||
run_result = subprocess.run(
|
||||
run_command,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if run_result.returncode != 0:
|
||||
return f"Execution failed: {run_result.stderr}"
|
||||
output = run_result.stdout
|
||||
|
||||
except subprocess.TimeoutExpired as e:
|
||||
return f"Execution timed out: {str(e)}"
|
||||
except FileNotFoundError:
|
||||
return "Error: 'gcc' not found. Ensure a C compiler (e.g., gcc) is installed and in PATH."
|
||||
except Exception as e:
|
||||
return f"Code execution failed: {str(e)}"
|
||||
|
||||
return output
|
||||
|
||||
def interpreter_feedback(self, output: str) -> str:
|
||||
"""
|
||||
Provide feedback based on the output of the code execution
|
||||
"""
|
||||
if self.execution_failure_check(output):
|
||||
feedback = f"[failure] Error in execution:\n{output}"
|
||||
else:
|
||||
feedback = "[success] Execution success, code output:\n" + output
|
||||
return feedback
|
||||
|
||||
def execution_failure_check(self, feedback: str) -> bool:
|
||||
"""
|
||||
Check if the code execution failed.
|
||||
"""
|
||||
error_patterns = [
|
||||
r"error",
|
||||
r"failed",
|
||||
r"traceback",
|
||||
r"invalid",
|
||||
r"exception",
|
||||
r"syntax",
|
||||
r"segmentation fault",
|
||||
r"core dumped",
|
||||
r"undefined",
|
||||
r"cannot"
|
||||
]
|
||||
combined_pattern = "|".join(error_patterns)
|
||||
if re.search(combined_pattern, feedback, re.IGNORECASE):
|
||||
return True
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
codes = [
|
||||
"""
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void hello() {
|
||||
printf("Hello, World!\\n");
|
||||
}
|
||||
""",
|
||||
"""
|
||||
int main() {
|
||||
hello();
|
||||
return 0;
|
||||
}
|
||||
"""]
|
||||
c = CInterpreter()
|
||||
print(c.execute(codes))
|
||||
@@ -0,0 +1,115 @@
|
||||
import subprocess
|
||||
import os
|
||||
import tempfile
|
||||
import re
|
||||
|
||||
if __name__ == "__main__":
|
||||
from tools import Tools
|
||||
else:
|
||||
from sources.tools.tools import Tools
|
||||
|
||||
class GoInterpreter(Tools):
|
||||
"""
|
||||
This class is a tool to allow execution of Go code.
|
||||
"""
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.tag = "go"
|
||||
|
||||
def execute(self, codes: str, safety=False) -> str:
|
||||
"""
|
||||
Execute Go code by compiling and running it.
|
||||
"""
|
||||
output = ""
|
||||
code = '\n'.join(codes) if isinstance(codes, list) else codes
|
||||
|
||||
if safety and input("Execute code? y/n ") != "y":
|
||||
return "Code rejected by user."
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
source_file = os.path.join(tmpdirname, "temp.go")
|
||||
exec_file = os.path.join(tmpdirname, "temp")
|
||||
with open(source_file, 'w') as f:
|
||||
f.write(code)
|
||||
|
||||
try:
|
||||
compile_command = ["go", "build", "-o", exec_file, source_file]
|
||||
compile_result = subprocess.run(
|
||||
compile_command,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if compile_result.returncode != 0:
|
||||
return f"Compilation failed: {compile_result.stderr}"
|
||||
|
||||
run_command = [exec_file]
|
||||
run_result = subprocess.run(
|
||||
run_command,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if run_result.returncode != 0:
|
||||
return f"Execution failed: {run_result.stderr}"
|
||||
output = run_result.stdout
|
||||
|
||||
except subprocess.TimeoutExpired as e:
|
||||
return f"Execution timed out: {str(e)}"
|
||||
except FileNotFoundError:
|
||||
return "Error: 'go' not found. Ensure Go is installed and in PATH."
|
||||
except Exception as e:
|
||||
return f"Code execution failed: {str(e)}"
|
||||
|
||||
return output
|
||||
|
||||
def interpreter_feedback(self, output: str) -> str:
|
||||
"""
|
||||
Provide feedback based on the output of the code execution
|
||||
"""
|
||||
if self.execution_failure_check(output):
|
||||
feedback = f"[failure] Error in execution:\n{output}"
|
||||
else:
|
||||
feedback = "[success] Execution success, code output:\n" + output
|
||||
return feedback
|
||||
|
||||
def execution_failure_check(self, feedback: str) -> bool:
|
||||
"""
|
||||
Check if the code execution failed.
|
||||
"""
|
||||
error_patterns = [
|
||||
r"error",
|
||||
r"failed",
|
||||
r"traceback",
|
||||
r"invalid",
|
||||
r"exception",
|
||||
r"syntax",
|
||||
r"panic",
|
||||
r"undefined",
|
||||
r"cannot"
|
||||
]
|
||||
combined_pattern = "|".join(error_patterns)
|
||||
if re.search(combined_pattern, feedback, re.IGNORECASE):
|
||||
return True
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
codes = [
|
||||
"""
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func hello() {
|
||||
fmt.Println("Hello, World!")
|
||||
}
|
||||
""",
|
||||
"""
|
||||
func main() {
|
||||
hello()
|
||||
}
|
||||
"""
|
||||
]
|
||||
g = GoInterpreter()
|
||||
print(g.execute(codes))
|
||||
@@ -71,10 +71,19 @@ class PyInterpreter(Tools):
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
codes = ["""
|
||||
def test():
|
||||
print("Hello world")
|
||||
test()
|
||||
"""]
|
||||
text = """
|
||||
Sure here is how to print in python:
|
||||
```python:tmp.py
|
||||
|
||||
def print_hello():
|
||||
hello = "Hello World"
|
||||
print(hello)
|
||||
|
||||
print_hello()
|
||||
|
||||
```
|
||||
"""
|
||||
py = PyInterpreter()
|
||||
codes, save_path = py.load_exec_block(text)
|
||||
py.save_block(codes, save_path)
|
||||
print(py.execute(codes))
|
||||
+22
-9
@@ -69,8 +69,18 @@ class Tools():
|
||||
if start_idx == -1 or end_idx == -1:
|
||||
return text
|
||||
return text[:start_idx] + text[end_idx:]
|
||||
|
||||
def save_block(self, blocks:[str], save_path:str) -> None:
|
||||
"""
|
||||
Save the code/query block to a file.
|
||||
"""
|
||||
if save_path is None:
|
||||
return
|
||||
for block in blocks:
|
||||
with open(save_path, 'w') as f:
|
||||
f.write(block)
|
||||
|
||||
def load_exec_block(self, generation: str) -> str:
|
||||
def load_exec_block(self, llm_text: str) -> str:
|
||||
"""
|
||||
Extract the code/query blocks from the answer text, removing consistent leading whitespace.
|
||||
"""
|
||||
@@ -79,24 +89,25 @@ class Tools():
|
||||
end_tag = '```'
|
||||
code_blocks = []
|
||||
start_index = 0
|
||||
save_path = None
|
||||
|
||||
if start_tag not in generation:
|
||||
if start_tag not in llm_text:
|
||||
return None
|
||||
|
||||
while True:
|
||||
start_pos = generation.find(start_tag, start_index)
|
||||
start_pos = llm_text.find(start_tag, start_index)
|
||||
if start_pos == -1:
|
||||
break
|
||||
|
||||
line_start = generation.rfind('\n', 0, start_pos) + 1
|
||||
line_start = llm_text.rfind('\n', 0, start_pos) + 1
|
||||
if line_start == 0:
|
||||
line_start = 0
|
||||
leading_whitespace = generation[line_start:start_pos]
|
||||
leading_whitespace = llm_text[line_start:start_pos]
|
||||
|
||||
end_pos = generation.find(end_tag, start_pos + len(start_tag))
|
||||
end_pos = llm_text.find(end_tag, start_pos + len(start_tag))
|
||||
if end_pos == -1:
|
||||
break
|
||||
content = generation[start_pos + len(start_tag):end_pos]
|
||||
content = llm_text[start_pos + len(start_tag):end_pos]
|
||||
lines = content.split('\n')
|
||||
if leading_whitespace:
|
||||
processed_lines = []
|
||||
@@ -107,7 +118,9 @@ class Tools():
|
||||
processed_lines.append(line)
|
||||
content = '\n'.join(processed_lines)
|
||||
|
||||
if ':' in content.split('\n')[0]:
|
||||
save_path = content.split('\n')[0].split(':')[1]
|
||||
content = content[content.find('\n')+1:]
|
||||
code_blocks.append(content)
|
||||
start_index = end_pos + len(end_tag)
|
||||
|
||||
return code_blocks
|
||||
return code_blocks, save_path
|
||||
Reference in New Issue
Block a user