From adfb7c2b49ba4ba691e89683afa700eabbb3388c Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 22 Jun 2024 06:52:45 +0800 Subject: refactor: rename bot to agent (#44) --- scripts/run-agent.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/run-agent.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/run-agent.sh | 80 ++++++++++++++++++++++++++++++++++++++ scripts/run-bot.js | 107 --------------------------------------------------- scripts/run-bot.py | 105 -------------------------------------------------- scripts/run-bot.sh | 80 -------------------------------------- 6 files changed, 292 insertions(+), 292 deletions(-) create mode 100755 scripts/run-agent.js create mode 100755 scripts/run-agent.py create mode 100755 scripts/run-agent.sh delete mode 100755 scripts/run-bot.js delete mode 100755 scripts/run-bot.py delete mode 100755 scripts/run-bot.sh (limited to 'scripts') diff --git a/scripts/run-agent.js b/scripts/run-agent.js new file mode 100755 index 0000000..69dd119 --- /dev/null +++ b/scripts/run-agent.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 [agentName, agentFunc, rawData] = parseArgv("run-agent.js"); + const agentData = parseRawData(rawData); + + const rootDir = path.resolve(__dirname, ".."); + setupEnv(rootDir, agentName); + + const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`); + await run(agentToolsPath, agentFunc, agentData); +} + +function parseArgv(thisFileName) { + let agentName = process.argv[1]; + let agentFunc = ""; + let agentData = null; + + if (agentName.endsWith(thisFileName)) { + agentName = process.argv[2]; + agentFunc = process.argv[3]; + agentData = process.argv[4]; + } else { + agentName = path.basename(agentName); + agentFunc = process.argv[2]; + agentData = process.argv[3]; + } + + if (agentName.endsWith(".js")) { + agentName = agentName.slice(0, -3); + } + + return [agentName, agentFunc, agentData]; +} + +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, agentName) { + process.env["LLM_ROOT_DIR"] = rootDir; + loadEnv(path.resolve(rootDir, ".env")); + process.env["LLM_AGENT_NAME"] = agentName; + process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(rootDir, "agents", agentName); + process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(rootDir, "cache", agentName); +} + +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(agentPath, agentFunc, agentData) { + let mod; + if (os.platform() === "win32") { + agentPath = `file://${agentPath}`; + } + try { + mod = await import(agentPath); + } catch { + throw new Error(`Unable to load agent tools at '${agentPath}'`); + } + if (!mod || !mod[agentFunc]) { + throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`); + } + const value = await mod[agentFunc](agentData); + 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(); diff --git a/scripts/run-agent.py b/scripts/run-agent.py new file mode 100755 index 0000000..1239753 --- /dev/null +++ b/scripts/run-agent.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python + +import os +import json +import sys +import importlib.util + + +def main(): + (agent_name, agent_func, raw_data) = parse_argv("run-agent.py") + agent_data = parse_raw_data(raw_data) + + root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + setup_env(root_dir, agent_name) + + agent_tools_path = os.path.join(root_dir, f"agents/{agent_name}/tools.py") + run(agent_tools_path, agent_func, agent_data) + + +def parse_raw_data(data): + if not data: + raise ValueError("No JSON data") + + try: + return json.loads(data) + except Exception: + raise ValueError("Invalid JSON data") + + +def parse_argv(this_file_name): + argv = sys.argv[:] + [None] * max(0, 4 - len(sys.argv)) + + agent_name = argv[0] + agent_func = "" + agent_data = None + + if agent_name.endswith(this_file_name): + agent_name = sys.argv[1] + agent_func = sys.argv[2] + agent_data = sys.argv[3] + else: + agent_name = os.path.basename(agent_name) + agent_func = sys.argv[1] + agent_data = sys.argv[2] + + if agent_name.endswith(".py"): + agent_name = agent_name[:-3] + + return agent_name, agent_func, agent_data + + +def setup_env(root_dir, agent_name): + os.environ["LLM_ROOT_DIR"] = root_dir + load_env(os.path.join(root_dir, ".env")) + os.environ["LLM_AGENT_NAME"] = agent_name + os.environ["LLM_AGENT_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name) + os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name) + + +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 + + +def run(agent_path, agent_func, agent_data): + try: + spec = importlib.util.spec_from_file_location( + os.path.basename(agent_path), agent_path + ) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + except: + raise Exception(f"Unable to load agent tools at '{agent_path}'") + + if not hasattr(mod, agent_func): + raise Exception(f"Not module function '{agent_func}' at '{agent_path}'") + + value = getattr(mod, agent_func)(**agent_data) + dump_value(value) + + +def dump_value(value): + if value is None: + return + + value_type = type(value).__name__ + if value_type in ("str", "int", "float", "bool"): + print(value) + elif value_type == "dict" or value_type == "list": + value_str = json.dumps(value, indent=2) + assert value == json.loads(value_str) + print(value_str) + + +if __name__ == "__main__": + main() diff --git a/scripts/run-agent.sh b/scripts/run-agent.sh new file mode 100755 index 0000000..0bc2ecd --- /dev/null +++ b/scripts/run-agent.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +set -e + +main() { + this_file_name=run-agent.sh + parse_argv "$@" + root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" + setup_env + agent_tools_path="$root_dir/agents/$agent_name/tools.sh" + run +} + +parse_argv() { + if [[ "$0" == *"$this_file_name" ]]; then + agent_name="$1" + agent_func="$2" + agent_data="$3" + else + agent_name="$(basename "$0")" + agent_func="$1" + agent_data="$2" + fi + if [[ "$agent_name" == *.sh ]]; then + agent_name="${agent_name:0:$((${#agent_name}-3))}" + fi +} + +setup_env() { + export LLM_ROOT_DIR="$root_dir" + if [[ -f "$LLM_ROOT_DIR/.env" ]]; then + source "$LLM_ROOT_DIR/.env" + fi + export LLM_AGENT_NAME="$agent_name" + export LLM_AGENT_ROOT_DIR="$LLM_ROOT_DIR/agents/$agent_name" + export LLM_AGENT_CACHE_DIR="$LLM_ROOT_DIR/cache/$agent_name" +} + +run() { + if [[ -z "$agent_data" ]]; then + die "No JSON data" + fi + + _jq=jq + if [[ "$OS" == "Windows_NT" ]]; then + _jq="jq -b" + agent_tools_path="$(cygpath -w "$agent_tools_path")" + fi + + data="$( + echo "$agent_data" | \ + $_jq -r ' + to_entries | .[] | + (.key | split("_") | join("-")) as $key | + if .value | type == "array" then + .value | .[] | "--\($key)\n\(. | @json)" + elif .value | type == "boolean" then + if .value then "--\($key)" else "" end + else + "--\($key)\n\(.value | @json)" + end' + )" || { + die "Invalid JSON data" + } + while IFS= read -r line; do + if [[ "$line" == '--'* ]]; then + args+=("$line") + else + args+=("$(echo "$line" | $_jq -r '.')") + fi + done <<< "$data" + "$agent_tools_path" "$agent_func" "${args[@]}" +} + +die() { + echo "$*" >&2 + exit 1 +} + +main "$@" + diff --git a/scripts/run-bot.js b/scripts/run-bot.js deleted file mode 100755 index 7a5b70c..0000000 --- a/scripts/run-bot.js +++ /dev/null @@ -1,107 +0,0 @@ -#!/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(); diff --git a/scripts/run-bot.py b/scripts/run-bot.py deleted file mode 100755 index d7c7ae1..0000000 --- a/scripts/run-bot.py +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/bin/env python - -import os -import json -import sys -import importlib.util - - -def main(): - (bot_name, bot_func, raw_data) = parse_argv("run-bot.py") - bot_data = parse_raw_data(raw_data) - - root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) - setup_env(root_dir, bot_name) - - bot_tools_path = os.path.join(root_dir, f"bots/{bot_name}/tools.py") - run(bot_tools_path, bot_func, bot_data) - - -def parse_raw_data(data): - if not data: - raise ValueError("No JSON data") - - try: - return json.loads(data) - except Exception: - raise ValueError("Invalid JSON data") - - -def parse_argv(this_file_name): - argv = sys.argv[:] + [None] * max(0, 4 - len(sys.argv)) - - bot_name = argv[0] - bot_func = "" - bot_data = None - - if bot_name.endswith(this_file_name): - bot_name = sys.argv[1] - bot_func = sys.argv[2] - bot_data = sys.argv[3] - else: - bot_name = os.path.basename(bot_name) - bot_func = sys.argv[1] - bot_data = sys.argv[2] - - if bot_name.endswith(".py"): - bot_name = bot_name[:-3] - - return bot_name, bot_func, bot_data - - -def setup_env(root_dir, bot_name): - os.environ["LLM_ROOT_DIR"] = root_dir - load_env(os.path.join(root_dir, ".env")) - os.environ["LLM_BOT_NAME"] = bot_name - os.environ["LLM_BOT_ROOT_DIR"] = os.path.join(root_dir, "bots", bot_name) - os.environ["LLM_BOT_CACHE_DIR"] = os.path.join(root_dir, "cache", bot_name) - - -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 - - -def run(bot_path, bot_func, bot_data): - try: - spec = importlib.util.spec_from_file_location( - os.path.basename(bot_path), bot_path - ) - mod = importlib.util.module_from_spec(spec) - spec.loader.exec_module(mod) - except: - raise Exception(f"Unable to load bot tools at '{bot_path}'") - - if not hasattr(mod, bot_func): - raise Exception(f"Not module function '{bot_func}' at '{bot_path}'") - - value = getattr(mod, bot_func)(**bot_data) - dump_value(value) - - -def dump_value(value): - if value is None: - return - - value_type = type(value).__name__ - if value_type in ("str", "int", "float", "bool"): - print(value) - elif value_type == "dict" or value_type == "list": - value_str = json.dumps(value, indent=2) - assert value == json.loads(value_str) - print(value_str) - - -if __name__ == "__main__": - main() diff --git a/scripts/run-bot.sh b/scripts/run-bot.sh deleted file mode 100755 index a709453..0000000 --- a/scripts/run-bot.sh +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env bash -set -e - -main() { - this_file_name=run-bot.sh - parse_argv "$@" - root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" - setup_env - bot_tools_path="$root_dir/bots/$bot_name/tools.sh" - run -} - -parse_argv() { - if [[ "$0" == *"$this_file_name" ]]; then - bot_name="$1" - bot_func="$2" - bot_data="$3" - else - bot_name="$(basename "$0")" - bot_func="$1" - bot_data="$2" - fi - if [[ "$bot_name" == *.sh ]]; then - bot_name="${bot_name:0:$((${#bot_name}-3))}" - fi -} - -setup_env() { - export LLM_ROOT_DIR="$root_dir" - if [[ -f "$LLM_ROOT_DIR/.env" ]]; then - source "$LLM_ROOT_DIR/.env" - fi - export LLM_BOT_NAME="$bot_name" - export LLM_BOT_ROOT_DIR="$LLM_ROOT_DIR/bots/$bot_name" - export LLM_BOT_CACHE_DIR="$LLM_ROOT_DIR/cache/$bot_name" -} - -run() { - if [[ -z "$bot_data" ]]; then - die "No JSON data" - fi - - _jq=jq - if [[ "$OS" == "Windows_NT" ]]; then - _jq="jq -b" - bot_tools_path="$(cygpath -w "$bot_tools_path")" - fi - - data="$( - echo "$bot_data" | \ - $_jq -r ' - to_entries | .[] | - (.key | split("_") | join("-")) as $key | - if .value | type == "array" then - .value | .[] | "--\($key)\n\(. | @json)" - elif .value | type == "boolean" then - if .value then "--\($key)" else "" end - else - "--\($key)\n\(.value | @json)" - end' - )" || { - die "Invalid JSON data" - } - while IFS= read -r line; do - if [[ "$line" == '--'* ]]; then - args+=("$line") - else - args+=("$(echo "$line" | $_jq -r '.')") - fi - done <<< "$data" - "$bot_tools_path" "$bot_func" "${args[@]}" -} - -die() { - echo "$*" >&2 - exit 1 -} - -main "$@" - -- cgit v1.2.3