aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-08-17 08:14:10 +0800
committerGitHub <noreply@github.com>2024-08-17 08:14:10 +0800
commitd3930a6e21210617642f3d6b2a6774e58b7e9fa8 (patch)
tree5cb28a059e4a41b90d11e0b500ca2f5ff94bb642
parentcdcb8aadd9bc0cd5cee31c2d904a54bd630d171b (diff)
downloadllm-functions-docker-d3930a6e21210617642f3d6b2a6774e58b7e9fa8.tar.gz
refactor: improve scripts/run-{tool,agent}.sh (#95)
-rwxr-xr-xscripts/run-agent.sh43
-rwxr-xr-xscripts/run-tool.sh43
2 files changed, 44 insertions, 42 deletions
diff --git a/scripts/run-agent.sh b/scripts/run-agent.sh
index 8694779..f52a4e6 100755
--- a/scripts/run-agent.sh
+++ b/scripts/run-agent.sh
@@ -60,34 +60,35 @@ run() {
tools_path="$(cygpath -w "$tools_path")"
fi
- data="$(
- echo "$agent_data" | \
- jq -r '
- to_entries | .[] |
- (.key | split("_") | join("-")) as $key |
- if .value | type == "array" then
- .value | .[] | "--\($key)\n\(. | @json)"
- elif .value | type == "boolean" then
- if .value then "--\($key)" else "" end
- else
- "--\($key)\n\(.value | @json)"
- end'
- )" || {
+ jq_script="$(cat <<-'EOF'
+def escape_shell_word:
+ tostring
+ | gsub("'"; "'\"'\"'")
+ | gsub("\n"; "'$'\\n''")
+ | "'\(.)'";
+def to_args:
+ to_entries | .[] |
+ (.key | split("_") | join("-")) as $key |
+ if .value | type == "array" then
+ .value | .[] | "--\($key) \(. | escape_shell_word)"
+ elif .value | type == "boolean" then
+ if .value then "--\($key)" else "" end
+ else
+ "--\($key) \(.value | escape_shell_word)"
+ end;
+[ to_args ] | join(" ")
+EOF
+)"
+ args="$(echo "$tool_data" | jq -r "$jq_script")" || {
die "Invalid JSON data"
}
- while IFS= read -r line; do
- if [[ "$line" == '--'* ]]; then
- args+=("$line")
- else
- args+=("$(echo "$line" | jq -r '.')")
- fi
- done <<< "$data"
+
no_llm_output=0
if [[ -z "$LLM_OUTPUT" ]]; then
no_llm_output=1
export LLM_OUTPUT="$(mktemp)"
fi
- "$tools_path" "$agent_func" "${args[@]}"
+ eval "'$tools_path' '$agent_func' $args"
if [[ "$no_llm_output" -eq 1 ]]; then
cat "$LLM_OUTPUT"
fi
diff --git a/scripts/run-tool.sh b/scripts/run-tool.sh
index 26d3e83..ecbd372 100755
--- a/scripts/run-tool.sh
+++ b/scripts/run-tool.sh
@@ -57,34 +57,35 @@ run() {
tool_path="$(cygpath -w "$tool_path")"
fi
- data="$(
- echo "$tool_data" | \
- jq -r '
- to_entries | .[] |
- (.key | split("_") | join("-")) as $key |
- if .value | type == "array" then
- .value | .[] | "--\($key)\n\(. | @json)"
- elif .value | type == "boolean" then
- if .value then "--\($key)" else "" end
- else
- "--\($key)\n\(.value | @json)"
- end'
- )" || {
+ jq_script="$(cat <<-'EOF'
+def escape_shell_word:
+ tostring
+ | gsub("'"; "'\"'\"'")
+ | gsub("\n"; "'$'\\n''")
+ | "'\(.)'";
+def to_args:
+ to_entries | .[] |
+ (.key | split("_") | join("-")) as $key |
+ if .value | type == "array" then
+ .value | .[] | "--\($key) \(. | escape_shell_word)"
+ elif .value | type == "boolean" then
+ if .value then "--\($key)" else "" end
+ else
+ "--\($key) \(.value | escape_shell_word)"
+ end;
+[ to_args ] | join(" ")
+EOF
+)"
+ args="$(echo "$tool_data" | jq -r "$jq_script")" || {
die "Invalid JSON data"
}
- while IFS= read -r line; do
- if [[ "$line" == '--'* ]]; then
- args+=("$line")
- else
- args+=("$(echo "$line" | jq -r '.')")
- fi
- done <<< "$data"
+
no_llm_output=0
if [[ -z "$LLM_OUTPUT" ]]; then
no_llm_output=1
export LLM_OUTPUT="$(mktemp)"
fi
- "$tool_path" "${args[@]}"
+ eval "'$tool_path' $args"
if [[ "$no_llm_output" -eq 1 ]]; then
cat "$LLM_OUTPUT"
fi