-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_run.sh
More file actions
executable file
·191 lines (166 loc) · 6.44 KB
/
docker_run.sh
File metadata and controls
executable file
·191 lines (166 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/bin/bash
# Run GBox CUA Agent in Docker container
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Default values
IMAGE_NAME="gbox-cua"
IMAGE_TAG="latest"
CONTAINER_NAME="gbox-cua-container"
# Load environment variables from .env file
# 1) Prefer .env in this repo (SCRIPT_DIR/.env)
# 2) Fallback to parent directory .env (useful when sharing config with a parent project)
ENV_FILE="$SCRIPT_DIR/.env"
PARENT_ENV_FILE="$(cd "$SCRIPT_DIR/.." && pwd)/.env"
if [ -f "$ENV_FILE" ]; then
echo -e "${GREEN}Loading environment variables from $ENV_FILE${NC}"
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
elif [ -f "$PARENT_ENV_FILE" ]; then
echo -e "${GREEN}Loading environment variables from parent $PARENT_ENV_FILE${NC}"
set -a
# shellcheck disable=SC1090
source "$PARENT_ENV_FILE"
set +a
else
echo -e "${YELLOW}Warning: .env file not found at $ENV_FILE or $PARENT_ENV_FILE${NC}"
echo -e "${YELLOW}Please copy env.example to .env and configure it in this repo:${NC}"
echo -e "${YELLOW} cp $SCRIPT_DIR/env.example $SCRIPT_DIR/.env${NC}"
echo ""
echo -e "${YELLOW}Continuing with environment variables from current shell...${NC}"
fi
# Check required environment variables
REQUIRED_VARS=("GBOX_API_KEY")
MISSING_VARS=()
for var in "${REQUIRED_VARS[@]}"; do
if [ -z "${!var}" ]; then
MISSING_VARS+=("$var")
fi
done
if [ ${#MISSING_VARS[@]} -ne 0 ]; then
echo -e "${RED}Error: Missing required environment variables:${NC}"
for var in "${MISSING_VARS[@]}"; do
echo -e "${RED} - $var${NC}"
done
echo ""
echo -e "${YELLOW}Please set these variables in .env file or export them:${NC}"
echo -e "${YELLOW} export GBOX_API_KEY=your_key${NC}"
exit 1
fi
# Check VLM provider configuration
if [ "$VLM_PROVIDER" = "openai" ] && [ -z "$OPENAI_API_KEY" ]; then
echo -e "${RED}Error: OPENAI_API_KEY is required when VLM_PROVIDER=openai${NC}"
exit 1
elif [ "$VLM_PROVIDER" = "openrouter" ] && [ -z "$OPENROUTER_API_KEY" ]; then
echo -e "${RED}Error: OPENROUTER_API_KEY is required when VLM_PROVIDER=openrouter${NC}"
exit 1
elif [ "$VLM_PROVIDER" = "vllm" ] && [ -z "$VLLM_API_BASE" ]; then
echo -e "${RED}Error: VLLM_API_BASE is required when VLM_PROVIDER=vllm${NC}"
exit 1
fi
# Check if image exists
if ! docker image inspect "${IMAGE_NAME}:${IMAGE_TAG}" >/dev/null 2>&1; then
echo -e "${YELLOW}Docker image ${IMAGE_NAME}:${IMAGE_TAG} not found.${NC}"
echo -e "${YELLOW}Building image...${NC}"
"$SCRIPT_DIR/build_docker.sh"
fi
# Stop and remove existing container if it exists
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo -e "${YELLOW}Stopping and removing existing container...${NC}"
docker stop "$CONTAINER_NAME" >/dev/null 2>&1 || true
docker rm "$CONTAINER_NAME" >/dev/null 2>&1 || true
fi
# Prepare Docker command arguments
DOCKER_ARGS=(
run
--name "$CONTAINER_NAME"
--rm
--network host
)
# Add interactive/TTY flags only if running in a terminal
if [ -t 0 ] && [ -t 1 ]; then
DOCKER_ARGS+=(-it)
else
DOCKER_ARGS+=(-i)
fi
# Add environment variables
DOCKER_ARGS+=(-e GBOX_API_KEY="${GBOX_API_KEY}")
DOCKER_ARGS+=(-e VLM_PROVIDER="${VLM_PROVIDER:-openai}")
if [ -n "$OPENAI_API_KEY" ]; then
DOCKER_ARGS+=(-e OPENAI_API_KEY="${OPENAI_API_KEY}")
fi
if [ -n "$OPENAI_API_BASE" ]; then
DOCKER_ARGS+=(-e OPENAI_API_BASE="${OPENAI_API_BASE}")
fi
if [ -n "$OPENROUTER_API_KEY" ]; then
DOCKER_ARGS+=(-e OPENROUTER_API_KEY="${OPENROUTER_API_KEY}")
fi
if [ -n "$OPENROUTER_API_BASE" ]; then
DOCKER_ARGS+=(-e OPENROUTER_API_BASE="${OPENROUTER_API_BASE}")
fi
if [ -n "$VLLM_API_BASE" ]; then
DOCKER_ARGS+=(-e VLLM_API_BASE="${VLLM_API_BASE}")
fi
if [ -n "$VLLM_API_KEY" ]; then
DOCKER_ARGS+=(-e VLLM_API_KEY="${VLLM_API_KEY}")
fi
# Add optional environment variables
[ -n "$MODEL_NAME" ] && DOCKER_ARGS+=(-e MODEL_NAME="${MODEL_NAME}")
[ -n "$MAX_TOKENS" ] && DOCKER_ARGS+=(-e MAX_TOKENS="${MAX_TOKENS}")
[ -n "$TEMPERATURE" ] && DOCKER_ARGS+=(-e TEMPERATURE="${TEMPERATURE}")
[ -n "$TOP_P" ] && DOCKER_ARGS+=(-e TOP_P="${TOP_P}")
[ -n "$MAX_TURNS" ] && DOCKER_ARGS+=(-e MAX_TURNS="${MAX_TURNS}")
[ -n "$BOX_TYPE" ] && DOCKER_ARGS+=(-e BOX_TYPE="${BOX_TYPE}")
[ -n "$LOG_LEVEL" ] && DOCKER_ARGS+=(-e LOG_LEVEL="${LOG_LEVEL}")
# Mount volumes
DOCKER_ARGS+=(-v "$SCRIPT_DIR/logs:/app/logs")
# Image name
DOCKER_ARGS+=("${IMAGE_NAME}:${IMAGE_TAG}")
# Command arguments (can be overridden by command line arguments)
CMD_ARGS=()
# Get task from environment or command line
TASK_ARG="${TASK:-Open the Settings app}"
# If command line arguments provided, use them; otherwise use environment variables
if [ $# -gt 0 ]; then
# If first argument is a single word and looks like a task, combine all args as task
# Otherwise, use all args as-is
if [ $# -eq 1 ] || [[ "$1" =~ ^[a-zA-Z]+$ ]]; then
# Single word or starts with a word - might be a task, combine all args
CMD_ARGS=("$@")
else
# Multiple args, use as-is
CMD_ARGS=("$@")
fi
else
# Use TASK from env, ensuring it's properly quoted if it contains spaces
CMD_ARGS=("$TASK_ARG")
[ "$VERBOSE" = "true" ] && CMD_ARGS+=(--verbose)
[ -n "$BOX_TYPE" ] && CMD_ARGS+=(--box-type "$BOX_TYPE")
[ -n "$MODEL_NAME" ] && CMD_ARGS+=(--model "$MODEL_NAME")
[ -n "$VLM_PROVIDER" ] && CMD_ARGS+=(--vlm-provider "$VLM_PROVIDER")
[ -n "$VLLM_API_BASE" ] && CMD_ARGS+=(--vllm-api-base "$VLLM_API_BASE")
[ -n "$OPENAI_API_KEY" ] && CMD_ARGS+=(--openai-api-key "$OPENAI_API_KEY")
[ -n "$OPENROUTER_API_KEY" ] && CMD_ARGS+=(--openrouter-api-key "$OPENROUTER_API_KEY")
fi
# Print configuration
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}GBox CUA Agent - Docker Run${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}Image: ${IMAGE_NAME}:${IMAGE_TAG}${NC}"
echo -e "${GREEN}Container: ${CONTAINER_NAME}${NC}"
echo -e "${GREEN}VLM Provider: ${VLM_PROVIDER:-openai}${NC}"
echo -e "${GREEN}Model: ${MODEL_NAME:-gpt-4o}${NC}"
echo -e "${GREEN}Task: ${CMD_ARGS[0]:-${TASK_ARG}}${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Run the container
echo -e "${GREEN}Starting container...${NC}"
docker "${DOCKER_ARGS[@]}" gbox-cua "${CMD_ARGS[@]}"