aboutsummaryrefslogtreecommitdiffstats
path: root/run/tool.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-05 18:37:52 +0800
committerGitHub <noreply@github.com>2024-06-05 18:37:52 +0800
commit005d23030dab2b7708996e779dc78dd5f8ebdb8d (patch)
tree0d05583d462a4795a4236a5006befc096047e969 /run/tool.js
parent7e3f47093f2dcb36cf94a35403027ec72bf7b084 (diff)
downloadllm-functions-docker-005d23030dab2b7708996e779dc78dd5f8ebdb8d.tar.gz
feat: adjust project structure (#16)
Diffstat (limited to 'run/tool.js')
-rwxr-xr-xrun/tool.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/run/tool.js b/run/tool.js
new file mode 100755
index 0000000..de97423
--- /dev/null
+++ b/run/tool.js
@@ -0,0 +1,55 @@
+#!/usr/bin/env node
+
+const path = require("path");
+
+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(__dirname, `../tools/js/${func_file}`)
+ try {
+ return require(func_path);
+ } catch {
+ console.log(`Invalid function: ${func_file}`)
+ process.exit(1)
+ }
+}
+
+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