Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,6 @@ import shutil
|
|
6 |
import sys
|
7 |
import google.generativeai as genai
|
8 |
|
9 |
-
def sanitize_command(command):
|
10 |
-
"""Sanitize the command to prevent shell injection."""
|
11 |
-
return command.replace(";", "").replace("&&", "").replace("||", "")
|
12 |
-
|
13 |
app = Flask(__name__)
|
14 |
|
15 |
# Create a temporary directory for operations
|
@@ -41,7 +37,7 @@ You are an AI-powered terminal assistant. Your role is to interpret user request
|
|
41 |
- Python script execution: For running .py files
|
42 |
- File creation and editing: For creating new files and adding content
|
43 |
|
44 |
-
2. Provide the exact command to be executed or action to be taken, without any explanation or additional text.
|
45 |
|
46 |
3. For pip install requests, always prefix the command with '!python -m' to ensure proper execution.
|
47 |
|
@@ -56,18 +52,19 @@ You are an AI-powered terminal assistant. Your role is to interpret user request
|
|
56 |
- If asked to edit a file with specific content, respond with: "EDIT_FILE:filename.py:file_content"
|
57 |
Replace 'filename.py' with the actual filename and 'file_content' with the requested content.
|
58 |
|
59 |
-
8. If a request
|
|
|
|
|
60 |
|
61 |
-
Always respond with ONLY the command to be executed or action to be taken, nothing else.
|
62 |
"""
|
63 |
|
64 |
chat = model.start_chat(history=[])
|
65 |
|
66 |
def execute_command(command, cwd=None):
|
67 |
"""Executes a command and returns the output."""
|
68 |
-
sanitized_command = sanitize_command(command)
|
69 |
process = subprocess.Popen(
|
70 |
-
|
71 |
shell=True,
|
72 |
stdout=subprocess.PIPE,
|
73 |
stderr=subprocess.PIPE,
|
@@ -101,30 +98,39 @@ def execute_code():
|
|
101 |
response = chat.send_message(f"{system_instruction}\n\nKnowledge Base:\n{knowledge_base}\n\nUser request: {ai_command}")
|
102 |
ai_result = response.text.strip()
|
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 |
elif command == "show files":
|
129 |
files = os.listdir(current_dir)
|
130 |
return jsonify({"result": "Files in current directory:\n" + "\n".join(files)})
|
|
|
6 |
import sys
|
7 |
import google.generativeai as genai
|
8 |
|
|
|
|
|
|
|
|
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
# Create a temporary directory for operations
|
|
|
37 |
- Python script execution: For running .py files
|
38 |
- File creation and editing: For creating new files and adding content
|
39 |
|
40 |
+
2. Provide the exact command(s) to be executed or action(s) to be taken, without any explanation or additional text.
|
41 |
|
42 |
3. For pip install requests, always prefix the command with '!python -m' to ensure proper execution.
|
43 |
|
|
|
52 |
- If asked to edit a file with specific content, respond with: "EDIT_FILE:filename.py:file_content"
|
53 |
Replace 'filename.py' with the actual filename and 'file_content' with the requested content.
|
54 |
|
55 |
+
8. If a request requires multiple commands, provide them as a list of commands, each on a new line, prefixed with 'CMD:'.
|
56 |
+
|
57 |
+
9. If a request is unclear or doesn't match any known command type, respond with "Unclear request. Please provide more details."
|
58 |
|
59 |
+
Always respond with ONLY the command(s) to be executed or action(s) to be taken, nothing else.
|
60 |
"""
|
61 |
|
62 |
chat = model.start_chat(history=[])
|
63 |
|
64 |
def execute_command(command, cwd=None):
|
65 |
"""Executes a command and returns the output."""
|
|
|
66 |
process = subprocess.Popen(
|
67 |
+
command,
|
68 |
shell=True,
|
69 |
stdout=subprocess.PIPE,
|
70 |
stderr=subprocess.PIPE,
|
|
|
98 |
response = chat.send_message(f"{system_instruction}\n\nKnowledge Base:\n{knowledge_base}\n\nUser request: {ai_command}")
|
99 |
ai_result = response.text.strip()
|
100 |
|
101 |
+
# Split multiple commands
|
102 |
+
commands = [cmd.strip() for cmd in ai_result.split('CMD:') if cmd.strip()]
|
103 |
+
results = []
|
104 |
+
|
105 |
+
for cmd in commands:
|
106 |
+
if cmd.startswith("CREATE_FILE:"):
|
107 |
+
filename = cmd.split(":")[1]
|
108 |
+
filepath = os.path.join(current_dir, filename)
|
109 |
+
with open(filepath, 'w') as f:
|
110 |
+
pass # Create an empty file
|
111 |
+
results.append(f"Created new file: {filename}")
|
112 |
+
elif cmd.startswith("EDIT_FILE:"):
|
113 |
+
_, filename, content = cmd.split(":", 2)
|
114 |
+
filepath = os.path.join(current_dir, filename)
|
115 |
+
with open(filepath, 'w') as f:
|
116 |
+
f.write(content)
|
117 |
+
results.append(f"File {filename} created and edited successfully.")
|
118 |
+
elif cmd.startswith("!"):
|
119 |
+
results.append(execute_command(cmd[1:])) # Remove the leading '!'
|
120 |
+
elif cmd.startswith("show files"):
|
121 |
+
files = os.listdir(current_dir)
|
122 |
+
results.append("Files in current directory:\n" + "\n".join(files))
|
123 |
+
elif cmd.startswith("cd "):
|
124 |
+
new_dir = os.path.join(current_dir, cmd[3:])
|
125 |
+
if os.path.isdir(new_dir):
|
126 |
+
current_dir = os.path.abspath(new_dir)
|
127 |
+
results.append(f"Changed directory to: {current_dir}")
|
128 |
+
else:
|
129 |
+
results.append(f"Error: Directory not found: {new_dir}")
|
130 |
+
else:
|
131 |
+
results.append(execute_command(cmd))
|
132 |
+
|
133 |
+
return jsonify({"result": f"AI Executed:\n{ai_result}\n\nOutput:\n" + "\n".join(results)})
|
134 |
elif command == "show files":
|
135 |
files = os.listdir(current_dir)
|
136 |
return jsonify({"result": "Files in current directory:\n" + "\n".join(files)})
|