aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/bin.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-06 11:40:09 +0800
committerGitHub <noreply@github.com>2024-06-06 11:40:09 +0800
commita144077aec90d13587d67b3a2065d5728d4f6319 (patch)
treedd5a81e34c160771817e940c49bedb20e02615b4 /scripts/bin.js
parent6f82b1f53af60fccb35aa7a13c13e6d9ebe7796f (diff)
downloadllm-functions-docker-a144077aec90d13587d67b3a2065d5728d4f6319.tar.gz
refactor: move run/tool.* to scripts/bin.* (#22)
Diffstat (limited to 'scripts/bin.js')
-rwxr-xr-xscripts/bin.js74
1 files changed, 74 insertions, 0 deletions
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