Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ PYTHONPATH=.
# MOBILE_PORT=5555
# ADB_HOST="127.0.0.1"
# ADB_PORT=5037

# Command Execution Safety Settings
COMMAND_SAFETY_CHECK_ENABLED=true
COMMAND_TIMEOUT=30
COMMAND_MAX_OUTPUT_CHARS=2000
1 change: 1 addition & 0 deletions DesktopAI
Submodule DesktopAI added at 30a09c
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ LLM_PROVIDER="groq"
LLM_TEMPERATURE=0.8
```

### Command Execution Safety

Commands passed to `run_command` are checked against built-in dangerous-command patterns, including patterns for `rm -rf`, `mkfs`, `dd if=`, `shutdown`, `sudo`, `reboot`, recursive `chmod`, and fork bombs. Chained commands are checked as well.

When a command matches a dangerous pattern and safety checks are enabled, the overlay displays the command and waits for confirmation. Approve with the **Yes** button or say/type `yes` or `confirm`; deny with **No** or say/type `no` or `cancel`. The command runs only after approval.

Execution is limited by a configurable timeout. If it expires, the process is terminated and a timeout message is returned. Output longer than the configured character limit is truncated and suffixed with `[Output truncated...]`.

Set these values in `.env`:

| Environment variable | Default | Description |
| --- | --- | --- |
| `COMMAND_SAFETY_CHECK_ENABLED` | `true` | Enable pattern checks and confirmation prompts for dangerous commands. |
| `COMMAND_TIMEOUT` | `30` | Maximum execution time in seconds. |
| `COMMAND_MAX_OUTPUT_CHARS` | `2000` | Maximum command output characters before truncation. |

The safety setting is named `COMMAND_SAFETY_CHECK_ENABLED` in the current implementation (not `COMMAND_SAFETY_CHECK`). Dangerous patterns are defined in `src/config.py` as `DEFAULT_DANGEROUS_PATTERNS`; they are built-in defaults, not an environment variable named `DANGEROUS_PATTERNS`.

### UI Configuration
```python
# Interface settings in src/config.py
Expand Down
109 changes: 80 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This is the main entry point for the DesktopAI assistant with a clean modular architecture.
"""

import argparse
import os
import sys

Expand All @@ -12,9 +13,7 @@

import threading

from src.audio.audio_processor import AudioProcessor
from src.audio.listener import Listener
from src.audio.ttsplayer import TTSPlayer
from src import config
from src.core.assistant import call_agent
from src.core.tools import register_stop_assistant
from src.ui.overlay import app, overlay
Expand All @@ -27,72 +26,124 @@
class DesktopAssistant:
"""Main DesktopAI Assistant class with clean modular architecture."""

def __init__(self):
def __init__(self, text_only: bool = False, no_tts: bool = False):
"""Initialize the assistant with all required components."""
self.text_only = text_only
self.no_tts = no_tts or text_only
config.TEXT_ONLY_MODE = text_only
config.NO_TTS = self.no_tts
self.lock = threading.Lock()
self._setup_components()
self._setup_ui()

def _setup_components(self):
"""Initialize core components."""
# Text-to-Speech
self.speech = TTSPlayer()
self.speech.start()
self.speech = None
if not self.no_tts:
from src.audio.ttsplayer import TTSPlayer

# Audio processing
self.audio_processor = AudioProcessor()
self.listener = Listener(tts_player=self.speech, overlay=overlay)
self.speech = TTSPlayer()
self.speech.start()

self.audio_processor = None
self.listener = None
if not self.text_only:
from src.audio.audio_processor import AudioProcessor
from src.audio.listener import Listener

self.audio_processor = AudioProcessor()
self.listener = Listener(tts_player=self.speech, overlay=overlay)

def _setup_ui(self):
"""Setup user interface."""
overlay.on_new_message = self.process_query
overlay.start()
self.overlay = overlay
self.overlay.on_new_message = self.process_query
self.overlay.start()

def _speak(self, text):
if self.speech is not None:
self.speech.speak(text)

def process_audio(self, audio):
"""Process incoming audio data."""
if self.audio_processor is None:
logger.error("Audio processing is unavailable in text-only mode.")
return

with self.lock:
logger.info("Processing audio...")
overlay.put_message("status", "Analyzing voice...", "gold")
self.overlay.put_message("status", "Analyzing voice...", "gold")
transcription = self.audio_processor.process_audio(audio)
self.process_query(transcription)

def process_query(self, query: str):
"""Process the text query."""
overlay.put_message("query", query)
self.overlay.put_message("query", query)
confirmation = self.overlay.resolve_voice_confirmation(query)
if confirmation is not None:
response = "Execution confirmed. Running the command." if confirmation else "Execution cancelled by user."
self.overlay.put_message("response", response)
self.overlay.put_message("status", "Active", "green")
self._speak(response)
return

logger.debug(f"Invoking agent with: {query}")
overlay.put_message("status", "Processing...", "gold")
self.overlay.put_message("status", "Processing...", "gold")
try:
response = call_agent(query)
logger.info(f"Agent response: {response}")
overlay.put_message("response", response)
overlay.put_message("status", "Active", "green")
self.speech.speak(response)
self.overlay.put_message("response", response)
self.overlay.put_message("status", "Active", "green")
self._speak(response)
except Exception as e:
logger.error(f"Error processing query: {e}")
overlay.put_message("status", "Error occurred", "red")
self.speech.speak("Sorry, an error occurred while processing your request.")
self.overlay.put_message("status", "Error occurred", "red")
self._speak("Sorry, an error occurred while processing your request.")

def start(self):
"""Start the assistant."""
logger.info("🔊 Listening in the background... Say something!")
overlay.put_message("status", "Active", "green")
executor.submit(self.listener.listen, self.process_audio)
if self.listener is not None:
logger.info("🔊 Listening in the background... Say something!")
executor.submit(self.listener.listen, self.process_audio)
else:
logger.info("Text-only mode active. Enter a message in the overlay.")
self.overlay.put_message("status", "Active", "green")

def shutdown(self):
"""Clean shutdown of the assistant."""
self.speech.speak("Shutting down!")
overlay.put_message("status", "Shutting down...", "red")
self._speak("Shutting down!")
self.overlay.put_message("status", "Shutting down...", "red")
logger.info("Shutting down...")

self.listener.stop_listening()
self.speech.shutdown()
if self.listener is not None:
self.listener.stop_listening()
if self.speech is not None:
self.speech.shutdown()
executor.shutdown(wait=False, cancel_futures=True)
overlay.close()
self.overlay.close()


def main():
"""Main function to run Desktop Assistant."""
assistant = DesktopAssistant()
parser = argparse.ArgumentParser()
parser.add_argument(
"--text-only",
action="store_true",
default=False,
help="Run in text-only mode without microphone or audio dependencies",
)
parser.add_argument(
"--no-tts",
action="store_true",
default=False,
help="Disable Text-To-Speech output",
)
args = parser.parse_args()

config.TEXT_ONLY_MODE = args.text_only
config.NO_TTS = args.no_tts

assistant = DesktopAssistant(text_only=args.text_only, no_tts=args.no_tts)
register_stop_assistant(assistant.shutdown)
assistant.start()
app.exec()
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ indent-style = "space"
line-ending = "auto"
docstring-code-format = true

[tool.pytest.ini_options]
pythonpath = ["."]

[dependency-groups]
dev = [
"ruff>=0.16.9",
Expand Down
19 changes: 19 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Centralized configuration management for Desktop assistant."""

import os
from typing import ClassVar

from dotenv import load_dotenv

Expand All @@ -10,10 +11,28 @@
# Base directory for resolving relative paths
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEXT_ONLY_MODE = False
NO_TTS = False


class Config:
"""Configuration settings for the Desktop assistant."""

# Command Execution Safety
COMMAND_SAFETY_CHECK_ENABLED = os.environ.get("COMMAND_SAFETY_CHECK_ENABLED", "true").lower() == "true"
COMMAND_TIMEOUT = int(os.environ.get("COMMAND_TIMEOUT", "30"))
COMMAND_MAX_OUTPUT_CHARS = int(os.environ.get("COMMAND_MAX_OUTPUT_CHARS", "2000"))
DEFAULT_DANGEROUS_PATTERNS: ClassVar[list[str]] = [
r"\brm\s+-(?:[^\s]*r[^\s]*f|[^\s]*f[^\s]*r)\b",
r"\bmkfs(?:\.[\w-]+)?\b",
r"\bdd\b(?:\s+\S+)*\s+\bif\s*=",
r"\bshutdown\b",
r"\bsudo\b",
r"\breboot\b",
r"\bchmod\s+-R\b",
r":\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&?\s*\}\s*;\s*:",
]

# API Keys
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
Expand Down
51 changes: 51 additions & 0 deletions src/core/command_safety.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Validation helpers for potentially dangerous shell commands."""

import re

from src.config import config


def _split_chained_commands(command: str) -> list[str]:
"""Split shell command chains without splitting operators inside quotes."""
segments = []
start = 0
quote = None
escaped = False
index = 0

while index < len(command):
character = command[index]

if escaped:
escaped = False
elif character == "\\" and quote != "'":
escaped = True
elif quote:
if character == quote:
quote = None
elif character in {"'", '"'}:
quote = character
elif command.startswith(("&&", "||"), index):
segments.append(command[start:index])
index += 1
start = index + 1
elif character in {";", "|"}:
segments.append(command[start:index])
start = index + 1

index += 1

segments.append(command[start:])
return segments


def is_dangerous_command(command: str, patterns: list[str] | None = None) -> bool:
"""Return whether a command or any command in its shell chain matches a pattern."""
if patterns is None:
patterns = config.DEFAULT_DANGEROUS_PATTERNS

compiled_patterns = [re.compile(pattern, re.IGNORECASE) for pattern in patterns]
if any(pattern.search(command) for pattern in compiled_patterns):
return True

return any(pattern.search(segment) for segment in _split_chained_commands(command) for pattern in compiled_patterns)
42 changes: 40 additions & 2 deletions src/core/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import contextlib
import functools
import os
import signal
import subprocess
from typing import Literal

Expand All @@ -10,6 +13,7 @@
from PIL import ImageGrab

from src.config import config
from src.core.command_safety import is_dangerous_command
from src.ui.overlay import overlay
from src.utils.logger import get_logger

Expand Down Expand Up @@ -75,7 +79,39 @@ def wrapper(*args, **kwargs):
@tool
def run_command(command: str) -> str:
"""Run a shell command on the local Linux machine."""
return subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
if (
config.COMMAND_SAFETY_CHECK_ENABLED
and is_dangerous_command(command)
and not overlay.request_command_confirmation(command)
):
return "Execution cancelled by user"

process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
start_new_session=os.name == "posix",
)
try:
output, _ = process.communicate(timeout=config.COMMAND_TIMEOUT)
except subprocess.TimeoutExpired:
if os.name == "posix":
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL)
else:
process.kill()
process.communicate()
return f"Command timed out after {config.COMMAND_TIMEOUT} seconds."

if process.returncode:
raise subprocess.CalledProcessError(process.returncode, command, output=output)

max_output_chars = max(0, config.COMMAND_MAX_OUTPUT_CHARS)
if len(output) > max_output_chars:
return output[:max_output_chars] + "[Output truncated...]"
return output


@tool
Expand Down Expand Up @@ -196,11 +232,13 @@ def mirror_mobile(
if source == "screen":
subprocess.Popen("scrcpy", shell=True)
return "Starting mobile screen mirroring using scrcpy."

if source == "camera":
if not camera_facing or camera_facing not in ["front", "back"]:
if camera_facing not in ("front", "back"):
camera_facing = "back"
subprocess.Popen(["scrcpy", "--video-source=camera", f"--camera-facing={camera_facing}"])
return f"Starting mobile camera {camera_facing} mirroring using scrcpy."

return "Invalid source. Use 'screen' or 'camera'."


Expand Down
Loading
Loading