From a144077aec90d13587d67b3a2065d5728d4f6319 Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 6 Jun 2024 11:40:09 +0800 Subject: refactor: move run/tool.* to scripts/bin.* (#22) --- Argcfile.sh | 10 +++--- run/tool.js | 74 -------------------------------------------- run/tool.py | 70 ------------------------------------------ run/tool.sh | 96 ---------------------------------------------------------- scripts/bin.js | 74 ++++++++++++++++++++++++++++++++++++++++++++ scripts/bin.py | 70 ++++++++++++++++++++++++++++++++++++++++++ scripts/bin.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 245 insertions(+), 245 deletions(-) delete mode 100755 run/tool.js delete mode 100755 run/tool.py delete mode 100755 run/tool.sh create mode 100755 scripts/bin.js create mode 100755 scripts/bin.py create mode 100755 scripts/bin.sh diff --git a/Argcfile.sh b/Argcfile.sh index a57debd..8c3c50c 100644 --- a/Argcfile.sh +++ b/Argcfile.sh @@ -52,7 +52,7 @@ build-bin() { _build_win_shim $lang > "$bin_file" else bin_file="$BIN_DIR/$basename" - ln -s "$PWD/run/tool.$lang" "$bin_file" + ln -s "$PWD/scripts/bin.$lang" "$bin_file" fi else not_found_funcs+=("$name") @@ -111,7 +111,7 @@ build-single-declaration() { func="$1" lang="${func##*.}" cmd="$(_lang_to_cmd "$lang")" - LLM_FUNCTION_ACTION=declarate "$cmd" "run/tool.$lang" "$func" + LLM_FUNCTION_ACTION=declarate "$cmd" "scripts/bin.$lang" "$func" } # @cmd List functions that can be put into functions.txt @@ -152,8 +152,8 @@ test-functions() { echo "Test $cmd_path: $(cat)" } if ! _is_win; then - "$cmd" "run/tool.$lang" "$func" "$data" | { - echo "Test $cmd run/tool.$lang $func: $(cat)" + "$cmd" "scripts/bin.$lang" "$func" "$data" | { + echo "Test $cmd scripts/bin.$lang $func: $(cat)" } fi fi @@ -174,7 +174,7 @@ install() { fi } -# @cmd Show versions of required tools for bug reports. +# @cmd Show pre-requisite tool versions version() { uname -a argc --argc-version diff --git a/run/tool.js b/run/tool.js deleted file mode 100755 index 3c8e966..0000000 --- a/run/tool.js +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env node - -const path = require("path"); -const fs = require('fs'); - -function parseArgv() { - let func_file = process.argv[1]; - let func_data = null; - - if (func_file.endsWith("tool.js")) { - func_file = process.argv[2] - func_data = process.argv[3] - } else { - func_file = path.basename(func_file) - func_data = process.argv[2]; - } - - if (!func_file.endsWith(".js")) { - func_file += '.js' - } - - return [func_file, func_data] -} - -function loadFunc(func_file) { - const func_path = path.resolve(process.env["LLM_FUNCTIONS_DIR"], `tools/${func_file}`) - try { - return require(func_path); - } catch { - console.log(`Invalid function: ${func_file}`) - process.exit(1) - } -} - -function loadEnv(filePath) { - try { - const data = fs.readFileSync(filePath, 'utf-8'); - const lines = data.split('\n'); - - lines.forEach(line => { - if (line.trim().startsWith('#') || line.trim() === '') return; - - const [key, ...value] = line.split('='); - process.env[key.trim()] = value.join('=').trim(); - }); - } catch {} -} - -process.env["LLM_FUNCTIONS_DIR"] = path.resolve(__dirname, ".."); - -loadEnv(path.resolve(process.env["LLM_FUNCTIONS_DIR"], ".env")); - -const [func_file, func_data] = parseArgv(); - -if (process.env["LLM_FUNCTION_ACTION"] == "declarate") { - const { declarate } = loadFunc(func_file); - console.log(JSON.stringify(declarate(), null, 2)) -} else { - if (!func_data) { - console.log("No json data"); - process.exit(1) - } - - let args; - try { - args = JSON.parse(func_data) - } catch { - console.log("Invalid json data") - process.exit(1) - } - - const { execute } = loadFunc(func_file); - execute(args) -} \ No newline at end of file diff --git a/run/tool.py b/run/tool.py deleted file mode 100755 index d986692..0000000 --- a/run/tool.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python - -import os -import json -import sys -import importlib.util - -def parse_argv(): - func_file = sys.argv[0] - func_data = None - - if func_file.endswith("tool.py"): - func_file = sys.argv[1] if len(sys.argv) > 1 else None - func_data = sys.argv[2] if len(sys.argv) > 2 else None - else: - func_file = os.path.basename(func_file) - func_data = sys.argv[1] if len(sys.argv) > 1 else None - - if not func_file.endswith(".py"): - func_file += ".py" - - return func_file, func_data - -def load_func(func_file): - func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file}") - if os.path.exists(func_path): - spec = importlib.util.spec_from_file_location(func_file, func_path) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - else: - print(f"Invalid function: {func_file}") - sys.exit(1) - -def load_env(file_path): - try: - with open(file_path, 'r') as f: - for line in f: - line = line.strip() - if line.startswith('#') or line == '': - continue - - key, *value = line.split('=') - os.environ[key.strip()] = '='.join(value).strip() - except FileNotFoundError: - pass - -os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..") - -load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env")) - -func_file, func_data = parse_argv() - -if os.getenv("LLM_FUNCTION_ACTION") == "declarate": - module = load_func(func_file) - print(json.dumps(module.declarate(), indent=2)) -else: - if not func_data: - print("No json data") - sys.exit(1) - - args = None - try: - args = json.loads(func_data) - except (json.JSONDecodeError, TypeError): - print("Invalid json data") - sys.exit(1) - - module = load_func(func_file) - module.execute(args) \ No newline at end of file diff --git a/run/tool.sh b/run/tool.sh deleted file mode 100755 index faafddd..0000000 --- a/run/tool.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env bash -set -e - -export LLM_FUNCTIONS_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" - -if [[ -f "$LLM_FUNCTIONS_DIR/.env" ]]; then - source "$LLM_FUNCTIONS_DIR/.env" -fi - -if [[ "$0" == *tool.sh ]]; then - FUNC_FILE="$1" - FUNC_DATA="$2" -else - FUNC_FILE="$(basename "$0")" - FUNC_DATA="$1" -fi -if [[ "$FUNC_FILE" != *'.sh' ]]; then - FUNC_FILE="$FUNC_FILE.sh" -fi - -FUNC_FILE="$LLM_FUNCTIONS_DIR/tools/$FUNC_FILE" - -if [[ "$OS" == "Windows_NT" ]]; then - FUNC_FILE="$(cygpath -w "$FUNC_FILE")" -fi - -if [[ "$LLM_FUNCTION_ACTION" == "declarate" ]]; then - argc --argc-export "$FUNC_FILE" | \ - jq -r ' - def parse_description(flag_option): - if flag_option.describe == "" then - {} - else - { "description": flag_option.describe } - end; - - def parse_enum(flag_option): - if flag_option.choice.type == "Values" then - { "enum": flag_option.choice.data } - else - {} - end; - - def parse_property(flag_option): - [ - { condition: (flag_option.flag == true), result: { type: "boolean" } }, - { condition: (flag_option.multiple_occurs == true), result: { type: "array", items: { type: "string" } } }, - { condition: (flag_option.notations[0] == "INT"), result: { type: "integer" } }, - { condition: (flag_option.notations[0] == "NUM"), result: { type: "number" } }, - { condition: true, result: { type: "string" } } ] - | map(select(.condition) | .result) | first - | (. + parse_description(flag_option)) - | (. + parse_enum(flag_option)) - ; - - - def parse_parameter(flag_options): - { - type: "object", - properties: (reduce flag_options[] as $item ({}; . + { ($item.id | sub("-"; "_"; "g")): parse_property($item) })), - required: [flag_options[] | select(.required == true) | .id], - }; - - { - name: (.name | sub("-"; "_"; "g")), - description: .describe, - parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")]) - }' -else - if [[ -z "$FUNC_DATA" ]]; then - echo "No json data" - exit 1 - fi - - data="$( - echo "$FUNC_DATA" | \ - jq -r ' - to_entries | .[] | - (.key | split("_") | join("-")) as $key | - if .value | type == "array" then - .value | .[] | "--\($key)\n\(.)" - elif .value | type == "boolean" then - if .value then "--\($key)" else "" end - else - "--\($key)\n\(.value)" - end' | \ - tr -d '\r' - )" || { - echo "Invalid json data" - exit 1 - } - while IFS= read -r line; do - ARGS+=("$line") - done <<< "$data" - "$FUNC_FILE" "${ARGS[@]}" -fi \ No newline at end of file diff --git a/scripts/bin.js b/scripts/bin.js new file mode 100755 index 0000000..22005ee --- /dev/null +++ b/scripts/bin.js @@ -0,0 +1,74 @@ +#!/usr/bin/env node + +const path = require("path"); +const fs = require('fs'); + +function parseArgv() { + let func_file = process.argv[1]; + let func_data = null; + + if (func_file.endsWith("bin.js")) { + func_file = process.argv[2] + func_data = process.argv[3] + } else { + func_file = path.basename(func_file) + func_data = process.argv[2]; + } + + if (!func_file.endsWith(".js")) { + func_file += '.js' + } + + return [func_file, func_data] +} + +function loadFunc(func_file) { + const func_path = path.resolve(process.env["LLM_FUNCTIONS_DIR"], `tools/${func_file}`) + try { + return require(func_path); + } catch { + console.log(`Invalid function: ${func_file}`) + process.exit(1) + } +} + +function loadEnv(filePath) { + try { + const data = fs.readFileSync(filePath, 'utf-8'); + const lines = data.split('\n'); + + lines.forEach(line => { + if (line.trim().startsWith('#') || line.trim() === '') return; + + const [key, ...value] = line.split('='); + process.env[key.trim()] = value.join('=').trim(); + }); + } catch {} +} + +process.env["LLM_FUNCTIONS_DIR"] = path.resolve(__dirname, ".."); + +loadEnv(path.resolve(process.env["LLM_FUNCTIONS_DIR"], ".env")); + +const [func_file, func_data] = parseArgv(); + +if (process.env["LLM_FUNCTION_ACTION"] == "declarate") { + const { declarate } = loadFunc(func_file); + console.log(JSON.stringify(declarate(), null, 2)) +} else { + if (!func_data) { + console.log("No json data"); + process.exit(1) + } + + let args; + try { + args = JSON.parse(func_data) + } catch { + console.log("Invalid json data") + process.exit(1) + } + + const { execute } = loadFunc(func_file); + execute(args) +} \ No newline at end of file diff --git a/scripts/bin.py b/scripts/bin.py new file mode 100755 index 0000000..6c56ea0 --- /dev/null +++ b/scripts/bin.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +import os +import json +import sys +import importlib.util + +def parse_argv(): + func_file = sys.argv[0] + func_data = None + + if func_file.endswith("bin.py"): + func_file = sys.argv[1] if len(sys.argv) > 1 else None + func_data = sys.argv[2] if len(sys.argv) > 2 else None + else: + func_file = os.path.basename(func_file) + func_data = sys.argv[1] if len(sys.argv) > 1 else None + + if not func_file.endswith(".py"): + func_file += ".py" + + return func_file, func_data + +def load_func(func_file): + func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file}") + if os.path.exists(func_path): + spec = importlib.util.spec_from_file_location(func_file, func_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + else: + print(f"Invalid function: {func_file}") + sys.exit(1) + +def load_env(file_path): + try: + with open(file_path, 'r') as f: + for line in f: + line = line.strip() + if line.startswith('#') or line == '': + continue + + key, *value = line.split('=') + os.environ[key.strip()] = '='.join(value).strip() + except FileNotFoundError: + pass + +os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..") + +load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env")) + +func_file, func_data = parse_argv() + +if os.getenv("LLM_FUNCTION_ACTION") == "declarate": + module = load_func(func_file) + print(json.dumps(module.declarate(), indent=2)) +else: + if not func_data: + print("No json data") + sys.exit(1) + + args = None + try: + args = json.loads(func_data) + except (json.JSONDecodeError, TypeError): + print("Invalid json data") + sys.exit(1) + + module = load_func(func_file) + module.execute(args) \ No newline at end of file diff --git a/scripts/bin.sh b/scripts/bin.sh new file mode 100755 index 0000000..77a6605 --- /dev/null +++ b/scripts/bin.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +set -e + +export LLM_FUNCTIONS_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" + +if [[ -f "$LLM_FUNCTIONS_DIR/.env" ]]; then + source "$LLM_FUNCTIONS_DIR/.env" +fi + +if [[ "$0" == *bin.sh ]]; then + FUNC_FILE="$1" + FUNC_DATA="$2" +else + FUNC_FILE="$(basename "$0")" + FUNC_DATA="$1" +fi +if [[ "$FUNC_FILE" != *'.sh' ]]; then + FUNC_FILE="$FUNC_FILE.sh" +fi + +FUNC_FILE="$LLM_FUNCTIONS_DIR/tools/$FUNC_FILE" + +if [[ "$OS" == "Windows_NT" ]]; then + FUNC_FILE="$(cygpath -w "$FUNC_FILE")" +fi + +if [[ "$LLM_FUNCTION_ACTION" == "declarate" ]]; then + argc --argc-export "$FUNC_FILE" | \ + jq -r ' + def parse_description(flag_option): + if flag_option.describe == "" then + {} + else + { "description": flag_option.describe } + end; + + def parse_enum(flag_option): + if flag_option.choice.type == "Values" then + { "enum": flag_option.choice.data } + else + {} + end; + + def parse_property(flag_option): + [ + { condition: (flag_option.flag == true), result: { type: "boolean" } }, + { condition: (flag_option.multiple_occurs == true), result: { type: "array", items: { type: "string" } } }, + { condition: (flag_option.notations[0] == "INT"), result: { type: "integer" } }, + { condition: (flag_option.notations[0] == "NUM"), result: { type: "number" } }, + { condition: true, result: { type: "string" } } ] + | map(select(.condition) | .result) | first + | (. + parse_description(flag_option)) + | (. + parse_enum(flag_option)) + ; + + + def parse_parameter(flag_options): + { + type: "object", + properties: (reduce flag_options[] as $item ({}; . + { ($item.id | sub("-"; "_"; "g")): parse_property($item) })), + required: [flag_options[] | select(.required == true) | .id], + }; + + { + name: (.name | sub("-"; "_"; "g")), + description: .describe, + parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")]) + }' +else + if [[ -z "$FUNC_DATA" ]]; then + echo "No json data" + exit 1 + fi + + data="$( + echo "$FUNC_DATA" | \ + jq -r ' + to_entries | .[] | + (.key | split("_") | join("-")) as $key | + if .value | type == "array" then + .value | .[] | "--\($key)\n\(.)" + elif .value | type == "boolean" then + if .value then "--\($key)" else "" end + else + "--\($key)\n\(.value)" + end' | \ + tr -d '\r' + )" || { + echo "Invalid json data" + exit 1 + } + while IFS= read -r line; do + ARGS+=("$line") + done <<< "$data" + "$FUNC_FILE" "${ARGS[@]}" +fi \ No newline at end of file -- cgit v1.2.3