refactor: minor refinement

This commit is contained in:
sigoden
2024-05-16 07:13:07 +00:00
parent d346fe5874
commit 4d0a823257
3 changed files with 49 additions and 29 deletions
+28 -25
View File
@@ -45,7 +45,34 @@ build-declarations-json() {
# @cmd Build declaration for a single function # @cmd Build declaration for a single function
# @arg func![`_choice_func`] The function name # @arg func![`_choice_func`] The function name
build-func-declaration() { build-func-declaration() {
argc --argc-export bin/$1 | \ argc --argc-export bin/$1 | _parse_declaration
}
# @cmd Build shims for the functions
# Because Windows OS can't run bash scripts directly, we need to make a shim for each function
#
# @flag --clear Clear the shims
build-win-shims() {
funcs=($(_choice_func))
for func in "${funcs[@]}"; do
echo "Shim bin/${func}.cmd"
_win_shim > "bin/${func}.cmd"
done
}
# @cmd Install this repo to aichat functions_dir
install() {
functions_dir="$(aichat --info | grep functions_dir | awk '{print $2}')"
if [[ ! -e "$functions_dir" ]]; then
ln -s "$(pwd)" "$functions_dir"
echo "$functions_dir symlinked"
else
echo "$functions_dir already exists"
fi
}
_parse_declaration() {
jq -r ' jq -r '
def parse_description(flag_option): def parse_description(flag_option):
if flag_option.describe == "" then if flag_option.describe == "" then
@@ -88,30 +115,6 @@ build-func-declaration() {
}' }'
} }
# @cmd Build shims for the functions
# Because Windows OS can't run bash scripts directly, we need to make a shim for each function
#
# @flag --clear Clear the shims
build-win-shims() {
funcs=($(_choice_func))
for func in "${funcs[@]}"; do
echo "Shim bin/${func}.cmd"
_win_shim > "bin/${func}.cmd"
done
}
# @cmd Install this repo to aichat functions_dir
install() {
functions_dir="$(aichat --info | grep functions_dir | awk '{print $2}')"
if [[ ! -e "$functions_dir" ]]; then
ln -s "$(pwd)" "$functions_dir"
echo "$functions_dir symlinked"
else
echo "$functions_dir already exists"
fi
}
_win_shim() { _win_shim() {
cat <<-'EOF' cat <<-'EOF'
@echo off @echo off
+12
View File
@@ -71,6 +71,18 @@ main() {
eval "$(argc --argc-eval "$0" "$@")" eval "$(argc --argc-eval "$0" "$@")"
``` ```
The relationship between flags/options and parameters in function declarations is as follows:
```sh
# @flag --boolean Parameter `{"type": "boolean"}`
# @option --string Parameter `{"type": "string"}`
# @option --string-enum[foo|bar] Parameter `{"type": "string", "enum": ["foo", "bar"]}`
# @option --integer <INT> Parameter `{"type": "integer"}`
# @option --number <NUM> Parameter `{"type": "number"}`
# @option --array* <VALUE> Parameter `{"type": "array", "items": {"type":"string"}}`
# @option --scalar-required! Use `!` to mark a scalar parameter as required.
# @option --array-required+ Use `+` to mark a array parameter as required
```
**After creating your function, don't forget to rebuild the function declarations.** **After creating your function, don't forget to rebuild the function declarations.**
+9 -4
View File
@@ -1,14 +1,19 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# @describe Use this function to search DuckDuckGo for a query. # @describe Takes in a query string and returns search result.
# Use it to answer user questions that require dates, facts, real-time information, or news.
# This ensures accurate and up-to-date answers.
# @meta require-tools ddgr # @meta require-tools ddgr
# @env DDG_MAX_RESULTS=5 The max results to return.
# @option --query! The query to search for. # @option --query! The query to search for.
# @option --max-results=5 The number of returned results.
main() { main() {
ddgr --num $argc_max_results --json "$argc_query" | \ ddgr -n $DDG_MAX_RESULTS --json "$argc_query" | \
jq -r '.[] | "**[\(.title)](\(.url))**\n\(.abstract)\n"' jq -r '. as $input |
reduce range(0; length) as $i ([]; . + [ $input[$i] | .index = $i ]) |
.[] | "### \(.index+1). \u001b]8;;\(.url)\u001b\\\(.title)\u001b]8;;\u001b\\\n\(.abstract)\n"
'
} }
eval "$(argc --argc-eval "$0" "$@")" eval "$(argc --argc-eval "$0" "$@")"