feat: agent support tools.txt (#64)

This commit is contained in:
sigoden
2024-07-08 19:52:01 +08:00
committed by GitHub
parent fd5f5418f3
commit b0e18e29ba
3 changed files with 60 additions and 25 deletions
+4 -6
View File
@@ -1,16 +1,14 @@
/tmp /tmp
functions.txt /tools.txt
tools.txt /agents.txt
agents.txt
functions.json functions.json
/bin /bin
/cache /cache
/tools/test.* /tools/test.*
/.env /.env
*.cmd
__pycache__ __pycache__
/venv /venv
node_modules node_modules
package.json /package.json
package-lock.json /package-lock.json
*.lock *.lock
+48 -12
View File
@@ -102,7 +102,7 @@ build-bin@tool() {
fi fi
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input tools, not found '$argc_names_file', please create it add some tools." _die "error: no tools provided. '$argc_names_file' is missing. please create it and add some tools."
fi fi
not_found_tools=() not_found_tools=()
for name in "${names[@]}"; do for name in "${names[@]}"; do
@@ -139,7 +139,7 @@ build-declarations@tool() {
names=($(cat "$argc_names_file" | grep -v '^#')) names=($(cat "$argc_names_file" | grep -v '^#'))
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input tools, not found '$argc_names_file', please create it add some tools." _die "error: no tools provided. '$argc_names_file' is missing. please create it and add some tools."
fi fi
json_list=() json_list=()
not_found_tools=() not_found_tools=()
@@ -162,8 +162,13 @@ build-declarations@tool() {
if [[ -n "$build_failed_tools" ]]; then if [[ -n "$build_failed_tools" ]]; then
_die "error: invalid tools: ${build_failed_tools[*]}" _die "error: invalid tools: ${build_failed_tools[*]}"
fi fi
json_data="$(echo "["$(IFS=,; echo "${json_list[*]}")"]" | jq '.')"
if [[ "$argc_declarations_file" == "-" ]]; then
echo "$json_data"
else
echo "Build $argc_declarations_file" echo "Build $argc_declarations_file"
echo "["$(IFS=,; echo "${json_list[*]}")"]" | jq '.' > "$argc_declarations_file" echo "$json_data" > "$argc_declarations_file"
fi
} }
@@ -210,7 +215,7 @@ build-bin@agent() {
fi fi
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input agents, not found '$argc_names_file', please create it add some tools." _die "error: no agents provided. '$argc_names_file' is missing. please create it and add some agents."
fi fi
not_found_agents=() not_found_agents=()
for name in "${names[@]}"; do for name in "${names[@]}"; do
@@ -229,6 +234,11 @@ build-bin@agent() {
ln -s -f "$PWD/scripts/run-agent.$lang" "$bin_file" ln -s -f "$PWD/scripts/run-agent.$lang" "$bin_file"
fi fi
echo "Build agent $name" echo "Build agent $name"
tool_names_file="$agent_dir/tools.txt"
if [[ -f "$tool_names_file" ]]; then
argc build-bin@tool --names-file "${tool_names_file}"
fi
break
fi fi
done done
if [[ "$found" == "false" ]] && [[ ! -d "$agent_dir" ]]; then if [[ "$found" == "false" ]] && [[ ! -d "$agent_dir" ]]; then
@@ -251,28 +261,54 @@ build-declarations@agent() {
names=($(cat "$argc_names_file" | grep -v '^#')) names=($(cat "$argc_names_file" | grep -v '^#'))
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input agents, not found '$argc_names_file', please create it add some tools." _die "error: no agents provided. '$argc_names_file' is missing. please create it and add some agents."
fi fi
not_found_agents=() not_found_agents=()
build_failed_agents=() build_failed_agents=()
for name in "${names[@]}"; do for name in "${names[@]}"; do
agent_dir="agents/$name" agent_dir="agents/$name"
build_ok=false declarations_file="$agent_dir/functions.json"
tool_names_file="$agent_dir/tools.txt"
rm -rf "$declarations_file"
found=false found=false
if [[ -d "$agent_dir" ]]; then
found=true
ok=true
json_data=""
agent_json_data=""
tools_json_data=""
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"
agent_tools_file="$agent_dir/tools.$lang" agent_tools_file="$agent_dir/tools.$lang"
if [[ -f "$agent_tools_file" ]]; then if [[ -f "$agent_tools_file" ]]; then
found=true agent_json_data="$(generate-declarations@agent "$name")" || {
json_data="$(generate-declarations@agent "$name")" || { ok=false
build_failed_agents+=("$name") build_failed_agents+=("$name")
} }
declarations_file="$agent_dir/functions.json" break
fi
done
if [[ -f "$tool_names_file" ]]; then
tools_json_data="$(argc build-declarations@tool --names-file="$tool_names_file" --declarations-file=-)" || {
ok=false
build_failed_agents+=("$name")
}
fi
if [[ "$ok" == "true" ]]; then
if [[ -n "$agent_json_data" ]] && [[ -n "$tools_json_data" ]]; then
json_data="$(jq -s '.[0] + .[1]' <(echo "$agent_json_data") <(echo "$tools_json_data"))"
elif [[ -n "$agent_json_data" ]]; then
json_data="$agent_json_data"
elif [[ -n "$tools_json_data" ]]; then
json_data="$tools_json_data"
fi
if [[ -n "$json_data" ]]; then
echo "Build $declarations_file" echo "Build $declarations_file"
echo "$json_data" > "$declarations_file" echo "$json_data" > "$declarations_file"
fi fi
done fi
if [[ "$found" == "false" ]] && [[ ! -d "$agent_dir" ]]; then fi
if [[ "$found" == "false" ]]; then
not_found_agents+=("$name") not_found_agents+=("$name")
fi fi
done done
@@ -295,7 +331,7 @@ generate-declarations@agent() {
fi fi
lang="${tools_path##*.}" lang="${tools_path##*.}"
cmd="$(_lang_to_cmd "$lang")" cmd="$(_lang_to_cmd "$lang")"
json="$("$cmd" "scripts/build-declarations.$lang" "$tools_path")" json="$("$cmd" "scripts/build-declarations.$lang" "$tools_path" | jq --arg agent "$1" 'map(. + {agent: $agent})')"
if [[ -n "$argc_oneline" ]]; then if [[ -n "$argc_oneline" ]]; then
echo "$json" | jq -r '.[] | .name + ": " + (.description | split("\n"))[0]' echo "$json" | jq -r '.[] | .name + ": " + (.description | split("\n"))[0]'
else else
+1
View File
@@ -125,6 +125,7 @@ The agent has the following folder structure:
└── myagent └── myagent
├── functions.json # Function declarations file (Auto-generated) ├── functions.json # Function declarations file (Auto-generated)
├── index.yaml # Agent definition file ├── index.yaml # Agent definition file
├── tools.txt # Reuse tools
└── tools.{sh,js,py} # Agent tools script └── tools.{sh,js,py} # Agent tools script
``` ```