Skip to content

Add ShellCommandExecutor for running shell commands directly #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

l33tdawg
Copy link

This PR adds a new ShellCommandExecutor class that provides a safe, efficient way to execute shell commands directly from ADK applications. This executor follows the established ADK executor pattern while providing specific functionality for shell commands.

Motivation

When building AI assistants that need to interact with the system, having a dedicated executor for shell commands makes the code cleaner and safer than building custom solutions. The current UnsafeLocalCodeExecutor is designed for running Python code, but not optimized for shell commands.

Implementation Details

The implementation:

  • Inherits from BaseCodeExecutor to maintain consistent API
  • Properly handles command timeouts with graceful termination
  • Provides detailed error reporting and exit code handling
  • Handles multi-line commands
  • Safely parses shell commands with proper argument handling
  • Returns a standardized CodeExecutionResult object

Usage Example

from google.adk.code_executors import ShellCommandExecutor

# Initialize the executor

executor = ShellCommandExecutor(timeout=30)

# Execute a command

result = await executor.execute_code(code="ls -la")

# Check execution result

if result.success:
    print(f"Command output: {result.output}")
else:
    print(f"Command failed: {result.error}")

# Alternative direct execution

stdout, stderr, returncode = await executor.execute("ls -la")

Testing Done

The executor has been tested with:

  • Simple commands like ls, echo, cat
  • Commands with complex arguments and quoting
  • Commands that time out
  • Commands that produce errors

Related Issue

This implementation addresses the need for a standardized way to execute shell commands within the ADK framework. %

Copy link

google-cla bot commented Apr 13, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant