refactor: rename bot to agent (#44)
This commit is contained in:
@@ -5,36 +5,36 @@ const fs = require("fs");
|
||||
const os = require("os");
|
||||
|
||||
async function main() {
|
||||
const [botName, botFunc, rawData] = parseArgv("run-bot.js");
|
||||
const botData = parseRawData(rawData);
|
||||
const [agentName, agentFunc, rawData] = parseArgv("run-agent.js");
|
||||
const agentData = parseRawData(rawData);
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..");
|
||||
setupEnv(rootDir, botName);
|
||||
setupEnv(rootDir, agentName);
|
||||
|
||||
const botToolsPath = path.resolve(rootDir, `bots/${botName}/tools.js`);
|
||||
await run(botToolsPath, botFunc, botData);
|
||||
const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`);
|
||||
await run(agentToolsPath, agentFunc, agentData);
|
||||
}
|
||||
|
||||
function parseArgv(thisFileName) {
|
||||
let botName = process.argv[1];
|
||||
let botFunc = "";
|
||||
let botData = null;
|
||||
let agentName = process.argv[1];
|
||||
let agentFunc = "";
|
||||
let agentData = null;
|
||||
|
||||
if (botName.endsWith(thisFileName)) {
|
||||
botName = process.argv[2];
|
||||
botFunc = process.argv[3];
|
||||
botData = process.argv[4];
|
||||
if (agentName.endsWith(thisFileName)) {
|
||||
agentName = process.argv[2];
|
||||
agentFunc = process.argv[3];
|
||||
agentData = process.argv[4];
|
||||
} else {
|
||||
botName = path.basename(botName);
|
||||
botFunc = process.argv[2];
|
||||
botData = process.argv[3];
|
||||
agentName = path.basename(agentName);
|
||||
agentFunc = process.argv[2];
|
||||
agentData = process.argv[3];
|
||||
}
|
||||
|
||||
if (botName.endsWith(".js")) {
|
||||
botName = botName.slice(0, -3);
|
||||
if (agentName.endsWith(".js")) {
|
||||
agentName = agentName.slice(0, -3);
|
||||
}
|
||||
|
||||
return [botName, botFunc, botData];
|
||||
return [agentName, agentFunc, agentData];
|
||||
}
|
||||
|
||||
function parseRawData(data) {
|
||||
@@ -48,12 +48,12 @@ function parseRawData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
function setupEnv(rootDir, botName) {
|
||||
function setupEnv(rootDir, agentName) {
|
||||
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);
|
||||
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) {
|
||||
@@ -70,20 +70,20 @@ function loadEnv(filePath) {
|
||||
} catch {}
|
||||
}
|
||||
|
||||
async function run(botPath, botFunc, botData) {
|
||||
async function run(agentPath, agentFunc, agentData) {
|
||||
let mod;
|
||||
if (os.platform() === "win32") {
|
||||
botPath = `file://${botPath}`;
|
||||
agentPath = `file://${agentPath}`;
|
||||
}
|
||||
try {
|
||||
mod = await import(botPath);
|
||||
mod = await import(agentPath);
|
||||
} catch {
|
||||
throw new Error(`Unable to load bot tools at '${botPath}'`);
|
||||
throw new Error(`Unable to load agent tools at '${agentPath}'`);
|
||||
}
|
||||
if (!mod || !mod[botFunc]) {
|
||||
throw new Error(`Not module function '${botFunc}' at '${botPath}'`);
|
||||
if (!mod || !mod[agentFunc]) {
|
||||
throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
|
||||
}
|
||||
const value = await mod[botFunc](botData);
|
||||
const value = await mod[agentFunc](agentData);
|
||||
dumpValue(value);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ import importlib.util
|
||||
|
||||
|
||||
def main():
|
||||
(bot_name, bot_func, raw_data) = parse_argv("run-bot.py")
|
||||
bot_data = parse_raw_data(raw_data)
|
||||
(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, bot_name)
|
||||
setup_env(root_dir, agent_name)
|
||||
|
||||
bot_tools_path = os.path.join(root_dir, f"bots/{bot_name}/tools.py")
|
||||
run(bot_tools_path, bot_func, bot_data)
|
||||
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):
|
||||
@@ -30,31 +30,31 @@ def parse_raw_data(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
|
||||
agent_name = argv[0]
|
||||
agent_func = ""
|
||||
agent_data = None
|
||||
|
||||
if bot_name.endswith(this_file_name):
|
||||
bot_name = sys.argv[1]
|
||||
bot_func = sys.argv[2]
|
||||
bot_data = sys.argv[3]
|
||||
if agent_name.endswith(this_file_name):
|
||||
agent_name = sys.argv[1]
|
||||
agent_func = sys.argv[2]
|
||||
agent_data = sys.argv[3]
|
||||
else:
|
||||
bot_name = os.path.basename(bot_name)
|
||||
bot_func = sys.argv[1]
|
||||
bot_data = sys.argv[2]
|
||||
agent_name = os.path.basename(agent_name)
|
||||
agent_func = sys.argv[1]
|
||||
agent_data = sys.argv[2]
|
||||
|
||||
if bot_name.endswith(".py"):
|
||||
bot_name = bot_name[:-3]
|
||||
if agent_name.endswith(".py"):
|
||||
agent_name = agent_name[:-3]
|
||||
|
||||
return bot_name, bot_func, bot_data
|
||||
return agent_name, agent_func, agent_data
|
||||
|
||||
|
||||
def setup_env(root_dir, bot_name):
|
||||
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_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)
|
||||
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):
|
||||
@@ -71,20 +71,20 @@ def load_env(file_path):
|
||||
pass
|
||||
|
||||
|
||||
def run(bot_path, bot_func, bot_data):
|
||||
def run(agent_path, agent_func, agent_data):
|
||||
try:
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
os.path.basename(bot_path), bot_path
|
||||
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 bot tools at '{bot_path}'")
|
||||
raise Exception(f"Unable to load agent tools at '{agent_path}'")
|
||||
|
||||
if not hasattr(mod, bot_func):
|
||||
raise Exception(f"Not module function '{bot_func}' at '{bot_path}'")
|
||||
if not hasattr(mod, agent_func):
|
||||
raise Exception(f"Not module function '{agent_func}' at '{agent_path}'")
|
||||
|
||||
value = getattr(mod, bot_func)(**bot_data)
|
||||
value = getattr(mod, agent_func)(**agent_data)
|
||||
dump_value(value)
|
||||
|
||||
|
||||
@@ -2,26 +2,26 @@
|
||||
set -e
|
||||
|
||||
main() {
|
||||
this_file_name=run-bot.sh
|
||||
this_file_name=run-agent.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"
|
||||
agent_tools_path="$root_dir/agents/$agent_name/tools.sh"
|
||||
run
|
||||
}
|
||||
|
||||
parse_argv() {
|
||||
if [[ "$0" == *"$this_file_name" ]]; then
|
||||
bot_name="$1"
|
||||
bot_func="$2"
|
||||
bot_data="$3"
|
||||
agent_name="$1"
|
||||
agent_func="$2"
|
||||
agent_data="$3"
|
||||
else
|
||||
bot_name="$(basename "$0")"
|
||||
bot_func="$1"
|
||||
bot_data="$2"
|
||||
agent_name="$(basename "$0")"
|
||||
agent_func="$1"
|
||||
agent_data="$2"
|
||||
fi
|
||||
if [[ "$bot_name" == *.sh ]]; then
|
||||
bot_name="${bot_name:0:$((${#bot_name}-3))}"
|
||||
if [[ "$agent_name" == *.sh ]]; then
|
||||
agent_name="${agent_name:0:$((${#agent_name}-3))}"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -30,24 +30,24 @@ setup_env() {
|
||||
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"
|
||||
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 "$bot_data" ]]; then
|
||||
if [[ -z "$agent_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")"
|
||||
agent_tools_path="$(cygpath -w "$agent_tools_path")"
|
||||
fi
|
||||
|
||||
data="$(
|
||||
echo "$bot_data" | \
|
||||
echo "$agent_data" | \
|
||||
$_jq -r '
|
||||
to_entries | .[] |
|
||||
(.key | split("_") | join("-")) as $key |
|
||||
@@ -68,7 +68,7 @@ run() {
|
||||
args+=("$(echo "$line" | $_jq -r '.')")
|
||||
fi
|
||||
done <<< "$data"
|
||||
"$bot_tools_path" "$bot_func" "${args[@]}"
|
||||
"$agent_tools_path" "$agent_func" "${args[@]}"
|
||||
}
|
||||
|
||||
die() {
|
||||
Reference in New Issue
Block a user