Artificial-superintelligence commited on
Commit
f1cc900
·
verified ·
1 Parent(s): 57a0931

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -33
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 is unclear or doesn't match any known command type, respond with "Unclear request. Please provide more details."
 
 
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
- sanitized_command,
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
- # Execute the AI-suggested command or action
105
- if ai_result.startswith("CREATE_FILE:"):
106
- filename = ai_result.split(":")[1]
107
- filepath = os.path.join(current_dir, filename)
108
- with open(filepath, 'w') as f:
109
- pass # Create an empty file
110
- result = f"Created new file: {filename}"
111
- elif ai_result.startswith("EDIT_FILE:"):
112
- _, filename, content = ai_result.split(":", 2)
113
- filepath = os.path.join(current_dir, filename)
114
- with open(filepath, 'w') as f:
115
- f.write(content)
116
- result = f"File {filename} created and edited successfully."
117
- elif ai_result.startswith("!python"):
118
- result = execute_command(ai_result[1:]) # Remove the leading '!'
119
- elif ai_result.startswith("git clone"):
120
- result = execute_command(ai_result)
121
- elif ai_result.startswith("show files"):
122
- files = os.listdir(current_dir)
123
- result = "Files in current directory:\n" + "\n".join(files)
124
- else:
125
- result = f"Unclear AI response: {ai_result}"
126
-
127
- return jsonify({"result": f"AI Executed: {ai_result}\n\nOutput:\n{result}"})
 
 
 
 
 
 
 
 
 
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)})