diff options
| author | sigoden <sigoden@gmail.com> | 2024-06-08 19:39:12 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-08 19:39:12 +0800 |
| commit | 82d7a7de8a76e56cff306b0da7f4f14fdb57cbf1 (patch) | |
| tree | 151c719d3ca5e8945b4aa410a7de34e49ebec4f2 | |
| parent | 57e8799d163ba3788d098c20a09dc6cb4cd710a2 (diff) | |
| download | llm-functions-docker-82d7a7de8a76e56cff306b0da7f4f14fdb57cbf1.tar.gz | |
refactor: extract some functions from Argcfile.sh to scripts (#38)
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Argcfile.sh | 64 | ||||
| -rwxr-xr-x[-rw-r--r--] | scripts/build-declarations.js | 0 | ||||
| -rwxr-xr-x[-rw-r--r--] | scripts/build-declarations.py | 0 | ||||
| -rwxr-xr-x | scripts/declarations-util.sh | 133 |
5 files changed, 139 insertions, 60 deletions
@@ -1,4 +1,6 @@ /tmp +functions.txt +bots.txt tools.txt functions.json /bin diff --git a/Argcfile.sh b/Argcfile.sh index 623b6c6..1283a55 100644 --- a/Argcfile.sh +++ b/Argcfile.sh @@ -292,9 +292,9 @@ EOF _ask_json_data() { declaration="$1" echo 'Missing the JSON data but here are its properties:' - echo "$declaration" | _inspect_declaration_params | sed 's/^/> /' + echo "$declaration" | ./scripts/declarations-util.sh pretty-print | sed -n '2,$s/^/>/p' echo 'Generate placeholder data:' - data="$(echo "$declaration" | _generate_data_according_declaration)" + data="$(echo "$declaration" | _declarations_json_data)" echo "> $data" read -r -p 'Use the generated data? (y/n) ' res case "$res" in @@ -308,64 +308,8 @@ _ask_json_data() { esac } -_inspect_declaration_params() { - jq -r ' -def get_indicator: - .value.type as $type | - [ - { condition: ($type == "array" and .required), result: "+" }, - { condition: ($type == "array"), result: "*" }, - { condition: .required, result: "!" }, - { condition: true, result: "" } - ] | map(select(.condition) | .result) | first; - -def get_kind: - .value.type as $type | - (.value.enum // []) as $enum | - ([ - { condition: ($type == "array"), result: "string[]" }, - { condition: ($type == "string" and ($enum | length > 0)), result: ($enum | join("|")) }, - { condition: ($type == "string"), result: "" }, - { condition: true, result: $type } - ] | map(select(.condition) | .result) | first) as $kind | - if $kind != "" then "(\($kind))" else "" end; - -def print_property: - .key as $key | - (.value.description | split("\n")[0]) as $description | - (. | get_kind) as $kind | - (. | get_indicator) as $indicator | - "\($key)\($kind)\($indicator): \($description)"; - -.parameters | -.required as $requiredProperties | -.properties | to_entries[] | -.key as $key | .+ { "required": ($requiredProperties | index($key) != null) } | -print_property -' -} - -_generate_data_according_declaration() { - jq -c ' -def convert_string: - if has("enum") then .enum[0] else "foo" end; - -def convert_property: - .key as $key | - .value.type as $type | - [ - { condition: ($type == "string"), result: { $key: (.value | convert_string) }}, - { condition: ($type == "boolean"), result: { $key: false }}, - { condition: ($type == "integer"), result: { $key: 42 }}, - { condition: ($type == "number"), result: { $key: 3.14 }}, - { condition: ($type == "array"), result: { $key: [ "v1" ] } } - ] | map(select(.condition) | .result) | first; - -.parameters | -[ - .properties | to_entries[] | convert_property -] | add // {} -' +_declarations_json_data() { + ./scripts/declarations-util.sh generate-json | tail -n +2 } _normalize_path() { diff --git a/scripts/build-declarations.js b/scripts/build-declarations.js index 37b840e..37b840e 100644..100755 --- a/scripts/build-declarations.js +++ b/scripts/build-declarations.js diff --git a/scripts/build-declarations.py b/scripts/build-declarations.py index 72b6aff..72b6aff 100644..100755 --- a/scripts/build-declarations.py +++ b/scripts/build-declarations.py diff --git a/scripts/declarations-util.sh b/scripts/declarations-util.sh new file mode 100755 index 0000000..2a0929d --- /dev/null +++ b/scripts/declarations-util.sh @@ -0,0 +1,133 @@ +#!/usr/bin/env bash + +set -e + +# @cmd Pretty print declarations +# +# Examples: +# ./scripts/declarations.sh pretty-print functions.json +# cat functions.json | ./scripts/declarations.sh pretty-print functions.json +# @flag --no-type Do not to display param type info +# @arg json-file The json file, Read stdin if omitted +pretty-print() { + _run _pretty_print +} + +# @cmd Generate placeholder json according to declarations +# Examples: +# ./scripts/declarations.sh generate-json-data functions.json +# cat functions.json | ./scripts/declarations.sh generate-json-data functions.json +# @arg json-file The json file, Read stdin if omitted +generate-json() { + _run _generate_json +} + +_run() { + func="$1" + _get_declarations_data + if [[ "$json_type" == "object" ]]; then + echo "$json_data" | $func + elif [[ "$json_type" == "array" ]]; then + for i in $(seq 1 $json_array_len); do + echo "$json_data" | jq '.['$((i-1))']' | $func + done + fi +} + +_get_declarations_data() { + if [[ -f "$argc_json_file" ]]; then + json_data="$(cat "$argc_json_file")" + else + json_data="$(cat)" + fi + json_type="$(echo "$json_data" | jq -r ' +if type == "array" then + (. | length) as $len | "array;\($len)" +else + if type == "object" then + type + else + "" + end +end +' 2>/dev/null || true)" + if [[ "$json_type" == *object* ]]; then + :; + elif [[ "$json_type" == *array* ]]; then + json_array_len="${json_type#*;}" + json_type="${json_type%%;*}" + if [[ ! "$json_array_len" -gt 0 ]]; then + json_type="" + fi + fi + if [[ -z "$json_type" ]]; then + echo "invalid JSON data" + exit 1 + fi +} + +_pretty_print() { + jq --arg no_type "$argc_no_type" -r ' +def get_type: + .value.type as $type | + (if .required then "" else "?" end) as $symbol | + (.value.enum // []) as $enum | + ([ + { condition: ($type == "array"), result: "string[]" }, + { condition: ($type == "string" and ($enum | length > 0)), result: ($enum | join("|")) }, + { condition: ($type == "string"), result: "" }, + { condition: true, result: $type } + ] | map(select(.condition) | .result) | first) as $kind | + if $kind != "" then "(\($kind))\($symbol)" else $symbol end; + +def oneline_description: split("\n")[0]; + +def parse_property: + .key as $key | + (.value.description | oneline_description) as $description | + (if $no_type != "1" then (. | get_type) else "" end) as $type | + " \($key)\($type): \($description)"; + +def print_params: + .parameters | + .required as $requiredProperties | + .properties | to_entries[] | + .key as $key | .+ { "required": ($requiredProperties | index($key) != null) } | + parse_property; + +def print_title: + (.description | oneline_description) as $description | + "\(.name): \($description)"; + +print_title, print_params +' +} + +_generate_json() { + jq -r -c ' +def convert_string: + if has("enum") then .enum[0] else "foo" end; + +def parse_property: + .key as $key | + .value.type as $type | + [ + { condition: ($type == "string"), result: { $key: (.value | convert_string) }}, + { condition: ($type == "boolean"), result: { $key: false }}, + { condition: ($type == "integer"), result: { $key: 42 }}, + { condition: ($type == "number"), result: { $key: 3.14 }}, + { condition: ($type == "array"), result: { $key: [ "v1" ] } } + ] | map(select(.condition) | .result) | first; + +.name, +( + .parameters | + [ + .properties | to_entries[] | parse_property + ] | add // {} +) +' +} + +# See more details at https://github.com/sigoden/argc +eval "$(argc --argc-eval "$0" "$@")" |
