refactor: move run/tool.* to scripts/bin.* (#22)

This commit is contained in:
sigoden
2024-06-06 11:40:09 +08:00
committed by GitHub
parent 6f82b1f53a
commit a144077aec
4 changed files with 8 additions and 8 deletions
Executable
+74
View File
@@ -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)
}
Executable
+70
View File
@@ -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)
Executable
+96
View File
@@ -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