diff options
Diffstat (limited to 'cmd')
| -rwxr-xr-x | cmd/cmd.js | 53 | ||||
| -rwxr-xr-x | cmd/cmd.py | 55 | ||||
| -rwxr-xr-x | cmd/cmd.rb | 48 | ||||
| -rwxr-xr-x | cmd/cmd.sh | 91 |
4 files changed, 193 insertions, 54 deletions
@@ -1,36 +1,55 @@ #!/usr/bin/env node -function loadModule() { - const path = require("path"); - let func_name = process.argv[1]; - if (func_name.endsWith("cmd.js")) { - func_name = process.argv[2] +const path = require("path"); + +function parseArgv() { + let func_file = process.argv[1]; + let func_data = null; + + if (func_file.endsWith("cmd.js")) { + func_file = process.argv[2] + func_data = process.argv[3] } else { - func_name = path.basename(func_name) + func_file = path.basename(func_file) + func_data = process.argv[2]; } - if (!func_name.endsWith(".js")) { - func_name += '.js' + + if (!func_file.endsWith(".js")) { + func_file += '.js' } - const func_path = path.resolve(__dirname, `../js/${func_name}`) + + return [func_file, func_data] +} + +function loadFunc(func_file) { + const func_path = path.resolve(__dirname, `../js/${func_file}`) try { return require(func_path); } catch { - console.log(`Invalid js function: ${func_name}`) + console.log(`Invalid function: ${func_file}`) process.exit(1) } } -if (process.env["LLM_FUNCTION_DECLARATE"]) { - const { declarate } = loadModule(); +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 { - let data = null; + if (!func_data) { + console.log("No json data"); + process.exit(1) + } + + let args; try { - data = JSON.parse(process.env["LLM_FUNCTION_DATA"]) + args = JSON.parse(func_data) } catch { - console.log("Invalid LLM_FUNCTION_DATA") + console.log("Invalid json data") process.exit(1) } - const { execute } = loadModule(); - execute(data) + + const { execute } = loadFunc(func_file); + execute(args) }
\ No newline at end of file @@ -5,39 +5,50 @@ import json import sys import importlib.util -def load_module(func_name): +def parse_argv(): + func_file = sys.argv[0] + func_data = None + + if func_file.endswith("cmd.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): base_dir = os.path.dirname(os.path.abspath(__file__)) - func_path = os.path.join(base_dir, f"../py/{func_name}") + func_path = os.path.join(base_dir, f"../py/{func_file}") if os.path.exists(func_path): - spec = importlib.util.spec_from_file_location(func_name, 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 py function: {func_name}") + print(f"Invalid function: {func_file}") sys.exit(1) -func_name = sys.argv[0] -if func_name.endswith("cmd.py"): - func_name = sys.argv[1] -else: - func_name = os.path.basename(func_name) - -if not func_name.endswith(".py"): - func_name += ".py" +func_file, func_data = parse_argv() -if os.getenv("LLM_FUNCTION_DECLARATE"): - module = load_module(func_name) - declarate = getattr(module, 'declarate') - print(json.dumps(declarate(), indent=2)) +if os.getenv("LLM_FUNCTION_ACTION") == "declarate": + module = load_func(func_file) + print(json.dumps(module.declarate(), indent=2)) else: - data = None + if not func_data: + print("No json data") + sys.exit(1) + + args = None try: - data = json.loads(os.getenv("LLM_FUNCTION_DATA")) + args = json.loads(func_data) except (json.JSONDecodeError, TypeError): - print("Invalid LLM_FUNCTION_DATA") + print("Invalid json data") sys.exit(1) - module = load_module(func_name) - execute = getattr(module, 'execute') - execute(data)
\ No newline at end of file + module = load_func(func_file) + module.execute(args)
\ No newline at end of file @@ -3,34 +3,52 @@ require 'json' require 'pathname' -def load_module - if __FILE__.end_with?("cmd.rb") - func_name = ARGV[0] +def parse_argv + func_file = __FILE__ + func_data = nil + + if func_file.end_with?("cmd.rb") + func_file = ARGV[0] + func_data = ARGV[1] else - func_name = Pathname.new(__FILE__).basename.to_s + func_file = File.basename(func_file) + func_data = ARGV[0] end - func_name += '.rb' unless func_name.end_with?('.rb') - func_path = File.expand_path("../rb/#{func_name}", __dir__) + func_file += '.rb' unless func_file.end_with?(".rb") + + [func_file, func_data] +end + +def load_func(func_file) + func_path = File.expand_path("../rb/#{func_file}", __dir__) begin - return require_relative func_path + require func_path rescue LoadError - puts "Invalid ruby function: #{func_name}" + puts "Invalid function: #{func_file}" exit 1 end end -if ENV["LLM_FUNCTION_DECLARATE"] - declarate = load_module.method(:declarate) - puts JSON.pretty_generate(declarate.call) +func_file, func_data = parse_argv + +if ENV["LLM_FUNCTION_ACTION"] == "declarate" + load_func(func_file) + puts JSON.pretty_generate(declarate) else + if func_data.nil? + puts "No json data" + exit 1 + end + begin - data = JSON.parse(ENV["LLM_FUNCTION_DATA"]) + args = JSON.parse(func_data) rescue JSON::ParserError - puts "Invalid LLM_FUNCTION_DATA" + puts "Invalid json data" exit 1 end - execute = load_module.method(:execute) - execute.call(data) + + load_func(func_file) + execute(args) end
\ No newline at end of file diff --git a/cmd/cmd.sh b/cmd/cmd.sh new file mode 100755 index 0000000..803be6a --- /dev/null +++ b/cmd/cmd.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +set -e + +if [[ "$0" == *cmd.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 + +PROJECT_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" +FUNC_FILE="$PROJECT_DIR/sh/$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 |
