-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmtools.sh
More file actions
executable file
·349 lines (309 loc) · 13.6 KB
/
dmtools.sh
File metadata and controls
executable file
·349 lines (309 loc) · 13.6 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
# DMTools CLI Wrapper
# Usage: ./dmtools.sh [command] [args...]
# Supports: STDIN, heredoc, file input, and inline JSON
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Brand colors (teal/cyan theme)
# Detect terminal capabilities and use appropriate color mode
# macOS Terminal often doesn't support 24-bit colors, so we check TERM_PROGRAM
USE_24BIT=false
if [ -n "$COLORTERM" ] && [[ "$COLORTERM" == *"truecolor"* ]]; then
USE_24BIT=true
elif [ -n "$TERM_PROGRAM" ] && [[ "$TERM_PROGRAM" != "Apple_Terminal" ]]; then
# For non-Apple terminals, check if 256color is supported
if [ -n "$TERM" ] && [[ "$TERM" == *"256color"* ]]; then
USE_24BIT=true
fi
fi
if [ "$USE_24BIT" = true ]; then
TEAL_LIGHT='\033[38;2;102;208;232m' # #66D0E8 - Light teal
TEAL_DARK='\033[38;2;51;153;204m' # #3399CC - Darker blue-green
else
# Use 256-color palette for better compatibility, or fallback to ANSI
# For macOS Terminal, use bright cyan which looks good
TEAL_LIGHT='\033[1;36m' # Bright cyan (works well in macOS Terminal)
TEAL_DARK='\033[0;36m' # Cyan
fi
CYAN='\033[0;36m'
CYAN_BRIGHT='\033[1;36m'
WHITE='\033[1;37m'
BOLD='\033[1m'
RESET='\033[0m'
# Helper functions
error() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
info() {
echo -e "${GREEN}Info: $1${NC}" >&2
}
# Execute java command with proper stderr handling
execute_java_command() {
local java_cmd="$1"
if [ "$DEBUG" = true ]; then
# Show stderr - execute command directly
eval "$java_cmd"
else
# Suppress stderr
eval "$java_cmd 2>/dev/null"
fi
}
# Load environment variables from various .env files
# Usage: load_env_files [quiet]
# quiet: if set, don't print info messages
load_env_files() {
local quiet_mode="${1:-false}"
local env_files=(
# Current working directory (highest priority)
".env"
"dmtools.env"
"dmtools-local.env"
# Script directory (lower priority)
"$SCRIPT_DIR/.env"
"$SCRIPT_DIR/dmtools.env"
"$SCRIPT_DIR/dmtools-local.env"
)
for env_file in "${env_files[@]}"; do
if [ -f "$env_file" ] && [ -r "$env_file" ]; then
# Check file permissions for security (should not be world-readable for sensitive data)
if [ -f "$env_file" ]; then
# Load the environment file, ignoring comments and empty lines
while IFS= read -r line || [ -n "$line" ]; do
# Skip empty lines and comments
if [[ -n "$line" && ! "$line" =~ ^[[:space:]]*# ]]; then
# Export the variable if it's in KEY=VALUE format
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
# Remove surrounding quotes if present
value=$(echo "$value" | sed -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//")
export "$key"="$value"
fi
fi
done < "$env_file"
if [ "$quiet_mode" != "quiet" ]; then
info "Loaded environment variables from: $env_file"
fi
fi
fi
done
}
# Load environment files silently (will be reloaded with messages when command is executed)
load_env_files quiet
# Try to find JAR file in multiple locations
find_jar_file() {
local jar_file=""
# 1. Check if installed via installer (user's home directory)
if [ -f "$HOME/.dmtools/dmtools.jar" ]; then
jar_file="$HOME/.dmtools/dmtools.jar"
# 2. Check local build directory (development)
elif ls "$SCRIPT_DIR/build/libs"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR/build/libs" -name "*-all.jar" | head -1)
# 3. Check for any JAR file in the script directory
elif ls "$SCRIPT_DIR"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR" -name "dmtools*.jar" -o -name "*-all.jar" | head -1)
# 4. Check parent directory build folder (if script is in subdirectory)
elif ls "$SCRIPT_DIR/../build/libs"/*.jar >/dev/null 2>&1; then
jar_file=$(find "$SCRIPT_DIR/../build/libs" -name "*-all.jar" | head -1)
fi
echo "$jar_file"
}
JAR_FILE=$(find_jar_file)
# Display branded banner
show_banner() {
echo ""
echo -e "${TEAL_LIGHT}╔══════════════════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${TEAL_LIGHT}║${RESET} ${TEAL_LIGHT}║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ██████╗ ███╗ ███╗${TEAL_DARK}████████╗ ██████╗ ██████╗ ██╗ ███████╗${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ██╔══██╗████╗ ████║${TEAL_DARK}╚══██╔══╝██╔═══██╗██╔═══██╗██║ ██╔════╝${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ██║ ██║██╔████╔██║${TEAL_DARK} ██║ ██║ ██║██║ ██║██║ ███████╗${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ██║ ██║██║╚██╔╝██║${TEAL_DARK} ██║ ██║ ██║██║ ██║██║ ██║ ${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ██████╔╝██║ ╚═╝ ██║${TEAL_DARK} ██║ ╚██████╔╝╚██████╔╝███████╗███████║${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} ╚═════╝ ╚═╝ ╚═╝${TEAL_DARK} ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${RESET} ${TEAL_LIGHT}║${RESET}"
echo -e "${TEAL_LIGHT}║${RESET} ${TEAL_LIGHT}║${RESET}"
echo -e "${TEAL_LIGHT}║${WHITE} Is it easier to train ${TEAL_DARK}thousands employees${WHITE} to write perfect ${TEAL_DARK}prompts${WHITE},${TEAL_LIGHT} ║${RESET}"
echo -e "${TEAL_LIGHT}║${RESET} or to build a system of ${TEAL_DARK}expert agents${WHITE} for them to use? ${TEAL_LIGHT}║${RESET}"
echo -e "${TEAL_LIGHT}╚══════════════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
}
usage() {
show_banner
# Check if JAR file exists before calling JobRunner
if [ -z "$JAR_FILE" ] || [ ! -f "$JAR_FILE" ]; then
error "DMTools JAR file not found. Please install DMTools first:
curl https://github.com/IstiN/dmtools/releases/latest/download/install.sh -fsS | bash
Or if you're developing locally, build the project first:
./gradlew build
Note: Java 23 is required for DMTools to run."
fi
execute_java_command "java -Dlog4j2.configurationFile=classpath:log4j2-cli.xml -Dlog4j.configuration=log4j2-cli.xml --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp \"$JAR_FILE\" com.github.istin.dmtools.job.JobRunner"
exit 0
}
# Parse arguments
if [ $# -eq 0 ]; then
show_banner
exit 0
fi
# Reload environment files with messages when executing commands
load_env_files
# Initialize variables for argument parsing
ARGS=()
DATA=""
FILE=""
QUIET=true
DEBUG=false
COMMAND=""
# Parse all arguments first to handle flags
while [[ $# -gt 0 ]]; do
case $1 in
--data)
DATA="$2"
shift 2
;;
--file)
FILE="$2"
shift 2
;;
--verbose)
QUIET=false
shift
;;
--debug)
QUIET=false
DEBUG=true
shift
;;
--quiet)
QUIET=true
shift
;;
*)
if [ -z "$COMMAND" ]; then
COMMAND="$1"
else
ARGS+=("$1")
fi
shift
;;
esac
done
# Validate command exists
if [ -z "$COMMAND" ]; then
echo "DMTools CLI - Use 'dmtools help' or 'dmtools --help' for usage information"
exit 0
fi
# Handle special commands
case "$COMMAND" in
"help"|"-h"|"--help")
usage
;;
"list")
if [ $# -gt 0 ]; then
# List with filter
execute_java_command "java -Dlog4j2.configurationFile=classpath:log4j2-cli.xml -Dlog4j.configuration=log4j2-cli.xml --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp \"$JAR_FILE\" com.github.istin.dmtools.job.JobRunner mcp list \"$1\""
else
# List all tools
execute_java_command "java -Dlog4j2.configurationFile=classpath:log4j2-cli.xml -Dlog4j.configuration=log4j2-cli.xml --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp \"$JAR_FILE\" com.github.istin.dmtools.job.JobRunner mcp list"
fi
exit 0
;;
"run")
# Handle new run command with JSON file + optional encoded parameter
if [ ${#ARGS[@]} -lt 1 ]; then
error "Run command requires at least one argument: json-file-path"
fi
JSON_FILE="${ARGS[0]}"
ENCODED_PARAM="${ARGS[1]:-}"
# Validate file exists and is readable
if [ ! -f "$JSON_FILE" ]; then
error "Configuration file not found: $JSON_FILE"
fi
if [ ! -r "$JSON_FILE" ]; then
error "Configuration file is not readable: $JSON_FILE"
fi
# Execute run command with JobRunner
if [ -n "$ENCODED_PARAM" ]; then
info "Executing job with file: $JSON_FILE and encoded parameter"
execute_java_command "java -cp \"$JAR_FILE\" com.github.istin.dmtools.job.JobRunner run \"$JSON_FILE\" \"$ENCODED_PARAM\""
else
info "Executing job with file: $JSON_FILE"
execute_java_command "java -cp \"$JAR_FILE\" com.github.istin.dmtools.job.JobRunner run \"$JSON_FILE\""
fi
exit $?
;;
esac
# Check if JAR file exists
if [ -z "$JAR_FILE" ] || [ ! -f "$JAR_FILE" ]; then
error "DMTools JAR file not found. Please install DMTools first:
curl https://github.com/IstiN/dmtools/releases/latest/download/install.sh -fsS | bash
Or if you're developing locally, build the project first:
./gradlew build
Note: Java 23 is required for DMTools to run."
fi
# Determine log configuration based on debug mode
if [ "$DEBUG" = true ]; then
LOG_CONFIG="log4j2-debug.xml"
else
LOG_CONFIG="log4j2-cli.xml"
fi
# Check if command starts with - or --, then proxy directly to JobRunner
if [[ "$COMMAND" == -* ]]; then
# Direct proxy to JobRunner for flags like --version, --help, etc.
CMD_ARGS=("$COMMAND")
if [ ${#ARGS[@]} -gt 0 ]; then
CMD_ARGS+=("${ARGS[@]}")
fi
# Execute directly without mcp prefix
if [ "$DEBUG" = true ]; then
java -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner "${CMD_ARGS[@]}"
else
java -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner "${CMD_ARGS[@]}" 2>/dev/null
fi
exit $?
fi
# Proxy all other commands to JobRunner as MCP tools
# Build command arguments array
CMD_ARGS=("$COMMAND")
if [ ${#ARGS[@]} -gt 0 ]; then
CMD_ARGS+=("${ARGS[@]}")
fi
# Check if STDIN has data (heredoc/pipe)
if [ ! -t 0 ]; then
STDIN_DATA=$(cat)
if [ -n "$STDIN_DATA" ]; then
[ "$QUIET" = false ] && info "Reading JSON from STDIN (${#STDIN_DATA} characters)"
CMD_ARGS+=("--stdin-data" "$STDIN_DATA")
fi
fi
# Add --data or --file flags if provided
if [ -n "$DATA" ]; then
[ "$QUIET" = false ] && info "Using inline data"
CMD_ARGS+=("--data" "$DATA")
elif [ -n "$FILE" ]; then
if [ ! -f "$FILE" ]; then
error "File not found: $FILE"
fi
[ "$QUIET" = false ] && info "Using file data: $FILE"
FILE_DATA=$(cat "$FILE")
CMD_ARGS+=("--data" "$FILE_DATA")
fi
# Add verbose/debug flags
if [ "$QUIET" = false ]; then
CMD_ARGS+=("--verbose")
fi
if [ "$DEBUG" = true ]; then
CMD_ARGS+=("--debug")
fi
# Execute command via JobRunner as MCP tool
if [ "$DEBUG" = true ]; then
java -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner mcp "${CMD_ARGS[@]}"
else
java -Dlog4j2.configurationFile=classpath:$LOG_CONFIG -Dlog4j.configuration=$LOG_CONFIG --add-opens java.base/java.lang=ALL-UNNAMED -XX:-PrintWarnings -cp "$JAR_FILE" com.github.istin.dmtools.job.JobRunner mcp "${CMD_ARGS[@]}" 2>/dev/null
fi