aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-tool.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-07 04:06:59 +0800
committerGitHub <noreply@github.com>2024-06-07 04:06:59 +0800
commit2b07fc2c7e4e6311d35ae72c17b25e47680d61f6 (patch)
tree813ea77176ccd89d91aa7b4e70771755eaf2cf41 /scripts/run-tool.js
parent6d1ad27900634bd16089eb3fe847fe9583dcf3d9 (diff)
downloadllm-functions-docker-2b07fc2c7e4e6311d35ae72c17b25e47680d61f6.tar.gz
refactor: rename scripts (#29)
Diffstat (limited to 'scripts/run-tool.js')
-rwxr-xr-xscripts/run-tool.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/scripts/run-tool.js b/scripts/run-tool.js
new file mode 100755
index 0000000..43a6587
--- /dev/null
+++ b/scripts/run-tool.js
@@ -0,0 +1,80 @@
+#!/usr/bin/env node
+
+const path = require("path");
+const fs = require("fs");
+
+function parseArgv() {
+ let funcName = process.argv[1];
+ let funcData = null;
+
+ if (funcName.endsWith("run-tool.js")) {
+ funcName = process.argv[2];
+ funcData = process.argv[3];
+ } else {
+ funcName = path.basename(funcName);
+ funcData = process.argv[2];
+ }
+
+ if (funcName.endsWith(".js")) {
+ funcName = funcName.slice(0, -3);
+ }
+
+ return [funcName, funcData];
+}
+
+function loadFunc(funcName) {
+ const funcFileName = `${funcName}.js`;
+ const funcPath = path.resolve(
+ process.env["LLM_FUNCTIONS_DIR"],
+ `tools/${funcFileName}`,
+ );
+ try {
+ return require(funcPath);
+ } catch {
+ console.log(`Invalid function: ${funcFileName}`);
+ 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 [funcName, funcData] = parseArgv();
+
+process.env["LLM_FUNCTION_NAME"] = funcName;
+
+if (process.env["LLM_FUNCTION_ACTION"] == "declarate") {
+ const { declarate } = loadFunc(funcName);
+ console.log(JSON.stringify(declarate(), null, 2));
+} else {
+ if (!funcData) {
+ console.log("No json data");
+ process.exit(1);
+ }
+
+ let args;
+ try {
+ args = JSON.parse(funcData);
+ } catch {
+ console.log("Invalid json data");
+ process.exit(1);
+ }
+
+ const { execute } = loadFunc(funcName);
+ execute(args);
+}