Agent Zero AI framework
Docker runtime in progress
frdel7fc17b39c52b
diff --git a/docker/docker_manager_macos.sh b/docker/docker_manager_macos.shnew file mode 100644index 000000000..603e12013--- /dev/null+++ b/docker/docker_manager_macos.sh@@ -0,0 +1,137 @@+#!/bin/bash++# Constants+IMAGE_NAME="agent-zero-run"+DEFAULT_CONTAINER_NAME="agent-zero"+BASE_PORT=50080++# Global variable for container IDs+container_ids=()++# Function to list unique containers based on the specified image+list_containers() {+ echo+ echo "Listing all containers using image '$IMAGE_NAME':"+ container_ids=() # Reset global array for each list update+ container_list=$(docker ps -a --filter "ancestor=$IMAGE_NAME" --format "{{.ID}} {{.Names}} {{.Ports}}" | sort -u -k2,2)++ if [ -z "$container_list" ]; then+ echo "No containers found for image '$IMAGE_NAME'."+ return 1+ fi++ index=1+ printed_ids=()+ while IFS= read -r line; do+ container_id=$(echo "$line" | awk '{print $1}')+ container_name=$(echo "$line" | awk '{print $2}')+ ports=$(echo "$line" | awk '{print substr($0, index($0,$3))}')++ # Extract the mapped port using sed+ mapped_port=$(echo "$ports" | sed -n 's/.*0.0.0.0:\([0-9]*\)->80.*/\1/p')+ container_url="http://localhost:${mapped_port}"++ # Check if this ID has already been printed and mapped_port is valid+ if [[ ! " ${printed_ids[@]} " =~ " ${container_id} " ]] && [ -n "$mapped_port" ]; then+ printed_ids+=("$container_id")+ container_ids+=("$container_id") # Store container ID in global array+ printf "%2d. [%s] %s - URL: %s\n" "$index" "$container_id" "$container_name" "$container_url"+ index=$((index + 1))+ fi+ done <<< "$container_list"+ return 0+}++++# Function to select an action for an existing container+manage_container() {+ container_id=$1+ while true; do+ echo -e "\nSelect an action for container ID: $container_id"+ echo "1. Restart"+ echo "2. Remove"+ echo "3. View Logs"+ echo "0. Back"+ read -p "Choose an action (0-3): " action+ case $action in+ 1)+ docker restart "$container_id"+ echo "Container restarted."+ ;;+ 2)+ docker rm -f "$container_id"+ echo "Container removed."+ return # Go back to the list screen after removing+ ;;+ 3)+ echo -e "\n--- Logs for container ID: $container_id ---"+ docker logs "$container_id"+ echo -e "\n--- End of logs ---"+ ;;+ 0)+ return+ ;;+ *)+ echo "Invalid selection. Please choose 0, 1, 2, or 3."+ ;;+ esac+ done+}++# Function to create a new container+create_container() {+ # Generate the default container name+ container_count=$(docker ps -a --filter "name=$DEFAULT_CONTAINER_NAME" --format "{{.Names}}" | wc -l)+ default_name="$DEFAULT_CONTAINER_NAME-$(($container_count + 1))"++ # Prompt for the container name+ read -p "Enter container name or leave empty for '$default_name': " name+ name=${name:-"$default_name"}++ # Calculate default port based on the container count+ default_port=$((BASE_PORT + container_count + 1))+ read -p "Enter web port or leave empty for '$default_port': " web_port+ web_port=${web_port:-$default_port}++ # Prompt for the data folder+ read -p "Enter data folder or leave empty for '$(pwd)/agent-zero': " data_folder+ data_folder=${data_folder:-$(pwd)/agent-zero}++ # Run the container with the specified name, port mapping, and data folder+ container_id=$(docker run -d -p "$web_port:80" --name "$name" -v "$data_folder:/a0" "$IMAGE_NAME:latest")+ echo -e "\nContainer '$name' created with ID '$container_id', data folder '$data_folder', and web port '$web_port' mapped to port 80."+}++# Main program loop+echo -e "\nWelcome to Agent Zero Docker Management Script.\n"+while true; do+ list_containers+ if [ $? -ne 0 ]; then+ echo -e "\nNo containers available. Would you like to create a new one? (y/n)"+ read -p "> " create_new+ if [ "$create_new" == "y" ]; then+ create_container+ else+ exit 0+ fi+ else+ echo -e "\nChoose a container by line number, type 'n' to create a new one, or 'r' to refresh the list:"+ read -p "> " choice++ # Refresh the list if the user inputs 'r'+ if [ "$choice" == "r" ]; then+ continue+ fi++ # Match choice to the actual container list+ if [[ $choice =~ ^[0-9]+$ ]] && ((choice >= 1 && choice <= ${#container_ids[@]})); then+ selected_container_id="${container_ids[$((choice - 1))]}"+ manage_container "$selected_container_id"+ elif [ "$choice" == "n" ]; then+ create_container+ else+ echo "Invalid choice, please enter a valid line number, 'n' to create a new container, or 'r' to refresh the list."+ fi+ fi+donediff --git a/docker/run/Dockerfile b/docker/run/Dockerfileindex 21685d893..b8a529008 100644--- a/docker/run/Dockerfile+++ b/docker/run/Dockerfile@@ -13,8 +13,15 @@ RUN apt-get update && apt-get install -y \npm \openssh-server \sudo \- git \- && rm -rf /var/lib/apt/lists/*+ curl \+ wget \+ git++# Cleanup package list+RUN rm -rf /var/lib/apt/lists/*++# Clone Agent Zero repository+RUN git clone --branch development https://github.com/frdel/agent-zero.git /git/agent-zero# Set up SSHRUN mkdir /var/run/sshd && \@@ -25,18 +32,26 @@ RUN mkdir /var/run/sshd && \ENV VIRTUAL_ENV=/opt/venvRUN python3 -m venv $VIRTUAL_ENV-# Copy initial .bashrc with virtual environment activation to a temporary location-COPY .bashrc /etc/skel/.bashrc-# Copy the script to ensure .bashrc is in the root directoryCOPY initialize.sh /usr/local/bin/initialize.shRUN chmod +x /usr/local/bin/initialize.sh+# Copy contents of filesystem directory to /fs+COPY ./fs/ /fs+# Ensure the virtual environment and pip setupRUN $VIRTUAL_ENV/bin/pip install --upgrade pip-# Expose SSH port-EXPOSE 22+# Install additional python packages+RUN $VIRTUAL_ENV/bin/pip install \+ ipython \+ requests++# Install A0 python packages+RUN $VIRTUAL_ENV/bin/pip install -r /git/agent-zero/requirements.txt++# Expose ports+EXPOSE 22 80# Init .bashrcCMD ["/usr/local/bin/initialize.sh"]\ No newline at end of filediff --git a/docker/run/build.txt b/docker/run/build.txtindex 37d76f0cb..49f29adb8 100644--- a/docker/run/build.txt+++ b/docker/run/build.txt@@ -1 +1,6 @@+# local image+docker build -t agent-zero-run:latest .++# dockerhub+docker logindocker buildx build --platform linux/amd64,linux/arm64 -t frdel/agent-zero-exe:latest --push .\ No newline at end of filediff --git a/docker/run/fs/exe/node_eval.js b/docker/run/fs/exe/node_eval.jsnew file mode 100644index 000000000..18bb773c1--- /dev/null+++ b/docker/run/fs/exe/node_eval.js@@ -0,0 +1,41 @@+#!/usr/bin/env node++const vm = require('vm');+const path = require('path');++// Create a comprehensive context with all important global objects+const context = vm.createContext({+ ...global,+ require: require,+ __filename: path.join(process.cwd(), 'eval.js'),+ __dirname: process.cwd(),+ module: { exports: {} },+ exports: module.exports,+ console: console,+ process: process,+ Buffer: Buffer,+ setTimeout: setTimeout,+ setInterval: setInterval,+ setImmediate: setImmediate,+ clearTimeout: clearTimeout,+ clearInterval: clearInterval,+ clearImmediate: clearImmediate+});++const code = process.argv[2];+const wrappedCode = `+ (async function() {+ try {+ const __result__ = await eval(${JSON.stringify(code)});+ if (__result__ !== undefined) console.log('Out[1]:', __result__);+ } catch (error) {+ console.error(error);+ }+ })();+`;++vm.runInContext(wrappedCode, context, {+ filename: 'eval.js',+ lineOffset: -2,+ columnOffset: 0,+}).catch(console.error);\ No newline at end of filediff --git a/docker/run/fs/exe/run_A0.sh b/docker/run/fs/exe/run_A0.shnew file mode 100644index 000000000..b4ba443d2--- /dev/null+++ b/docker/run/fs/exe/run_A0.sh@@ -0,0 +1,40 @@+#!/bin/bash++# Paths+PYTHON_SCRIPT="/a0/run_ui.py"+SOURCE_DIR="/git/agent-zero"+TARGET_DIR="/a0"+++# Loop to restart the Python script when it finishes+while true; do++ # Create virtual environment if it doesn't exist+ if [ ! -d /opt/venv ]; then+ echo "Creating virtual environment..."+ python3 -m venv /opt/venv+ /opt/venv/bin/pip install ipython requests+ fi++ # Activate the virtual environment+ source /opt/venv/bin/activate++ # Copy repository files if target is empty+ if [ -z "$(ls -A "$TARGET_DIR")" ]; then+ echo "Copying files from $SOURCE_DIR to $TARGET_DIR..."+ cp -rn --no-preserve=ownership,mode "$SOURCE_DIR/" "$TARGET_DIR"+ fi++ echo "Starting A0..."+ python "$PYTHON_SCRIPT" --port 80++ # Check the exit status+ if [ $? -ne 0 ]; then+ echo "A0 script exited with an error. Restarting..."+ else+ echo "A0 script finished. Restarting..."+ fi++ # Optional: Add a small delay if needed to avoid rapid restarts+ sleep 1+donediff --git a/docker/run/.bashrc b/docker/run/fs/root/.bashrcsimilarity index 100%rename from docker/run/.bashrcrename to docker/run/fs/root/.bashrcdiff --git a/docker/run/fs/root/.profile b/docker/run/fs/root/.profilenew file mode 100644index 000000000..88a273c5f--- /dev/null+++ b/docker/run/fs/root/.profile@@ -0,0 +1,9 @@+# .bashrc++# Source global definitions+if [ -f /etc/bashrc ]; then+ . /etc/bashrc+fi++# Activate the virtual environment+source /opt/venv/bin/activatediff --git a/docker/run/initialize.sh b/docker/run/initialize.shindex a0a377698..03ec8da54 100644--- a/docker/run/initialize.sh+++ b/docker/run/initialize.sh@@ -1,18 +1,17 @@#!/bin/bash-# Ensure .bashrc is in the root directory-if [ ! -f /root/.bashrc ]; then- cp /etc/skel/.bashrc /root/.bashrc- chmod 444 /root/.bashrc-fi+# Copy all contents from /fs to root directory (/) without overwriting+cp -rn --no-preserve=ownership,mode /fs/* /-# Ensure .profile is in the root directory-if [ ! -f /root/.profile ]; then- cp /etc/skel/.bashrc /root/.profile- chmod 444 /root/.profile-fi+# allow execution of /root/.bashrc and /root/.profile+chmod 444 /root/.bashrc+chmod 444 /root/.profile+# update package list to save time laterapt-get update+# Start A0+bash /exe/run_A0.sh+# Start SSH service-exec /usr/sbin/sshd -D+exec /usr/sbin/sshd -D\ No newline at end of filediff --git a/run_ui.py b/run_ui.pyindex 9dcfeac8f..806c57b21 100644--- a/run_ui.py+++ b/run_ui.py@@ -1,3 +1,4 @@+import argparseimport jsonfrom functools import wrapsimport os@@ -235,13 +236,13 @@ async def handle_message(sync: bool):attachments = request.files.getlist('attachments')attachment_paths = []- upload_folder = os.path.join(os.getcwd(), 'work_dir', 'uploads')+ upload_folder = files.get_abs_path('work_dir/uploads')if attachments:os.makedirs(upload_folder, exist_ok=True)for attachment in attachments:filename = secure_filename(attachment.filename)- save_path = os.path.join(upload_folder, filename)+ save_path = files.get_abs_path(upload_folder, filename)attachment.save(save_path)attachment_paths.append(save_path)else:@@ -570,8 +571,12 @@ def run():def log_request(self, code="-", size="-"):pass # Override to suppress request logging+ parser = argparse.ArgumentParser()+ parser.add_argument("--port", type=int, default=0, help="Web UI port")+ args = parser.parse_args()+# Get configuration from environment- port = int(os.environ.get("WEB_UI_PORT", 0)) or None+ port = args.port or int(os.environ.get("WEB_UI_PORT", 0)) or Nonehost = os.environ.get("WEB_UI_HOST") or Noneuse_cloudflare = os.environ.get("USE_CLOUDFLARE", "false").lower() == "true"