aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/run-bot.js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-08 20:46:27 +0800
committerGitHub <noreply@github.com>2024-06-08 20:46:27 +0800
commit213e949fc8c2362046d197554fda98c87a7906df (patch)
tree595a883c6b1fd1154a916569c3d0255634c6e193 /scripts/run-bot.js
parent82d7a7de8a76e56cff306b0da7f4f14fdb57cbf1 (diff)
downloadllm-functions-docker-213e949fc8c2362046d197554fda98c87a7906df.tar.gz
feat: support bots (#39)
Diffstat (limited to 'scripts/run-bot.js')
-rwxr-xr-xscripts/run-bot.js107
1 files changed, 107 insertions, 0 deletions
diff --git a/scripts/run-bot.js b/scripts/run-bot.js
new file mode 100755
index 0000000..7a5b70c
--- /dev/null
+++ b/scripts/run-bot.js
@@ -0,0 +1,107 @@
+#!/usr/bin/env node
+
+const path = require("path");
+const fs = require("fs");
+const os = require("os");
+
+async function main() {
+ const [botName, botFunc, rawData] = parseArgv("run-bot.js");
+ const botData = parseRawData(rawData);
+
+ const rootDir = path.resolve(__dirname, "..");
+ setupEnv(rootDir, botName);
+
+ const botToolsPath = path.resolve(rootDir, `bots/${botName}/tools.js`);
+ await run(botToolsPath, botFunc, botData);
+}
+
+function parseArgv(thisFileName) {
+ let botName = process.argv[1];
+ let botFunc = "";
+ let botData = null;
+
+ if (botName.endsWith(thisFileName)) {
+ botName = process.argv[2];
+ botFunc = process.argv[3];
+ botData = process.argv[4];
+ } else {
+ botName = path.basename(botName);
+ botFunc = process.argv[2];
+ botData = process.argv[3];
+ }
+
+ if (botName.endsWith(".js")) {
+ botName = botName.slice(0, -3);
+ }
+
+ return [botName, botFunc, botData];
+}
+
+function parseRawData(data) {
+ if (!data) {
+ throw new Error("No JSON data");
+ }
+ try {
+ return JSON.parse(data);
+ } catch {
+ throw new Error("Invalid JSON data");
+ }
+}
+
+function setupEnv(rootDir, botName) {
+ process.env["LLM_ROOT_DIR"] = rootDir;
+ loadEnv(path.resolve(rootDir, ".env"));
+ process.env["LLM_BOT_NAME"] = botName;
+ process.env["LLM_BOT_ROOT_DIR"] = path.resolve(rootDir, "bots", botName);
+ process.env["LLM_BOT_CACHE_DIR"] = path.resolve(rootDir, "cache", botName);
+}
+
+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 {}
+}
+
+async function run(botPath, botFunc, botData) {
+ let mod;
+ if (os.platform() === "win32") {
+ botPath = `file://${botPath}`;
+ }
+ try {
+ mod = await import(botPath);
+ } catch {
+ throw new Error(`Unable to load bot tools at '${botPath}'`);
+ }
+ if (!mod || !mod[botFunc]) {
+ throw new Error(`Not module function '${botFunc}' at '${botPath}'`);
+ }
+ const value = await mod[botFunc](botData);
+ dumpValue(value);
+}
+
+function dumpValue(value) {
+ if (value === null || value === undefined) {
+ return;
+ }
+ const type = typeof value;
+ if (type === "string" || type === "number" || type === "boolean") {
+ console.log(value);
+ } else if (type === "object") {
+ const proto = Object.prototype.toString.call(value);
+ if (proto === "[object Object]" || proto === "[object Array]") {
+ const valueStr = JSON.stringify(value, null, 2);
+ require("assert").deepStrictEqual(value, JSON.parse(valueStr));
+ console.log(valueStr);
+ }
+ }
+}
+
+main();