refactor: improve running tool/agent (#63)

This commit is contained in:
sigoden
2024-07-06 10:35:38 +08:00
committed by GitHub
parent 58d2ee0494
commit 02067cd913
+19 -31
View File
@@ -14,14 +14,11 @@ LANG_CMDS=( \
# @cmd Run the tool # @cmd Run the tool
# @alias tool:run # @alias tool:run
# @arg cmd![`_choice_cmd`] The tool command # @arg tool![`_choice_tool`] The tool name
# @arg json The json data # @arg json The json data
run@tool() { run@tool() {
if _is_win; then
ext=".cmd"
fi
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
declaration="$(jq --arg name "$argc_cmd" '.[] | select(.name == $name)' functions.json)" declaration="$(generate-declarations@tool "$argc_tool" | jq -r '.[0]')"
if [[ -n "$declaration" ]]; then if [[ -n "$declaration" ]]; then
_ask_json_data "$declaration" _ask_json_data "$declaration"
fi fi
@@ -29,31 +26,30 @@ run@tool() {
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
_die "error: no JSON data" _die "error: no JSON data"
fi fi
"$BIN_DIR/$argc_cmd$ext" "$argc_json" lang="${argc_tool##*.}"
cmd="$(_lang_to_cmd "$lang")"
"$cmd" ./scripts/run-tool.$lang "$argc_tool" "$argc_json"
} }
# @cmd Run the agent # @cmd Run the agent
# @alias agent:run # @alias agent:run
# @arg cmd![`_choice_agent`] The agent command # @arg agent![`_choice_agent`] The agent name
# @arg action![`_choice_agent_action`] The agent action # @arg action![`_choice_agent_action`] The agent action
# @arg json The json data # @arg json The json data
run@agent() { run@agent() {
if _is_win; then
ext=".cmd"
fi
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
functions_path="agents/$argc_cmd/functions.json" declaration="$(generate-declarations@agent "$argc_agent" | jq --arg name "$argc_action" '.[] | select(.name == $name)')"
if [[ -f "$functions_path" ]]; then if [[ -n "$declaration" ]]; then
declaration="$(jq --arg name "$argc_action" '.[] | select(.name == $name)' "$functions_path")" _ask_json_data "$declaration"
if [[ -n "$declaration" ]]; then
_ask_json_data "$declaration"
fi
fi fi
fi fi
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
_die "error: no JSON data" _die "error: no JSON data"
fi fi
"$BIN_DIR/$argc_cmd$ext" "$argc_action" "$argc_json" tools_path="$(_get_agent_tools_path "$argc_agent")"
lang="${tools_path##*.}"
cmd="$(_lang_to_cmd "$lang")"
"$cmd" ./scripts/run-agent.$lang "$argc_agent" "$argc_action" "$argc_json"
} }
# @cmd Build the project # @cmd Build the project
@@ -173,7 +169,7 @@ build-declarations@tool() {
# @cmd Generate function declaration for the tool # @cmd Generate function declaration for the tool
# @alias tool:generate-declarations # @alias tool:generate-declarations
# @arg tool![`_choice_tool`] The function name # @arg tool![`_choice_tool`] The tool name
generate-declarations@tool() { generate-declarations@tool() {
lang="${1##*.}" lang="${1##*.}"
cmd="$(_lang_to_cmd "$lang")" cmd="$(_lang_to_cmd "$lang")"
@@ -539,16 +535,12 @@ _ask_json_data() {
echo 'Generate placeholder data:' echo 'Generate placeholder data:'
data="$(echo "$declaration" | _declarations_json_data)" data="$(echo "$declaration" | _declarations_json_data)"
echo "> $data" echo "> $data"
read -r -p 'Use the generated data? (y/n) ' res read -e -r -p 'JSON data (Press ENTER to use placeholder): ' res
case "$res" in if [[ -z "$res" ]]; then
[yY][eE][sS]|[yY])
argc_json="$data" argc_json="$data"
;; else
*) argc_json="$res"
read -r -p "Please enter the data: " data fi
argc_json="$data"
;;
esac
} }
_declarations_json_data() { _declarations_json_data() {
@@ -594,10 +586,6 @@ _choice_agent_action() {
argc generate-declarations@agent "$1" --oneline | sed "$expr" argc generate-declarations@agent "$1" --oneline | sed "$expr"
} }
_choice_cmd() {
ls -1 "$BIN_DIR" | sed -e 's/\.cmd$//'
}
_die() { _die() {
echo "$*" >&2 echo "$*" >&2
exit 1 exit 1