Skip to content

Commit

Permalink
Merge pull request #1445 from lolsZz/main
Browse files Browse the repository at this point in the history
Add sudo support for apt package installation in Terminal
  • Loading branch information
KillianLucas authored Oct 10, 2024
2 parents 286e9c5 + 623b776 commit 6ef8d22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,4 @@ misc/

# Ignore litellm_uuid.txt
litellm_uuid.txt
.aider*
33 changes: 33 additions & 0 deletions interpreter/core/computer/terminal/terminal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os
import time
import subprocess
import getpass

from ..utils.recipient_utils import parse_for_recipient
from .languages.applescript import AppleScript
Expand Down Expand Up @@ -45,6 +47,29 @@ def __init__(self, computer):
]
self._active_languages = {}

def sudo_install(self, package):
try:
# First, try to install without sudo
subprocess.run(['apt', 'install', '-y', package], check=True)
except subprocess.CalledProcessError:
# If it fails, try with sudo
print(f"Installation of {package} requires sudo privileges.")
sudo_password = getpass.getpass("Enter sudo password: ")

try:
# Use sudo with password
subprocess.run(
['sudo', '-S', 'apt', 'install', '-y', package],
input=sudo_password.encode(),
check=True
)
print(f"Successfully installed {package}")
except subprocess.CalledProcessError as e:
print(f"Failed to install {package}. Error: {e}")
return False

return True

def get_language(self, language):
for lang in self.languages:
if language.lower() == lang.name.lower() or (
Expand All @@ -55,6 +80,14 @@ def get_language(self, language):
return None

def run(self, language, code, stream=False, display=False):
# Check if this is an apt install command
if language == "shell" and code.strip().startswith("apt install"):
package = code.split()[-1]
if self.sudo_install(package):
return [{"type": "console", "format": "output", "content": f"Package {package} installed successfully."}]
else:
return [{"type": "console", "format": "output", "content": f"Failed to install package {package}."}]

if language == "python":
if (
self.computer.import_computer_api
Expand Down

0 comments on commit 6ef8d22

Please sign in to comment.